printrun-src/printrun/gcodeplater.py

changeset 46
cce0af6351f0
parent 15
0bbb006204fc
equal deleted inserted replaced
45:c82943fb205f 46:cce0af6351f0
1 #!/usr/bin/env python 1 #!/usr/bin/env python3
2 2
3 # This file is part of the Printrun suite. 3 # This file is part of the Printrun suite.
4 # 4 #
5 # Printrun is free software: you can redistribute it and/or modify 5 # Printrun is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by 6 # it under the terms of the GNU General Public License as published by
77 load_wildcard = _("GCODE files (*.gcode;*.GCODE;*.g)") + "|*.gcode;*.gco;*.g" 77 load_wildcard = _("GCODE files (*.gcode;*.GCODE;*.g)") + "|*.gcode;*.gco;*.g"
78 save_wildcard = _("GCODE files (*.gcode;*.GCODE;*.g)") + "|*.gcode;*.gco;*.g" 78 save_wildcard = _("GCODE files (*.gcode;*.GCODE;*.g)") + "|*.gcode;*.gco;*.g"
79 79
80 def prepare_ui(self, filenames = [], callback = None, 80 def prepare_ui(self, filenames = [], callback = None,
81 parent = None, build_dimensions = None, 81 parent = None, build_dimensions = None,
82 circular_platform = False, antialias_samples = 0): 82 circular_platform = False,
83 antialias_samples = 0,
84 grid = (1, 10)):
83 super(GcodePlaterPanel, self).prepare_ui(filenames, callback, parent, build_dimensions) 85 super(GcodePlaterPanel, self).prepare_ui(filenames, callback, parent, build_dimensions)
84 viewer = gcview.GcodeViewPanel(self, build_dimensions = self.build_dimensions, 86 viewer = gcview.GcodeViewPanel(self, build_dimensions = self.build_dimensions,
85 antialias_samples = antialias_samples) 87 antialias_samples = antialias_samples)
86 self.set_viewer(viewer) 88 self.set_viewer(viewer)
87 self.platform = actors.Platform(self.build_dimensions, 89 self.platform = actors.Platform(self.build_dimensions,
88 circular = circular_platform) 90 circular = circular_platform,
91 grid = grid)
89 self.platform_object = gcview.GCObject(self.platform) 92 self.platform_object = gcview.GCObject(self.platform)
90 93
91 def get_objects(self): 94 def get_objects(self):
92 return [self.platform_object] + self.models.values() 95 return [self.platform_object] + list(self.models.values())
93 objects = property(get_objects) 96 objects = property(get_objects)
94 97
95 def load_file(self, filename): 98 def load_file(self, filename):
96 gcode = gcoder.GCode(open(filename, "rU"), 99 gcode = gcoder.GCode(open(filename, "rU"),
97 get_home_pos(self.build_dimensions)) 100 get_home_pos(self.build_dimensions))
98 model = actors.GcodeModel() 101 model = actors.GcodeModel()
99 if gcode.filament_length > 0: 102 if gcode.filament_length > 0:
100 model.display_travels = False 103 model.display_travels = False
101 generator = model.load_data(gcode) 104 generator = model.load_data(gcode)
102 generator_output = generator.next() 105 generator_output = next(generator)
103 while generator_output is not None: 106 while generator_output is not None:
104 generator_output = generator.next() 107 generator_output = next(generator)
105 obj = gcview.GCObject(model) 108 obj = gcview.GCObject(model)
106 obj.offsets = [self.build_dimensions[3], self.build_dimensions[4], 0] 109 obj.offsets = [self.build_dimensions[3], self.build_dimensions[4], 0]
107 obj.gcode = gcode 110 obj.gcode = gcode
108 obj.dims = [gcode.xmin, gcode.xmax, 111 obj.dims = [gcode.xmin, gcode.xmax,
109 gcode.ymin, gcode.ymax, 112 gcode.ymin, gcode.ymax,
140 def export_to(self, name): 143 def export_to(self, name):
141 return self.export_combined(name) 144 return self.export_combined(name)
142 return self.export_sequential(name) 145 return self.export_sequential(name)
143 146
144 def export_combined(self, name): 147 def export_combined(self, name):
145 models = self.models.values() 148 models = list(self.models.values())
146 last_real_position = None 149 last_real_position = None
147 # Sort models by Z max to print smaller objects first 150 # Sort models by Z max to print smaller objects first
148 models.sort(key = lambda x: x.dims[-1]) 151 models.sort(key = lambda x: x.dims[-1])
149 alllayers = [] 152 alllayers = []
150 for (model_i, model) in enumerate(models): 153 for (model_i, model) in enumerate(models):
151 def add_offset(layer): 154 def add_offset(layer):
152 return layer.z + model.offsets[2] if layer.z is not None else layer.z 155 return layer.z + model.offsets[2] if layer.z is not None else layer.z
153 alllayers += [(add_offset(layer), model_i, layer_i) 156 alllayers += [(add_offset(layer), model_i, layer_i)
154 for (layer_i, layer) in enumerate(model.gcode.all_layers) if layer] 157 for (layer_i, layer) in enumerate(model.gcode.all_layers) if add_offset(layer) is not None]
155 alllayers.sort() 158 alllayers.sort()
156 laste = [0] * len(models) 159 laste = [0] * len(models)
157 lasttool = [0] * len(models) 160 lasttool = [0] * len(models)
158 lastrelative = [False] * len(models) 161 lastrelative = [False] * len(models)
159 with open(name, "w") as f: 162 with open(name, "w") as f:
194 lastrelative[model_i] = analyzer.relative 197 lastrelative[model_i] = analyzer.relative
195 lasttool[model_i] = analyzer.current_tool 198 lasttool[model_i] = analyzer.current_tool
196 logging.info(_("Exported merged G-Codes to %s") % name) 199 logging.info(_("Exported merged G-Codes to %s") % name)
197 200
198 def export_sequential(self, name): 201 def export_sequential(self, name):
199 models = self.models.values() 202 models = list(self.models.values())
200 last_real_position = None 203 last_real_position = None
201 # Sort models by Z max to print smaller objects first 204 # Sort models by Z max to print smaller objects first
202 models.sort(key = lambda x: x.dims[-1]) 205 models.sort(key = lambda x: x.dims[-1])
203 with open(name, "w") as f: 206 with open(name, "w") as f:
204 for model_i, model in enumerate(models): 207 for model_i, model in enumerate(models):
221 if r == 0: 224 if r == 0:
222 f.write(l.raw + "\n") 225 f.write(l.raw + "\n")
223 else: 226 else:
224 f.write(rewrite_gline(co, l, math.cos(r), math.sin(r)) + "\n") 227 f.write(rewrite_gline(co, l, math.cos(r), math.sin(r)) + "\n")
225 # Find the current real position 228 # Find the current real position
226 for i in xrange(len(model.gcode) - 1, -1, -1): 229 for i in range(len(model.gcode) - 1, -1, -1):
227 gline = model.gcode.lines[i] 230 gline = model.gcode.lines[i]
228 if gline.is_move: 231 if gline.is_move:
229 last_real_position = (- trans[0] + gline.current_x, 232 last_real_position = (- trans[0] + gline.current_x,
230 - trans[1] + gline.current_y, 233 - trans[1] + gline.current_y,
231 - trans[2] + gline.current_z) 234 - trans[2] + gline.current_z)

mercurial