DimLinePoint和TextPosition的区别
1.DimLinePoint(尺寸线点)
这是尺寸线的位置,决定了标注线本身放在哪里。
// 示例:创建标注的基本设置 AlignedDimension dim = new AlignedDimension(); dim.XLine1Point = startPoint; // 第一个标注点(起点) dim.XLine2Point = endPoint; // 第二个标注点(终点) dim.DimLinePoint = dimensionPoint; // 尺寸线通过的点
作用:告诉AutoCAD尺寸线应该通过哪个点。
对于对齐标注:这个点决定尺寸线的位置和方向
对于线性标注:这个点决定尺寸线的位置
2.TextPosition(文字位置)
这是标注文字的具体位置,可以覆盖自动计算的位置。
// 示例:手动设置文字位置 dim.TextPosition = textPoint; // 文字的具体位置
作用:覆盖默认的文字位置,将文字放在指定的精确位置。
public static void SimpleAddPolylineLengthDimensions() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; try { // 提示用户选择多段线 PromptSelectionOptions pso = new PromptSelectionOptions(); pso.MessageForAdding = "\n请选择多段线: "; PromptSelectionResult psr = ed.GetSelection(pso); if (psr.Status != PromptStatus.OK) { ed.WriteMessage("\n未选择对象。"); return; } SelectionSet selectionSet = psr.Value; if (selectionSet.Count == 0) { ed.WriteMessage("\n未选择任何多段线。"); return; } ed.WriteMessage($"\n选择了 {selectionSet.Count} 个对象。"); // 询问用户标注偏移距离 PromptDoubleOptions pdo = new PromptDoubleOptions("\n请输入标注偏移距离: "); pdo.DefaultValue = 10.0; // 默认偏移10个单位 pdo.AllowNone = false; PromptDoubleResult pdr = ed.GetDouble(pdo); if (pdr.Status != PromptStatus.OK) { ed.WriteMessage("\n已取消。"); return; } double offsetDistance = pdr.Value; // 开始事务 using (Transaction trans = db.TransactionManager.StartTransaction()) { // 打开模型空间 BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; BlockTableRecord btr = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; int dimCount = 0; // 处理每个选中的对象 foreach (SelectedObject selectedObj in selectionSet) { try { // 尝试获取多段线 Polyline pline = trans.GetObject(selectedObj.ObjectId, OpenMode.ForRead) as Polyline; if (pline != null) { // 计算多段线长度 double length = pline.Length; // 获取起点和终点 Point3d startPoint = pline.GetPoint3dAt(0); Point3d endPoint = pline.GetPoint3dAt(pline.NumberOfVertices - 1); // 计算多段线的方向向量 Vector3d lineVector = endPoint - startPoint; double lineLength = lineVector.Length; // 计算中点 Point3d midPoint = new Point3d( (startPoint.X + endPoint.X) / 2, (startPoint.Y + endPoint.Y) / 2, (startPoint.Z + endPoint.Z) / 2 ); // 计算垂直偏移方向 - 确保标注在多段线的正确一侧 Vector3d perpendicularVector; if (lineVector.Length > 0) { // 计算多段线角度 double angle = Math.Atan2(lineVector.Y, lineVector.X); // 根据多段线方向计算偏移方向 // 对于水平线,我们希望标注在上方(Y+方向) // 对于垂直线,我们希望标注在右侧(X+方向) // 对于斜线,我们希望标注在斜线的"外侧" // 方法1:简单的垂直偏移 perpendicularVector = new Vector3d(-lineVector.Y, lineVector.X, 0); perpendicularVector = perpendicularVector.GetNormal() * offsetDistance; // 方法2:智能选择偏移方向(避免标注压线) // 检查当前偏移方向是否合适,如果不合适则反向 // 对于接近水平的多段线,确保标注在上方 if (Math.Abs(lineVector.Y) < Math.Abs(lineVector.X)) { // 接近水平 if (perpendicularVector.Y < 0) { // 如果当前偏移方向是下方,则反转 perpendicularVector = -perpendicularVector; } } } else { // 如果起点和终点相同,使用默认偏移 perpendicularVector = new Vector3d(0, offsetDistance, 0); } // 标注点位置 Point3d dimPoint = midPoint + perpendicularVector; // 创建对齐标注 AlignedDimension dim = new AlignedDimension(); dim.SetDatabaseDefaults(); // 设置默认值 dim.XLine1Point = startPoint; dim.XLine2Point = endPoint; dim.DimLinePoint = dimPoint; dim.DimensionStyle = db.Dimstyle; dim.DimensionText = length.ToString("F2"); // 显示实际长度 // 设置标注文字位置(确保不压线) // 默认的TextPosition可能在标注线上,我们可以稍微调整 double textOffsetRatio = 0.3; // 文字在标注线上偏移30% Vector3d textOffsetVector = lineVector * textOffsetRatio; Point3d textPosition = startPoint + textOffsetVector + perpendicularVector; dim.TextPosition = textPosition; // 设置标注文字高度 // 注意:这里通过系统变量设置,因为AlignedDimension没有TextHeight属性 Application.SetSystemVariable("DIMTXT", 2.5); // 文字高度2.5 // 设置标注文字在尺寸线上方 Application.SetSystemVariable("DIMTAD", 1); // 1=上方,0=中间 // 设置标注文字与尺寸线对齐 Application.SetSystemVariable("DIMTIH", 0); // 0=对齐,1=水平 // 添加到模型空间 btr.AppendEntity(dim); trans.AddNewlyCreatedDBObject(dim, true); dimCount++; } } catch (Exception ex) { ed.WriteMessage($"\n处理对象时出错: {ex.Message}"); } } // 提交事务 trans.Commit(); ed.WriteMessage($"\n成功添加了 {dimCount} 个长度标注。"); // 重生成显示 doc.Editor.Regen(); } } catch (Exception ex) { ed.WriteMessage($"\n错误: {ex.Message}"); } }public static void CreateRotatedDimension() { // 获得当前数据库 Get the current database Document acDoc = Application.DocumentManager.MdiActiveDocument; Database acCurDb = acDoc.Database; // 启动一个事务 Start a transaction using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // 以只读方式打开块表 Open the Block table for read BlockTable acBlkTbl; acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable; // 以写方式打开模型空间块表记录 Open the Block table record Model space for write BlockTableRecord acBlkTblRec; acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; // 创建旋转标注 Create the rotated dimension RotatedDimension acRotDim = new RotatedDimension(); acRotDim.SetDatabaseDefaults(); acRotDim.XLine1Point = new Point3d(0, 0, 0); acRotDim.XLine2Point = new Point3d(6, 3, 0); acRotDim.Rotation = 0.707; acRotDim.DimLinePoint = new Point3d(0, 5, 0); acRotDim.DimensionStyle = acCurDb.Dimstyle; // 添加新对象到模型空间和事务中 Add the new object to Model space and the transaction acBlkTblRec.AppendEntity(acRotDim); acTrans.AddNewlyCreatedDBObject(acRotDim, true); // 提交修改并销毁事务 Commit the changes and dispose of the transaction acTrans.Commit(); } }