同济大学出版的二次开发基础教程,里面经常出现这样的代码:
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;
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;
}
}
}
{
[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;
}
}
}
执行结果如下: