The node of the Dynamo software package is relatively easy, but the drop-down box is more troublesome, and the interface DSDropDownBase needs to be implemented here.
First look at the effect:
Paste the source code directly here:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using CoreNodeModels; using Dynamo.Graph.Nodes; using Newtonsoft.Json; using ProtoCore.AST.AssociativeAST; using RevitServices.Persistence; using Autodesk.Revit.DB.Structure; using Autodesk.Revit.DB; using RenGeDynamo.Filter; namespace RenGeDynamo.Rebar { [NodeName("Rebar BarType Name")] [NodeDescription("Input reinforcement type, output reinforcement type name")] [IsDesignScriptCompatible] public class RebarBarTypeName : DSDropDownBase { public RebarBarTypeName() : base("rebarBarType") { } [JsonConstructor] public RebarBarTypeName(IEnumerable<PortModel> inPorts, IEnumerable<PortModel> outPorts) : base("rebarBarType", inPorts, outPorts) { } protected override SelectionState PopulateItemsCore(string currentSelection) { Items.Clear(); foreach(var rebarTypeElement in GetRebarType()) { Items.Add(new DynamoDropDownItem(rebarTypeElement.Name, rebarTypeElement.Name)); } SelectedIndex = 0; return SelectionState.Done; } public override IEnumerable<AssociativeNode> BuildOutputAst(List<AssociativeNode> inputAstNodes) { // Build an AST node for the type of object contained in your Items collection. var intNode = AstFactory.BuildPrimitiveNodeFromObject(Items[SelectedIndex].Item); var assign = AstFactory.BuildAssignment (GetAstIdentifierForOutputIndex (0), intNode); return new List<AssociativeNode> { assign }; } private List<Element> GetRebarType() { Document doc = DocumentManager.Instance.CurrentDBDocument; RebarFilter filter = new RebarFilter(); return filter.FilterRebarBarType(doc).ToList(); } } }
The above code uses a filter, but this filter needs to use Revit documents. It is very easy to obtain Revit documents in Dynamo, just use Document doc = DocumentManager.Instance.CurrentDBDocument.
Filtering is simpler, the same as Revit secondary development.
public class RebarFilter { public FilteredElementCollector FilterRebarBarType(Document doc) { FilteredElementCollector fliteredElements = new FilteredElementCollector(doc); ElementClassFilter classFilter = new ElementClassFilter(typeof(RebarBarType)); fliteredElements = fliteredElements.WherePasses(classFilter); return fliteredElements; } public FilteredElementCollector FilterRebarShape(Document doc) { FilteredElementCollector fliteredElements = new FilteredElementCollector(doc); ElementClassFilter classFilter = new ElementClassFilter(typeof(RebarShape)); fliteredElements = fliteredElements.WherePasses(classFilter); return fliteredElements; } }
What is more troublesome is that there are many dll files that need to be referenced to make this control. As shown below:
If you are interested in Revit secondary development and Dyanmo programming, please join the QQ group to communicate: 660319009
For personal inquiries, please add qq: 254033230, I see money, don't bother! ! !