This repository has been archived by the owner on May 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapper.py
169 lines (140 loc) · 4 KB
/
wrapper.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
def expand(l):
return "".join([obj.toScript() for obj in l])
class base():
def __init__(self):
self.modifier=""
self.alignmentPoints={}
def getAligned(self,name):
if name in self.alignmentPoints:
return translate(*[-p for p in self.alignmentPoints[name]])(self)
else:
raise Exception("no alignment point \"{}\" found. Available alignment points: \"{}\"".format(name, "\",\"".join(self.alignmentPoints)))
class obj(base):
def __init__(self):
base.__init__(self)
def write(self,writer):
writer.writeLine(self.toScript())
class cube(obj):
def __init__(self,x,y,z):
obj.__init__(self)
self.x = x
self.y = y
self.z = z
def toScript(self):
return self.modifier+"cube([{},{},{}]);".format(self.x,self.y,self.z)
class cylinder(obj):
def __init__(self,r,h,fn=None,r2=None):
obj.__init__(self)
self.r = r
self.h = h
self.fn = fn
self.r2 = r2
def toScript(self):
params={
"r" if self.r2 is None else "r1":self.r,
"h":self.h
}
if self.r2 is not None:
params["r2"]=self.r2
return self.modifier+"cylinder({params}{additional});".format(params=",".join(["{}={}".format(k,v) for k,v in params.iteritems()]),additional=",$fn={}".format(self.fn) if self.fn is not None else "")
class sphere(obj):
def __init__(self,r,fn=None):
obj.__init__(self)
self.r = r
self.fn = fn
def toScript(self):
return self.modifier+"sphere(r={r}{additional});".format(r=self.r,additional=",$fn={}".format(self.fn) if self.fn is not None else "")
class container(base):
def __init__(self,**kwargs):
base.__init__(self,**kwargs)
self.positive=[]
self.negative=[]
def __call__(self,*args):
for arg in args:
if isinstance(arg,list) or isinstance(arg,tuple):
self(*arg)
else:
assert isinstance(arg,base)
if isinstance(arg,negative):
self.negative.append(arg)
else:
self.positive.append(arg)
return self
def writeBody(self,writer):
if len(self.negative)==0:
if len(self.positive)==0:
writer.writeLine("{}")
else:
writer.writeLine("union()")
writer.writeLine("{")
writer.incDepth()
for p in self.positive:
p.write(writer)
writer.decDepth()
writer.writeLine("}")
else:
if len(self.positive)==0:
raise Exception("only negative elements in body")
else:
writer.writeLine("difference()")
writer.writeLine("{")
writer.incDepth()
writer.writeLine("union()")
writer.writeLine("{")
writer.incDepth()
for p in self.positive:
p.write(writer)
writer.decDepth()
writer.writeLine("}")
for p in self.negative:
p.write(writer)
writer.decDepth()
writer.writeLine("}")
class mirror(container):
def __init__(self,x,y,z):
container.__init__(self)
self.x = x
self.y = y
self.z = z
def write(self,writer):
writer.writeLine(self.modifier+"mirror([{},{},{}])".format(self.x,self.y,self.z))
self.writeBody(writer)
class translate(container):
def __init__(self,x,y,z):
container.__init__(self)
self.x = x
self.y = y
self.z = z
def write(self,writer):
writer.writeLine(self.modifier+"translate([{},{},{}])".format(self.x,self.y,self.z))
self.writeBody(writer)
class rotate(container):
def __init__(self,x,y,z):
container.__init__(self)
self.x = x
self.y = y
self.z = z
def write(self,writer):
writer.writeLine(self.modifier+"rotate([{},{},{}])".format(self.x,self.y,self.z))
self.writeBody(writer)
class negative(container):
def __init__(self):
container.__init__(self)
def write(self,writer):
writer.writeLine(self.modifier)
self.writeBody(writer)
class group(container):
def __init__(self):
container.__init__(self)
def write(self,writer):
writer.writeLine(self.modifier)
self.writeBody(writer)
class module_call(container):
def __init__(self,module_name,*args,**kwargs):
container.__init__(self)
self.module_name = module_name
self.args = args
self.kwargs = kwargs
def write(self,writer):
writer.writeLine(self.modifier+"{}([{},{},{}])".format(self.module_name,self.x,self.y,self.z))
self.writeBody(writer)