-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathm2d.py
492 lines (445 loc) · 17.8 KB
/
m2d.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#!/usr/bin/env python
#
# Copyright 2020 Doug Blanding (dblanding@gmail.com)
#
# This file is part of kodacad.
# The latest version of this file can be found at:
# //https://github.com/dblanding/kodacad
#
# kodacad is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# kodacad is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# if not, write to the Free Software Foundation, Inc.
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
from OCC.Core.BRep import BRep_Tool
from OCC.Core.TopoDS import TopoDS_Vertex, topods_Vertex
class M2D:
"""Methods for creating and drawing elements on 2D workplanes"""
def __init__(self, win, display):
self.win = win
self.display = display
#############################################
#
# Create 2d Construction Line functions
#
#############################################
def add_vertex_to_xyPtStack(self, shapeList):
"""Helper function to convert vertex to gp_Pnt and put on ptStack."""
wp = self.win.activeWp
for shape in shapeList:
if isinstance(shape, TopoDS_Vertex): # Guard against wrong type
vrtx = topods_Vertex(shape)
pnt = BRep_Tool.Pnt(vrtx) # convert vertex to type <gp_Pnt>
trsf = wp.Trsf.Inverted() # New transform. Don't invert wp.Trsf
pnt.Transform(trsf)
pt2d = (pnt.X(), pnt.Y()) # 2d point
self.win.xyPtStack.append(pt2d)
else:
print(f"(Unwanted) shape type: {type(shape)}")
def processLineEdit(self):
"""pop value from lineEditStack and place on floatStack or ptStack."""
text = self.win.lineEditStack.pop()
if "," in text:
try:
xstr, ystr = text.split(",")
p = (float(xstr) * self.win.unitscale,
float(ystr) * self.win.unitscale)
self.win.xyPtStack.append(p)
except:
print("Problem with processing line edit stack")
else:
try:
self.win.floatStack.append(float(text))
except ValueError as e:
print(f"{e}")
def clineH(self):
"""Horizontal construction line"""
if self.win.xyPtStack:
wp = self.win.activeWp
p = self.win.xyPtStack.pop()
self.win.xyPtStack = []
wp.hcl(p)
self.win.draw_wp(self.win.activeWpUID)
else:
self.win.registerCallback(self.clineHC)
self.display.SetSelectionModeVertex()
self.win.xyPtStack = []
self.win.clearLEStack()
self.win.lineEdit.setFocus()
statusText = "Select point or enter Y-value for horizontal cline."
self.win.statusBar().showMessage(statusText)
def clineHC(self, shapeList, *args):
"""Callback (collector) for clineH"""
self.add_vertex_to_xyPtStack(shapeList)
if self.win.lineEditStack:
self.processLineEdit()
if self.win.floatStack:
y = self.win.floatStack.pop() * self.win.unitscale
pnt = (0, y)
self.win.xyPtStack.append(pnt)
if self.win.xyPtStack:
self.clineH()
def clineV(self):
"""Vertical construction line"""
if self.win.xyPtStack:
wp = self.win.activeWp
p = self.win.xyPtStack.pop()
self.win.xyPtStack = []
wp.vcl(p)
self.win.draw_wp(self.win.activeWpUID)
else:
self.win.registerCallback(self.clineVC)
self.display.SetSelectionModeVertex()
self.win.xyPtStack = []
self.win.clearLEStack()
self.win.lineEdit.setFocus()
statusText = "Select point or enter X-value for vertcal cline."
self.win.statusBar().showMessage(statusText)
def clineVC(self, shapeList, *args):
"""Callback (collector) for clineV"""
self.add_vertex_to_xyPtStack(shapeList)
if self.win.lineEditStack:
self.processLineEdit()
if self.win.floatStack:
x = self.win.floatStack.pop() * self.win.unitscale
pnt = (x, 0)
self.win.xyPtStack.append(pnt)
if self.win.xyPtStack:
self.clineV()
def clineHV(self):
"""Horizontal + Vertical construction lines"""
if self.win.xyPtStack:
wp = self.win.activeWp
p = self.win.xyPtStack.pop()
self.win.xyPtStack = []
wp.hvcl(p)
self.win.draw_wp(self.win.activeWpUID)
else:
self.win.registerCallback(self.clineHVC)
self.display.SetSelectionModeVertex()
self.win.xyPtStack = []
self.win.clearLEStack()
self.win.lineEdit.setFocus()
statusText = "Select point or enter x,y coords for H+V cline."
self.win.statusBar().showMessage(statusText)
def clineHVC(self, shapeList, *args):
"""Callback (collector) for clineHV"""
self.add_vertex_to_xyPtStack(shapeList)
if self.win.lineEditStack:
self.processLineEdit()
if self.win.xyPtStack:
self.clineHV()
def cline2Pts(self):
"""Construction line through two points"""
if len(self.win.xyPtStack) == 2:
wp = self.win.activeWp
p2 = self.win.xyPtStack.pop()
p1 = self.win.xyPtStack.pop()
wp.acl(p1, p2)
self.win.xyPtStack = []
self.win.draw_wp(self.win.activeWpUID)
else:
self.win.registerCallback(self.cline2PtsC)
self.display.SetSelectionModeVertex()
self.win.xyPtStack = []
self.win.clearLEStack()
self.win.lineEdit.setFocus()
statusText = "Select 2 points for Construction Line."
self.win.statusBar().showMessage(statusText)
def cline2PtsC(self, shapeList, *args):
"""Callback (collector) for cline2Pts"""
self.add_vertex_to_xyPtStack(shapeList)
if self.win.lineEditStack:
self.processLineEdit()
if len(self.win.xyPtStack) == 2:
self.cline2Pts()
def clineAng(self):
"""Construction line through a point and at an angle"""
if self.win.xyPtStack and self.win.floatStack:
wp = self.win.activeWp
text = self.win.floatStack.pop()
angle = float(text)
pnt = self.win.xyPtStack.pop()
wp.acl(pnt, ang=angle)
self.win.xyPtStack = []
self.win.draw_wp(self.win.activeWpUID)
else:
self.win.registerCallback(self.clineAngC)
self.display.SetSelectionModeVertex()
self.win.xyPtStack = []
self.win.floatStack = []
self.win.lineEditStack = []
self.win.lineEdit.setFocus()
statusText = "Select point on WP (or enter x,y coords) then enter angle."
self.win.statusBar().showMessage(statusText)
def clineAngC(self, shapeList, *args):
"""Callback (collector) for clineAng"""
self.add_vertex_to_xyPtStack(shapeList)
self.win.lineEdit.setFocus()
if self.win.lineEditStack:
self.processLineEdit()
if self.win.xyPtStack and self.win.floatStack:
self.clineAng()
def clineRefAng(self):
pass
def clineAngBisec(self):
pass
def clineLinBisec(self):
"""Linear bisector between two points"""
if len(self.win.xyPtStack) == 2:
wp = self.win.activeWp
pnt2 = self.win.xyPtStack.pop()
pnt1 = self.win.xyPtStack.pop()
wp.lbcl(pnt1, pnt2)
self.win.xyPtStack = []
self.win.draw_wp(self.win.activeWpUID)
else:
self.win.registerCallback(self.clineLinBisecC)
self.display.SetSelectionModeVertex()
def clineLinBisecC(self, shapeList, *args):
"""Callback (collector) for clineLinBisec"""
self.add_vertex_to_xyPtStack(shapeList)
if len(self.win.xyPtStack) == 2:
self.clineLinBisec()
def clinePara(self):
pass
def clinePerp(self):
pass
def clineTan1(self):
pass
def clineTan2(self):
pass
def ccirc(self):
"""Create a c-circle from center & radius or center & Pnt on circle"""
wp = self.win.activeWp
if len(self.win.xyPtStack) == 2:
p2 = self.win.xyPtStack.pop()
p1 = self.win.xyPtStack.pop()
rad = wp.p2p_dist(p1, p2)
wp.circle(p1, rad, constr=True)
self.win.xyPtStack = []
self.win.floatStack = []
self.win.draw_wp(self.win.activeWpUID)
elif self.win.xyPtStack and self.win.floatStack:
pnt = self.win.xyPtStack.pop()
rad = self.win.floatStack.pop() * self.win.unitscale
wp.circle(pnt, rad, constr=True)
self.win.xyPtStack = []
self.win.floatStack = []
self.win.draw_wp(self.win.activeWpUID)
else:
self.win.registerCallback(self.ccircC)
self.display.SetSelectionModeVertex()
self.win.xyPtStack = []
self.win.floatStack = []
self.win.lineEditStack = []
self.win.lineEdit.setFocus()
statusText = "Pick center of construction circle and enter radius."
self.win.statusBar().showMessage(statusText)
def ccircC(self, shapeList, *args):
"""callback (collector) for ccirc"""
self.add_vertex_to_xyPtStack(shapeList)
self.win.lineEdit.setFocus()
if self.win.lineEditStack:
self.processLineEdit()
if len(self.win.xyPtStack) == 2:
self.ccirc()
if self.win.xyPtStack and self.win.floatStack:
self.ccirc()
#############################################
#
# Create 2d Edge Profile functions
#
#############################################
def line(self):
"""Create a profile geometry line between two end points."""
if len(self.win.xyPtStack) == 2:
wp = self.win.activeWp
pnt2 = self.win.xyPtStack.pop()
pnt1 = self.win.xyPtStack.pop()
wp.line(pnt1, pnt2)
self.win.xyPtStack = []
self.win.draw_wp(self.win.activeWpUID)
else:
self.win.registerCallback(self.lineC)
self.display.SetSelectionModeVertex()
self.win.xyPtStack = []
self.win.lineEdit.setFocus()
statusText = "Select 2 end points for line."
self.win.statusBar().showMessage(statusText)
def lineC(self, shapeList, *args):
"""callback (collector) for line"""
self.add_vertex_to_xyPtStack(shapeList)
self.win.lineEdit.setFocus()
if self.win.lineEditStack:
self.processLineEdit()
if len(self.win.xyPtStack) == 2:
self.line()
def rect(self):
"""Create a profile geometry rectangle from two diagonally opposite corners."""
if len(self.win.xyPtStack) == 2:
wp = self.win.activeWp
pnt2 = self.win.xyPtStack.pop()
pnt1 = self.win.xyPtStack.pop()
wp.rect(pnt1, pnt2)
self.win.xyPtStack = []
self.win.draw_wp(self.win.activeWpUID)
else:
self.win.registerCallback(self.rectC)
self.display.SetSelectionModeVertex()
self.win.xyPtStack = []
self.win.lineEdit.setFocus()
statusText = "Select 2 points for Rectangle."
self.win.statusBar().showMessage(statusText)
def rectC(self, shapeList, *args):
"""callback (collector) for rect"""
self.add_vertex_to_xyPtStack(shapeList)
self.win.lineEdit.setFocus()
if self.win.lineEditStack:
self.processLineEdit()
if len(self.win.xyPtStack) == 2:
self.rect()
def circle(self):
"""Create a geometry circle from cntr & rad or cntr & pnt on circle."""
wp = self.win.activeWp
if len(self.win.xyPtStack) == 2:
p2 = self.win.xyPtStack.pop()
p1 = self.win.xyPtStack.pop()
rad = wp.p2p_dist(p1, p2)
wp.circle(p1, rad, constr=False)
self.win.xyPtStack = []
self.win.floatStack = []
self.win.draw_wp(self.win.activeWpUID)
elif self.win.xyPtStack and self.win.floatStack:
pnt = self.win.xyPtStack.pop()
rad = self.win.floatStack.pop() * self.win.unitscale
wp.circle(pnt, rad, constr=False)
self.win.xyPtStack = []
self.win.floatStack = []
self.win.draw_wp(self.win.activeWpUID)
else:
self.win.registerCallback(self.circleC)
self.display.SetSelectionModeVertex()
self.win.xyPtStack = []
self.win.floatStack = []
self.win.lineEditStack = []
self.win.lineEdit.setFocus()
statusText = "Pick center and enter radius or pick center & 2nd point."
self.win.statusBar().showMessage(statusText)
def circleC(self, shapeList, *args):
"""callback (collector) for circle"""
self.add_vertex_to_xyPtStack(shapeList)
self.win.lineEdit.setFocus()
if self.win.lineEditStack:
self.processLineEdit()
if len(self.win.xyPtStack) == 2:
self.circle()
if self.win.xyPtStack and self.win.floatStack:
self.circle()
def arcc2p(self):
"""Create an arc from center pt, start pt and end pt."""
wp = self.win.activeWp
if len(self.win.xyPtStack) == 3:
pe = self.win.xyPtStack.pop()
ps = self.win.xyPtStack.pop()
pc = self.win.xyPtStack.pop()
wp.arcc2p(pc, ps, pe)
self.win.xyPtStack = []
self.win.floatStack = []
self.win.draw_wp(self.win.activeWpUID)
else:
self.win.registerCallback(self.arcc2pC)
self.display.SetSelectionModeVertex()
self.win.xyPtStack = []
statusText = "Pick center of arc, then start then end point."
self.win.statusBar().showMessage(statusText)
def arcc2pC(self, shapeList, *args):
"""callback (collector) for arcc2p"""
self.add_vertex_to_xyPtStack(shapeList)
self.win.lineEdit.setFocus()
if self.win.lineEditStack:
self.processLineEdit()
if len(self.win.xyPtStack) == 3:
self.arcc2p()
def arc3p(self):
"""Create an arc from start pt, end pt, and 3rd pt on the arc."""
wp = self.win.activeWp
if len(self.win.xyPtStack) == 3:
ps = self.win.xyPtStack.pop()
pe = self.win.xyPtStack.pop()
p3 = self.win.xyPtStack.pop()
wp.arc3p(ps, pe, p3)
self.win.xyPtStack = []
self.win.floatStack = []
self.win.draw_wp(self.win.activeWpUID)
else:
self.win.registerCallback(self.arc3pC)
self.display.SetSelectionModeVertex()
self.win.xyPtStack = []
statusText = "Pick start point on arc, then end then 3rd point on arc."
self.win.statusBar().showMessage(statusText)
def arc3pC(self, shapeList, *args):
"""Callback (collector) for arc3p"""
self.add_vertex_to_xyPtStack(shapeList)
self.win.lineEdit.setFocus()
if self.win.lineEditStack:
self.processLineEdit()
if len(self.win.xyPtStack) == 3:
self.arc3p()
def geom(self):
pass
#############################################
#
# 2D Delete functions
#
#############################################
def delCl(self):
"""Delete selected 2d construction element.
Todo: Get this working. Able to pre-select lines from the display
as type <AIS_InteractiveObject> but haven't figured out how to get
the type <AIS_Line> (or the cline or Geom_Line that was used to make
it)."""
self.win.registerCallback(self.delClC)
statusText = "Select a construction element to delete."
self.win.statusBar().showMessage(statusText)
self.display = self.win.canvas._self.display.Context
print(self.display.NbSelected()) # Use shift-select for multiple lines
selected_line = self.display.SelectedInteractive()
if selected_line:
print(type(selected_line)) # <AIS_InteractiveObject>
print(selected_line.GetOwner()) # <Standard_Transient>
def delClC(self, shapeList, *args):
"""Callback (collector) for delCl"""
print(shapeList)
print(args)
self.delCl()
def delEl(self):
"""Delete selected geometry profile element."""
wp = self.win.activeWp
if self.win.shapeStack:
while self.win.shapeStack:
shape = self.win.shapeStack.pop()
if shape in wp.edgeList:
wp.edgeList.remove(shape)
self.win.redraw()
else:
self.win.registerCallback(self.delElC)
self.display.SetSelectionModeEdge()
self.win.xyPtStack = []
statusText = "Select a geometry profile element to delete."
self.win.statusBar().showMessage(statusText)
def delElC(self, shapeList, *args):
"""Callback (collector) for delEl"""
for shape in shapeList:
self.win.shapeStack.append(shape)
if self.win.shapeStack:
self.delEl()