printrun-src/printrun/objectplater.py

changeset 46
cce0af6351f0
parent 15
0bbb006204fc
equal deleted inserted replaced
45:c82943fb205f 46:cce0af6351f0
1 #!/usr/bin/env python
2
3 # This file is part of the Printrun suite. 1 # This file is part of the Printrun suite.
4 # 2 #
5 # Printrun is free software: you can redistribute it and/or modify 3 # Printrun is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by 4 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or 5 # the Free Software Foundation, either version 3 of the License, or
33 31
34 class PlaterPanel(wx.Panel): 32 class PlaterPanel(wx.Panel):
35 def __init__(self, **kwargs): 33 def __init__(self, **kwargs):
36 self.destroy_on_done = False 34 self.destroy_on_done = False
37 parent = kwargs.get("parent", None) 35 parent = kwargs.get("parent", None)
38 super(PlaterPanel, self).__init__(parent = parent) 36 super().__init__(parent = parent)
39 self.prepare_ui(**kwargs) 37 self.prepare_ui(**kwargs)
40 38
41 def prepare_ui(self, filenames = [], callback = None, parent = None, build_dimensions = None): 39 def prepare_ui(self, filenames = [], callback = None, parent = None, build_dimensions = None):
42 self.filenames = filenames 40 self.filenames = filenames
43 self.mainsizer = wx.BoxSizer(wx.HORIZONTAL) 41 panel = self.menupanel = wx.Panel(self)
44 panel = self.menupanel = wx.Panel(self, -1)
45 sizer = self.menusizer = wx.GridBagSizer() 42 sizer = self.menusizer = wx.GridBagSizer()
43 # Load button
44 loadbutton = wx.Button(panel, label = _("Load"))
45 loadbutton.Bind(wx.EVT_BUTTON, self.load)
46 sizer.Add(loadbutton, pos = (0, 0), span = (1, 1), flag = wx.EXPAND)
47 # Export button
48 exportbutton = wx.Button(panel, label = _("Export"))
49 exportbutton.Bind(wx.EVT_BUTTON, self.export)
50 sizer.Add(exportbutton, pos = (0, 1), span = (1, 1), flag = wx.EXPAND)
51
46 self.l = wx.ListBox(panel) 52 self.l = wx.ListBox(panel)
47 sizer.Add(self.l, pos = (1, 0), span = (1, 2), flag = wx.EXPAND) 53 sizer.Add(self.l, pos = (1, 0), span = (1, 2), flag = wx.EXPAND)
48 sizer.AddGrowableRow(1, 1) 54 sizer.AddGrowableRow(1, 1)
49 # Clear button 55 # Clear button
50 clearbutton = wx.Button(panel, label = _("Clear")) 56 clearbutton = wx.Button(panel, label = _("Clear"))
51 clearbutton.Bind(wx.EVT_BUTTON, self.clear) 57 clearbutton.Bind(wx.EVT_BUTTON, self.clear)
52 sizer.Add(clearbutton, pos = (2, 0), span = (1, 2), flag = wx.EXPAND) 58 sizer.Add(clearbutton, pos = (2, 0), span = (1, 2), flag = wx.EXPAND)
53 # Load button
54 loadbutton = wx.Button(panel, label = _("Load"))
55 loadbutton.Bind(wx.EVT_BUTTON, self.load)
56 sizer.Add(loadbutton, pos = (0, 0), span = (1, 1), flag = wx.EXPAND)
57 # Snap to Z = 0 button 59 # Snap to Z = 0 button
58 snapbutton = wx.Button(panel, label = _("Snap to Z = 0")) 60 snapbutton = wx.Button(panel, label = _("Snap to Z = 0"))
59 snapbutton.Bind(wx.EVT_BUTTON, self.snap) 61 snapbutton.Bind(wx.EVT_BUTTON, self.snap)
60 sizer.Add(snapbutton, pos = (3, 0), span = (1, 1), flag = wx.EXPAND) 62 sizer.Add(snapbutton, pos = (3, 0), span = (1, 1), flag = wx.EXPAND)
61 # Put at center button 63 # Put at center button
68 sizer.Add(deletebutton, pos = (4, 0), span = (1, 1), flag = wx.EXPAND) 70 sizer.Add(deletebutton, pos = (4, 0), span = (1, 1), flag = wx.EXPAND)
69 # Auto arrange button 71 # Auto arrange button
70 autobutton = wx.Button(panel, label = _("Auto arrange")) 72 autobutton = wx.Button(panel, label = _("Auto arrange"))
71 autobutton.Bind(wx.EVT_BUTTON, self.autoplate) 73 autobutton.Bind(wx.EVT_BUTTON, self.autoplate)
72 sizer.Add(autobutton, pos = (5, 0), span = (1, 2), flag = wx.EXPAND) 74 sizer.Add(autobutton, pos = (5, 0), span = (1, 2), flag = wx.EXPAND)
73 # Export button
74 exportbutton = wx.Button(panel, label = _("Export"))
75 exportbutton.Bind(wx.EVT_BUTTON, self.export)
76 sizer.Add(exportbutton, pos = (0, 1), span = (1, 1), flag = wx.EXPAND)
77 if callback is not None: 75 if callback is not None:
78 donebutton = wx.Button(panel, label = _("Done")) 76 donebutton = wx.Button(panel, label = _("Done"))
79 donebutton.Bind(wx.EVT_BUTTON, lambda e: self.done(e, callback)) 77 donebutton.Bind(wx.EVT_BUTTON, lambda e: self.done(e, callback))
80 sizer.Add(donebutton, pos = (6, 0), span = (1, 1), flag = wx.EXPAND) 78 sizer.Add(donebutton, pos = (6, 0), span = (1, 1), flag = wx.EXPAND)
81 cancelbutton = wx.Button(panel, label = _("Cancel")) 79 cancelbutton = wx.Button(panel, label = _("Cancel"))
82 cancelbutton.Bind(wx.EVT_BUTTON, lambda e: self.Destroy()) 80 cancelbutton.Bind(wx.EVT_BUTTON, lambda e: self.Destroy())
83 sizer.Add(cancelbutton, pos = (6, 1), span = (1, 1), flag = wx.EXPAND) 81 sizer.Add(cancelbutton, pos = (6, 1), span = (1, 1), flag = wx.EXPAND)
84 self.basedir = "." 82 self.basedir = "."
85 self.models = {} 83 self.models = {}
86 panel.SetSizerAndFit(sizer) 84 panel.SetSizer(sizer)
85 self.mainsizer = wx.BoxSizer(wx.HORIZONTAL)
87 self.mainsizer.Add(panel, flag = wx.EXPAND) 86 self.mainsizer.Add(panel, flag = wx.EXPAND)
88 self.SetSizer(self.mainsizer) 87 self.SetSizer(self.mainsizer)
89 if build_dimensions: 88 self.build_dimensions = build_dimensions or [200, 200, 100, 0, 0, 0]
90 self.build_dimensions = build_dimensions
91 else:
92 self.build_dimensions = [200, 200, 100, 0, 0, 0]
93 89
94 def set_viewer(self, viewer): 90 def set_viewer(self, viewer):
95 # Patch handle_rotation on the fly 91 # Patch handle_rotation on the fly
96 if hasattr(viewer, "handle_rotation"): 92 if hasattr(viewer, "handle_rotation"):
97 def handle_rotation(self, event, orig_handler): 93 def handle_rotation(self, event, orig_handler):
98 if self.initpos is None: 94 if self.initpos is None:
99 self.initpos = event.GetPositionTuple() 95 self.initpos = event.GetPosition()
100 else: 96 else:
101 if event.ShiftDown(): 97 if event.ShiftDown():
102 p1 = self.initpos 98 p1 = self.initpos
103 p2 = event.GetPositionTuple() 99 p2 = event.GetPosition()
104 x1, y1, _ = self.mouse_to_3d(p1[0], p1[1]) 100 x1, y1, _ = self.mouse_to_3d(p1[0], p1[1])
105 x2, y2, _ = self.mouse_to_3d(p2[0], p2[1]) 101 x2, y2, _ = self.mouse_to_3d(p2[0], p2[1])
106 self.parent.move_shape((x2 - x1, y2 - y1)) 102 self.parent.move_shape((x2 - x1, y2 - y1))
107 self.initpos = p2 103 self.initpos = p2
108 else: 104 else:
110 patch_method(viewer, "handle_rotation", handle_rotation) 106 patch_method(viewer, "handle_rotation", handle_rotation)
111 # Patch handle_wheel on the fly 107 # Patch handle_wheel on the fly
112 if hasattr(viewer, "handle_wheel"): 108 if hasattr(viewer, "handle_wheel"):
113 def handle_wheel(self, event, orig_handler): 109 def handle_wheel(self, event, orig_handler):
114 if event.ShiftDown(): 110 if event.ShiftDown():
115 delta = event.GetWheelRotation()
116 angle = 10 111 angle = 10
117 if delta > 0: 112 if event.GetWheelRotation() < 0:
118 self.parent.rotate_shape(angle / 2) 113 angle = -angle
119 else: 114 self.parent.rotate_shape(angle / 2)
120 self.parent.rotate_shape(-angle / 2)
121 else: 115 else:
122 orig_handler(event) 116 orig_handler(event)
123 patch_method(viewer, "handle_wheel", handle_wheel) 117 patch_method(viewer, "handle_wheel", handle_wheel)
124 self.s = viewer 118 self.s = viewer
125 self.mainsizer.Add(self.s, 1, wx.EXPAND) 119 self.mainsizer.Add(self.s, 1, wx.EXPAND)
244 self.l.Select(self.l.GetCount() - 1) 238 self.l.Select(self.l.GetCount() - 1)
245 self.Refresh() 239 self.Refresh()
246 240
247 def add_model(self, name, model): 241 def add_model(self, name, model):
248 newname = os.path.split(name.lower())[1] 242 newname = os.path.split(name.lower())[1]
249 if not isinstance(newname, unicode): 243 if not isinstance(newname, str):
250 newname = unicode(newname, "utf-8") 244 newname = str(newname, "utf-8")
251 c = 1 245 c = 1
252 while newname in self.models: 246 while newname in self.models:
253 newname = os.path.split(name.lower())[1] 247 newname = os.path.split(name.lower())[1]
254 newname = newname + "(%d)" % c 248 newname = newname + "(%d)" % c
255 c += 1 249 c += 1

mercurial