diff --git a/0-python-code/HowTo_GetAllElementsOfCategory.py b/0-python-code/HowTo_GetAllElementsOfCategory.py new file mode 100644 index 0000000..54910a4 --- /dev/null +++ b/0-python-code/HowTo_GetAllElementsOfCategory.py @@ -0,0 +1,30 @@ +""" +All elements of Category +Get all elements of the specified category from Model. + +TESTED REVIT API: 2016,2017 + +Author: Francisco Possetto | github.com/franpossetto + +Shared on www.revitapidocs.com +For more information visit http://github.com/gtalarico/revitapidocs +License: http://github.com/gtalarico/revitapidocs/blob/master/LICENSE.md +""" + +#Imports. +from Autodesk.Revit.DB import FilteredElementCollector, BuiltInCategory + +doc = __revit__.ActiveUIDocument.Document + +def all_elements_of_category(category): + return FilteredElementCollector(doc).OfCategory(category).WhereElementIsNotElementType().ToElements() + +#All Elements Of Walls Category. +walls = all_elements_of_category(BuiltInCategory.OST_Walls) + +#All Elements Of Doors Category. +doors = all_elements_of_category(BuiltInCategory.OST_Doors) + +#All Elements Of Windows Category. +windows = all_elements_of_category(BuiltInCategory.OST_Windows) + diff --git a/0-python-code/HowTo_GetAllWorksets.py b/0-python-code/HowTo_GetAllWorksets.py new file mode 100644 index 0000000..9fc9792 --- /dev/null +++ b/0-python-code/HowTo_GetAllWorksets.py @@ -0,0 +1,23 @@ +""" +GET ALL WORKSETS FROM THE CURRENT DOCUMENT + +TESTED REVIT API: 2016,2017,2018 + +Author: min.naung@https://twentytwo.space/contact | https://github.com/mgjean + +This file is shared on www.revitapidocs.com +For more information visit http://github.com/gtalarico/revitapidocs +License: http://github.com/gtalarico/revitapidocs/blob/master/LICENSE.md +""" +from Autodesk.Revit.DB import FilteredWorksetCollector, WorksetKind + +# document instance +doc = __revit__.ActiveUIDocument.Document + +# collect user created worksets +worksets = FilteredWorksetCollector(doc).OfKind(WorksetKind.UserWorkset).ToWorksets() + +# loop worksets +for workset in worksets: + # print name, workset + print workset.Name,workset diff --git a/0-python-code/HowTo_GetParameterValueByName.py b/0-python-code/HowTo_GetParameterValueByName.py new file mode 100644 index 0000000..6aeb99d --- /dev/null +++ b/0-python-code/HowTo_GetParameterValueByName.py @@ -0,0 +1,29 @@ +""" +Get Parameter Value by Name +Get value of one of element's parameters. + +TESTED REVIT API: 2016,2017 + +Author: Francisco Possetto | github.com/franpossetto + +Shared on www.revitapidocs.com +For more information visit http://github.com/gtalarico/revitapidocs +License: http://github.com/gtalarico/revitapidocs/blob/master/LICENSE.md +""" + +#Imports. +from Autodesk.Revit.DB import Element + +doc = __revit__.ActiveUIDocument.Document +uidoc = __revit__.ActiveUIDocument + +def get_parameter_value_by_name(element, parameterName): + return element.LookupParameter(parameterName).AsValueString() + +#Select elements from revit. +selection = [doc.GetElement(x) for x in uidoc.Selection.GetElementIds()] + +#Example with Walls. +for wall in selection: + print get_parameter_value_by_name(wall, "Base Constraint") + \ No newline at end of file diff --git a/0-python-code/HowTo_HideUnhideLinksLevelsGrids.py b/0-python-code/HowTo_HideUnhideLinksLevelsGrids.py new file mode 100644 index 0000000..ef20aa7 --- /dev/null +++ b/0-python-code/HowTo_HideUnhideLinksLevelsGrids.py @@ -0,0 +1,96 @@ +""" +HIDE / UNHIDE - LEVELS AND GRIDS FROM LINKS DOCUMENTS + +TESTED REVIT API: 2017, 2018 + +Author: min.naung@https://twentytwo.space/contact | https://github.com/mgjean + +This file is shared on www.revitapidocs.com +For more information visit http://github.com/gtalarico/revitapidocs +License: http://github.com/gtalarico/revitapidocs/blob/master/LICENSE.md +""" + +import System +from System.Collections.Generic import List +from Autodesk.Revit.DB import Transaction +from Autodesk.Revit.DB import * + +doc = __revit__.ActiveUIDocument.Document +active_view = doc.ActiveView + +# filter name "can name anything" +ifilter = "GiveFilterAName" + +endWiths = "Anything" + +# filter check +found = False + +unhide = False # Edit here to hide/unhide +msg = "Unhide" if unhide else "Hide" +trans = Transaction(doc,"%s links levels grids" %(msg)) +trans.Start() + +# collect all filter elements +allFilters = FilteredElementCollector(doc).OfClass(FilterElement).ToElements() + +# get filters from current view +viewFilters = active_view.GetFilters() +# collect filters' names +viewFiltersName = [doc.GetElement(i).Name.ToString() for i in viewFilters] + +# loop each filter +for fter in allFilters: + # filter already have in doc but not in current view + if ifilter == fter.Name.ToString() and ifilter not in viewFiltersName: + # add filter + active_view.AddFilter(fter.Id) + # set filter visibility + active_view.SetFilterVisibility(fter.Id, unhide) + found = True + # filter already have in doc and current view + if ifilter == fter.Name.ToString() and ifilter in viewFiltersName: + # set filter visibility + active_view.SetFilterVisibility(fter.Id, unhide) + found = True + +# if filter not found in doc +if not found: + # all grids in doc + grids = FilteredElementCollector(doc).OfClass(Grid).ToElements() + # all levels in doc + levels = FilteredElementCollector(doc).OfClass(Level).ToElements() + # collect category id from grid and level + CateIds = List[ElementId]([grids[0].Category.Id,levels[0].Category.Id]) + + # type ids from grids + gridTypeIds = set([i.GetTypeId() for i in grids]) + # type ids from levels + levelTypeIds = set([i.GetTypeId() for i in levels]) + + # get grid type element + type_elems = [doc.GetElement(i) for i in gridTypeIds] + # get level type element + type_elems.extend([doc.GetElement(l) for l in levelTypeIds]) + + # loop type elements + for elem in type_elems: + # if endwiths not include in type name + if not endWiths in elem.LookupParameter("Type Name").AsString(): + # add endwiths in type name + elem.Name = elem.LookupParameter("Type Name").AsString() + endWiths + # get type names + type_names = [i.LookupParameter("Type Name").AsString() for i in type_elems] + # type name parameter id + paramId = type_elems[0].LookupParameter("Type Name").Id + # create a "not ends with" filter rule + notendswith = ParameterFilterRuleFactory.CreateNotEndsWithRule(paramId,endWiths,False) + # create parameter filter element + paramFilterElem = ParameterFilterElement.Create(doc, ifilter,CateIds,[notendswith]) + # set filter overrides (same with add filter to current) + active_view.SetFilterOverrides(paramFilterElem.Id, OverrideGraphicSettings()) + # set filter visibility + active_view.SetFilterVisibility(paramFilterElem.Id, unhide) + +print "DONE!" +trans.Commit() \ No newline at end of file diff --git a/0-python-code/HowTo_ReloadAllLinkDocuments.py b/0-python-code/HowTo_ReloadAllLinkDocuments.py new file mode 100644 index 0000000..79c9867 --- /dev/null +++ b/0-python-code/HowTo_ReloadAllLinkDocuments.py @@ -0,0 +1,31 @@ +""" +Reload All Link Documents + +TESTED REVIT API: 2017 + +Author: min.naung@https://twentytwo.space/contact | https://github.com/mgjean + +This file is shared on www.revitapidocs.com +For more information visit http://github.com/gtalarico/revitapidocs +License: http://github.com/gtalarico/revitapidocs/blob/master/LICENSE.md +""" + +from Autodesk.Revit.DB import FilteredElementCollector,RevitLinkInstance + +uidoc = __revit__.ActiveUIDocument + +linkInstances = FilteredElementCollector(doc).OfClass(RevitLinkInstance).ToElements() + +load = [] + +for link in linkInstances: + linkType = doc.GetElement(link.GetTypeId()); + filepath = linkType.GetExternalFileReference().GetAbsolutePath(); + try: + linkType.LoadFrom(filepath,None); + load.append(link.Name.split(" : ")[0]+" "); + except: + load.append(link.Name.split(" : ")[0]+" ") + pass +for i in load: + print i diff --git a/0-python-code/HowTo_SetParameterByName.py b/0-python-code/HowTo_SetParameterByName.py new file mode 100644 index 0000000..a2c675a --- /dev/null +++ b/0-python-code/HowTo_SetParameterByName.py @@ -0,0 +1,35 @@ +""" +Set Parameter by Name +Set one of element's parameters. + +TESTED REVIT API: 2016,2017 + +Author: Francisco Possetto | github.com/franpossetto + +Shared on www.revitapidocs.com +For more information visit http://github.com/gtalarico/revitapidocs +License: http://github.com/gtalarico/revitapidocs/blob/master/LICENSE.md +""" + +#Imports +from Autodesk.Revit.DB import Element, Transaction + +doc = __revit__.ActiveUIDocument.Document +uidoc = __revit__.ActiveUIDocument +t = Transaction(doc, 'Set Parameter by Name') + +#Select element from revit. +selection = [doc.GetElement(x) for x in uidoc.Selection.GetElementIds()] + +def set_parameter_by_name(element, parameterName, value): + element.LookupParameter(parameterName).Set(value) + +#Start Transaction +t.Start() + +for s in selection: + #Set a new Comment + set_parameter_by_name(s,"Comments", "Good Element") + +#End Transaction +t.Commit() \ No newline at end of file diff --git a/0-python-code/Tools_MoveRoomTagToRoomCenter.py b/0-python-code/Tools_MoveRoomTagToRoomCenter.py index 3968f0f..ad6ec0a 100644 --- a/0-python-code/Tools_MoveRoomTagToRoomCenter.py +++ b/0-python-code/Tools_MoveRoomTagToRoomCenter.py @@ -1,9 +1,10 @@ """ -Moves all selected tags to the center of their corresponding rooms +Moves all tags to the "Room Location Point" of their corresponding rooms TESTED REVIT API: 2015, 2016, 2017, 2017.1 Author: Gui Talarico | github.com/gtalarico + min.naung | https://twentytwo.space/contact | https://github.com/mgjean This file is shared on www.revitapidocs.com For more information visit http://github.com/gtalarico/revitapidocs @@ -16,17 +17,15 @@ clr.AddReference('RevitAPIUI') from Autodesk.Revit.DB import Transaction -from Autodesk.Revit.DB import FilteredElementCollector, BuiltInCategory -from Autodesk.Revit.DB.Architecture import Room +from Autodesk.Revit.DB import FilteredElementCollector, SpatialElementTag uidoc = __revit__.ActiveUIDocument doc = __revit__.ActiveUIDocument.Document ########################################################################### # TAG COLLECTOR [IN VIEW BY: doc.ActiveView.Id] -room_tags = FilteredElementCollector(doc, doc.ActiveView.Id).OfCategory( - BuiltInCategory.OST_RoomTags).WhereElementIsNotElementType().\ - ToElements() +room_tags = FilteredElementCollector(doc, doc.ActiveView.Id)\ + .OfClass(SpatialElementTag).ToElements() ########################################################################### transaction = Transaction(doc, 'Move Room Tags on Room Points') diff --git a/0-python-code/Tools_SetViewTemplateParameterToNotControlled.py b/0-python-code/Tools_SetViewTemplateParameterToNotControlled.py new file mode 100644 index 0000000..c371edc --- /dev/null +++ b/0-python-code/Tools_SetViewTemplateParameterToNotControlled.py @@ -0,0 +1,57 @@ +""" +Set view template parameter to not controlled by view template + +Sets a single view template parameter 'keep_non_sheet_view' +to be not controlled by view template, keeping other view +template parameters settings. + +TESTED REVIT API: 2017 + +Author: Frederic Beaupere | github.com/hdm-dt-fb + +This file is shared on www.revitapidocs.com +For more information visit http://github.com/gtalarico/revitapidocs +License: http://github.com/gtalarico/revitapidocs/blob/master/LICENSE.md +""" + +import clr +clr.AddReference("RevitAPI") +from Autodesk.Revit.DB import FilteredElementCollector as Fec +from Autodesk.Revit.DB import Transaction +from System.Collections.Generic import List + + +doc = __revit__.ActiveUIDocument.Document +all_views = Fec(doc).OfClass(View).ToElements() +view_templates = [view for view in all_views if view.IsTemplate] +first_view_template = view_templates[0] +view_template_params = first_view_template.GetTemplateParameterIds() +switch_off_param_name = "keep_non_sheet_view" + +# get the id of the parameter we want to switch off +for param_id in view_template_params: + param = doc.GetElement(param_id) + if "Name" in dir(param): + print(param.Name) + if param.Name == switch_off_param_name: + switch_off_param_id = param_id + break + +# set the switch off parameter to be non controlled +# while keeping the setting of the other parameters + +t = Transaction(doc, "adjust view_templates") +t.Start() + +for view_template in view_templates: + set_param_list = List[ElementId]() + set_param_list.Add(switch_off_param_id) + + non_controlled_param_ids = view_template.GetNonControlledTemplateParameterIds() + + for param_id in non_controlled_param_ids: + set_param_list.Add(param_id) + + view_template.SetNonControlledTemplateParameterIds(set_param_list) + +t.Commit() diff --git a/1-csharp-code/Snippets_DimensionGrids b/1-csharp-code/Snippets_DimensionGrids new file mode 100644 index 0000000..09b2c04 --- /dev/null +++ b/1-csharp-code/Snippets_DimensionGrids @@ -0,0 +1,93 @@ +/* +Create a dimension line between all chosen grid. The gird lines need to be parallel to each other, +which is the case most of the time but not always. +TESTED REVIT API: 2018 +The snippet can be used as is in a Revit Application Macro for test purposes +Author: Deyan Nenov | github.com/ArchilizerLtd | www.archilizer.com +This file is shared on www.revitapidocs.com +For more information visit http://github.com/gtalarico/revitapidocs +License: http://github.com/gtalarico/revitapidocs/blob/master/LICENSE.md +*/ +/// +/// Creates a single dimension string between the chosen Grid lines +/// +public void DimGrids() +{ + UIDocument uidoc = this.ActiveUIDocument; + Document doc = uidoc.Document; + + // Pick all the grid lines you want to dimension to + GridSelectionFilter filter = new ThisApplication.GridSelectionFilter(doc); + var grids = uidoc.Selection.PickElementsByRectangle(filter, "Pick Grid Lines"); + + ReferenceArray refArray = new ReferenceArray(); + XYZ dir = null; + + foreach(Element el in grids) + { + Grid gr = el as Grid; + + if(gr == null) continue; + if(dir == null) + { + Curve crv = gr.Curve; + dir = new XYZ(0,0,1).CrossProduct((crv.GetEndPoint(0) - crv.GetEndPoint(1))); // Get the direction of the gridline + } + + Reference gridRef = null; + + // Options to extract the reference geometry needed for the NewDimension method + Options opt = new Options(); + opt.ComputeReferences = true; + opt.IncludeNonVisibleObjects = true; + opt.View = doc.ActiveView; + foreach (GeometryObject obj in gr.get_Geometry(opt)) + { + if (obj is Line) + { + Line l = obj as Line; + gridRef = l.Reference; + refArray.Append(gridRef); // Append to the list of all reference lines + } + } + } + + XYZ pickPoint = uidoc.Selection.PickPoint(); // Pick a placement point for the dimension line + Line line = Line.CreateBound(pickPoint, pickPoint + dir * 100); // Creates the line to be used for the dimension line + + using(Transaction t = new Transaction(doc, "Make Dim")) + { + t.Start(); + if( !doc.IsFamilyDocument ) + { + doc.Create.NewDimension( + doc.ActiveView, line, refArray); + } + t.Commit(); + } +} +/// +/// Grid Selection Filter (example for selection filters) +/// +public class GridSelectionFilter : ISelectionFilter +{ + Document doc = null; + public GridSelectionFilter(Document document) + { + doc = document; + } + + public bool AllowElement(Element element) + { + if(element.Category.Name == "Grids") + { + return true; + } + return false; + } + + public bool AllowReference(Reference refer, XYZ point) + { + return true; + } +} diff --git a/1-csharp-code/Snippets_FilterSelect b/1-csharp-code/Snippets_FilterSelect new file mode 100644 index 0000000..a0d2375 --- /dev/null +++ b/1-csharp-code/Snippets_FilterSelect @@ -0,0 +1,27 @@ +/* +Make a selection of objects in view filtering by category +This works just as Filter Selection but in Reverse. It allows you to first select the Category of the Selection by +picking up an Object of that Category and follow up with a standard rectangular selection. After finishing the selection, only +objects from that Category will be selected. +TESTED REVIT API: 2018 +The snippet can be used as is in a Revit Application Macro for test purposes +Author: Deyan Nenov | github.com/ArchilizerLtd +This file is shared on www.revitapidocs.com +For more information visit http://github.com/gtalarico/revitapidocs +License: http://github.com/gtalarico/revitapidocs/blob/master/LICENSE.md +*/ + +public void FilterSelect() +{ + UIDocument uidoc = ActiveUIDocument; + Document doc = uidoc.Document; + + Reference refer = uidoc.Selection.PickObject(ObjectType.Element, "Set the filter"); //Pick an object by which Category you will filter + + IList elements = uidoc.Selection.PickElementsByRectangle(); + + uidoc.Selection.SetElementIds(elements + .Where(x => x.Category.Id.IntegerValue.Equals(doc.GetElement(refer).Category.Id.IntegerValue)) + .Select(x => x.Id) + .ToList()); +} diff --git a/1-csharp-code/Snippets_GetWorksetByName.cs b/1-csharp-code/Snippets_GetWorksetByName.cs new file mode 100644 index 0000000..478d881 --- /dev/null +++ b/1-csharp-code/Snippets_GetWorksetByName.cs @@ -0,0 +1,21 @@ +/* +Get workset by name. +TESTED REVIT API: 2020 +Author: Francisco Possetto | github.com/franpossetto +This file is shared on www.revitapidocs.com +For more information visit http://github.com/gtalarico/revitapidocs +License: http://github.com/gtalarico/revitapidocs/blob/master/LICENSE.md +*/ + +using System.Linq; +using System.Collections.Generic; +using Autodesk.Revit.DB; + +public Workset GetWorksetByName(Document doc, string WorksetName) +{ + IList worksets = new FilteredWorksetCollector(doc).OfKind(WorksetKind.UserWorkset).ToWorksets(); + Workset workset = worksets.Where(x => x.Name == WorksetName).FirstOrDefault(); + + //if the workset does not exist, return the first one. + return (workset != null) ? workset : worksets.FirstOrDefault(); +} diff --git a/1-csharp-code/Snippets_HideCategory.cs b/1-csharp-code/Snippets_HideCategory.cs new file mode 100644 index 0000000..0d29f05 --- /dev/null +++ b/1-csharp-code/Snippets_HideCategory.cs @@ -0,0 +1,31 @@ +/* +Hide Category. +TESTED REVIT API: 2020 +Hide / Unhide Category in the active view. +Author: Francisco Possetto | github.com/franpossetto +This file is shared on www.revitapidocs.com +For more information visit http://github.com/gtalarico/revitapidocs +License: http://github.com/gtalarico/revitapidocs/blob/master/LICENSE.md +*/ + +using Autodesk.Revit.DB; + +public void HideCategory(Document doc, string categoryName, bool hide) +{ + View activeView = doc.ActiveView; + Categories categories = doc.Settings.Categories; + Category category = null; + + // Get the category by name + foreach (Category cat in categories) + if (cat.Name == categoryName) category = cat; + + if (category == null) return; + + using (Transaction t = new Transaction(doc, "Hide/Unhide Category")) + { + t.Start(); + activeView.SetCategoryHidden(category.Id, hide); + t.Commit(); + } +} diff --git a/1-csharp-code/Snippets_ToggleLevelBubbleVisibility b/1-csharp-code/Snippets_ToggleLevelBubbleVisibility new file mode 100644 index 0000000..f3c4466 --- /dev/null +++ b/1-csharp-code/Snippets_ToggleLevelBubbleVisibility @@ -0,0 +1,30 @@ +/* +Toggle the visibility of the Level Bubble in Section/Elevation +The snippet can be reused for Grids +TESTED REVIT API: 2018 +The snippet can be used as is in a Revit Application Macro for test purposes +Author: Deyan Nenov | github.com/ArchilizerLtd +This file is shared on www.revitapidocs.com +For more information visit http://github.com/gtalarico/revitapidocs +License: http://github.com/gtalarico/revitapidocs/blob/master/LICENSE.md +*/ + +public void ToggleLevelBubbles() +{ + Document doc = this.ActiveUIDocument.Document; + Selection sel = this.ActiveUIDocument.Selection; + + do + { + //Pick the Level you want to toggle in Elevation/Section + Level lvl = doc.GetElement(sel.PickObject(ObjectType.Element, "Pick View to Align To")) as Level; + + using(Transaction t = new Transaction(doc, "toggle")) + { + t.Start(); + lvl.HideBubbleInView(DatumEnds.End0,doc.ActiveView); //DatumEnds.End1 to toggle the other End + t.Commit(); + } + } + while(true); +} diff --git a/1-csharp-code/Snippit_FindFamilySymbolByName.cs b/1-csharp-code/Snippit_FindFamilySymbolByName.cs new file mode 100644 index 0000000..f3db9fc --- /dev/null +++ b/1-csharp-code/Snippit_FindFamilySymbolByName.cs @@ -0,0 +1,38 @@ +/* +Pass Document and name string as Variables +This snippet is a utility function to make working with family symbols easier +also is an example on using FilteredElementCollector filters + +TESTED REVIT API: 2019 +The snippet can be used as is in a Revit Application Macro for test purposes + +Author: Robert Curry | https://github.com/RobertCurry0216 + +This file is shared on www.revitapidocs.com +For more information visit http://github.com/gtalarico/revitapidocs +License: http://github.com/gtalarico/revitapidocs/blob/master/LICENSE.md +*/ + +public static FamilySymbol GetFamilySymbolByName(Document doc, string name) +{ + var paramId = new ElementId(BuiltInParameter.ALL_MODEL_FAMILY_NAME); + var paramValueProvider = new ParameterValueProvider(paramId); + var equalsRule = new FilterStringEquals(); + var filterRule = new FilterStringRule(paramValueProvider, equalsRule, name, false); + var filter = new ElementParameterFilter(filterRule); + + var fec = new FilteredElementCollector(doc); + fec.OfClass(typeof(FamilySymbol)).WhereElementIsElementType().WherePasses(filter); + + if (fec.GetElementCount() == 1) + { + var symbol = fec.FirstElement() as FamilySymbol; + if (!symbol.IsActive) + { + symbol.Activate(); + doc.Regenerate(); + } + return symbol; + } + return null; +} \ No newline at end of file