Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "bufio"
- "os"
- "log"
- "fmt"
- "strconv"
- "math"
- "sort"
- )
- 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, err := os.Open(filename) // Open the file.
- defer f.Close() // close the file when the function is done
- if err != nil {fmt.Println(err)}
- scanner := bufio.NewScanner(f) // Create a new Scanner for the file.
- scanner.Split(bufio.ScanLines)
- lines := []string{}
- inEntities := false
- for scanner.Scan() { // Loop over all lines in the file
- switch scanner.Text() {
- case "ENTITIES":
- inEntities = true
- case "ENDSEC":
- inEntities = false
- }
- if inEntities {
- lines = append(lines, scanner.Text())
- }
- }
- return lines, 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(s int, t float64, e []Ent) ([]Ent, error) {
- xe := e[s].Xe
- ye := e[s].Ye
- index := 1
- for skip := range e {
- if skip != s { // skip the start entity
- for i := range e {
- if math.Abs(e[i].Xs - xe) < t && math.Abs(e[i].Ys - ye) < t {
- e[i].Index = index
- index++
- xe = e[i].Xe
- ye = e[i].Ye
- break
- }
- }
- }
- }
- return e, nil
- }
- type Ent struct {
- Index int
- G0, G8 string
- G10, G11, G20, G21, G30, G31, G40, G50, G51,
- Xs, Xe, Ys, Ye, Zs, Ze float64
- }
- type ByIndex []Ent
- func (a ByIndex) Len() int { return len(a) }
- func (a ByIndex) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
- func (a ByIndex) Less(i, j int) bool { return a[i].Index < a[j].Index }
- func main(){
- lines, err := getLines("test.dxf")
- var entities []Ent
- if err == nil {
- entities, err = getEnt(lines, entities)
- entities, err = findEndPoints(entities)
- start := 3
- tolerance := 0.0000001
- entities, err = sortEnt(start, tolerance, entities)
- sort.Sort(ByIndex(entities))
- } else {
- log.Fatalf("%v\n", err)
- }
- for i := range(entities){
- fmt.Printf("%d %s %f %f %f %f\n",
- entities[i].Index, entities[i].G0, entities[i].Xs,
- entities[i].Ys, entities[i].Xe, entities[i].Ye)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement