printrun-src/printrun/objectplater.py

Sat, 04 Jun 2016 13:06:30 +0200

author
mbayer
date
Sat, 04 Jun 2016 13:06:30 +0200
changeset 23
e18b2a4ef561
parent 15
0bbb006204fc
child 46
cce0af6351f0
permissions
-rw-r--r--

Remove empty lines output on bitmap plotter

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 from .utils import install_locale, iconfile
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
19 install_locale('plater')
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
20
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
21 import logging
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
22 import os
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
23 import types
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
24 import wx
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
25
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
26 def patch_method(obj, method, replacement):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
27 orig_handler = getattr(obj, method)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
28
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
29 def wrapped(*a, **kwargs):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
30 kwargs['orig_handler'] = orig_handler
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
31 return replacement(*a, **kwargs)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
32 setattr(obj, method, types.MethodType(wrapped, obj))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
33
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
34 class PlaterPanel(wx.Panel):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
35 def __init__(self, **kwargs):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
36 self.destroy_on_done = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
37 parent = kwargs.get("parent", None)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
38 super(PlaterPanel, self).__init__(parent = parent)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
39 self.prepare_ui(**kwargs)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
40
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
41 def prepare_ui(self, filenames = [], callback = None, parent = None, build_dimensions = None):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
42 self.filenames = filenames
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
43 self.mainsizer = wx.BoxSizer(wx.HORIZONTAL)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
44 panel = self.menupanel = wx.Panel(self, -1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
45 sizer = self.menusizer = wx.GridBagSizer()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
46 self.l = wx.ListBox(panel)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
47 sizer.Add(self.l, pos = (1, 0), span = (1, 2), flag = wx.EXPAND)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
48 sizer.AddGrowableRow(1, 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
49 # Clear button
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
50 clearbutton = wx.Button(panel, label = _("Clear"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
51 clearbutton.Bind(wx.EVT_BUTTON, self.clear)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
52 sizer.Add(clearbutton, pos = (2, 0), span = (1, 2), flag = wx.EXPAND)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
53 # Load button
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
54 loadbutton = wx.Button(panel, label = _("Load"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
55 loadbutton.Bind(wx.EVT_BUTTON, self.load)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
56 sizer.Add(loadbutton, pos = (0, 0), span = (1, 1), flag = wx.EXPAND)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
57 # Snap to Z = 0 button
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
58 snapbutton = wx.Button(panel, label = _("Snap to Z = 0"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
59 snapbutton.Bind(wx.EVT_BUTTON, self.snap)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
60 sizer.Add(snapbutton, pos = (3, 0), span = (1, 1), flag = wx.EXPAND)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
61 # Put at center button
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
62 centerbutton = wx.Button(panel, label = _("Put at center"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
63 centerbutton.Bind(wx.EVT_BUTTON, self.center)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
64 sizer.Add(centerbutton, pos = (3, 1), span = (1, 1), flag = wx.EXPAND)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
65 # Delete button
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
66 deletebutton = wx.Button(panel, label = _("Delete"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
67 deletebutton.Bind(wx.EVT_BUTTON, self.delete)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
68 sizer.Add(deletebutton, pos = (4, 0), span = (1, 1), flag = wx.EXPAND)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
69 # Auto arrange button
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
70 autobutton = wx.Button(panel, label = _("Auto arrange"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
71 autobutton.Bind(wx.EVT_BUTTON, self.autoplate)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
72 sizer.Add(autobutton, pos = (5, 0), span = (1, 2), flag = wx.EXPAND)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
73 # Export button
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
74 exportbutton = wx.Button(panel, label = _("Export"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
75 exportbutton.Bind(wx.EVT_BUTTON, self.export)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
76 sizer.Add(exportbutton, pos = (0, 1), span = (1, 1), flag = wx.EXPAND)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
77 if callback is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
78 donebutton = wx.Button(panel, label = _("Done"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
79 donebutton.Bind(wx.EVT_BUTTON, lambda e: self.done(e, callback))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
80 sizer.Add(donebutton, pos = (6, 0), span = (1, 1), flag = wx.EXPAND)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
81 cancelbutton = wx.Button(panel, label = _("Cancel"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
82 cancelbutton.Bind(wx.EVT_BUTTON, lambda e: self.Destroy())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
83 sizer.Add(cancelbutton, pos = (6, 1), span = (1, 1), flag = wx.EXPAND)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
84 self.basedir = "."
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
85 self.models = {}
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
86 panel.SetSizerAndFit(sizer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
87 self.mainsizer.Add(panel, flag = wx.EXPAND)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
88 self.SetSizer(self.mainsizer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
89 if build_dimensions:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
90 self.build_dimensions = build_dimensions
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
91 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
92 self.build_dimensions = [200, 200, 100, 0, 0, 0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
93
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
94 def set_viewer(self, viewer):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
95 # Patch handle_rotation on the fly
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
96 if hasattr(viewer, "handle_rotation"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
97 def handle_rotation(self, event, orig_handler):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
98 if self.initpos is None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
99 self.initpos = event.GetPositionTuple()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
100 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
101 if event.ShiftDown():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
102 p1 = self.initpos
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
103 p2 = event.GetPositionTuple()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
104 x1, y1, _ = self.mouse_to_3d(p1[0], p1[1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
105 x2, y2, _ = self.mouse_to_3d(p2[0], p2[1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
106 self.parent.move_shape((x2 - x1, y2 - y1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
107 self.initpos = p2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
108 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
109 orig_handler(event)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
110 patch_method(viewer, "handle_rotation", handle_rotation)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
111 # Patch handle_wheel on the fly
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
112 if hasattr(viewer, "handle_wheel"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
113 def handle_wheel(self, event, orig_handler):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
114 if event.ShiftDown():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
115 delta = event.GetWheelRotation()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
116 angle = 10
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
117 if delta > 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
118 self.parent.rotate_shape(angle / 2)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
119 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
120 self.parent.rotate_shape(-angle / 2)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
121 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
122 orig_handler(event)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
123 patch_method(viewer, "handle_wheel", handle_wheel)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
124 self.s = viewer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
125 self.mainsizer.Add(self.s, 1, wx.EXPAND)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
126
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
127 def move_shape(self, delta):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
128 """moves shape (selected in l, which is list ListBox of shapes)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
129 by an offset specified in tuple delta.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
130 Positive numbers move to (rigt, down)"""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
131 name = self.l.GetSelection()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
132 if name == wx.NOT_FOUND:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
133 return False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
134
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
135 name = self.l.GetString(name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
136
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
137 model = self.models[name]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
138 model.offsets = [model.offsets[0] + delta[0],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
139 model.offsets[1] + delta[1],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
140 model.offsets[2]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
141 ]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
142 return True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
143
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
144 def rotate_shape(self, angle):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
145 """rotates acive shape
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
146 positive angle is clockwise
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
147 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
148 name = self.l.GetSelection()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
149 if name == wx.NOT_FOUND:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
150 return False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
151 name = self.l.GetString(name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
152 model = self.models[name]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
153 model.rot += angle
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
154
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
155 def autoplate(self, event = None):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
156 logging.info(_("Autoplating"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
157 separation = 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
158 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
159 from printrun import packer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
160 p = packer.Packer()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
161 for i in self.models:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
162 width = abs(self.models[i].dims[0] - self.models[i].dims[1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
163 height = abs(self.models[i].dims[2] - self.models[i].dims[3])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
164 p.add_rect(width, height, data = i)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
165 centerx = self.build_dimensions[0] / 2 + self.build_dimensions[3]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
166 centery = self.build_dimensions[1] / 2 + self.build_dimensions[4]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
167 rects = p.pack(padding = separation,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
168 center = packer.Vector2(centerx, centery))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
169 for rect in rects:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
170 i = rect.data
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
171 position = rect.center()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
172 self.models[i].offsets[0] = position.x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
173 self.models[i].offsets[1] = position.y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
174 except ImportError:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
175 bedsize = self.build_dimensions[0:3]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
176 cursor = [0, 0, 0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
177 newrow = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
178 max = [0, 0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
179 for i in self.models:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
180 self.models[i].offsets[2] = -1.0 * self.models[i].dims[4]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
181 x = abs(self.models[i].dims[0] - self.models[i].dims[1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
182 y = abs(self.models[i].dims[2] - self.models[i].dims[3])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
183 centre = [x / 2, y / 2]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
184 centreoffset = [self.models[i].dims[0] + centre[0],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
185 self.models[i].dims[2] + centre[1]]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
186 if (cursor[0] + x + separation) >= bedsize[0]:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
187 cursor[0] = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
188 cursor[1] += newrow + separation
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
189 newrow = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
190 if (newrow == 0) or (newrow < y):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
191 newrow = y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
192 # To the person who works out why the offsets are applied
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
193 # differently here:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
194 # Good job, it confused the hell out of me.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
195 self.models[i].offsets[0] = cursor[0] + centre[0] - centreoffset[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
196 self.models[i].offsets[1] = cursor[1] + centre[1] - centreoffset[1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
197 if (max[0] == 0) or (max[0] < (cursor[0] + x)):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
198 max[0] = cursor[0] + x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
199 if (max[1] == 0) or (max[1] < (cursor[1] + x)):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
200 max[1] = cursor[1] + x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
201 cursor[0] += x + separation
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
202 if (cursor[1] + y) >= bedsize[1]:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
203 logging.info(_("Bed full, sorry sir :("))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
204 self.Refresh()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
205 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
206 centerx = self.build_dimensions[0] / 2 + self.build_dimensions[3]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
207 centery = self.build_dimensions[1] / 2 + self.build_dimensions[4]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
208 centreoffset = [centerx - max[0] / 2, centery - max[1] / 2]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
209 for i in self.models:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
210 self.models[i].offsets[0] += centreoffset[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
211 self.models[i].offsets[1] += centreoffset[1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
212 self.Refresh()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
213
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
214 def clear(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
215 result = wx.MessageBox(_('Are you sure you want to clear the grid? All unsaved changes will be lost.'),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
216 _('Clear the grid?'),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
217 wx.YES_NO | wx.ICON_QUESTION)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
218 if result == 2:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
219 self.models = {}
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
220 self.l.Clear()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
221 self.Refresh()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
222
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
223 def center(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
224 i = self.l.GetSelection()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
225 if i != -1:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
226 m = self.models[self.l.GetString(i)]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
227 centerx = self.build_dimensions[0] / 2 + self.build_dimensions[3]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
228 centery = self.build_dimensions[1] / 2 + self.build_dimensions[4]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
229 m.offsets = [centerx, centery, m.offsets[2]]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
230 self.Refresh()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
231
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
232 def snap(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
233 i = self.l.GetSelection()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
234 if i != -1:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
235 m = self.models[self.l.GetString(i)]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
236 m.offsets[2] = -m.dims[4]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
237 self.Refresh()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
238
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
239 def delete(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
240 i = self.l.GetSelection()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
241 if i != -1:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
242 del self.models[self.l.GetString(i)]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
243 self.l.Delete(i)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
244 self.l.Select(self.l.GetCount() - 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
245 self.Refresh()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
246
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
247 def add_model(self, name, model):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
248 newname = os.path.split(name.lower())[1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
249 if not isinstance(newname, unicode):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
250 newname = unicode(newname, "utf-8")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
251 c = 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
252 while newname in self.models:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
253 newname = os.path.split(name.lower())[1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
254 newname = newname + "(%d)" % c
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
255 c += 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
256 self.models[newname] = model
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
257
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
258 self.l.Append(newname)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
259 i = self.l.GetSelection()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
260 if i == wx.NOT_FOUND:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
261 self.l.Select(0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
262
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
263 self.l.Select(self.l.GetCount() - 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
264
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
265 def load(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
266 dlg = wx.FileDialog(self, _("Pick file to load"), self.basedir, style = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
267 dlg.SetWildcard(self.load_wildcard)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
268 if dlg.ShowModal() == wx.ID_OK:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
269 name = dlg.GetPath()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
270 self.load_file(name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
271 dlg.Destroy()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
272
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
273 def load_file(self, filename):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
274 raise NotImplementedError
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
275
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
276 def export(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
277 dlg = wx.FileDialog(self, _("Pick file to save to"), self.basedir, style = wx.FD_SAVE)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
278 dlg.SetWildcard(self.save_wildcard)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
279 if dlg.ShowModal() == wx.ID_OK:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
280 name = dlg.GetPath()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
281 self.export_to(name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
282 dlg.Destroy()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
283
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
284 def export_to(self, name):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
285 raise NotImplementedError
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
286
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
287 class Plater(wx.Frame):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
288 def __init__(self, **kwargs):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
289 self.destroy_on_done = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
290 parent = kwargs.get("parent", None)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
291 size = kwargs.get("size", (800, 580))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
292 if "size" in kwargs:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
293 del kwargs["size"]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
294 wx.Frame.__init__(self, parent, title = _("Plate building tool"), size = size)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
295 self.SetIcon(wx.Icon(iconfile("plater.png"), wx.BITMAP_TYPE_PNG))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
296 self.prepare_ui(**kwargs)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
297
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
298 def make_plater(panel_class):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
299 name = panel_class.__name__.replace("Panel", "")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
300 return type(name, (Plater, panel_class), {})

mercurial