forked from CADacombs/rhinopython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxBrepFace_tryGetPrimitiveShape.py
More file actions
303 lines (252 loc) · 11.5 KB
/
xBrepFace_tryGetPrimitiveShape.py
File metadata and controls
303 lines (252 loc) · 11.5 KB
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
"""
190521,22: Import-related update.
190523-24: Added some functions.
190529: Removed a function.
190530-31: Improved printed output.
190605: Fixed a minor bug.
190617: Improved efficiency of a function.
190619: Added a function.
190721: Argument name change.
190722-24: Refactoring. Changed output of some functions.
190730: More feedback is now returned from a function.
190731: Removed some debug prints.
190809-10: Corrected the default return value in a function.
190822: Modified a comment.
191118-19: Import-related update.
"""
import Rhino
import Rhino.Geometry as rg
import scriptcontext as sc
import xPlaneSurface
import xPrimitiveShape
def tryGetPlane(rgFace0, bMatchToShrunkFace=True, fTolerance=sc.doc.ModelAbsoluteTolerance, bDebug=False):
if bMatchToShrunkFace:
rgBrep_1Face_Shrunk = rgFace0.DuplicateFace(False)
rgBrep_1Face_Shrunk.Faces.ShrinkFaces()
rgFace_Shrunk = rgBrep_1Face_Shrunk.Faces[0]
rgSrfs_ToTry = rgFace0.UnderlyingSurface(), rgFace_Shrunk.UnderlyingSurface()
else:
rgSrfs_ToTry = rgFace0.UnderlyingSurface(),
for i_NotShrunk_Shrunk, rgSrf_ToTry in enumerate(rgSrfs_ToTry):
rc = xPrimitiveShape.Surface.tryGetPlane(
rgSrf_ToTry,
fTolerance=fTolerance,
bDebug=bDebug)
if rc[0]:
if bMatchToShrunkFace: rgBrep_1Face_Shrunk.Dispose()
rgPlane_Found, fTol_PlaneFound = rc
if not rgPlane_Found.IsValid:
return None, "Plane is not valid."
bShrunkUsed = bool(i_NotShrunk_Shrunk)
return (rgPlane_Found, fTol_PlaneFound, bShrunkUsed), None
if bMatchToShrunkFace: rgBrep_1Face_Shrunk.Dispose()
return None, "Plane not found within tolerance of {:.2e}.".format(fTolerance)
def tryGetRoundPrimitive(rgFace0, bMatchToShrunkFace=True, bCylinder=True, bCone=True, bSphere=True, bTorus=True, fTolerance=sc.doc.ModelAbsoluteTolerance, bDebug=False):
"""
Round primitives are Cone, Cylinder, Sphere, and Torus.
"""
if bDebug:
reload(xPrimitiveShape)
if bMatchToShrunkFace:
rgBrep_1Face_Shrunk = rgFace0.DuplicateFace(False)
rgBrep_1Face_Shrunk.Faces.ShrinkFaces()
rgFace_Shrunk = rgBrep_1Face_Shrunk.Faces[0]
rgFaces_ToTest = rgFace0, rgFace_Shrunk
rgSrfs_ToTry = rgFace0.UnderlyingSurface(), rgFace_Shrunk.UnderlyingSurface()
else:
rgSrfs_ToTry = rgFace0.UnderlyingSurface(),
for i_NotShrunk_Shrunk, rgSrf_ToTry in enumerate(rgSrfs_ToTry):
rc = xPrimitiveShape.Surface.tryGetRoundPrimitive(
rgSrf0=rgSrf_ToTry,
bCylinder=bCylinder,
bCone=bCone,
bSphere=bSphere,
bTorus=bTorus,
fTolerance=fTolerance,
bDebug=bDebug)
if rc[0]:
if bMatchToShrunkFace: rgBrep_1Face_Shrunk.Dispose()
rgShape_Found, fTol_ShapeFound = rc
if not rgShape_Found.IsValid:
return None, "{} is not valid.".format(rgShape_Found.GetType().Name)
bShrunkUsed = bool(i_NotShrunk_Shrunk)
return (rgShape_Found, fTol_ShapeFound, bShrunkUsed), None
if bMatchToShrunkFace: rgBrep_1Face_Shrunk.Dispose()
return None, "Round primitive not found within tolerance of {:.2e}.".format(fTolerance)
def tryGetPlaneSurface(rgFace0, bMatchToShrunkFace=True, fTolerance=0.1*sc.doc.ModelAbsoluteTolerance, bDebug=False):
"""
Return on success:
PlaneSurface at a size relative to the BrepFace
fTol_Used
bShrunkUsed
Return on fail:
None
String: Log statement.
"""
rgSrf_Face0 = rgFace0.UnderlyingSurface()
if isinstance(rgSrf_Face0, rg.PlaneSurface):
return None, "Surface is already a PlaneSurface."
rc = tryGetPlane(
rgFace0,
bMatchToShrunkFace=bMatchToShrunkFace,
fTolerance=fTolerance,
bDebug=bDebug)
if rc[0] is None:
return rc
else:
rgPlane1, fTol_Used, bShrunkUsed = rc[0]
rgSrf1 = xPlaneSurface.createFromPlaneAndObjectSize(
rgPlane=rgPlane1,
obj_ForSize=rgFace0,
bDebug=bDebug)
if rgSrf1 is None:
return None, "createFromPlaneAndObjectSize fail."
return (rgSrf1, fTol_Used, bShrunkUsed), None
def tryGetRevSurfaceOfPrimitiveShape(rgFace0, bMatchToShrunkFace=True, bCylinder=True, bCone=True, bSphere=True, bTorus=True, fTolerance=0.1*sc.doc.ModelAbsoluteTolerance, bDebug=False):
"""
"""
rgSrf0 = rgFace0.UnderlyingSurface()
rc = tryGetRoundPrimitive(
rgFace0=rgFace0,
bMatchToShrunkFace=bMatchToShrunkFace,
bCylinder=bCylinder,
bCone=bCone,
bSphere=bSphere,
bTorus=bTorus,
fTolerance=fTolerance,
bDebug=bDebug)
if rc[0] is None: return None, "No matching primitive shape found."
rgShape1, fTol_PrimitiveMatch, bFoundUsingShunkFace = rc[0]
if isinstance(rgSrf0, rg.RevSurface):
if isinstance(rgShape1, rg.Cylinder) or isinstance(rgShape1, rg.Cone):
if rgSrf0.IsClosed(0) or rgSrf0.IsClosed(1):
return None, "Surface is already a RevSurface of {} shape.".format(rgShape1.GetType().Name)
if isinstance(rgShape1, rg.Sphere) or isinstance(rgShape1, rg.Torus):
if rgSrf0.IsClosed(0) and rgSrf0.IsClosed(1):
return None, "Surface is already a RevSurface of {} shape.".format(rgShape1.GetType().Name)
typeShape1 = rgShape1.GetType()
if not rgShape1.IsValid:
return None, "{} is not valid.".format(typeShape1)
if typeShape1 == rg.Plane:
rgPlane = rgShape1
sType = "Plane"
rgSrf_Converted = xPlaneSurface.createFromPlaneAndObjectSize(
rgPlane, rgFace0)
if rgSrf_Converted is not None:
return (rgSrf_Converted, fTol_PrimitiveMatch, bFoundUsingShunkFace), None
else:
sPrint = 'rgSrf_Converted'
return None, sPrint + ':', eval(sPrint)
elif typeShape1 == rg.Cylinder:
rgCyl = rgShape1
sType = "Cylinder"
# Fix cylinder that is infinite (has 0 height).
if not rgCyl.IsFinite:
if bDebug: print "Making cylinder finite..."
rgBbox_Face = rgFace0.GetBoundingBox(True)
fAddLen = 1.1 * rgBbox_Face.Min.DistanceTo(rgBbox_Face.Max)
rgCyl.Height1 = -fAddLen
rgCyl.Height2 = fAddLen
# for sAttr in dir(rgCyl):
# if (sAttr != 'Unset' and sAttr != '__doc__' and
# not callable(getattr(rgCyl, sAttr))):
# sPrint = 'rgCyl.' + sAttr; print sPrint + ':', eval(sPrint)
# sPrint = 'dir(rgCyl)'; print sPrint + ':', eval(sPrint)
#sPrint = 'rgCyl.Unset'; print sPrint + ':', eval(sPrint)
# for sAttr in dir(rgCyl):
# if (sAttr != 'Unset' and sAttr != '__doc__' and
# not callable(getattr(rgCyl, sAttr))):
# sPrint = 'rgShape1.' + sAttr; print sPrint + ':', eval(sPrint)
elif rgCyl.TotalHeight < 2.0*sc.doc.ModelAbsoluteTolerance:
if bDebug:
print "Resultant height of cylinder is {}.".format(
rgCyl.TotalHeight)
print "Using 100 times model's absolute tolerance."
rgCyl.Height1 = -100.0*sc.doc.ModelAbsoluteTolerance
rgCyl.Height2 = 100.0*sc.doc.ModelAbsoluteTolerance
else:
# Increase length of cylinder so that surface is always larger than trim.
rgBbox_Face = rgFace0.GetBoundingBox(False)
rgLines = rgBbox_Face.GetEdges()
fMaxLen = 0
for rgLine in rgLines:
if rgLine.Length > fMaxLen: fMaxLen = rgLine.Length
fAddLen = 1.1 * fMaxLen
#fAddLen = 1.1 * rgBbox_Face.Min.DistanceTo(rgBbox_Face.Max)
rgCyl.Height1 = -fAddLen
rgCyl.Height2 = fAddLen
rgSrf_Converted = rgCyl.ToRevSurface()
if rgSrf_Converted is not None:
return (rgSrf_Converted, fTol_PrimitiveMatch, bFoundUsingShunkFace), None
else:
return None, "RevSurface could not be obtained from Cylinder."
elif typeShape1 == rg.Cone:
rgCone = rgShape1
sType = "Cone"
rgSrf_Converted = rgCone.ToRevSurface()
if rgSrf_Converted is not None:
return (rgSrf_Converted, fTol_PrimitiveMatch, bFoundUsingShunkFace), None
else:
return None, "RevSurface could not be obtained from Cone."
# Increase length of cone so that surface is always larger than trim.
xf = rg.Transform.Scale(rgCone.ApexPoint, 1.1)
rgSrf_Converted.Transform(xf)
elif typeShape1 == rg.Sphere:
rgSphere = rgShape1
sType = "Sphere"
rgSrf_Converted = rgSphere.ToRevSurface()
if rgSrf_Converted is not None:
return (rgSrf_Converted, fTol_PrimitiveMatch, bFoundUsingShunkFace), None
else:
return None, "RevSurface could not be obtained from Sphere."
elif typeShape1 == rg.Torus:
rgTorus = rgShape1
sType = "Torus"
rgSrf_Converted = rgTorus.ToRevSurface()
if rgSrf_Converted is not None:
return (rgSrf_Converted, fTol_PrimitiveMatch, bFoundUsingShunkFace), None
else:
return None, "RevSurface could not be obtained from Torus."
return None, "What happened?"
s = " {} found".format(sType)
s += " matching {} underlying surface".format(
"shrunk" if bFoundUsingShunkFace else "full")
s += " at a tolerance of {}.".format(fTol_PrimitiveMatch)
return (rgSrf_Converted, fTol_PrimitiveMatch, bFoundUsingShunkFace), s
def tryGetPrimitiveShape(rgFace0, bMatchToShrunkFace=True, bPlane=True, bCylinder=True, bCone=True, bSphere=True, bTorus=True, fTolerance=1e-9, bDebug=False):
"""
Updated in May of 2019. Based on hasPrimitiveShape.
Returns:
On Success (regardless if primitive is found):
tuple:
(
tuple: (rg.Plane or rg.Cylinder or rg.Cone or rg.Sphere or rg.Torus,
float of tolerance used to find shape,
'shrunk' or 'not shrunk')
,
str: (Fail log)
)
On fail: None, None
"""
rgSrf_Face0 = rgFace0.UnderlyingSurface()
if bPlane:
rc = tryGetPlane(
rgFace0,
bMatchToShrunkFace=bMatchToShrunkFace,
fTolerance=fTolerance,
bDebug=bDebug)
if rc[0] is not None:
return rc
if bCylinder or bCone or bSphere or bTorus:
rc = tryGetRoundPrimitive(
rgFace0=rgFace0,
bMatchToShrunkFace=bMatchToShrunkFace,
bCylinder=bCylinder,
bCone=bCone,
bSphere=bSphere,
bTorus=bTorus,
fTolerance=fTolerance,
bDebug=bDebug)
if rc[0] is not None:
return rc
return None, None