Dynamo软件包的节点比较容易,但是下拉框还是比较麻烦的,这里需要实现DSDropDownBase这个接口。
先看效果:
这里直接贴出源码:
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("输入钢筋类型,输出钢筋类型名称")]
[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();
}
}
}
上面的代码中用了个过滤,但是这个过滤需要用到Revit的文档,在Dynamo中获取Revit文档还是很容易的,直接用Document doc = DocumentManager.Instance.CurrentDBDocument即可。
过滤就更简单了,跟Revit二次开发一样。
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;
}
}
比较麻烦的是,做这个控件需要引用的dll文件比较多。如下图:
对Revit二次开发和Dyanmo编程这块感兴趣请加qq群交流:660319009
个人问题咨询请加qq:254033230,本人见钱眼开,不要轻易打扰!!!