Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "bufio"
- "os"
- "log"
- "fmt"
- "strconv"
- "math"
- )
- func getXcartesian(radius, angle float64) (coordinate float64){
- return radius * math.Cos(angle * (math.Pi / 180))
- }
- func getYcartesian(radius, angle float64) (coordinate float64){
- return radius * math.Sin(angle * (math.Pi / 180))
- }
- func getLines(filename string) ([]string, error) {
- // open the file and parse out the entites
- f, _ := os.Open(filename) // Open the file.
- scanner := bufio.NewScanner(f) // Create a new Scanner for the file.
- entities := []string{}
- for scanner.Scan() { // Loop over all lines in the file
- line := scanner.Text()
- inEntities := false
- if line == "ENTITIES" {
- inEntities = true
- } else if inEntities {
- if line == "ENDSEC" {
- inEntities = false
- } else {
- entities = append(entities, line)
- }
- }
- }
- return entities, nil
- }
- func getEnt(list []string, e []Ent) ([]Ent, error){
- //add a slice for each entitie and populate the groups for each entitie
- // add error handling
- group := ""
- count := 0
- for i := range list {
- if len(group) > 0 { // update struct, if group == " 0" append new struct
- switch group {
- case " 0":
- if len(e) > 0 {count++} // increment count for next slice
- e = append(e, Ent{})
- e[count].G0 = list[i]
- case " 8":
- e[count].G8 = list[i]
- case " 10":
- e[count].G10, _ = strconv.ParseFloat(list[i],64)
- case " 11":
- e[count].G11, _ = strconv.ParseFloat(list[i],64)
- case " 20":
- e[count].G20, _ = strconv.ParseFloat(list[i],64)
- case " 21":
- e[count].G21, _ = strconv.ParseFloat(list[i],64)
- case " 30":
- e[count].G30, _ = strconv.ParseFloat(list[i],64)
- case " 31":
- e[count].G31, _ = strconv.ParseFloat(list[i],64)
- case " 40":
- e[count].G40, _ = strconv.ParseFloat(list[i],64)
- case " 50":
- e[count].G50, _ = strconv.ParseFloat(list[i],64)
- case " 51":
- e[count].G51, _ = strconv.ParseFloat(list[i],64)
- }
- //fmt.Println(group, list[i])
- group = ""
- }
- switch list[i] { // trigger when a group is found
- case " 0", " 8", " 10", " 11", " 20", " 21",
- " 30", " 31", " 40", " 50", " 51":
- group = list[i]
- }
- }
- return e, nil
- }
- func findEndPoints (e []Ent) ([]Ent, error){
- for i := range e {
- switch e[i].G0 {
- case "ARC": // get the X and Y points
- e[i].Xs = getXcartesian(e[i].G40, e[i].G50) + e[i].G10
- e[i].Xe = getXcartesian(e[i].G40, e[i].G51) + e[i].G10
- e[i].Ys = getYcartesian(e[i].G40, e[i].G50) + e[i].G20
- e[i].Ye = getYcartesian(e[i].G40, e[i].G51) + e[i].G20
- case "LINE": // redundant but transfer the X and Y points
- e[i].Xs = e[i].G10
- e[i].Ys = e[i].G20
- e[i].Zs = e[i].G30
- e[i].Xe = e[i].G11
- e[i].Ye = e[i].G21
- e[i].Zs = e[i].G31
- case "CIRCLE": // if it is a circle it must be the only entity on that layer
- fmt.Println("Circle")
- }
- }
- return e, nil
- }
- func sortEnt(e []Ent) ([]Ent, error){
- /*
- another variant is to make a map of endpoints, take random element,
- and extend it left and right until the map is emptied
- i would agree with pmezard. work through all elements building a map,
- then iterate through linking together
- you can do that by sorting on endpoints
- you put the start point and end point of each element in a slice and sort
- them by x, then y then isStart (so start points come before end points)
- */
- var s []Ent
- s = append(s, e[0])
- e = append(e[:0], e[1:]...) // delete the first element
- xe := s[0].Xe
- ye := s[0].Ye
- fmt.Println(len(e))
- fmt.Println(xe, ye)
- for i := 0; len(e) > 0; i++ {
- for i := range e {
- if e[i].Xs == xe && e[i].Ys == ye {
- s = append(s, e[i]) // copy the match
- xe = e[i].Xe
- ye = e[i].Ye
- e = append(e[:i], e[i+1:]...) // delete the match
- }
- }
- if i > 20{break}
- }
- for i := range e {
- xs := e[i].Xs
- xe := e[i].Xe
- /*for i := range e {
- //fmt.Println(xs, xe, i)
- }*/
- fmt.Println(xs, xe, i)
- }
- return s, nil
- }
- type Ent struct {
- G0, G8 string
- G10, G11, G20, G21, G30, G31, G40, G50, G51,
- Xs, Xe, Ys, Ye, Zs, Ze float64
- }
- func main(){
- lines, err := getLines("test.dxf")
- var entities []Ent
- if err == nil {
- entities, err = getEnt(lines, entities)
- entities, err = findEndPoints(entities)
- //entities, err = sortEnt(entities)
- } else {
- log.Fatalf("%v\n", err)
- }
- //fmt.Println(sorted)
- /*
- for i := range(entities){
- fmt.Print(entities[i].G0, " ")
- fmt.Printf("Xs %f Xe %f ", entities[i].Xs, entities[i].Xe)
- fmt.Printf("Ys %f Ye %f\n", entities[i].Ys, entities[i].Ye)
- }*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement