-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathspb_Block_listDefinitions.py
More file actions
173 lines (139 loc) · 5.78 KB
/
spb_Block_listDefinitions.py
File metadata and controls
173 lines (139 loc) · 5.78 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
"""
"""
#! python 2 Must be on a line number less than 32.
from __future__ import absolute_import, division, print_function, unicode_literals
"""
180528: Created, replacing ListDefinitionNamesOfSelBlockInstances.rvb.
180606: Testing alternative output format.
180620: Added rs.BlockInstanceCount to printed summary.
Changed name from definitionNamesOfSelectedBlockInstances.py to listDefinitionNamesOfSelectedBlockInstances.py
181024: Added check at start for whether there are any blocks in the document.
250421-22: Now reports more useful output for nested counts.
Now, can also report on all of document's blocks instead of a selection.
"""
import Rhino.DocObjects as rd
import Rhino.Input as ri
import rhinoscriptsyntax as rs
import scriptcontext as sc
from clr import StrongBox
def getInput():
# Create options with default values before any are changed with those in sticky.
optT_bCount = ri.Custom.OptionToggle(True, 'No', 'Yes')
# Load sticky.
stickyKeys = (
'bCount({})'.format(__file__),
)
stickyValues = [
optT_bCount.CurrentValue,
] # Default values.
for i, stickyKey in enumerate(stickyKeys):
if sc.sticky.has_key(stickyKey): stickyValues[i] = sc.sticky[stickyKey]
(
optT_bCount.CurrentValue,
) = stickyValues
# Get instance references with optional input.
def addOptions():
go.AddOptionToggle('IncludeQuantity', optT_bCount)
go = ri.Custom.GetObject()
go.SetCommandPrompt("Select block instances")
go.GeometryFilter = rd.ObjectType.InstanceReference
go.AcceptNothing(True)
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)
while True:
go.ClearCommandOptions()
addOptions()
res = go.GetMultiple(minimumNumber=1, maximumNumber=0)
if res == ri.GetResult.Cancel:
go.Dispose()
return
if res == ri.GetResult.Nothing:
go.Dispose()
return [], optT_bCount.CurrentValue
if res == ri.GetResult.Object:
gInsts = [objref.ObjectId for objref in go.Objects()]
go.Dispose()
return (
gInsts,
optT_bCount.CurrentValue,
)
stickyValues = (optT_bCount.CurrentValue,)
for i, stickyKey in enumerate(stickyKeys):
sc.sticky[stickyKey] = stickyValues[i]
def main():
rdIdefs = sc.doc.InstanceDefinitions.GetList(ignoreDeleted=True)
if not rdIdefs:
print("No blocks in document.")
return
rc = getInput()
if rc is None: return
gInsts, bCount = rc
sBlockNames = []
iSelInstCts = []
rdIdefs_Out = []
if len(gInsts) == 0:
sBlockNames = [_.Name for _ in rdIdefs]
iSelInstCts = [0]*len(rdIdefs)
rdIdefs_Out = rdIdefs
else:
for gInst in gInsts:
sDefName = rs.BlockInstanceName(gInst)
if sDefName not in sBlockNames:
sBlockNames.append(sDefName)
iSelInstCts.append(1)
rdIdefs_Out.append(
sc.doc.InstanceDefinitions.Find(
instanceDefinitionName=sDefName,
ignoreDeletedInstanceDefinitions=True))
else:
iSelInstCts[sBlockNames.index(sDefName)] += 1
if not bCount:
for name in sorted(sBlockNames):
print("{}".format(name))
return
try:
rd.InstanceDefinition.UseCount
bRhinoVerIsAtLeast7_4 = True
except:
bRhinoVerIsAtLeast7_4 = False
if bRhinoVerIsAtLeast7_4:
topLevelReferenceCount = StrongBox[int]()
nestedReferenceCount = StrongBox[int]()
for name, rdIdef, qty in sorted(zip(sBlockNames, rdIdefs_Out, iSelInstCts)):
iUseCount_Total = rdIdef.UseCount(topLevelReferenceCount, nestedReferenceCount)
s = ""
if qty:
s += "{} selected of ".format(qty)
s += "{} ({} documentTotal = {} top-level + {} nested".format(
name,
iUseCount_Total,
topLevelReferenceCount,
nestedReferenceCount,
)
if nestedReferenceCount.Value:
rdIrefs = rdIdef.GetReferences(wheretoLook=2)
sDefNames = [_.InstanceDefinition.Name for _ in rdIrefs]
s += " in {} definitions".format(len(rdIdef.GetContainers()))
s += ")"
print(s)
else:
for name, rdIdef, qty in sorted(zip(sBlockNames, rdIdefs_Out, iSelInstCts)):
print(" Meaning of values in parentheses, ( (top level refs in active doc), (top level and nested refs in active doc), (refs from other inst defs) )")
s = ""
if qty:
s += "{} selected of ".format(qty)
s += "{} ({}, {}, {})".format(
name,
rs.BlockInstanceCount(block_name=name, where_to_look=0),
rs.BlockInstanceCount(block_name=name, where_to_look=1),
rs.BlockInstanceCount(block_name=name, where_to_look=2),
)
rdIrefs = rdIdef.GetReferences(wheretoLook=2)
sDefNames = [_.InstanceDefinition.Name for _ in rdIrefs]
s += " in {} other definitions".format(len(rdIdef.GetContainers()))
s += ")"
print(s)
if __name__ == '__main__': main()