1. 程式人生 > >Revit二次開發--為管道新增標註

Revit二次開發--為管道新增標註

程式碼如下:

	 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版本,如有問題,請多多指教;