怎样制作Dyanmo软件包(3)——下拉框控件

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,本人见钱眼开,不要轻易打扰!!!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值