forked from CADacombs/rhinopython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxBrep_selectOthersByAttribute.py
More file actions
420 lines (321 loc) · 13.2 KB
/
xBrep_selectOthersByAttribute.py
File metadata and controls
420 lines (321 loc) · 13.2 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
"""
201030: Created, starting with xBreps_similar.py.
201102: Added support for Extrusions. Now considers Attributes.ColorSource. Added feedback for when analyzing large data sets.
201105: Improved efficency of volume checking and open breps are now skipped for volume matching.
"""
import Rhino
import Rhino.DocObjects as rd
import Rhino.Geometry as rg
import Rhino.Input as ri
import scriptcontext as sc
sAttrs = [
'Colour',
'Name',
'Layer',
'Volume',
]
class Opts:
keys = []
values = {}
names = {}
riOpts = {}
listValues = {}
stickyKeys = {}
key = 'fFloatTol'; keys.append(key)
values[key] = sc.doc.ModelAbsoluteTolerance
riOpts[key] = ri.Custom.OptionDouble(initialValue=values[key], setLowerLimit=True, limit=sc.doc.ModelAbsoluteTolerance)
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bEcho'; keys.append(key)
values[key] = True
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bDebug'; keys.append(key)
values[key] = False
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
stickyKeys[key] = '{}({})'.format(key, __file__)
for key in keys:
if key not in names:
names[key] = key[1:]
# Load sticky.
for key in stickyKeys:
if stickyKeys[key] in sc.sticky:
if key in riOpts:
riOpts[key].CurrentValue = values[key] = sc.sticky[stickyKeys[key]]
else:
# For OptionList.
values[key] = sc.sticky[stickyKeys[key]]
@classmethod
def addOption(cls, go, key):
idxOpt = None
if key in cls.riOpts:
if key[0] == 'b':
idxOpt = go.AddOptionToggle(
cls.names[key], cls.riOpts[key])[0]
elif key[0] == 'f':
idxOpt = go.AddOptionDouble(
cls.names[key], cls.riOpts[key])[0]
elif key[0] == 'i':
idxOpt = go.AddOptionInteger(
englishName=cls.names[key], intValue=cls.riOpts[key])[0]
else:
idxOpt = go.AddOptionList(
englishOptionName=cls.names[key],
listValues=cls.listValues[key],
listCurrentIndex=cls.values[key])
return idxOpt
@classmethod
def setValues(cls):
for key in cls.riOpts:
cls.values[key] = cls.riOpts[key].CurrentValue
@classmethod
def saveSticky(cls):
for key in cls.stickyKeys:
if key in cls.riOpts:
sc.sticky[cls.stickyKeys[key]] = cls.riOpts[key].CurrentValue
else:
# For OptionList.
sc.sticky[cls.stickyKeys[key]] = cls.values[key]
def getInput():
"""
"""
# Get Brep/Extrusion with optional input.
go = ri.Custom.GetObject()
go.SetCommandPrompt("Select source brep/extrusion")
go.GeometryFilter = rd.ObjectType.Brep # Brep will also filter Extrusions ( https://discourse.mcneel.com/t/restriction-of-objecttype-in-rhinocommon/73603/5 )
go.SubObjectSelect = False
idxs_Opt = {}
while True:
key = 'fFloatTol'; idxs_Opt[key] = Opts.addOption(go, key)
key = 'bEcho'; idxs_Opt[key] = Opts.addOption(go, key)
key = 'bDebug'; idxs_Opt[key] = Opts.addOption(go, key)
res = go.Get()
if res == ri.GetResult.Object:
objref = go.Object(0)
go.Dispose()
go = ri.Custom.GetOption()
go.SetCommandPrompt("Select matching attribute")
idxs_Opt = {}
while True:
for key in sAttrs:
idxs_Opt[key] = go.AddOption(key)
key = 'fFloatTol'; idxs_Opt[key] = Opts.addOption(go, key)
key = 'bEcho'; idxs_Opt[key] = Opts.addOption(go, key)
key = 'bDebug'; idxs_Opt[key] = Opts.addOption(go, key)
res = go.Get()
if res == ri.GetResult.Object:
objref = go.Object(0)
go.Dispose()
return tuple([objref] + [Opts.values[key] for key in Opts.keys])
elif res == ri.GetResult.Cancel:
go.Dispose()
return
else:
# An option was selected or a number was entered.
for key in sAttrs:
if go.OptionIndex() == idxs_Opt[key]:
return tuple([objref] + [key] + [Opts.values[key] for key in Opts.keys])
Opts.setValues()
Opts.saveSticky()
go.ClearCommandOptions()
elif res == ri.GetResult.Cancel:
go.Dispose()
return
else:
# An option was selected.
pass
Opts.saveSticky()
def getMatches(objref_toMatch, **kwargs):
"""
"""
def setOpt(key, value=None):
if key in kwargs:
return kwargs[key]
elif key in Opts.riOpts:
return Opts.riOpts[key].InitialValue
else:
return value
sAttr = setOpt('sAttr')
fFloatTol = setOpt('fFloatTol')
bEcho = setOpt('bEcho')
bDebug = setOpt('bDebug')
objref0 = objref_toMatch
bToMatch_IsExtrusion = bool(objref0.Surface())
if sAttr == "Colour":
rdRhObj0 = objref0.Object()
if rdRhObj0.Attributes.ColorSource == rd.ObjectColorSource.ColorFromMaterial:
print "Colour from material not supported yet."
return
elif rdRhObj0.Attributes.ColorSource == rd.ObjectColorSource.ColorFromObject:
colorRhObj0 = rdRhObj0.Attributes.ObjectColor
else:
if rdRhObj0.Attributes.ColorSource == rd.ObjectColorSource.ColorFromLayer:
pass
else:
# Color from parent.
# Use layer color unless object is within a block definition.
pass
li = rdRhObj0.Attributes.LayerIndex
layer = sc.doc.Layers.FindIndex(li)
colorRhObj0 = layer.Color
elif sAttr == 'Volume':
rgBrep0 = objref0.Brep() # Brep is also returned when objref0 contains an Extrusion.
if not rgBrep0.IsValid:
print "Reference {} {} is invalid. Repair it then rerun this script.".format(
"Extrusion" if bToMatch_IsExtrusion else "Brep",
objref0.ObjectId)
rgBrep0.Dispose()
return
if not rgBrep0.IsSolid:
print "Reference {} {} is open. Its 'Volume' will not be matched.".format(
"Extrusion" if bToMatch_IsExtrusion else "Brep",
objref0.ObjectId)
rgBrep0.Dispose()
return
fVol0 = rgBrep0.GetVolume()
if not fVol0:
print "Volume can not be calculated. Repair {} {}.".format(
"Extrusion" if bToMatch_IsExtrusion else "Brep",
objref0.ObjectId)
rgBrep0.Dispose()
return
iToMatch_FaceCt = rgBrep0.Faces.Count
rgBrep0.Dispose()
gBreps_ToSearch = []
rgBreps_ToSearch = []
iCts_Faces_ToSearch = []
fEdgeLens_ToSearch = []
iter = rd.ObjectEnumeratorSettings()
iter.NormalObjects = True
iter.LockedObjects = False
iter.IncludeLights = False
iter.IncludeGrips = False
iter.ObjectTypeFilter = rd.ObjectType.Brep | rd.ObjectType.Extrusion
gBreps_MatchesFound = [] # Also can include Extrusions.
iBrepExtrCt = 0
for rdObj in sc.doc.Objects.GetObjectList(iter):
iBrepExtrCt += 1
iOpenBrepCt = 0
idxs_AtTenths = [int(round(0.1*i*iBrepExtrCt,0)) for i in range(10)]
for i, rdRhObjX in enumerate(sc.doc.Objects.GetObjectList(iter)):
if sc.escape_test(throw_exception=False):
print "*** Analysis interrupted by user." \
" Selected breps/extrusions are of partial results."
return gBreps_MatchesFound
if rdRhObjX.Id == objref0.ObjectId: continue
if iBrepExtrCt > 10:
if i in idxs_AtTenths:
Rhino.RhinoApp.SetCommandPrompt("Analysis at {:d}% of {} breps/extrusions ...".format(
int(100.0 * (i+1) / iBrepExtrCt), iBrepExtrCt))
elif iBrepExtrCt > 1:
Rhino.RhinoApp.SetCommandPrompt(
"Analysis at {} of {} breps/extrusions".format(
i+1, iBrepExtrCt))
else:
Rhino.RhinoApp.SetCommandPrompt("Analyzing other brep/extrusion ...")
if sAttr == 'Colour':
if rdRhObjX.Attributes.ColorSource == rd.ObjectColorSource.ColorFromObject:
colorRhObjX = rdRhObjX.Attributes.ObjectColor
elif rdRhObjX.Attributes.ColorSource == rd.ObjectColorSource.ColorFromMaterial:
print "Colour from material not supported yet."
return
else:
if rdRhObjX.Attributes.ColorSource == rd.ObjectColorSource.ColorFromLayer:
pass
else:
# Color from parent.
# Use layer color unless object is within a block definition.
pass
li = rdRhObjX.Attributes.LayerIndex
layer = sc.doc.Layers.FindIndex(li)
colorRhObjX = layer.Color
if colorRhObjX == colorRhObj0:
gBreps_MatchesFound.append(rdRhObjX.Id)
elif sAttr == 'Name':
if rdRhObjX.Attributes.Name == objref0.Object().Attributes.Name:
gBreps_MatchesFound.append(rdRhObjX.Id)
elif sAttr == 'Layer':
if rdRhObjX.Attributes.LayerIndex == objref0.Object().Attributes.LayerIndex:
gBreps_MatchesFound.append(rdRhObjX.Id)
elif sAttr == 'Volume':
bToCheck_IsExtrusion = isinstance(rdRhObjX, rd.ExtrusionObject)
rgGeomX = rdRhObjX.Geometry
if not rgGeomX.IsValid:
print "{} {} is invalid. Fix first.".format(
rdRhObjX.GetType().Name,
rdRhObjX.Id)
rgGeomX.Dispose()
continue
if bToCheck_IsExtrusion:
rgBrepX = rgGeomX.ToBrep(splitKinkyFaces=True)
rgGeomX.Dispose()
else:
rgBrepX = rgGeomX
if not rgBrepX.IsSolid:
iOpenBrepCt += 1
rgBrepX.Dispose()
continue
## This significantly speeds up the analysis.
#if rdRhObjX.ObjectType == rd.ObjectType.Brep:
# if rgBrepX.Faces.Count != iToMatch_FaceCt:
# rgBrepX.Dispose()
# continue
fVol = rgBrepX.GetVolume() # GetVolume may be faster than VolumeMassProperties.Compute.
if not fVol:
print "Volume can not be calculated. Repair {} {}.".format(
rdRhObjX.GetType().Name,
rdRhObjX.Id)
rgBrepX.Dispose()
continue
rgBrepX.Dispose()
fVolDiff = abs(fVol - fVol0)
if bDebug: print "Volume:", fVol0, fVolDiff
if fVolDiff > fFloatTol:
continue
gBreps_MatchesFound.append(rdRhObjX.Id)
if sAttr == 'Volume':
if iOpenBrepCt:
print "{} open breps skipped for volume matching.".format(iOpenBrepCt)
else:
print "No open breps are present."
return gBreps_MatchesFound
def main():
"""
"""
rc = getInput()
if rc is None: return
(
objref_toMatch,
sAttr,
fFloatTol,
bEcho,
bDebug,
) = rc
if bDebug:
pass
#reload()
else:
sc.doc.Views.RedrawEnabled = False
#print list(sc.doc.Objects.GetSelectedObjects(includeLights=False, includeGrips=False))
sc.doc.Objects.UnselectAll()
rc = getMatches(
objref_toMatch=objref_toMatch,
sAttr=sAttr,
fFloatTol=fFloatTol,
bEcho=bEcho,
bDebug=bDebug,
)
if not rc:
print "No matches found. No objects are selected."
return
gBreps_MatchesFound = rc
sc.doc.Objects.Select(objectIds=[objref_toMatch.ObjectId] + gBreps_MatchesFound)
rdObjs_Sel = list(sc.doc.Objects.GetSelectedObjects(includeLights=False, includeGrips=False))
if rdObjs_Sel:
print "{} [{}] breps / [{}] extrusions are selected.".format(
len(rdObjs_Sel),
len([rdObj for rdObj in rdObjs_Sel if rdObj.ObjectType == rd.ObjectType.Brep]),
len([rdObj for rdObj in rdObjs_Sel if rdObj.ObjectType == rd.ObjectType.Extrusion]))
else:
print "No objects are selected."
sc.doc.Views.RedrawEnabled = True
if __name__ == '__main__': main()