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
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值