/
shapes.py
294 lines (209 loc) · 6.67 KB
/
shapes.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
from __future__ import print_function
from __future__ import absolute_import
from __future__ import division
import scriptcontext as sc # type: ignore
import Rhino # type: ignore
from compas.geometry import Plane
from compas.geometry import Circle
from compas.geometry import Box
from compas.geometry import Sphere
from compas.geometry import Cone
from compas.geometry import Cylinder
from compas.geometry import Torus
from compas.geometry import Frame
# from .geometry import plane_to_rhino
from .geometry import frame_to_rhino
from .geometry import point_to_rhino
from .geometry import plane_to_compas_frame
from .geometry import plane_to_compas
from .geometry import point_to_compas
from .geometry import vector_to_compas
from .curves import line_to_rhino_curve
from .curves import circle_to_rhino
# =============================================================================
# To Rhino
# =============================================================================
def box_to_rhino(box):
"""Convert a COMPAS box to a Rhino box.
Parameters
----------
box : :class:`compas.geometry.Box`
Returns
-------
:rhino:`Rhino.Geometry.Box`
"""
return Rhino.Geometry.Box(
frame_to_rhino(box.frame),
Rhino.Geometry.Interval(-0.5 * box.xsize, 0.5 * box.xsize),
Rhino.Geometry.Interval(-0.5 * box.ysize, 0.5 * box.ysize),
Rhino.Geometry.Interval(-0.5 * box.zsize, 0.5 * box.zsize),
)
def sphere_to_rhino(sphere):
"""Convert a COMPAS sphere to a Rhino sphere.
Parameters
----------
sphere : :class:`compas.geometry.Sphere`
Returns
-------
:rhino:`Rhino.Geometry.Sphere`
"""
return Rhino.Geometry.Sphere(point_to_rhino(sphere.frame.point), sphere.radius)
def cone_to_rhino(cone):
"""Convert a COMPAS cone to a Rhino cone.
Parameters
----------
cone : :class:`compas.geometry.Cone`
Returns
-------
:rhino:`Rhino.Geometry.Cone`
"""
# return Rhino.Geometry.Cone(plane_to_rhino(cone.circle.plane), cone.height, cone.circle.radius)
frame = Frame(cone.frame.point + cone.frame.zaxis * cone.height, cone.frame.xaxis, cone.frame.yaxis)
return Rhino.Geometry.Cone(frame_to_rhino(frame), -cone.height, cone.radius)
def cone_to_rhino_brep(cone):
"""Convert a COMPAS cone to a Rhino Brep.
Parameters
----------
cone : :class:`compas.geometry.Cone`
A COMPAS cone.
Returns
-------
Rhino.Geometry.Brep
"""
return Rhino.Geometry.Cone.ToBrep(cone_to_rhino(cone), True)
def cylinder_to_rhino(cylinder):
"""Convert a COMPAS cylinder to a Rhino cylinder.
Parameters
----------
cylinder : :class:`compas.geometry.Cylinder`
Returns
-------
:rhino:`Rhino.Geometry.Cylinder`
"""
circle = cylinder.circle.copy()
circle.frame.point += circle.frame.zaxis * (-0.5 * cylinder.height)
return Rhino.Geometry.Cylinder(circle_to_rhino(circle), cylinder.height)
def cylinder_to_rhino_brep(cylinder):
"""Convert a COMPAS cylinder to a Rhino Brep.
Parameters
----------
cylinder : :class:`compas.geometry.Cylinder`
A COMPAS cylinder.
Returns
-------
Rhino.Geometry.Brep
"""
return Rhino.Geometry.Cylinder.ToBrep(cylinder_to_rhino(cylinder), True, True)
def capsule_to_rhino(capsule):
"""Convert a COMPAS capsule to a Rhino cylinder.
Parameters
----------
capsule : :class:`compas.geometry.Capsule`
Returns
-------
:rhino:`Rhino.Geometry.Cylinder`
"""
raise NotImplementedError
def capsule_to_rhino_brep(capsule):
"""Convert a COMPAS capsule to a Rhino Brep.
Parameters
----------
capsule : :class:`compas.geometry.Capsule`
A COMPAS capsule.
Returns
-------
list[Rhino.Geometry.Brep]
"""
abs_tol = sc.doc.ModelAbsoluteTolerance
ang_tol = sc.doc.ModelAngleToleranceRadians
radius = capsule.radius
line = capsule.axis
curve = line_to_rhino_curve(line)
return Rhino.Geometry.Brep.CreatePipe(
curve, radius, False, Rhino.Geometry.PipeCapMode.Round, False, abs_tol, ang_tol
)
def torus_to_rhino(torus):
"""Convert a COMPAS torus to a Rhino torus.
Parameters
----------
torus : :class:`compas.geometry.Torus`
Returns
-------
:rhino:`Rhino.Geometry.Torus`
"""
return Rhino.Geometry.Torus(frame_to_rhino(torus.frame), torus.radius_axis, torus.radius_pipe)
def torus_to_rhino_brep(torus):
"""Convert a COMPAS torus to a Rhino Brep.
Parameters
----------
torus : :class:`compas.geometry.Torus`
A COMPAS torus.
Returns
-------
Rhino.Geometry.Brep
The Rhino brep representation.
"""
return torus_to_rhino(torus).ToNurbsSurface().ToBrep()
# =============================================================================
# To COMPAS
# =============================================================================
def box_to_compas(box):
"""Convert a Rhino box to a COMPAS box.
Parameters
----------
box : :rhino:`Rhino.Geometry.Box`
Returns
-------
:class:`compas.geometry.Box`
"""
xsize = box.X.Length
ysize = box.Y.Length
zsize = box.Z.Length
frame = plane_to_compas_frame(box.Plane)
frame.point = point_to_compas(box.Center)
return Box(xsize, ysize, zsize, frame=frame)
def sphere_to_compas(sphere):
"""Convert a Rhino sphere to a COMPAS sphere.
Parameters
----------
sphere : :rhino:`Rhino.Geometry.Sphere`
Returns
-------
:class:`compas.geometry.Sphere`
"""
return Sphere(point_to_compas(sphere.Center), sphere.Radius)
def cone_to_compas(cone):
"""Convert a Rhino cone to a COMPAS cone.
Parameters
----------
cone : :rhino:`Rhino.Geometry.Cone`
Returns
-------
:class:`compas.geometry.Cone`
"""
plane = Plane(cone.BasePoint, vector_to_compas(cone.Plane.Normal).inverted())
return Cone(Circle(plane, cone.Radius), cone.Height)
def cylinder_to_compas(cylinder):
"""Convert a Rhino cylinder to a COMPAS cylinder.
Parameters
----------
cylinder : :rhino:`Rhino.Geometry.Cylinder`
Returns
-------
:class:`compas.geometry.Cylinder`
"""
plane = plane_to_compas(cylinder.BasePlane)
height = cylinder.TotalHeight
plane.point += plane.normal * (0.5 * height)
return Cylinder(Circle(plane, cylinder.Radius), height)
def torus_to_compas(torus):
"""Convert a Rhino torus to a COMPAS torus.
Parameters
----------
torus : :rhino:`Rhino.Geometry.Torus`
Returns
-------
:class:`compas.geometry.Torus`
"""
frame = plane_to_compas_frame(torus.Plane)
return Torus(torus.MajorRadius, torus.MinorRadius, frame=frame)