news 2026/8/29 9:33:16

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

作者头像

张小明

前端开发工程师

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/8/29 6:44:57

OpCore Simplify完整指南:3步打造稳定黑苹果系统

OpCore Simplify完整指南&#xff1a;3步打造稳定黑苹果系统 【免费下载链接】OpCore-Simplify A tool designed to simplify the creation of OpenCore EFI 项目地址: https://gitcode.com/GitHub_Trending/op/OpCore-Simplify 想要在普通PC上体验macOS的流畅操作却担心…

作者头像 李华
网站建设 2026/8/24 11:03:35

轻量模型部署风向:Qwen2.5-0.5B成为开发者首选

轻量模型部署风向&#xff1a;Qwen2.5-0.5B成为开发者首选 1. 小参数也能大作为&#xff1a;为什么0.5B模型突然火了&#xff1f; 你有没有遇到过这种情况&#xff1a;想在本地跑个AI对话机器人&#xff0c;结果发现动辄7B、13B的大模型根本带不动&#xff1f;显卡吃满、内存…

作者头像 李华
网站建设 2026/8/22 22:11:43

BiliTools智能视频分析工具:让B站内容处理变得如此简单

BiliTools智能视频分析工具&#xff1a;让B站内容处理变得如此简单 【免费下载链接】BiliTools A cross-platform bilibili toolbox. 跨平台哔哩哔哩工具箱&#xff0c;支持视频、音乐、番剧、课程下载……持续更新 项目地址: https://gitcode.com/GitHub_Trending/bilit/Bil…

作者头像 李华
网站建设 2026/8/25 17:11:07

AutoGLM-Phone启动无响应?常见问题排查与修复指南

AutoGLM-Phone启动无响应&#xff1f;常见问题排查与修复指南 1. 什么是AutoGLM-Phone&#xff1a;手机端AI智能助理的底层逻辑 AutoGLM-Phone不是一款普通App&#xff0c;而是一个轻量但能力扎实的手机端AI Agent框架。它由智谱开源&#xff0c;属于Open-AutoGLM项目的重要组…

作者头像 李华
网站建设 2026/8/27 23:21:30

Django工作流自动化实战:Viewflow框架深度解析与高效应用

Django工作流自动化实战&#xff1a;Viewflow框架深度解析与高效应用 【免费下载链接】viewflow Reusable workflow library for Django 项目地址: https://gitcode.com/gh_mirrors/vi/viewflow 在当今快速发展的企业环境中&#xff0c;业务流程自动化已成为提升运营效率…

作者头像 李华
网站建设 2026/8/21 15:59:09

零门槛玩转本地语音合成:ChatTTS-ui让你轻松拥有专属语音助手

零门槛玩转本地语音合成&#xff1a;ChatTTS-ui让你轻松拥有专属语音助手 【免费下载链接】ChatTTS-ui 匹配ChatTTS的web界面和api接口 项目地址: https://gitcode.com/GitHub_Trending/ch/ChatTTS-ui 还在为配音烦恼吗&#xff1f;想不想拥有一个完全免费、无需联网的语…

作者头像 李华