GLS测试组_LY 发表于 2022-5-19 18:54:01

标注归一

本帖最后由 GLS测试组_LY 于 2022-5-26 09:52 编辑

在低版本的revit中,如果标注的内容一样的话是不能用一个标注来标注多个element的;想到一个折中的方法就是把相同标注内容的标注放到同一个位置来实现效果,如下图:


分享一段小代码,revit2018亲测有效。

namespace 标注归一
{
    //开发需求:把相同的注释放到同一个位置
   

    class OneLeadDIM : IExternalCommand
    {
      public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
      {
            Autodesk.Revit.UI.UIApplication uiApp = commandData.Application;
            Autodesk.Revit.ApplicationServices.Application app = uiApp.Application;
            Autodesk.Revit.UI.UIDocument uidoc = uiApp.ActiveUIDocument;
            Autodesk.Revit.DB.Document doc = uidoc.Document;



            Transaction ts = new Transaction(doc, "标注归一");

            try
            {
                Selection sel = uidoc.Selection;
                Reference refe1 = sel.PickObject(ObjectType.Element,new IndependentTagFilter(),"选择基准标注");
                Element ele1 = doc.GetElement(refe1) as Element;
                XYZ point = null;
                TagOrientation tagOrientation =new TagOrientation();

                string tagtext = null;
                IndependentTag independentTag = ele1 as IndependentTag;
                ts.Start();
                if (independentTag != null)
                {
                  point = independentTag.TagHeadPosition;
                  tagtext = independentTag.TagText;
                  tagOrientation=independentTag.TagOrientation;
                }

                IList<Reference> refs = sel.PickObjects(ObjectType.Element,new IndependentTagFilter(), "选择要合并的标注");
                foreach (var reference in refs)
                {
                  Element ele2 = doc.GetElement(reference) as Element;
                  IndependentTag independentTag2 = ele2 as IndependentTag;
                  if (independentTag2 != null&&independentTag2.TagText== tagtext)
                  {
                        independentTag2.TagHeadPosition = point;
                        independentTag2.TagOrientation = tagOrientation;
                  }
                }
                ts.Commit();
                return Result.Succeeded;

            }
            catch (Autodesk.Revit.Exceptions.OperationCanceledException)//防止按ESC报错
            {
                ts.RollBack();
                return Result.Cancelled;
            }
            catch (Exception eee)
            {

                message = eee.Message;
                ts.RollBack();
                return Autodesk.Revit.UI.Result.Failed;
            }


      }



    }
    public class IndependentTagFilter : ISelectionFilter
    {
      public bool AllowElement(Element elem)
      {
            if (elem is IndependentTag)
            {
                return true;
            }
            return false;
      }

      public bool AllowReference(Reference reference, XYZ position)
      {
            return true;
      }
    }
}



页: [1]
查看完整版本: 标注归一