我们知道API中有提供一个获取点Reference的方法,如下图
可是我们每次通过线取的时候,都会报错,取不到对应的Reference。这里我也去试了一下,发现其中的问题,这个对线取点的话,需要是通过构件的Geometry里面的Curve才是可以的。下面演示的是选择一根modelCurve为例。
UIDocument uidoc = commandData.Application.ActiveUIDocument;
Document doc = uidoc.Document;
Reference selRef = uidoc.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element);
ModelCurve mc = doc.GetElement(selRef) as ModelCurve;
GeometryElement geo = mc.get_Geometry(new Options() { View=doc.ActiveView,ComputeReferences=true});
foreach (var geoObj in geo)
{
Curve c = geoObj as Curve;
if (c == null) continue;
ReferenceArray rArr = new ReferenceArray();
rArr.Append(c.GetEndPointReference(0));
rArr.Append(c.GetEndPointReference(1));
Transaction trans = new Transaction(doc, "标注");
trans.Start();
doc.Create.NewDimension(doc.ActiveView, c as Line, rArr);
trans.Commit();
}
return Result.Succeeded;
实现效果如下:
对于其他的构件,实现过程都是相通的,可以自行实现。今天的分享就到这里啦,你的点赞将是对我对大的鼓励~~