Revit API創建標注NewTag


start
///   <summary>
///  創建水管管徑標注
///   </summary>
[Transaction(TransactionMode.Manual)]
public  class CreatPipeDiameterTag : IExternalCommand
{
     #region IExternalCommand Members

     public Result Execute(ExternalCommandData commandData,  ref  string message, ElementSet elements)
    {
        UIDocument uiDoc = commandData.Application.ActiveUIDocument;
        Document doc = uiDoc.Document; // 當前活動文檔
        Autodesk.Revit.DB.View view = uiDoc.ActiveView; // 當前活動視圖
        Selection sel = uiDoc.Selection; // 選擇集
        Transaction ts =  new Transaction(doc,  " 水管管徑標記 ");
         try
        {
            ts.Start();
            PipeSelectionFilter psf =  new PipeSelectionFilter(doc);
            Reference refer = sel.PickObject(ObjectType.Element, psf,  " 請選擇要標注的水管: ");
            Pipe pipe = doc.GetElement(refer)  as Pipe;
             if (pipe ==  null)
            {
                ts.Dispose();
                TaskDialog.Show( " RevitMassge "" 沒有選中水管 ");
                 return Result.Failed;
            }
             // Define tag mode and tag orientation for new tag
            TagMode tageMode = TagMode.TM_ADDBY_CATEGORY;
            TagOrientation tagOri = TagOrientation.Horizontal;
             // Add the tag to the middle of duct
            LocationCurve locCurve = pipe.Location  as LocationCurve;
            XYZ pipeMid = locCurve.Curve.Evaluate( 0.275true);
            IndependentTag tag = doc.Create.NewTag(view, pipe,  false, tageMode, tagOri, pipeMid);
             // 遍歷類型
            FilteredElementCollector filterColl = GetElementsOfType(doc,  typeof(FamilySymbol), BuiltInCategory.OST_PipeTags);
             // WinFormTools.MsgBox(filterColl.ToElements().Count.ToString());
             int elId =  0;
             foreach (Element el  in filterColl.ToElements())
            {
                 if (el.Name ==  " 管道尺寸標記 ")
                    elId = el.Id.IntegerValue;
            }
            tag.ChangeTypeId( new ElementId(elId));
            ElementId eId =  null;
             if (tag ==  null)
            {
                ts.Dispose();
                TaskDialog.Show( " RevitMassge "" 創建標注失敗! ");
                 return Result.Failed;
            }
            ICollection<ElementId> eSet = tag.GetValidTypes();
             foreach (ElementId item  in eSet)
            {
                 if (item.IntegerValue ==  532753)
                {
                    eId = item;
                }
            }
            tag = doc.get_Element(eId)  as IndependentTag;
        }
         catch (Exception)
        {
            ts.Dispose();
             return Result.Cancelled;
        }
        ts.Commit();

         return Result.Succeeded;
    }
    FilteredElementCollector GetElementsOfType(Document doc, Type type, BuiltInCategory bic)
    {
        FilteredElementCollector collector =  new FilteredElementCollector(doc);

        collector.OfCategory(bic);
        collector.OfClass(type);

         return collector;
    }
     #endregion
}


///   <summary>
/// 水管選擇過濾器
///   </summary>
public  class PipeSelectionFilter : ISelectionFilter
{
     #region ISelectionFilter Members

    Document doc =  null;
     public PipeSelectionFilter(Document document)
    {
        doc = document;
    }

     public  bool AllowElement(Element elem)
    {
         return elem  is Pipe;
    }

     public  bool AllowReference(Reference reference, XYZ position)
    {
         return doc.GetElement(reference)  is Pipe;
    }

     #endregion
}
end


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM