C# CAD二次开发:RotatedDimension 旋转标注完全指南
-
- 前言
- [一、RotatedDimension 概述](#一、RotatedDimension 概述)
-
- [1.1 什么是旋转标注](#1.1 什么是旋转标注)
- [1.2 适用场景](#1.2 适用场景)
- [1.3 类继承关系](#1.3 类继承关系)
- 二、创建旋转标注的核心要素
-
- [2.1 三个关键点详解](#2.1 三个关键点详解)
- [2.2 Rotation 旋转角度](#2.2 Rotation 旋转角度)
- 三、文字位置控制属性详解
-
- [3.1 Dimtad ------ 文字垂直位置](#3.1 Dimtad —— 文字垂直位置)
- [3.2 Dimtih ------ 文字对齐方式](#3.2 Dimtih —— 文字对齐方式)
- [3.3 Dimgap ------ 文字与尺寸线间距](#3.3 Dimgap —— 文字与尺寸线间距)
- [3.4 Dimtix ------ 文字强制内侧](#3.4 Dimtix —— 文字强制内侧)
- [3.5 常用文字排布组合方案](#3.5 常用文字排布组合方案)
- 四、完整代码示例
-
- [4.1 基础旋转标注方法](#4.1 基础旋转标注方法)
- [4.2 批量标注平行线](#4.2 批量标注平行线)
- [五、RotatedDimension 属性速查表](#五、RotatedDimension 属性速查表)
-
- [5.1 几何定位属性](#5.1 几何定位属性)
- [5.2 文字控制属性](#5.2 文字控制属性)
- [5.3 主单位控制属性](#5.3 主单位控制属性)
- [5.4 箭头与符号属性](#5.4 箭头与符号属性)
- [5.5 公差控制属性](#5.5 公差控制属性)
- 六、常见问题与解决方案
-
- [6.1 标注文字不显示](#6.1 标注文字不显示)
- [6.2 标注方向错误](#6.2 标注方向错误)
- [6.3 文字位置偏移](#6.3 文字位置偏移)
- [6.4 标注值不精确](#6.4 标注值不精确)
- 七、总结
前言
在CAD二次开发中,标注是图纸表达的核心元素之一。RotatedDimension(旋转标注)作为AutoCAD .NET API中最常用的线性标注类型,能够创建与坐标轴成任意角度的尺寸标注。本文将深入解析 RotatedDimension 的使用方法、属性配置以及常见问题的解决方案。
一、RotatedDimension 概述
1.1 什么是旋转标注
RotatedDimension 是 Dimension 类的派生类,用于创建线性标注。与普通的 AlignedDimension(对齐标注)不同,旋转标注允许开发者指定尺寸线的旋转角度,使其不局限于平行于被标注对象的走向。
1.2 适用场景
| 场景 | 说明 |
|---|---|
| 建筑平面图 | 标注斜墙、坡道、楼梯等倾斜构件 |
| 机械制图 | 标注斜孔中心距、斜面投影尺寸 |
| 轴测图标注 | 在等轴测视图中创建符合投影规则的标注 |
| 任意角度标注 | 需要尺寸线保持特定角度而非平行于对象的情况 |
1.3 类继承关系
DBObject
└── Entity
└── Dimension
└── RotatedDimension
二、创建旋转标注的核心要素
一个完整的旋转标注需要确定以下三个关键点和两个核心参数:
csharp
RotatedDimension dimension = new RotatedDimension
{
XLine1Point = dimLine1Point, // 第一条尺寸界线起点
XLine2Point = dimLine2Point, // 第二条尺寸界线起点
DimLinePoint = dimLinePoint, // 尺寸线位置点
Rotation = rotationAngle, // 尺寸线旋转角度(弧度)
Text = "" // 空字符串表示自动测量
};
2.1 三个关键点详解
| 点 | 类型 | 说明 | 示例值 |
|---|---|---|---|
XLine1Point |
Point3d |
第一条尺寸界线的起点,通常取第一条线的端点 | (10, 10, 0) |
XLine2Point |
Point3d |
第二条尺寸界线的起点,通常取第二条线的端点 | (50, 30, 0) |
DimLinePoint |
Point3d |
尺寸线的经过点,用于确定尺寸线距离被标注对象的偏移距离 | (30, 40, 0) |
旋转标注构成
XLine1Point
第一条尺寸界线
XLine2Point
第二条尺寸界线
DimLinePoint
尺寸线位置
Rotation
尺寸线倾斜角度
2.2 Rotation 旋转角度
Rotation 属性以弧度为单位,定义尺寸线的倾斜角度:
| 角度 | 弧度值 | 效果 |
|---|---|---|
| 0° | 0 |
水平尺寸线 |
| 30° | Math.PI / 6 |
右上方倾斜 |
| 45° | Math.PI / 4 |
45度倾斜 |
| 90° | Math.PI / 2 |
垂直尺寸线 |
| 135° | 3 * Math.PI / 4 |
左上方倾斜 |
计算两条平行线标注角度的常用方法:
csharp
private double GetDimensionRotation(Line line1, Line line2)
{
// 获取直线的方向角度(相对于X轴)
Vector3d direction = line1.EndPoint - line1.StartPoint;
double angle = Math.Atan2(direction.Y, direction.X);
// 标注线与线段垂直,所以需要加上90度(π/2)
return angle + Math.PI / 2;
}
三、文字位置控制属性详解
文字放置是标注美观性的关键,以下四个属性共同决定了文字的最终位置:
3.1 Dimtad ------ 文字垂直位置
| 值 | 含义 | 视觉效果 |
|---|---|---|
0 |
文字在尺寸线中间,打断尺寸线 | 尺寸线从文字中间穿过 |
1 |
文字在尺寸线上方 | 文字悬浮于尺寸线之上 |
2 |
文字在尺寸线外侧 | 文字远离被标注对象 |
3 |
遵循JIS标准放置 | 日本工业标准规范 |
3.2 Dimtih ------ 文字对齐方式
| 值 | 含义 | 效果说明 |
|---|---|---|
true |
文字始终水平 | 无论尺寸线如何倾斜,文字始终保持可读的水平方向 |
false |
文字与尺寸线平行 | 文字随尺寸线一同旋转,保持与尺寸线对齐 |
3.3 Dimgap ------ 文字与尺寸线间距
csharp
// 建议根据文字高度动态计算
dimension.Dimgap = textHeight * 0.3; // 间距为文字高度的30%
3.4 Dimtix ------ 文字强制内侧
| 值 | 含义 | 适用场景 |
|---|---|---|
true |
强制文字置于尺寸界线内侧 | 空间充足时保证整齐排布 |
false |
允许文字放置于尺寸界线外侧 | 狭窄空间避免文字重叠 |
3.5 常用文字排布组合方案
| 组合名称 | Dimtad | Dimtih | 效果描述 |
|---|---|---|---|
| 标准水平 | 0 |
true |
文字水平居中,打断尺寸线(CAD默认) |
| 标准对齐 | 0 |
false |
文字与尺寸线平行,打断尺寸线 |
| 上方水平 | 1 |
true |
文字在尺寸线上方,始终保持水平 |
| 上方对齐 | 1 |
false |
文字在尺寸线上方,随尺寸线旋转 |
| 建筑标注 | 1 |
true |
常用于建筑图纸,清晰易读 |
四、完整代码示例
4.1 基础旋转标注方法
csharp
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
/// <summary>
/// 创建旋转标注
/// </summary>
/// <param name="line1">第一条线段</param>
/// <param name="line2">第二条线段</param>
/// <param name="offset">尺寸线偏移距离</param>
/// <param name="textHeight">文字高度</param>
public static void CreateRotatedDimension(Line line1, Line line2, double offset, double textHeight)
{
Database db = HostApplicationServices.WorkingDatabase;
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);
// 计算标注方向
Vector3d direction = line1.EndPoint - line1.StartPoint;
double angle = Math.Atan2(direction.Y, direction.X);
double dimRotation = angle + Math.PI / 2; // 垂直方向
// 计算尺寸线位置(两条线中点连线的中点 + 偏移)
Point3d mid1 = line1.StartPoint + (line1.EndPoint - line1.StartPoint) * 0.5;
Point3d mid2 = line2.StartPoint + (line2.EndPoint - line2.StartPoint) * 0.5;
Point3d center = mid1 + (mid2 - mid1) * 0.5;
Vector3d perpDirection = new Vector3d(-direction.Y, direction.X, 0).GetNormal();
Point3d dimLinePoint = center + perpDirection * offset;
// 创建标注对象
RotatedDimension dimension = new RotatedDimension
{
XLine1Point = line1.StartPoint,
XLine2Point = line2.StartPoint,
DimLinePoint = dimLinePoint,
Rotation = dimRotation,
Text = "", // 自动测量
DimensionStyle = db.Dimstyle
};
// 配置文字样式
dimension.Dimtad = 1; // 文字在尺寸线上方
dimension.Dimtih = false; // 文字与尺寸线平行
dimension.Dimgap = textHeight * 0.3; // 文字间距
dimension.Dimtix = true; // 强制文字在内侧
dimension.Dimdec = 2; // 保留两位小数
// 添加到模型空间
btr.AppendEntity(dimension);
tr.AddNewlyCreatedDBObject(dimension, true);
tr.Commit();
}
}
4.2 批量标注平行线
csharp
/// <summary>
/// 批量标注平行线段
/// </summary>
public static void BatchDimensionParallelLines(List<Line> lines, double offset, double textHeight)
{
if (lines.Count < 2) return;
// 按起点X坐标排序
lines.Sort((a, b) => a.StartPoint.X.CompareTo(b.StartPoint.X));
Database db = HostApplicationServices.WorkingDatabase;
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);
for (int i = 0; i < lines.Count - 1; i++)
{
Line line1 = lines[i];
Line line2 = lines[i + 1];
// 计算标注方向
Vector3d direction = line1.EndPoint - line1.StartPoint;
double angle = Math.Atan2(direction.Y, direction.X);
// 计算尺寸线位置
Point3d mid1 = GetMidPoint(line1);
Point3d mid2 = GetMidPoint(line2);
Point3d center = GetMidPoint(mid1, mid2);
Vector3d perpDirection = new Vector3d(-direction.Y, direction.X, 0).GetNormal();
Point3d dimLinePoint = center + perpDirection * offset;
// 创建标注
RotatedDimension dim = new RotatedDimension
{
XLine1Point = line1.StartPoint,
XLine2Point = line2.StartPoint,
DimLinePoint = dimLinePoint,
Rotation = angle + Math.PI / 2,
Text = "",
DimensionStyle = db.Dimstyle,
Dimtad = 1,
Dimtih = false,
Dimgap = textHeight * 0.3
};
btr.AppendEntity(dim);
tr.AddNewlyCreatedDBObject(dim, true);
}
tr.Commit();
}
}
private static Point3d GetMidPoint(Line line)
{
return line.StartPoint + (line.EndPoint - line.StartPoint) * 0.5;
}
private static Point3d GetMidPoint(Point3d p1, Point3d p2)
{
return p1 + (p2 - p1) * 0.5;
}
五、RotatedDimension 属性速查表
5.1 几何定位属性
| 属性名 | 类型 | 必填 | 说明 |
|---|---|---|---|
XLine1Point |
Point3d |
✅ | 第一条尺寸界线起点 |
XLine2Point |
Point3d |
✅ | 第二条尺寸界线起点 |
DimLinePoint |
Point3d |
✅ | 尺寸线经过点 |
Rotation |
double |
✅ | 尺寸线旋转角度(弧度) |
TextPosition |
Point3d |
❌ | 文字位置(通常自动计算) |
5.2 文字控制属性
| 属性名 | 类型 | 默认值 | 说明 |
|---|---|---|---|
Text |
string |
"" |
标注文字,空字符串自动测量 |
TextRotation |
double |
0 |
文字旋转角度(弧度) |
Dimtad |
int |
0 |
文字垂直位置 |
Dimtih |
bool |
true |
文字是否水平 |
Dimtix |
bool |
false |
是否强制文字在内侧 |
Dimgap |
double |
0.09 |
文字与尺寸线间距 |
Dimtmove |
int |
0 |
文字移动行为 |
Dimtxsty |
ObjectId |
- | 文字样式ID |
5.3 主单位控制属性
| 属性名 | 类型 | 默认值 | 说明 |
|---|---|---|---|
Dimlfac |
double |
1.0 |
线性比例因子 |
Dimdec |
int |
4 |
小数位数 |
Dimrnd |
double |
0 |
舍入值 |
Dimpost |
string |
"" |
前后缀 |
Dimzin |
int |
0 |
零抑制 |
Dimalt |
bool |
false |
是否显示换算单位 |
Dimaltf |
double |
25.4 |
换算单位比例因子 |
5.4 箭头与符号属性
| 属性名 | 类型 | 默认值 | 说明 |
|---|---|---|---|
Dimasz |
double |
0.18 |
箭头大小 |
Dimsah |
bool |
false |
是否使用自定义箭头 |
Dimblk1 |
ObjectId |
- | 第一个箭头块 |
Dimblk2 |
ObjectId |
- | 第二个箭头块 |
Dimse1 |
bool |
false |
是否抑制第一条尺寸界线 |
Dimse2 |
bool |
false |
是否抑制第二条尺寸界线 |
Dimdle |
double |
0 |
尺寸线超出量 |
Dimdli |
double |
0.38 |
基线间距 |
5.5 公差控制属性
| 属性名 | 类型 | 默认值 | 说明 |
|---|---|---|---|
Dimtol |
bool |
false |
是否显示公差 |
Dimtdec |
int |
4 |
公差小数位数 |
Dimtp |
double |
0 |
上偏差 |
Dimtm |
double |
0 |
下偏差 |
Dimtolj |
int |
0 |
公差对齐方式 |
Dimtzin |
int |
0 |
公差零抑制 |
六、常见问题与解决方案
6.1 标注文字不显示
原因: 文字高度设置过小或文字颜色与背景色相同。
解决方案:
csharp
dimension.Dimtxt = 2.5; // 设置文字高度
dimension.Color = Color.FromRgb(255, 255, 255); // 设置颜色
6.2 标注方向错误
原因: Rotation 角度计算有误。
解决方案:
csharp
// 正确计算标注方向(垂直于线段)
Vector3d lineDir = line.EndPoint - line.StartPoint;
double rotation = Math.Atan2(lineDir.Y, lineDir.X) + Math.PI / 2;
// 确保角度在合理范围内
if (rotation > Math.PI * 2) rotation -= Math.PI * 2;
if (rotation < 0) rotation += Math.PI * 2;
6.3 文字位置偏移
原因: DimLinePoint 计算不准确。
解决方案:
csharp
// 确保尺寸线点位于垂直于线段的正确方向
Vector3d perpDir = new Vector3d(-lineDir.Y, lineDir.X, 0).GetNormal();
Point3d correctPoint = centerPoint + perpDir * offsetDistance;
6.4 标注值不精确
原因: 未设置小数位数或舍入规则。
解决方案:
csharp
dimension.Dimdec = 2; // 保留2位小数
dimension.Dimrnd = 0; // 不舍入
dimension.Dimlfac = 1.0; // 线性比例因子为1
七、总结
RotatedDimension 是CAD二次开发中功能强大且灵活的标注类型。掌握其三个关键点的确定方法和文字位置控制属性,能够满足绝大多数工程图纸的标注需求。
核心要点回顾:
- 三个必设点 :
XLine1Point、XLine2Point、DimLinePoint - 一个核心参数 :
Rotation(弧度制) - 四个文字控制属性 :
Dimtad、Dimtih、Dimgap、Dimtix
建议在实际开发中,先通过CAD软件手动创建标注并观察效果,再对照本文的属性表进行代码实现,这样可以更直观地理解每个属性的作用。
扩展阅读:
- AutoCAD .NET API 官方文档 - Dimension 类
- C# CAD二次开发系列教程 - 标注样式管理
本文基于 AutoCAD 2024 .NET API 编写,适用于大多数现代版本。