forked from openscad/openscad
-
Notifications
You must be signed in to change notification settings - Fork 3
/
pyscad.py
251 lines (207 loc) · 6.49 KB
/
pyscad.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
import ctypes
_openscad=ctypes.cdll.LoadLibrary('./libopenscad.so')
_openscad.inst_module.restype = ctypes.c_void_p
_openscad.export_stl.restype = ctypes.c_char_p
_openscad.export_dxf.restype = ctypes.c_char_p
_openscad.to_source.restype = ctypes.c_char_p
_openscad.init()
class Value(ctypes.Union):
"""A C++ OpenSCAD value."""
_fields_ = [
("dblValue", ctypes.c_double),
("strValue", ctypes.c_char_p),
("boolValue", ctypes.c_bool),
("vecValue", ctypes.POINTER(ctypes.c_double)),
]
class Arg(ctypes.Structure):
"""A C++ OpenSCAD argument."""
_fields_ = [
("name", ctypes.c_char_p),
("type", ctypes.c_char),
("vecLen", ctypes.c_int),
("value", Value),
]
_anonymous_ = ("value",)
def setFrom(self, val, name=None):
"""Create an argument using a value as input.
Arguments:
Value -- an OpenSCAD value
string -- the name of the value
"""
if name:
self.name = ctypes.c_char_p(name)
else:
self.name = ctypes.c_char_p(0)
if isinstance(val, bool):
self.type = 'b'
self.boolValue = ctypes.c_bool(val)
elif isinstance(val, int) or isinstance(val, float):
self.type = 'd'
self.dblValue = ctypes.c_double(val)
elif isinstance(val, str):
self.type = 's'
self.strValue = ctypes.c_char_p(val)
elif isinstance(val, list) or isinstance(val, tuple):
self.type = 'v'
self.vecLen = ctypes.c_int(len(val))
arr = (ctypes.c_double * len(val))()
for i, v in enumerate(val):
arr[i] = ctypes.c_double(v)
self.vecValue = arr
class SCADObject(object):
"""An OpenSCAD object."""
def __init__(self, modname, *args, **kwargs):
"""Create and display an OpenSCAD object. Not called directly,
only by descendents.
Arguments:
string -- type of OpenSCAD object
tuple -- tuple of arguments
mixed -- any number of keyword arguments
"""
self.modname = modname
if 'children' in kwargs:
self.children = kwargs['children']
del kwargs['children']
else:
self.children = []
self.args = args
self.kwargs = kwargs
self.transforms = []
#overload addition, subtraction, multiplication for SCADObjects
def __add__(self, x):
"""OpenSCAD addition.
Arguments:
SCADObject -- the object to add to the current object
"""
return union(self, x)
def __sub__(self, x):
"""OpenSCAD subtraction.
Arguments:
SCADObject -- the object to subtract from the current object
"""
return difference(self, x)
def __mul__(self, x):
"""OpenSCAD multiplication.
Arguments:
SCADObject -- the object to multiply by the current object
"""
return intersection(self, x)
def _cpp_object(self):
"""The C++ representation of this object."""
numargs = len(self.args) + len(self.kwargs)
args = (Arg*numargs)()
i = 0
for a in self.args:
args[i].setFrom(a)
i += 1
for n in self.kwargs:
args[i].setFrom(self.kwargs[n], name=n)
i += 1
numchildren = len(self.children)
children = (ctypes.c_void_p * numchildren)()
for i, c in enumerate(self.children):
children[i] = c._cpp_object()
result = _openscad.inst_module(self.modname, numargs, ctypes.byref(args), numchildren, ctypes.byref(children))
if len(self.transforms) > 0:
for transform in self.transforms:
modname = ctypes.c_char_p(transform.modname)
args = (Arg * 1)()
args[0].setFrom(transform.args[0])
children = (ctypes.c_void_p * 1)()
children[0] = result
result = _openscad.inst_module(modname, 1, ctypes.byref(args), 1, children)
return result
def render(self):
"""Render and display what you have made."""
_openscad.render(self._cpp_object())
def export_stl(self, filename):
"""Export what you have made to an .stl file, only works if
what you have made is 3D.
"""
err = _openscad.export_stl(self._cpp_object(), filename)
if err:
raise ValueError(err)
def export_dxf(self, filename):
"""Export what you have made to a .dxf file, only works if
what you have made is 2D.
"""
err = _openscad.export_dxf(self._cpp_object(), filename)
if err:
raise ValueError(err)
def to_source(self):
"""return what you have made as a scad string"""
return _openscad.to_source(self._cpp_object())
class sphere(SCADObject):
"""An OpenSCAD sphere."""
def __init__(self, radius, center = True, transforms = []):
"""Create and display a sphere.
Arguments:
real -- the radius
tuple -- the position
"""
super(sphere, self).__init__(modname='sphere', r = radius, center = center)
self.transforms = transforms
class cube(SCADObject):
"""An OpenSCAD cube."""
def __init__(self, size, center = False, transforms = []):
"""Create and display a cube.
Arguments:
real or tuple -- the size
tuple -- the position
"""
super(cube, self).__init__(modname='cube', size = size, center = center)
self.transforms = transforms
class cylinder(SCADObject):
"""An OpenSCAD cylinder."""
def __init__(self, height, radiusTop, radiusBottom = None, center = False, transforms = []):
"""Create and display a cylinder.
Arguments:
real -- the height
real -- the radius of the top of the cylinder
real -- the radius of the bottom of the cylinder
tuple -- the position
"""
if radiusBottom == None:
radiusBottom = radiusTop
super(cylinder, self).__init__(modname='cylinder', h = height, r1 = radiusTop, r2 = radiusBottom, center = center)
self.transforms = transforms
class union(SCADObject):
"""An OpenSCAD union."""
def __init__(self, *children):
"""Create and display a union of objects
Arguments:
tuple -- objects to union
"""
super(union, self).__init__(modname='union', children=children)
class difference(SCADObject):
"""An OpenSCAD difference."""
def __init__(self, *children):
"""Create and display a difference of objects
Arguments:
tuple -- objects to difference
"""
super(difference, self).__init__(modname='difference', children=children)
class intersection(SCADObject):
"""An OpenSCAD intersection."""
def __init__(self, *children):
"""Create and display an intersection of objects
Arguments:
tuple -- objects to intersect
"""
super(intersection, self).__init__(modname='intersection', children=children)
class translate(SCADObject):
"""An OpenSCAD translation."""
def __init__(self, translation):
"""Create a translation
Arguments:
tuple -- x, y, z to translate
"""
super(translate, self).__init__('translate', translation)
class rotate(SCADObject):
"""An OpenSCAD rotation."""
def __init__(self, rotation):
"""Create a rotation
Arguments:
tuple -- degrees x, y, z to rotate
"""
super(rotate, self).__init__('rotate', rotation)