printrun-src/printrun/excluder.py

Fri, 03 Jun 2016 09:42:44 +0200

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

Implemented svg, png and hpgl compilers to pronterface

15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1 # This file is part of the Printrun suite.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
2 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
3 # Printrun is free software: you can redistribute it and/or modify
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
4 # it under the terms of the GNU General Public License as published by
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
5 # the Free Software Foundation, either version 3 of the License, or
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
6 # (at your option) any later version.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
7 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
8 # Printrun is distributed in the hope that it will be useful,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
11 # GNU General Public License for more details.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
12 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
13 # You should have received a copy of the GNU General Public License
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
14 # along with Printrun. If not, see <http://www.gnu.org/licenses/>.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
16 import wx
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
17 from printrun import gviz
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
18
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
19 from .utils import imagefile, install_locale
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
20 install_locale('pronterface')
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
21
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
22 class ExcluderWindow(gviz.GvizWindow):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
23
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
24 def __init__(self, excluder, *args, **kwargs):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
25 super(ExcluderWindow, self).__init__(*args, **kwargs)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
26 self.SetTitle(_("Part excluder: draw rectangles where print instructions should be ignored"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
27 self.toolbar.AddLabelTool(128, " " + _("Reset selection"),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
28 wx.Image(imagefile('reset.png'), wx.BITMAP_TYPE_PNG).ConvertToBitmap(),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
29 shortHelp = _("Reset selection"),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
30 longHelp = "")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
31 self.Bind(wx.EVT_TOOL, self.reset_selection, id = 128)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
32 self.parent = excluder
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
33 self.p.paint_overlay = self.paint_selection
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
34 self.p.layerup()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
35
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
36 def real_to_gcode(self, x, y):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
37 return (x + self.p.build_dimensions[3],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
38 self.p.build_dimensions[4] + self.p.build_dimensions[1] - y)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
39
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
40 def gcode_to_real(self, x, y):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
41 return (x - self.p.build_dimensions[3],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
42 self.p.build_dimensions[1] - (y - self.p.build_dimensions[4]))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
43
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
44 def mouse(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
45 if event.ButtonUp(wx.MOUSE_BTN_LEFT) \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
46 or event.ButtonUp(wx.MOUSE_BTN_RIGHT):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
47 self.initpos = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
48 elif event.Dragging() and event.RightIsDown():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
49 e = event.GetPositionTuple()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
50 if not self.initpos or not hasattr(self, "basetrans"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
51 self.initpos = e
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
52 self.basetrans = self.p.translate
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
53 self.p.translate = [self.basetrans[0] + (e[0] - self.initpos[0]),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
54 self.basetrans[1] + (e[1] - self.initpos[1])]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
55 self.p.dirty = 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
56 wx.CallAfter(self.p.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
57 elif event.Dragging() and event.LeftIsDown():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
58 x, y = event.GetPositionTuple()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
59 if not self.initpos:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
60 self.basetrans = self.p.translate
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
61 x = (x - self.basetrans[0]) / self.p.scale[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
62 y = (y - self.basetrans[1]) / self.p.scale[1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
63 x, y = self.real_to_gcode(x, y)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
64 if not self.initpos:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
65 self.initpos = (x, y)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
66 self.parent.rectangles.append((0, 0, 0, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
67 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
68 pos = (x, y)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
69 x0 = min(self.initpos[0], pos[0])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
70 y0 = min(self.initpos[1], pos[1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
71 x1 = max(self.initpos[0], pos[0])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
72 y1 = max(self.initpos[1], pos[1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
73 self.parent.rectangles[-1] = (x0, y0, x1, y1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
74 wx.CallAfter(self.p.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
75 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
76 event.Skip()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
77
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
78 def _line_scaler(self, orig):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
79 x0, y0 = self.gcode_to_real(orig[0], orig[1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
80 x0 = self.p.scale[0] * x0 + self.p.translate[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
81 y0 = self.p.scale[1] * y0 + self.p.translate[1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
82 x1, y1 = self.gcode_to_real(orig[2], orig[3])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
83 x1 = self.p.scale[0] * x1 + self.p.translate[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
84 y1 = self.p.scale[1] * y1 + self.p.translate[1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
85 width = max(x0, x1) - min(x0, x1) + 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
86 height = max(y0, y1) - min(y0, y1) + 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
87 return (min(x0, x1), min(y0, y1), width, height,)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
88
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
89 def paint_selection(self, dc):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
90 dc = wx.GCDC(dc)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
91 dc.SetPen(wx.TRANSPARENT_PEN)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
92 dc.DrawRectangleList([self._line_scaler(rect)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
93 for rect in self.parent.rectangles],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
94 None, wx.Brush((200, 200, 200, 150)))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
95
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
96 def reset_selection(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
97 self.parent.rectangles = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
98 wx.CallAfter(self.p.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
99
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
100 class Excluder(object):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
101
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
102 def __init__(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
103 self.rectangles = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
104 self.window = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
105
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
106 def pop_window(self, gcode, *args, **kwargs):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
107 if not self.window:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
108 self.window = ExcluderWindow(self, *args, **kwargs)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
109 self.window.p.addfile(gcode, True)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
110 self.window.Bind(wx.EVT_CLOSE, self.close_window)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
111 self.window.Show()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
112 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
113 self.window.Show()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
114 self.window.Raise()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
115
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
116 def close_window(self, event = None):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
117 if self.window:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
118 self.window.Destroy()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
119 self.window = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
120
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
121 if __name__ == '__main__':
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
122 import sys
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
123 import gcoder
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
124 gcode = gcoder.GCode(open(sys.argv[1]))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
125 app = wx.App(False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
126 ex = Excluder()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
127 ex.pop_window(gcode)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
128 app.MainLoop()

mercurial