Revit二次开发重复命令(以创建墙为例)

2019年4月28号。Revit版本2017,VS版本2017

先看实现的效果

 通过加载一次代码实现多次重读创建墙的功能。原理比较简单,首先选取模型线,根据模型线生成默认墙,并将顶部约束在标高2上。具体的实现代码如下:

 public class Class1 : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;
            Selection sel = uidoc.Selection;
            View view = uidoc.ActiveView;
            ElementId l1 = new FilteredElementCollector(doc).OfClass(typeof(Level)).Cast<Level>()
                .Where(m => m.Name == "标高 1").First().Id;
            bool isCoutine = true;
            ISelectionFilter selectionFilter = new ModelLineFillter();
            Transaction ts = new Transaction(doc, "外部事件");
            try
            {
                while (isCoutine)
                {
                    ts.Start();
                    try
                    {
                        ModelLine c1 =
                            doc.GetElement(sel.PickObject(ObjectType.Element, selectionFilter, "请选择模型线")) as ModelLine;
                        Curve curve = c1.GeometryCurve;
                        if (c1 != null)
                        {
                            Wall wall = Wall.Create(doc, curve, l1, false);
                            if (wall != null)
                            {
                                Parameter para = wall.get_Parameter(BuiltInParameter.WALL_HEIGHT_TYPE);
                                if (!para.IsReadOnly)
                                {
                                    bool isC = para.Set(new ElementId(694));
                                }
                            }

                            ts.Commit();
                        }
                    }
                    catch (Autodesk.Revit.Exceptions.OperationCanceledException ex)
                    {
                        TaskDialog.Show("Revit", "命令被取消!");
                        ts.Commit();
                        return Result.Succeeded;
                    }
                }
            }
            catch (Exception ex)
            {
                if (ts.GetStatus() == TransactionStatus.Started)
                {
                    ts.RollBack();
                }

                TaskDialog.Show("Revit", ex.Message);

            }

            return Result.Succeeded;
        }

 以下为ModelLineFillter的代码

 /// <summary>
 /// 选择限制类,以下为只能选择模型线
 /// </summary>
        public class ModelLineFillter : ISelectionFilter
        {
            public bool AllowElement(Element elem)
            {
                if (elem.Name == "模型线")
                {
                    return true;
                }

                return false;
            }

            public bool AllowReference(Reference reference, XYZ position)
            {
                return false;
            }
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值