news 2026/5/1 9:37:17

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

作者头像

张小明

前端开发工程师

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/5/1 8:41:41

OpCore Simplify:告别繁琐配置的黑苹果智能生成器终极指南

OpCore Simplify&#xff1a;告别繁琐配置的黑苹果智能生成器终极指南 【免费下载链接】OpCore-Simplify A tool designed to simplify the creation of OpenCore EFI 项目地址: https://gitcode.com/GitHub_Trending/op/OpCore-Simplify 还在为复杂的OpenCore EFI制作而…

作者头像 李华
网站建设 2026/5/1 7:27:01

OpCore Simplify:智能黑苹果配置新体验

OpCore Simplify&#xff1a;智能黑苹果配置新体验 【免费下载链接】OpCore-Simplify A tool designed to simplify the creation of OpenCore EFI 项目地址: https://gitcode.com/GitHub_Trending/op/OpCore-Simplify 还在为复杂的OpenCore配置而头疼吗&#xff1f;OpC…

作者头像 李华
网站建设 2026/5/1 7:51:46

Qwen3-1.7B一键启动指南:Jupyter快速上手实战

Qwen3-1.7B一键启动指南&#xff1a;Jupyter快速上手实战 1. 引言&#xff1a;为什么选择Qwen3-1.7B&#xff1f; 你是否正在寻找一个轻量级但功能强大的大语言模型&#xff0c;既能快速部署又具备出色的推理能力&#xff1f;Qwen3-1.7B 正是为此而生。作为阿里巴巴通义千问系…

作者头像 李华
网站建设 2026/4/17 4:09:56

如何快速备份网站:完整离线下载工具使用指南

如何快速备份网站&#xff1a;完整离线下载工具使用指南 【免费下载链接】Website-downloader &#x1f4a1; Download the complete source code of any website (including all assets). [ Javascripts, Stylesheets, Images ] using Node.js 项目地址: https://gitcode.c…

作者头像 李华
网站建设 2026/4/30 16:42:53

5分钟学会:如何用AI神器秒懂B站视频核心内容

5分钟学会&#xff1a;如何用AI神器秒懂B站视频核心内容 【免费下载链接】BilibiliSummary A chrome extension helps you summary video on bilibili. 项目地址: https://gitcode.com/gh_mirrors/bi/BilibiliSummary 在信息爆炸的今天&#xff0c;B站已成为我们获取知识…

作者头像 李华
网站建设 2026/5/1 7:52:44

中文歇后语补全可行吗?BERT语义推理能力极限测试

中文歇后语补全可行吗&#xff1f;BERT语义推理能力极限测试 1. BERT 智能语义填空服务 你有没有试过一句话卡在嘴边&#xff0c;就差一个词却怎么也想不起来&#xff1f;或者读到一句古诗&#xff0c;中间缺了一个字&#xff0c;总感觉“差点意思”&#xff1f;现在&#xf…

作者头像 李华