This repository has been archived by the owner on Sep 6, 2023. It is now read-only.
/
occmodeldemo.py
637 lines (496 loc) · 17 KB
/
occmodeldemo.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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
#!/usr/bin/python2
# -*- coding: utf-8 -*-
import sys
import math
import geotools as geo
import gltools as gl
import occmodel as occ
from occmodelviewer import Viewer
class Demo:
@classmethod
def eval(self):
loc = {
'pi':math.pi,
'Vertex':occ.Vertex,
'Edge':occ.Edge,
'Wire':occ.Wire,
'Face':occ.Face,
'Solid':occ.Solid,
'SWEEP_RIGHT_CORNER': occ.SWEEP_RIGHT_CORNER,
}
if sys.hexversion > 0x03000000:
exec(self.TEXT, loc)
else:
exec(self.TEXT) in loc
return self.results(loc)
class Edge_1(Demo):
NAME = "Primitives"
TEXT = \
"""
e1 = Edge().createLine(start = (0.,0.,0.), end = (1.,1.,0.))
e2 = Edge().createCircle(center = (0.,.5,0.), normal = (0.,0.,1.), radius = .5)
e3 = Edge().createArc(start = (-.5,0.,0.), end = (.5,1.,0.), center = (.5,0.,0.))
e4 = Edge().createArc3P(start = (1.,0.,0.), end = (-1.,0.,0.), pnt = (0.,1.,0.))
e5 = Edge().createEllipse(center=(0.,0.,0.),normal=(0.,0.,1.), rMajor = .5, rMinor=.2)
e6 = Edge().createHelix(pitch = .5, height = 1., radius = .25, angle = pi/5.)
pnts = ((0.,0.,0.), (0.,1.,0.), (1.,.5,0.), (1.,0.,0.))
e7 = Edge().createBezier(points = pnts)
pnts = ((0.,0.,0.), (0.,.5,0.), (1.,.25,0.),(1.,0.,0.))
e8 = Edge().createSpline(points = pnts)
"""
@classmethod
def results(self, loc):
ret = []
x,y = 0.,2.
for name in ('e1','e2','e3','e4'):
e = loc[name]
e.translate((x,y,0))
ret.append(e)
x += 1.5
x,y = 0.,0.
for name in ('e5','e6','e7','e8'):
e = loc[name]
e.translate((x,y,0))
ret.append(e)
x += 1.5
return ret
class Wire_1(Demo):
NAME = "Primitives"
TEXT = \
"""
w1 = Wire().createRectangle(width = 1., height = 0.75, radius = 0.)
w2 = Wire().createRectangle(width = 1., height = 0.75, radius = .25)
w3 = Wire().createPolygon((
(-.5,-.5,0.),
(.5,-.5,0.),
(0.,.5,0.)),
close = True,
)
w4 = Wire().createRegularPolygon(radius = .5, sides = 6.)
"""
@classmethod
def results(self, loc):
ret = []
x,y = 0.,0.
for name in ('w1','w2','w3','w4'):
e = loc[name]
e.translate((x,y,0))
ret.append(e)
x += 1.5
return ret
class Wire_2(Demo):
NAME = "Operations"
TEXT = \
"""
# offset wire
w1 = Wire().createRectangle(width = 1., height = 0.75, radius = 0.)
w2 = Wire().createRectangle(width = 1., height = 0.75, radius = 0.)
w2.offset(0.1)
# fillet all edges
w3 = Wire().createRegularPolygon(radius = .5, sides = 6.)
w3.fillet(0.2)
# chamfer all edges
w4 = Wire().createRectangle(width = 1., height = 0.75, radius = 0.)
w4.chamfer(0.15)
# wire boolean cut operation
w5 = Wire().createRectangle(width = 1., height = 1., radius = 0.)
e1 = Edge().createCircle(center=(-.5,-.5,0.),normal=(0.,0.,1.),radius = .35)
e2 = Edge().createEllipse(center=(.5,.5,0.),normal=(0.,0.,1.), rMajor = .75, rMinor=.35)
w5.cut((e1,e2))
# wire boolean common operation
w6 = Wire().createRectangle(width = 1., height = 1., radius = 0.)
e2 = Edge().createEllipse(center=(-.5,-.5,0.),normal=(0.,0.,1.), rMajor = .75, rMinor=.35)
e2.rotate(-pi/.6, (0.,0.,1.), (-.5,-.5,0.))
w6.common(e2)
"""
@classmethod
def results(self, loc):
ret = [loc['w1'], loc['w2']]
x,y = 1.5,0.
for name in ('w3','w4','w5', 'w6'):
e = loc[name]
e.translate((x,y,0))
ret.append(e)
x += 1.5
return ret
class Face_1(Demo):
NAME = "Create 1"
TEXT = \
"""
# create planar face from outer wire and edges/wires defining hole
w1 = Wire().createRectangle(width = 1., height = 1., radius = 0.)
e1 = Edge().createCircle(center=(0.,0.,0.),normal=(0.,0.,1.),radius = .25)
f1 = Face().createFace((w1, e1))
# create a face constrained by circle and points
e2 = Edge().createCircle(center=(0.,0.,0.),normal=(0.,0.,1.),radius = .5)
f2 = Face().createConstrained(e2, ((0.,.0,.25),))
# create planar polygonal face from series of points
pnts = ((-.5,-.5,0.), (0.,.5,0.), (1.,.5,0.), (.5,-.5,0.))
f3 = Face().createPolygonal(pnts)
"""
@classmethod
def results(self, loc):
ret = []
x,y = 0.,0.
for name in ('f1','f2','f3'):
e = loc[name]
e.translate((x,y,0))
ret.append(e)
x += 1.5
return ret
class Face_2(Demo):
NAME = "Create 2"
TEXT = \
"""
# create face by extruding edge/wire
e1 = Edge().createArc(start = (-.5,-.25,0.), end = (.5,.75,0.),
center = (.5,-.25,0.))
f1 = Face().extrude(e1, (0.,0.,0.), (0.,0.,1.))
# create face by revolving edge
pnts = ((0.,0.,0.), (0.,1.,0.), (1.,.5,0.), (1.,0.,0.))
e2 = Edge().createBezier(points = pnts)
f2 = Face().revolve(e2, (0.,-1.,0.), (1.,-1.,0.), pi/2.)
# create face by sweeping edge along spine
e3 = Edge().createArc((0.,0.,0.), (1.,0.,1.), (1.,0.,0.))
e4 = Edge().createCircle(center=(0.,0.,0.),normal=(0.,0.,1.),radius = .25)
f3 = Face().sweep(e3, e4)
# create face by lofting through edges
e5 = Edge().createArc((0.,0.,0.),(1.,0.,1.),(1.,0.,0.))
e6 = Edge().createArc((0.,1.,0.),(2.,1.,2.),(2.,1.,0.))
f4 = Face().loft((e5,e6))
"""
@classmethod
def results(self, loc):
ret = []
x,y = 0.,0.
for name in ('f1','f2','f3','f4'):
e = loc[name]
e.translate((x,y,0))
ret.append(e)
x += 1.5
return ret
class Face_3(Demo):
NAME = "Create 3"
TEXT = \
"""
# cut face by edge
e1 = Edge().createArc(start = (-.5,-.25,0.), end = (.5,.75,0.), center = (.5,-.25,0.))
f1 = Face().extrude(e1, (0.,0.,0.), (0.,0.,1.))
e2 = Edge().createCircle(center=(.5,.5,0.),normal=(0.,1.,0.),radius = .75)
f1.cut(e2)
# find common face between circulare face and ellipse
e3 = Edge().createCircle(center=(0.,0.,0.),normal=(0.,0.,1.),radius = .5)
f2 = Face().createFace(e3)
e4 = Edge().createEllipse(center=(0.,0.,0.),normal=(0.,0.,1.), rMajor = .75, rMinor=.3)
f2.common(e4)
"""
@classmethod
def results(self, loc):
ret = []
x,y = 0.,0.
for name in ('f1','f2'):
e = loc[name]
e.translate((x,y,0))
ret.append(e)
x += 1.5
return ret
class Solid_1(Demo):
NAME = "Primitives"
TEXT = \
"""
# solid sphere from center and radius
s1 = Solid().createSphere((0.,0.,0.),.5)
# solid cylinder from two points and radius
s2 = Solid().createCylinder((0.,0.,0.),(0.,0.,1.), .25)
# solid torus from two points defining axis, ring radius and radius.
s3 = Solid().createTorus((0.,0.,0.),(0.,0.,.1), .5, .1)
# solid cone from two points defining axis and upper and lower radius
s4 = Solid().createCone((0.,0.,0.),(0.,0.,1.), .2, .5)
# solid box from two points defining diagonal of box
s5 = Solid().createBox((-.5,-.5,-.5),(.5,.5,.5))
"""
@classmethod
def results(self, loc):
ret = []
x,y = 0.,0.
for name in ('s1','s2','s3','s4','s5'):
e = loc[name]
e.translate((x,y,0))
ret.append(e)
x += 1.5
return ret
class Solid_2(Demo):
NAME = "Create 1"
TEXT = \
"""
# create solid by extruding face
e1 = Edge().createLine((-.5,0.,0.),(.5,0.,0.))
e2 = Edge().createArc3P((.5,0.,0.),(-.5,0.,0.),(0.,.5,0.))
w1 = Wire().createWire((e1,e2))
f1 = Face().createFace(w1)
s1 = Solid().extrude(f1, (0.,0.,0.), (0.,0.,1.))
# create solid by revolving face
e2 = Edge().createEllipse(center=(0.,0.,0.),normal=(0.,0.,1.), rMajor = .5, rMinor=.2)
f2 = Face().createFace(e2)
s2 = Solid().revolve(f2, (1.,0.,0.), (1.,1.,0.), pi/2.)
# create solid by sweeping wire along wire path
w1 = Wire().createPolygon((
(0.,0.,0.),
(0.,0.,1.),
(.75,0.,1.),
(.75,0.,0.)),
close = False
)
e3 = Edge().createCircle(center=(0.,0.,0.),normal=(0.,0.,1.),radius = .2)
s3 = Solid().sweep(w1, e3, cornerMode = SWEEP_RIGHT_CORNER)
# create solid by lofting through edges, wires and optional start/end vertex.
e4 = Edge().createCircle(center=(.25,0.,0.),normal=(0.,0.,1.),radius = .25)
e5 = Edge().createCircle(center=(.25,0.,.5),normal=(0.,0.,1.),radius = .5)
v1 = Vertex(.25,0.,1.)
s4 = Solid().loft((e4,e5,v1))
# create solid by sweeping face along path
e6 = Edge().createHelix(.4, 1., .4)
e7 = Edge().createCircle(center=(.5,0.,0.),normal=(0.,1.,0.),radius = 0.1)
f3 = Face().createFace(e7)
s5 = Solid().pipe(f3, e6)
"""
@classmethod
def results(self, loc):
ret = []
x,y = 0.,0.
for name in ('s1','s2','s3','s4','s5'):
e = loc[name]
e.translate((x,y,0))
ret.append(e)
x += 1.5
return ret
class Solid_3(Demo):
NAME = "Create 2"
TEXT = \
"""
# fuse solids
s1 = Solid().createBox((0.,0.,0.),(.5,.5,.5))
s2 = Solid().createBox((.25,.25,.25),(.75,.75,.75))
s1.fuse(s2)
# modifying solid by cutting against edge,wire,face or solid.
# Edge and wire always cut through, but Face only cuts in the
# direction of the normal.
s2 = Solid().createBox((0.,0.,0.),(1.,1.,1.))
e1 = Edge().createCircle(center=(0.5,0.5,1.),normal=(0.,0.,1.),radius = 0.1)
e2 = Edge().createCircle(center=(.5,0.,.5),normal=(0.,0.,1.),radius = 0.25)
f1 = Face().createFace(e2)
s3 = Solid().createSphere((1.,1.,1.),.35)
s2.cut((e1,f1,s3))
# find common shape
s4 = Solid().createSphere((.5,.5,0.),.75)
s5 = Solid().createCylinder((.5,.5,-1),(0.,.5,1.), .5)
s4.common(s5)
# fillet edges
s6 = Solid().createBox((0.,0.,0.),(1.,1.,1.))
s6.fillet(.2)
# chamfer edges
s7 = Solid().createBox((0.,0.,0.),(1.,1.,1.))
s7.chamfer(.2)
# shell operation
s8 = Solid().createBox((0.,0.,0.),(1.,1.,1.))
s8.shell(-.1)
# offset face to create solid
e3 = Edge().createArc((0.,0.,0.),(.5,0.,.5),(.5,0.,0.))
e4= Edge().createArc((0.,.5,0.),(1.,.5,1.),(1.,.5,0.))
f2 = Face().loft((e3,e4))
s9 = Solid().offset(f2, 0.2)
"""
@classmethod
def results(self, loc):
ret = []
x,y = 0.,0.
for name in ('s1','s2','s4','s6','s7','s8','s9'):
e = loc[name]
e.translate((x,y,0))
ret.append(e)
x += 1.5
return ret
class Solid_4(Demo):
NAME = "Create 3"
TEXT = \
"""
# create solid by sewing together faces
e1 = Edge().createCircle(center=(0.,0.,0.),normal=(0.,0.,-1.),radius = .5)
f1 = Face().createConstrained(e1, ((0.,.0,-.5),))
e2 = Edge().createCircle(center=(0.,0.,1.),normal=(0.,0.,1.),radius = .5)
f2 = Face().createConstrained(e2, ((0.,.0,1.5),))
f3 = Face().loft((e1,e2))
s1 = Solid().createSolid((f1,f3,f2))
# create solid from TrueType font
s2 = Solid().createText(1., .25, 'Tenko')
"""
@classmethod
def results(self, loc):
ret = []
x,y = 0.,0.
for name in ('s1','s2'):
e = loc[name]
e.translate((x,y,0))
ret.append(e)
x += 1.5
return ret
class DemoViewer(Viewer):
def __init__(self, fullscreen = False):
title = "Demo (F1 for help - 'm' to toggle menu)"
Viewer.__init__(self, -1, -1, title, fullscreen)
self.uiView = False
self.uiDemo = True
self.showUI = True
self.source = ''
self.defaultColor = gl.ColorRGBA(100,100,100,255)
self.edgeColor = gl.ColorRGBA(255,255,255,255)
def activeUI(self, x, y):
w, h = self.width, self.height
y = h - y
if not self.showUI:
return False
if self.ui.anyActive() or self.uiHelp:
return True
if x >= 10 and x <= 200:
if y >= .4*h and y <= h - 10:
return True
if self.source and x >= .5*w and x < w - 10:
if y >= 10 and y <= 160:
return True
return False
def onUI(self):
if self.uiQuit:
return self.onUIQuit()
if self.uiHelp:
return self.onUIHelp()
ui = self.ui
update = False
w, h = self.width, self.height
x, y = self.lastPos
scroll = self.uiScroll
if scroll != 0:
self.uiScroll = 0
if not self.showUI:
# empty gui
ui.beginFrame(x,h - y,self.currentButton,scroll)
ui.endFrame()
return update
ui.beginFrame(x,h - y,self.currentButton,scroll)
ui.beginScrollArea("Menu", 10, .4*h, 200, .6*h - 10)
ui.separatorLine()
if ui.collapse("View settings", "", self.uiView, True):
self.uiView = not self.uiView
if self.uiView:
ui.indent()
ui.label("View presets")
ui.indent()
if ui.item('Top', True):
self.onTopView()
update = True
if ui.item('Bottom', True):
self.onBottomView()
update = True
if ui.item('Front', True):
self.onFrontView()
update = True
if ui.item('Back', True):
self.onBackView()
update = True
if ui.item('Left', True):
self.onLeftView()
update = True
if ui.item('Right', True):
self.onRightView()
update = True
if ui.item('Iso', True):
self.onIsoView()
update = True
ui.unindent()
if ui.check('Gradient background', self.uiGradient, True):
self.uiGradient = not self.uiGradient
update = True
if ui.check('Specular material', self.uiSpecular, True):
self.uiSpecular = not self.uiSpecular
update = True
if ui.check('Draw face edges', self.uiEdges, True):
self.uiEdges = not self.uiEdges
update = True
if ui.button('Take screenshot', True):
self.onScreenShot(prefix = 'demoShot')
ui.unindent()
ui.separatorLine()
if ui.collapse("Demos", "", self.uiDemo, True):
self.uiDemo = not self.uiDemo
if self.uiDemo:
ui.indent()
ui.label("Edges")
ui.indent()
if ui.item(Edge_1.NAME, True):
self.onSetDemo(Edge_1)
update = True
ui.unindent()
ui.label("Wires")
ui.indent()
if ui.item(Wire_1.NAME, True):
self.onSetDemo(Wire_1)
update = True
if ui.item(Wire_2.NAME, True):
self.onSetDemo(Wire_2)
update = True
ui.unindent()
ui.label("Faces")
ui.indent()
if ui.item(Face_1.NAME, True):
self.onSetDemo(Face_1)
update = True
if ui.item(Face_2.NAME, True):
self.onSetDemo(Face_2)
update = True
if ui.item(Face_3.NAME, True):
self.onSetDemo(Face_3)
update = True
ui.unindent()
ui.label("Solids")
ui.indent()
if ui.item(Solid_1.NAME, True):
self.onSetDemo(Solid_1)
update = True
if ui.item(Solid_2.NAME, True):
self.onSetDemo(Solid_2)
update = True
if ui.item(Solid_3.NAME, True):
self.onSetDemo(Solid_3)
update = True
if ui.item(Solid_4.NAME, True):
self.onSetDemo(Solid_4)
update = True
ui.indent()
ui.endScrollArea()
if self.source:
ui.beginScrollArea("Source", .5*w, 10, .5*w - 10, 150.)
for line in self.source.splitlines():
if not line.strip():
ui.separator()
continue
ui.label(line)
ui.endScrollArea()
ui.endFrame()
return update
def onSetDemo(self, demo):
self.source = demo.TEXT.strip()
self.bbox.invalidate()
self.clear()
for obj in demo.eval():
self.add(obj)
self.onIsoView()
def onChar(self, ch):
if ch == 'm':
self.showUI = not self.showUI
self.onRefresh()
Viewer.onChar(self, ch)
def main():
mw = DemoViewer()
mw.onIsoView()
mw.mainLoop()
if __name__ == '__main__':
main()