CaribouReinforcement
Get CaribouReinforcement package from Dynamo Package Manager to get this functionality and many moreDescription
This article handles managing Rebars for Revit created with Dynamo in Python. Iworked at a project that involved automating creation of rebars for a foundation element. I used a pretty nice package (DynamoRebar) and it provided support for some of the processes I implemented in the project. However we needed some functionalities that needed developed,I am refering to managing the visibility of the Rebars in a certain view.


The custom node can be created by just using python editor and some input and output nodes. The result is the altering of bars visibility according to our design. The left-side image shows the exact result we achieved using our implementation.
Using the custom node is pretty simple, just pass on 4 input data, even lists and it will handle them:


Solution
Create the appropiate imports
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import clr import re clr.AddReference("RevitServices") from RevitServices.Persistence import DocumentManager from RevitServices.Transactions import TransactionManager as TM doc = DocumentManager.Instance.CurrentDBDocument clr.AddReference("RevitAPI"); from Autodesk.Revit.DB import * from Autodesk.Revit.DB.Structure import * In_RebarContainers = IN[0]; In_Views = IN[1]; In_Index = IN[2] In_Hide = IN[3]; |
Define some helper functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#neg will provide a negation of a boolean value def neg(boolean): if boolean == True: return False; else: return True; #checking if a value is a list def isIterable(unit): t1 = t2 = False; if(isinstance(unit, (tuple, set, list, dict))): t1 = True; if(hasattr(unit, "__iter__")): iterstr = str(unit.__iter__()); if(re.search("System.Array", iterstr)): t2 = True; return (t1 or t2); |
Because we want to pass RevitElement types (straigth from Dynamo, we need to get the actual DB elements)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#getting the rebar container def getContainer(data): if data is None: return None if (hasattr(data,"Id")): rebarContainer = doc.GetElement(ElementId(data.Id)) if (rebarContainer <> None): if rebarContainer.GetType().Equals(RebarContainer): return rebarContainer; return None #getting the view def getView(data): if hasattr(data, "Id"): view = doc.GetElement(ElementId(data.Id)); return view return None |
Creating the function that will recursively handle sets of containers and views
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
def hideAll(dataRebarContainer, dataView, dataIndex): resultList = [] if isIterable(dataView): retlist = [] for item in dataView: retlist.append(hideAll(dataRebarContainer, item, dataIndex)) return retlist elif isIterable(dataRebarContainer): for item in dataRebarContainer: resultList.append(hideAll(item, dataView, dataIndex)) return resultList else: container = getContainer(dataRebarContainer); view = getView(dataView) HideElements(container, view, dataIndex, In_Hide) return container |
Creating the function that will hide items in a rebar container. And of course handling the index passed, because you may pass a list or a number.
However if for example you want to show only the first item in a set of rebars, then you pass 0 as index. The other bars are automatically hidden.
This works the other way around too because we set up the display as passed with the argument In_Hide and for the other items we negate the boolean value neg( .. )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
def HideElements(dataRebarContainer, dataView, dataIndex, dataHide): if (dataRebarContainer is None): return; if (dataView is None): return; if isIterable(dataIndex): for idx in range(dataRebarContainer.ItemsCount): if (idx in dataIndex): HideElements(dataRebarContainer, dataView, idx, dataHide); else: HideElements(dataRebarContainer, dataView, idx, neg(dataHide)); else: if (dataIndex < dataRebarContainer.ItemsCount): dataRebarContainer.SetItemHiddenStatus(dataView, dataIndex, dataHide); |
Final step is to create a transaction and set up the OUT port. TM is the transaction manager, see point 1.
1 2 3 4 |
TM.Instance.EnsureInTransaction(doc) result = hideAll(In_RebarContainers, In_Views, In_Index) TM.Instance.TransactionTaskDone() OUT = result |