printrun-src/printrun/gcodeplater.py

Tue, 19 Jan 2021 20:45:09 +0100

author
mdd
date
Tue, 19 Jan 2021 20:45:09 +0100
changeset 45
c82943fb205f
parent 15
0bbb006204fc
child 46
cce0af6351f0
permissions
-rw-r--r--

updated main files to new github master version

15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1 #!/usr/bin/env python
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
3 # This file is part of the Printrun suite.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
4 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
5 # Printrun is free software: you can redistribute it and/or modify
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
6 # it under the terms of the GNU General Public License as published by
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
7 # the Free Software Foundation, either version 3 of the License, or
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
8 # (at your option) any later version.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
9 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
10 # Printrun is distributed in the hope that it will be useful,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
13 # GNU General Public License for more details.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
14 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
15 # You should have received a copy of the GNU General Public License
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
16 # along with Printrun. If not, see <http://www.gnu.org/licenses/>.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
17
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
18 # Set up Internationalization using gettext
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
19 # searching for installed locales on /usr/share; uses relative folder if not found (windows)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
20 from .utils import install_locale, get_home_pos
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
21 install_locale('pronterface')
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
22
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
23 import wx
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
24 import sys
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
25 import os
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
26 import time
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
27 import types
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
28 import re
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
29 import math
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
30 import logging
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
31
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
32 from printrun import gcoder
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
33 from printrun.objectplater import make_plater, PlaterPanel
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
34 from printrun.gl.libtatlin import actors
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
35 import printrun.gui.viz # NOQA
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
36 from printrun import gcview
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
37
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
38 def extrusion_only(gline):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
39 return gline.e is not None \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
40 and (gline.x, gline.y, gline.z) == (None, None, None)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
41
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
42 # Custom method for gcoder.GCode to analyze & output gcode in a single call
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
43 def gcoder_write(self, f, line, store = False):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
44 f.write(line)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
45 self.append(line, store = store)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
46
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
47 rewrite_exp = re.compile("(%s)" % "|".join(["X([-+]?[0-9]*\.?[0-9]*)",
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
48 "Y([-+]?[0-9]*\.?[0-9]*)"]))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
49
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
50 def rewrite_gline(centeroffset, gline, cosr, sinr):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
51 if gline.is_move and (gline.x is not None or gline.y is not None):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
52 if gline.relative:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
53 xc = yc = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
54 cox = coy = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
55 if gline.x is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
56 xc = gline.x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
57 if gline.y is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
58 yc = gline.y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
59 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
60 xc = gline.current_x + centeroffset[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
61 yc = gline.current_y + centeroffset[1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
62 cox = centeroffset[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
63 coy = centeroffset[1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
64 new_x = "X%.04f" % (xc * cosr - yc * sinr - cox)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
65 new_y = "Y%.04f" % (xc * sinr + yc * cosr - coy)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
66 new = {"X": new_x, "Y": new_y}
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
67 new_line = rewrite_exp.sub(lambda ax: new[ax.group()[0]], gline.raw)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
68 new_line = new_line.split(";")[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
69 if gline.x is None: new_line += " " + new_x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
70 if gline.y is None: new_line += " " + new_y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
71 return new_line
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
72 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
73 return gline.raw
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
74
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
75 class GcodePlaterPanel(PlaterPanel):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
76
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
77 load_wildcard = _("GCODE files (*.gcode;*.GCODE;*.g)") + "|*.gcode;*.gco;*.g"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
78 save_wildcard = _("GCODE files (*.gcode;*.GCODE;*.g)") + "|*.gcode;*.gco;*.g"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
79
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
80 def prepare_ui(self, filenames = [], callback = None,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
81 parent = None, build_dimensions = None,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
82 circular_platform = False, antialias_samples = 0):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
83 super(GcodePlaterPanel, self).prepare_ui(filenames, callback, parent, build_dimensions)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
84 viewer = gcview.GcodeViewPanel(self, build_dimensions = self.build_dimensions,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
85 antialias_samples = antialias_samples)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
86 self.set_viewer(viewer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
87 self.platform = actors.Platform(self.build_dimensions,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
88 circular = circular_platform)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
89 self.platform_object = gcview.GCObject(self.platform)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
90
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
91 def get_objects(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
92 return [self.platform_object] + self.models.values()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
93 objects = property(get_objects)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
94
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
95 def load_file(self, filename):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
96 gcode = gcoder.GCode(open(filename, "rU"),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
97 get_home_pos(self.build_dimensions))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
98 model = actors.GcodeModel()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
99 if gcode.filament_length > 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
100 model.display_travels = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
101 generator = model.load_data(gcode)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
102 generator_output = generator.next()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
103 while generator_output is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
104 generator_output = generator.next()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
105 obj = gcview.GCObject(model)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
106 obj.offsets = [self.build_dimensions[3], self.build_dimensions[4], 0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
107 obj.gcode = gcode
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
108 obj.dims = [gcode.xmin, gcode.xmax,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
109 gcode.ymin, gcode.ymax,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
110 gcode.zmin, gcode.zmax]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
111 obj.centeroffset = [-(obj.dims[1] + obj.dims[0]) / 2,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
112 -(obj.dims[3] + obj.dims[2]) / 2,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
113 0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
114 self.add_model(filename, obj)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
115 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
116
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
117 def done(self, event, cb):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
118 if not os.path.exists("tempgcode"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
119 os.mkdir("tempgcode")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
120 name = "tempgcode/" + str(int(time.time()) % 10000) + ".gcode"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
121 self.export_to(name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
122 if cb is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
123 cb(name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
124 if self.destroy_on_done:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
125 self.Destroy()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
126
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
127 # What's hard in there ?
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
128 # 1) [x] finding the order in which the objects are printed
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
129 # 2) [x] handling layers correctly
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
130 # 3) [x] handling E correctly
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
131 # 4) [x] handling position shifts: should we either reset absolute 0 using
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
132 # G92 or should we rewrite all positions ? => we use G92s
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
133 # 5) [ ] handling the start & end gcode properly ?
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
134 # 6) [x] handling of current tool
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
135 # 7) [x] handling of Z moves for sequential printing (don't lower Z before
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
136 # reaching the next object print area)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
137 # 8) [x] handling of absolute/relative status
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
138 # Initial implementation should just print the objects sequentially,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
139 # but the end goal is to have a clean per-layer merge
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
140 def export_to(self, name):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
141 return self.export_combined(name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
142 return self.export_sequential(name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
143
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
144 def export_combined(self, name):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
145 models = self.models.values()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
146 last_real_position = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
147 # Sort models by Z max to print smaller objects first
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
148 models.sort(key = lambda x: x.dims[-1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
149 alllayers = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
150 for (model_i, model) in enumerate(models):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
151 def add_offset(layer):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
152 return layer.z + model.offsets[2] if layer.z is not None else layer.z
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
153 alllayers += [(add_offset(layer), model_i, layer_i)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
154 for (layer_i, layer) in enumerate(model.gcode.all_layers) if layer]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
155 alllayers.sort()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
156 laste = [0] * len(models)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
157 lasttool = [0] * len(models)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
158 lastrelative = [False] * len(models)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
159 with open(name, "w") as f:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
160 analyzer = gcoder.GCode(None, get_home_pos(self.build_dimensions))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
161 analyzer.write = types.MethodType(lambda self, line: gcoder_write(self, f, line), analyzer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
162 for (layer_z, model_i, layer_i) in alllayers:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
163 model = models[model_i]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
164 layer = model.gcode.all_layers[layer_i]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
165 r = math.radians(model.rot)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
166 o = model.offsets
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
167 co = model.centeroffset
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
168 offset_pos = last_real_position if last_real_position is not None else (0, 0, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
169 analyzer.write("; %f %f %f\n" % offset_pos)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
170 trans = (- (o[0] + co[0]),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
171 - (o[1] + co[1]),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
172 - (o[2] + co[2]))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
173 trans_wpos = (offset_pos[0] + trans[0],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
174 offset_pos[1] + trans[1],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
175 offset_pos[2] + trans[2])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
176 analyzer.write("; GCodePlater: Model %d Layer %d at Z = %s\n" % (model_i, layer_i, layer_z))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
177 if lastrelative[model_i]:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
178 analyzer.write("G91\n")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
179 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
180 analyzer.write("G90\n")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
181 if analyzer.current_tool != lasttool[model_i]:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
182 analyzer.write("T%d\n" % lasttool[model_i])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
183 analyzer.write("G92 X%.5f Y%.5f Z%.5f\n" % trans_wpos)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
184 analyzer.write("G92 E%.5f\n" % laste[model_i])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
185 for l in layer:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
186 if l.command != "G28" and (l.command != "G92" or extrusion_only(l)):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
187 if r == 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
188 analyzer.write(l.raw + "\n")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
189 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
190 analyzer.write(rewrite_gline(co, l, math.cos(r), math.sin(r)) + "\n")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
191 # Find the current real position & E
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
192 last_real_position = analyzer.current_pos
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
193 laste[model_i] = analyzer.current_e
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
194 lastrelative[model_i] = analyzer.relative
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
195 lasttool[model_i] = analyzer.current_tool
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
196 logging.info(_("Exported merged G-Codes to %s") % name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
197
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
198 def export_sequential(self, name):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
199 models = self.models.values()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
200 last_real_position = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
201 # Sort models by Z max to print smaller objects first
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
202 models.sort(key = lambda x: x.dims[-1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
203 with open(name, "w") as f:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
204 for model_i, model in enumerate(models):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
205 r = math.radians(model.rot)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
206 o = model.offsets
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
207 co = model.centeroffset
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
208 offset_pos = last_real_position if last_real_position is not None else (0, 0, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
209 trans = (- (o[0] + co[0]),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
210 - (o[1] + co[1]),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
211 - (o[2] + co[2]))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
212 trans_wpos = (offset_pos[0] + trans[0],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
213 offset_pos[1] + trans[1],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
214 offset_pos[2] + trans[2])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
215 f.write("; GCodePlater: Model %d\n" % model_i)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
216 f.write("G90\n")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
217 f.write("G92 X%.5f Y%.5f Z%.5f E0\n" % trans_wpos)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
218 f.write("G1 X%.5f Y%.5f" % (-co[0], -co[1]))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
219 for l in model.gcode:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
220 if l.command != "G28" and (l.command != "G92" or extrusion_only(l)):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
221 if r == 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
222 f.write(l.raw + "\n")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
223 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
224 f.write(rewrite_gline(co, l, math.cos(r), math.sin(r)) + "\n")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
225 # Find the current real position
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
226 for i in xrange(len(model.gcode) - 1, -1, -1):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
227 gline = model.gcode.lines[i]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
228 if gline.is_move:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
229 last_real_position = (- trans[0] + gline.current_x,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
230 - trans[1] + gline.current_y,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
231 - trans[2] + gline.current_z)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
232 break
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
233 logging.info(_("Exported merged G-Codes to %s") % name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
234
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
235 GcodePlater = make_plater(GcodePlaterPanel)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
236
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
237 if __name__ == '__main__':
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
238 app = wx.App(False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
239 main = GcodePlater(filenames = sys.argv[1:])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
240 for fn in main.filenames:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
241 main.load_file(fn)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
242 main.filenames = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
243 main.autoplate()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
244 main.export_to("gcodeplate___test.gcode")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
245 raise SystemExit
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
246 main.Show()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
247 app.MainLoop()

mercurial