-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfamilies.py
414 lines (341 loc) · 14.1 KB
/
families.py
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
from collections import defaultdict
import csv
import os
from Autodesk.Revit.DB import BuiltInParameter
from Autodesk.Revit.DB import BuiltInCategory
from Autodesk.Revit.DB import Family
from Autodesk.Revit.DB import FamilyInstance
from Autodesk.Revit.DB import FamilySymbol
from Autodesk.Revit.DB import FilteredElementCollector
from api import *
from lib import transaction
from lib import get_temp_path
import constants
def report_corrupt_families(doc):
"""Generate CSV of all loaded families and their corrupted status"""
families = FilteredElementCollector(doc).OfClass(Family)
rows = []
category = raw_input('Enter category: ').strip()
for family in families:
if not family.IsEditable or family.IsInPlace:
continue
if category and family.FamilyCategory.Name != category:
continue
try:
fam_doc = doc.EditFamily(family)
fam_doc.Close(False)
rows.append((family.FamilyCategory.Name, family.Name, True))
print('{} - OK'.format(family.Name))
except Exception:
print('{} - Corrupt'.format(family.Name))
rows.append((family.FamilyCategory.Name, family.Name, False))
csv_path = get_temp_path('corrupt.csv')
with open(csv_path, 'w') as f:
w = csv.writer(f, lineterminator='\n')
w.writerow(('Category', 'Family', 'OK'))
w.writerows(rows)
os.startfile(csv_path, 'open')
def fix_duplicate_marks(doc):
"""Find elements with duplicate marks and rename"""
mark_map = defaultdict(list)
instances = FilteredElementCollector(doc).OfClass(FamilyInstance)
for instance in instances:
mark = instance.get_Parameter(BuiltInParameter.ALL_MODEL_MARK)
if mark:
mark_map[mark.AsString()].append(instance.Id)
with transaction(doc, 'Fix duplicate marks'):
for mark, ids in mark_map.items():
if len(ids) < 2:
continue
if not mark:
continue
for i, id in enumerate(ids[1:], 1):
new_mark = '{}-{}'.format(mark, i)
instance = doc.GetElement(id)
instance.get_Parameter(BuiltInParameter.ALL_MODEL_MARK).Set(new_mark)
print(new_mark)
def rename_single_types(doc, type_name='Default'):
"""Rename single types"""
families = FilteredElementCollector(doc).OfClass(Family)
with transaction(doc, 'Rename types'):
for family in families:
symbols = list(family.GetFamilySymbolIds())
if len(symbols) == 1:
symbol = doc.GetElement(symbols[0])
symbol.Name = type_name
def rename_families(doc):
acronyms = ['MAT', 'SSTN', 'OR', 'SC', 'PAED', 'ICU', 'WUP', 'FHR', 'NB',
'AV', 'FIP', 'ED', 'CU', 'NWOW', 'NBC', 'EDPAEDS', 'ISOL', 'IPU', 'PB',
'PECC', 'UB', 'UP', 'CIS']
remove_words = ['SPJN', 'HE', '3D']
families = FilteredElementCollector(doc).OfClass(Family)
with transaction(doc, 'Rename families'):
for fam in families:
bits = re.findall(r'(SPJN[0-9X]{2,5})\s*[-_]\s*(.*)', fam.Name)
if not bits:
continue
code, name = bits[0]
name_bits = re.sub(r'[-_\s]', ' ', name).split()
out = []
for bit in name_bits:
if bit.upper() in remove_words:
continue
if bit in acronyms:
out.append(bit)
else:
out.append(bit.title())
name = ' '.join(out)
fam.Name = '{} - {}'.format(code, name)
print(fam.Name)
def report_family_types(doc):
"""Generate CSV report of types and parameters of family"""
fm = doc.FamilyManager
types = []
fieldnames = {'Type'}
for ft in fm.Types:
ft_dict = {'Type': ft.Name}
for p in fm.GetParameters():
p_value = ft.AsValueString(p) or ft.AsString(p)
if not p_value:
continue
ft_dict[p.Definition.Name] = p_value
fieldnames.add(p.Definition.Name)
types.append(ft_dict)
csv_path = get_temp_path('types.csv')
with open(csv_path, 'w') as f:
dw = csv.DictWriter(f, fieldnames, lineterminator='\n')
dw.writeheader()
dw.writerows(types)
os.startfile(csv_path, 'open')
def report_families(doc):
"""Generate report of families"""
#start_dir = r'H:\NBIF\Health Library\Revit'
#start_dir = r'C:\Users\DodswoS\Documents\Revit testing\New Families\temp\GE'
start_dir = r'C:\Users\DodswoS\Documents\Revit testing\New Families\temp\Sorted'
out_dir = r'C:\Users\DodswoS\Documents\Revit testing\New Families\temp\Sorted'
out = []
outpath = os.path.join(constants.HOMEDIR, 'types.csv')
for root, dirs, files in os.walk(start_dir):
for fname in files:
with open(outpath, 'a') as f:
w = csv.writer(f, lineterminator='\n')
basename, ext = os.path.splitext(fname)
if ext != '.rfa':
continue
filepath = os.path.join(root, fname)
famdoc = APP.OpenDocumentFile(filepath)
fm = famdoc.FamilyManager
category = famdoc.OwnerFamily.FamilyCategory.Name
for ft in fm.Types:
for p in fm.GetParameters():
p_value = ft.AsValueString(p) or ft.AsString(p)
if not p_value:
continue
row = (
start_dir,
category,
fname,
ft.Name,
p.Definition.Name,
p_value,
)
try:
w.writerow(row)
except Exception as e:
print(e)
print(row)
famdoc.Close(False)
# Move family to category folder
#category_folder = os.path.join(out_dir, category)
#if not os.path.exists(category_folder):
# os.mkdir(category_folder)
#shutil.move(filepath, category_folder)
#print('moved to {}'.format(category_folder))
#with open(outpath, 'w') as f:
# w = csv.writer(f, lineterminator='\n')
# w.writerow((
# 'Source', 'Category', 'Family', 'Type', 'Parameter Name', 'Parameter Value'))
# #w.writerows(out)
# for row in out:
# try:
# w.writerow(row)
# except Exception:
# print(row)
os.startfile(outpath, 'open')
def fix_types(doc):
families = set()
with open(os.path.join(HOMEDIR, 'types1.csv')) as f:
families = {row['Family'] for row in csv.DictReader(f)}
start_dir = r'H:\NBIF\Health Library\Revit'
#start_dir = r'I:\NBIF\Projects\NB98100\Deliverables\BIM\01_Architecture\F_Project Content\Exported Families 20170109'
out = []
for root, dirs, files in os.walk(start_dir):
for fname in files:
if fname not in families:
print('Ignoring {}'.format(fname))
continue
print(fname)
basename, ext = os.path.splitext(fname)
if ext != '.rfa':
continue
filepath = os.path.join(root, fname)
famdoc = app.OpenDocumentFile(filepath)
fm = famdoc.FamilyManager
for ft in fm.Types:
for p in fm.GetParameters():
out.append(
(
start_dir,
fname,
ft.Name,
p.Definition.Name,
ft.AsValueString(p) or ft.AsString(p)
)
)
famdoc.Close(False)
with open(os.path.join(HOMEDIR, 'types.csv'), 'w') as f:
w = csv.writer(f, lineterminator='\n')
w.writerow((
'Source', 'Family', 'Type', 'Parameter Name', 'Parameter Value'))
w.writerows(out)
def duplicate_families_by_parameter(doc):
#categories = ['Furniture', 'Furniture Systems', 'Specialty Equipment']
instances = FilteredElementCollector(doc).OfClass(FamilyInstance)
families = FilteredElementCollector(doc).OfClass(Family)
instance_families = {x.Symbol.Family.Name for x in instances}
#loaded_families = {x.Name for x in families if x.FamilyCategory and x.FamilyCategory.Name in categories}
rows = []
param_name = constants.DESCRIPTOR_TX
param_map = defaultdict(set)
symbols = FilteredElementCollector(doc).OfClass(FamilySymbol)
for s in symbols:
param = s.LookupParameter(param_name)
if param and param.AsString():
param_map[param.AsString()].add(s)
for param, symbols in param_map.items():
if len(symbols) > 1:
for x in param_map[param]:
rows.append([param, x.Category.Name, x.Family.Name, x.UniqueId, x.Family.Name in instance_families])
with open(os.path.join(HOMEDIR, 'Duplicates by {}.csv'.format(param_name)), 'w') as f:
w = csv.writer(f, lineterminator='\n')
w.writerow([param_name, 'Category', 'Family', 'Type ID', 'Used'])
w.writerows(rows)
print('Done')
def fix_duplicate_families(doc):
dupe_family_names = {}
for fam in FilteredElementCollector(doc).OfClass(Family):
if fam.Name.endswith('3D1'):
dupe_family_names[fam.Name] = set()
pprint(dupe_family_names)
def rename_doors(doc):
doors = FilteredElementCollector(
doc
).OfClass(
FamilySymbol
).OfCategory(
BuiltInCategory.OST_Doors
)
patt = re.compile(r'D(\d{1,3})([a-zA-Z]*) (2100)')
with transaction(doc, 'Rename doors'):
for symbol in doors:
name_param = symbol.get_Parameter(BuiltInParameter.ALL_MODEL_TYPE_NAME)
name = name_param.AsString()
bits = patt.findall(name)
if bits:
bits = bits[0]
new_name = 'D{}{}'.format(bits[0], bits[1])
symbol.Name = new_name
print('{} -> {}'.format(name, new_name))
def set_groups(doc):
csvfile = os.path.join(HOMEDIR, 'br2_groups.csv')
bimid_dict = defaultdict(set)
for item in csv.DictReader(open(csvfile)):
bimid_dict[item['BIM ID']].add(item['Budget: Code'])
all_symbols = FilteredElementCollector(doc).OfClass(FamilySymbol)
param_dict = {}
for s in all_symbols:
param = s.LookupParameter(constants.DESCRIPTOR_TX)
if param:
param_dict[get_param_value(param, doc)] = s
with transaction(doc, 'Set groups'):
for id, groups in bimid_dict.items():
if len(groups) != 1:
continue
matching_symbol = param_dict.get(id)
if matching_symbol:
group = list(groups)[0]
param = matching_symbol.LookupParameter('InstallationGroup_TX')
if param:
param.Set(group)
print(id, group)
def fix_hfbs(doc):
symbols = FilteredElementCollector(doc).OfClass(FamilySymbol)
tr = Transaction(doc, 'Fix HFBS codes')
tr.Start()
for symbol in symbols:
hfbs_param = symbol.LookupParameter(constants.HFBS_CODE_PARAM)
if hfbs_param:
val = hfbs_param.AsString()
if not val:
continue
bits = val.split('-')
if len(bits) == 2:
try:
prefix = bits[0]
if prefix == 'S':
prefix = 'SE'
codenum = int(bits[1])
new_val = '{}-{:06d}'.format(prefix, codenum)
if val != new_val:
hfbs_param.Set(new_val)
print('{} -> {}'.format(val, new_val))
except ValueError:
pass
tr.Commit()
def set_door_ids(doc):
tr = Transaction(doc, 'Fix HFBS codes')
tr.Start()
dw = DocumentWrapper(doc)
for door in dw.doors:
door.LookupParameter('ElementID').Set(door.Id.IntegerValue)
print(door.Id)
tr.Commit()
def fix_doors(doc):
dw = DocumentWrapper(doc)
with transaction(doc, "Fix doors"):
for door in dw.doors:
for k in range(1, 4):
param = door.LookupParameter("Kick_K{}_YN".format(k))
if param:
param.Set(False)
print(get_param_value(door.get_Parameter(BuiltInParameter.ALL_MODEL_MARK), doc))
def swap_symbol(doc, family_name, from_type_name, to_type_name):
dw = DocumentWrapper(doc)
to_symbol = [x for x in dw.symbols if
x.FamilyName == family_name and x.get_Parameter(BuiltInParameter.ALL_MODEL_TYPE_NAME).AsString() == to_type_name][0]
instances = [x for x in dw.instances if
x.Symbol.get_Parameter(BuiltInParameter.ALL_MODEL_TYPE_NAME).AsString() == from_type_name and
x.Symbol.FamilyName == family_name]
with transaction(doc, "Swap symbols"):
for instance in instances:
if instance.GroupId.IntegerValue < 0:
print('{} is in group {}'.format(instance.Id, instance.GroupId))
else:
print(instance.GroupId, instance.Id)
instance.Symbol = to_symbol
def purge_unused_types(doc):
"""Remove unused symbols for all families matching name_pattern"""
name_pattern = raw_input('Enter family name pattern: ')
matching_families = [x for x in FilteredElementCollector(doc).OfClass(
Family) if name_pattern in x.Name]
instances = FilteredElementCollector(doc).OfClass(FamilyInstance)
used_symbols = {x.Symbol.Id for x in instances}
count = 0
with transaction(doc, "Delete unused symbols"):
for family in matching_families:
for symbol_id in family.GetFamilySymbolIds():
if symbol_id not in used_symbols:
doc.Delete(symbol_id)
count += 1
print('{} symbols deleted in {} families'.format(
count, len(matching_families)))