C#实现AutoCAD旋转与直径标注

在 AutoCAD 二次开发中,使用 C# 创建标注是一个核心功能,主要涉及旋转标注(线性标注)和直径标注等类型。其核心流程是:通过事务(Transaction)获取当前数据库的块表(BlockTable)和模型空间块表记录(BlockTableRecord),然后创建标注实体并将其添加到模型空间中。下面将分别阐述两种常见标注的创建方法、关键参数,并提供完整的代码示例。

一、 旋转标注(RotatedDimension)的创建

旋转标注通常用于标注两点间的线性距离,可以倾斜任意角度。创建它需要定义几个关键点和一个旋转角度。

关键参数说明:

参数名 类型 说明
dimLinePoint Point3d 尺寸线的位置点,决定标注文字放置在哪里。
defPoint1 Point3d 第一条尺寸界线的原点(标注起点)。
defPoint2 Point3d 第二条尺寸界线的原点(标注终点)。
dimText string 标注上显示的文字。如果为空字符串 "",则自动显示测量值。
dimStyleId ObjectId 标注样式的对象ID。通常使用数据库的当前标注样式。
rotation double 尺寸线的旋转角度(弧度制)。

创建步骤与代码示例:

  1. 在事务中获取当前数据库和模型空间。
  2. 定义标注的各个特征点。
  3. 创建 RotatedDimension 对象并设置属性。
  4. 将标注添加到模型空间并提交事务。
csharp 复制代码
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

[CommandMethod("CreateRotatedDim")]
public void CreateRotatedDimension()
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Database db = doc.Database;
    Editor ed = doc.Editor;

    using (Transaction trans = db.TransactionManager.StartTransaction())
    {
        try
        {
            // 1. 打开块表和模型空间块表记录
            BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
            BlockTableRecord btr = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

            // 2. 定义标注点
            // 假设标注一条从 (0,0,0) 到 (100,0,0) 的水平线
            Point3d defPoint1 = new Point3d(0, 0, 0); // 标注起点
            Point3d defPoint2 = new Point3d(100, 0, 0); // 标注终点
            Point3d dimLinePoint = new Point3d(50, 20, 0); // 尺寸线位置(在线段上方)
            double rotation = 0; // 旋转角度为0弧度(水平)
            string dimText = ""; // 显示自动测量值

            // 3. 创建旋转标注对象
            RotatedDimension rotDim = new RotatedDimension();
            rotDim.XLine1Point = defPoint1;
            rotDim.XLine2Point = defPoint2;
            rotDim.DimLinePoint = dimLinePoint;
            rotDim.Rotation = rotation;
            rotDim.DimensionText = dimText;
            // 使用当前标注样式
            rotDim.DimensionStyle = db.Dimstyle;

            // 4. 将标注添加到模型空间
            btr.AppendEntity(rotDim);
            trans.AddNewlyCreatedDBObject(rotDim, true);

            trans.Commit();
            ed.WriteMessage("
旋转标注创建成功!");
        }
        catch (System.Exception ex)
        {
            ed.WriteMessage($"
创建标注时出错:{ex.Message}");
            trans.Abort();
        }
    } // 事务自动释放
}

二、 直径标注(DiametricDimension)的创建

直径标注用于标注圆或圆弧的直径,其构造需要定义圆弧上的弦点(Chord Point)和远弦点(Far Chord Point)。

关键参数说明:

参数名 类型 说明
chordPoint Point3d 圆弧上的一个点(弦点)。
farChordPoint Point3d 穿过圆心、与弦点相对的另一个圆弧上的点(远弦点)。
leaderLength double 引线长度。当值为正时,标注文字在圆外;为负时,标注文字在圆内。
dimText string 标注文字。为空时自动显示带直径符号的测量值。

创建步骤与代码示例:

  1. 同样需要在事务中操作数据库和模型空间。
  2. 根据要标注的圆,计算或指定弦点和远弦点。
  3. 创建 DiametricDimension 对象并设置属性。
  4. 添加到模型空间并提交事务。
csharp 复制代码
[CommandMethod("CreateDiametricDim")]
public void CreateDiametricDimension()
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Database db = doc.Database;
    Editor ed = doc.Editor;

    using (Transaction trans = db.TransactionManager.StartTransaction())
    {
        try
        {
            BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
            BlockTableRecord btr = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

            // 假设有一个圆心在 (0,0,0),半径为 50 的圆
            Point3d center = new Point3d(0, 0, 0);
            double radius = 50;

            // 计算直径标注的两个点:圆右侧点和左侧点
            Point3d chordPoint = new Point3d(center.X + radius, center.Y, center.Z); // 右侧弦点
            Point3d farChordPoint = new Point3d(center.X - radius, center.Y, center.Z); // 左侧远弦点
            double leaderLength = 10; // 引线长度,正数表示文字在圆外

            // 创建直径标注对象
            DiametricDimension diaDim = new DiametricDimension();
            diaDim.ChordPoint = chordPoint;
            diaDim.FarChordPoint = farChordPoint;
            diaDim.LeaderLength = leaderLength;
            diaDim.DimensionText = ""; // 显示自动测量值(如:Φ100)
            diaDim.DimensionStyle = db.Dimstyle;

            btr.AppendEntity(diaDim);
            trans.AddNewlyCreatedDBObject(diaDim, true);

            trans.Commit();
            ed.WriteMessage("
直径标注创建成功!");
        }
        catch (System.Exception ex)
        {
            ed.WriteMessage($"
创建直径标注时出错:{ex.Message}");
            trans.Abort();
        }
    }
}

三、 关键技术与注意事项

  1. 事务管理 :所有对 AutoCAD 数据库对象的创建、修改和删除操作都必须在事务(Transaction)内进行,以确保数据的一致性和操作的原子性。代码中使用 using 语句可以确保事务正确释放。
  2. 对象模型访问 :必须通过 BlockTableBlockTableRecord 来访问和操作模型空间,这是 AutoCAD 数据库存储图形对象的层级结构。
  3. 标注样式 :创建标注时,通常将其 DimensionStyle 属性设置为 db.Dimstyle,以继承当前图形的标注样式。你也可以通过 db.DimStyleTableId 获取特定的标注样式ID。
  4. 用户交互 :上述示例使用了固定坐标。在实际开发中,通常需要结合 Editor 类的 GetPoint()GetEntity() 等方法获取用户输入的坐标或选择的对象,从而动态创建标注。
  5. 错误处理 :务必在事务中使用 try-catch 块,并在异常发生时调用 trans.Abort() 回滚事务,防止数据库处于不一致状态。

通过组合使用这些基础标注对象,并利用 AutoCAD .NET API 提供的其他属性和方法(如设置文字位置、覆盖文字、调整公差等),可以开发出满足各种复杂需求的标注功能。


参考来源

相关推荐
专注VB编程开发20年2 小时前
专业分析python底层调用与按键精灵,ah3等的对比,hookdll,内存加载,调用.net dll
开发语言·javascript·python·microsoft·php·.net
FL16238631292 小时前
基于C#winform实现yolo26-plate中文车牌检测识别支持12种中文双层颜色车牌文字识别
开发语言·c#
djk88882 小时前
.net swagger api 开启跨域 开启注释
java·前端·.net
Eiceblue2 小时前
锁定单元格 :C# 控制 Excel 单元格编辑权限
开发语言·c#·excel
我是唐青枫3 小时前
C#.NET YARP 详解:用 ASP.NET Core 打造高性能反向代理网关
c#·.net
火星papa12 小时前
C# 阻塞队列(BlockingCollection)
c#·queue·阻塞队列
OctShop大型商城源码17 小时前
.NET线上商城源码_C#商城源码_技术赋能下的电商新生态
开发语言·c#·.net·商城系统源码
hixiong12320 小时前
C#文件目录结构生成工具
开发语言·c#
柠檬苏打z21 小时前
C# SwaggerLoginAuthPlugin 一款给Swagger文档加登录页面的小插件
.net·swagger