svg2gcode/svg2gcode.py

Sat, 07 Nov 2015 20:50:55 +0100

author
mbayer
date
Sat, 07 Nov 2015 20:50:55 +0100
changeset 10
92835c3f171a
parent 9
89d724cfd8c3
child 11
e7feabe18234
permissions
-rwxr-xr-x

finished refactoring

4
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
1 #!/usr/bin/env python
5
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
2 # -*- coding: utf-8 -*-
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
3
10
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
4 import svg, sys, math
4
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
5 from gcode import Gcode
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
6 from optparse import OptionParser
6
ff679c15cb0e infill only on Polygon or Path which have fill style attribute
mbayer
parents: 5
diff changeset
7 from tinycss import CSS21Parser
4
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
8
8
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
9 from shapely.geometry import box, MultiLineString, Polygon
5
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
10 from shapely.affinity import rotate
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
11 from shapely import speedups
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
12 from math import sqrt
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
13
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
14 # enable Shapely speedups, if possible
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
15 if speedups.available:
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
16 speedups.enable()
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
17
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
18 def hatchbox(rect, angle, spacing):
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
19 """
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
20 returns a Shapely geometry (MULTILINESTRING, or more rarely,
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
21 GEOMETRYCOLLECTION) for a simple hatched rectangle.
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
22
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
23 args:
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
24 rect - a Shapely geometry for the outer boundary of the hatch
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
25 Likely most useful if it really is a rectangle
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
26
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
27 angle - angle of hatch lines, conventional anticlockwise -ve
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
28
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
29 spacing - spacing between hatch lines
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
30
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
31 GEOMETRYCOLLECTION case occurs when a hatch line intersects with
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
32 the corner of the clipping rectangle, which produces a point
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
33 along with the usual lines.
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
34 """
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
35
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
36 (llx, lly, urx, ury) = rect.bounds
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
37 centre_x = (urx + llx) / 2
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
38 centre_y = (ury + lly) / 2
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
39 diagonal_length = sqrt((urx - llx) ** 2 + (ury - lly) ** 2)
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
40 number_of_lines = 2 + int(diagonal_length / spacing)
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
41 hatch_length = spacing * (number_of_lines - 1)
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
42
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
43 # build a square (of side hatch_length) horizontal lines
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
44 # centred on centroid of the bounding box, 'spacing' units apart
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
45 coords = []
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
46 for i in range(number_of_lines):
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
47 # alternate lines l2r and r2l to keep HP-7470A plotter happy ☺
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
48 if i % 2:
8
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
49 coords.extend([((
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
50 centre_x - hatch_length / 2, \
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
51 centre_y - hatch_length / 2 + i * spacing), (
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
52 centre_x + hatch_length / 2, \
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
53 centre_y - hatch_length / 2 + i * spacing))])
5
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
54 else:
8
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
55 coords.extend([(( \
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
56 centre_x + hatch_length / 2, \
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
57 centre_y - hatch_length / 2 + i * spacing), (
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
58 centre_x - hatch_length / 2, \
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
59 centre_y - hatch_length / 2 + i * spacing))])
5
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
60 # turn array into Shapely object
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
61 lines = MultiLineString(coords)
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
62 # Rotate by angle around box centre
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
63 lines = rotate(lines, angle, origin='centroid', use_radians=False)
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
64 # return clipped array
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
65 return rect.intersection(lines)
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
66
6
ff679c15cb0e infill only on Polygon or Path which have fill style attribute
mbayer
parents: 5
diff changeset
67 def parse_style(stylestr):
10
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
68 """
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
69 Parse the given string containing CSS2.1 syntax
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
70 Returns a dict with the keys/values
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
71 """
6
ff679c15cb0e infill only on Polygon or Path which have fill style attribute
mbayer
parents: 5
diff changeset
72 if stylestr.strip() == '':
ff679c15cb0e infill only on Polygon or Path which have fill style attribute
mbayer
parents: 5
diff changeset
73 return None
ff679c15cb0e infill only on Polygon or Path which have fill style attribute
mbayer
parents: 5
diff changeset
74 parser = CSS21Parser()
ff679c15cb0e infill only on Polygon or Path which have fill style attribute
mbayer
parents: 5
diff changeset
75 style = parser.parse_style_attr(stylestr)
10
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
76 data = {}
6
ff679c15cb0e infill only on Polygon or Path which have fill style attribute
mbayer
parents: 5
diff changeset
77 for obj in style[0]:
10
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
78 data[obj.name] = obj.value[0].value
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
79 return data
6
ff679c15cb0e infill only on Polygon or Path which have fill style attribute
mbayer
parents: 5
diff changeset
80
9
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
81 class Image(object):
10
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
82 """
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
83 SVG Image handler class
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
84 """
9
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
85 def __init__(self, filename, options, gcoder):
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
86 self.gcoder = gcoder
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
87 self.options = options
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
88 self.svg = svg.parse(filename)
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
89 self.bb1, self.bb2 = self.svg.bbox()
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
90 self.width, self.height = self.bb2.coord()
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
91 self.infill = None
8
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
92
9
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
93 self._check_dimensions()
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
94 self._generate_infill()
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
95
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
96 def _check_dimensions(self):
10
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
97 """
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
98 Output image dimensions/scaling to console and gcode
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
99 """
9
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
100 msg = "Original dimension: %.2f x %.2f" % (self.width, self.height)
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
101 print msg
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
102 self.gcoder.comment(msg)
10
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
103 self.gcoder.comment("Scale: %.2f" % (self.options.scale))
9
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
104 width = self.width * self.gcoder.mm_pixel * self.options.scale
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
105 height = self.height * self.gcoder.mm_pixel * self.options.scale
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
106 msg = "Print dimension: %.2fmm x %.2fmm" % (width, height)
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
107 print msg
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
108 self.gcoder.comment(msg)
8
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
109
9
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
110 def _generate_infill(self):
10
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
111 """
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
112 Generates infill pattern image for later use
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
113 """
9
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
114 b1x, b1y = self.bb1.coord()
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
115 b2x, b2y = self.bb2.coord()
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
116 page = box(b1x, b1y, b2x, b2y)
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
117 # TODO: Infill spacing needs to be calculated with proper scaling and gcode MM dimensions
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
118 # TODO: Make infill angle 0, 45 or 90 degrees configurable to options parser (0° = X, 90° = Y, 45° = X and Y but half the speed/accel needed!)
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
119 self.infill = hatchbox(page, 0, 2)
4
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
120
9
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
121 def normalize(self, coord):
10
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
122 """
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
123 Normalize X / Y Axis of coordinates
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
124 At the moment only Y gets flipped to match Reprap coordinate system (0,0 is bottom left instead top left on SVG)
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
125 """
9
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
126 c_x = coord[0]
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
127 c_y = coord[1]
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
128 # flip y
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
129 c_y = (self.height - c_y)
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
130 return (c_x, c_y)
4
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
131
9
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
132 def get_drawings(self):
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
133 """
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
134 Returns a list of all svg drawings with segments attribute
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
135 """
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
136 data = []
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
137 for dwg in self.svg.flatten():
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
138 if hasattr(dwg, "segments"):
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
139 data.append(dwg)
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
140 return data
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
141
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
142 def svg2gcode(options, gcoder):
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
143 image = Image(options.filename, options, gcoder)
8
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
144
9
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
145 for dwg in image.get_drawings():
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
146 for l in dwg.segments(1):
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
147 # THE OUTLINE
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
148 coord = image.normalize(l[0].coord())
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
149 gcoder.move(coord[0], coord[1])
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
150 for pt in l[1:]:
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
151 coord = image.normalize(pt.coord())
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
152 gcoder.engrave(coord[0], coord[1])
8
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
153
9
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
154 if options.outline:
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
155 continue
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
156
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
157 if isinstance(dwg, svg.Polygon) or isinstance(dwg, svg.Path):
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
158 #check if we should infill?
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
159 style = parse_style(dwg.style)
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
160 if not style:
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
161 continue
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
162 if not 'fill' in style.keys():
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
163 continue
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
164 if style['fill'] == 'none':
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
165 continue
6
ff679c15cb0e infill only on Polygon or Path which have fill style attribute
mbayer
parents: 5
diff changeset
166
9
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
167 # try to generate the infill poly complex
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
168 poly = None
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
169 for l in dwg.segments(1):
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
170 segments = []
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
171 for pnt in l:
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
172 segments.append(pnt.coord())
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
173 shape = Polygon(segments)
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
174 if shape.is_valid:
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
175 if not poly:
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
176 poly = shape
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
177 else:
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
178 if shape.within(poly):
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
179 poly = poly.difference(shape)
8
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
180 else:
9
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
181 poly = poly.union(shape)
8
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
182
9
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
183 lines = poly.intersection(image.infill)
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
184 if lines:
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
185 # THE INFILL
10
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
186 prev_end = None
9
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
187 for line in lines:
10
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
188 coords = [
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
189 image.normalize((line.coords[0][0], line.coords[0][1])),
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
190 image.normalize((line.coords[1][0], line.coords[1][1]))
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
191 ]
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
192 if prev_end:
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
193 # calculate distances to previous end, swap if current end is nearest
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
194 dist = [
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
195 abs(math.hypot(coords[0][0] - prev_end[0], coords[0][1] - prev_end[1])),
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
196 abs(math.hypot(coords[1][0] - prev_end[0], coords[1][1] - prev_end[1]))
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
197 ]
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
198 if dist[0] > dist[1]:
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
199 coords = list(reversed(coords))
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
200 prev_end = coords[1]
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
201 gcoder.move(coords[0][0], coords[0][1])
92835c3f171a finished refactoring
mbayer
parents: 9
diff changeset
202 gcoder.engrave(coords[1][0], coords[1][1])
6
ff679c15cb0e infill only on Polygon or Path which have fill style attribute
mbayer
parents: 5
diff changeset
203
9
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
204 def init_options():
8
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
205 parser = OptionParser()
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
206 parser.add_option("-f", "--file", dest="filename", default=None,
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
207 help="Load SVG file", metavar="FILE")
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
208 parser.add_option("-s", "--scale",
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
209 dest="scale", type="float", default=1.0,
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
210 help="set scale factor (default 1.0)")
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
211 parser.add_option("-e", "",
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
212 dest="engrave_speed", type="float", default=20,
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
213 help="engrave speed mm/sec (default 20)")
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
214 parser.add_option("-t", "",
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
215 dest="travel_speed", type="float", default=130,
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
216 help="travel speed mm/sec (default 130)")
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
217 parser.add_option("-o", "--outline", action="store_true",
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
218 dest="outline", default=False,
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
219 help="no infill, only outlines")
9
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
220 return parser.parse_args()
7
421aae29d9d9 finished poly infill
mbayer
parents: 6
diff changeset
221
9
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
222 if __name__ == "__main__":
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
223 (OPTIONS, ARGS) = init_options()
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
224 if not OPTIONS.filename:
8
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
225 print "no filename given!"
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
226 sys.exit(1)
5
b41cdab37aab added generic infill support, alpha stage
mbayer
parents: 4
diff changeset
227
8
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
228 # initialize gcode worker
9
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
229 GCODER = Gcode(scale=OPTIONS.scale, travel_speed=OPTIONS.travel_speed, engrave_speed=OPTIONS.engrave_speed)
8
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
230
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
231 # processing
9
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
232 svg2gcode(OPTIONS, GCODER)
8
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
233
86f90bddac0f code refactoring
mbayer
parents: 7
diff changeset
234 # write gcode file
9
89d724cfd8c3 code refactoring - more cleanup
mbayer
parents: 8
diff changeset
235 GCODER.write(OPTIONS.filename + ".g")

mercurial