11import click
22from pathlib import Path
33import vpype as vp
4+ import typing
45from vpype .layers import LayerType
56
67# Load the default config
@@ -36,7 +37,7 @@ def invert_axis(document: vp.Document, invert_x: bool, invert_y: bool):
3637
3738
3839@click .command ()
39- @click .argument ("filename " , type = click .Path ( exists = False ))
40+ @click .argument ("output " , type = click .File ( "w" ))
4041@click .option (
4142 "-p" ,
4243 "--profile" ,
@@ -46,7 +47,7 @@ def invert_axis(document: vp.Document, invert_x: bool, invert_y: bool):
4647 help = "gcode writer profile from the vpype configuration file subsection 'gwrite'" ,
4748)
4849@vp .global_processor
49- def gwrite (document : vp .Document , filename : str , profile : str ):
50+ def gwrite (document : vp .Document , output : typing . TextIO , profile : str ):
5051 gwrite_config = vp .CONFIG_MANAGER .config ["gwrite" ]
5152
5253 # If no profile was provided, try to use a default
@@ -94,68 +95,67 @@ def gwrite(document: vp.Document, filename: str, profile: str):
9495
9596 if invert_x or invert_y :
9697 document = invert_axis (document , invert_x , invert_y )
97-
98- with open (filename , "w" ) as f :
99- if document_start is not None :
100- f .write (document_start .format (filename = filename ))
101- last_x = 0
102- last_y = 0
103- xx = 0
104- yy = 0
105- lastlayer_index = len (document .layers .values ()) - 1
106- for layer_index , layer in enumerate (document .layers .values ()):
107- if layer_start is not None :
108- f .write (layer_start .format (index = layer_index ))
109- lastlines_index = len (layer ) - 1
110- for lines_index , lines in enumerate (layer ):
111- lines_scaled = lines * scale
112- if linecollection_start is not None :
113- f .write (linecollection_start .format (index = lines_index ))
114- segment_last_index = len (lines_scaled ) - 1
115- for segment_index , seg in enumerate (lines_scaled ):
116- x = seg .real
117- y = seg .imag
118- dx = x - last_x
119- dy = y - last_y
120- idx = int (round (x - xx ))
121- idy = int (round (y - yy ))
122- xx += idx
123- yy += idy
124- if segment_first is not None and segment_index == 0 :
125- seg_write = segment_first
126- elif segment_last is not None and segment_index == segment_last_index :
127- seg_write = segment_last
128- else :
129- seg_write = segment
130- f .write (
131- seg_write .format (
132- x = x ,
133- y = y ,
134- dx = dx ,
135- dy = dy ,
136- _x = - x ,
137- _y = - y ,
138- _dx = - dx ,
139- _dy = - dy ,
140- ix = xx ,
141- iy = yy ,
142- idx = idx ,
143- idy = idy ,
144- index = segment_index ,
145- )
98+ filename = output .name
99+ if document_start is not None :
100+ output .write (document_start .format (filename = filename ))
101+ last_x = 0
102+ last_y = 0
103+ xx = 0
104+ yy = 0
105+ lastlayer_index = len (document .layers .values ()) - 1
106+ for layer_index , layer in enumerate (document .layers .values ()):
107+ if layer_start is not None :
108+ output .write (layer_start .format (index = layer_index ))
109+ lastlines_index = len (layer ) - 1
110+ for lines_index , lines in enumerate (layer ):
111+ lines_scaled = lines * scale
112+ if linecollection_start is not None :
113+ output .write (linecollection_start .format (index = lines_index ))
114+ segment_last_index = len (lines_scaled ) - 1
115+ for segment_index , seg in enumerate (lines_scaled ):
116+ x = seg .real
117+ y = seg .imag
118+ dx = x - last_x
119+ dy = y - last_y
120+ idx = int (round (x - xx ))
121+ idy = int (round (y - yy ))
122+ xx += idx
123+ yy += idy
124+ if segment_first is not None and segment_index == 0 :
125+ seg_write = segment_first
126+ elif segment_last is not None and segment_index == segment_last_index :
127+ seg_write = segment_last
128+ else :
129+ seg_write = segment
130+ output .write (
131+ seg_write .format (
132+ x = x ,
133+ y = y ,
134+ dx = dx ,
135+ dy = dy ,
136+ _x = - x ,
137+ _y = - y ,
138+ _dx = - dx ,
139+ _dy = - dy ,
140+ ix = xx ,
141+ iy = yy ,
142+ idx = idx ,
143+ idy = idy ,
144+ index = segment_index ,
146145 )
147- last_x = x
148- last_y = y
149- if linecollection_end is not None :
150- f .write (linecollection_end .format (index = lines_index ))
151- if linecollection_join is not None and lines_index != lastlines_index :
152- f .write (linecollection_join )
153- if layer_end is not None :
154- f .write (layer_end .format (index = layer_index ))
155- if layer_join is not None and layer_index != lastlayer_index :
156- f .write (layer_join )
157- if document_end is not None :
158- f .write (document_end .format (filename = filename ))
146+ )
147+ last_x = x
148+ last_y = y
149+ if linecollection_end is not None :
150+ output .write (linecollection_end .format (index = lines_index ))
151+ if linecollection_join is not None and lines_index != lastlines_index :
152+ output .write (linecollection_join )
153+ if layer_end is not None :
154+ output .write (layer_end .format (index = layer_index ))
155+ if layer_join is not None and layer_index != lastlayer_index :
156+ output .write (layer_join )
157+ if document_end is not None :
158+ output .write (document_end .format (filename = filename ))
159159
160160 return document
161161
0 commit comments