Advertisement
Guest User

Untitled

a guest
Oct 13th, 2015
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "bufio"
  5. "os"
  6. "log"
  7. "fmt"
  8. "strconv"
  9. "math"
  10. "sort"
  11. )
  12.  
  13. func getXcartesian(radius, angle float64) (coordinate float64){
  14. return radius * math.Cos(angle * (math.Pi / 180))
  15. }
  16. func getYcartesian(radius, angle float64) (coordinate float64){
  17. return radius * math.Sin(angle * (math.Pi / 180))
  18. }
  19.  
  20. func getLines(filename string) ([]string) {
  21. lines := []string{}
  22. f, err := os.Open(filename)
  23. defer f.Close()
  24. if err != nil {
  25. fmt.Println(err)
  26. os.Exit(1)
  27. }
  28. scanner := bufio.NewScanner(f)
  29. scanner.Split(bufio.ScanLines)
  30. inEntities := false
  31. // extract all the lines of entities
  32. for scanner.Scan() {
  33. switch scanner.Text() {
  34. case "ENTITIES":
  35. inEntities = true
  36. case "ENDSEC":
  37. inEntities = false
  38. }
  39. if inEntities {
  40. lines = append(lines, scanner.Text())
  41. }
  42. }
  43. return lines
  44. }
  45.  
  46. func getEnt(list []string, e []Ent) ([]Ent){
  47. // add error handling
  48. group := ""
  49. count := 0
  50. for i := range list {
  51. if len(group) > 0 {
  52. switch group {
  53. case " 0":
  54. if len(e) > 0 {count++}
  55. e = append(e, Ent{})
  56. e[count].G0 = list[i]
  57. case " 8":
  58. e[count].G8 = list[i]
  59. case " 10":
  60. e[count].G10, _ = strconv.ParseFloat(list[i],64)
  61. case " 11":
  62. e[count].G11, _ = strconv.ParseFloat(list[i],64)
  63. case " 20":
  64. e[count].G20, _ = strconv.ParseFloat(list[i],64)
  65. case " 21":
  66. e[count].G21, _ = strconv.ParseFloat(list[i],64)
  67. case " 30":
  68. e[count].G30, _ = strconv.ParseFloat(list[i],64)
  69. case " 31":
  70. e[count].G31, _ = strconv.ParseFloat(list[i],64)
  71. case " 40":
  72. e[count].G40, _ = strconv.ParseFloat(list[i],64)
  73. case " 50":
  74. e[count].G50, _ = strconv.ParseFloat(list[i],64)
  75. case " 51":
  76. e[count].G51, _ = strconv.ParseFloat(list[i],64)
  77. }
  78. group = ""
  79. }
  80. switch list[i] { // trigger when a group is found
  81. case " 0", " 8", " 10", " 11", " 20", " 21",
  82. " 30", " 31", " 40", " 50", " 51":
  83. group = list[i]
  84. }
  85. }
  86. return e
  87. }
  88.  
  89. func findEndPoints (e []Ent) ([]Ent){
  90. // add error handling
  91. for i := range e {
  92. switch e[i].G0 {
  93. case "ARC": // get the X and Y end points
  94. e[i].Xs = getXcartesian(e[i].G40, e[i].G50) + e[i].G10
  95. e[i].Xe = getXcartesian(e[i].G40, e[i].G51) + e[i].G10
  96. e[i].Ys = getYcartesian(e[i].G40, e[i].G50) + e[i].G20
  97. e[i].Ye = getYcartesian(e[i].G40, e[i].G51) + e[i].G20
  98. case "LINE":
  99. e[i].Xs = e[i].G10
  100. e[i].Ys = e[i].G20
  101. e[i].Zs = e[i].G30
  102. e[i].Xe = e[i].G11
  103. e[i].Ye = e[i].G21
  104. e[i].Zs = e[i].G31
  105. case "CIRCLE": // if it is a circle it must be the only entity on that layer
  106. fmt.Println("Circle")
  107. }
  108. }
  109. return e
  110. }
  111.  
  112. // find the matching start points for each entity and assign index
  113. func findIndex(s int, t float64, e []Ent) ([]Ent) {
  114. // add error handling
  115. xe := e[s].Xe
  116. ye := e[s].Ye
  117. index := 1
  118. for skip := range e {
  119. if skip != s {
  120. for i := range e {
  121. if math.Abs(e[i].Xs - xe) < t && math.Abs(e[i].Ys - ye) < t {
  122. e[i].Index = index
  123. index++
  124. xe = e[i].Xe
  125. ye = e[i].Ye
  126. break
  127. }
  128. }
  129. }
  130. }
  131. return e
  132. }
  133.  
  134. type Ent struct {
  135. Index int
  136. G0, G8 string
  137. G10, G11, G20, G21, G30, G31, G40, G50, G51,
  138. Xs, Xe, Ys, Ye, Zs, Ze float64
  139. }
  140.  
  141. type ByIndex []Ent
  142.  
  143. func (a ByIndex) Len() int { return len(a) }
  144. func (a ByIndex) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  145. func (a ByIndex) Less(i, j int) bool { return a[i].Index < a[j].Index }
  146.  
  147. func main(){
  148. var file = ""
  149. if len(os.Args) > 1 {
  150. file = os.Args[1]
  151. } else {
  152. file = "test.dxf"
  153. }
  154. var entities []Ent
  155. lines := getLines(file)
  156. entities = getEnt(lines, entities)
  157. entities = findEndPoints(entities)
  158. start := 3
  159. tolerance := 0.0000001
  160. entities = findIndex(start, tolerance, entities)
  161. sort.Sort(ByIndex(entities))
  162.  
  163. for i := range(entities){
  164. fmt.Printf("%d %s %f %f %f %f\n",
  165. entities[i].Index, entities[i].G0, entities[i].Xs,
  166. entities[i].Ys, entities[i].Xe, entities[i].Ye)
  167. }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement