-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathspb_Brep_shortEdges.py
More file actions
353 lines (266 loc) · 10.5 KB
/
spb_Brep_shortEdges.py
File metadata and controls
353 lines (266 loc) · 10.5 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
"""
Send any questions, comments, or script development service needs to
@spb on the McNeel Forums, https://discourse.mcneel.com/
"""
#! python 2 Must be on a line number less than 32.
from __future__ import absolute_import, division, print_function, unicode_literals
"""
221114: Created.
221118: Bug fix: Added a View.Redraw.
221119: Added option to dot edges.
221204: Bug fix: Now dots are added to current layer.
251001: Modified an option default value.
"""
import Rhino.DocObjects as rd
import Rhino.Geometry as rg
import Rhino.Input as ri
import scriptcontext as sc
from System.Drawing import Color
class Opts:
keys = []
values = {}
names = {}
riOpts = {}
listValues = {}
stickyKeys = {}
key = 'fMaxLength'; keys.append(key)
values[key] = 0.1 * sc.doc.ModelAbsoluteTolerance #1e-4
riOpts[key] = ri.Custom.OptionDouble(values[key])
stickyKeys[key] = '{}({})({})'.format(key, __file__, sc.doc.Name)
key = 'bShortestOnly'; keys.append(key)
values[key] = False
names[key] = 'Mark'
riOpts[key] = ri.Custom.OptionToggle(values[key], 'All', 'Shortest')
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bDupCrv'; keys.append(key)
values[key] = False
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bAddDot'; keys.append(key)
values[key] = True
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bIncludeIndex'; keys.append(key)
values[key] = False
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'iDotHt'; keys.append(key)
values[key] = 11
riOpts[key] = ri.Custom.OptionInteger(values[key], setLowerLimit=True, limit=3)
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:
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 == 'fMaxLength':
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 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 getAllNormalBreps():
oes = rd.ObjectEnumeratorSettings()
oes.NormalObjects = True
oes.LockedObjects = False # Default is True.
oes.IncludeLights = False
oes.IncludeGrips = False
oes.ObjectTypeFilter = rd.ObjectType.Brep
return list(sc.doc.Objects.GetObjectList(oes))
def getInput():
"""
"""
go = ri.Custom.GetObject()
go.SetCommandPrompt("Select breps")
go.SetCommandPromptDefault("All normal when none are selected")
go.GeometryFilter = rd.ObjectType.Brep
go.SubObjectSelect = False
go.AcceptNothing(True)
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('fMaxLength')
addOption('bShortestOnly')
addOption('bDupCrv')
addOption('bAddDot')
if Opts.values['bAddDot']:
addOption('bIncludeIndex')
addOption('iDotHt')
addOption('bEcho')
addOption('bDebug')
res = go.GetMultiple(minimumNumber=1, maximumNumber=0)
if res == ri.GetResult.Cancel:
go.Dispose()
return
if res == ri.GetResult.Nothing:
go.Dispose()
return getAllNormalBreps()
if res == ri.GetResult.Object:
rdObjs = [o.Object() for o in go.Objects()]
go.Dispose()
return rdObjs
if res == ri.GetResult.Number:
key = 'fMaxLength'
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, iPrecision=None):
if iPrecision is None:
iPrecision = sc.doc.ModelDistanceDisplayPrecision
if fDistance is None:
return "(No value provided)"
if fDistance == 0.0:
return "0"
if fDistance < 0.01:
return "{:.2e}".format(fDistance)
return "{:.{}g}".format(fDistance, iPrecision)
def main():
rdBs_In = getInput()
if rdBs_In is None: return
fMaxLength = Opts.values['fMaxLength']
bShortestOnly = Opts.values['bShortestOnly']
bDupCrv = Opts.values['bDupCrv']
bAddDot = Opts.values['bAddDot']
bIncludeIndex = Opts.values['bIncludeIndex']
iDotHt = Opts.values['iDotHt']
bEcho = Opts.values['bEcho']
bDebug = Opts.values['bDebug']
fLengths_Short = []
gBs_WithShort = []
idxEs_Short = []
fMinLength_All = float("inf")
bReportEveryBrep = False
if 1 <= len(rdBs_In) <= 10:
bReportEveryBrep = True
elif 10 < len(rdBs_In):
s = "More than 10 breps are selected,"
s += " so only maximum of all edge tolerances,"
s += " is reported."
print(s)
else:
return
gCrvs_Out = []
gDots_Out = []
if bAddDot:
attrib_Dot = rd.ObjectAttributes()
attrib_Dot.ColorSource = rd.ObjectColorSource.ColorFromObject
attrib_Dot.ObjectColor = Color.Red
attrib_Dot.LayerIndex = sc.doc.Layers.CurrentLayerIndex
epsilon_Shortest = 0.5 * 10.0**-sc.doc.ModelDistanceDisplayPrecision
print("TODO: determine epsilon for shortest. Now is {}.".format(epsilon_Shortest))
for iB, rdB in enumerate(rdBs_In):
gB = rdB.Id
rgB = rdB.BrepGeometry
for rgE in rgB.Edges:
fLength = rgE.GetLength()
if fLength > fMaxLength: continue
fLengths_Short.append(fLength)
gBs_WithShort.append(gB)
idxEs_Short.append([rgE.EdgeIndex])
if fLength < fMinLength_All:
fMinLength_All = fLength
if bShortestOnly:
continue # Shortest will be marked after for loops.
if bDupCrv:
gCrvs_Out.append(sc.doc.Objects.AddCurve(rgE))
if bAddDot:
text = formatDistance(fLength)
if bIncludeIndex:
text = "e[{}]:".format(rgE.EdgeIndex) + text
location = rgE.PointAt(rgE.Domain.Mid)
rgDot = rg.TextDot(text, location)
rgDot.FontHeight = iDotHt
gDots_Out.append(sc.doc.Objects.AddTextDot(rgDot, attrib_Dot))
if not fLengths_Short:
print("No edge lengths <= {} found.".format(formatDistance(fMaxLength)))
return
print("Found {} edges in {} breps with lengths [{},{}].".format(
len(idxEs_Short),
len(set(gBs_WithShort)),
formatDistance(min(fLengths_Short)),
formatDistance(max(fLengths_Short))))
if not bShortestOnly:
sc.doc.Views.Redraw()
return
# Find edges closest to maximum.
fLengths_Shortest = []
gBs_WithShortest = []
idxEs_Shortest = []
for fLength, gB, idxE in zip(fLengths_Short, gBs_WithShort, idxEs_Short):
if (fLength - fMinLength_All) < epsilon_Shortest:
fLengths_Shortest.append(fLength)
gBs_WithShortest.append(gBs_WithShortest)
idxEs_Shortest.append(idxEs_Shortest)
if bDupCrv:
gCrvs_Out.append(sc.doc.Objects.AddCurve(rgE))
if bAddDot:
text = formatDistance(fLength)
if bIncludeIndex:
text = "e[{}]:".format(rgE.EdgeIndex) + text
location = rgE.PointAt(rgE.Domain.Mid)
rgDot = rg.TextDot(text, location)
rgDot.FontHeight = iDotHt
gDots_Out.append(sc.doc.Objects.AddTextDot(rgDot, attrib_Dot))
print("Found {} edges in {} breps within {} of {}.".format(
len(idxEs_Shortest),
len(set(gBs_WithShortest)),
formatDistance(epsilon_Shortest),
formatDistance(fLengths_Shortest)))
if bDupCrv:
print("{} curves added.".format(len(gDots_Out)))
if bAddDot:
print("{} dots added.".format(len(gDots_Out)))
sc.doc.Views.Redraw()
if __name__ == '__main__': main()