-
Notifications
You must be signed in to change notification settings - Fork 17
/
svgout.py
84 lines (72 loc) · 2.93 KB
/
svgout.py
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
import shapely.geometry as sg
import shapely.affinity as sa
import shapely.ops as so
import svgwrite
def write(board, filename, style = 'laser'):
gml = board.layers['GML'].lines
block = sg.Polygon(gml[-1], gml[:-1])
block = block.buffer(1).buffer(-1)
for d,xys in board.holes.items():
if d > 0.3:
hlist = so.unary_union([sg.Point(xy).buffer(d / 2) for xy in xys])
block = block.difference(hlist)
block = sa.scale(block, 1, -1, origin = (0,0)) # flip Y for svg
(x0, y0, x1, y1) = block.bounds
block = sa.translate(block, -x0, -y0)
x1 -= x0
y1 -= y0
if style == 'laser':
args = {'stroke':'red', 'fill_opacity':0.0, 'stroke_width':.1}
else:
args = {'stroke':'gray', 'fill_opacity':0.0, 'stroke_width':.1}
dwg = svgwrite.Drawing(filename, size=('%fmm' % x1, '%fmm' % y1), viewBox=('0 0 %f %f' % (x1, y1)))
li = [block.exterior] + list(block.interiors)
for l in li:
dwg.add(dwg.polyline(l.coords, **args))
def renderpoly(po, args):
if type(po) == sg.MultiPolygon:
[renderpoly(p, args) for p in po.geoms]
return
# Subdivide a poly if it has holes
if len(po.interiors) == 0:
dwg.add(dwg.polygon(po.exterior.coords, **args))
else:
x0 = min([x for (x, y) in po.exterior.coords])
x1 = max([x for (x, y) in po.exterior.coords])
y0 = min([y for (x, y) in po.exterior.coords])
y1 = max([y for (x, y) in po.exterior.coords])
xm = (x0 + x1) / 2
eps = 0.00
renderpoly(po.intersection(sg.box(x0, y0, xm + eps, y1)), args)
renderpoly(po.intersection(sg.box(xm - eps, y0, x1, y1)), args)
if 0:
args = {'stroke':'blue', 'fill_opacity':0.0, 'stroke_width':.1}
for po in gto.geoms:
li = [po.exterior] + list(po.interiors)
for l in li:
dwg.add(dwg.polyline(l.coords, **args))
def layer(nm, args1, args2):
gto = board.layers[nm].preview()
gto = sa.scale(gto, 1, -1, origin = (0, 0)) # flip Y for svg
gto = sa.translate(gto, -x0, -y0)
if 1:
if isinstance(gto, sg.Polygon):
renderpoly(gto, args1)
else:
[renderpoly(po, args1) for po in gto.geoms]
for po in gto.geoms:
li = [po.exterior] + list(po.interiors)
for l in li:
dwg.add(dwg.polyline(l.coords, **args2))
if style == 'laser':
args = {'stroke':'blue', 'fill_opacity':0.0, 'stroke_width':.1}
else:
args = {'stroke':'black', 'fill_opacity':0.0, 'stroke_width':0}
layer('GTO',
{'fill':'black', 'fill_opacity':1.0, 'stroke_width':0},
args)
if style == 'lands':
layer('GTL',
{'fill':'black', 'fill_opacity':1.0, 'stroke_width':0},
{'stroke':'grey', 'fill_opacity':0.0, 'stroke_width':.1})
dwg.save()