Cad c#.net 一键修改标注dimension中的文本内容

本例为给标注加前缀,也可定制其他形式,效果如下:

cs 复制代码
  public class Demo
  {
      [CommandMethod("xx")]//
      public void Dim()
      {
          Document doc = Application.DocumentManager.MdiActiveDocument;

          Database db = doc.Database;

          Editor ed = doc.Editor;
          try

          {
              using (Transaction tr = db.TransactionManager.StartTransaction())

              {
                  BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);

                  BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                  foreach (ObjectId dimId in btr)

                  {

                      if (dimId.ObjectClass.Name.ToLower().Contains("dimension"))

                      {

                          Dimension dim = (Dimension)tr.GetObject(dimId, OpenMode.ForWrite);

                          string originalText = dim.DimensionText;
                          string measureText = Math.Round( dim.Measurement,3).ToString();
                          if (!string.IsNullOrEmpty(originalText))

                          {

                              dim.DimensionText = "dzb_"+ originalText ;

                          }

                          else
                          { dim.DimensionText = "dzb_" + measureText; }
                          if (!string.IsNullOrEmpty(measureText))

                          {


                              ed.WriteMessage($"\n{measureText}\n");

                          }

                      }

                  }

                  tr.Commit();

                  ed.WriteMessage("\n修改标注文本前缀成功。");
              }

          }

          catch (System.Exception ex)

          {

              ed.WriteMessage("\n发生错误: " + ex.Message);

          }

      }
  }

插件联系↓↓↓

相关推荐
吴可可1232 小时前
富怡CAD二次开发指南
c#
1-1=02 小时前
C# IO和序列化
c#·.net·.netcore
寂寞的船3 小时前
【Unity Bug挑战】第10期 SerializeReference 内存泄漏之谜
unity·c#·游戏引擎·bug
CSharp精选营3 小时前
从控制台到「会动手」的 AI:用 Semantic Kernel 搭应用(小白教程)
c#·.net·ai agent·semantickernel·functioncalling
宝桥南山1 天前
.NET 11 - 尝试一下Blazor中的新特性SupplyParameterFromTempData
microsoft·微软·c#·asp.net·.net·.netcore
1-1=01 天前
C# 中 Lambda、Linq 与 Expression
c#·linq
zhanghaha13141 天前
C# 系列教程_7 判断语句(分支)保姆级精讲
开发语言·c#
zhanghaha13141 天前
C# 系列教程_8 循环语句 保姆级精讲(面向新手)
开发语言·c#
rockey6272 天前
AScript之LINQ扩展
c#·.net·script·动态脚本
1-1=02 天前
C# 委托与事件
开发语言·c#