forked from comawill/plottool
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhpglpreview.py
executable file
·101 lines (74 loc) · 2.97 KB
/
hpglpreview.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python3
import wx
from wx.lib.floatcanvas import NavCanvas, FloatCanvas
import hpgl
import numpy
# fix broken reference to float_ in wxPython 4.2.0
FloatCanvas.float_ = numpy.float_
HPGL2MM = hpgl.hpgl2mm(1)
def XYPlotterScale(center):
"""
Simulate Plotter axis
"""
# center gets ignored in this case
return (-1.0, 1.0)
class HPGLPreview(wx.Frame):
def __init__(self, hpgldata, title="HPGL preview", size=(1200, 700), dialog=False, *args, **kwargs):
super(HPGLPreview, self).__init__(parent=None, title=title, size=size, *args, **kwargs)
self.checked = False
self.CreateStatusBar()
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.Canvas = NavCanvas.NavCanvas(self, -1, ProjectionFun=XYPlotterScale, BackgroundColor="white")
self.sizer.Add(self.Canvas, 1, wx.ALL | wx.EXPAND)
self.bsizer = wx.BoxSizer(wx.HORIZONTAL)
if dialog:
self.btn_ok = wx.Button(self, wx.ID_OK, label="OK")
self.btn_cancel = wx.Button(self, wx.ID_CANCEL, label="Cancel")
self.bsizer.AddStretchSpacer(1)
self.bsizer.Add(self.btn_ok, 0, wx.EXPAND | wx.RIGHT, 5)
self.bsizer.Add(self.btn_cancel, 0, wx.EXPAND | wx.LEFT, 5)
self.btn_ok.Bind(wx.EVT_BUTTON, self.OnOK)
self.btn_cancel.Bind(wx.EVT_BUTTON, self.OnCancel)
self.bsizer.AddStretchSpacer(1)
self.sizer.Add(self.bsizer, 0, wx.ALL | wx.EXPAND, 2)
self.SetSizer(self.sizer)
last = (0, 0)
for line in hpgldata.getPaths():
self.Canvas.Canvas.AddLine(line)
self.Canvas.Canvas.AddLine([last, line[0]], LineColor="blue")
last = line[-1]
self.Canvas.Canvas.AddLine([last, (0, 0)], LineColor="green")
m, mm = hpgldata.getBoundingBox()
self.Canvas.Canvas.AddRectangle((0, 0), (mm[0] + m[0], mm[1] + m[1]), LineColor="orange")
self.Canvas.Canvas.Bind(wx.EVT_MOTION, self.OnMove)
self.Bind(wx.EVT_CLOSE, self.OnClose)
def OnOK(self, event):
self.checked = True
self.Close()
def OnCancel(self, event):
self.Close()
def OnMove(self, event):
x = event.x * HPGL2MM
y = event.y * HPGL2MM
self.SetStatusText("%.2f mm, %.2f mm" % (x, y))
def OnClose(self, event):
self.eventLoop.Exit()
def ShowModal(self):
if hasattr(self, "MakeModal"):
self.MakeModal()
self.Show()
self.Canvas.Canvas.ZoomToBB()
# now to stop execution start a event loop
self.eventLoop = wx.GUIEventLoop()
self.eventLoop.Run()
self.Destroy()
return self.checked
if __name__ == "__main__":
import argparse
app = wx.App(False)
parser = argparse.ArgumentParser("HPGL preview")
parser.add_argument("file", type=str, help="the HPGL-file to open")
args = parser.parse_args()
hpglfile = hpgl.HPGL(args.file)
dialog = HPGLPreview(hpglfile)
dialog.ShowModal()