-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathNcCode.py
261 lines (205 loc) · 8.23 KB
/
NcCode.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
from CamObject import CamObject
import geom
import cad
from HeeksConfig import HeeksConfig
import math
import wx
from Object import PyProperty
import cam
type = 0
class NcCode(CamObject):
def __init__(self):
self.nc_code = cam.NcCode()
CamObject.__init__(self)
self.nc_code.SetOwner(self)
self.box = None
def icon(self):
return "nccode"
def TypeName(self):
return "nccode"
def GetTitle(self):
return 'NC Code'
def GetType(self):
return type
def GetBox(self):
box = self.nc_code.GetBox()
return box
def CanBeDeleted(self):
return False
def Clear(self):
self.nc_code.Clear()
def OneOfAKind(self):
return True
def OnAdd(self):
self.GetOwner().nccode = self
def OnGlCommands(self, select, marked, no_color):
self.nc_code.OnGlCommands(select, marked, no_color)
def KillGLLists(self):
self.nc_code.DestroyGLLists()
# def GetBox(self):
# if self.box == None:
# self.box = geom.Box3D()
# self.nc_code.GetBox(self.box)
# print('self.box = ' + str(self.box))
# return None
# return self.box
def MakeACopy(self):
object = NcCode()
object.CopyFrom(self)
return object
def CopyFrom(self, object):
self.nc_code.CopyFrom(object.nc_code)
def ReadXml(self):
self.nc_code.ReadXml()
def WriteXml(self):
self.nc_code.WriteXml()
def SetClickMarkPoint(self, marked_object, ray_start, ray_direction):
self.nc_code.SetClickMarkPoint(marked_object, ray_start, ray_direction)
def SetTextCtrl(self, textCtrl):
textCtrl.Clear()
textCtrl.Freeze()
import wx
font = wx.Font(10, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, False, "Lucida Console", wx.FONTENCODING_SYSTEM)
ta = wx.TextAttr()
ta.SetFont(font)
textCtrl.SetDefaultStyle(ta)
s = ''
for block in self.nc_code.GetBlocks():
s += block.Text()
s += '\n'
textCtrl.SetValue(s)
textCtrl.SetStyle(0, 100, ta)
# '''
# import platform
# if platform.system() != "Windows":
# # for Windows, this is done in OutputTextCtrl.OnPaint
# for block in self.blocks:
# block.FormatText(textCtrl, block == self.highlighted_block, False)
# '''
textCtrl.Thaw()
def FormatBlockText(self, block, textCtrl, highlighted, force_format):
if block.formatted and not force_format:
return
i = block.from_pos
for text in block.GetTexts():
col = cam.CncColor(text.color_type)
c = wx.Colour(col.red, col.green, col.blue)
length = len(text.str)
font = wx.Font(10, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, False, 'Lucida Console', wx.FONTENCODING_SYSTEM)
ta = wx.TextAttr(c)
ta.SetFont(font)
if highlighted:
ta.SetBackgroundColour(wx.Colour(218, 242, 142))
else:
ta.SetBackgroundColour(wx.Colour(255, 255, 255))
textCtrl.SetStyle(i, i + length, ta)
i += length
block.formatted = True
def FormatBlocks(self, textCtrl, i0, i1):
textCtrl.Freeze()
highlighted_block = self.nc_code.GetHighlightedBlock()
for block in self.nc_code.GetBlocks():
if i0 <= block.from_pos and block.from_pos <= i1:
self.FormatBlockText(block, textCtrl, block == highlighted_block, False)
textCtrl.Thaw()
def GetHighlightedBlock(self):
return self.nc_code.GetHighlightedBlock()
def SetHighlightedBlock(self, block):
highlighted_block = self.nc_code.GetHighlightedBlock()
if highlighted_block != None:
self.FormatBlockText(highlighted_block, wx.GetApp().output_window.textCtrl, False, True)
self.nc_code.SetHighlightedBlock(block)
if block != None:
self.FormatBlockText(block, wx.GetApp().output_window.textCtrl, True, True)
def HighlightBlock(self, pos):
self.SetHighlightedBlock(None)
for block in self.nc_code.GetBlocks():
if pos < block.to_pos:
self.nc_code.SetHighlightedBlock(block)
break
self.nc_code.DestroyGLLists()
def GetProperties(self):
properties = []
properties.append(cad.PropertyStringReadOnly('number of lines', str(self.nc_code.GetNumBlocks())))
properties += CamObject.GetProperties(self)
return properties
class NcCodeWriter:
# this fills out an NcCode object with path, used in backplotting, instead of the old hxml_writer
def __init__(self, nccode):
self.nccode = nccode
self.t = None
self.oldp = geom.Point3D(0,0,50)
self.current_block = None
self.current_path = None
self.next_line_number = 0
def begin_ncblock(self):
self.current_block = cam.NewNcCodeBlock()
self.current_block.line_number = self.next_line_number
self.next_line_number += 1
def end_ncblock(self):
self.nccode.nc_code.AddBlock(self.current_block)
self.current_block = None
def add_text(self, s, col, cdata):
text = cam.NewColouredText()
text.str = s
if col == None:
text.color_type = cam.ColorEnum.COLOR_DEFAULT_TYPE
else:
text.color_type = cam.GetTextColor(col)
self.current_block.AddText(text)
def set_mode(self, units):
cam.SetMultiplier(float(units))
def metric(self):
self.set_mode(units = 1.0)
def imperial(self):
self.set_mode(units = 25.4)
def begin_path(self, col):
self.current_path = cam.NewColouredPath()
self.current_path.color_type = cam.GetTextColor(col)
def end_path(self):
self.current_block.AddPath(self.current_path)
self.current_path = None
def rapid(self, x=None, y=None, z=None, a=None, b=None, c=None):
self.begin_path("rapid")
self.add_line(x, y, z, a, b, c)
self.end_path()
def feed(self, x=None, y=None, z=None, a=None, b=None, c=None):
self.begin_path("feed")
self.add_line(x, y, z, a, b, c)
self.end_path()
def arc_cw(self, x=None, y=None, z=None, i=None, j=None, k=None, r=None):
self.begin_path("feed")
self.add_arc(x, y, z, i, j, k, r, -1)
self.end_path()
def arc_ccw(self, x=None, y=None, z=None, i=None, j=None, k=None, r=None):
self.begin_path("feed")
self.add_arc(x, y, z, i, j, k, r, 1)
self.end_path()
def tool_change(self, id):
self.t = id
def current_tool(self):
return self.t
def spindle(self, s, clockwise):
pass
def feedrate(self, f):
pass
def add_line(self, x, y, z, a = None, b = None, c = None):
line = cam.NewPathLine()
if x != None: self.oldp.x = float(x)
if y != None: self.oldp.y = float(y)
if z != None: self.oldp.z = float(z)
line.point = geom.Point3D(self.oldp)
self.current_path.AddPathObject(line)
def add_arc(self, x, y, z, i, j, k, r = None, d = None):
arc = cam.NewPathArc()
arc.c = geom.Point3D(self.oldp)
if i != None: arc.c.x = float(i) - self.oldp.x
if j != None: arc.c.y = float(j) - self.oldp.y
if k != None: arc.c.z = float(k) - self.oldp.z
if x != None: self.oldp.x = float(x)
if y != None: self.oldp.y = float(y)
if z != None: self.oldp.z = float(z)
arc.point = geom.Point3D(self.oldp)
if r != None: arc.radius = float(r)
if d != None: arc.dir = int(d)
self.current_path.AddPathObject(arc)