Repository navigation
Expand file tree
/
Copy pathspb_Crv_ArcRefGeom.py
More file actions
297 lines (224 loc) · 8.83 KB
/
Copy pathspb_Crv_ArcRefGeom.py
File metadata and controls
297 lines (224 loc) · 8.83 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
"""
"""
#! python 2 Must be on a line less than 32.
from __future__ import absolute_import, division, print_function, unicode_literals
"""
250312-13: Created.
TODO:
WIP: Only allow arc-shaped curves to be selected.
"""
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 = 'fScaleFactor'; keys.append(key)
#values[key] = 2.0
#names[key] = 'ScaleFactor'
#riOpts[key] = ri.Custom.OptionDouble(
# initialValue=values[key],
# setLowerLimit=True,
# limit=Rhino.RhinoMath.SqrtEpsilon
#)
#stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'fTol_IsArc'; keys.append(key)
values[key] = 1e-6 * Rhino.RhinoMath.UnitScale(
Rhino.UnitSystem.Millimeters, sc.doc.ModelUnitSystem)
names[key] = 'IsArcTol'
riOpts[key] = ri.Custom.OptionDouble(values[key], setLowerLimit=True,
limit=Rhino.RhinoMath.ZeroTolerance)
stickyKeys[key] = '{}({})({})'.format(key, __file__, sc.doc.ModelUnitSystem)
key = 'bAddArcIfInputIsNotArcCrv'; 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 == 'fSearchTol':
# if cls.riOpts[key].CurrentValue < 0.0:
# cls.riOpts[key].CurrentValue = cls.values[key] = cls.riOpts[key].InitialValue
# sc.sticky[cls.stickyKeys[key]] = cls.values[key]
# return
# sc.sticky[cls.stickyKeys[key]] = cls.values[key] = cls.riOpts[key].CurrentValue
# return
if key == 'fTol_IsArc':
if cls.riOpts[key].CurrentValue < 0.0:
cls.riOpts[key].CurrentValue = cls.riOpts[key].InitialValue
elif cls.riOpts[key].CurrentValue < cls.riOpts[key].InitialValue:
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
print("Invalid key?")
def getInput(bDebug=False):
"""
Get linear curves.
"""
go = ri.Custom.GetObject()
go.SetCommandPrompt("Select arc-shaped curves")
go.GeometryFilter = Rhino.DocObjects.ObjectType.Curve
#go.GeometryAttributeFilter = (
# ri.Custom.GeometryAttributeFilter.
#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('fScaleFactor')
addOption('fTol_IsArc')
addOption('bAddArcIfInputIsNotArcCrv')
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_IsArc'
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 _formatDistance(fDistance, fPrecision=None):
if fDistance is None:
return "(None)"
if fDistance == Rhino.RhinoMath.UnsetValue:
return "(Infinite)"
if fPrecision is None:
fPrecision = sc.doc.ModelDistanceDisplayPrecision
if fDistance < 10.0**(-(fPrecision-1)):
# For example, if fDistance is 1e-5 and fPrecision == 5,
# the end of this script would display only one digit.
# Instead, this return displays 2 digits.
return "{:.2e}".format(fDistance)
return "{:.{}f}".format(fDistance, fPrecision)
def _formatRadius(radius):
if sc.doc.ModelUnitSystem == Rhino.UnitSystem.Millimeters:
return _formatDistance(radius)
return "{} {} [{} millimeters]".format(
_formatDistance(radius),
sc.doc.GetUnitSystemName(
modelUnits=True,
capitalize=False,
singular=False,
abbreviate=False),
_formatDistance(radius*Rhino.RhinoMath.UnitScale(
sc.doc.ModelUnitSystem, Rhino.UnitSystem.Millimeters),
fPrecision=4)
)
def main():
objrefs = getInput()
if objrefs is None: return
#fScaleFactor = Opts.values['fScaleFactor']
fTol_IsArc = Opts.values['fTol_IsArc']
bAddArcIfInputIsNotArcCrv = Opts.values['bAddArcIfInputIsNotArcCrv']
bEcho = Opts.values['bEcho']
bDebug = Opts.values['bDebug']
if not bDebug: sc.doc.Views.RedrawEnabled = False
radii = []
for objref in objrefs:
rgC_In = objref.Curve()
if rgC_In is None:
continue
bSuccess, arc = rgC_In.TryGetArc(tolerance=fTol_IsArc)
if not bSuccess:
continue
if isinstance(rgC_In, rg.BrepEdge):
bIsEdge = True
rgC_In_NotProxy = rgC_In.DuplicateCurve()
else:
bIsEdge = False
rgC_In_NotProxy = rgC_In
if isinstance(rgC_In_NotProxy, rg.ArcCurve):
bIsArcCrv = True
arc = rgC_In_NotProxy.Arc
else:
bIsArcCrv = False
radius = arc.Radius
radii.append(radius)
plane = arc.Plane
ps = rg.PlaneSurface(
plane,
xExtents=rg.Interval(-radius, radius),
yExtents=rg.Interval(-radius, radius))
line = rg.Line(plane.Origin, span=arc.Radius*plane.Normal)
sc.doc.Objects.AddSurface(ps)
sc.doc.Objects.AddLine(line)
if bAddArcIfInputIsNotArcCrv and not bIsArcCrv:
sc.doc.Objects.AddArc(arc)
if bEcho:
if len(objrefs) == 1:
if len(radii) == 0:
print("Curve is not arc-shaped.")
else:
print("Curve's radius is {}.".format(_formatRadius(radius)))
else:
if len(radii) == 0:
print("None of the curves are arc-shaped.")
elif len(objrefs) > 10:
print("Arc-shaped curves found.")
else:
print("Curves' radii: {}".format(
", ".join([_formatRadius(radius) for radius in radii])))
sc.doc.Views.RedrawEnabled = True
if __name__ == '__main__': main()