-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathspb_Block_RenameBlockObjectsToBlockName.py
More file actions
318 lines (250 loc) · 10.3 KB
/
spb_Block_RenameBlockObjectsToBlockName.py
File metadata and controls
318 lines (250 loc) · 10.3 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
"""
"""
#! python 2 Must be on a line number less than 32.
from __future__ import absolute_import, division, print_function, unicode_literals
"""
151111-12: Created.
180830: Various code refactoring.
181204: Options added whether to name Block ... objects and to name them to Block ... container blocks.
250815: Refactored and added some options.
"""
import Rhino
import Rhino.DocObjects as rd
import Rhino.Geometry as rg
import Rhino.Input as ri
import rhinoscriptsyntax as rs
import scriptcontext as sc
_sBlockBlockNote = "'Block xx' block objects will be renamed to container block of 'Block xx', not to 'Block xx'.\nRename will not occur if container doesn't exist."
class Opts():
keys = []
values = {}
names = {}
riOpts = {}
listValues = {}
stickyKeys = {}
key = 'bProcessAllBlocks'; keys.append(key)
values[key] = False
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bRenameOnlyEmptyOrNone'; keys.append(key)
values[key] = True
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bRenameBlockBlockObjs'; keys.append(key)
values[key] = False
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bVerifyAll'; 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:
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 == 'fRadius':
# if cls.riOpts[key].CurrentValue < 2.0*sc.doc.ModelAbsoluteTolerance:
# cls.riOpts[key].CurrentValue = 0.0
if key in cls.riOpts:
cls.values[key] = cls.riOpts[key].CurrentValue
elif key in cls.listValues:
cls.values[key] = idxList
else:
print("Invalid key?")
return
sc.sticky[cls.stickyKeys[key]] = cls.values[key]
def getInput():
"""
Get block instances with options.
"""
if Opts.values['bProcessAllBlocks']:
go = ri.Custom.GetOption()
go.SetCommandPrompt("Set options")
go.AcceptNothing(True)
else:
go = ri.Custom.GetObject()
go.SetCommandPrompt("Select block instances")
go.GeometryFilter = Rhino.DocObjects.ObjectType.InstanceReference
# go.AcceptNothing(False) is already set.
idxs_Opts = {}
def addOption(key): idxs_Opts[key] = Opts.addOption(go, key)
while True:
go.ClearCommandOptions()
idxs_Opts.clear()
addOption('bProcessAllBlocks')
addOption('bRenameOnlyEmptyOrNone')
addOption('bRenameBlockBlockObjs')
addOption('bVerifyAll')
addOption('bEcho')
addOption('bDebug')
if Opts.values['bProcessAllBlocks']:
res = go.Get()
else:
res = go.GetMultiple(minimumNumber=1, maximumNumber=0)
if res == ri.GetResult.Cancel:
go.Dispose()
return
if res == ri.GetResult.Nothing:
go.Dispose()
return [] # Means 'all block definitions'.
if res == ri.GetResult.Object:
objrefs = go.Objects()
go.Dispose()
return objrefs
if not go.Option():
print("What happened?")
continue
# An option was selected.
#sEval = 'go.Option().Index'; print(sEval,'=',eval(sEval))
for key in idxs_Opts:
if go.Option().Index == idxs_Opts[key]:
Opts.setValue(key, go.Option().CurrentListOptionIndex)
if (
'bProcessAllBlocks' in idxs_Opts and
go.Option().Index == idxs_Opts['bProcessAllBlocks']):
go.Dispose()
if Opts.values['bProcessAllBlocks']:
go = ri.Custom.GetOption()
go.SetCommandPrompt("Set options")
else:
go = ri.Custom.GetObject()
go.SetCommandPrompt("Select block instances")
go.GeometryFilter = Rhino.DocObjects.ObjectType.InstanceReference
elif (
'bRenameBlockBlockObjs' in idxs_Opts and
go.Option().Index == idxs_Opts['bRenameBlockBlockObjs']):
if Opts.values['bRenameBlockBlockObjs']:
print(_sBlockBlockNote)
break
def main():
sBlocks_All = rs.BlockNames(sort=True);
if not sBlocks_All:
print("No blocks in document.")
return
objrefs_Iref = getInput()
if objrefs_Iref is None: return
#sEval = 'objrefs_Iref'; print(sEval,'=',eval(sEval))
bProcessAllBlocks = Opts.values['bProcessAllBlocks']
bRenameOnlyEmptyOrNone = Opts.values['bRenameOnlyEmptyOrNone']
bRenameBlockBlockObjs = Opts.values['bRenameBlockBlockObjs']
bVerifyAll = Opts.values['bVerifyAll']
bEcho = Opts.values['bEcho']
bDebug = Opts.values['bDebug']
if not bDebug: sc.doc.Views.RedrawEnabled = False
#Individual block instance selection to replace contents of sBlocks_All
if bProcessAllBlocks:
sBlocks_ToProcess = sBlocks_All
else:
sBlocks_ToProcess = []
for objref_Iref in objrefs_Iref:
rgIref = objref_Iref.Geometry()
rdIdef = sc.doc.InstanceDefinitions.FindId(rgIref.ParentIdefId)
sBlocks_ToProcess.append(rdIdef.Name)
if bRenameBlockBlockObjs:
print(_sBlockBlockNote)
# Process block definitions.
print("Block name (New object name) ( (Objects renamed) of (Total objects) )")
for sBlock in sBlocks_ToProcess:
if bDebug: print(sBlock,)
if bRenameBlockBlockObjs and sBlock[:6] == "Block " and sBlock[6].isnumeric():
sContainers = rs.BlockContainers(sBlock)
if bDebug: print(sContainers)
ct_Containers = len(sContainers)
if ct_Containers == 0:
print("No container for {}.".format(sBlock))
continue
elif ct_Containers == 1:
sObjName_Out = sContainer
else:
sObjName_Out = None
for sContainer in sContainers:
# Verify that the block def. in sContainers is the immediate container block.
for g in rs.BlockObjects(sContainer):
if (
rs.ObjectType(g) == rs.filter.instance
and
rs.BlockInstanceName(g) == sBlock
):
sObjName_Out = sContainer
break
if sObjName_Out is not None: break
else:
print("No immediate container found for {}!".format(
sBlock))
else:
sObjName_Out = sBlock
nObjsRenamed = 0
gObjs = rs.BlockObjects(sBlock)
if not gObjs:
print("{} has no objects!".format(sBlock))
continue
gAlreadyNamedAsTarget = []
for gObj in gObjs:
sObjName_In = rs.ObjectName(gObj)
if sObjName_In == sObjName_Out:
gAlreadyNamedAsTarget.append(gObj)
continue
if (
not bRenameOnlyEmptyOrNone or
(not sObjName_In or sObjName_In.lower() == "none")
):
if bVerifyAll:
sAnswer = rs.GetString(
"Rename {} to {}?".format(sObjName_In, sObjName_Out),
defaultString="Y",
strings=("Yes", "No"))
if sAnswer is None or sAnswer[0].lower() != 'y':
continue
rs.ObjectName(gObj, sObjName_Out)
sObjName1 = rs.ObjectName(gObj)
if sObjName_In <> sObjName1:
nObjsRenamed += 1
print("{} -> {}".format(sObjName_In, sObjName_Out))
s = "In block def., {}, {} of {} objects were renamed to {}.".format(
sBlock,
nObjsRenamed,
len(gObjs),
sObjName_Out,
)
if gAlreadyNamedAsTarget:
s += " {} objects already have that name.".format(len(gAlreadyNamedAsTarget))
print(s)
if __name__ == '__main__': main()