-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathspb_Brep_findFaceByBoundingBox.py
More file actions
351 lines (259 loc) · 10.5 KB
/
spb_Brep_findFaceByBoundingBox.py
File metadata and controls
351 lines (259 loc) · 10.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
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
"""
This script is to be used more as a module by other scripts.
Running this script directly provides a command for debugging this script.
"""
#! python 2 Must be on a line number less than 32.
from __future__ import absolute_import, division, print_function, unicode_literals
"""
200526: Created.
250515: Import-related updates.
"""
import Rhino
import Rhino.DocObjects as rd
import Rhino.Geometry as rg
import Rhino.Input as ri
import rhinoscriptsyntax as rs
import scriptcontext as sc
from System import Guid
import spb_BoundingBox
import xBrepObject
class Opts:
keys = []
values = {}
names = {}
riOpts = {}
riAddOpts = {}
stickyKeys = {}
def addOptionDouble(key, names, riOpts):
return lambda getObj: ri.Custom.GetBaseClass.AddOptionDouble(
getObj, englishName=names[key], numberValue=riOpts[key])
def addOptionInteger(key, names, riOpts):
return lambda getObj: ri.Custom.GetBaseClass.AddOptionInteger(
getObj, englishName=names[key], intValue=riOpts[key])
def addOptionList(key, names, listValues, values):
return lambda getObj: ri.Custom.GetBaseClass.AddOptionList(
getObj,
englishOptionName=names[key],
listValues=listValues,
listCurrentIndex=values[key])
def addOptionToggle(key, names, riOpts):
return lambda getObj: ri.Custom.GetBaseClass.AddOptionToggle(
getObj, englishName=names[key], toggleValue=riOpts[key])
key = 'fTolerance'; keys.append(key)
values[key] = 1.0 * sc.doc.ModelAbsoluteTolerance
names[key] = key[1:]
riOpts[key] = ri.Custom.OptionDouble(values[key])
riAddOpts[key] = addOptionDouble(key, names, riOpts)
stickyKeys[key] = '{}({})({})'.format(key, __file__, sc.doc.Name)
key = 'bDelOtherFacesNotExtract'; keys.append(key)
values[key] = False
names[key] = 'Action'
riOpts[key] = ri.Custom.OptionToggle(values[key], 'Extract', 'DeleteOtherFaces')
riAddOpts[key] = addOptionToggle(key, names, riOpts)
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bEcho'; keys.append(key)
values[key] = True
names[key] = key[1:]
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
riAddOpts[key] = addOptionToggle(key, names, riOpts)
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bDebug'; keys.append(key)
values[key] = False
names[key] = key[1:]
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
riAddOpts[key] = addOptionToggle(key, names, riOpts)
stickyKeys[key] = '{}({})'.format(key, __file__)
# 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:
values[key] = sc.sticky[stickyKeys[key]]
@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:
sc.sticky[cls.stickyKeys[key]] = cls.values[key]
def getInput_GeomForBBoxToMatch():
"""
"""
go = ri.Custom.GetObject()
go.SetCommandPrompt("Select objects to create bounding box to match")
#go.GeometryFilter = rd.ObjectType
go.GroupSelect = True
go.SubObjectSelect = False
idxs_Opts = {}
while True:
key = 'fTolerance'; idxs_Opts[key] = Opts.riAddOpts[key](go)[0]
key = 'bDelOtherFacesNotExtract'; idxs_Opts[key] = Opts.riAddOpts[key](go)[0]
key = 'bEcho'; idxs_Opts[key] = Opts.riAddOpts[key](go)[0]
key = 'bDebug'; idxs_Opts[key] = Opts.riAddOpts[key](go)[0]
res = go.GetMultiple(minimumNumber=1, maximumNumber=0)
if res == ri.GetResult.Cancel:
go.Dispose()
return
elif res == ri.GetResult.Object:
objrefs = go.Objects()
go.Dispose()
return [objrefs] + [Opts.values[key] for key in Opts.keys]
else:
# An option was selected or a number was entered.
key = 'fTolerance'
if res == ri.GetResult.Number:
Opts.riOpts[key].CurrentValue = go.Number()
if Opts.riOpts[key].CurrentValue < 1e-12:
Opts.riOpts[key].CurrentValue = Opts.riOpts[key].InitialValue
Opts.setValues()
Opts.saveSticky()
go.ClearCommandOptions()
def getInput_BrepWithFacesToSearch():
"""
Get Brep Surface with optional input
"""
sc.doc.Objects.UnselectAll()
sc.doc.Views.Redraw()
go = ri.Custom.GetObject()
go.SetCommandPrompt("Select breps to search")
go.GeometryFilter = rd.ObjectType.Brep
idxs_Opts = {}
while True:
key = 'fTolerance'; idxs_Opts[key] = Opts.riAddOpts[key](go)[0]
key = 'bDelOtherFacesNotExtract'; idxs_Opts[key] = Opts.riAddOpts[key](go)[0]
key = 'bEcho'; idxs_Opts[key] = Opts.riAddOpts[key](go)[0]
key = 'bDebug'; idxs_Opts[key] = Opts.riAddOpts[key](go)[0]
res = go.Get()
if res == ri.GetResult.Cancel:
go.Dispose()
return
elif res == ri.GetResult.Object:
objref = go.Object(0)
go.Dispose()
return [objref] + [Opts.values[key] for key in Opts.keys]
else:
# An option was selected or a number was entered.
key = 'fTolerance'
if res == ri.GetResult.Number:
Opts.riOpts[key].CurrentValue = go.Number()
if Opts.riOpts[key].CurrentValue < 1e-12:
Opts.riOpts[key].CurrentValue = Opts.riOpts[key].InitialValue
Opts.setValues()
Opts.saveSticky()
go.ClearCommandOptions()
def findFace(rgBrep_toSearch, rgObjs_forBBoxToMatch, fTolerance=None, bDebug=False):
"""
Returns:
Index of brep in breps.
This function is used by other scripts, so leave it at the root level.
"""
if fTolerance is None:
fTolerance = 2.0 * sc.doc.ModelAbsoluteTolerance
bbox_toMatch = spb_BoundingBox.createBoundingBox(rgObjs_forBBoxToMatch)
#sc.doc.Objects.AddBox(rg.Box(bbox=bbox_toMatch)); sc.doc.Views.Redraw()
bboxes_Faces_perBrep = []
rgB = rgBrep_toSearch
for idxF, rgFace in enumerate(rgB.Faces):
rgEs_forBorder = []
# This only uses the OutLoop.
#for rgT in rgFace.OuterLoop.Trims:
# if rgT != rg.BrepTrimType.Seam:
# rgEs_forBorder.append(rgT.Edge)
# Accumulate the edge curves of all non-seam trims.
for idxE in rgFace.AdjacentEdges():
rgE = rgB.Edges[idxE]
idxsTs = rgE.TrimIndices()
if rgB.Trims[idxsTs[0]].TrimType != rg.BrepTrimType.Seam:
rgEs_forBorder.append(rgE)
bbox_ofFaceBorder = spb_BoundingBox.createBoundingBox(rgEs_forBorder)
bboxes_Faces_perBrep.append(bbox_ofFaceBorder)
#sc.doc.Objects.AddBox(rg.Box(bbox=bbox_ofFaceBorder)); sc.doc.Views.Redraw()
idxF_Match = spb_BoundingBox.findMatchingBoundingBox(
bboxes_Faces_perBrep,
bbox_toMatch,
fTolerance=fTolerance,
bDebug=bDebug)
return idxF_Match
def processBrepObject(rhBrep_toSearch, rhObjs_forBBoxToMatch, **kwargs):
"""
Parameters:
fTolerance
bDelOtherFacesNotExtract
bEcho
bDebug
Returns on success:
GUID of monoface brep
Returns on fail:
None
"""
if not rhBrep_toSearch or rhObjs_forBBoxToMatch is None: return
def getOpt(key): return kwargs[key] if key in kwargs else Opts.values[key]
fTolerance = getOpt('fTolerance')
bDelOtherFacesNotExtract = getOpt('bDelOtherFacesNotExtract')
bEcho = getOpt('bEcho')
bDebug = getOpt('bDebug')
try: rhBrep_toSearch = list(rhBrep_toSearch)
except: rhBrep_toSearch = [rhBrep_toSearch]
try: rhObjs_forBBoxToMatch = list(rhObjs_forBBoxToMatch)
except: rhObjs_forBBoxToMatch = [rhObjs_forBBoxToMatch]
geom_forBBox_toMatch = [rs.coercegeometry(o) for o in rhObjs_forBBoxToMatch]
gB_toSearch = rs.coerceguid(rhBrep_toSearch)
rgB = rs.coercebrep(rhBrep_toSearch)
idxFace_Match = findFace(rgB, geom_forBBox_toMatch)
if idxFace_Match is None:
print("Matching face was not found.")
return
if bDelOtherFacesNotExtract:
rdBrep = rs.coercerhinoobject(rhBrep_toSearch)
rgB_1F = rgB.Faces[idxFace_Match].DuplicateFace(duplicateMeshes=True)
gB_1F = sc.doc.Objects.AddBrep(
rgB_1F,
attributes=rdBrep.Attributes)
rgB_1F.Dispose()
if gB_1F == Guid.Empty:
print("Brep could not be added.")
return
if sc.doc.Objects.Delete(objectId=gB_toSearch, quiet=False):
print("Monoface brep replaced polyface brep.")
return gB_1F
else:
rc = xBrepObject.extractFaces(gB_toSearch, [idxFace_Match])
if rc is None: return
gExtracted, gRemaining = rc
return gExtracted[0]
def main():
rc = getInput_GeomForBBoxToMatch()
if rc is None: return
objrefs_forBBox_toMatch = rc[0]
rc = getInput_BrepWithFacesToSearch()
if rc is None: return
objref_Brep_toSearch = rc[0]
if not Opts.values['bDebug']:
sc.doc.Views.RedrawEnabled = False
rc = processBrepObject(
rhBrep_toSearch=objref_Brep_toSearch,
rhObjs_forBBoxToMatch=objrefs_forBBox_toMatch)
if not rc: return
#if isinstance(rc, int):
# idxF = rc
# sc.doc.Objects.UnselectAll()
# if Rhino.RhinoApp.ExeVersion >= 6:
# rdBrep = rs.coercerhinoobject(objref_Brep_toSearch)
# compIdx = rg.ComponentIndex(
# rg.ComponentIndexType.BrepFace,
# index=idxF)
# rdBrep.SelectSubObject(
# componentIndex=compIdx,
# select=True,
# syncHighlight=True,
# persistentSelect=True)
if isinstance(rc, Guid):
gB_Extracted = rc
sc.doc.Objects.Select(objectId=gB_Extracted)
sc.doc.Views.RedrawEnabled = True
if __name__ == '__main__': main()