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 fix_walls(doc): """Rename wall types""" wall_types = FilteredElementCollector(doc).OfClass(WallType) pat = re.compile('A_Wall_Generic Dry Wall (\d{2,3})mm') with transaction(doc, 'Fix walls'): for wt in wall_types: try: type_name = wt.get_Parameter( BuiltInParameter.ALL_MODEL_TYPE_NAME).AsString() width = int(round(wt.Width * constants.MM)) #acoustic = wt.LookupParameter('AcousticRating_TX').AsString() #fire_rating = wt.LookupParameter('FireRating_TX').AsString() code = wt.get_Parameter( BuiltInParameter.ALL_MODEL_TYPE_MARK).AsString() description = wt.get_Parameter( BuiltInParameter.ALL_MODEL_DESCRIPTION).AsString() bits = pat.findall(type_name) print(bits) if bits: wt.Name = 'GENERIC_DW_{}'.format(width, bits[0][1]) print(wt.Name) #fam.get_Parameter(BuiltInParameter.KEYNOTE_PARAM).Set('') #fam.LookupParameter("Acoustic Rating").Set(acoustic) #fam.LookupParameter("FireRating_TX").Set(fire_rating) #print('{} {}'.format(width, code)) except Exception as e: print(e)
def delete_unused_scope_boxes(doc): """Delete scope boxes that are not used in any views""" all_views = FilteredElementCollector(doc).OfClass(View) all_scope_boxes = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_VolumeOfInterest) all_scope_box_ids = set(x.Id for x in all_scope_boxes) used_scope_box_ids = set() for view in all_views: try: used_scope_box_ids.add( view.get_Parameter( BuiltInParameter.VIEWER_VOLUME_OF_INTEREST_CROP ).AsElementId()) except: pass count = 0 unused_scope_box_ids = all_scope_box_ids.difference(used_scope_box_ids) with transaction(doc, "Delete unused scope boxes"): for id in unused_scope_box_ids: scope_box = doc.GetElement(id) print(scope_box.Name) doc.Delete(id) count += 1 print('{} scope boxes deleted'.format(count))
def create_text_styles(doc): black = int('000000', 16) blue = int('ff0000', 16) std_sizes = [ ('1.8mm', 1.8, black), ('1.8mm Blue', 1.8, blue), ('2.0mm', 2.0, black), ('2.5mm', 2.5, black), ('3.5mm', 3.5, black), ('5.0mm', 5.0, black), ('7.0mm', 7.0, black), ('10.0mm', 10, black), ] #size_pairs = [std_sizes[i:i + 2] for i in range(len(std_sizes))] #if len(size_pairs[-1]) == 1: # size_pairs = size_pairs[:-1] text_types = FilteredElementCollector(doc).OfClass(TextNoteType) text_type = list(text_types)[0] with transaction(doc, 'Create text types'): for name, size, color in std_sizes: try: elem = text_type.Duplicate(name) elem.LookupParameter('Text Font').Set('Arial Narrow') elem.LookupParameter('Text Size').Set(size / MM) elem.LookupParameter('Color').Set(color) print(elem) except Exception as e: print(e) continue
def rename_elevations(doc): rooms = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Rooms) room_map = { x.get_Parameter(BuiltInParameter.ROOM_NUMBER).AsString(): x.get_Parameter(BuiltInParameter.ROOM_NAME).AsString() for x in rooms} views = [x for x in FilteredElementCollector(doc).OfClass(View) if x.ViewType == ViewType.FloorPlan] #for elev in elevations: # identifierParam = elev.LookupParameter(constants.IDENTIFIER_PARAM) # descriptorParam = elev.LookupParameter(constants.DESCRIPTOR_TX) # if identifierParam and \ # identifierParam.AsString() == 'PLOT' and \ # descriptorParam and \ # descriptorParam.AsString(): # print(elev.Name) #pat = re.compile('(\d{4})_(\d)') pat = re.compile('^(\d{4})+') with transaction(doc, 'Rename views'): for view in views: bits = pat.findall(view.Name) if bits: number = bits[0] name = room_map.get(number, 'XXX') print(view.Name, '{} - {} - PLAN'.format(','.join(bits), name))
def fix_room_tags(doc): """Find room tags that are outside their room and move them back""" room_tags = FilteredElementCollector(doc).OfClass( SpatialElementTag).OfCategory(BuiltInCategory.OST_RoomTags) with transaction(doc, 'Move room tags'): count = 0 for tag in room_tags: room = tag.Room if not room: continue box = room.get_BoundingBox(None) if not box: continue tag_point = tag.Location.Point tag_is_in_room = tag_point.X > box.Min.X and \ tag_point.X < box.Max.X and \ tag_point.Y > box.Min.Y and \ tag_point.Y < box.Max.Y if not tag_is_in_room: tag.Location.Move(room.Location.Point - tag.Location.Point) count += 1 print(room.Level.Name, room.Number) print('{} room tags moved'.format(count))
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 fix_view_names(doc): with transaction(doc, 'Rename views'): for view in FilteredElementCollector(doc).OfClass(View): title_param = view.get_Parameter(BuiltInParameter.VIEW_DESCRIPTION) sheet_param = view.get_Parameter(BuiltInParameter.VIEWPORT_SHEET_NUMBER) if sheet_param.AsString() and title_param.AsString().strip() and sheet_param.AsString().startswith('RM'): #print(title_param.AsString(), sheet_param.AsString()) print(view.Name) title_param.Set('')
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 rename_filters(doc): """Rename View Filters with '(1)' at the end of the name""" pat = re.compile('(.*)\(\d\)$') filters = FilteredElementCollector(doc).OfClass(ParameterFilterElement) with transaction(doc, 'Rename filters'): for f in filters: bits = pat.findall(f.Name) if bits: f.Name = bits[0].strip() print(f.Name)
def create_text(doc): type_id = doc.GetDefaultElementTypeId(ElementTypeGroup.TextNoteType) x = 0 y = 0 with transaction(doc, 'Create text'): text_types = FilteredElementCollector(doc).OfClass(TextNoteType) for tt in text_types: note = TextNote.Create(doc, doc.ActiveView.Id, XYZ(x, y, 0), tt.LookupParameter('Type Name').AsString(), tt.Id) y += 10 / MM
def rename_zone_views(doc): pat = re.compile(r'RCP - LEVEL (\d) - 1-(\d{2,3}) Zone (\d)') views = [x for x in FilteredElementCollector(doc).OfClass(View) if x.ViewType == ViewType.CeilingPlan] with transaction(doc, "Rename views"): for view in views: bits = pat.findall(view.Name) if bits: bits = bits[0] new_name = 'RCP_{}_{}_Z{}'.format(*bits) view.Name = new_name print(view.Name)
def swap_view_templates(doc, from_name, to_name): all_views = [x for x in FilteredElementCollector(doc).OfClass(View) if not x.IsTemplate] all_templates = [x for x in FilteredElementCollector(doc).OfClass(View) if x.IsTemplate] from_template = [x for x in all_templates if x.Name == from_name][0] to_template = [x for x in all_templates if x.Name == to_name][0] with transaction(doc, "Swap view templates"): for view in all_views: if view.ViewTemplateId == from_template.Id: view.ViewTemplateId = to_template.Id print(view.Name)
def sheet_report(doc): """Report on sheets""" sheets = [ x for x in FilteredElementCollector(doc).OfClass(ViewSheet) if x.LookupParameter('Identifier_TX').AsString() == 'RLS' ] with lib.transaction(doc, 'Add sheets to schedule'): for sheet in sheets: scheduled_param = sheet.get_Parameter( BuiltInParameter.SHEET_SCHEDULED) if not scheduled_param.AsInteger(): print(sheet.SheetNumber) scheduled_param.Set(1)
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 add(conf, args): """Add a transaction (gain or spending) in the balance sheet. """ if not conf: # The account book is not inited print("There is no account book there.", end=' ') print("Create one with: picsou init.") sys.exit() # Get the list a known payees # and of tags with accountBook() as a: payeesList = a.getPayees() tagsList = a.getAllTags() if args.spend: sum = -args.spend print("Add a spending of: %.2f €" % sum) else: sum = args.gain print("Add a gain of: %.2f €" % sum) # Get the paying method default_method = 'cb' valid_methods = ['cb', 'cash', 'cb_web', 'paypal', 'transfer', 'cheque'] method = reinput("\tpaying method", valid = valid_methods, default = default_method, func=cinput, complete=valid_methods) # Get the date today = date.today().strftime("%Y/%m/%d") ddate = reinput("\tdate", default=today, func=pinput, pre=today) # Get the payee payee = reinput("\tpayee", func=cinput, complete=payeesList) # Get a description desc = reinput("\tdescription", default='.') # Get tags tags = linput("\ttags", func=cinput, complete=tagsList) # Save the transaction t = transaction(sum=sum, timestamp=ddate, payee=payee, desc=desc, method=method, tags=tags) stager().add(t)
def rename_schedules(doc): pat = re.compile('(FITOUT)[- ]+(\d{4})[- ]+(.*)', re.IGNORECASE) schedules = [x for x in FilteredElementCollector(doc).OfClass(View) if x.ViewType == ViewType.Schedule and 'fitout' in x.Name.lower()] with transaction(doc, 'Fix schedule titles'): for schedule in schedules: match = pat.findall(schedule.Name) if match: bits = match[0] new_name = '{} - {} - {}'.format(*bits).upper() try: if schedule.Name != new_name: schedule.Name = new_name except: print(schedule.Name, new_name)
def fix_schedules(doc): pat = re.compile('(\d{4})') schedules = [x for x in FilteredElementCollector(doc).OfClass(View) if x.ViewType == ViewType.Schedule and 'fitout' in x.Name.lower()] levels = list(FilteredElementCollector(doc).OfClass(Level)) with transaction(doc, "Fix schedules"): for schedule in schedules: # Get room name from schedule name bits = pat.findall(schedule.Name) room_name = None if bits: room_name = bits[0] else: continue # Check if level field already added stop = False fields = [schedule.Definition.GetField(x) for x in schedule.Definition.GetFieldOrder()] for f in fields: if f.GetName() == 'Level': stop = True if stop: print('{} has Level field'.format(schedule.Name)) continue # Add level field field = None for sf in schedule.Definition.GetSchedulableFields(): if sf.GetName(doc) == 'Level': field = schedule.Definition.AddField(sf) if not field: continue field.IsHidden = True # Get level to filter by level_num = room_name[0] level = [x for x in levels if x.Name == 'LEVEL ' + level_num][0] # Add level filter filters = list(schedule.Definition.GetFilters()) level_filter = ScheduleFilter( field.FieldId, ScheduleFilterType.Equal, level.Id) filters.insert(0, level_filter) schedule.Definition.SetFilters(filters) print(schedule.Name)
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)))
def fix_worksets(doc): """Apply correct worksets to elements based on their category""" workset_table = doc.GetWorksetTable() worksets = FilteredWorksetCollector(doc).OfKind(WorksetKind.UserWorkset) workset_map = {x.Name: x.Id.IntegerValue for x in worksets} elements = FilteredElementCollector(doc).OfClass(FamilyInstance) count = 0 with transaction(doc, 'Fix worksets'): for element in elements: category = element.Symbol.Category.Name ws_getter = CATEGORY_WORKSETS.get(category) if not ws_getter: continue new_workset = ws_getter(element) if not new_workset: continue # new_workset can be either the workset name or the workset id if not isinstance(new_workset, str): new_workset = workset_table.GetWorkset(new_workset).Name workset_id = doc.GetWorksetId(element.Id) workset = workset_table.GetWorkset(workset_id).Name if workset in STOP_WORKSETS: continue if workset != new_workset: workset_param = element.get_Parameter( BuiltInParameter.ELEM_PARTITION_PARAM) if workset_param.IsReadOnly: continue workset_param.Set(workset_map.get(new_workset)) count += 1 if count and not count % 10: print('{} changed'.format(count)) print('Total of {} elements changed'.format(count))
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 smart_purge(DOC): """ 27828407 20206287 0 20206291 0 955 20380607 0 21208765 20522972 4 21165783 21165966 2 """ symbol_map = {} family_map = defaultdict(dict) symbols = FilteredElementCollector(DOC).OfClass(FamilySymbol) instances = FilteredElementCollector(DOC).OfClass(FamilyInstance) instances_grouped = groupby(instances, lambda x: x.Symbol.Id) for symbol_id, instances in instances_grouped: symbol_map[symbol_id] = len(list(instances)) symbols_grouped = groupby(symbols, lambda x: x.Family.Id) for family_id, symbols in symbols_grouped: print(family_id) for symbol in symbols: family_map[family_id][symbol.Id] = symbol_map.get(symbol.Id, 0) print(' {} {}'.format(symbol.Id, symbol_map.get(symbol.Id, 0))) with lib.transaction(DOC, "Smart purge"): for family_id, type_ids in family_map.items(): has_instances = False for type_id, count in type_ids.items(): if count > 0: has_instance = True continue #print family_id, has_instances if not has_instances: family = DOC.GetElement(family_id) if not family.IsCurtainPanelFamily: print(family.Name) DOC.Delete(family_id)
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 place_all_walls(doc): """Place all walls""" levels = list(FilteredElementCollector(doc).OfClass(Level)) wall_types = sorted(FilteredElementCollector(doc).OfClass(WallType), key=lambda x: x.Width) with transaction(doc, 'Place walls'): x = 0 y = 0 i = 0 step = 10 for wt in wall_types: if i % 10 == 0: x = 0 y += step else: x += step start = XYZ(x, y, 0) end = XYZ(x, y + step, 0) line = Line.CreateBound(start, end) w = Wall.Create(doc, line, levels[0].Id, False) w.WallType = wt i += 1
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)