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