-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
194 lines (157 loc) · 3.7 KB
/
parse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"bufio"
"log"
"math"
"os"
"sort"
"strconv"
"strings"
)
var (
drillPositions = map[string]bool{}
parseContour = false
points []coordinates
)
func parseInput(file string) {
input, err := os.Open(file)
if err != nil {
log.Fatal(err)
}
defer input.Close()
scanner := bufio.NewScanner(input)
// scan polygon apertures
for scanner.Scan() {
line := scanner.Text()
parseInputLine(line)
}
sort.Sort(BySize(apertures))
}
func parseInputLine(line string) {
if parseContour {
switch {
case strings.HasPrefix(line, "X"):
point := parseCoordinates(line)
points = append(points, point)
case strings.HasPrefix(line, "G37*"):
pos := getCenter(points)
size := getSize(points)
tag := pos.String()
drillPositions[tag] = true
a := aperture{size: size, pos: []coordinates{pos}}
apertures = append(apertures, a)
parseContour = false
}
return
}
switch {
// start of file
case strings.HasPrefix(line, "G04 Gerber Fmt "):
parseFormat(line)
// aperture definition
case strings.HasPrefix(line, "%AD"):
a := parseAperture(line)
apertures = append(apertures, a)
// existing aperture
case strings.HasPrefix(line, "D"):
name := strings.TrimSuffix(line, "*")
currentAperture = apertureByName(name)
// aperture coordinates
case strings.HasPrefix(line, "X"):
pos := parseCoordinates(line)
tag := pos.String()
if drillPositions[tag] {
// log.Println("ignoring aperture", currentAperture.name)
break
}
drillPositions[tag] = true
currentAperture.pos = append(currentAperture.pos, pos)
// start of freeform aperture
case strings.HasPrefix(line, "G36*"):
points = []coordinates{}
parseContour = true
}
}
func parseFormat(line string) {
line = strings.TrimPrefix(line, "G04 Gerber Fmt ")
if strings.HasSuffix(line, ", Leading zero omitted, Abs format (unit mm)*") {
units = "metric"
line = strings.TrimSuffix(line, ", Leading zero omitted, Abs format (unit mm)*")
}
format := strings.Split(line, ".")
i, err := strconv.Atoi(format[1])
if err != nil {
log.Fatal("incorrect number format")
}
decimalDivider = math.Pow10(i)
}
func apertureByName(name string) *aperture {
for i, e := range apertures {
if e.name == name {
return &apertures[i]
}
}
log.Fatal("aperture not found")
return nil
}
func parseAperture(line string) aperture {
parts := strings.Split(line, ",")
// get name and shape
name := strings.TrimPrefix(parts[0], "%AD")
shape := name[len(name)-1]
name = name[:len(name)-1]
// get size
dimensions := strings.Split(strings.TrimSuffix(parts[1], "*%"), "X")
size := 0.0
switch shape {
case 'C':
d, err := strconv.ParseFloat(dimensions[0], 64)
if err != nil {
log.Fatal(err, line)
}
size = d
case 'O':
fallthrough
case 'R':
x, err := strconv.ParseFloat(dimensions[0], 64)
if err != nil {
log.Fatal(err, line)
}
y, err := strconv.ParseFloat(dimensions[1], 64)
if err != nil {
log.Fatal(err, line)
}
// best size for area
areaSize := math.Sqrt(x * y)
// max size for smaller dimension
maxSize := math.Min(x, y) * 1.2
size = math.Min(maxSize, areaSize)
default:
log.Fatal("unknown aperture shape: ", line)
}
if units == "imperial" {
size *= 25.4
}
return aperture{name: name, size: size}
}
func parseCoordinates(line string) coordinates {
line = strings.Split(line, "D")[0]
line = strings.TrimPrefix(line, "X")
pos := strings.Split(line, "Y")
x, err := strconv.ParseFloat(pos[0], 64)
if err != nil {
log.Fatal(err, line)
}
y, err := strconv.ParseFloat(pos[1], 64)
if err != nil {
log.Fatal(err, line)
}
if units == "imperial" {
x *= 25.4
y *= 25.4
}
if bottom {
x = -x
}
return coordinates{x: x / decimalDivider, y: y / decimalDivider}
}