前言
Revit 作为一款成熟的商业软件,它将自己的UI选择功能也通过 API 暴露出来。通过 API 可以按照特定的过滤规则来选择相应的元素,能力和UI基本上是等价的。这个 SDK 用四个例子展示了 API 的能力,内容如下。
内容
PickforDeletion
核心逻辑:
- 通过
m_document.Selection.PickObjects(ObjectType.Element)
选择若干个元素 - 通过
m_document.Document.Delete(elemDeleteList)
删除元素
核心代码:
// 保存需要删除的元素
List<ElementId> elemDeleteList = new List<ElementId>();
// 通过PickObjects选择多个元素
IList<Reference> eRefList = m_document.Selection.PickObjects(ObjectType.Element, "Please pick some element to delete. ESC for Cancel.");
// 将选中的元素加入选择集
foreach (Reference eRef in eRefList){if (eRef != null && eRef.ElementId != ElementId.InvalidElementId){elemDeleteList.Add(eRef.ElementId);}
}
// 删除选择集中的元素
m_document.Document.Delete(elemDeleteList);
PlaceAtPointOnWallFace
核心逻辑:
- 通过
m_document.Selection.PickObject(ObjectType.PointOnElement)
选择一个点 - 放置窗户:
m_document.Document.Create.NewFamilyInstance
核心代码:
// 选择墙上的一个点
Reference pickedRefer = m_document.Selection.PickObject(ObjectType.PointOnElement, new WallFaceFilter(m_document.Document), "Please pick a point on Wall face.");
// 找到对应的窗户类型
string symbolName = "36\" x 48\"";
FilteredElementCollector elemCollector = new FilteredElementCollector(m_document.Document);
elemCollector.WhereElementIsElementType();
var query = from element in elemCollector where element.Name == symbolName select element;
Element elemType = query.Single<Element>();
FamilySymbol windowType = elemType as FamilySymbol;
// 放置窗户
m_document.Document.Create.NewFamilyInstance(pickedRefer .GlobalPoint, windowType, m_document.Document.GetElement(pickedRefer ), StructuralType.NonStructural);
PlaceAtPickedFaceWorkplane
核心逻辑:
- 选择一个面,
m_document.Selection.PickObject(ObjectType.Face)
- 提取面,创建工作平面:
SketchPlane plane = SketchPlane.Create(m_document.Document, geometryPlane);
- 设置新的工作平面:
m_document.Document.ActiveView.SketchPlane = faceSketchPlane;m_document.Document.ActiveView.ShowActiveWorkPlane();
- 选择工作平面上的一个点:
m_document.Selection.PickPoint
- 创建模型线:
Curve circle = Arc.Create(point, 5, 0, Math.PI * 2, faceSketchPlane.GetPlane().XVec, faceSketchPlane.GetPlane().YVec);m_CreationBase.NewModelCurve(circle, faceSketchPlane);
核心代码:
// 选择一个面
Reference faceRef = m_document.Selection.PickObject(ObjectType.Face, new PlanarFaceFilter(m_document.Document), "Please pick a planar face to set the work plane. ESC for cancel.");
// 通过面的索引获取面的几何
GeometryObject geoObject = m_document.Document.GetElement(faceRef).GetGeometryObjectFromReference(faceRef);
PlanarFace planarFace = geoObject as PlanarFace;
// 创建一个工作平面
Plane geometryPlane = Plane.CreateByNormalAndOrigin(planarFace.FaceNormal, planarFace.Origin);
SketchPlane faceSketchPlane = SketchPlane.Create(m_document.Document, geometryPlane);
// 将新建的工作平面设置为当前视图的活动工作平面
m_document.Document.ActiveView.SketchPlane = faceSketchPlane;
m_document.Document.ActiveView.ShowActiveWorkPlane();
// 选择工作平面上的一个点
ObjectSnapTypes snapType = ObjectSnapTypes.Centers | ObjectSnapTypes.Endpoints | ObjectSnapTypes.Intersections | ObjectSnapTypes.Midpoints | ObjectSnapTypes.Nearest | ObjectSnapTypes.WorkPlaneGrid;
XYZ point = m_document.Selection.PickPoint(snapType, "Please pick a point to place component.");
// 以选中点为中心绘制一个圆
Curve circle = Arc.Create(point, 5, 0, Math.PI * 2, faceSketchPlane.GetPlane().XVec, faceSketchPlane.GetPlane().YVec);
m_CreationBase.NewModelCurve(circle, faceSketchPlane);
SelectionDialog
核心逻辑:
- 选择一个元素:
m_document.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element
- 选择一个目标点:
XYZ targetPoint = m_document.Selection.PickPoint("Please pick a point.")
- 移动元素到目标点:
ElementTransformUtils.MoveElement(m_document.Document,elem.Id, vecToMove);
核心代码:
// 选择要移动的元素,记录初始位置
Reference eRef = m_document.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, "Please pick an element.");
SelectedElement = m_document.Document.GetElement(eRef);
m_elemPickedPoint = eRef.GlobalPoint;
// 选择一个目标点
XYZ targetPoint = m_document.Selection.PickPoint("Please pick a point.);
// 移动元素到目标点
XYZ vecToMove = targetPoint - m_elemPickedPoint;
m_elemPickedPoint = targetPoint;
ElementTransformUtils.MoveElement(m_document.Document,elem.Id, vecToMove);
参考
创建族实例,参考博客:https://blog.csdn.net/weixin_44153630/article/details/104650998
ElementTransformUtils,参考博客:https://blog.csdn.net/weixin_44153630/article/details/103884784