-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathspb_BoundingBox.py
More file actions
420 lines (332 loc) · 13.1 KB
/
spb_BoundingBox.py
File metadata and controls
420 lines (332 loc) · 13.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
"""
This script is to be used both as a module and a stand-along script.
Unlike _BoundingBox, the script offers an Accurate option.
Unlike the script, _BoundingBox has an Output object type option.
"""
#! python 2 Must be on a line number less than 32.
from __future__ import absolute_import, division, print_function, unicode_literals
"""
...
190505: Removed a function.
200525-26: Added functionality so this script can be run on its own or continued being used as a module.
250515: Added support to create bounding box per CPlane. Added command options.
TODO:
Merge spb_BoundingBox2 with this script. It includes options for center point, etc.
"""
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
class Opts:
keys = []
values = {}
names = {}
riOpts = {}
listValues = {}
stickyKeys = {}
key = 'bAccurate'; keys.append(key)
values[key] = True
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bCPlane_notWorld'; keys.append(key)
values[key] = True
names[key] = 'CoordinateSystem'
riOpts[key] = ri.Custom.OptionToggle(values[key], 'World', 'CPlane')
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bCumulative'; keys.append(key)
values[key] = True
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
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 riOpts[key]:
values[key] = riOpts[key].CurrentValue = 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]
elif key in cls.listValues:
idxOpt = go.AddOptionList(
englishOptionName=cls.names[key],
listValues=cls.listValues[key],
listCurrentIndex=cls.values[key])
else:
print("{} is not a valid key in Opts.".format(key))
return idxOpt
@classmethod
def setValue(cls, key, idxList=None):
#if key == 'fTol_Edge_and_trim':
# if cls.riOpts[key].CurrentValue < 0.0:
# cls.riOpts[key].CurrentValue = cls.riOpts[key].InitialValue
# elif cls.riOpts[key].CurrentValue < Rhino.RhinoMath.ZeroTolerance:
# cls.riOpts[key].CurrentValue = Rhino.RhinoMath.ZeroTolerance
# sc.sticky[cls.stickyKeys[key]] = cls.values[key] = cls.riOpts[key].CurrentValue
# return
if key in cls.riOpts:
sc.sticky[cls.stickyKeys[key]] = cls.values[key] = cls.riOpts[key].CurrentValue
return
if key in cls.listValues:
sc.sticky[cls.stickyKeys[key]] = cls.values[key] = idxList
return
print("Invalid key?")
def getInput():
"""
Get objects.
"""
go = ri.Custom.GetObject()
go.SetCommandPrompt("Select objects to frame with a box")
go.GeometryFilter = Rhino.DocObjects.ObjectType.Brep
#go.AcceptNumber(True, acceptZero=False)
go.EnableClearObjectsOnEntry(False) # If not set to False, faces will be unselected when result == ri.GetResult.Object
idxs_Opts = {}
def addOption(key): idxs_Opts[key] = Opts.addOption(go, key)
while True:
go.ClearCommandOptions()
idxs_Opts.clear()
addOption('bAccurate')
addOption('bCPlane_notWorld')
addOption('bCumulative')
addOption('bEcho')
addOption('bDebug')
res = go.GetMultiple(minimumNumber=1, maximumNumber=0)
if res == ri.GetResult.Cancel:
go.Dispose()
return
if res == ri.GetResult.Object:
objrefs = go.Objects()
go.Dispose()
return objrefs
if res == ri.GetResult.Number:
key = 'fTol_Edge_and_trim'
Opts.riOpts[key].CurrentValue = go.Number()
Opts.setValue(key)
continue
# An option was selected.
for key in idxs_Opts:
if go.Option().Index == idxs_Opts[key]:
Opts.setValue(key, go.Option().CurrentListOptionIndex)
break
def createInverseTransform(xform_In):
bSuccess, xform_Out = xform_In.TryGetInverse()
return xform_Out if bSuccess else None
def createBoundingBox(rgObjs, bAccurate=True, xform=rg.Plane.WorldXY):
"""
Returns:
rg.BoundingBox as transformed per xform.
"""
try: rgObjs = list(rgObjs)
except: rgObjs = [rgObjs]
bbox = rg.BoundingBox.Empty
if xform is None or xform == rg.Plane.WorldXY:
for geom in rgObjs:
bbox.Union(geom.GetBoundingBox(accurate=bAccurate))
else:
for geom in rgObjs:
dup = geom.Duplicate()
dup.Transform(xform)
#sc.doc.Objects.Add(dup); #1/0
bbox.Union(dup.GetBoundingBox(accurate=bAccurate))
#sEval = "geom.IsDocumentControlled"; print(sEval, '=', eval(sEval))
dup.Dispose()
sEval = "bbox"; print(sEval, '=', eval(sEval))
#sc.doc.Objects.AddPoints(bbox.GetCorners())
xform_Inverse = createInverseTransform(xform)
if xform_Inverse is None:
raise Exception("Could not get inverse transform.")
#bbox.Transform(xform_Inverse)
#sc.doc.Objects.AddPoints(bbox.GetCorners())
sEval = "bbox"; print(sEval, '=', eval(sEval))
if bbox is None:
raise ValueError("Bounding box not created.")
return bbox
def epsilonEquals(bBoxA, bBoxB, fTol=sc.doc.ModelAbsoluteTolerance):
"""
Created because BoundingBox structure does not contain an EpsilonEquals method.
"""
boxA = Rhino.Geometry.Box(bBoxA)
if not boxA: return
boxB = Rhino.Geometry.Box(bBoxB)
if not boxB: return
return boxA.EpsilonEquals(boxB, fTol)
def findMatchingBoundingBox(bboxes_toSearch, bbox_toMatch, fTolerance=None, bDebug=False):
"""
Parameters:
bboxes_toSearch: Iterable of BoundingBoxes from which to search.
bbox_toMatch: BoundingBox to match.
fTolerance
bDebug
Return on success:
int(Index of single matching bounding box)
Return on fail:
None
"""
if bDebug: print('findMatchingBoundingBox()...')
if fTolerance is None:
fTolerance = sc.doc.ModelAbsoluteTolerance
idx_Found = None
for i, bbox_toCheck in enumerate(bboxes_toSearch):
if (
bbox_toMatch.Min.EpsilonEquals(bboxes_toSearch[i].Min, epsilon=fTolerance) and
bbox_toMatch.Max.EpsilonEquals(bboxes_toSearch[i].Max, epsilon=fTolerance)
):
if idx_Found is not None:
print("More than one match found. Check results.")
return
idx_Found = i
if idx_Found is None:
if bDebug:
print("Matching bounding box NOT found.")
#sc.doc.Objects.AddBrep(bbox_toMatch.ToBrep())
#for bb in bboxes_toSearch:
#sc.doc.Objects.AddBrep(bb.ToBrep())
return idx_Found
def addDocObject_of_boundingBox(bbox, xform_for_bbbox, bEcho=True, bDebug=False):
iCt_MatchingCoords = 0
bCoordMatches = [False, False, False]
if abs(bbox.Min.X - bbox.Max.X) < 1e-9:
iCt_MatchingCoords += 1
bCoordMatches[0] = True
if abs(bbox.Min.Y - bbox.Max.Y) < 1e-9:
iCt_MatchingCoords += 1
bCoordMatches[1] = True
if abs(bbox.Min.Z - bbox.Max.Z) < 1e-9:
iCt_MatchingCoords += 1
bCoordMatches[2] = True
iCt_MatchingCoords = bCoordMatches.count(True)
if bDebug: print(bbox)
if iCt_MatchingCoords == 0:
box = rg.Box(bbox=bbox)
if xform_for_bbbox is not None:
box.Transform(xform_for_bbbox)
gBox = sc.doc.Objects.AddBox(box)
if gBox == Guid.Empty:
if bEcho: print("Box could not be added.")
return
else:
if bEcho: print("Box (extrusion) was added.")
return gBox
if iCt_MatchingCoords == 1:
if bDebug: print("Bounding box is planar.")
if bCoordMatches[0]:
plane = rg.Plane.WorldYZ
plane.Translate(rg.Vector3d(bbox.Min.X, 0.0, 0.0))
elif bCoordMatches[1]:
plane = rg.Plane.WorldZX
plane.Translate(rg.Vector3d(0.0, bbox.Min.Y, 0.0))
elif bCoordMatches[2]:
plane = rg.Plane.WorldXY
plane.Translate(rg.Vector3d(0.0, 0.0, bbox.Min.Z))
rect = rg.Rectangle3d(
plane=plane,
cornerA=bbox.Min,
cornerB=bbox.Max)
if xform_for_bbbox is not None:
rect.Transform(xform_for_bbbox)
gPLCrv = sc.doc.Objects.AddRectangle(rect)
if gPLCrv == Guid.Empty:
if bEcho: print("Polyline could not be added.")
return
else:
if bEcho: print("Polyline was added.")
return gPLCrv
if iCt_MatchingCoords == 2:
if bEcho: print("Bounding box is linear.")
line = rg.Line(bbox.Min, to=bbox.Max)
if xform_for_bbbox is not None:
line.Transform(xform_for_bbbox)
gLineCrv = sc.doc.Objects.AddLine(line)
if gLineCrv == Guid.Empty:
if bEcho: print("Line could not be added.")
return
else:
sc.doc.Views.Redraw()
if bEcho: print("Line was added.")
return gLineCrv
if iCt_MatchingCoords == 3:
if bEcho: print("Bounding box is a point. Point was not added.")
return
raise Exception("What happened?")
def main():
#res, objrefs = ri.RhinoGet.GetMultipleObjects(
# "Select objects to create bounding box",
# acceptNothing=False,
# filter=rd.ObjectType.AnyObject)
#if res == Rhino.Commands.Result.Cancel: return
objrefs = getInput()
if objrefs is None: return
bAccurate = Opts.values['bAccurate']
bCPlane_notWorld = Opts.values['bCPlane_notWorld']
bCumulative = Opts.values['bCumulative']
bEcho = Opts.values['bEcho']
bDebug = Opts.values['bDebug']
xform_WtoC = xform_CtoW = None
if bCPlane_notWorld:
plane = sc.doc.Views.ActiveView.ActiveViewport.ConstructionPlane()
if plane != rg.Plane.WorldXY:
xform_WtoC = rg.Transform.ChangeBasis(rg.Plane.WorldXY, plane)
xform_CtoW = createInverseTransform(xform_WtoC)
if xform_CtoW is None:
raise Exception("Could not get inverse of Cplane-to-World transform.")
sc.doc.Objects.UnselectAll()
if not bDebug: sc.doc.Views.RedrawEnabled = False
Rhino.RhinoApp.SetCommandPrompt("Working ...")
gResults = []
if bCumulative:
rgObjs = [o.Geometry() for o in objrefs]
bbox = createBoundingBox(rgObjs, bAccurate=bAccurate, xform=xform_WtoC)
if bbox is None:
if bEcho: print("Could not create bounding box.")
return
rv = addDocObject_of_boundingBox(
bbox=bbox,
xform_for_bbbox=xform_CtoW,
bEcho=bEcho,
bDebug=bDebug)
if rv:
gResults.append(rv)
else:
for objref in objrefs:
rgObjs = [objref.Geometry()]
bbox = createBoundingBox(rgObjs, bAccurate=bAccurate, xform=xform_WtoC)
if bbox is None:
if bEcho: print("Could not create bounding box.")
continue
rv = addDocObject_of_boundingBox(
bbox=bbox,
xform_for_bbbox=xform_CtoW,
bEcho=bEcho,
bDebug=bDebug)
if rv:
gResults.append(rv)
#box = rg.Box(bbox=bbox)
#box.Transform(xform_CtoW)
#sc.doc.Objects.AddBox(box)
#return
sc.doc.Views.RedrawEnabled = True
if __name__ == '__main__': main()