Advertisement
Guest User

Untitled

a guest
Oct 12th, 2015
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.52 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "bufio"
  5. "os"
  6. "log"
  7. "fmt"
  8. "strconv"
  9. "math"
  10. )
  11.  
  12. func getXcartesian(radius, angle float64) (coordinate float64){
  13. return radius * math.Cos(angle * (math.Pi / 180))
  14. }
  15. func getYcartesian(radius, angle float64) (coordinate float64){
  16. return radius * math.Sin(angle * (math.Pi / 180))
  17. }
  18.  
  19. func getLines(filename string) ([]string, error) {
  20. // open the file and parse out the entites
  21. f, _ := os.Open(filename) // Open the file.
  22. scanner := bufio.NewScanner(f) // Create a new Scanner for the file.
  23. entities := []string{}
  24. for scanner.Scan() { // Loop over all lines in the file
  25. line := scanner.Text()
  26. inEntities := false
  27. if line == "ENTITIES" {
  28. inEntities = true
  29. } else if inEntities {
  30. if line == "ENDSEC" {
  31. inEntities = false
  32. } else {
  33. entities = append(entities, line)
  34. }
  35. }
  36. }
  37. return entities, nil
  38. }
  39.  
  40. func getEnt(list []string, e []Ent) ([]Ent, error){
  41. //add a slice for each entitie and populate the groups for each entitie
  42. // add error handling
  43. group := ""
  44. count := 0
  45. for i := range list {
  46. if len(group) > 0 { // update struct, if group == " 0" append new struct
  47. switch group {
  48. case " 0":
  49. if len(e) > 0 {count++} // increment count for next slice
  50. e = append(e, Ent{})
  51. e[count].G0 = list[i]
  52. case " 8":
  53. e[count].G8 = list[i]
  54. case " 10":
  55. e[count].G10, _ = strconv.ParseFloat(list[i],64)
  56. case " 11":
  57. e[count].G11, _ = strconv.ParseFloat(list[i],64)
  58. case " 20":
  59. e[count].G20, _ = strconv.ParseFloat(list[i],64)
  60. case " 21":
  61. e[count].G21, _ = strconv.ParseFloat(list[i],64)
  62. case " 30":
  63. e[count].G30, _ = strconv.ParseFloat(list[i],64)
  64. case " 31":
  65. e[count].G31, _ = strconv.ParseFloat(list[i],64)
  66. case " 40":
  67. e[count].G40, _ = strconv.ParseFloat(list[i],64)
  68. case " 50":
  69. e[count].G50, _ = strconv.ParseFloat(list[i],64)
  70. case " 51":
  71. e[count].G51, _ = strconv.ParseFloat(list[i],64)
  72. }
  73. //fmt.Println(group, list[i])
  74. group = ""
  75. }
  76. switch list[i] { // trigger when a group is found
  77. case " 0", " 8", " 10", " 11", " 20", " 21",
  78. " 30", " 31", " 40", " 50", " 51":
  79. group = list[i]
  80. }
  81. }
  82. return e, nil
  83. }
  84.  
  85. func findEndPoints (e []Ent) ([]Ent, error){
  86. for i := range e {
  87. switch e[i].G0 {
  88. case "ARC": // get the X and Y points
  89. e[i].Xs = getXcartesian(e[i].G40, e[i].G50) + e[i].G10
  90. e[i].Xe = getXcartesian(e[i].G40, e[i].G51) + e[i].G10
  91. e[i].Ys = getYcartesian(e[i].G40, e[i].G50) + e[i].G20
  92. e[i].Ye = getYcartesian(e[i].G40, e[i].G51) + e[i].G20
  93. case "LINE": // redundant but transfer the X and Y points
  94. e[i].Xs = e[i].G10
  95. e[i].Ys = e[i].G20
  96. e[i].Zs = e[i].G30
  97. e[i].Xe = e[i].G11
  98. e[i].Ye = e[i].G21
  99. e[i].Zs = e[i].G31
  100. case "CIRCLE": // if it is a circle it must be the only entity on that layer
  101. fmt.Println("Circle")
  102. }
  103. }
  104. return e, nil
  105. }
  106.  
  107. func sortEnt(e []Ent) ([]Ent, error){
  108. /*
  109. another variant is to make a map of endpoints, take random element,
  110. and extend it left and right until the map is emptied
  111. i would agree with pmezard. work through all elements building a map,
  112. then iterate through linking together
  113.  
  114. you can do that by sorting on endpoints
  115. you put the start point and end point of each element in a slice and sort
  116. them by x, then y then isStart (so start points come before end points)
  117. */
  118.  
  119. var s []Ent
  120. s = append(s, e[0])
  121. e = append(e[:0], e[1:]...) // delete the first element
  122. xe := s[0].Xe
  123. ye := s[0].Ye
  124. fmt.Println(len(e))
  125. fmt.Println(xe, ye)
  126. for i := 0; len(e) > 0; i++ {
  127. for i := range e {
  128. if e[i].Xs == xe && e[i].Ys == ye {
  129. s = append(s, e[i]) // copy the match
  130. xe = e[i].Xe
  131. ye = e[i].Ye
  132. e = append(e[:i], e[i+1:]...) // delete the match
  133. }
  134. }
  135. if i > 20{break}
  136.  
  137.  
  138. }
  139. for i := range e {
  140. xs := e[i].Xs
  141. xe := e[i].Xe
  142. /*for i := range e {
  143. //fmt.Println(xs, xe, i)
  144. }*/
  145. fmt.Println(xs, xe, i)
  146. }
  147.  
  148. return s, nil
  149. }
  150.  
  151. type Ent struct {
  152. G0, G8 string
  153. G10, G11, G20, G21, G30, G31, G40, G50, G51,
  154. Xs, Xe, Ys, Ye, Zs, Ze float64
  155. }
  156.  
  157. func main(){
  158. lines, err := getLines("test.dxf")
  159. var entities []Ent
  160. if err == nil {
  161. entities, err = getEnt(lines, entities)
  162. entities, err = findEndPoints(entities)
  163. //entities, err = sortEnt(entities)
  164. } else {
  165. log.Fatalf("%v\n", err)
  166. }
  167. //fmt.Println(sorted)
  168. /*
  169. for i := range(entities){
  170. fmt.Print(entities[i].G0, " ")
  171. fmt.Printf("Xs %f Xe %f ", entities[i].Xs, entities[i].Xe)
  172. fmt.Printf("Ys %f Ye %f\n", entities[i].Ys, entities[i].Ye)
  173. }*/
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement