Fri, 03 Jun 2016 10:05:45 +0200
remove pyc files
15 | 1 | # This file is part of the Printrun suite. |
2 | # | |
3 | # Printrun is free software: you can redistribute it and/or modify | |
4 | # it under the terms of the GNU General Public License as published by | |
5 | # the Free Software Foundation, either version 3 of the License, or | |
6 | # (at your option) any later version. | |
7 | # | |
8 | # Printrun is distributed in the hope that it will be useful, | |
9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | # GNU General Public License for more details. | |
12 | # | |
13 | # You should have received a copy of the GNU General Public License | |
14 | # along with Printrun. If not, see <http://www.gnu.org/licenses/>. | |
15 | ||
16 | import xml.etree.ElementTree | |
17 | import wx | |
18 | import wx.lib.agw.floatspin as floatspin | |
19 | import os | |
20 | import time | |
21 | import zipfile | |
22 | import tempfile | |
23 | import shutil | |
24 | from cairosvg.surface import PNGSurface | |
25 | import cStringIO | |
26 | import imghdr | |
27 | import copy | |
28 | import re | |
29 | from collections import OrderedDict | |
30 | import itertools | |
31 | import math | |
32 | ||
33 | class DisplayFrame(wx.Frame): | |
34 | def __init__(self, parent, title, res = (1024, 768), printer = None, scale = 1.0, offset = (0, 0)): | |
35 | wx.Frame.__init__(self, parent = parent, title = title, size = res) | |
36 | self.printer = printer | |
37 | self.control_frame = parent | |
38 | self.pic = wx.StaticBitmap(self) | |
39 | self.bitmap = wx.EmptyBitmap(*res) | |
40 | self.bbitmap = wx.EmptyBitmap(*res) | |
41 | self.slicer = 'bitmap' | |
42 | self.dpi = 96 | |
43 | dc = wx.MemoryDC() | |
44 | dc.SelectObject(self.bbitmap) | |
45 | dc.SetBackground(wx.Brush("black")) | |
46 | dc.Clear() | |
47 | dc.SelectObject(wx.NullBitmap) | |
48 | ||
49 | self.SetBackgroundColour("black") | |
50 | self.pic.Hide() | |
51 | self.SetDoubleBuffered(True) | |
52 | self.SetPosition((self.control_frame.GetSize().x, 0)) | |
53 | self.Show() | |
54 | ||
55 | self.scale = scale | |
56 | self.index = 0 | |
57 | self.size = res | |
58 | self.offset = offset | |
59 | self.running = False | |
60 | self.layer_red = False | |
61 | ||
62 | def clear_layer(self): | |
63 | try: | |
64 | dc = wx.MemoryDC() | |
65 | dc.SelectObject(self.bitmap) | |
66 | dc.SetBackground(wx.Brush("black")) | |
67 | dc.Clear() | |
68 | self.pic.SetBitmap(self.bitmap) | |
69 | self.pic.Show() | |
70 | self.Refresh() | |
71 | except: | |
72 | raise | |
73 | pass | |
74 | ||
75 | def resize(self, res = (1024, 768)): | |
76 | self.bitmap = wx.EmptyBitmap(*res) | |
77 | self.bbitmap = wx.EmptyBitmap(*res) | |
78 | dc = wx.MemoryDC() | |
79 | dc.SelectObject(self.bbitmap) | |
80 | dc.SetBackground(wx.Brush("black")) | |
81 | dc.Clear() | |
82 | dc.SelectObject(wx.NullBitmap) | |
83 | ||
84 | def draw_layer(self, image): | |
85 | try: | |
86 | dc = wx.MemoryDC() | |
87 | dc.SelectObject(self.bitmap) | |
88 | dc.SetBackground(wx.Brush("black")) | |
89 | dc.Clear() | |
90 | ||
91 | if self.slicer == 'Slic3r' or self.slicer == 'Skeinforge': | |
92 | ||
93 | if self.scale != 1.0: | |
94 | layercopy = copy.deepcopy(image) | |
95 | height = float(layercopy.get('height').replace('m', '')) | |
96 | width = float(layercopy.get('width').replace('m', '')) | |
97 | ||
98 | layercopy.set('height', str(height * self.scale) + 'mm') | |
99 | layercopy.set('width', str(width * self.scale) + 'mm') | |
100 | layercopy.set('viewBox', '0 0 ' + str(width * self.scale) + ' ' + str(height * self.scale)) | |
101 | ||
102 | g = layercopy.find("{http://www.w3.org/2000/svg}g") | |
103 | g.set('transform', 'scale(' + str(self.scale) + ')') | |
104 | stream = cStringIO.StringIO(PNGSurface.convert(dpi = self.dpi, bytestring = xml.etree.ElementTree.tostring(layercopy))) | |
105 | else: | |
106 | stream = cStringIO.StringIO(PNGSurface.convert(dpi = self.dpi, bytestring = xml.etree.ElementTree.tostring(image))) | |
107 | ||
108 | pngImage = wx.ImageFromStream(stream) | |
109 | ||
110 | # print "w:", pngImage.Width, ", dpi:", self.dpi, ", w (mm): ",(pngImage.Width / self.dpi) * 25.4 | |
111 | ||
112 | if self.layer_red: | |
113 | pngImage = pngImage.AdjustChannels(1, 0, 0, 1) | |
114 | ||
115 | dc.DrawBitmap(wx.BitmapFromImage(pngImage), self.offset[0], self.offset[1], True) | |
116 | ||
117 | elif self.slicer == 'bitmap': | |
118 | if isinstance(image, str): | |
119 | image = wx.Image(image) | |
120 | if self.layer_red: | |
121 | image = image.AdjustChannels(1, 0, 0, 1) | |
122 | dc.DrawBitmap(wx.BitmapFromImage(image.Scale(image.Width * self.scale, image.Height * self.scale)), self.offset[0], -self.offset[1], True) | |
123 | else: | |
124 | raise Exception(self.slicer + " is an unknown method.") | |
125 | ||
126 | self.pic.SetBitmap(self.bitmap) | |
127 | self.pic.Show() | |
128 | self.Refresh() | |
129 | ||
130 | except: | |
131 | raise | |
132 | pass | |
133 | ||
134 | def show_img_delay(self, image): | |
135 | print "Showing", str(time.clock()) | |
136 | self.control_frame.set_current_layer(self.index) | |
137 | self.draw_layer(image) | |
138 | wx.FutureCall(1000 * self.interval, self.hide_pic_and_rise) | |
139 | ||
140 | def rise(self): | |
141 | if (self.direction == "Top Down"): | |
142 | print "Lowering", str(time.clock()) | |
143 | else: | |
144 | print "Rising", str(time.clock()) | |
145 | ||
146 | if self.printer is not None and self.printer.online: | |
147 | self.printer.send_now("G91") | |
148 | ||
149 | if (self.prelift_gcode): | |
150 | for line in self.prelift_gcode.split('\n'): | |
151 | if line: | |
152 | self.printer.send_now(line) | |
153 | ||
154 | if (self.direction == "Top Down"): | |
155 | self.printer.send_now("G1 Z-%f F%g" % (self.overshoot, self.z_axis_rate,)) | |
156 | self.printer.send_now("G1 Z%f F%g" % (self.overshoot - self.thickness, self.z_axis_rate,)) | |
157 | else: # self.direction == "Bottom Up" | |
158 | self.printer.send_now("G1 Z%f F%g" % (self.overshoot, self.z_axis_rate,)) | |
159 | self.printer.send_now("G1 Z-%f F%g" % (self.overshoot - self.thickness, self.z_axis_rate,)) | |
160 | ||
161 | if (self.postlift_gcode): | |
162 | for line in self.postlift_gcode.split('\n'): | |
163 | if line: | |
164 | self.printer.send_now(line) | |
165 | ||
166 | self.printer.send_now("G90") | |
167 | else: | |
168 | time.sleep(self.pause) | |
169 | ||
170 | wx.FutureCall(1000 * self.pause, self.next_img) | |
171 | ||
172 | def hide_pic(self): | |
173 | print "Hiding", str(time.clock()) | |
174 | self.pic.Hide() | |
175 | ||
176 | def hide_pic_and_rise(self): | |
177 | wx.CallAfter(self.hide_pic) | |
178 | wx.FutureCall(500, self.rise) | |
179 | ||
180 | def next_img(self): | |
181 | if not self.running: | |
182 | return | |
183 | if self.index < len(self.layers): | |
184 | print self.index | |
185 | wx.CallAfter(self.show_img_delay, self.layers[self.index]) | |
186 | self.index += 1 | |
187 | else: | |
188 | print "end" | |
189 | wx.CallAfter(self.pic.Hide) | |
190 | wx.CallAfter(self.Refresh) | |
191 | ||
192 | def present(self, | |
193 | layers, | |
194 | interval = 0.5, | |
195 | pause = 0.2, | |
196 | overshoot = 0.0, | |
197 | z_axis_rate = 200, | |
198 | prelift_gcode = "", | |
199 | postlift_gcode = "", | |
200 | direction = "Top Down", | |
201 | thickness = 0.4, | |
202 | scale = 1, | |
203 | size = (1024, 768), | |
204 | offset = (0, 0), | |
205 | layer_red = False): | |
206 | wx.CallAfter(self.pic.Hide) | |
207 | wx.CallAfter(self.Refresh) | |
208 | self.layers = layers | |
209 | self.scale = scale | |
210 | self.thickness = thickness | |
211 | self.size = size | |
212 | self.interval = interval | |
213 | self.pause = pause | |
214 | self.overshoot = overshoot | |
215 | self.z_axis_rate = z_axis_rate | |
216 | self.prelift_gcode = prelift_gcode | |
217 | self.postlift_gcode = postlift_gcode | |
218 | self.direction = direction | |
219 | self.layer_red = layer_red | |
220 | self.offset = offset | |
221 | self.index = 0 | |
222 | self.running = True | |
223 | ||
224 | self.next_img() | |
225 | ||
226 | class SettingsFrame(wx.Frame): | |
227 | ||
228 | def _set_setting(self, name, value): | |
229 | if self.pronterface: | |
230 | self.pronterface.set(name, value) | |
231 | ||
232 | def _get_setting(self, name, val): | |
233 | if self.pronterface: | |
234 | try: | |
235 | return getattr(self.pronterface.settings, name) | |
236 | except AttributeError: | |
237 | return val | |
238 | else: | |
239 | return val | |
240 | ||
241 | def __init__(self, parent, printer = None): | |
242 | wx.Frame.__init__(self, parent, title = "ProjectLayer Control", style = (wx.DEFAULT_FRAME_STYLE | wx.WS_EX_CONTEXTHELP)) | |
243 | self.SetExtraStyle(wx.FRAME_EX_CONTEXTHELP) | |
244 | self.pronterface = parent | |
245 | self.display_frame = DisplayFrame(self, title = "ProjectLayer Display", printer = printer) | |
246 | ||
247 | self.panel = wx.Panel(self) | |
248 | ||
249 | vbox = wx.BoxSizer(wx.VERTICAL) | |
250 | buttonbox = wx.StaticBoxSizer(wx.StaticBox(self.panel, label = "Controls"), wx.HORIZONTAL) | |
251 | ||
252 | load_button = wx.Button(self.panel, -1, "Load") | |
253 | load_button.Bind(wx.EVT_BUTTON, self.load_file) | |
254 | load_button.SetHelpText("Choose an SVG file created from Slic3r or Skeinforge, or a zip file of bitmap images (with extension: .3dlp.zip).") | |
255 | buttonbox.Add(load_button, flag = wx.LEFT | wx.RIGHT | wx.BOTTOM, border = 5) | |
256 | ||
257 | present_button = wx.Button(self.panel, -1, "Present") | |
258 | present_button.Bind(wx.EVT_BUTTON, self.start_present) | |
259 | present_button.SetHelpText("Starts the presentation of the slices.") | |
260 | buttonbox.Add(present_button, flag = wx.LEFT | wx.RIGHT | wx.BOTTOM, border = 5) | |
261 | ||
262 | self.pause_button = wx.Button(self.panel, -1, "Pause") | |
263 | self.pause_button.Bind(wx.EVT_BUTTON, self.pause_present) | |
264 | self.pause_button.SetHelpText("Pauses the presentation. Can be resumed afterwards by clicking this button, or restarted by clicking present again.") | |
265 | buttonbox.Add(self.pause_button, flag = wx.LEFT | wx.RIGHT | wx.BOTTOM, border = 5) | |
266 | ||
267 | stop_button = wx.Button(self.panel, -1, "Stop") | |
268 | stop_button.Bind(wx.EVT_BUTTON, self.stop_present) | |
269 | stop_button.SetHelpText("Stops presenting the slices.") | |
270 | buttonbox.Add(stop_button, flag = wx.LEFT | wx.RIGHT | wx.BOTTOM, border = 5) | |
271 | ||
272 | self.help_button = wx.ContextHelpButton(self.panel) | |
273 | buttonbox.Add(self.help_button, flag = wx.LEFT | wx.RIGHT | wx.BOTTOM, border = 5) | |
274 | ||
275 | fieldboxsizer = wx.StaticBoxSizer(wx.StaticBox(self.panel, label = "Settings"), wx.VERTICAL) | |
276 | fieldsizer = wx.GridBagSizer(10, 10) | |
277 | ||
278 | # Left Column | |
279 | ||
280 | fieldsizer.Add(wx.StaticText(self.panel, -1, "Layer (mm):"), pos = (0, 0), flag = wx.ALIGN_CENTER_VERTICAL) | |
281 | self.thickness = wx.TextCtrl(self.panel, -1, str(self._get_setting("project_layer", "0.1")), size = (80, -1)) | |
282 | self.thickness.Bind(wx.EVT_TEXT, self.update_thickness) | |
283 | self.thickness.SetHelpText("The thickness of each slice. Should match the value used to slice the model. SVG files update this value automatically, 3dlp.zip files have to be manually entered.") | |
284 | fieldsizer.Add(self.thickness, pos = (0, 1)) | |
285 | ||
286 | fieldsizer.Add(wx.StaticText(self.panel, -1, "Exposure (s):"), pos = (1, 0), flag = wx.ALIGN_CENTER_VERTICAL) | |
287 | self.interval = wx.TextCtrl(self.panel, -1, str(self._get_setting("project_interval", "0.5")), size = (80, -1)) | |
288 | self.interval.Bind(wx.EVT_TEXT, self.update_interval) | |
289 | self.interval.SetHelpText("How long each slice should be displayed.") | |
290 | fieldsizer.Add(self.interval, pos = (1, 1)) | |
291 | ||
292 | fieldsizer.Add(wx.StaticText(self.panel, -1, "Blank (s):"), pos = (2, 0), flag = wx.ALIGN_CENTER_VERTICAL) | |
293 | self.pause = wx.TextCtrl(self.panel, -1, str(self._get_setting("project_pause", "0.5")), size = (80, -1)) | |
294 | self.pause.Bind(wx.EVT_TEXT, self.update_pause) | |
295 | self.pause.SetHelpText("The pause length between slices. This should take into account any movement of the Z axis, plus time to prepare the resin surface (sliding, tilting, sweeping, etc).") | |
296 | fieldsizer.Add(self.pause, pos = (2, 1)) | |
297 | ||
298 | fieldsizer.Add(wx.StaticText(self.panel, -1, "Scale:"), pos = (3, 0), flag = wx.ALIGN_CENTER_VERTICAL) | |
299 | self.scale = floatspin.FloatSpin(self.panel, -1, value = self._get_setting('project_scale', 1.0), increment = 0.1, digits = 3, size = (80, -1)) | |
300 | self.scale.Bind(floatspin.EVT_FLOATSPIN, self.update_scale) | |
301 | self.scale.SetHelpText("The additional scaling of each slice.") | |
302 | fieldsizer.Add(self.scale, pos = (3, 1)) | |
303 | ||
304 | fieldsizer.Add(wx.StaticText(self.panel, -1, "Direction:"), pos = (4, 0), flag = wx.ALIGN_CENTER_VERTICAL) | |
305 | self.direction = wx.ComboBox(self.panel, -1, choices = ["Top Down", "Bottom Up"], value = self._get_setting('project_direction', "Top Down"), size = (80, -1)) | |
306 | self.direction.Bind(wx.EVT_COMBOBOX, self.update_direction) | |
307 | self.direction.SetHelpText("The direction the Z axis should move. Top Down is where the projector is above the model, Bottom up is where the projector is below the model.") | |
308 | fieldsizer.Add(self.direction, pos = (4, 1), flag = wx.ALIGN_CENTER_VERTICAL) | |
309 | ||
310 | fieldsizer.Add(wx.StaticText(self.panel, -1, "Overshoot (mm):"), pos = (5, 0), flag = wx.ALIGN_CENTER_VERTICAL) | |
311 | self.overshoot = floatspin.FloatSpin(self.panel, -1, value = self._get_setting('project_overshoot', 3.0), increment = 0.1, digits = 1, min_val = 0, size = (80, -1)) | |
312 | self.overshoot.Bind(floatspin.EVT_FLOATSPIN, self.update_overshoot) | |
313 | self.overshoot.SetHelpText("How far the axis should move beyond the next slice position for each slice. For Top Down printers this would dunk the model under the resi and then return. For Bottom Up printers this would raise the base away from the vat and then return.") | |
314 | fieldsizer.Add(self.overshoot, pos = (5, 1)) | |
315 | ||
316 | fieldsizer.Add(wx.StaticText(self.panel, -1, "Pre-lift Gcode:"), pos = (6, 0), flag = wx.ALIGN_CENTER_VERTICAL) | |
317 | self.prelift_gcode = wx.TextCtrl(self.panel, -1, str(self._get_setting("project_prelift_gcode", "").replace("\\n", '\n')), size = (-1, 35), style = wx.TE_MULTILINE) | |
318 | self.prelift_gcode.SetHelpText("Additional gcode to run before raising the Z axis. Be sure to take into account any additional time needed in the pause value, and be careful what gcode is added!") | |
319 | self.prelift_gcode.Bind(wx.EVT_TEXT, self.update_prelift_gcode) | |
320 | fieldsizer.Add(self.prelift_gcode, pos = (6, 1), span = (2, 1)) | |
321 | ||
322 | fieldsizer.Add(wx.StaticText(self.panel, -1, "Post-lift Gcode:"), pos = (6, 2), flag = wx.ALIGN_CENTER_VERTICAL) | |
323 | self.postlift_gcode = wx.TextCtrl(self.panel, -1, str(self._get_setting("project_postlift_gcode", "").replace("\\n", '\n')), size = (-1, 35), style = wx.TE_MULTILINE) | |
324 | self.postlift_gcode.SetHelpText("Additional gcode to run after raising the Z axis. Be sure to take into account any additional time needed in the pause value, and be careful what gcode is added!") | |
325 | self.postlift_gcode.Bind(wx.EVT_TEXT, self.update_postlift_gcode) | |
326 | fieldsizer.Add(self.postlift_gcode, pos = (6, 3), span = (2, 1)) | |
327 | ||
328 | # Right Column | |
329 | ||
330 | fieldsizer.Add(wx.StaticText(self.panel, -1, "X (px):"), pos = (0, 2), flag = wx.ALIGN_CENTER_VERTICAL) | |
331 | projectX = int(math.floor(float(self._get_setting("project_x", 1920)))) | |
332 | self.X = wx.SpinCtrl(self.panel, -1, str(projectX), max = 999999, size = (80, -1)) | |
333 | self.X.Bind(wx.EVT_SPINCTRL, self.update_resolution) | |
334 | self.X.SetHelpText("The projector resolution in the X axis.") | |
335 | fieldsizer.Add(self.X, pos = (0, 3)) | |
336 | ||
337 | fieldsizer.Add(wx.StaticText(self.panel, -1, "Y (px):"), pos = (1, 2), flag = wx.ALIGN_CENTER_VERTICAL) | |
338 | projectY = int(math.floor(float(self._get_setting("project_y", 1200)))) | |
339 | self.Y = wx.SpinCtrl(self.panel, -1, str(projectY), max = 999999, size = (80, -1)) | |
340 | self.Y.Bind(wx.EVT_SPINCTRL, self.update_resolution) | |
341 | self.Y.SetHelpText("The projector resolution in the Y axis.") | |
342 | fieldsizer.Add(self.Y, pos = (1, 3)) | |
343 | ||
344 | fieldsizer.Add(wx.StaticText(self.panel, -1, "OffsetX (mm):"), pos = (2, 2), flag = wx.ALIGN_CENTER_VERTICAL) | |
345 | self.offset_X = floatspin.FloatSpin(self.panel, -1, value = self._get_setting("project_offset_x", 0.0), increment = 1, digits = 1, size = (80, -1)) | |
346 | self.offset_X.Bind(floatspin.EVT_FLOATSPIN, self.update_offset) | |
347 | self.offset_X.SetHelpText("How far the slice should be offset from the edge in the X axis.") | |
348 | fieldsizer.Add(self.offset_X, pos = (2, 3)) | |
349 | ||
350 | fieldsizer.Add(wx.StaticText(self.panel, -1, "OffsetY (mm):"), pos = (3, 2), flag = wx.ALIGN_CENTER_VERTICAL) | |
351 | self.offset_Y = floatspin.FloatSpin(self.panel, -1, value = self._get_setting("project_offset_y", 0.0), increment = 1, digits = 1, size = (80, -1)) | |
352 | self.offset_Y.Bind(floatspin.EVT_FLOATSPIN, self.update_offset) | |
353 | self.offset_Y.SetHelpText("How far the slice should be offset from the edge in the Y axis.") | |
354 | fieldsizer.Add(self.offset_Y, pos = (3, 3)) | |
355 | ||
356 | fieldsizer.Add(wx.StaticText(self.panel, -1, "ProjectedX (mm):"), pos = (4, 2), flag = wx.ALIGN_CENTER_VERTICAL) | |
357 | self.projected_X_mm = floatspin.FloatSpin(self.panel, -1, value = self._get_setting("project_projected_x", 505.0), increment = 1, digits = 1, size = (80, -1)) | |
358 | self.projected_X_mm.Bind(floatspin.EVT_FLOATSPIN, self.update_projected_Xmm) | |
359 | self.projected_X_mm.SetHelpText("The actual width of the entire projected image. Use the Calibrate grid to show the full size of the projected image, and measure the width at the same level where the slice will be projected onto the resin.") | |
360 | fieldsizer.Add(self.projected_X_mm, pos = (4, 3)) | |
361 | ||
362 | fieldsizer.Add(wx.StaticText(self.panel, -1, "Z Axis Speed (mm/min):"), pos = (5, 2), flag = wx.ALIGN_CENTER_VERTICAL) | |
363 | self.z_axis_rate = wx.SpinCtrl(self.panel, -1, str(self._get_setting("project_z_axis_rate", 200)), max = 9999, size = (80, -1)) | |
364 | self.z_axis_rate.Bind(wx.EVT_SPINCTRL, self.update_z_axis_rate) | |
365 | self.z_axis_rate.SetHelpText("Speed of the Z axis in mm/minute. Take into account that slower rates may require a longer pause value.") | |
366 | fieldsizer.Add(self.z_axis_rate, pos = (5, 3)) | |
367 | ||
368 | fieldboxsizer.Add(fieldsizer) | |
369 | ||
370 | # Display | |
371 | ||
372 | displayboxsizer = wx.StaticBoxSizer(wx.StaticBox(self.panel, label = "Display"), wx.VERTICAL) | |
373 | displaysizer = wx.GridBagSizer(10, 10) | |
374 | ||
375 | displaysizer.Add(wx.StaticText(self.panel, -1, "Fullscreen:"), pos = (0, 0), flag = wx.ALIGN_CENTER_VERTICAL) | |
376 | self.fullscreen = wx.CheckBox(self.panel, -1) | |
377 | self.fullscreen.Bind(wx.EVT_CHECKBOX, self.update_fullscreen) | |
378 | self.fullscreen.SetHelpText("Toggles the project screen to full size.") | |
379 | displaysizer.Add(self.fullscreen, pos = (0, 1), flag = wx.ALIGN_CENTER_VERTICAL) | |
380 | ||
381 | displaysizer.Add(wx.StaticText(self.panel, -1, "Calibrate:"), pos = (0, 2), flag = wx.ALIGN_CENTER_VERTICAL) | |
382 | self.calibrate = wx.CheckBox(self.panel, -1) | |
383 | self.calibrate.Bind(wx.EVT_CHECKBOX, self.show_calibrate) | |
384 | self.calibrate.SetHelpText("Toggles the calibration grid. Each grid should be 10mmx10mm in size. Use the grid to ensure the projected size is correct. See also the help for the ProjectedX field.") | |
385 | displaysizer.Add(self.calibrate, pos = (0, 3), flag = wx.ALIGN_CENTER_VERTICAL) | |
386 | ||
387 | displaysizer.Add(wx.StaticText(self.panel, -1, "1st Layer:"), pos = (0, 4), flag = wx.ALIGN_CENTER_VERTICAL) | |
388 | ||
389 | first_layer_boxer = wx.BoxSizer(wx.HORIZONTAL) | |
390 | self.first_layer = wx.CheckBox(self.panel, -1) | |
391 | self.first_layer.Bind(wx.EVT_CHECKBOX, self.show_first_layer) | |
392 | self.first_layer.SetHelpText("Displays the first layer of the model. Use this to project the first layer for longer so it holds to the base. Note: this value does not affect the first layer when the \"Present\" run is started, it should be used manually.") | |
393 | ||
394 | first_layer_boxer.Add(self.first_layer, flag = wx.ALIGN_CENTER_VERTICAL) | |
395 | ||
396 | first_layer_boxer.Add(wx.StaticText(self.panel, -1, " (s):"), flag = wx.ALIGN_CENTER_VERTICAL) | |
397 | self.show_first_layer_timer = floatspin.FloatSpin(self.panel, -1, value=-1, increment = 1, digits = 1, size = (55, -1)) | |
398 | self.show_first_layer_timer.SetHelpText("How long to display the first layer for. -1 = unlimited.") | |
399 | first_layer_boxer.Add(self.show_first_layer_timer, flag = wx.ALIGN_CENTER_VERTICAL) | |
400 | displaysizer.Add(first_layer_boxer, pos = (0, 6), flag = wx.ALIGN_CENTER_VERTICAL) | |
401 | ||
402 | displaysizer.Add(wx.StaticText(self.panel, -1, "Red:"), pos = (0, 7), flag = wx.ALIGN_CENTER_VERTICAL) | |
403 | self.layer_red = wx.CheckBox(self.panel, -1) | |
404 | self.layer_red.Bind(wx.EVT_CHECKBOX, self.show_layer_red) | |
405 | self.layer_red.SetHelpText("Toggles whether the image should be red. Useful for positioning whilst resin is in the printer as it should not cause a reaction.") | |
406 | displaysizer.Add(self.layer_red, pos = (0, 8), flag = wx.ALIGN_CENTER_VERTICAL) | |
407 | ||
408 | displayboxsizer.Add(displaysizer) | |
409 | ||
410 | # Info | |
411 | infosizer = wx.StaticBoxSizer(wx.StaticBox(self.panel, label = "Info"), wx.VERTICAL) | |
412 | ||
413 | infofieldsizer = wx.GridBagSizer(10, 10) | |
414 | ||
415 | filelabel = wx.StaticText(self.panel, -1, "File:") | |
416 | filelabel.SetHelpText("The name of the model currently loaded.") | |
417 | infofieldsizer.Add(filelabel, pos = (0, 0)) | |
418 | self.filename = wx.StaticText(self.panel, -1, "") | |
419 | ||
420 | infofieldsizer.Add(self.filename, pos = (0, 1)) | |
421 | ||
422 | totallayerslabel = wx.StaticText(self.panel, -1, "Total Layers:") | |
423 | totallayerslabel.SetHelpText("The total number of layers found in the model.") | |
424 | infofieldsizer.Add(totallayerslabel, pos = (1, 0)) | |
425 | self.total_layers = wx.StaticText(self.panel, -1) | |
426 | ||
427 | infofieldsizer.Add(self.total_layers, pos = (1, 1)) | |
428 | ||
429 | currentlayerlabel = wx.StaticText(self.panel, -1, "Current Layer:") | |
430 | currentlayerlabel.SetHelpText("The current layer being displayed.") | |
431 | infofieldsizer.Add(currentlayerlabel, pos = (2, 0)) | |
432 | self.current_layer = wx.StaticText(self.panel, -1, "0") | |
433 | infofieldsizer.Add(self.current_layer, pos = (2, 1)) | |
434 | ||
435 | estimatedtimelabel = wx.StaticText(self.panel, -1, "Estimated Time:") | |
436 | estimatedtimelabel.SetHelpText("An estimate of the remaining time until print completion.") | |
437 | infofieldsizer.Add(estimatedtimelabel, pos = (3, 0)) | |
438 | self.estimated_time = wx.StaticText(self.panel, -1, "") | |
439 | infofieldsizer.Add(self.estimated_time, pos = (3, 1)) | |
440 | ||
441 | infosizer.Add(infofieldsizer) | |
442 | ||
443 | # | |
444 | ||
445 | vbox.Add(buttonbox, flag = wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP | wx.BOTTOM, border = 10) | |
446 | vbox.Add(fieldboxsizer, flag = wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM, border = 10) | |
447 | vbox.Add(displayboxsizer, flag = wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM, border = 10) | |
448 | vbox.Add(infosizer, flag = wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM, border = 10) | |
449 | ||
450 | self.panel.SetSizer(vbox) | |
451 | self.panel.Fit() | |
452 | self.Fit() | |
453 | self.SetPosition((0, 0)) | |
454 | self.Show() | |
455 | ||
456 | def __del__(self): | |
457 | if hasattr(self, 'image_dir') and self.image_dir != '': | |
458 | shutil.rmtree(self.image_dir) | |
459 | if self.display_frame: | |
460 | self.display_frame.Destroy() | |
461 | ||
462 | def set_total_layers(self, total): | |
463 | self.total_layers.SetLabel(str(total)) | |
464 | self.set_estimated_time() | |
465 | ||
466 | def set_current_layer(self, index): | |
467 | self.current_layer.SetLabel(str(index)) | |
468 | self.set_estimated_time() | |
469 | ||
470 | def display_filename(self, name): | |
471 | self.filename.SetLabel(name) | |
472 | ||
473 | def set_estimated_time(self): | |
474 | if not hasattr(self, 'layers'): | |
475 | return | |
476 | ||
477 | current_layer = int(self.current_layer.GetLabel()) | |
478 | remaining_layers = len(self.layers[0]) - current_layer | |
479 | # 0.5 for delay between hide and rise | |
480 | estimated_time = remaining_layers * (float(self.interval.GetValue()) + float(self.pause.GetValue()) + 0.5) | |
481 | self.estimated_time.SetLabel(time.strftime("%H:%M:%S", time.gmtime(estimated_time))) | |
482 | ||
483 | def parse_svg(self, name): | |
484 | et = xml.etree.ElementTree.ElementTree(file = name) | |
485 | # xml.etree.ElementTree.dump(et) | |
486 | ||
487 | slicer = 'Slic3r' if et.getroot().find('{http://www.w3.org/2000/svg}metadata') is None else 'Skeinforge' | |
488 | zlast = 0 | |
489 | zdiff = 0 | |
490 | ol = [] | |
491 | if (slicer == 'Slic3r'): | |
492 | height = et.getroot().get('height').replace('m', '') | |
493 | width = et.getroot().get('width').replace('m', '') | |
494 | ||
495 | for i in et.findall("{http://www.w3.org/2000/svg}g"): | |
496 | z = float(i.get('{http://slic3r.org/namespaces/slic3r}z')) | |
497 | zdiff = z - zlast | |
498 | zlast = z | |
499 | ||
500 | svgSnippet = xml.etree.ElementTree.Element('{http://www.w3.org/2000/svg}svg') | |
501 | svgSnippet.set('height', height + 'mm') | |
502 | svgSnippet.set('width', width + 'mm') | |
503 | ||
504 | svgSnippet.set('viewBox', '0 0 ' + width + ' ' + height) | |
505 | svgSnippet.set('style', 'background-color:black;fill:white;') | |
506 | svgSnippet.append(i) | |
507 | ||
508 | ol += [svgSnippet] | |
509 | else: | |
510 | ||
511 | slice_layers = et.findall("{http://www.w3.org/2000/svg}metadata")[0].findall("{http://www.reprap.org/slice}layers")[0] | |
512 | minX = slice_layers.get('minX') | |
513 | maxX = slice_layers.get('maxX') | |
514 | minY = slice_layers.get('minY') | |
515 | maxY = slice_layers.get('maxY') | |
516 | ||
517 | height = str(abs(float(minY)) + abs(float(maxY))) | |
518 | width = str(abs(float(minX)) + abs(float(maxX))) | |
519 | ||
520 | for g in et.findall("{http://www.w3.org/2000/svg}g")[0].findall("{http://www.w3.org/2000/svg}g"): | |
521 | ||
522 | g.set('transform', '') | |
523 | ||
524 | text_element = g.findall("{http://www.w3.org/2000/svg}text")[0] | |
525 | g.remove(text_element) | |
526 | ||
527 | path_elements = g.findall("{http://www.w3.org/2000/svg}path") | |
528 | for p in path_elements: | |
529 | p.set('transform', 'translate(' + maxX + ',' + maxY + ')') | |
530 | p.set('fill', 'white') | |
531 | ||
532 | z = float(g.get('id').split("z:")[-1]) | |
533 | zdiff = z - zlast | |
534 | zlast = z | |
535 | ||
536 | svgSnippet = xml.etree.ElementTree.Element('{http://www.w3.org/2000/svg}svg') | |
537 | svgSnippet.set('height', height + 'mm') | |
538 | svgSnippet.set('width', width + 'mm') | |
539 | ||
540 | svgSnippet.set('viewBox', '0 0 ' + width + ' ' + height) | |
541 | svgSnippet.set('style', 'background-color:black;fill:white;') | |
542 | svgSnippet.append(g) | |
543 | ||
544 | ol += [svgSnippet] | |
545 | return ol, zdiff, slicer | |
546 | ||
547 | def parse_3DLP_zip(self, name): | |
548 | if not zipfile.is_zipfile(name): | |
549 | raise Exception(name + " is not a zip file!") | |
550 | accepted_image_types = ['gif', 'tiff', 'jpg', 'jpeg', 'bmp', 'png'] | |
551 | zipFile = zipfile.ZipFile(name, 'r') | |
552 | self.image_dir = tempfile.mkdtemp() | |
553 | zipFile.extractall(self.image_dir) | |
554 | ol = [] | |
555 | ||
556 | # Note: the following funky code extracts any numbers from the filenames, matches | |
557 | # them with the original then sorts them. It allows for filenames of the | |
558 | # format: abc_1.png, which would be followed by abc_10.png alphabetically. | |
559 | os.chdir(self.image_dir) | |
560 | vals = filter(os.path.isfile, os.listdir('.')) | |
561 | keys = map(lambda p: int(re.search('\d+', p).group()), vals) | |
562 | imagefilesDict = dict(itertools.izip(keys, vals)) | |
563 | imagefilesOrderedDict = OrderedDict(sorted(imagefilesDict.items(), key = lambda t: t[0])) | |
564 | ||
565 | for f in imagefilesOrderedDict.values(): | |
566 | path = os.path.join(self.image_dir, f) | |
567 | if os.path.isfile(path) and imghdr.what(path) in accepted_image_types: | |
568 | ol.append(path) | |
569 | ||
570 | return ol, -1, "bitmap" | |
571 | ||
572 | def load_file(self, event): | |
573 | dlg = wx.FileDialog(self, ("Open file to print"), style = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) | |
574 | dlg.SetWildcard(("Slic3r or Skeinforge svg files (;*.svg;*.SVG;);3DLP Zip (;*.3dlp.zip;)")) | |
575 | if dlg.ShowModal() == wx.ID_OK: | |
576 | name = dlg.GetPath() | |
577 | if not(os.path.exists(name)): | |
578 | self.status.SetStatusText(("File not found!")) | |
579 | return | |
580 | if name.endswith(".3dlp.zip"): | |
581 | layers = self.parse_3DLP_zip(name) | |
582 | layerHeight = float(self.thickness.GetValue()) | |
583 | else: | |
584 | layers = self.parse_svg(name) | |
585 | layerHeight = layers[1] | |
586 | self.thickness.SetValue(str(layers[1])) | |
587 | print "Layer thickness detected:", layerHeight, "mm" | |
588 | print len(layers[0]), "layers found, total height", layerHeight * len(layers[0]), "mm" | |
589 | self.layers = layers | |
590 | self.set_total_layers(len(layers[0])) | |
591 | self.set_current_layer(0) | |
592 | self.current_filename = os.path.basename(name) | |
593 | self.display_filename(self.current_filename) | |
594 | self.slicer = layers[2] | |
595 | self.display_frame.slicer = self.slicer | |
596 | dlg.Destroy() | |
597 | ||
598 | def show_calibrate(self, event): | |
599 | if self.calibrate.IsChecked(): | |
600 | self.present_calibrate(event) | |
601 | else: | |
602 | if hasattr(self, 'layers'): | |
603 | self.display_frame.slicer = self.layers[2] | |
604 | self.display_frame.scale = float(self.scale.GetValue()) | |
605 | self.display_frame.clear_layer() | |
606 | ||
607 | def show_first_layer(self, event): | |
608 | if self.first_layer.IsChecked(): | |
609 | self.present_first_layer(event) | |
610 | else: | |
611 | if hasattr(self, 'layers'): | |
612 | self.display_frame.slicer = self.layers[2] | |
613 | self.display_frame.scale = float(self.scale.GetValue()) | |
614 | self.display_frame.clear_layer() | |
615 | ||
616 | def show_layer_red(self, event): | |
617 | self.display_frame.layer_red = self.layer_red.IsChecked() | |
618 | ||
619 | def present_calibrate(self, event): | |
620 | if self.calibrate.IsChecked(): | |
621 | self.display_frame.Raise() | |
622 | self.display_frame.offset = (float(self.offset_X.GetValue()), -float(self.offset_Y.GetValue())) | |
623 | self.display_frame.scale = 1.0 | |
624 | resolution_x_pixels = int(self.X.GetValue()) | |
625 | resolution_y_pixels = int(self.Y.GetValue()) | |
626 | ||
627 | gridBitmap = wx.EmptyBitmap(resolution_x_pixels, resolution_y_pixels) | |
628 | dc = wx.MemoryDC() | |
629 | dc.SelectObject(gridBitmap) | |
630 | dc.SetBackground(wx.Brush("black")) | |
631 | dc.Clear() | |
632 | ||
633 | dc.SetPen(wx.Pen("red", 7)) | |
634 | dc.DrawLine(0, 0, resolution_x_pixels, 0) | |
635 | dc.DrawLine(0, 0, 0, resolution_y_pixels) | |
636 | dc.DrawLine(resolution_x_pixels, 0, resolution_x_pixels, resolution_y_pixels) | |
637 | dc.DrawLine(0, resolution_y_pixels, resolution_x_pixels, resolution_y_pixels) | |
638 | ||
639 | dc.SetPen(wx.Pen("red", 2)) | |
640 | aspectRatio = float(resolution_x_pixels) / float(resolution_y_pixels) | |
641 | ||
642 | projectedXmm = float(self.projected_X_mm.GetValue()) | |
643 | projectedYmm = round(projectedXmm / aspectRatio) | |
644 | ||
645 | pixelsXPerMM = resolution_x_pixels / projectedXmm | |
646 | pixelsYPerMM = resolution_y_pixels / projectedYmm | |
647 | ||
648 | gridCountX = int(projectedXmm / 10) | |
649 | gridCountY = int(projectedYmm / 10) | |
650 | ||
651 | for y in xrange(0, gridCountY + 1): | |
652 | for x in xrange(0, gridCountX + 1): | |
653 | dc.DrawLine(0, y * (pixelsYPerMM * 10), resolution_x_pixels, y * (pixelsYPerMM * 10)) | |
654 | dc.DrawLine(x * (pixelsXPerMM * 10), 0, x * (pixelsXPerMM * 10), resolution_y_pixels) | |
655 | ||
656 | self.first_layer.SetValue(False) | |
657 | self.display_frame.slicer = 'bitmap' | |
658 | self.display_frame.draw_layer(gridBitmap.ConvertToImage()) | |
659 | ||
660 | def present_first_layer(self, event): | |
661 | if (self.first_layer.GetValue()): | |
662 | if not hasattr(self, "layers"): | |
663 | print "No model loaded!" | |
664 | self.first_layer.SetValue(False) | |
665 | return | |
666 | self.display_frame.offset = (float(self.offset_X.GetValue()), float(self.offset_Y.GetValue())) | |
667 | self.display_frame.scale = float(self.scale.GetValue()) | |
668 | ||
669 | self.display_frame.slicer = self.layers[2] | |
670 | self.display_frame.dpi = self.get_dpi() | |
671 | self.display_frame.draw_layer(copy.deepcopy(self.layers[0][0])) | |
672 | self.calibrate.SetValue(False) | |
673 | if self.show_first_layer_timer != -1.0: | |
674 | def unpresent_first_layer(): | |
675 | self.display_frame.clear_layer() | |
676 | self.first_layer.SetValue(False) | |
677 | wx.CallLater(self.show_first_layer_timer.GetValue() * 1000, unpresent_first_layer) | |
678 | ||
679 | def update_offset(self, event): | |
680 | ||
681 | offset_x = float(self.offset_X.GetValue()) | |
682 | offset_y = float(self.offset_Y.GetValue()) | |
683 | self.display_frame.offset = (offset_x, offset_y) | |
684 | ||
685 | self._set_setting('project_offset_x', offset_x) | |
686 | self._set_setting('project_offset_y', offset_y) | |
687 | ||
688 | self.refresh_display(event) | |
689 | ||
690 | def refresh_display(self, event): | |
691 | self.present_calibrate(event) | |
692 | self.present_first_layer(event) | |
693 | ||
694 | def update_thickness(self, event): | |
695 | self._set_setting('project_layer', self.thickness.GetValue()) | |
696 | self.refresh_display(event) | |
697 | ||
698 | def update_projected_Xmm(self, event): | |
699 | self._set_setting('project_projected_x', self.projected_X_mm.GetValue()) | |
700 | self.refresh_display(event) | |
701 | ||
702 | def update_scale(self, event): | |
703 | scale = float(self.scale.GetValue()) | |
704 | self.display_frame.scale = scale | |
705 | self._set_setting('project_scale', scale) | |
706 | self.refresh_display(event) | |
707 | ||
708 | def update_interval(self, event): | |
709 | interval = float(self.interval.GetValue()) | |
710 | self.display_frame.interval = interval | |
711 | self._set_setting('project_interval', interval) | |
712 | self.set_estimated_time() | |
713 | self.refresh_display(event) | |
714 | ||
715 | def update_pause(self, event): | |
716 | pause = float(self.pause.GetValue()) | |
717 | self.display_frame.pause = pause | |
718 | self._set_setting('project_pause', pause) | |
719 | self.set_estimated_time() | |
720 | self.refresh_display(event) | |
721 | ||
722 | def update_overshoot(self, event): | |
723 | overshoot = float(self.overshoot.GetValue()) | |
724 | self.display_frame.pause = overshoot | |
725 | self._set_setting('project_overshoot', overshoot) | |
726 | ||
727 | def update_prelift_gcode(self, event): | |
728 | prelift_gcode = self.prelift_gcode.GetValue().replace('\n', "\\n") | |
729 | self.display_frame.prelift_gcode = prelift_gcode | |
730 | self._set_setting('project_prelift_gcode', prelift_gcode) | |
731 | ||
732 | def update_postlift_gcode(self, event): | |
733 | postlift_gcode = self.postlift_gcode.GetValue().replace('\n', "\\n") | |
734 | self.display_frame.postlift_gcode = postlift_gcode | |
735 | self._set_setting('project_postlift_gcode', postlift_gcode) | |
736 | ||
737 | def update_z_axis_rate(self, event): | |
738 | z_axis_rate = int(self.z_axis_rate.GetValue()) | |
739 | self.display_frame.z_axis_rate = z_axis_rate | |
740 | self._set_setting('project_z_axis_rate', z_axis_rate) | |
741 | ||
742 | def update_direction(self, event): | |
743 | direction = self.direction.GetValue() | |
744 | self.display_frame.direction = direction | |
745 | self._set_setting('project_direction', direction) | |
746 | ||
747 | def update_fullscreen(self, event): | |
748 | if (self.fullscreen.GetValue()): | |
749 | self.display_frame.ShowFullScreen(1) | |
750 | else: | |
751 | self.display_frame.ShowFullScreen(0) | |
752 | self.refresh_display(event) | |
753 | ||
754 | def update_resolution(self, event): | |
755 | x = int(self.X.GetValue()) | |
756 | y = int(self.Y.GetValue()) | |
757 | self.display_frame.resize((x, y)) | |
758 | self._set_setting('project_x', x) | |
759 | self._set_setting('project_y', y) | |
760 | self.refresh_display(event) | |
761 | ||
762 | def get_dpi(self): | |
763 | resolution_x_pixels = int(self.X.GetValue()) | |
764 | projected_x_mm = float(self.projected_X_mm.GetValue()) | |
765 | projected_x_inches = projected_x_mm / 25.4 | |
766 | ||
767 | return resolution_x_pixels / projected_x_inches | |
768 | ||
769 | def start_present(self, event): | |
770 | if not hasattr(self, "layers"): | |
771 | print "No model loaded!" | |
772 | return | |
773 | ||
774 | self.pause_button.SetLabel("Pause") | |
775 | self.set_current_layer(0) | |
776 | self.display_frame.Raise() | |
777 | if (self.fullscreen.GetValue()): | |
778 | self.display_frame.ShowFullScreen(1) | |
779 | self.display_frame.slicer = self.layers[2] | |
780 | self.display_frame.dpi = self.get_dpi() | |
781 | self.display_frame.present(self.layers[0][:], | |
782 | thickness = float(self.thickness.GetValue()), | |
783 | interval = float(self.interval.GetValue()), | |
784 | scale = float(self.scale.GetValue()), | |
785 | pause = float(self.pause.GetValue()), | |
786 | overshoot = float(self.overshoot.GetValue()), | |
787 | z_axis_rate = int(self.z_axis_rate.GetValue()), | |
788 | prelift_gcode = self.prelift_gcode.GetValue(), | |
789 | postlift_gcode = self.postlift_gcode.GetValue(), | |
790 | direction = self.direction.GetValue(), | |
791 | size = (float(self.X.GetValue()), float(self.Y.GetValue())), | |
792 | offset = (float(self.offset_X.GetValue()), float(self.offset_Y.GetValue())), | |
793 | layer_red = self.layer_red.IsChecked()) | |
794 | ||
795 | def stop_present(self, event): | |
796 | print "Stop" | |
797 | self.pause_button.SetLabel("Pause") | |
798 | self.set_current_layer(0) | |
799 | self.display_frame.running = False | |
800 | ||
801 | def pause_present(self, event): | |
802 | if self.pause_button.GetLabel() == 'Pause': | |
803 | print "Pause" | |
804 | self.pause_button.SetLabel("Continue") | |
805 | self.display_frame.running = False | |
806 | else: | |
807 | print "Continue" | |
808 | self.pause_button.SetLabel("Pause") | |
809 | self.display_frame.running = True | |
810 | self.display_frame.next_img() | |
811 | ||
812 | if __name__ == "__main__": | |
813 | provider = wx.SimpleHelpProvider() | |
814 | wx.HelpProvider_Set(provider) | |
815 | a = wx.App() | |
816 | SettingsFrame(None).Show() | |
817 | a.MainLoop() |