-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathspb_Crv_Fillet_BestFit.py
More file actions
385 lines (275 loc) · 12 KB
/
spb_Crv_Fillet_BestFit.py
File metadata and controls
385 lines (275 loc) · 12 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
"""
This script will find the best-fit fillet given a reference curve to replace and
the 2 curves to fillet.
"""
#! python 2 Must be on a line less than 32.
from __future__ import absolute_import, division, print_function, unicode_literals
"""
230902-06: Created.
231004: Output points are selected to immediately be used for trimming the 2 curves to fillet.
231024: Bug fix in determining default resolution. Improved printed output.
250307: Bug fix in a reported failure.
TODO:
Add option whether to trim the curves to the fillet.
Add option whether to add end points of fillet.
Handle bad input such as a linear curve for curve to replace.
Replace brute force finder with more robust one.
"""
import Rhino
import Rhino.DocObjects as rd
import Rhino.Geometry as rg
import Rhino.Input as ri
import scriptcontext as sc
import math
class Opts():
keys = []
values = {}
names = {}
riOpts = {}
listValues = {}
stickyKeys = {}
key = 'fResolution'; keys.append(key)
values[key] = 10**(-int(abs(math.log10(sc.doc.ModelAbsoluteTolerance)))) # Tol. of 0.01 -> Res. of 0.1; 0.0004 -> 0.001; etc.
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]
else:
idxOpt = go.AddOptionList(
englishOptionName=cls.names[key],
listValues=cls.listValues[key],
listCurrentIndex=cls.values[key])
return idxOpt
@classmethod
def setValue(cls, key, idxList=None):
if key == 'fResolution':
if cls.riOpts[key].CurrentValue < 0.0:
cls.riOpts[key].CurrentValue = cls.riOpts[key].InitialValue
elif cls.riOpts[key].CurrentValue < 1e-6:
cls.riOpts[key].CurrentValue = 0.0
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_Crv_to_replace():
"""
Get curve with optional input.
"""
go = ri.Custom.GetObject()
go.SetCommandPrompt("Select curve to approximate with fillet")
go.GeometryFilter = Rhino.DocObjects.ObjectType.Curve
#go.GeometryAttributeFilter = ri.Custom.GeometryAttributeFilter.WireCurve
def customGeometryFilter(rdObj, rgObj, compIdx):
return not rg.Curve.IsLinear(rgObj)
go.SetCustomGeometryFilter(customGeometryFilter)
go.AcceptNumber(True, acceptZero=True)
idxs_Opt = {}
def addOption(key): idxs_Opt[key] = Opts.addOption(go, key)
while True:
go.ClearCommandOptions()
idxs_Opt.clear()
addOption('fResolution')
addOption('bEcho')
addOption('bDebug')
res = go.Get()
if res == ri.GetResult.Cancel:
go.Dispose()
return
if res == ri.GetResult.Object:
objref = go.Object(0)
go.Dispose()
return objref
if res == ri.GetResult.Number:
key = 'fResolution'
Opts.riOpts[key].CurrentValue = go.Number()
Opts.setValue(key)
continue
for key in idxs_Opt:
if go.Option().Index == idxs_Opt[key]:
Opts.setValue(key, go.Option().CurrentListOptionIndex)
break
def getInput_Crvs_to_fillet():
"""
Get curve with optional input.
"""
go = ri.Custom.GetObject()
go.SetCommandPrompt("Select 2 curves to fillet")
go.GeometryFilter = Rhino.DocObjects.ObjectType.Curve
#go.GeometryAttributeFilter = ri.Custom.GeometryAttributeFilter.WireCurve
#def customGeometryFilter(rdObj, rgObj, compIdx):
# return rdObj.Id != gCrv_1st
#go.SetCustomGeometryFilter(customGeometryFilter)
go.OneByOnePostSelect = True
go.DisablePreSelect()
idxs_Opt = {}
def addOption(key): idxs_Opt[key] = Opts.addOption(go, key)
while True:
go.ClearCommandOptions()
idxs_Opt.clear()
addOption('bEcho')
addOption('bDebug')
res = go.GetMultiple(minimumNumber=2, maximumNumber=2)
if res == ri.GetResult.Cancel:
go.Dispose()
return
if res == ri.GetResult.Object:
objrefs = go.Objects()
go.Dispose()
return objrefs
for key in idxs_Opt:
if go.Option().Index == idxs_Opt[key]:
Opts.setValue(key, go.Option().CurrentListOptionIndex)
break
def _formatDistance(fDistance, iPrecision=15):
if fDistance is None: return "(No deviation provided)"
if fDistance < 1e-4:
return "{:.{}e}".format(fDistance, iPrecision)
#if fDistance < 0.1:
# return "{:.{}g}".format(fDistance, iPrecision)
return "{:.{}g}".format(fDistance, iPrecision)
def _createArcCurveOfFillet(curve0, curve1, radius, t0Base, t1Base):
arc = rg.Curve.CreateFillet(curve0, curve1, radius, t0Base, t1Base)
if not arc.IsValid:
raise Exception("CreateFillet failed at fillet R{}. _Connect curves to each other and try again.".format(radius))
#sEval = "arc.Length"; print("{}: {}".format(sEval, eval(sEval)))
#sEval = "arc.IsValid"; print("{}: {}".format(sEval, eval(sEval)))
return rg.ArcCurve(arc)
def findFillet(crv_Ref, crv_A_ToFillet, crv_B_ToFillet, tA, tB, fResolution, bEcho=True, bDebug=False):
"""
"""
def _getDistancesBetweenCurves(curveA, curveB):
rc = rg.Curve.GetDistancesBetweenCurves(curveA, curveB, tolerance=0.1*sc.doc.ModelAbsoluteTolerance)
if not rc[0]:
sc.doc.Objects.AddCurve(curveB)
raise Exception("GetDistancesBetweenCurves failed at fillet R{}. _Connect curves to each other and try again.".format(fRadius_Start))
return rc[1]
fRadius_Raw_Start = 1.0/rg.Curve.CurvatureAt(crv_Ref, crv_Ref.Domain.Mid).Length
if bDebug:
print("Raw radius start: {}".format(fRadius_Raw_Start))
m = round(1.0/fResolution, 0)
fRadius_Start = round(m * fRadius_Raw_Start, 0) / m
cA = crv_A_ToFillet
cB = crv_B_ToFillet
arccrv_Start = _createArcCurveOfFillet(cA, cB, fRadius_Start, tA, tB)
dev_Start = _getDistancesBetweenCurves(crv_Ref, arccrv_Start)
if bDebug:
sEval = "fRadius_Start"; print("{}: {}".format(sEval, eval(sEval)))
sEval = "dev_Start"; print("{}: {}".format(sEval, eval(sEval)))
# Determine whether the target radius is >, <, or equal to the start radius.
fRadius_Plus = fRadius_Start + fResolution
arccrv_Plus = _createArcCurveOfFillet(cA, cB, fRadius_Plus, tA, tB)
dev_Plus = _getDistancesBetweenCurves(crv_Ref, arccrv_Plus)
fRadius_Minus = fRadius_Start - fResolution
arccrv_Minus = _createArcCurveOfFillet(cA, cB, fRadius_Minus, tA, tB)
dev_Minus = _getDistancesBetweenCurves(crv_Ref, arccrv_Minus)
if dev_Start < dev_Plus and dev_Start < dev_Minus:
if bEcho: print("Start radius is the best radius.")
return fRadius_Start, arccrv_Start, dev_Start
if dev_Plus < dev_Start and dev_Plus < dev_Minus:
if bEcho: print("Best radius is greater than the start radius.")
fResolution_Signed = +fResolution
fRadius_ToTry = fRadius_Plus
arccrv_ToTry = arccrv_Plus
dev_ToTry = dev_Plus
elif dev_Minus < dev_Start and dev_Minus < dev_Plus:
if bEcho: print("Best radius is less than the start radius.")
fResolution_Signed = -fResolution
fRadius_ToTry = fRadius_Minus
arccrv_ToTry = arccrv_Minus
dev_ToTry = dev_Minus
else:
sEval = "dev_Minus"; print("{}: {}".format(sEval, eval(sEval)))
sEval = "dev_Start"; print("{}: {}".format(sEval, eval(sEval)))
sEval = "dev_Plus"; print("{}: {}".format(sEval, eval(sEval)))
raise Exception("What happened?")
# This is just to accomodate entering the while loop.
arccrv_Best = arccrv_Start
dev_Best = dev_Start
iCt_While = 0
while dev_ToTry < dev_Best:
iCt_While += 1
arccrv_Best.Dispose()
arccrv_Best = arccrv_ToTry
fRadius_Best = fRadius_ToTry
dev_Best = dev_ToTry
fRadius_ToTry += fResolution_Signed
arccrv_ToTry = _createArcCurveOfFillet(cA, cB, fRadius_ToTry, tA, tB)
dev_ToTry = _getDistancesBetweenCurves(crv_Ref, arccrv_ToTry)
if bDebug:
sEval = "fRadius_ToTry"; print("{}: {}".format(sEval, eval(sEval)))
sEval = "dev_ToTry"; print("{}: {}".format(sEval, eval(sEval)))
print("After {} iterations of while loop.".format(iCt_While))
return fRadius_Best, arccrv_Best, dev_Best
def main():
objref_Ref = getInput_Crv_to_replace()
if objref_Ref is None: return
objrefs_ToFillet = getInput_Crvs_to_fillet()
if objrefs_ToFillet is None: return
fResolution = Opts.values['fResolution']
bEcho = Opts.values['bEcho']
bDebug = Opts.values['bDebug']
sc.doc.Objects.UnselectAll()
if not bDebug: sc.doc.Views.RedrawEnabled = False
crv_Ref = objref_Ref.Curve()
# crv_A_ToFillet = objrefs_ToFillet[0].Curve()
# crv_B_ToFillet = objrefs_ToFillet[1].Curve()
crv_A_ToFillet, tA = rd.ObjRef.CurveParameter(objrefs_ToFillet[0])
crv_B_ToFillet, tB = rd.ObjRef.CurveParameter(objrefs_ToFillet[1])
Rhino.RhinoApp.SetCommandPrompt("Working ...")
rc = findFillet(
crv_Ref=crv_Ref,
crv_A_ToFillet=crv_A_ToFillet,
crv_B_ToFillet=crv_B_ToFillet,
tA=tA,
tB=tB,
fResolution=fResolution,
bEcho=bEcho,
bDebug=bDebug,
)
if rc is not None:
fRadius_Winner, fArcCrve_Winner, fDev_Winner = rc
print("Best fillet at {} resolution is R{}".format(fResolution, fRadius_Winner))
print("Deviation from reference curve: {}".format(
_formatDistance(fDev_Winner, iPrecision=max((6, sc.doc.ModelDistanceDisplayPrecision)))))
sc.doc.Objects.AddCurve(fArcCrve_Winner)
sc.doc.Objects.Select(sc.doc.Objects.AddPoint(fArcCrve_Winner.PointAtStart))
sc.doc.Objects.Select(sc.doc.Objects.AddPoint(fArcCrve_Winner.PointAtEnd))
print("{} points added and are selected.".format(
len(list(sc.doc.Objects.GetSelectedObjects(includeLights=False, includeGrips=False)))))
sc.doc.Views.RedrawEnabled = True
if __name__ == '__main__': main()