关于二次开发基础教程中遇到的问题001


同济大学出版的二次开发基础教程,里面经常出现这样的代码:
ElementSet elems = commandData.Application.ActiveUIDocument.Selection.Elements;
然而在2015API中 Elements属性已经废弃 Obsolete,你在输入这段代码的时候系统会报错。
替代方法是:获得选中元素的ID;然后对ID进行操作。
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;
using System.Collections;
namespace RevitDemotest
{
    [Transaction(TransactionMode.Manual)]
    public class Command : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            message = "Please take attention on the highlighted Walls!";
            //先从 UI 选取元素,然后执行该插件
            ElementSet elementSet = new ElementSet();
            ArrayList walls = new ArrayList();
            foreach (ElementId elementId in commandData.Application.ActiveUIDocument.Selection.GetElementIds())
            {
                elementSet.Insert(commandData.Application.ActiveUIDocument.Document.GetElement(elementId));
            }
                foreach (Element element in elementSet)
                {
                    Wall wall = element as Wall;
                 if (null!=wall)
                {
                    walls.Add(wall);
                }
                }           
            return Result.Failed;
        }
    }
}
执行结果如下:

猜你喜欢

转载自blog.csdn.net/star65806841/article/details/80009387