forked from CADacombs/rhinopython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxNurbsCurve_decreaseDegreeFromEnd.py
More file actions
394 lines (287 loc) · 10.6 KB
/
xNurbsCurve_decreaseDegreeFromEnd.py
File metadata and controls
394 lines (287 loc) · 10.6 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
"""
201124-30: Created.
"""
import Rhino
import Rhino.Geometry as rg
import Rhino.Input as ri
import scriptcontext as sc
class Opts:
keys = []
values = {}
names = {}
riOpts = {}
listValues = {}
stickyKeys = {}
key = 'bConservePickedEnd'; keys.append(key)
values[key] = False
names[key] = 'ConserveContinuityEnd'
riOpts[key] = ri.Custom.OptionToggle(values[key], 'OppPicked', 'Picked')
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bReplace'; keys.append(key)
values[key] = False
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 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]
else:
idxOpt = go.AddOptionList(
englishOptionName=cls.names[key],
listValues=cls.listValues[key],
listCurrentIndex=cls.values[key])
return idxOpt
@classmethod
def setValues(cls):
for key in cls.riOpts:
cls.values[key] = cls.riOpts[key].CurrentValue
if key == 'fTol':
if cls.riOpts[key].CurrentValue < 0.0:
cls.riOpts[key].CurrentValue = cls.riOpts[key].InitialValue
@classmethod
def saveSticky(cls):
for key in cls.stickyKeys:
if key in cls.riOpts:
sc.sticky[cls.stickyKeys[key]] = cls.riOpts[key].CurrentValue
else:
# For OptionList.
sc.sticky[cls.stickyKeys[key]] = cls.values[key]
def getInput():
"""
"""
go = ri.Custom.GetObject()
go.SetCommandPrompt("Pick curve near an end")
go.GeometryFilter = Rhino.DocObjects.ObjectType.Curve
def geomFilter_Curve(rdObj, geom, compIdx):
#print rdObj, geom, compIdx.ComponentIndexType, compIdx.Index
if isinstance(geom, rg.BrepEdge):
# DuplicateCurve gets the edge as a curve, which may be a subset of the EdgeCurve.
rgC = geom.DuplicateCurve()
elif isinstance(geom, rg.Curve):
rgC = geom
else:
return False
if isinstance(rgC, rg.NurbsCurve):
nc = rgC
elif isinstance(rgC, rg.PolyCurve):
nc = rgC.ToNurbsCurve()
else:
return False
if nc.IsPeriodic:
print "Periodic curves are not supported."
return False
if nc.Degree == 1:
print "Ignorded edge because NURBS curve is degree 1."
return False
return True
go.SetCustomGeometryFilter(geomFilter_Curve)
go.DisablePreSelect()
idxs_Opt = {}
while True:
key = 'bConservePickedEnd'; idxs_Opt[key] = Opts.addOption(go, key)
key = 'bReplace'; idxs_Opt[key] = Opts.addOption(go, key)
key = 'bEcho'; idxs_Opt[key] = Opts.addOption(go, key)
key = 'bDebug'; idxs_Opt[key] = Opts.addOption(go, key)
res = go.Get()
if res == ri.GetResult.Cancel:
go.Dispose()
return
if res == ri.GetResult.Object:
objref = go.Object(0)
go.Dispose()
rgC = objref.Curve()
iDegree_Start = rgC.Degree
if iDegree_Start == 2:
bDegree_Target = 1
else:
rc, bDegree_Target = ri.RhinoGet.GetInteger(
"Change degree from {} to".format(iDegree_Start),
acceptNothing=True,
outputNumber=iDegree_Start-1 if iDegree_Start < 5 else 3,
lowerLimit=1,
upperLimit=iDegree_Start-1)
if rc == Rhino.Commands.Result.Cancel:
return
return tuple([objref, bDegree_Target] + [Opts.values[key] for key in Opts.keys])
# An option was selected.
Opts.setValues()
Opts.saveSticky()
go.ClearCommandOptions()
def getScaleFactorForDegreeChange(nc_From, bConserveT1End, iDeg_To):
"""
"""
scale = 1.0
for i in range(nc_From.Degree-1, iDeg_To-1, -1):
scale *= (float(i)**2 - 1.0) / (float(i)**2)
if nc_From.SpanCount > 1:
if bConserveT1End:
iKnot_AdjToEnd = nc_From.Knots.Count - nc_From.Degree - 1
else:
iKnot_AdjToEnd = nc_From.Degree
if nc_From.Knots.KnotMultiplicity(iKnot_AdjToEnd) == 1:
# Adjust scale factor for knot vector.
if bConserveT1End:
domSpan = nc_From.SpanDomain(nc_From.SpanCount-1)
domSpan_Adj = nc_From.SpanDomain(nc_From.SpanCount-2)
else:
domSpan = nc_From.SpanDomain(0)
domSpan_Adj = nc_From.SpanDomain(1)
m = (domSpan.Length + domSpan_Adj.Length) / domSpan.Length
scale *= m
if nc_From.IsRational:
# Adjust scale factor for rationality.
if bConserveT1End:
w0 = nc_From.Points.GetWeight(nc_From.Points.Count-1)
w1 = nc_From.Points.GetWeight(nc_From.Points.Count-2)
w2 = nc_From.Points.GetWeight(nc_From.Points.Count-3)
else:
w0 = nc_From.Points.GetWeight(0)
w1 = nc_From.Points.GetWeight(1)
w2 = nc_From.Points.GetWeight(2)
m = w1**2 / (w0 * w2)
scale *= m
return scale
def createCurve(nc_From, iDeg_To, bConserveT1End, bDebug=False):
"""
Parameters:
nc_From: rg.NurbsCurve
iDeg_To: int of target degree
bConserveT1End: True for T1, False for T0
bDebug: bool
Returns on success: rg.NurbsCurve
Returns on fail: None
"""
if not isinstance(nc_From, rg.NurbsCurve): return
nc_In = nc_From
# Periodic curves are not supported.
if nc_In.IsPeriodic: return
pts_New = []
scale = getScaleFactorForDegreeChange(nc_In, bConserveT1End, iDeg_To) if iDeg_To > 1 else 1.0
if not bConserveT1End:
# Don't include points on T1 end.
pts_New.append(nc_In.Points[0].Location)
xform = rg.Transform.Scale(pts_New[0], scale)
for i in range(1, iDeg_To+1):
pt_New = rg.Point3d(nc_In.Points[i].Location)
pt_New.Transform(xform)
pts_New.append(pt_New)
else:
# Don't include points on T0 end.
pt_Anchor = nc_In.Points[nc_In.Points.Count-1].Location
xform = rg.Transform.Scale(pt_Anchor, scale)
for i in range(nc_In.Points.Count-iDeg_To-1, nc_In.Points.Count-1):
pt_New = rg.Point3d(nc_In.Points[i].Location)
pt_New.Transform(rg.Transform.Scale(pt_Anchor, scale))
pts_New.append(pt_New)
pts_New.append(pt_Anchor)
if bDebug:
for i in range(len(pts_New)):
pt = pts_New[i]
rgDot = rg.TextDot("{}".format(i), pt)
rgDot.FontHeight = 11
sc.doc.Objects.AddTextDot(rgDot)
sc.doc.Views.Redraw()
nc_Out = rg.NurbsCurve.Create(
periodic=False,
degree=iDeg_To,
points=pts_New)
return nc_Out
def processCurveObject(objref_In, **kwargs):
"""
"""
def getOpt(key): return kwargs[key] if key in kwargs else Opts.values[key]
iDegree = getOpt('iDegree')
bConservePickedEnd = getOpt('bConservePickedEnd')
bReplace = getOpt('bReplace')
bEcho = getOpt('bEcho')
bDebug = getOpt('bDebug')
rgC_In = objref_In.Curve()
if isinstance(rgC_In, rg.BrepEdge):
nc_In = rgC_In.ToNurbsCurve()
elif isinstance(rgC_In, rg.NurbsCurve):
nc_In = rgC_In
elif isinstance(rgC_In, rg.PolyCurve):
nc_In = rgC_In.ToNurbsCurve()
else:
return
bSuccess, t_AtPicked = nc_In.ClosestPoint(objref_In.SelectionPoint())
if not bSuccess:
return
bPickedEndIsT1 = t_AtPicked > nc_In.Domain.Mid
rgT_Sel = objref_In.Trim()
bConserveT1End = bPickedEndIsT1 == bConservePickedEnd
nc_Res = createCurve(
nc_From=nc_In,
bConserveT1End=bConserveT1End,
iDeg_To=iDegree,
bDebug=bDebug,
)
if nc_Res is None:
print "Curve could not be created."
return
if not bReplace or objref_In.Edge():
gC_Out = sc.doc.Objects.AddCurve(nc_Res)
if gC_Out == gC_Out.Empty:
print "Could not add curve."
else:
print "Curve was added."
else:
if sc.doc.Objects.Replace(objref_In.ObjectId, nc_Res):
gC_Out = objref_In.ObjectId
print "Replaced curve."
else:
print "Could not replace curve."
return gC_Out
def main():
rc = getInput()
if rc is None: return
(
objref_In,
iDegree,
bConservePickedEnd,
bReplace,
bEcho,
bDebug,
) = rc
if not bDebug: sc.doc.Views.RedrawEnabled = False
sc.doc.Objects.UnselectAll()
gC_Res = processCurveObject(
objref_In=objref_In,
iDegree=iDegree,
bConservePickedEnd=bConservePickedEnd,
bReplace=bReplace,
bEcho=bEcho,
bDebug=bDebug,
)
if gC_Res is None:
return
sc.doc.Views.RedrawEnabled = True
if __name__ == '__main__': main()