printrun-src/printrun/stlplater.py

Fri, 03 Jun 2016 09:16:07 +0200

author
mbayer
date
Fri, 03 Jun 2016 09:16:07 +0200
changeset 15
0bbb006204fc
child 46
cce0af6351f0
permissions
-rw-r--r--

Added printrun sourcecode from
https://github.com/kliment/Printrun
03.06.2016 09:10

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 import os
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
19
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
20 # Set up Internationalization using gettext
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
21 # searching for installed locales on /usr/share; uses relative folder if not found (windows)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
22 from .utils import install_locale
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
23 install_locale('pronterface')
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
24
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
25 import wx
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
26 import time
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
27 import logging
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
28 import threading
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
29 import math
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
30 import sys
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
31 import re
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
32 import traceback
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
33 import subprocess
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
34 from copy import copy
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
35
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
36 from printrun import stltool
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
37 from printrun.objectplater import make_plater, PlaterPanel
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
38
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
39 glview = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
40 if "-nogl" not in sys.argv:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
41 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
42 from printrun import stlview
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
43 glview = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
44 except:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
45 logging.warning("Could not load 3D viewer for plater:"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
46 + "\n" + traceback.format_exc())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
47
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
48
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
49 def evalme(s):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
50 return eval(s[s.find("(") + 1:s.find(")")])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
51
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
52 def transformation_matrix(model):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
53 matrix = stltool.I
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
54 if any(model.centeroffset):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
55 matrix = model.translation_matrix(model.centeroffset).dot(matrix)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
56 if model.rot:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
57 matrix = model.rotation_matrix([0, 0, model.rot]).dot(matrix)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
58 if any(model.offsets):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
59 matrix = model.translation_matrix(model.offsets).dot(matrix)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
60 return matrix
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
61
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
62 class showstl(wx.Window):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
63 def __init__(self, parent, size, pos):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
64 wx.Window.__init__(self, parent, size = size, pos = pos)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
65 self.i = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
66 self.parent = parent
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
67 self.previ = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
68 self.Bind(wx.EVT_MOUSEWHEEL, self.rot)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
69 self.Bind(wx.EVT_MOUSE_EVENTS, self.move)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
70 self.Bind(wx.EVT_PAINT, self.repaint)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
71 self.Bind(wx.EVT_KEY_DOWN, self.keypress)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
72 self.triggered = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
73 self.initpos = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
74 self.prevsel = -1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
75
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
76 def prepare_model(self, m, scale):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
77 m.bitmap = wx.EmptyBitmap(800, 800, 32)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
78 dc = wx.MemoryDC()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
79 dc.SelectObject(m.bitmap)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
80 dc.SetBackground(wx.Brush((0, 0, 0, 0)))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
81 dc.SetBrush(wx.Brush((0, 0, 0, 255)))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
82 dc.SetBrush(wx.Brush(wx.Colour(128, 255, 128)))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
83 dc.SetPen(wx.Pen(wx.Colour(128, 128, 128)))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
84 for i in m.facets:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
85 dc.DrawPolygon([wx.Point(400 + scale * p[0], (400 - scale * p[1])) for p in i[1]])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
86 dc.SelectObject(wx.NullBitmap)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
87 m.bitmap.SetMask(wx.Mask(m.bitmap, wx.Colour(0, 0, 0, 255)))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
88
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
89 def move_shape(self, delta):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
90 """moves shape (selected in l, which is list ListBox of shapes)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
91 by an offset specified in tuple delta.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
92 Positive numbers move to (rigt, down)"""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
93 name = self.parent.l.GetSelection()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
94 if name == wx.NOT_FOUND:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
95 return False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
96 name = self.parent.l.GetString(name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
97 model = self.parent.models[name]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
98 model.offsets = [model.offsets[0] + delta[0],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
99 model.offsets[1] + delta[1],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
100 model.offsets[2]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
101 ]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
102 self.Refresh()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
103 return True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
104
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
105 def move(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
106 if event.ButtonUp(wx.MOUSE_BTN_LEFT):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
107 if self.initpos is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
108 currentpos = event.GetPositionTuple()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
109 delta = (0.5 * (currentpos[0] - self.initpos[0]),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
110 -0.5 * (currentpos[1] - self.initpos[1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
111 )
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
112 self.move_shape(delta)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
113 self.Refresh()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
114 self.initpos = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
115 elif event.ButtonDown(wx.MOUSE_BTN_RIGHT):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
116 self.parent.right(event)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
117 elif event.Dragging():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
118 if self.initpos is None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
119 self.initpos = event.GetPositionTuple()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
120 self.Refresh()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
121 dc = wx.ClientDC(self)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
122 p = event.GetPositionTuple()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
123 dc.DrawLine(self.initpos[0], self.initpos[1], p[0], p[1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
124 del dc
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
125 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
126 event.Skip()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
127
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
128 def rotate_shape(self, angle):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
129 """rotates acive shape
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
130 positive angle is clockwise
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
131 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
132 self.i += angle
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
133 if not self.triggered:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
134 self.triggered = 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
135 threading.Thread(target = self.cr).start()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
136
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
137 def keypress(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
138 """gets keypress events and moves/rotates acive shape"""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
139 keycode = event.GetKeyCode()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
140 step = 5
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
141 angle = 18
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
142 if event.ControlDown():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
143 step = 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
144 angle = 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
145 # h
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
146 if keycode == 72:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
147 self.move_shape((-step, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
148 # l
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
149 if keycode == 76:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
150 self.move_shape((step, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
151 # j
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
152 if keycode == 75:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
153 self.move_shape((0, step))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
154 # k
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
155 if keycode == 74:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
156 self.move_shape((0, -step))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
157 # [
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
158 if keycode == 91:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
159 self.rotate_shape(-angle)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
160 # ]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
161 if keycode == 93:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
162 self.rotate_shape(angle)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
163 event.Skip()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
164
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
165 def rotateafter(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
166 if self.i != self.previ:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
167 i = self.parent.l.GetSelection()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
168 if i != wx.NOT_FOUND:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
169 self.parent.models[self.parent.l.GetString(i)].rot -= 5 * (self.i - self.previ)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
170 self.previ = self.i
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
171 self.Refresh()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
172
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
173 def cr(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
174 time.sleep(0.01)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
175 wx.CallAfter(self.rotateafter)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
176 self.triggered = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
177
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
178 def rot(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
179 z = event.GetWheelRotation()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
180 s = self.parent.l.GetSelection()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
181 if self.prevsel != s:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
182 self.i = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
183 self.prevsel = s
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
184 if z < 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
185 self.rotate_shape(-1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
186 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
187 self.rotate_shape(1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
188
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
189 def repaint(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
190 dc = wx.PaintDC(self)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
191 self.paint(dc = dc)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
192
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
193 def paint(self, coord1 = "x", coord2 = "y", dc = None):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
194 if dc is None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
195 dc = wx.ClientDC(self)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
196 scale = 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
197 dc.SetPen(wx.Pen(wx.Colour(100, 100, 100)))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
198 for i in xrange(20):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
199 dc.DrawLine(0, i * scale * 10, 400, i * scale * 10)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
200 dc.DrawLine(i * scale * 10, 0, i * scale * 10, 400)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
201 dc.SetPen(wx.Pen(wx.Colour(0, 0, 0)))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
202 for i in xrange(4):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
203 dc.DrawLine(0, i * scale * 50, 400, i * scale * 50)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
204 dc.DrawLine(i * scale * 50, 0, i * scale * 50, 400)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
205 dc.SetBrush(wx.Brush(wx.Colour(128, 255, 128)))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
206 dc.SetPen(wx.Pen(wx.Colour(128, 128, 128)))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
207 dcs = wx.MemoryDC()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
208 for m in self.parent.models.values():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
209 b = m.bitmap
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
210 im = b.ConvertToImage()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
211 imgc = wx.Point(im.GetWidth() / 2, im.GetHeight() / 2)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
212 im = im.Rotate(math.radians(m.rot), imgc, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
213 bm = wx.BitmapFromImage(im)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
214 dcs.SelectObject(bm)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
215 bsz = bm.GetSize()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
216 dc.Blit(scale * m.offsets[0] - bsz[0] / 2, 400 - (scale * m.offsets[1] + bsz[1] / 2), bsz[0], bsz[1], dcs, 0, 0, useMask = 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
217 del dc
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
218
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
219 class StlPlaterPanel(PlaterPanel):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
220
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
221 load_wildcard = _("STL files (*.stl;*.STL)|*.stl;*.STL|OpenSCAD files (*.scad)|*.scad")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
222 save_wildcard = _("STL files (*.stl;*.STL)|*.stl;*.STL")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
223
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
224 def prepare_ui(self, filenames = [], callback = None,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
225 parent = None, build_dimensions = None, circular_platform = False,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
226 simarrange_path = None, antialias_samples = 0):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
227 super(StlPlaterPanel, self).prepare_ui(filenames, callback, parent, build_dimensions)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
228 self.cutting = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
229 self.cutting_axis = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
230 self.cutting_dist = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
231 if glview:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
232 viewer = stlview.StlViewPanel(self, (580, 580),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
233 build_dimensions = self.build_dimensions,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
234 circular = circular_platform,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
235 antialias_samples = antialias_samples)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
236 # Cutting tool
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
237 nrows = self.menusizer.GetRows()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
238 self.menusizer.Add(wx.StaticText(self.menupanel, -1, _("Cut along:")),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
239 pos = (nrows, 0), span = (1, 1), flag = wx.ALIGN_CENTER)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
240 cutconfirmbutton = wx.Button(self.menupanel, label = _("Confirm cut"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
241 cutconfirmbutton.Bind(wx.EVT_BUTTON, self.cut_confirm)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
242 cutconfirmbutton.Disable()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
243 self.cutconfirmbutton = cutconfirmbutton
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
244 self.menusizer.Add(cutconfirmbutton, pos = (nrows, 1), span = (1, 1), flag = wx.EXPAND)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
245 cutpanel = wx.Panel(self.menupanel, -1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
246 cutsizer = self.cutsizer = wx.BoxSizer(wx.HORIZONTAL)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
247 cutpanel.SetSizer(cutsizer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
248 cutxplusbutton = wx.ToggleButton(cutpanel, label = _(">X"), style = wx.BU_EXACTFIT)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
249 cutxplusbutton.Bind(wx.EVT_TOGGLEBUTTON, lambda event: self.start_cutting_tool(event, "x", 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
250 cutsizer.Add(cutxplusbutton, 1, flag = wx.EXPAND)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
251 cutzplusbutton = wx.ToggleButton(cutpanel, label = _(">Y"), style = wx.BU_EXACTFIT)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
252 cutzplusbutton.Bind(wx.EVT_TOGGLEBUTTON, lambda event: self.start_cutting_tool(event, "y", 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
253 cutsizer.Add(cutzplusbutton, 1, flag = wx.EXPAND)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
254 cutzplusbutton = wx.ToggleButton(cutpanel, label = _(">Z"), style = wx.BU_EXACTFIT)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
255 cutzplusbutton.Bind(wx.EVT_TOGGLEBUTTON, lambda event: self.start_cutting_tool(event, "z", 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
256 cutsizer.Add(cutzplusbutton, 1, flag = wx.EXPAND)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
257 cutxminusbutton = wx.ToggleButton(cutpanel, label = _("<X"), style = wx.BU_EXACTFIT)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
258 cutxminusbutton.Bind(wx.EVT_TOGGLEBUTTON, lambda event: self.start_cutting_tool(event, "x", -1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
259 cutsizer.Add(cutxminusbutton, 1, flag = wx.EXPAND)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
260 cutzminusbutton = wx.ToggleButton(cutpanel, label = _("<Y"), style = wx.BU_EXACTFIT)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
261 cutzminusbutton.Bind(wx.EVT_TOGGLEBUTTON, lambda event: self.start_cutting_tool(event, "y", -1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
262 cutsizer.Add(cutzminusbutton, 1, flag = wx.EXPAND)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
263 cutzminusbutton = wx.ToggleButton(cutpanel, label = _("<Z"), style = wx.BU_EXACTFIT)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
264 cutzminusbutton.Bind(wx.EVT_TOGGLEBUTTON, lambda event: self.start_cutting_tool(event, "z", -1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
265 cutsizer.Add(cutzminusbutton, 1, flag = wx.EXPAND)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
266 self.menusizer.Add(cutpanel, pos = (nrows + 1, 0), span = (1, 2), flag = wx.EXPAND)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
267 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
268 viewer = showstl(self, (580, 580), (0, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
269 self.simarrange_path = simarrange_path
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
270 self.set_viewer(viewer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
271
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
272 def start_cutting_tool(self, event, axis, direction):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
273 toggle = event.GetEventObject()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
274 if toggle.GetValue():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
275 # Disable the other toggles
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
276 for child in self.cutsizer.GetChildren():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
277 child = child.GetWindow()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
278 if child != toggle:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
279 child.SetValue(False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
280 self.cutting = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
281 self.cutting_axis = axis
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
282 self.cutting_dist = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
283 self.cutting_direction = direction
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
284 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
285 self.cutting = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
286 self.cutting_axis = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
287 self.cutting_dist = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
288 self.cutting_direction = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
289
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
290 def cut_confirm(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
291 name = self.l.GetSelection()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
292 name = self.l.GetString(name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
293 model = self.models[name]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
294 transformation = transformation_matrix(model)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
295 transformed = model.transform(transformation)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
296 logging.info(_("Cutting %s alongside %s axis") % (name, self.cutting_axis))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
297 axes = ["x", "y", "z"]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
298 cut = transformed.cut(axes.index(self.cutting_axis),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
299 self.cutting_direction,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
300 self.cutting_dist)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
301 cut.offsets = [0, 0, 0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
302 cut.rot = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
303 cut.scale = model.scale
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
304 cut.filename = model.filename
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
305 cut.centeroffset = [0, 0, 0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
306 self.s.prepare_model(cut, 2)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
307 self.models[name] = cut
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
308 self.cutconfirmbutton.Disable()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
309 self.cutting = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
310 self.cutting_axis = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
311 self.cutting_dist = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
312 self.cutting_direction = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
313 for child in self.cutsizer.GetChildren():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
314 child = child.GetWindow()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
315 child.SetValue(False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
316
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
317 def clickcb(self, event, single = False):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
318 if not isinstance(self.s, stlview.StlViewPanel):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
319 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
320 if self.cutting:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
321 self.clickcb_cut(event)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
322 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
323 self.clickcb_rebase(event)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
324
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
325 def clickcb_cut(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
326 axis = self.cutting_axis
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
327 self.cutting_dist, _, _ = self.s.get_cutting_plane(axis, None,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
328 local_transform = True)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
329 if self.cutting_dist is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
330 self.cutconfirmbutton.Enable()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
331
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
332 def clickcb_rebase(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
333 x, y = event.GetPosition()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
334 ray_near, ray_far = self.s.mouse_to_ray(x, y, local_transform = True)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
335 best_match = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
336 best_facet = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
337 best_dist = float("inf")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
338 for key, model in self.models.iteritems():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
339 transformation = transformation_matrix(model)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
340 transformed = model.transform(transformation)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
341 if not transformed.intersect_box(ray_near, ray_far):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
342 logging.debug("Skipping %s for rebase search" % key)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
343 continue
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
344 facet, facet_dist = transformed.intersect(ray_near, ray_far)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
345 if facet is not None and facet_dist < best_dist:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
346 best_match = key
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
347 best_facet = facet
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
348 best_dist = facet_dist
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
349 if best_match is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
350 logging.info("Rebasing %s" % best_match)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
351 model = self.models[best_match]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
352 newmodel = model.rebase(best_facet)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
353 newmodel.offsets = list(model.offsets)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
354 newmodel.rot = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
355 newmodel.scale = model.scale
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
356 newmodel.filename = model.filename
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
357 newmodel.centeroffset = [-(newmodel.dims[1] + newmodel.dims[0]) / 2,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
358 -(newmodel.dims[3] + newmodel.dims[2]) / 2,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
359 0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
360 self.s.prepare_model(newmodel, 2)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
361 self.models[best_match] = newmodel
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
362 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
363
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
364 def done(self, event, cb):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
365 if not os.path.exists("tempstl"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
366 os.mkdir("tempstl")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
367 name = "tempstl/" + str(int(time.time()) % 10000) + ".stl"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
368 self.export_to(name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
369 if cb is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
370 cb(name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
371 if self.destroy_on_done:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
372 self.Destroy()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
373
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
374 def load_file(self, filename):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
375 if filename.lower().endswith(".stl"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
376 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
377 self.load_stl(filename)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
378 except:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
379 dlg = wx.MessageDialog(self, _("Loading STL file failed"),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
380 _("Error:") + traceback.format_exc(),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
381 wx.OK)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
382 dlg.ShowModal()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
383 logging.error(_("Loading STL file failed:")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
384 + "\n" + traceback.format_exc())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
385 elif filename.lower().endswith(".scad"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
386 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
387 self.load_scad(filename)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
388 except:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
389 dlg = wx.MessageDialog(self, _("Loading OpenSCAD file failed"),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
390 _("Error:") + traceback.format_exc(),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
391 wx.OK)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
392 dlg.ShowModal()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
393 logging.error(_("Loading OpenSCAD file failed:")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
394 + "\n" + traceback.format_exc())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
395
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
396 def load_scad(self, name):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
397 lf = open(name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
398 s = [i.replace("\n", "").replace("\r", "").replace(";", "") for i in lf if "stl" in i]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
399 lf.close()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
400
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
401 for i in s:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
402 parts = i.split()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
403 for part in parts:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
404 if 'translate' in part:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
405 translate_list = evalme(part)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
406 for part in parts:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
407 if 'rotate' in part:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
408 rotate_list = evalme(part)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
409 for part in parts:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
410 if 'import' in part:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
411 stl_file = evalme(part)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
412
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
413 newname = os.path.split(stl_file.lower())[1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
414 c = 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
415 while newname in self.models:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
416 newname = os.path.split(stl_file.lower())[1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
417 newname = newname + "(%d)" % c
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
418 c += 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
419 stl_path = os.path.join(os.path.split(name)[0:len(os.path.split(stl_file)) - 1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
420 stl_full_path = os.path.join(stl_path[0], str(stl_file))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
421 self.load_stl_into_model(stl_full_path, stl_file, translate_list, rotate_list[2])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
422
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
423 def load_stl(self, name):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
424 if not os.path.exists(name):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
425 logging.error(_("Couldn't load non-existing file %s") % name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
426 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
427 path = os.path.split(name)[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
428 self.basedir = path
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
429 if name.lower().endswith(".stl"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
430 for model in self.models.values():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
431 if model.filename == name:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
432 newmodel = copy(model)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
433 newmodel.offsets = list(model.offsets)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
434 newmodel.rot = model.rot
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
435 newmodel.scale = list(model.scale)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
436 self.add_model(name, newmodel)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
437 self.s.prepare_model(newmodel, 2)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
438 break
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
439 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
440 # Filter out the path, just show the STL filename.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
441 self.load_stl_into_model(name, name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
442 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
443
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
444 def load_stl_into_model(self, path, name, offset = None, rotation = 0, scale = [1.0, 1.0, 1.0]):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
445 model = stltool.stl(path)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
446 if offset is None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
447 offset = [self.build_dimensions[3], self.build_dimensions[4], 0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
448 model.offsets = list(offset)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
449 model.rot = rotation
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
450 model.scale = list(scale)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
451 model.filename = name
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
452 self.add_model(name, model)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
453 model.centeroffset = [-(model.dims[1] + model.dims[0]) / 2,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
454 -(model.dims[3] + model.dims[2]) / 2,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
455 0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
456 self.s.prepare_model(model, 2)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
457
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
458 def export_to(self, name):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
459 with open(name.replace(".", "_") + ".scad", "w") as sf:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
460 facets = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
461 for model in self.models.values():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
462 r = model.rot
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
463 o = model.offsets
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
464 co = model.centeroffset
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
465 sf.write("translate([%s, %s, %s])"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
466 "rotate([0, 0, %s])"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
467 "translate([%s, %s, %s])"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
468 "import(\"%s\");\n" % (o[0], o[1], o[2],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
469 r,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
470 co[0], co[1], co[2],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
471 model.filename))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
472 model = model.transform(transformation_matrix(model))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
473 facets += model.facets
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
474 stltool.emitstl(name, facets, "plater_export")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
475 logging.info(_("Wrote plate to %s") % name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
476
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
477 def autoplate(self, event = None):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
478 if self.simarrange_path:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
479 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
480 self.autoplate_simarrange()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
481 except Exception, e:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
482 logging.warning(_("Failed to use simarrange for plating, "
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
483 "falling back to the standard method. "
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
484 "The error was: ") + e)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
485 super(StlPlaterPanel, self).autoplate()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
486 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
487 super(StlPlaterPanel, self).autoplate()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
488
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
489 def autoplate_simarrange(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
490 logging.info(_("Autoplating using simarrange"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
491 models = dict(self.models)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
492 files = [model.filename for model in models.values()]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
493 command = [self.simarrange_path, "--dryrun",
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
494 "-m", # Pack around center
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
495 "-x", str(int(self.build_dimensions[0])),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
496 "-y", str(int(self.build_dimensions[1]))] + files
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
497 p = subprocess.Popen(command, stdout = subprocess.PIPE)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
498
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
499 pos_regexp = re.compile("File: (.*) minx: ([0-9]+), miny: ([0-9]+), minrot: ([0-9]+)")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
500 for line in p.stdout:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
501 line = line.rstrip()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
502 if "Generating plate" in line:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
503 plateid = int(line.split()[-1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
504 if plateid > 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
505 logging.error(_("Plate full, please remove some objects"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
506 break
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
507 if "File:" in line:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
508 bits = pos_regexp.match(line).groups()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
509 filename = bits[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
510 x = float(bits[1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
511 y = float(bits[2])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
512 rot = -float(bits[3])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
513 for name, model in models.items():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
514 # FIXME: not sure this is going to work superwell with utf8
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
515 if model.filename == filename:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
516 model.offsets[0] = x + self.build_dimensions[3]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
517 model.offsets[1] = y + self.build_dimensions[4]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
518 model.rot = rot
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
519 del models[name]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
520 break
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
521 if p.wait() != 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
522 raise RuntimeError(_("simarrange failed"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
523
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
524 StlPlater = make_plater(StlPlaterPanel)

mercurial