-
Notifications
You must be signed in to change notification settings - Fork 7
/
spb_BrepTrim_removeInteriorKnots.py
556 lines (415 loc) · 16.5 KB
/
spb_BrepTrim_removeInteriorKnots.py
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
"""
Knot removal methods will not deform curve when there is adequate parametric continuity
through the knots.
When removing knots one by one and the knot remains, Location of it before and after knot removal
can be combined with curve deviation to test whether knot should be removed.
Unfortunately, location is of surface parameter space,
and thus determining a tolerance is not straightforward.
Possibly, tolerance should be RhinoMath.ZeroTolerance or a fraction of a domain length of the surface.
"""
from __future__ import absolute_import, division, print_function, unicode_literals
"""
221024-26: Created.
"""
import Rhino
import Rhino.DocObjects as rd
import Rhino.Geometry as rg
import Rhino.Input as ri
import scriptcontext as sc
class Opts:
keys = []
values = {}
names = {}
riOpts = {}
listValues = {}
stickyKeys = {}
key = 'bAllowBrepSelection'; keys.append(key)
values[key] = False
names[key] = 'SelectionMode'
riOpts[key] = ri.Custom.OptionToggle(values[key], 'EdgesOnly', 'EdgesandBreps')
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bDecimalTol'; keys.append(key)
values[key] = False
names[key] = 'ToleranceType'
riOpts[key] = ri.Custom.OptionToggle(values[key], 'AbsValueOfSrfParamSpace', 'DecimalOfTrimCrvDomain')
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'fTol_Absolute'; keys.append(key)
values[key] = 1e-9
names[key] = 'Tol'
riOpts[key] = ri.Custom.OptionDouble(values[key])
stickyKeys[key] = '{}({})({})'.format(key, __file__, sc.doc.Name)
key = 'fTol_Decimal'; keys.append(key)
values[key] = 1e-9
names[key] = 'Decimal'
riOpts[key] = ri.Custom.OptionDouble(values[key])
stickyKeys[key] = '{}({})({})'.format(key, __file__, sc.doc.Name)
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:
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_Absolute':
if cls.riOpts[key].CurrentValue < 0.0:
cls.values[key] = cls.riOpts[key].CurrentValue = cls.riOpts[key].InitialValue
elif cls.riOpts[key].CurrentValue < Rhino.RhinoMath.ZeroTolerance:
cls.values[key] = cls.riOpts[key].CurrentValue = Rhino.RhinoMath.ZeroTolerance
else:
cls.values[key] = cls.riOpts[key].CurrentValue
sc.sticky[cls.stickyKeys[key]] = cls.values[key]
return
if key == 'fTol_Decimal':
if cls.riOpts[key].CurrentValue < 0.0:
cls.values[key] = cls.riOpts[key].CurrentValue = cls.riOpts[key].InitialValue
elif cls.riOpts[key].CurrentValue < Rhino.RhinoMath.ZeroTolerance:
cls.values[key] = cls.riOpts[key].CurrentValue = Rhino.RhinoMath.ZeroTolerance
else:
cls.values[key] = cls.riOpts[key].CurrentValue
sc.sticky[cls.stickyKeys[key]] = cls.values[key]
return
if key in cls.riOpts:
cls.values[key] = cls.riOpts[key].CurrentValue
elif key in cls.listValues:
cls.values[key] = idxList
else:
return
sc.sticky[cls.stickyKeys[key]] = cls.values[key]
def getInput():
"""
Get breps and/or edges with optional input.
"""
go = ri.Custom.GetObject()
go.GeometryAttributeFilter = ri.Custom.GeometryAttributeFilter.EdgeCurve
go.AcceptNothing(True)
go.AcceptNumber(True, acceptZero=True)
go.AlreadySelectedObjectSelect = True
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)
bPreselectedObjsChecked = False
idxs_Opts = {}
def addOption(key): idxs_Opts[key] = Opts.addOption(go, key)
while True:
if Opts.values['bAllowBrepSelection']:
go.SetCommandPrompt("Select breps and/or edges")
go.SetCommandPromptDefault("All normal breps when none are selected")
go.GeometryFilter = rd.ObjectType.Brep | rd.ObjectType.Curve
else:
go.SetCommandPrompt("Select edges")
go.GeometryFilter = rd.ObjectType.Curve
go.ClearCommandOptions()
idxs_Opts.clear()
addOption('bAllowBrepSelection')
addOption('bDecimalTol')
if Opts.values['bDecimalTol']:
addOption('fTol_Decimal')
else:
addOption('fTol_Absolute')
addOption('bEcho')
addOption('bDebug')
res = go.GetMultiple(minimumNumber=1, maximumNumber=0)
# Use bPreselectedObjsChecked so that only objects before the
# first call to go.GetMultiple is considered.
if not bPreselectedObjsChecked and go.ObjectsWerePreselected:
bPreselectedObjsChecked = True
go.EnablePreSelect(False, ignoreUnacceptablePreselectedObjects=True)
continue
if res == ri.GetResult.Cancel:
go.Dispose()
return
if res == ri.GetResult.Object:
objrefs = go.Objects()
go.Dispose()
return objrefs
if res == ri.GetResult.Nothing:
#print(len(go.Objects()))
oes = rd.ObjectEnumeratorSettings()
oes.NormalObjects = True
oes.LockedObjects = False
oes.IncludeLights = False
oes.IncludeGrips = False
oes.ObjectTypeFilter = rd.ObjectType.Brep
rdBrepObjs = list(sc.doc.Objects.GetObjectList(oes))
go.Dispose()
if len(rdBrepObjs) == 0: return
return rdBrepObjs
if res == ri.GetResult.Number:
if Opts.values['bDecimalTol']:
key = 'fTol_Decimal'
else:
key = 'fTol_Absolute'
Opts.riOpts[key].CurrentValue = go.Number()
Opts.setValue(key)
continue
for key in idxs_Opts:
if go.Option().Index == idxs_Opts[key]:
Opts.setValue(key, go.Option().CurrentListOptionIndex)
break
def knotMultiplicities(nc):
mp = []
iK = 0
while iK < nc.Knots.Count:
m = nc.Knots.KnotMultiplicity(iK)
mp.append(m)
iK += m
return mp
def getMaxDev(rgCrvA, rgCrvB):
rc = rg.Curve.GetDistancesBetweenCurves(
rgCrvA,
rgCrvB,
tolerance=0.1*sc.doc.ModelAbsoluteTolerance)
if rc[0]:
return rc[1]
def removeKnots(nc_In, bDecimalTol, fTol, bEcho=True, bDebug=False):
"""
"""
if not isinstance(nc_In, rg.NurbsCurve):
return
if nc_In.SpanCount == 1:
return
def getOpt(key): return kwargs[key] if key in kwargs else Opts.values[key]
if bDecimalTol is None: bDecimalTol = Opts.values['bDecimalTol']
if fTol is None:
if bDecimalTol:
fTol = getOpt('fTol_Decimal')
else:
fTol = getOpt('fTol_Absolute')
fTol_Actual = max((1e-9, fTol/nc_In.Domain.Length)) if bDecimalTol else fTol
#print(fTol_Actual)
nc_Res = nc_In.DuplicateCurve()
if bDebug:
sEval = "knotMultiplicities(nc_Res)"; print("{}: {}".format(sEval, eval(sEval)))
iK = nc_Res.Knots.Count - nc_Res.Degree - 1
nc_WIP = nc_Res.Duplicate()
nc_Success = None
while iK >= nc_Res.Degree:
sc.escape_test()
if nc_Success is None:
nc_WIP = nc_Res.Duplicate()
else:
nc_WIP = nc_Success.Duplicate()
m = nc_WIP.Knots.KnotMultiplicity(iK)
if bDebug: sEval = "m"; print("{}: {}".format(sEval, eval(sEval)))
for i in range(m):
iK_Start = iK - m + i + 1
if bDebug: sEval = "iK_Start"; print("{}: {}".format(sEval, eval(sEval)))
iK_End = iK + 1
if bDebug: sEval = "iK_End"; print("{}: {}".format(sEval, eval(sEval)))
bKnotsRemoved = nc_WIP.Knots.RemoveKnots(iK_Start, iK_End)
if bDebug: sEval = "bKnotsRemoved"; print("{}: {}".format(sEval, eval(sEval)))
if not bKnotsRemoved:
continue
if getMaxDev(nc_WIP, nc_In) > fTol_Actual:
nc_WIP.Dispose()
if nc_Success is None:
nc_WIP = nc_Res.Duplicate()
else:
nc_WIP = nc_Success.Duplicate()
continue
# Success.
if nc_Success is not None:
nc_Success.Dispose()
nc_Success = nc_WIP
nc_WIP = None
m = nc_Success.Knots.KnotMultiplicity(iK)
if bDebug: sEval = "m"; print("{}: {}".format(sEval, eval(sEval)))
break
if nc_Success is None:
pass
iK -= m
return nc_Success
def processBrep(rgBrep, idx_Edges=None, bDecimalTol=None, fTol=None, bEcho=True, bDebug=False):
"""
rgBrep: Input and modified.
"""
def getOpt(key): return kwargs[key] if key in kwargs else Opts.values[key]
if bDecimalTol is None: bDecimalTol = Opts.values['bDecimalTol']
if fTol is None:
if bDecimalTol:
fTol = getOpt('fTol_Decimal')
else:
fTol = getOpt('fTol_Absolute')
iTrims_Modified = []
iFaces_Modified = []
sCmdPrompt_In = Rhino.RhinoApp.CommandPrompt
if idx_Edges is None:
idxTrims_ToMod = range(rgBrep.Trims.Count)
else:
idxTrims_ToMod = []
for iE in idx_Edges:
for iT in rgBrep.Edges[iE].TrimIndices():
idxTrims_ToMod.append(iT)
for iT in idxTrims_ToMod:
Rhino.RhinoApp.CommandPrompt = sCmdPrompt_In + " Processing at {} of {} BrepTrims in this Brep.".format(
iT+1,
len(idxTrims_ToMod))
rgT = rgBrep.Trims[iT]
rgC_In = rgT.TrimCurve
#print(rgC_In.Domain.Length)
nc_Res = removeKnots(
rgC_In,
bDecimalTol=bDecimalTol,
fTol=fTol,
bEcho=bEcho,
bDebug=bDebug,
)
if nc_Res is None: continue
i_New = rgBrep.Curves2D.Add(nc_Res)
bSetTrimCurve = rgT.SetTrimCurve(i_New)
if bSetTrimCurve:
iTrims_Modified.append(iT)
iFaces_Modified.append(rgT.Face.FaceIndex)
iFaces_Modified = sorted(set(iFaces_Modified))
#sEval = "rgBrep_In.Curves2D.Count"; print("{}: {}".format(sEval, eval(sEval)))
if bDebug:
sEval = "len(iTrims_Modified)"; print("{}: {}".format(sEval, eval(sEval)))
sEval = "len(iFaces_Modified)"; print("{}: {}".format(sEval, eval(sEval)))
if len(iTrims_Modified) == 0: return
for iF in iFaces_Modified:
face = rgBrep.Faces[iF]
face.RebuildEdges(
tolerance=sc.doc.ModelAbsoluteTolerance,
rebuildSharedEdges=True,
rebuildVertices=True)
rgBrep.Compact()
bIsValid, sLog = rgBrep.IsValidWithLog()
if not bIsValid:
rgBrep.Repair(tolerance=sc.doc.ModelAbsoluteTolerance)
bIsValid = rgBrep.IsValid
if bIsValid and bEcho:
print("Error(s) fixed by Brep.Repair: {}".format(sLog))
else:
print("Error(s) that was not fixed by Brep.Repair: {}".format(sLog))
#sEval = "rgBrep_In.Curves2D.Count"; print("{}: {}".format(sEval, eval(sEval)))
return True
def createSortedBrepsAndEdges_FromObjRefs(objrefs):
"""
Parameters:
objrefs of Edges
Returns on success:
list(GUIDs of breps) and list(list(Edge indices) per brep)
"""
rdBreps = []
gBreps = []
idxs_Edges = []
# Organize input into synchronized lists of brep ids and edge indices.
for objref in objrefs:
rdBrep = objref.Object()
gBrep = objref.ObjectId
idx_Edge = objref.Edge().EdgeIndex
if gBrep not in gBreps:
# Brep not in list, so add it as well as objref's edge.
rdBreps.append(rdBrep)
gBreps.append(gBrep)
idxs_Edges.append([idx_Edge])
else:
# Brep already in list, so add the objref's edge to the relative index.
iB = gBreps.index(gBrep)
if idx_Edge not in idxs_Edges[iB]:
idxs_Edges[iB].append(idx_Edge)
return rdBreps, idxs_Edges
def processBrepObjects(rhObjs_ToModify, bDecimalTol=None, fTol=None, bEcho=True, bDebug=False):
"""
Parameters:
rhObjs_In: (rd.ObjRef of BrepEdge) or rd.BrepObject
"""
def getOpt(key): return kwargs[key] if key in kwargs else Opts.values[key]
if bDecimalTol is None: bDecimalTol = Opts.values['bDecimalTol']
if fTol is None:
if bDecimalTol:
fTol = getOpt('fTol_Decimal')
else:
fTol = getOpt('fTol_Absolute')
if isinstance(rhObjs_ToModify[0], rd.BrepObject):
rdBs = rhObjs_ToModify
idx_Edges = None
elif isinstance(rhObjs_ToModify[0], rd.ObjRef):
rdBs = [o.Object() for o in rhObjs_ToModify]
gBs = [rdB.Id for rdB in rdBs]
edge = rhObjs_ToModify[0].Edge()
if edge is None:
idx_Edges = None
else:
rdBs, idx_Edges = createSortedBrepsAndEdges_FromObjRefs(rhObjs_ToModify)
#print(rdBs_In)
#print(idx_Edges)
gB_Modified = []
for iB, rdB in enumerate(rdBs):
Rhino.RhinoApp.SetCommandPrompt(
prompt="Searching brep {} of {} ...".format(iB+1, len(rdBs)))
rgB_In = rdB.Geometry
bModified = processBrep(
rgBrep=rdB.Geometry,
idx_Edges=idx_Edges[iB] if idx_Edges else None,
bDecimalTol=bDecimalTol,
fTol = fTol,
bEcho=True if len(rdBs) == 1 else False,
bDebug=bDebug,
)
if not bModified:
continue
if rdB.CommitChanges():
gB_Modified.append(rdB.Id)
if bEcho and len(rdBs) > 1:
print("Brep {} was modified.".format(rdB.Id))
if gB_Modified:
print("{} breps were modified.".format(len(gB_Modified)))
else:
print("No breps were modified.")
return gB_Modified
def main():
rhBrepObjs_In = getInput()
if rhBrepObjs_In is None: return
bDecimalTol = Opts.values['bDecimalTol']
if bDecimalTol:
fTol = Opts.values['fTol_Decimal']
else:
fTol = Opts.values['fTol_Absolute']
bEcho = Opts.values['bEcho']
bDebug = Opts.values['bDebug']
if not bDebug: sc.doc.Views.RedrawEnabled = False
sc.doc.Objects.UnselectAll()
processBrepObjects(
rhBrepObjs_In,
bDecimalTol=bDecimalTol,
fTol=fTol,
bEcho=bEcho,
bDebug=bDebug,
)
sc.doc.Views.RedrawEnabled = True
if __name__ == '__main__': main()