代码如下:
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uiDoc = commandData.Application.ActiveUIDocument;
Document doc = uiDoc.Document;
Pipe pipe = doc.GetElement(new ElementId(1595857)) as Pipe;
View view = doc.ActiveView;
TagMode tagMode = TagMode.TM_ADDBY_CATEGORY;
TagOrientation tagOrientation = TagOrientation.Horizontal;//标记的朝向
LocationCurve location = pipe.Location as LocationCurve;
XYZ mid = location.Curve.Evaluate(0.5,true);//取得管道的中心点
Reference reference = new Reference(pipe);//由已知element取得Reference的方法
using (Transaction trans = new Transaction(doc))
{
trans.Start("Creat IndependentTag");
IndependentTag tag = IndependentTag.Create(doc, view.Id, reference, true, tagMode, tagOrientation, mid);
if (null == tag)
{
throw new Exception("Create IndependentTag Failed.");
}
tag.LeaderEndCondition = LeaderEndCondition.Free;
tag.HasLeader = false;//不要箭头
//XYZ elbowPnt = mid + new XYZ(2.0, 2.0, 0.0);
//tag.LeaderElbow = elbowPnt;//有箭头的话,箭头拐点
XYZ headerPnt = mid + new XYZ(2.0, 2.0, 0.0);
tag.TagHeadPosition = headerPnt;//标记的位置
trans.Commit();
}
return Result.Succeeded;
}
Revit2018版本,如有问题,请多多指教;