-
Notifications
You must be signed in to change notification settings - Fork 7
/
spb_NurbsCrv_makeUniform.py
501 lines (372 loc) · 14.2 KB
/
spb_NurbsCrv_makeUniform.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
"""
This script is an alternative to _MakeUniform for curves in that it includes
options to
1. Reject results outside of a distance tolerance,
2. Preserve end curvatures,
3. Filter some curve types,
4. Add instead of only modify input.
"""
from __future__ import absolute_import, division, print_function, unicode_literals
"""
200507: Created.
221122: Import-related update.
230102: Added option to preserve curvature of each ends.
"""
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 = 'bLimitCrvDev'; keys.append(key)
values[key] = False
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'fDevTol'; keys.append(key)
values[key] = 0.1 * sc.doc.ModelAbsoluteTolerance
riOpts[key] = ri.Custom.OptionDouble(initialValue=values[key])
stickyKeys[key] = '{}({})({})'.format(key, __file__, sc.doc.Name)
key = 'bPreserveEndCurvatures'; keys.append(key)
values[key] = False
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bModifyArcCrvs'; keys.append(key)
values[key] = True
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bModifyPolyCrvs'; keys.append(key)
values[key] = True
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bModify_NotAdd'; keys.append(key)
values[key] = True
names[key] = 'DocAction'
riOpts[key] = ri.Custom.OptionToggle(values[key], 'Add', 'Modify')
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]
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 == 'fDevTol':
if cls.riOpts[key].CurrentValue < 0.0:
cls.riOpts[key].CurrentValue = cls.riOpts[key].InitialValue
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 curve and parameter with optional input.
"""
go = ri.Custom.GetObject()
go.SetCommandPrompt("Select curves")
go.GeometryFilter = Rhino.DocObjects.ObjectType.Curve
go.GeometryAttributeFilter = ri.Custom.GeometryAttributeFilter.WireCurve
go.AlreadySelectedObjectSelect = True
go.DeselectAllBeforePostSelect = False # So objects won't be deselected on repeats of While loop.
#go.SubObjectSelect = False
go.EnableClearObjectsOnEntry(False) # Keep objects in go on repeats of While loop.
go.EnableUnselectObjectsOnExit(False)
idxs_Opt = {}
bPreselectedObjsChecked = False
def addOption(key): idxs_Opt[key] = Opts.addOption(go, key)
while True:
go.ClearCommandOptions()
idxs_Opt.clear()
addOption('bLimitCrvDev')
if Opts.values['bLimitCrvDev']:
addOption('fDevTol')
go.AcceptNumber(True, acceptZero=True)
else:
go.AcceptNumber(False, acceptZero=True)
addOption('bPreserveEndCurvatures')
addOption('bModifyArcCrvs')
addOption('bModifyPolyCrvs')
addOption('bModify_NotAdd')
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.Number:
key = 'fDevTol'
Opts.riOpts[key].CurrentValue = go.Number()
Opts.setValue(key)
continue
# An option was selected.
for key in idxs_Opt:
if go.Option().Index == idxs_Opt[key]:
Opts.setValue(key, go.Option().CurrentListOptionIndex)
break
def formatDistance(fDistance):
if fDistance is None:
return "(No deviation provided)"
if fDistance == 0.0:
return 0.0
if fDistance < 0.001:
return "{:.2e}".format(fDistance)
else:
return "{:.{}f}".format(fDistance, sc.doc.ModelDistanceDisplayPrecision)
def areEpsilonEqual(a, b, epsilon):
# This is a relative comparison.
delta = abs(a-b)
#print f2s(delta),
fRelComp = delta / max(abs(a), abs(b))
#print f2s(fRelComp)
return fRelComp < epsilon
def isUniform(nc):
if nc.Points.Count == nc.Degree + 1:
return True
start = 0 if nc.IsPeriodic else nc.Degree - 1
end = nc.Knots.Count - (0 if nc.IsPeriodic else nc.Degree - 1) - 1
#print start, end
span0 = nc.Knots[start+1] - nc.Knots[start]
#print f2s(span0)
for i in range(start+1, end):
if nc.Knots.KnotMultiplicity(i) > 1:
return False
#print f2s(nc.Knots[i+1] - nc.Knots[i])
if not areEpsilonEqual(
span0, nc.Knots[i+1] - nc.Knots[i],
epsilon=1e-9):
return False
return True
def matchEndCurvatures(nc_ToMod, nc_Ref):
"""
nc_ToMod is modified.
"""
bSuccess = nc_ToMod.SetEndCondition(
bSetEnd=False,
continuity=rg.NurbsCurve.NurbsCurveEndConditionType.Curvature,
point=nc_ToMod.PointAtStart,
tangent=nc_Ref.TangentAtStart,
curvature=nc_Ref.CurvatureAt(nc_Ref.Domain.T0))
if not bSuccess:
print("SetEndCondition failed.")
bSuccess = nc_ToMod.SetEndCondition(
bSetEnd=True,
continuity=rg.NurbsCurve.NurbsCurveEndConditionType.Curvature,
point=nc_ToMod.PointAtEnd,
tangent=nc_Ref.TangentAtEnd,
curvature=nc_Ref.CurvatureAt(nc_Ref.Domain.T1))
if not bSuccess:
print("SetEndCondition failed.")
return True
def getDistancesBetweenCurves(crvA, crvB):
rc = rg.Curve.GetDistancesBetweenCurves(
crvA, crvB, 0.1*sc.doc.ModelAbsoluteTolerance)
if not rc[0]:
raise Exception("GetDistancesBetweenCurves returned None.")
return None
return rc[1]
def createUniformNurbsCurve(nc_In, fDevTol=None, bPreserveEndCurvatures=False, bDebug=False):
"""
Parameters:
fDevTol: None for no limit.
bPreserveEndCurvatures
bDebug
Returns:
Success: rgNurbsCurve, fMinRadius, fDeviation, None
Fail: None, None, None, sLog
"""
if not isinstance(nc_In, rg.NurbsCurve):
return None, "Not a NURBS curve."
if isUniform(nc_In):
return None, 'Curve is already uniform.'
nc_Out = nc_In.ToNurbsCurve()
if nc_In.IsPeriodic:
nc_Out.Knots.CreatePeriodicKnots(knotSpacing=1.0) # Modifies Knots.
#fK = float(-nc_In.Degree)
#for iK in range(nc_In.Knots.Count):
# fK += 1.0
# nc_Out.Knots[iK] = fK
else:
nc_Out.Knots.CreateUniformKnots(knotSpacing=1.0) # Modifies Knots.
#fK = 0.0
#for iK in range(nc_In.Knots.Count):
# if nc_In.Degree <= iK <= (nc_In.Knots.Count - nc_In.Degree):
# fK += 1.0
# nc_Out.Knots[iK] = fK
if bPreserveEndCurvatures:
if not matchEndCurvatures(nc_Out, nc_In):
raise Exception("Preserve end curvatures failed.")
if fDevTol is not None:
dev = getDistancesBetweenCurves(nc_In, nc_Out)
if dev > fDevTol:
nc_Out.Dispose()
return None, "Result is not within allowed deviation."
return nc_Out, None
def processCurveObject(rhCrv_In, **kwargs):
"""
Parameters:
fDevTol
bPreserveEndCurvatures
bModifyArcCrvs
bModifyPolyCrvs
bModify_NotAdd
bEcho
bDebug
"""
def getOpt(key): return kwargs[key] if key in kwargs else Opts.values[key]
fDevTol = getOpt('fDevTol')
bPreserveEndCurvatures = getOpt('bPreserveEndCurvatures')
bModifyArcCrvs = getOpt('bModifyArcCrvs')
bModifyPolyCrvs = getOpt('bModifyPolyCrvs')
bModify_NotAdd = getOpt('bModify_NotAdd')
bEcho = getOpt('bEcho')
bDebug = getOpt('bDebug')
def getCurve(rhObj):
if isinstance(rhObj, rd.CurveObject):
return rhObj, rhObj.CurveGeometry
if isinstance(rhObj, rg.Curve):
return None, rhObj
if isinstance(rhObj, rg.GeometryBase):
rdObj = None
rgObj = rhObj
elif isinstance(rhObj, rd.ObjRef):
rdObj = rhObj.Object()
rgObj = rhObj.Geometry()
elif isinstance(rhObj, Guid):
rdObj = sc.doc.Objects.FindId(rhObj) if Rhino.RhinoApp.ExeVersion >= 6 else sc.doc.Objects.Find(rhObj)
rgObj = rdObj.Geometry
else:
return
if isinstance(rgObj, rg.Curve):
return rdObj, rgObj
rdCrv_In, rgCrv_In = getCurve(rhCrv_In)
if isinstance(rgCrv_In, rg.NurbsCurve):
nc_Start = rgCrv_In.DuplicateCurve()
elif bModifyArcCrvs and isinstance(rgCrv_In, rg.ArcCurve):
nc_Start = rgCrv_In.ToNurbsCurve()
elif bModifyPolyCrvs and isinstance(rgCrv_In, rg.PolyCurve):
nc_Start = rgCrv_In.ToNurbsCurve()
else:
return None, "{} is not an accepted input.".format(rgCrv_In.GetType().Name)
if isUniform(nc_Start):
nc_Start.Dispose()
return None, 'Curve is already uniform.'
gCrv_In = rdCrv_In.Id
rc = createUniformNurbsCurve(
nc_In=nc_Start,
fDevTol=fDevTol,
bPreserveEndCurvatures=bPreserveEndCurvatures,
bDebug=bDebug,
)
if rc[0] is None:
return rc
nc_Res, sLog = rc
if sLog:
raise Exception("sLog should be None but is {}".format(sLog))
nc_Res = rc[0]
if bModify_NotAdd:
if sc.doc.Objects.Replace(gCrv_In, nc_Res):
return gCrv_In, "Curve was replaced."
else:
return None, "Curve could not be replaced."
else:
g1 = sc.doc.Objects.AddCurve(nc_Res)
if g1 != Guid.Empty:
return gCrv_In, "Curve was added."
else:
return None, "Curve could not be added."
nc_Start.Dispose()
return gs1, sLogs
def main():
objrefs = getInput()
if objrefs is None: return
fDevTol = Opts.values['fDevTol'] if Opts.values['bLimitCrvDev'] else None
bPreserveEndCurvatures = Opts.values['bPreserveEndCurvatures']
bModifyArcCrvs = Opts.values['bModifyArcCrvs']
bModifyPolyCrvs = Opts.values['bModifyPolyCrvs']
bModify_NotAdd = Opts.values['bModify_NotAdd']
bEcho = Opts.values['bEcho']
bDebug = Opts.values['bDebug']
if not bDebug:
sc.doc.Views.RedrawEnabled = False
gCs_Result = []
sLogs = []
Rhino.RhinoApp.SetCommandPrompt("Working ...")
for objref in objrefs:
gC_Result, sLog = processCurveObject(
objref,
fDevTol=Opts.values['fDevTol'] if Opts.values['bLimitCrvDev'] else None,
bPreserveEndCurvatures=bPreserveEndCurvatures,
bModifyArcCrvs=bModifyArcCrvs,
bModifyPolyCrvs=bModifyPolyCrvs,
bModify_NotAdd=bModify_NotAdd,
bEcho=bEcho,
bDebug=bDebug
)
if gC_Result:
gCs_Result.append(gC_Result)
if sLog:
sLogs.append(sLog)
if bEcho:
for sLog in set(sLogs):
print("[{}] {}".format(sLogs.count(sLog), sLog))
for gC in gCs_Result:
sc.doc.Objects.Select(gC)
sc.doc.Views.RedrawEnabled = True
if __name__ == '__main__': main()