实体操作
本章涵盖 AutoCAD .NET API 中最常用的实体类型:直线、圆、圆弧、多段线、文字等,以及它们的创建、修改和查询。
实体继承层次
DBObject
└── Entity (所有图形对象的基类)
├── Curve (曲线基类)
│ ├── Line
│ ├── Arc
│ ├── Circle
│ ├── Ellipse
│ ├── Polyline
│ ├── Polyline2d
│ ├── Polyline3d
│ └── Spline
├── BlockReference (块引用/插入)
├── DBText (单行文字)
├── MText (多行文字)
├── Hatch (填充)
├── Dimension (标注)
├── MLine (多线)
└── ...Note
详细请看最后的补充
常用几何类型(Geometry 命名空间)
using Autodesk.AutoCAD.Geometry;
// Point3d —— 三维点(值类型 struct)
Point3d pt = new Point3d(10, 20, 0);
double x = pt.X;
// Vector3d —— 三维向量
Vector3d v = new Vector3d(1, 0, 0);
Vector3d normal = Vector3d.ZAxis; // (0, 0, 1)
// Matrix3d —— 变换矩阵
Matrix3d mat = Matrix3d.Displacement(new Vector3d(100, 0, 0));
Point3d moved = pt.TransformBy(mat); // 平移 (110, 20, 0)Line(直线)
// 创建
Line line = new Line(startPoint, endPoint);
line.Layer = "DIM";
line.ColorIndex = 3; // AutoCAD 颜色索引(ACI)
// 属性
double length = line.Length;
Point3d start = line.StartPoint;
Point3d end = line.EndPoint;
double angle = line.Angle; // 弧度
Vector3d delta = line.Delta; // 从起点到终点的向量
// 修改
line.StartPoint = new Point3d(0, 0, 0);
line.EndPoint = new Point3d(200, 0, 0);
// 创建无限线与射线
Xline xline = new Xline(); // 构造线
Ray ray = new Ray(); // 射线Circle(圆)
// 创建方式 1:圆心 + 法向 + 半径
Circle circle = new Circle(
new Point3d(0, 0, 0),
Vector3d.ZAxis,
50.0
);
// 创建方式 2:三点定圆
CircularArc3d ca = new CircularArc3d(p1, p2, p3);
Circle circle2 = new Circle();
circle2.Set(ca);
// 属性
double radius = circle.Radius;
Point3d center = circle.Center;
double area = circle.Area;
double circumference = circle.Circumference;
double diameter = circle.Diameter; // getter / setterArc(圆弧)
// 圆心 + 半径 + 起止角度(角度为绝对角度,弧度制)
Arc arc = new Arc(
center: new Point3d(0, 0, 0),
radius: 50.0,
startAngle: 0.0, // 0° = 向东
endAngle: Math.PI / 2 // 90° = 向北
);
// 三点定弧
Arc arc2 = new Arc();
arc2.CreateFromPoints(startPoint, midPoint, endPoint);
// 属性
double arcRadius = arc.Radius;
Point3d arcCenter = arc.Center;
double startAngle = arc.StartAngle;
double endAngle = arc.EndAngle;
double totalAngle = arc.TotalAngle; // 圆弧夹角
double arcLength = arc.Length;
double bulge = arc.GetBulgeAt(0); // 凸度Polyline(多段线)
// 创建(推荐使用 Polyline 而非 Polyline2d)
Polyline pl = new Polyline();
// 添加顶点
pl.AddVertexAt(0, new Point2d(0, 0), 0, 0, 0);
pl.AddVertexAt(1, new Point2d(100, 0), 0, 0, 0);
pl.AddVertexAt(2, new Point2d(100, 100), 1.0, 0, 0); // bulge=1 → 90° 弧
pl.AddVertexAt(3, new Point2d(0, 100), 0, 0, 0);
pl.Closed = true; // 闭合多段线
// 属性
int vertexCount = pl.NumberOfVertices;
double plLength = pl.Length;
double plArea = pl.Area;
bool closed = pl.Closed;
// 遍历顶点
for (int i = 0; i < pl.NumberOfVertices; i++)
{
Point2d vertex = pl.GetPoint2dAt(i);
double bulge = pl.GetBulgeAt(i);
double startWidth = pl.GetStartWidthAt(i);
double endWidth = pl.GetEndWidthAt(i);
}
// 修改顶点
pl.SetPointAt(2, new Point2d(150, 150));
pl.SetBulgeAt(2, 0.5);
// 凸度 (Bulge) 与弧
// bulge = tan(夹角/4)
// bulge > 0 → 逆时针弧
// bulge < 0 → 顺时针弧
// bulge = 0 → 直线段🔬 凸度 公式:
bulge = tan(θ/4),其中 θ 是弧段夹角(弧度制)。bulge = 1表示 90° 弧(π/2),bulge = tan(π/8) ≈ 0.414表示 45° 弧。
文字
单行文字 DBText
DBText text = new DBText();
text.Position = new Point3d(10, 10, 0);
text.Height = 5.0;
text.TextString = "Hello AutoCAD";
text.Rotation = 0; // 旋转角度(弧度)
text.HorizontalMode = TextHorizontalMode.TextCenter;
text.VerticalMode = TextVerticalMode.TextVerticalMid;
text.AlignmentPoint = new Point3d(50, 50, 0); // 对齐点多行文字 MText
MText mtext = new MText();
mtext.Location = new Point3d(10, 10, 0);
mtext.TextHeight = 5.0;
mtext.Width = 100; // 文本框宽度
mtext.Contents = "第一行\\P第二行\\P{\\fArial|b1|i1|c256;格式化文字}";
// \\P = 换段
// \\fFontName; = 字体
// \\H2x; = 高度倍数
// {\\C256;...} = 颜色Curve 基类通用方法
所有曲线(Line, Arc, Circle, Polyline, Spline…)共享 Curve 基类的方法:
Curve curve = ...; // 任意曲线
// 长度
double length = curve.Length;
// 面积(闭合时)
double area = curve.Area;
// 参数与距离
double param = curve.GetParameterAtDistance(50);
double dist = curve.GetDistanceAtParameter(param);
Point3d pt = curve.GetPointAtDist(50); // 距起点 50 单位的点
Vector3d deriv = curve.GetFirstDerivative(pt); // 该点切线方向
// 最近点
Point3d closestPoint = curve.GetClosestPointTo(targetPoint, false);
// 偏移(生成平行曲线)
DBObjectCollection offsetCurves = curve.GetOffsetCurves(5.0);
// 打断
DBObjectCollection pieces = curve.GetSplitCurves(
new DoubleCollection(new[] { param1, param2 }));实体通用属性与方法
// 所有 Entity 都有以下属性
entity.Layer = "WALL";
entity.ColorIndex = 3; // ACI 颜色 1-255
entity.Color = Color.FromRgb(255, 128, 0); // TrueColor
entity.Linetype = "DASHED";
entity.LineWeight = LineWeight.LineWeight050;
entity.LinetypeScale = 1.0;
entity.Transparency = new Transparency(50); // 0-255,255 不透明
entity.Visible = true;
// 几何变换
Matrix3d rotation = Matrix3d.Rotation(Math.PI/4, Vector3d.ZAxis, basePoint);
entity.TransformBy(rotation);
// 删除
entity.Erase(); // 并非立即删除,而是标记为已删除(可通过 Undo 恢复)
// 克隆
Entity copy = entity.Clone() as Entity;添加实体完整示例
[CommandMethod("CREATEENTITIES")]
public void CreateEntities()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord modelSpace = (BlockTableRecord)tr.GetObject(
bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
// 创建多种实体
var entities = new List<Entity>
{
new Line(new Point3d(0, 0, 0), new Point3d(100, 0, 0)) { Layer = "0" },
new Circle(new Point3d(50, 50, 0), Vector3d.ZAxis, 30) { Layer = "0" },
new Arc(new Point3d(100, 100, 0), 20, 0, Math.PI) { Layer = "0" }
};
// 批量添加
foreach (Entity entity in entities)
{
modelSpace.AppendEntity(entity);
tr.AddNewlyCreatedDBObject(entity, true);
}
tr.Commit();
}
}修改实体
[CommandMethod("MODIFYLINES")]
public void ModifyLines()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord modelSpace = (BlockTableRecord)tr.GetObject(
bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
int count = 0;
foreach (ObjectId id in modelSpace)
{
Line line = tr.GetObject(id, OpenMode.ForWrite) as Line;
if (line != null && line.Layer == "WALL")
{
line.ColorIndex = 1; // 改为红色
count++;
}
}
tr.Commit();
ed.WriteMessage($"\n修改了 {count} 条直线。");
}
}详细补充
DBObject (所有数据库对象的基类)
└── Entity (所有可见图形对象的基类)
│
├── Curve (曲线基类,有长度、起点、终点等)
│ ├── Line (直线)
│ │ ├── StartPoint 起点 (Point3d)
│ │ ├── EndPoint 终点 (Point3d)
│ │ └── Length 长度 (double)
│ │
│ ├── Arc (圆弧)
│ │ ├── Center 圆心 (Point3d)
│ │ ├── Radius 半径 (double)
│ │ ├── StartAngle 起始角 (double,弧度)
│ │ └── EndAngle 终止角 (double,弧度)
│ │
│ ├── Circle (圆)
│ │ ├── Center 圆心 (Point3d)
│ │ ├── Radius 半径 (double)
│ │ └── Diameter 直径 (double)
│ │
│ ├── Ellipse (椭圆)
│ │ ├── Center 中心点 (Point3d)
│ │ ├── MajorAxis 长轴方向 (Vector3d)
│ │ ├── MinorAxis 短轴方向 (Vector3d)
│ │ ├── RadiusRatio 长短轴比例 (double)
│ │ └── StartAngle/EndAngle 起始/终止角
│ │
│ ├── Polyline (轻量多段线,LWPOLYLINE,最常用)
│ │ ├── NumberOfVertices 顶点数 (int)
│ │ ├── GetPoint2dAt(i) 获取第i个顶点 (Point2d)
│ │ ├── GetBulgeAt(i) 获取第i个顶点的凸度 (double)
│ │ ├── Closed 是否闭合 (bool)
│ │ ├── Area 面积 (double)
│ │ └── Length 总长度 (double)
│ │
│ ├── Polyline2d (旧版二维多段线,已少用)
│ │ └── 类似 Polyline,但顶点是 Vertex2d
│ │
│ ├── Polyline3d (三维多段线)
│ │ └── 类似 Polyline,但顶点是 Point3d
│ │
│ └── Spline (样条曲线)
│ ├── Degree 次数/阶数 (int)
│ ├── ControlPoints 控制点集合
│ └── FitPoints 拟合点集合
│
├── BlockReference (块引用/块参照)← 你的考题核心!
│ ├── Position 插入点 (Point3d)
│ ├── Rotation 旋转角 (double,弧度)
│ ├── ScaleFactors 缩放因子 (三维向量)
│ ├── BlockTableRecord 引用的块定义 (ObjectId)
│ ├── Name 块名称 (string) — 通过BlockTableRecord获取
│ └── AttributeCollection 属性集合 (如果有属性定义)
│
├── DBText (单行文字)
│ ├── TextString 文字内容 (string)
│ ├── Position 定位点 (Point3d)
│ ├── Height 字高 (double)
│ ├── Rotation 旋转角度 (double)
│ ├── Oblique 倾斜角 (double)
│ └── WidthFactor 宽度因子 (double)
│
├── MText (多行文字)
│ ├── Contents 文字内容 (string,含格式化代码)
│ ├── Text Contents的纯文本版本
│ ├── Location 定位点 (Point3d)
│ ├── Height 字高 (double)
│ ├── Width 框宽度 (double)
│ ├── Rotation 旋转角度 (double)
│ └── LineSpacingFactor 行距因子 (double)
│
├── Hatch (填充图案)
│ ├── PatternName 填充图案名称 (string)
│ ├── Area 面积 (double)
│ ├── Angle 角度 (double)
│ ├── Scale 比例 (double)
│ └── Associative 是否关联 (bool,随边界变化)
│
├── Dimension (标注,基类,下面有子类)
│ ├── AlignedDimension 对齐标注
│ ├── LinearDimension 线性标注
│ ├── RadialDimension 半径标注
│ ├── DiametricDimension 直径标注
│ ├── AngularDimension 角度标注
│ └── OrdinateDimension 坐标标注
│ └── 共同属性:
│ ├── DimLinePoint 尺寸线位置
│ ├── DimText 标注文字
│ ├── DimScale 标注比例
│ └── TextPosition 文字位置
│
├── MLine (多线,多条平行线)
│ ├── StyleName 多线样式名称 (string)
│ ├── Justification 对齐方式 (int)
│ ├── Scale 比例 (double)
│ └── Vertices 顶点集合
│
└── ... (还有很多子类)