Not a member of Pastebin yet?
                        Sign Up,
                        it unlocks many cool features!                    
                - package main
 - import (
 - "bufio"
 - "os"
 - "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) {
 - lines := []string{}
 - f, err := os.Open(filename)
 - defer f.Close()
 - if err != nil {
 - fmt.Println(err)
 - os.Exit(1)
 - }
 - scanner := bufio.NewScanner(f)
 - scanner.Split(bufio.ScanLines)
 - inEntities := false
 - // extract all the lines of entities
 - for scanner.Scan() {
 - switch scanner.Text() {
 - case "ENTITIES":
 - inEntities = true
 - case "ENDSEC":
 - inEntities = false
 - }
 - if inEntities {
 - lines = append(lines, scanner.Text())
 - }
 - }
 - return lines
 - }
 - func getEnt(list []string, e []Ent) ([]Ent){
 - // add error handling
 - group := ""
 - count := 0
 - for i := range list {
 - if len(group) > 0 {
 - switch group {
 - case " 0":
 - if len(e) > 0 {count++}
 - 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)
 - }
 - 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
 - }
 - func findEndPoints (e []Ent) ([]Ent){
 - // add error handling
 - for i := range e {
 - switch e[i].G0 {
 - case "ARC": // get the X and Y end 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":
 - 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
 - }
 - // find the matching start points for each entity and assign index
 - func findIndex(s int, t float64, e []Ent) ([]Ent) {
 - // add error handling
 - xe := e[s].Xe
 - ye := e[s].Ye
 - index := 1
 - for skip := range e {
 - if skip != s {
 - 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
 - }
 - 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 genGcode (e []Ent) {
 - xe, ye := 0.0, 0.0 // figure out what to do if the first entity is an arc
 - xo, yo := 0.0, 0.0
 - for _, ent := range e {
 - switch ent.G0 {
 - case "LINE":
 - fmt.Printf("G1 X%f Y%f\n", ent.G11, ent.G21)
 - xe = ent.Xe
 - ye = ent.Ye
 - case "ARC": // need some smarts here, offset depends on last xy position
 - xo = ent.G10 - xe
 - yo = ent.G20 - ye
 - fmt.Printf("G3 X%f Y%f I%f J%f\n", ent.Xe, ent.Ye, xo, yo)
 - xe = ent.Xe
 - ye = ent.Ye
 - case "CIRCLE":
 - fmt.Println("Circle")
 - }
 - }
 - }
 - func main(){
 - var file = ""
 - if len(os.Args) > 1 {
 - file = os.Args[1]
 - } else {
 - file = "test.dxf"
 - }
 - var entities []Ent
 - lines := getLines(file)
 - entities = getEnt(lines, entities)
 - entities = findEndPoints(entities)
 - start := 3
 - tolerance := 0.0000001
 - entities = findIndex(start, tolerance, entities)
 - sort.Sort(ByIndex(entities))
 - genGcode(entities)
 - /*
 - 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