-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathspb_Brep_extractSelectedFaces.py
More file actions
381 lines (307 loc) · 11.8 KB
/
spb_Brep_extractSelectedFaces.py
File metadata and controls
381 lines (307 loc) · 11.8 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
"""
180612: Created, starting with extractFilletFacesByRadius.py.
181109: Added Opts. Added bRetainColor option.
181127: Enabled bCopy functionality.
181202, 190325: Updated an import name.
190330: Fixed bug concerning single extracted face not being selected.
190429: Added function to handle preselected faces.
191010: Import-related update.
191022: Feedback change.
191101: Import-related update.
250604: Modified a printed feedback.
"""
import Rhino
import Rhino.DocObjects as rd
import Rhino.Geometry as rg
import Rhino.Input as ri
import scriptcontext as sc
from System import Guid
from System.Collections.Generic import List
import xBrepObject
sOpts = (
'bAddOnlyMonofaces',
'bRetainLayer',
'bRetainColor',
'bCopy',
'bEcho',
'bDebug',
)
class Opts:
keys = []
values = {}
names = {}
riOpts = {}
stickyKeys = {}
@classmethod
def init(cls):
for key in sOpts:
cls.keys.append(key)
cls.names[key] = key[1:] # Overwrite as wanted in the following.
key = 'bAddOnlyMonofaces'
cls.keys.append(key)
cls.values[key] = False
cls.names[key] = key[1:]
cls.riOpts[key] = ri.Custom.OptionToggle(cls.values[key], 'No', 'Yes')
cls.stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bRetainLayer'
cls.keys.append(key)
cls.values[key] = True
cls.names[key] = key[1:]
cls.riOpts[key] = ri.Custom.OptionToggle(cls.values[key], 'No', 'Yes')
cls.stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bRetainColor'
cls.keys.append(key)
cls.values[key] = True
cls.names[key] = key[1:]
cls.riOpts[key] = ri.Custom.OptionToggle(cls.values[key], 'No', 'Yes')
cls.stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bCopy'
cls.keys.append(key)
cls.values[key] = False
cls.names[key] = 'Copy'
cls.riOpts[key] = ri.Custom.OptionToggle(cls.values[key], 'No', 'Yes')
cls.stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bEcho'
cls.keys.append(key)
cls.values[key] = True
cls.names[key] = 'Echo'
cls.riOpts[key] = ri.Custom.OptionToggle(cls.values[key], 'No', 'Yes')
cls.stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bDebug'
cls.keys.append(key)
cls.values[key] = False
cls.names[key] = 'Debug'
cls.riOpts[key] = ri.Custom.OptionToggle(cls.values[key], 'No', 'Yes')
cls.stickyKeys[key] = '{}({})'.format(key, __file__)
# Load sticky.
for key in cls.stickyKeys:
if sc.sticky.has_key(cls.stickyKeys[key]):
if cls.riOpts[key]:
cls.values[key] = cls.riOpts[key].CurrentValue = sc.sticky[cls.stickyKeys[key]]
else:
# For OptionList.
cls.values[key] = sc.sticky[cls.stickyKeys[key]]
@classmethod
def addOptions(cls, go, sInclude=None):
if sInclude is None:
sInclude = cls.keys[:]
if not sInclude: return
def addOptionDouble(key, lower=None, upper=None):
if key in sInclude:
go.AddOptionDouble(cls.names[key], cls.riOpts[key])
def addOptionInteger(key):
if key in sInclude:
go.AddOptionInteger(cls.names[key], cls.riOpts[key])
def addOptionToggle(key):
if key in sInclude:
go.AddOptionToggle(cls.names[key], cls.riOpts[key])
addOptionToggle('bAddOnlyMonofaces')
addOptionToggle('bRetainLayer')
addOptionToggle('bRetainColor')
addOptionToggle('bCopy')
addOptionToggle('bEcho')
addOptionToggle('bDebug')
@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]
@classmethod
def processInput(cls, go):
res = go.Result()
cls.setValues()
cls.saveSticky()
# Clear and add options regardless if a number was entered or options were modified in another way.
go.ClearCommandOptions()
Opts.addOptions(go)
Opts.init()
def getPreselectedFaceInput():
"""
"""
go = ri.Custom.GetObject()
go.GeometryFilter = Rhino.DocObjects.ObjectType.Surface
go.EnablePreSelect(enable=True, ignoreUnacceptablePreselectedObjects=False)
go.EnablePostSelect(enable=False)
res = go.GetMultiple(1,0)
if res != ri.GetResult.Object: return
gBreps = []
idx_Faces_AllBreps = []
# Prepare lists of brep GUIDs and face indices.
for objref in go.Objects():
gBrep = objref.ObjectId
if gBrep in gBreps:
idx_Faces_AllBreps[gBreps.index(gBrep)].append(objref.Face().FaceIndex)
else:
gBreps.append(gBrep)
idx_Faces_AllBreps.append([objref.Face().FaceIndex])
rdBrep = sc.doc.Objects.Find(gBrep)
sc.doc.Objects.UnselectAll()
gopt = ri.Custom.GetOption()
gopt.SetCommandPrompt("Set options for preselected faces")
gopt.AcceptNothing(enable=True)
Opts.addOptions(gopt)
while True:
res = gopt.Get()
if res == ri.GetResult.Nothing:
break
elif res == ri.GetResult.Cancel:
return
else:
# An option was selected or a number was entered.
Opts.processInput(gopt)
return (
gBreps,
idx_Faces_AllBreps,
Opts.values['bAddOnlyMonofaces'],
Opts.values['bRetainLayer'],
Opts.values['bRetainColor'],
Opts.values['bCopy'],
Opts.values['bEcho'],
Opts.values['bDebug'])
def getInput():
"""
"""
# Get breps with optional input
go = ri.Custom.GetObject()
go.SetCommandPrompt("Select faces to extract")
go.GeometryFilter = rd.ObjectType.Surface
go.DeselectAllBeforePostSelect = False # So objects won't be deselected on repeats of While loop.
go.EnableClearObjectsOnEntry(False) # Keep objects in go on repeats of While loop.
#go.EnableUnselectObjectsOnExit(False)
#sc.doc.Objects.UnselectAll()
sc.doc.Views.Redraw()
Opts.addOptions(go)
while True:
res = go.GetMultiple(minimumNumber=1, maximumNumber=0)
if res == ri.GetResult.Object:
break
elif res == ri.GetResult.Cancel:
return
else:
# An option was selected or a number was entered.
Opts.processInput(go)
# Rehighlight faces already selected.
for objref in go.Objects():
objref.Object().HighlightSubObject(
componentIndex=objref.GeometryComponentIndex,
highlight=True)
sc.doc.Views.Redraw()
gBreps = []
idx_Faces_AllBreps = []
# Prepare lists of brep GUIDs and face indices.
for objref in go.Objects():
gBrep = objref.ObjectId
if gBrep in gBreps:
idx_Faces_AllBreps[gBreps.index(gBrep)].append(objref.Face().FaceIndex)
else:
gBreps.append(gBrep)
idx_Faces_AllBreps.append([objref.Face().FaceIndex])
rdBrep = sc.doc.Objects.Find(gBrep)
#rdBrep.Highlight(enable=False)
# Unselect any non-surface preselections.
sc.doc.Objects.UnselectAll()
sc.doc.Views.Redraw()
return (
gBreps,
idx_Faces_AllBreps,
Opts.values['bAddOnlyMonofaces'],
Opts.values['bRetainLayer'],
Opts.values['bRetainColor'],
Opts.values['bCopy'],
Opts.values['bEcho'],
Opts.values['bDebug'])
def processBrepObjects(gBreps0, idx_Faces_AllBreps, bAddOnlyMonofaces=None, bRetainLayer=None, bRetainColor=None, bCopy=None, bEcho=None, bDebug=None):
"""
"""
if bAddOnlyMonofaces is None: bAddOnlyMonofaces = Opts.values['bAddOnlyMonofaces']
if bRetainLayer is None: bRetainLayer = Opts.values['bRetainLayer']
if bRetainColor is None: bRetainColor = Opts.values['bRetainColor']
if bCopy is None: bCopy = Opts.values['bCopy']
if bEcho is None: bEcho = Opts.values['bEcho']
if bDebug is None: bDebug = Opts.values['bDebug']
try:
gBreps0 = list(gBreps0)
try:
dummy = list(idx_Faces_AllBreps[0])
idx_Faces_AllBreps = list(idx_Faces_AllBreps)
except:
idx_Faces_AllBreps = [idx_Faces_AllBreps]
except:
gBreps0 = [gBreps0]
idx_Faces_AllBreps = [idx_Faces_AllBreps] # Based on an assumption. (Not fail-safe.)
gExtracted_All = [] # Accumulation of duplicated faces (breps)
for gBrep0, idx_rgFaces in zip(gBreps0, idx_Faces_AllBreps):
if bCopy:
gExtracted = xBrepObject.addFromSubsetOfFaces(
gBrep0,
idx_rgFaces,
bAddOnlyMonofaces=bAddOnlyMonofaces,
bRetainLayer=bRetainLayer,
bRetainColor=bRetainColor,
)
if gExtracted is None:
print "Faces could not be duplicated."
gExtracted = []
else:
# Extract faces.
rc = xBrepObject.extractFaces(
gBrep0,
idx_rgFaces,
bAddOnlyMonofaces=bAddOnlyMonofaces,
bRetainLayer=bRetainLayer,
bRetainColor=bRetainColor,
)
if rc is None:
print "Faces could not be extracted."
gExtracted = []
else: gExtracted = rc[0]
if gExtracted is not None:
gExtracted_All.extend(gExtracted)
count_Selected = sc.doc.Objects.Select(List[Guid](gExtracted_All))
if not count_Selected:
print "Face(s) could not be extracted. Is the polyfaced brep invalid?"
else:
print "{} brep(s) are selected.".format(count_Selected)
return gExtracted_All
def main(bEcho=True, bDebug=False):
rc = getPreselectedFaceInput() if Rhino.RhinoApp.ExeVersion >= 6 else None
if rc is not None:
(
gBreps0,
idx_Faces_AllBreps,
bAddOnlyMonofaces,
bRetainLayer,
bRetainColor,
bCopy,
bEcho,
bDebug) = rc
else:
rc = getInput()
if rc is None: return
(
gBreps0,
idx_Faces_AllBreps,
bAddOnlyMonofaces,
bRetainLayer,
bRetainColor,
bCopy,
bEcho,
bDebug) = rc
if bDebug:
reload(xBrepObject)
processBrepObjects(
gBreps0=gBreps0,
idx_Faces_AllBreps=idx_Faces_AllBreps)
sc.doc.Views.Redraw()
if __name__ == '__main__': main(bEcho=bool(1), bDebug=bool(0))