-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathspb_Brep_contiguousCoshapedFaces.py
More file actions
437 lines (350 loc) · 14.1 KB
/
spb_Brep_contiguousCoshapedFaces.py
File metadata and controls
437 lines (350 loc) · 14.1 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
"""
190523-24: Created.
190721: Removed an unused import.
190722: Import-related change.
191004: Bug fix.
191101: Import-related update.
191117: Import-related bug fix.
191118: Import-related update.
241224: Bug fix.
"""
import Rhino
import Rhino.Geometry as rg
import Rhino.Input as ri
import rhinoscriptsyntax as rs
import scriptcontext as sc
import xBrepObject
import xPrimitiveShape
sOpts = (
'bPrimitives',
'bNurbsSrfs',
'fTolerance',
'bEcho',
'bDebug',
)
class Opts():
keys = []
values = {}
names = {}
riOpts = {}
stickyKeys = {}
for key in sOpts:
keys.append(key)
names[key] = key[1:] # Overwrite as wanted in the following.
key = 'bPrimitives'
values[key] = True
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bNurbsSrfs'
values[key] = True
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'fTolerance'
values[key] = 0.1 * sc.doc.ModelAbsoluteTolerance
riOpts[key] = ri.Custom.OptionDouble(values[key], setLowerLimit=True, limit=0.0)
stickyKeys[key] = '{}({})({})'.format(key, __file__, sc.doc.Name)
key = 'bEcho'
values[key] = True
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bDebug'
values[key] = False
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
stickyKeys[key] = '{}({})'.format(key, __file__)
# Load sticky.
for key in stickyKeys:
if sc.sticky.has_key(stickyKeys[key]):
if riOpts[key]:
values[key] = riOpts[key].CurrentValue = sc.sticky[stickyKeys[key]]
else:
# For OptionList.
values[key] = sc.sticky[stickyKeys[key]]
@classmethod
def setValues(cls):
for key in cls.stickyKeys:
if cls.riOpts[key]:
cls.values[key] = cls.riOpts[key].CurrentValue
else:
# For OptionList.
pass
@classmethod
def saveSticky(cls):
for key in cls.stickyKeys:
if cls.riOpts[key]:
sc.sticky[cls.stickyKeys[key]] = cls.riOpts[key].CurrentValue
else:
# For OptionList.
sc.sticky[cls.stickyKeys[key]] = cls.values[key]
def getInput():
"""Get breps with optional input"""
go = ri.Custom.GetObject()
go.SetCommandPrompt("Select starting face for match")
go.GeometryFilter = Rhino.DocObjects.ObjectType.Surface
go.GeometryAttributeFilter = ri.Custom.GeometryAttributeFilter.SubSurface
go.EnableHighlight(False)
go.DisablePreSelect()
go.AcceptNumber(enable=True, acceptZero=True)
print "Base shape will be obtained from first face picked."
while True:
go.AddOptionToggle(Opts.names['bPrimitives'], Opts.riOpts['bPrimitives'])
go.AddOptionToggle(Opts.names['bNurbsSrfs'], Opts.riOpts['bNurbsSrfs'])
go.AddOptionDouble(Opts.names['fTolerance'], Opts.riOpts['fTolerance'])
go.AddOptionToggle(Opts.names['bEcho'], Opts.riOpts['bEcho'])
go.AddOptionToggle(Opts.names['bDebug'], Opts.riOpts['bDebug'])
res = go.Get()
if res == ri.GetResult.Object:
break
elif res == ri.GetResult.Cancel:
return
key = 'fTolerance'
if res == ri.GetResult.Number:
Opts.riOpts[key].CurrentValue = abs(go.Number())
if Opts.riOpts[key].CurrentValue == 0.0:
Opts.riOpts[key].CurrentValue = Opts.riOpts[key].InitialValue
Opts.setValues()
Opts.saveSticky()
go.ClearCommandOptions()
if sc.doc.Objects.UnselectAll() > 0: sc.doc.Views.Redraw() # Allows for face highlighting.
rdObjRef = go.Object(0)
gBrep = rdObjRef.ObjectId
rgBrep = rdObjRef.Brep()
idx_rgFaceA = rdObjRef.GeometryComponentIndex.Index
idxFaces_AdjA = rgBrep.Faces[idx_rgFaceA].AdjacentFaces()
#idxFaces_AdjA = None
return (
gBrep,
idx_rgFaceA,
Opts.values['bPrimitives'],
Opts.values['bNurbsSrfs'],
Opts.values['fTolerance'],
Opts.values['bEcho'],
Opts.values['bDebug'],
)
def getNurbsSrfFaces(rgFaceA, rgNurbsSurfaceA=None, idx_rgFaces_Filter=None, fTolerance=1e-9, bDebug=False):
if rgNurbsSurfaceA is None:
rgSrfA = rgFaceA.UnderlyingSurface()
if not isinstance(rgSrfA, rg.NurbsSurface):
rgSrfA.Dispose()
return
nsA = rgSrfA
else:
nsA = rgNurbsSurfaceA
idx_rgFaceA = rgFaceA.FaceIndex
# Find matching shapes of adjacent faces.
idxFaces_Pass = [idx_rgFaceA]
idxFaces_Fail = []
idxFaces_LastAdded = [idx_rgFaceA]
rgBrep0 = rgFaceA.Brep
for idxFace_LastAdded in idxFaces_LastAdded:
sc.escape_test()
for idx_Face_Adj in rgBrep0.Faces[idxFace_LastAdded].AdjacentFaces():
if idx_rgFaces_Filter and idx_Face_Adj not in idx_rgFaces_Filter:
continue
elif idx_Face_Adj in idxFaces_Pass:
continue
elif idx_Face_Adj in idxFaces_Fail:
continue
rgFaceB = rgBrep0.Faces[idx_Face_Adj]
srfB = rgFaceB.UnderlyingSurface()
if not isinstance(srfB, rg.NurbsSurface): continue
nsB = srfB
if not nsA.EpsilonEquals(other=nsB, epsilon=fTolerance):
idxFaces_Fail.append(idx_Face_Adj)
continue
# Main parameters of NurbsSurface are equal, so now check all ControlPoints.
if nsA.Points.CountU != nsB.Points.CountU:
idxFaces_Fail.append(idx_Face_Adj)
continue
if nsA.Points.CountV != nsB.Points.CountV:
idxFaces_Fail.append(idx_Face_Adj)
continue
for ptA, ptB in zip(nsA.Points, nsB.Points):
if not ptA.EpsilonEquals(other=ptB, epsilon=fTolerance):
idxFaces_Fail.append(idx_Face_Adj)
break
else:
if bDebug: print "NurbsSurfaces are EpsilonEqual."
idxFaces_Pass.append(idx_Face_Adj)
idxFaces_LastAdded.append(idx_Face_Adj)
return idxFaces_Pass
def getPrimitiveShapedFaces(rgFaceA, rgPrimitiveShapeA=None, idx_rgFaces_Filter=None, fTolerance=1e-9, bDebug=False):
rgBrep0 = rgFaceA.Brep
idx_rgFaceA = rgFaceA.FaceIndex
if rgPrimitiveShapeA is None:
rc = xPrimitiveShape.BrepFace.tryGetPrimitiveShape(
rgFace0=rgFaceA,
fTolerance=fTolerance,
bDebug=bDebug)
if rc[0] is None: return
rgPrimitiveShapeA = rc[0][0]
bPlane = bCylinder = bCone = bSphere = bTorus = False
if isinstance(rgPrimitiveShapeA, rg.Plane):
bPlane = True
elif isinstance(rgPrimitiveShapeA, rg.Cylinder):
bCylinder = True
elif isinstance(rgPrimitiveShapeA, rg.Cone):
bCone = True
elif isinstance(rgPrimitiveShapeA, rg.Sphere):
bSphere = True
elif isinstance(rgPrimitiveShapeA, rg.Torus):
bTorus = True
idxFaces_Pass = [idx_rgFaceA]
# Find matching shapes of adjacent faces.
idxFaces_Pass = [idx_rgFaceA]
idxFaces_Fail = []
idxFaces_LastAdded = [idx_rgFaceA]
for idxFace_LastAdded in idxFaces_LastAdded:
sc.escape_test()
for idx_Face_Adj in rgBrep0.Faces[idxFace_LastAdded].AdjacentFaces():
if idx_rgFaces_Filter and idx_Face_Adj not in idx_rgFaces_Filter:
continue
elif idx_Face_Adj in idxFaces_Pass:
continue
elif idx_Face_Adj in idxFaces_Fail:
continue
rgFaceB = rgBrep0.Faces[idx_Face_Adj]
rc = xPrimitiveShape.BrepFace.tryGetPrimitiveShape(
rgFace0=rgFaceB,
bPlane=bPlane,
bCylinder=bCylinder,
bCone=bCone,
bSphere=bSphere,
bTorus=bTorus,
fTolerance=fTolerance,
bDebug=bDebug)
if rc[0] is None:
idxFaces_Fail.append(idx_Face_Adj)
continue
shapeB = rc[0][0]
if xPrimitiveShape.AnyShape.areEqual(
[rgPrimitiveShapeA, shapeB],
epsilon=fTolerance,
bDebug=bDebug
):
idxFaces_Pass.append(idx_Face_Adj)
idxFaces_LastAdded.append(idx_Face_Adj)
else:
idxFaces_Fail.append(idx_Face_Adj)
return idxFaces_Pass
def getFaces(rgBrep0, idx_rgFaceA, idx_rgFaces_Filter=None, bMergeAll=True, bPrimitives=None, bNurbsSrfs=None, fTolerance=None, bEcho=None, bDebug=None):
"""
"""
if bPrimitives is None: bPrimitives = Opts.values['bPrimitives']
if bNurbsSrfs is None: bNurbsSrfs = Opts.values['bNurbsSrfs']
if fTolerance is None: fTolerance = Opts.values['fTolerance']
if bEcho is None: bEcho = Opts.values['bEcho']
if bDebug is None: bDebug = Opts.values['bDebug']
# Get shape of FaceA.
rgShapeA = None
rgFaceA = rgBrep0.Faces[idx_rgFaceA]
if bPrimitives:
rc = xPrimitiveShape.BrepFace.tryGetPrimitiveShape(
rgFace0=rgFaceA,
fTolerance=fTolerance,
bDebug=bDebug)
if rc is not None and rc[0] is not None:
rgShapeA, fTol_Used, sShrunkOrNot = rc[0]
idx_rgFs_B0_Pass = getPrimitiveShapedFaces(
rgFaceA=rgFaceA,
rgPrimitiveShapeA=rgShapeA,
idx_rgFaces_Filter=idx_rgFaces_Filter,
fTolerance=fTolerance,
bDebug=bDebug)
if bNurbsSrfs and rgShapeA is None:
rgShapeA = rgFaceA.UnderlyingSurface()
if not isinstance(rgShapeA, rg.NurbsSurface):
rgShapeA.Dispose()
return
idx_rgFs_B0_Pass = getNurbsSrfFaces(
rgFaceA=rgFaceA,
rgNurbsSurfaceA=rgShapeA,
idx_rgFaces_Filter=idx_rgFaces_Filter,
fTolerance=fTolerance,
bDebug=bDebug)
if bDebug: sEval='idx_rgFs_B0_Pass'; print sEval+':',eval(sEval)
if len(idx_rgFs_B0_Pass) < 2:
if bEcho: print "No matching contiguous faces found for starting face."
return
return idx_rgFs_B0_Pass, rgShapeA
def processBrepObject(gBrep0, idx_rgFaceA, idx_rgFaces_Filter=None, bPrimitives=None, bNurbsSrfs=None, fTolerance=None, bCopy=None, bEcho=None, bDebug=None):
"""
"""
if bPrimitives is None: bPrimitives = Opts.values['bPrimitives']
if bNurbsSrfs is None: bNurbsSrfs = Opts.values['bNurbsSrfs']
if fTolerance is None: fTolerance = Opts.values['fTolerance']
if bEcho is None: bEcho = Opts.values['bEcho']
if bDebug is None: bDebug = Opts.values['bDebug']
rdBrep0 = sc.doc.Objects.FindId(gBrep0) if Rhino.RhinoApp.ExeVersion >= 6 else sc.doc.Objects.Find(gBrep0)
rgBrep0 = rdBrep0.Geometry
rc = getFaces(
rgBrep0=rgBrep0,
bPrimitives=bPrimitives,
bNurbsSrfs=bNurbsSrfs,
idx_rgFaceA=idx_rgFaceA,
idx_rgFaces_Filter=idx_rgFaces_Filter,
bMergeAll=True,
fTolerance=fTolerance,
bEcho=bEcho,
bDebug=bDebug,
)
if rc is None: return
idx_rgFs_B0_Pass, rgShapeA = rc
if rgBrep0.Faces.Count == len(idx_rgFs_B0_Pass):
print "Entire brep matches the face."
bSelected = rs.SelectObject(gBrep0)
rgBrep0.Dispose()
return bSelected
else:
if Rhino.RhinoApp.ExeVersion >= 6:
xBrepObject.selectFaces(rdBrep0, idx_rgFs_B0_Pass)
rgBrep0.Dispose()
else:
# Rhino V5.
rc = xBrepObject.extractFaces(
gBrep0,
idxFaces=idx_rgFs_B0_Pass,
bCurrentLayer=False,
bByLayerColor=False,
bAddOnlyMonofaces=True,
bEcho=bEcho,
bDebug=bDebug)
rgBrep0.Dispose()
return None if rc is None else rc[0]
def main():
rc = getInput()
if rc is None: return
(
gBrep0,
idx_rgFaceA,
bPrimitives,
bNurbsSrfs,
fTolerance,
bEcho,
bDebug,
) = rc
if bDebug:
import sys
for sModule in list(sys.modules):
if sModule[0] == 'x':
try:
reload(sys.modules[sModule])
print "{} reloaded.".format(sys.modules[sModule])
except:
# This is for any module name changes.
print "{} NOT reloaded.".format(sys.modules[sModule])
else:
sc.doc.Views.RedrawEnabled = False
if bEcho: print "Maximum conversion tolerance allowed: {}".format(fTolerance)
Rhino.RhinoApp.SetCommandPrompt(prompt="Working ...")
processBrepObject(
gBrep0=gBrep0,
bPrimitives=bPrimitives,
bNurbsSrfs=bNurbsSrfs,
idx_rgFaceA=idx_rgFaceA,
idx_rgFaces_Filter=None,
fTolerance=fTolerance,
bEcho=bEcho,
bDebug=bDebug,
)
sc.doc.Views.RedrawEnabled = True
if __name__ == '__main__': main()