Advertisement
Guest User

Untitled

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