-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclass_wrapping.py
45 lines (30 loc) · 1.49 KB
/
class_wrapping.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
import cadquery as cq
import logging
# A so-far unsuccessful experiment of how custom parts could be wrapped into classes. A better
# technique is shown in Case.py in the same directory.
class TestPart(cq.Workplane):
def __init__(self, param, name = "TestPart", color = None, alpha = None):
self.param = float(param)
self.name = name
self.color = color
self.alpha = alpha
self.log = logging.getLogger(__name__)
super(TestPart, self).__init__('XY')
self.log.info("DEBUG: __init__(self, param)")
def build(self):
self.log.info("DEBUG: build(self)")
# Since CadQuery Workplane methods generally return a copy instead of modifying self,
# we cannot do "self.box(100, 100, 100); return self;".
box = self.box(100, 100, 100)
return self.union(box)
def show(self):
"""
It is somehow not possible to use show_object() with custom classes. To work around that without having to expose
an inner CadQuery object, we implement our own show() method for that inner object.
"""
options = {}
if self.color != None: options["color"] = self.color
if self.alpha != None: options["alpha"] = float(self.alpha) # Allow accidental string arguments, unlike show_object().
show_object(self.build(), self.name, options)
part = TestPart(param = 10, name = "my_test_part", color = "blue", alpha = 0.5)
part.show()