news 2026/6/10 18:26:40

标注——尺寸线位置和文字位置

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
标注——尺寸线位置和文字位置

DimLinePointTextPosition的区别

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(); } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/10 11:45:11

BongoCat个性化定制终极指南:从创意到专属桌面宠物的神奇旅程

BongoCat个性化定制终极指南&#xff1a;从创意到专属桌面宠物的神奇旅程 【免费下载链接】BongoCat 让呆萌可爱的 Bongo Cat 陪伴你的键盘敲击与鼠标操作&#xff0c;每一次输入都充满趣味与活力&#xff01; 项目地址: https://gitcode.com/gh_mirrors/bong/BongoCat …

作者头像 李华
网站建设 2026/6/10 11:45:43

YimMenu深度解析:GTA5辅助工具实战指南

YimMenu深度解析&#xff1a;GTA5辅助工具实战指南 【免费下载链接】YimMenu YimMenu, a GTA V menu protecting against a wide ranges of the public crashes and improving the overall experience. 项目地址: https://gitcode.com/GitHub_Trending/yi/YimMenu 你是否…

作者头像 李华
网站建设 2026/6/9 23:31:28

如何快速构建OpenCore EFI:智能配置工具完整指南

如何快速构建OpenCore EFI&#xff1a;智能配置工具完整指南 【免费下载链接】OpCore-Simplify A tool designed to simplify the creation of OpenCore EFI 项目地址: https://gitcode.com/GitHub_Trending/op/OpCore-Simplify 还在为复杂的黑苹果配置而头疼吗&#xf…

作者头像 李华
网站建设 2026/6/10 14:25:32

Shairport4w:让Windows电脑秒变AirPlay音频接收中心

Shairport4w&#xff1a;让Windows电脑秒变AirPlay音频接收中心 【免费下载链接】Shairport4w An AirPlay Audio-Receiver for your Windows-PC 项目地址: https://gitcode.com/gh_mirrors/sh/Shairport4w 还在为苹果设备与Windows电脑之间的音频传输障碍而困扰吗&#…

作者头像 李华
网站建设 2026/6/6 21:45:05

终极指南:如何快速掌握OpCore Simplify打造专属黑苹果系统

终极指南&#xff1a;如何快速掌握OpCore Simplify打造专属黑苹果系统 【免费下载链接】OpCore-Simplify A tool designed to simplify the creation of OpenCore EFI 项目地址: https://gitcode.com/GitHub_Trending/op/OpCore-Simplify 想要体验macOS的流畅操作却受限…

作者头像 李华