-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathspb_Brep_findByFaceCount.py
More file actions
255 lines (191 loc) · 7.55 KB
/
spb_Brep_findByFaceCount.py
File metadata and controls
255 lines (191 loc) · 7.55 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
"""
"""
#! python 2 Must be on a line number less than 32.
from __future__ import absolute_import, division, print_function, unicode_literals
"""
180608: Created.
220707,09: Added option to use selected breps for face count instead of entering the value.
250604: Bug fixes.
"""
import Rhino.DocObjects as rd
import Rhino.Input as ri
import rhinoscriptsyntax as rs
import scriptcontext as sc
class Opts:
keys = []
values = {}
names = {}
riOpts = {}
listValues = {}
stickyKeys = {}
key = 'iTargetCt'; keys.append(key)
values[key] = 2
riOpts[key] = ri.Custom.OptionInteger(values[key])
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'iCtTol'; keys.append(key)
values[key] = 0
riOpts[key] = ri.Custom.OptionInteger(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:
# 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])
if not idxOpt: print("Add option for {} failed.".format(key))
return idxOpt
@classmethod
def setValue(cls, key, idxList=None):
if key == 'iTargetCt':
if cls.riOpts[key].CurrentValue < 1:
cls.riOpts[key].CurrentValue = cls.values[key] = cls.riOpts[key].InitialValue
else:
cls.values[key] = cls.riOpts[key].CurrentValue
elif key == 'iCtTol':
if cls.riOpts[key].CurrentValue < 0:
cls.riOpts[key].CurrentValue = cls.values[key] = cls.riOpts[key].InitialValue
else:
cls.values[key] = cls.riOpts[key].CurrentValue
else:
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_FaceCounts():
"""
Get open breps with optional input.
"""
go = ri.Custom.GetObject()
go.SetCommandPrompt("Optionally select breps to use their face counts")
go.GeometryFilter = rd.ObjectType.Brep
go.SubObjectSelect = False
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)
go.AcceptNothing(True)
go.AcceptNumber(True, acceptZero=True)
bPreselectedObjsChecked = False
idxs_Opt = {}
while True:
go.ClearCommandOptions()
idxs_Opt.clear()
go.SetCommandPromptDefault("{}".format(Opts.values['iTargetCt']))
def addOption(key): idxs_Opt[key] = Opts.addOption(go, key)
addOption('iTargetCt')
addOption('iCtTol')
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, True)
continue
if res == ri.GetResult.Cancel:
go.Dispose()
return
if res == ri.GetResult.Nothing:
go.Dispose()
return [Opts.values['iTargetCt']]
if res == ri.GetResult.Object:
objrefs = go.Objects()
go.Dispose()
iCts_NoTol = sorted(set([o.Brep().Faces.Count for o in objrefs]))
if len(iCts_NoTol) == 1:
Opts.setValue('iTargetCt')
iCtTol = Opts.values['iCtTol']
if iCtTol == 0:
return iCts_NoTol
iCts_WithTol = []
for iCt in iCts_NoTol:
for iTol in range(-iCtTol, iCtTol+1):
iCts_WithTol.append(iCt + iTol)
return sorted(set(iCts_WithTol))
if res == ri.GetResult.Number:
if Opts.values['bLimitDev']:
key = 'fDevTol'
Opts.riOpts[key].CurrentValue = go.Number()
else:
key = 'iMinTargetGContinuity'
Opts.riOpts[key].CurrentValue = int(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 main():
for gObj in rs.NormalObjects():
if rs.ObjectType(gObj) in (8,16): break
else:
print("No breps are selectable.")
return
iFaceCts_Target = getInput_FaceCounts()
if iFaceCts_Target is None: return
gBreps = []
iCts_Faces = []
for gObj in rs.NormalObjects():
if rs.ObjectType(gObj) not in (8,16): continue
gBreps.append(gObj)
brep = rs.coercebrep(gObj)
iCts_Faces.append(brep.Faces.Count)
if len(gBreps) == 0:
print("No breps are selectable.")
return
iCtTol = Opts.values['iCtTol']
idxs_Found = []
for idxB, iCt in enumerate(iCts_Faces):
for iFaceCt_Target in iFaceCts_Target:
if (iFaceCt_Target - iCtTol) <= iCt <= (iFaceCt_Target + iCtTol):
idxs_Found.append(idxB)
break
#idxs_Found = [i for i, v in enumerate(iCts_Faces) if v in iFaceCts_Target]
sc.doc.Objects.UnselectAll()
iCts_Faces_Found = []
for i in idxs_Found:
sc.doc.Objects.Select(gBreps[i])
iCts_Faces_Found.append(iCts_Faces[i])
nSelected = len(list(
sc.doc.Objects.GetSelectedObjects(
includeLights=False, includeGrips=False)))
print("{} breps with {} faces are selected.".format(nSelected, iCts_Faces_Found))
sc.doc.Views.Redraw()
if __name__ == '__main__': main()