明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 832|回复: 0

(H5编辑CAD)WEB CAD实现形位公差标注

[复制链接]
发表于 2025-7-22 15:09:17 | 显示全部楼层 |阅读模式
本帖最后由 MxDraw 于 2025-7-22 15:12 编辑

一、前言
形位公差是指对零件几何要素的形状误差和位置误差所允许的最大变动量,它与传统的尺寸公差不同,不仅关注长度或直径等线性尺寸的变化,还关注零件的几何特性是否符合设计意图。在本篇文章中我们将介绍如何通过mxcad插件根据形位公差的特性来实现形位公差标注功能。

二、形位公差的分类
形位公差的所有公差代号如下图所示:

根据国家标准 **GB/T 1182-2018**(等同于 ISO 1101),形位公差主要分为以下几类:
1. 形状公差(Form Tolerance)
2. 方向公差(Orientation Tolerance)
3. 位置公差(Location Tolerance)
4. 跳动公差(Runout Tolerance)

三、形位公差的基本要素

1. 被测要素:需要控制其形状或位置的几何要素。
2. 基准要素:作为参照的标准几何要素,通常用大写字母 A、B、C 表示。
3. 公差带:允许误差存在的区域范围,通常是二维或三维的空间区域。
4. 公差框格:标注形位公差信息的图形符号,包含:
   - 公差类型符号
   - 公差值
   - 基准字母(如果有)



、mxcad实现形位公差类

根据上面的介绍,我们可以根据形位公差的“特征控制框”来构建我们的形位公差自定义实体,结构如下:
[公差符号]
[公差值]
[基准A]
[基准

4.1. 类定义与继承
  1. <font size="3" face="宋体">export class MxDbGeometricTolerances extends McDbCustomEntity {
  2.     // 类实现
  3. }
  4. </font>
复制代码

4.2. 核心属性
  1. <font size="3" face="宋体">// 标注起点
  2. private startPoint: McGePoint3d = new McGePoint3d();
  3. // 标注转折点
  4. private turningPoint: McGePoint3d = new McGePoint3d();
  5. // 标注点
  6. private dimPoint: McGePoint3d = new McGePoint3d();
  7. // 标注终点
  8. private endPoint: McGePoint3d = new McGePoint3d();
  9. // 标注大小
  10. private size: number = 4;
  11. // 是否显示全周符号
  12. private isShowFull: boolean = false;
  13. // 公差内容
  14. private toleranceContents: toleranceContent[] = [];
  15. // 附注上
  16. private notesUp: string = '';
  17. // 附注下
  18. private notesDown: string = '';
  19. </font>

4.3. 公差代号枚举
  1. <font size="3" face="宋体">export enum symbolName {
  2.     Straightness = 'u',        // 直线度
  3.     Flatness = 'c',           // 平面度
  4.     Roundness = 'e',          // 圆度
  5.     Cylindricity = 'g',       // 圆柱度
  6.     LineContourdegree = 'k',  // 线轮廓度
  7.     SurfaceContourDegree = 'd',// 面轮廓度
  8.     Parallelism = 'f',        // 平行度
  9.     Perpendicularity = 'b',   // 垂直度
  10.     TiltDegree = 'a',         // 倾斜度
  11.     PositionalDegree = 'j',   // 位置度
  12.     Coaxiality = 'r',         // 同轴度
  13.     SymmetryDegree = 'i',     // 对称度
  14.     CircleJumping = 'h',      // 圆跳动
  15.     FullBeat = 't',           // 全跳动
  16.     None = ''                 // 无
  17. }</font>
复制代码


、主要功能实现


5.1. 绘制功能
5.1.1 箭头和引线绘制
  1. <font size="3" face="宋体">private drawArrow(): McDbEntity[] {
  2.     // 绘制箭头
  3.     const pt1 = this.startPoint;
  4.     const pt2 = this.turningPoint;
  5.     const arrowLength = this.size;
  6.     const vec = pt2.sub(pt1).normalize().mult(arrowLength);
  7.     const pt = pt1.clone().addvec(vec);
  8.     const _vec = vec.clone().rotateBy(Math.PI / 2).normalize().mult(arrowLength / 8);
  9.     const pt3 = pt.clone().addvec(_vec);
  10.     const pt4 = pt.clone().subvec(_vec);
  11.     const solid = new McDbHatch();
  12.     solid.appendLoop(new McGePoint3dArray([pt1, pt3, pt4]));
  13.     // 绘制引线
  14.     const points = [this.startPoint, this.turningPoint, this.dimPoint, this.endPoint];
  15.     const pl = new McDbPolyline();
  16.     points.forEach(pt => pl.addVertexAt(pt));
  17.     return [solid, pl];
  18. }</font>

5.1.2 公差代号绘制
  1. <font size="3" face="宋体">    // 绘制公差代号
  2.     private drawCode(topPt: McGePoint3d, endPt: McGePoint3d, boxHeight: number): McDbEntity[] {
  3.         const entityArr: McDbEntity[] = [];
  4.         // 首先绘制整体code边框
  5.         const pl = new McDbPolyline();
  6.         pl.isClosed = true;
  7.         const v = McGeVector3d.kXAxis.clone().mult(boxHeight);
  8.         const ptArr = [endPt, topPt, topPt.clone().addvec(v), endPt.clone().addvec(v)];
  9.         ptArr.forEach(pt => pl.addVertexAt(pt));
  10.         entityArr.push(pl);
  11.         // 合并相邻公差代号
  12.         const mergeAdjacent = (arr) => {
  13.             const result = [];
  14.             let i = 0;
  15.             while (i < arr.length) {
  16.                 let j = i + 1;
  17.                 // 找到连续相同的元素
  18.                 while (j < arr.length && arr[j] === arr[i]) {
  19.                     j++;
  20.                 }
  21.                 // 推入当前值和个数
  22.                 result.push({
  23.                     value: arr[i],
  24.                     count: j - i
  25.                 });
  26.                 // 移动指针到下一个不同的位置
  27.                 i = j;
  28.             }
  29.             return result;
  30.         }
  31.         const arr = this.toleranceContents.map(item => item.toleranceCode);
  32.         const res = mergeAdjacent(arr);
  33.         const vec_x = McGeVector3d.kXAxis.clone().mult(boxHeight);
  34.         res.forEach(item => {
  35.             // 绘制公差代号
  36.             const text = new McDbMText();
  37.             text.textHeight = this.size * (3 / 4);
  38.             text.contents = item.value;
  39.             text.attachment = McDb.AttachmentPoint.kBottomMid;
  40.             const v = endPt.sub(topPt).normalize().mult(boxHeight * item.count);
  41.             const center = topPt.clone().addvec(v.clone().mult(1 / 2)).addvec(vec_x.clone().mult(1 / 2));
  42.             text.location = center;
  43.             text.textStyle = 'cxgdt';
  44.             topPt.addvec(v);
  45.             entityArr.push(text);
  46.             text.reCompute();
  47.             const { maxPt, minPt } = MxCADUtility.getTextEntityBox(text, false);  
  48.             if(minPt.distanceTo(maxPt) > text.textHeight*3){
  49.                 maxPt.addvec(McGeVector3d.kXAxis.clone().negate().mult(text.textHeight*(7/4)));
  50.             }
  51.             const midPt = minPt.clone().addvec(maxPt.sub(minPt).mult(1 / 2));
  52.             text.move(midPt, center);
  53.             const line = new McDbLine(topPt, topPt.clone().addvec(vec_x));
  54.             entityArr.push(line)
  55.         });
  56.         return entityArr
  57.     }</font>

5.1.3 公差内容绘制
  1. <font size="3" face="宋体"> // 绘制公差内容
  2.     private drawToleranceContents(topPt: McGePoint3d, endPt: McGePoint3d, boxHeight: number): { MText: MxCADMText[], entityArr: McDbEntity[], max_x: number } {
  3.         const entityArr: McDbEntity[] = [];
  4.         const MText: MxCADMText[] = [];
  5.         const mxcad = MxCpp.getCurrentMxCAD();
  6.         /**
  7.          * 公差1标注:3个标注中记录出X最大值
  8.          */
  9.         let max_x = topPt.x;
  10.         const v_spacing = McGeVector3d.kXAxis.clone().mult(this.size / 4);
  11.         const vec_y = McGeVector3d.kYAxis.clone().mult(boxHeight).negate();
  12.         this.toleranceContents.forEach((item, ind) => {
  13.             const _topPt = topPt.clone();
  14.             const _endPt = endPt.clone();
  15.             if (item.tolerance.length) {
  16.                 _topPt.addvec(v_spacing).addvec(vec_y.clone().mult(ind));
  17.                 const _v = McGeVector3d.kYAxis.clone().mult(boxHeight / 2 - this.size * (3 / 8)).negate();
  18.                 item.tolerance.forEach((str, index) => {
  19.                     if (index === 4 || (index === 3 && (str === '{' || str === '@'))) {
  20.                         const mText = new MxCADMText()
  21.                         mText.data = [{
  22.                             type: 'paragraph',
  23.                             children: [
  24.                                 { text: str, font: 'cxgdtshp.shx' },
  25.                             ]
  26.                         }];
  27.                         mText.position = _topPt.clone().addvec(_v);
  28.                         mText.textBaseHeight = this.size * (3 / 4);
  29.                         MText.push(mText);
  30.                         const v = McGeVector3d.kXAxis.clone().mult(this.size);
  31.                         const pt = mText.position.clone().addvec(v);
  32.                         _topPt.x = _endPt.x = pt.x;
  33.                     } else {
  34.                         const text = new McDbMText();
  35.                         text.contents = str;
  36.                         text.location = _topPt.clone().addvec(_v);
  37.                         text.textHeight = this.size * (3 / 4);
  38.                         if (index == 2 || index == 4) {
  39.                             text.textStyle = 'cxgdtshp';
  40.                         } else if (index == 3 && str !== '{') {
  41.                             text.textStyle = 'cxgdt'
  42.                         } else {
  43.                             text.textStyleId = mxcad.getDatabase().getCurrentlyTextStyleId()
  44.                         }
  45.                         text.reCompute();
  46.                         const { maxPt, minPt } = MxCADUtility.getTextEntityBox(text, false);
  47.                         _topPt.x = _endPt.x = maxPt.x;
  48.                         entityArr.push(text);
  49.                     }
  50.                 });
  51.             };
  52.             if (_topPt.x > max_x) max_x = _topPt.x;
  53.         });
  54.         if (max_x > topPt.x) {
  55.             this.toleranceContents.forEach((item, index) => {
  56.                 const pl = new McDbPolyline();
  57.                 const topPt_max = new McGePoint3d(max_x, topPt.y);
  58.                 const endPt_max = new McGePoint3d(max_x, endPt.y);
  59.                 const points = [topPt, topPt_max.clone().addvec(v_spacing), endPt_max.clone().addvec(v_spacing)];
  60.                 points.forEach(pt => pl.addVertexAt(pt));
  61.                 entityArr.push(pl);
  62.                 topPt.addvec(vec_y);
  63.                 if (index === this.toleranceContents.length - 1) {
  64.                     const line = new McDbLine(endPt_max.clone().addvec(v_spacing), endPt);
  65.                     entityArr.push(line);
  66.                 }
  67.             });
  68.             max_x = new McGePoint3d(max_x, topPt.y).addvec(v_spacing).x;
  69.         };
  70.         return { entityArr, MText, max_x }
  71.     }
  72. </font>

5.1.4 绘制基准内容
  1. <font size="3" face="宋体">// 绘制基准内容
  2.     private drawBenchmark(topPt: McGePoint3d, endPt: McGePoint3d, boxHeight: number): { entityArr: McDbEntity[], MText } {
  3.         const mxcad = MxCpp.getCurrentMxCAD();
  4.         const _v = McGeVector3d.kYAxis.clone().mult(boxHeight / 2 - this.size * (3 / 8)).negate();
  5.         const vec_y = McGeVector3d.kYAxis.clone().mult(boxHeight).negate();
  6.         const v_spacing = McGeVector3d.kXAxis.clone().mult(this.size / 4);
  7.         let max_x = topPt.x;
  8.         const lineX = [];
  9.         const MText: MxCADMText[] = []
  10.         const getTextEnts = (contents: Array<string[]>, _topPt: McGePoint3d): McDbEntity[] => {
  11.             const entity = [];
  12.             let endPt = _topPt.clone();
  13.             _topPt.addvec(v_spacing)
  14.             if (contents.length === 2 && contents.filter(item => item.length != 0).length === 2) contents.splice(1, 0, ['-']);
  15.             contents.forEach((arr, index) => {
  16.                 if (arr.length) {
  17.                     arr.forEach((str, ind) => {
  18.                         if (ind === 1 && str === '{') {
  19.                             const mText = new MxCADMText()
  20.                             mText.data = [{
  21.                                 type: 'paragraph',
  22.                                 children: [
  23.                                     { text: str, font: 'cxgdtshp.shx' },
  24.                                 ]
  25.                             }];
  26.                             mText.position = _topPt.clone().addvec(_v);
  27.                             mText.textBaseHeight = this.size * (3 / 4);
  28.                             MText.push(mText);
  29.                             const v = McGeVector3d.kXAxis.clone().mult(this.size);
  30.                             const pt = mText.position.clone().addvec(v);
  31.                             _topPt.x = endPt.x = pt.x;
  32.                         } else {
  33.                             const text = new McDbMText();
  34.                             text.contents = str;
  35.                             text.location = _topPt.clone().addvec(_v);
  36.                             text.textHeight = this.size * (3 / 4);
  37.                             if (ind == 1) {
  38.                                 text.textStyle = 'CXGDT';
  39.                             } else {
  40.                                 text.textStyleId = mxcad.getDatabase().getCurrentlyTextStyleId()
  41.                             }
  42.                             text.reCompute();
  43.                             const { maxPt, minPt } = MxCADUtility.getTextEntityBox(text, false);
  44.                             if (max_x < maxPt.x) max_x = maxPt.x;
  45.                             endPt.x = maxPt.x;
  46.                             entity.push(text);
  47.                             _topPt.x = maxPt.x;
  48.                         }
  49.                     })
  50.                 }
  51.             });
  52.             if (max_x < endPt.x) max_x = endPt.x;
  53.             return entity;
  54.         }
  55.         const entityArr: McDbEntity[] = [];
  56.         const maxXArr: number[] = [];
  57.         //先统一绘制基准1、2、3
  58.         const bNames = ['b1', 'b2', 'b3'];
  59.         let textEnd_x = topPt.x;
  60.         bNames.forEach((name, index) => {
  61.             if (index != 0 && lineX.length) {
  62.                 textEnd_x = Math.max(...lineX)
  63.                 maxXArr.push(textEnd_x)
  64.             };
  65.             lineX.length = 0;
  66.             this.toleranceContents.forEach((item, ind) => {
  67.                 if (item.Benchmark[name]?.length && item.Benchmark[name].filter(i => i.length).length > 0) {
  68.                     const _topPt = new McGePoint3d(textEnd_x, topPt.clone().addvec(vec_y.clone().mult(ind)).y);
  69.                     entityArr.push(...getTextEnts(item.Benchmark[name], _topPt));
  70.                     const pt = new McGePoint3d(max_x, topPt.y);
  71.                     pt.addvec(v_spacing);
  72.                     max_x = pt.x;
  73.                     lineX.push(max_x)
  74.                 }
  75.             })
  76.         })
  77.         if (entityArr.length > 0) {
  78.             maxXArr.forEach(x => {
  79.                 const line = new McDbLine(new McGePoint3d(x, topPt.y), new McGePoint3d(x, endPt.y));
  80.                 entityArr.push(line)
  81.             })
  82.             const line = new McDbLine(new McGePoint3d(max_x, topPt.y), new McGePoint3d(max_x, endPt.y));
  83.             const _line = new McDbLine(endPt, new McGePoint3d(max_x, endPt.y));
  84.             entityArr.push(line, _line);
  85.             this.toleranceContents.forEach((item, index) => {
  86.                 if (index != 0) topPt.addvec(vec_y.clone());
  87.                 const line = new McDbLine(topPt, new McGePoint3d(max_x, topPt.y));
  88.                 entityArr.push(line)
  89.             });
  90.         }
  91.         return { entityArr, MText }
  92.     }
  93. </font>

5.1.5 动态绘制
  1. <font size="3" face="宋体"> // 绘制实体
  2.     public worldDraw(draw: MxCADWorldDraw): void {
  3.         const mxcad = MxCpp.getCurrentMxCAD();
  4.         const textStyle = ['cxgdtshp', 'cxgdt'];
  5.         textStyle.forEach(name => {
  6.             if (!mxcad.getDatabase().getTextStyleTable().has(name)) {
  7.                 MxCpp.App.loadFonts([`${name}.shx`], [], [`${name}.shx`], () => {
  8.                     mxcad.addTextStyle(name, `${name}.shx`, `${name}.shx`);
  9.                 });
  10.             }
  11.         })
  12.         const { MText, allEntityArr } = this.getAllEnitty();
  13.         MText.forEach((ent: MxCADMText) => {
  14.             ent.worldDraw(draw);
  15.         });
  16.         allEntityArr.forEach((ent: McDbEntity) => {
  17.             draw.drawEntity(ent);
  18.         })
  19.     }
  20.     // 获取所有实体
  21.     private getAllEnitty(): { allEntityArr: McDbEntity[], MText: MxCADMText[] } {
  22.         const allEntityArr = [];
  23.         // 绘制箭头
  24.         const entity = this.drawArrow();
  25.         allEntityArr.push(...entity);
  26.         //  绘制全周符号
  27.         const types = [symbolName.LineContourdegree, symbolName.SurfaceContourDegree];
  28.         if (this.toleranceContents.length === 1 && types.includes(this.toleranceContents[0].toleranceCode) && this.isShowFull) {
  29.             const circle = new McDbCircle(this.dimPoint.x, this.dimPoint.y, 0, this.size * (7 / 16));
  30.             allEntityArr.push(circle);
  31.         }
  32.         // 绘制公差标注
  33.         const boxHeight = this.size * (7 / 4);
  34.         const vec = McGeVector3d.kYAxis.clone().mult(boxHeight * (this.toleranceContents.length / 2))
  35.         const topPt = this.endPoint.clone().addvec(vec);
  36.         const endPt = this.endPoint.clone().addvec(vec.clone().negate());
  37.         // 绘制公差代号
  38.         allEntityArr.push(...this.drawCode(topPt.clone(), endPt.clone(), boxHeight));
  39.         // 绘制公差内容
  40.         const vec_x = McGeVector3d.kXAxis.clone().mult(boxHeight);
  41.         const { MText, entityArr, max_x } = this.drawToleranceContents(topPt.clone().addvec(vec_x), endPt.clone().addvec(vec_x), boxHeight);
  42.         allEntityArr.push(...entityArr);
  43.         // 绘制基准一二三
  44.         const { MText: _MText, entityArr: _entityArr } = this.drawBenchmark(new McGePoint3d(max_x, topPt.y), new McGePoint3d(max_x, endPt.y), boxHeight)
  45.         allEntityArr.push(..._entityArr);
  46.         MText.push(..._MText);
  47.         // 绘制上下附注
  48.         const arr = [this.notesUp, this.notesDown];
  49.         arr.forEach((item, index) => {
  50.             const text = new McDbMText();
  51.             text.contents = item;
  52.             text.textHeight = this.size * (3 / 4)
  53.             text.location = index == 0 ? topPt.clone().addvec(McGeVector3d.kYAxis.clone().mult(this.size)) : endPt.clone().addvec(McGeVector3d.kYAxis.clone().mult(this.size / 4).negate());
  54.             allEntityArr.push(text);
  55.         })
  56.         return { allEntityArr, MText }
  57.     }</font>

5.2. 数据存储
  1. <font size="3" face="宋体">// 读取自定义实体数据
  2.     public dwgInFields(filter: IMcDbDwgFiler): boolean {
  3.         this.startPoint = filter.readPoint("startPoint").val;
  4.         this.turningPoint = filter.readPoint("turningPoint").val;
  5.         this.dimPoint = filter.readPoint("dimPoint").val;
  6.         this.endPoint = filter.readPoint("endPoint").val;
  7.         this.size = filter.readDouble("size").val;
  8.         this.isShowFull = filter.readLong("isShowFull").val ? true : false;
  9.         this.notesUp = filter.readString('notesUp').val;
  10.         this.notesDown = filter.readString('notesDown').val;
  11.         this.minPt = filter.readPoint("minPt").val;
  12.         this.maxPt = filter.readPoint("maxPt").val;
  13.         this.toleranceContents = JSON.parse(filter.readString('toleranceContents').val);
  14.         return true;
  15.     }
  16.     // 写入自定义实体数据
  17.     public dwgOutFields(filter: IMcDbDwgFiler): boolean {
  18.         filter.writePoint("startPoint", this.startPoint);
  19.         filter.writePoint("turningPoint", this.turningPoint);
  20.         filter.writePoint("dimPoint", this.dimPoint);
  21.         filter.writePoint("endPoint", this.endPoint);
  22.         filter.writeDouble("size", this.size);
  23.         filter.writeLong("isShowFull", this.isShowFull ? 1 : 0);
  24.         filter.writeString("notesUp", this.notesUp);
  25.         filter.writeString("notesDown", this.notesDown);
  26.         filter.writePoint("minPt", this.minPt);
  27.         filter.writePoint("maxPt", this.maxPt);
  28.         filter.writeString('toleranceContents', JSON.stringify(this.toleranceContents));
  29.         return true;
  30.     }</font>
复制代码

5.3.夹点编辑功能
  1. <font size="3" face="宋体"> // 移动自定义对象的夹点
  2.     public moveGripPointsAt(iIndex: number, dXOffset: number, dYOffset: number, dZOffset: number) {
  3.         this.assertWrite();
  4.         if (iIndex === 0) {
  5.             this.startPoint.x += dXOffset;
  6.             this.startPoint.y += dYOffset;
  7.             this.startPoint.z += dZOffset;
  8.         } else if (iIndex === 1) {
  9.             this.turningPoint.x += dXOffset;
  10.             this.turningPoint.y += dYOffset;
  11.             this.turningPoint.z += dZOffset;
  12.         } else if (iIndex === 2) {
  13.             this.dimPoint.x += dXOffset;
  14.             this.dimPoint.y += dYOffset;
  15.             this.dimPoint.z += dZOffset;
  16.             this.endPoint.x += dXOffset;
  17.             this.endPoint.y += dYOffset;
  18.             this.endPoint.z += dZOffset;
  19.         } else if (iIndex === 3) {
  20.             this.endPoint.x += dXOffset;
  21.             this.endPoint.y += dYOffset;
  22.             this.endPoint.z += dZOffset;
  23.         }
  24.     };
  25.     // 获取自定义对象的夹点
  26.     public getGripPoints(): McGePoint3dArray {
  27.         let ret = new McGePoint3dArray()
  28.         ret.append(this.startPoint);
  29.         ret.append(this.turningPoint);
  30.         ret.append(this.dimPoint);
  31.         ret.append(this.endPoint);
  32.         return ret;
  33.     };</font>
复制代码


5.4.暴露内部属性方法
  1. <font size="3" face="宋体"> //设置或获取标注大小
  2.     public set dimSize(val: number) {
  3.         this.size = val;
  4.     }
  5.     public get dimSize(): number {
  6.         return this.size;
  7.     }
  8.     //设置或获取是否显示全周符号
  9.     public set isShowFullWeekSymbol(val: boolean) {
  10.         this.isShowFull = val;
  11.     }
  12.     public get isShowFullWeekSymbol(): boolean {
  13.         return this.isShowFull;
  14.     }
  15.     //设置或获取公差标注内容
  16.     public set dimToleranceContents(val: toleranceContent[]) {
  17.         this.toleranceContents = val;
  18.     }
  19.     public get dimToleranceContents(): toleranceContent[] {
  20.         return this.toleranceContents;
  21.     }
  22.     //设置或获取附注上
  23.     public set dimNotesUp(val: string) {
  24.         this.notesUp = val;
  25.     }
  26.     public get dimNotesUp(): string {
  27.         return this.notesUp;
  28.     }
  29.     //设置或获取附注下
  30.     public set dimNotesDown(val: string) {
  31.         this.notesDown = val;
  32.     }
  33.     public get dimNotesDown(): string {
  34.         return this.notesDown;
  35.     }
  36.     private getBox(entityArr: McDbEntity[]) {
  37.         const mxcad = MxCpp.getCurrentMxCAD();
  38.         let _minPt, _maxPt = null;
  39.         let result;
  40.         entityArr.forEach(entity => {
  41.             if (entity instanceof McDbMText) {
  42.                 // 将ent的文字样式设置为当前文字样式
  43.                 const textStyleId = mxcad.getDatabase().getCurrentlyTextStyleId();
  44.                 entity.textStyleId = textStyleId;
  45.                 entity.reCompute();
  46.                 result = MxCADUtility.getTextEntityBox(entity, false);
  47.             } else if (entity instanceof McDbMText) {
  48.                 entity.reCompute();
  49.                 result = MxCADUtility.getTextEntityBox(entity, false);
  50.             } else {
  51.                 result = entity.getBoundingBox();
  52.             }
  53.             const { minPt, maxPt, ret } = result;
  54.             if (!_minPt) _minPt = minPt.clone();
  55.             if (!_maxPt) _maxPt = maxPt.clone();
  56.             if (minPt.x < _minPt.x) _minPt.x = minPt.x;
  57.             if (minPt.y < _minPt.y) _minPt.y = minPt.y;
  58.             if (maxPt.x > _maxPt.x) _maxPt.x = maxPt.x;
  59.             if (maxPt.y > _maxPt.y) _maxPt.y = maxPt.y;
  60.         });
  61.         if (_minPt && _maxPt) {
  62.             this.maxPt = _maxPt;
  63.             this.minPt = _minPt;
  64.         }
  65.     }
  66.     // 设置标注起点
  67.     public setStartPoint(pt: McGePoint3d) {
  68.         this.startPoint = this.turningPoint = this.endPoint = this.dimPoint = pt.clone();
  69.     }
  70.     // 获取标注起点
  71.     public getStartPoint() {
  72.         return this.startPoint;
  73.     }
  74.     // 设置标注转折点
  75.     public setTurningPoint(pt: McGePoint3d) {
  76.         this.turningPoint = this.endPoint = this.dimPoint = pt.clone();
  77.     }
  78.     // 获取标注转折点
  79.     public getTurningPoint() {
  80.         return this.turningPoint;
  81.     }
  82.     // 设置标注终点
  83.     public setEndPoint(pt: McGePoint3d) {
  84.         this.endPoint = pt.clone();
  85.     }
  86.     // 获取标注终点
  87.     public getEndPoint() {
  88.         return this.endPoint;
  89.     }
  90.     // 获取标注转折点
  91.     public getDimPoint() {
  92.         return this.dimPoint;
  93.     }
  94.     // 设置标注终点
  95.     public setDimPoint(pt: McGePoint3d) {
  96.         this.dimPoint = this.endPoint = pt.clone();
  97.     }
  98.     // 获取包围盒
  99.     public getBoundingBox(): { minPt: McGePoint3d; maxPt: McGePoint3d; ret: boolean; } {
  100.         const { allEntityArr } = this.getAllEnitty();
  101.         this.getBox(allEntityArr);
  102.         return { minPt: this.minPt, maxPt: this.maxPt, ret: true }
  103.     }</font>


、使用方法
6.1. 注册MxDbGeometricTolerances实体类
  1. <font size="3" face="宋体">new MxDbGeometricTolerances().rxInit();
  2. </font>
复制代码

6.2. 绘制形位公差
  1. <font size="3" face="宋体">async function Mx_drawGeometricTolerance() {
  2.              // 创建形位公差实体
  3.             const dim = new MxDbGeometricTolerances();
  4.             // 设置公差内容
  5.             dim.dimToleranceContents = [{
  6.                 "toleranceCode": "b", "tolerance": ["φ","we","<","@"],
  7.                 "Benchmark": {"b1": [[],[]],"b1": [[],[]],"b1": [[],[]]}
  8.             }];
  9.             dim.isShowFullWeekSymbol = false;
  10.             dim.dimNotesDown = '下标注';
  11.             dim.dimNotesUp = '上标注';
  12.             // 设置标注点
  13.             const getStartPt = new MxCADUiPrPoint();
  14.             getStartPt.setMessage('请设置定位点或直线或圆弧或圆');
  15.             const startPt = await getStartPt.go();
  16.             if (!startPt) return;
  17.             dim.setStartPoint(startPt);           
  18.             // 设置转折点
  19.             const getTurningPt = new MxCADUiPrPoint();
  20.             getTurningPt.setMessage('请设置转折点');
  21.             getTurningPt.setUserDraw((pt, pw) => {
  22.                 dim.setTurningPoint(pt);
  23.                 pw.drawMcDbEntity(dim);
  24.             });
  25.             const turnPt = await getTurningPt.go();
  26.             if (!turnPt) return;
  27.             dim.setTurningPoint(turnPt);           
  28.             // 设置标注位置
  29.             const getDimPt = new MxCADUiPrPoint();
  30.             getDimPt.setMessage('拖动确定标注位置');
  31.             getDimPt.setUserDraw((pt, pw) => {
  32.                 dim.setDimPoint(pt);
  33.                 pw.drawMcDbEntity(dim);
  34.             });
  35.             const dimPt = await getDimPt.go();
  36.             if (!dimPt) return;
  37.             dim.setDimPoint(dimPt);           
  38.             // 设置终点
  39.             const getEndPt = new MxCADUiPrPoint();
  40.             getEndPt.setMessage('拖动确定标注位置');
  41.             getEndPt.setUserDraw((pt, pw) => {
  42.                 dim.setEndPoint(pt);
  43.                 pw.drawMcDbEntity(dim);
  44.             });
  45.             const endPt = await getEndPt.go();
  46.             if (!endPt) return;
  47.             dim.setEndPoint(endPt);   
  48.             // 绘制实体
  49.             const mxcad = MxCpp.getCurrentMxCAD();
  50.             mxcad.drawEntity(dim);
  51. }</font>

、注意事项
1. 使用前需要确保已正确加载字体文件(cxgdtshp.shx 和 cxgdt.shx)
2. 形位公差的绘制需要按照正确的顺序设置各个点(起点、转折点、标注点、终点)
3. 公差内容的设置需要符合规范,包括公差代号、公差值和基准等
4. 在绘制过程中需要注意坐标系统的正确使用


、效果演示
在上述介绍中,我们已经实现了形位公差的自定义实体,通过该实体与我们的mxcad项目结合,我们就能够实现更完善的形位公差标注功能,Demo查看地址:https://demo2.mxdraw3d.com:3000/mxcad/
基础效果演示:
根据上述内容可做扩展开发,根据焊接符号特性设置对应的弹框,其示例效果如下:



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2026-1-9 04:30 , Processed in 0.180334 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表