Revit包含以下几种交互方式:
1.先选择元素,在执行外部命令,外部命令可以获取选择元素并做相应处理。
2.先执行命令,在选择元素或者位置,选择结束后命令继续执行。
这种形式又包括:单选、多选、框选、点选、
public class RevitSelect : IExternalCommand { public Result Execute(ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements) { try { //取得当前文档 UIDocument uidoc = commandData.Application.ActiveUIDocument; //取得当前选择集 Selection selection = uidoc.Selection; //获取当前选择元素Id List<ElementId> elementIdList = selection.GetElementIds().ToList(); StringBuilder strBuild = new StringBuilder(); foreach (var id in elementIdList) { Element ele = uidoc.Document.GetElement(id); strBuild.AppendLine(ele.GetType().ToString()); } TaskDialog.Show("Revit", strBuild.ToString()); return Result.Succeeded; } catch (Exception ex) { message = ex.Message; return Result.Failed; } } }
记得加上.addin文件。
之后你可以尝试,在项目中选择一个选择,在点击外部工具下的RevitSelect
我这边的结果:
在运行外部命令过程中选取元素:
public class SelectRevit : IExternalCommand { public Result Execute(ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements) { try { UIDocument doc = commandData.Application.ActiveUIDocument; Reference pickedElemRef = doc.Selection.PickObject(ObjectType.Element); Element elem = doc.Document.GetElement(pickedElemRef.ElementId); TaskDialog.Show("Revit", string.Format("所选元素类型为: {0}", elem.GetType().ToString())); return Result.Succeeded; } catch (Exception ex) { message = ex.Message; return Result.Failed; } } }
这个效果在你点击了外部工具下的SelectRevit之后,在选择一个元素,这是会弹出你选择元素的信息
来源:https://www.cnblogs.com/hualuohuakaihuamanyuan/p/5345184.html