printrun-src/printrun/gcodeplater.py

Wed, 20 Jan 2021 10:15:13 +0100

author
mdd
date
Wed, 20 Jan 2021 10:15:13 +0100
changeset 46
cce0af6351f0
parent 15
0bbb006204fc
permissions
-rwxr-xr-x

updated and added new files for printrun

46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
1 #!/usr/bin/env python3
15
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,
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
82 circular_platform = False,
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
83 antialias_samples = 0,
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
84 grid = (1, 10)):
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
85 super(GcodePlaterPanel, self).prepare_ui(filenames, callback, parent, build_dimensions)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
86 viewer = gcview.GcodeViewPanel(self, build_dimensions = self.build_dimensions,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
87 antialias_samples = antialias_samples)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
88 self.set_viewer(viewer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
89 self.platform = actors.Platform(self.build_dimensions,
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
90 circular = circular_platform,
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
91 grid = grid)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
92 self.platform_object = gcview.GCObject(self.platform)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
93
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
94 def get_objects(self):
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
95 return [self.platform_object] + list(self.models.values())
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
96 objects = property(get_objects)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
97
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
98 def load_file(self, filename):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
99 gcode = gcoder.GCode(open(filename, "rU"),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
100 get_home_pos(self.build_dimensions))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
101 model = actors.GcodeModel()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
102 if gcode.filament_length > 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
103 model.display_travels = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
104 generator = model.load_data(gcode)
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
105 generator_output = next(generator)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
106 while generator_output is not None:
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
107 generator_output = next(generator)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
108 obj = gcview.GCObject(model)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
109 obj.offsets = [self.build_dimensions[3], self.build_dimensions[4], 0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
110 obj.gcode = gcode
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
111 obj.dims = [gcode.xmin, gcode.xmax,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
112 gcode.ymin, gcode.ymax,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
113 gcode.zmin, gcode.zmax]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
114 obj.centeroffset = [-(obj.dims[1] + obj.dims[0]) / 2,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
115 -(obj.dims[3] + obj.dims[2]) / 2,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
116 0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
117 self.add_model(filename, obj)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
118 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
119
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
120 def done(self, event, cb):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
121 if not os.path.exists("tempgcode"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
122 os.mkdir("tempgcode")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
123 name = "tempgcode/" + str(int(time.time()) % 10000) + ".gcode"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
124 self.export_to(name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
125 if cb is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
126 cb(name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
127 if self.destroy_on_done:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
128 self.Destroy()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
129
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
130 # What's hard in there ?
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
131 # 1) [x] finding the order in which the objects are printed
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
132 # 2) [x] handling layers correctly
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
133 # 3) [x] handling E correctly
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
134 # 4) [x] handling position shifts: should we either reset absolute 0 using
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
135 # G92 or should we rewrite all positions ? => we use G92s
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
136 # 5) [ ] handling the start & end gcode properly ?
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
137 # 6) [x] handling of current tool
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
138 # 7) [x] handling of Z moves for sequential printing (don't lower Z before
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
139 # reaching the next object print area)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
140 # 8) [x] handling of absolute/relative status
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
141 # Initial implementation should just print the objects sequentially,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
142 # but the end goal is to have a clean per-layer merge
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
143 def export_to(self, name):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
144 return self.export_combined(name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
145 return self.export_sequential(name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
146
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
147 def export_combined(self, name):
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
148 models = list(self.models.values())
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
149 last_real_position = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
150 # Sort models by Z max to print smaller objects first
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
151 models.sort(key = lambda x: x.dims[-1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
152 alllayers = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
153 for (model_i, model) in enumerate(models):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
154 def add_offset(layer):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
155 return layer.z + model.offsets[2] if layer.z is not None else layer.z
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
156 alllayers += [(add_offset(layer), model_i, layer_i)
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
157 for (layer_i, layer) in enumerate(model.gcode.all_layers) if add_offset(layer) is not None]
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
158 alllayers.sort()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
159 laste = [0] * len(models)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
160 lasttool = [0] * len(models)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
161 lastrelative = [False] * len(models)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
162 with open(name, "w") as f:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
163 analyzer = gcoder.GCode(None, get_home_pos(self.build_dimensions))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
164 analyzer.write = types.MethodType(lambda self, line: gcoder_write(self, f, line), analyzer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
165 for (layer_z, model_i, layer_i) in alllayers:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
166 model = models[model_i]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
167 layer = model.gcode.all_layers[layer_i]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
168 r = math.radians(model.rot)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
169 o = model.offsets
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
170 co = model.centeroffset
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
171 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
172 analyzer.write("; %f %f %f\n" % offset_pos)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
173 trans = (- (o[0] + co[0]),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
174 - (o[1] + co[1]),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
175 - (o[2] + co[2]))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
176 trans_wpos = (offset_pos[0] + trans[0],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
177 offset_pos[1] + trans[1],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
178 offset_pos[2] + trans[2])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
179 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
180 if lastrelative[model_i]:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
181 analyzer.write("G91\n")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
182 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
183 analyzer.write("G90\n")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
184 if analyzer.current_tool != lasttool[model_i]:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
185 analyzer.write("T%d\n" % lasttool[model_i])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
186 analyzer.write("G92 X%.5f Y%.5f Z%.5f\n" % trans_wpos)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
187 analyzer.write("G92 E%.5f\n" % laste[model_i])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
188 for l in layer:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
189 if l.command != "G28" and (l.command != "G92" or extrusion_only(l)):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
190 if r == 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
191 analyzer.write(l.raw + "\n")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
192 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
193 analyzer.write(rewrite_gline(co, l, math.cos(r), math.sin(r)) + "\n")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
194 # Find the current real position & E
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
195 last_real_position = analyzer.current_pos
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
196 laste[model_i] = analyzer.current_e
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
197 lastrelative[model_i] = analyzer.relative
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
198 lasttool[model_i] = analyzer.current_tool
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
199 logging.info(_("Exported merged G-Codes to %s") % name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
200
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
201 def export_sequential(self, name):
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
202 models = list(self.models.values())
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
203 last_real_position = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
204 # Sort models by Z max to print smaller objects first
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
205 models.sort(key = lambda x: x.dims[-1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
206 with open(name, "w") as f:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
207 for model_i, model in enumerate(models):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
208 r = math.radians(model.rot)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
209 o = model.offsets
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
210 co = model.centeroffset
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
211 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
212 trans = (- (o[0] + co[0]),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
213 - (o[1] + co[1]),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
214 - (o[2] + co[2]))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
215 trans_wpos = (offset_pos[0] + trans[0],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
216 offset_pos[1] + trans[1],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
217 offset_pos[2] + trans[2])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
218 f.write("; GCodePlater: Model %d\n" % model_i)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
219 f.write("G90\n")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
220 f.write("G92 X%.5f Y%.5f Z%.5f E0\n" % trans_wpos)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
221 f.write("G1 X%.5f Y%.5f" % (-co[0], -co[1]))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
222 for l in model.gcode:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
223 if l.command != "G28" and (l.command != "G92" or extrusion_only(l)):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
224 if r == 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
225 f.write(l.raw + "\n")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
226 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
227 f.write(rewrite_gline(co, l, math.cos(r), math.sin(r)) + "\n")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
228 # Find the current real position
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
229 for i in range(len(model.gcode) - 1, -1, -1):
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
230 gline = model.gcode.lines[i]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
231 if gline.is_move:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
232 last_real_position = (- trans[0] + gline.current_x,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
233 - trans[1] + gline.current_y,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
234 - trans[2] + gline.current_z)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
235 break
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
236 logging.info(_("Exported merged G-Codes to %s") % name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
237
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
238 GcodePlater = make_plater(GcodePlaterPanel)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
239
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
240 if __name__ == '__main__':
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
241 app = wx.App(False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
242 main = GcodePlater(filenames = sys.argv[1:])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
243 for fn in main.filenames:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
244 main.load_file(fn)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
245 main.filenames = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
246 main.autoplate()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
247 main.export_to("gcodeplate___test.gcode")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
248 raise SystemExit
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
249 main.Show()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
250 app.MainLoop()

mercurial