(在线编辑DWG)网页CAD二次开发实现多重引线功能

本文涉及的产品
云原生网关 MSE Higress,422元/月
容器镜像服务 ACR,镜像仓库100个 不限时长
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
简介: 本章介绍如何使用 mxcad 插件在 CAD 图纸中实现箭头引注功能。用户可通过点击画布确定箭头起点和引线顶点,自定义箭头形状、上标和下标文字内容及位置,提高图纸的完整性和可读性。功能实现包括自定义箭头引注类、注册自定义类信息和调用自定义类。示例代码展示了详细的实现步骤,用户可根据需求进行二次开发。在线示例 demo 可供参考。

箭头引注

本章介绍如何利用 mxcad 插件实现在CAD图纸中箭头引注的功能,该功能中用户点击画布确定箭头起点,再次连续点击画布确定箭头引线顶点及终点位置。
用户可自定义选择箭头形状,上标文字和下标文字内容,还可以根据绘制需求修改文字位置等,帮助用户快速标注图纸内容,增加图纸内容的完整性和可读性。

功能实现

1.实现自定义箭头引注类
为了方便后期管理与修改标注,我们可以通过继承[McDbCustomEntity]自定义实体类来扩展实现自定义箭头引注类。然后我们可以利用[McDbMText][McDbText]构造测量信息多文本对象,将标注信息绘制在页面中。

下面示例的箭头引注类中我们提供了箭头、点、十字、半箭头等箭头样式,以及上下标文字在线端、齐线中、齐线端等对齐方式,用户可参考下面的示例代码根据自身项目需求进行二次开发,为方便管理我们将箭头样式和文字对齐方式设置为枚举对象,代码如下:

   // 箭头样式
   enum arrowType {
   
     // 箭头
     arrow,
     // 半箭头
     halfArrow,
     // 点
     point,
     // 十字
     cross,
     // 无
     none
   }
   // 文字对齐方式
   export enum alginType {
   
     // 始端
     start,
     // 中间
     middle,
     // 末端
     end
   }

箭头引注自定义实体代码如下,下面示例只作参考,用户可根据自身需求修改, 代码如下:

 class McDbTestArrowCitation extends McDbCustomEntity {
   
       // 定义McDbTestConMeasurement内部的点对象 
       // 箭头线点数组
       private points: McGePoint3d[] = [];
       // 文字点位置集合
       private positionArr: McGePoint3d[] = [];
       // 文字高度
       private height: number = 0;
       // 上标文字内容
       private _textUp: string = "";
       // 下标文字内容
       private _textDown: string = "";
       // 箭头样式
       private _arrowType: number = arrowType.arrow;
       // 对齐方式
       private _alginType: number = alginType.start;
       // 记录初始长度
       private arrowLength: number = MxFun.viewCoordLong2Cad(20);;
       // 文字旋转角度
       private angle: number = 0;
       // 构造函数
       constructor(imp?: any) {
   
           super(imp);
       }
       // 创建函数
       public create(imp: any) {
   
           return new McDbTestArrowCitation(imp)
       }
       // 获取类名
       public getTypeName(): string {
   
           return "McDbTestArrowCitation";
       }
       //设置或获取文本字高
       public set textHeight(val: number) {
   
           this.height = val;
       }
       public get textHeight(): number {
   
           return this.height;
       }
       //设置或获取上标文本
       public set textUp(val: string) {
   
           this._textUp = val;
       }
       public get textUp(): string {
   
           return this._textUp;
       }
       //设置或获取下标文本
       public set textDown(val: string) {
   
           this._textDown = val;
       }
       public get textDown(): string {
   
           return this._textDown;
       }
       //设置或获取箭头样式
       public set arrowType(val: number) {
   
           this._arrowType = val;
       }
       public get arrowType(): number {
   
           return this._arrowType;
       }
       //设置或获取对齐样式
       public set alginType(val: number) {
   
           this._alginType = val;
       }
       public get alginType(): number {
   
           return this._alginType;
       }
       // 读取自定义实体数据
       public dwgInFields(filter: IMcDbDwgFiler): boolean {
   
           this.points = filter.readPoints("points").val;
           this.positionArr = filter.readPoints("positionArr").val;
           this._textDown = filter.readString("textDown").val;
           this._textUp = filter.readString("textUp").val;
           this._arrowType = filter.readLong("arrowType").val;
           this._alginType = filter.readLong("alginType").val;
           this.arrowLength = filter.readLong("arrowLength").val;
           this.angle = filter.readDouble("angle").val;
           this.height = filter.readDouble("height").val;
           return true;
       }
       // 写入自定义实体数据
       public dwgOutFields(filter: IMcDbDwgFiler): boolean {
   
           filter.writePoints("points", this.points);
           filter.writePoints("positionArr", this.positionArr);
           filter.writeString("textDown", this._textDown);
           filter.writeString("textUp", this._textUp);
           filter.writeLong("arrowType", this._arrowType);
           filter.writeLong("alginType", this._alginType);
           filter.writeLong("arrowLength", this.arrowLength);
           filter.writeDouble("angle", this.angle);
           filter.writeDouble("height", this.height);
           return true;
       }
       // 移动自定义对象的夹点
       public moveGripPointsAt(iIndex: number, dXOffset: number, dYOffset: number, dZOffset: number) {
   
           this.assertWrite();
           const length = this.points.length
           if (iIndex <= length - 1) {
   
               this.points[iIndex].x += dXOffset;
               this.points[iIndex].y += dYOffset;
               this.points[iIndex].z += dZOffset;
           }
           if (iIndex === length - 1) {
   
               this.positionArr.forEach(position => {
   
                   position.x += dXOffset;
                   position.y += dYOffset;
                   position.z += dZOffset;
               });
               this.reCountData();
           };
           if (iIndex > length - 1) {
   
               this.positionArr.forEach((position, index) => {
   
                   if (iIndex - length === index) {
   
                       position.x += dXOffset;
                       position.y += dYOffset;
                       position.z += dZOffset;
                   }
               });
           }
       };
       // 获取自定义对象的夹点
       public getGripPoints(): McGePoint3dArray {
   
           let ret = new McGePoint3dArray()
           this.points.forEach(pt => {
   
               ret.append(pt)
           });
           this.positionArr.forEach(pt => {
   
               ret.append(pt);
           })
           return ret;
       };
       // 画箭头
       private drawArrow(): McDbEntity[] {
   
           const pt1 = this.points[0];
           const pt2 = this.points[1];
           if (this._arrowType === arrowType.arrow || this._arrowType === arrowType.halfArrow) {
   
               const vec = pt2.sub(pt1).normalize().mult(this.arrowLength);
               const pt = pt1.clone().addvec(vec);
               const _vec = vec.clone().rotateBy(Math.PI / 2).normalize().mult(this.arrowLength / 8);
               const pt3 = pt.clone().addvec(_vec);
               const pt4 = pt.clone().subvec(_vec);
               const solid = new McDbHatch();
               this._arrowType === arrowType.arrow ? solid.appendLoop(new McGePoint3dArray([pt1, pt3, pt4])) : solid.appendLoop(new McGePoint3dArray([pt1, pt3, pt]));
               return [solid]
           }else if(this._arrowType === arrowType.point){
   
               const solid = new McDbHatch();
               solid.appendCircleLoop(pt1.x,pt1.y,this.arrowLength/3);
               return [solid]
           }else if(this._arrowType === arrowType.cross){
   
               const point1 = pt1.clone().addvec(McGeVector3d.kXAxis.normalize().mult(this.arrowLength/2));
               const point2 = pt1.clone().subvec(McGeVector3d.kXAxis.normalize().mult(this.arrowLength/2));
               const point3 = pt1.clone().addvec(McGeVector3d.kYAxis.normalize().mult(this.arrowLength/2));
               const point4 = pt1.clone().subvec(McGeVector3d.kYAxis.normalize().mult(this.arrowLength/2));
               const line1 = new McDbLine(point1,point2);
               const line2 = new McDbLine(point3, point4);
               return [line1,line2]
           }
       }
       // 画文字
       private drawText(): McDbEntity[] {
   
           const textArr = [];
           const textUp = new McDbText();
           textUp.height = this.height;
           textUp.textString = this._textUp;
           textUp.position = textUp.alignmentPoint = this.positionArr[0];
           textUp.horizontalMode = McDb.TextHorzMode.kTextLeft;
           textUp.rotate(this.positionArr[0], this.angle);
           if (this._alginType === alginType.middle || this._alginType === alginType.end) {
   
               const textDown = new McDbMText()
               textDown.contents = this._textDown;
               textDown.location = this.positionArr[1];
               textDown.textHeight = this.height;
               textDown.attachment = McDb.AttachmentPoint.kTopCenter;
               textDown.rotate(this.positionArr[1], this.angle);
               if (this._alginType === alginType.middle) {
   
                   textUp.horizontalMode = McDb.TextHorzMode.kTextMid;
               }
               if (this._alginType === alginType.end) {
   
                   textDown.attachment = McDb.AttachmentPoint.kTopLeft;
               }
               textArr.push(textDown);
           }
           textArr.push(textUp);
           return textArr
       }
       // 绘制实体 
       public worldDraw(draw: MxCADWorldDraw): void {
   
           // 画多段线
           const pl = new McDbPolyline();
           this.points.forEach((pt) => {
   
               pl.addVertexAt(pt);
           });
           draw.drawEntity(pl);
           // 画箭头
           if(this._arrowType !== arrowType.none && this.points.length > 1){
   
               const arrowArr = this.drawArrow();
               arrowArr.forEach( arrow =>{
   
                   draw.drawEntity(arrow)
               })
           }
           if (this.points.length > 1) {
   
               // 画标注
               const textArr = this.drawText();
               textArr.forEach(text => {
   
                   draw.drawEntity(text)
               })
           }
       }
       private reCountData() {
   
           const length = this.points.length;
           // 获取最后一段直线的方向与旋转角度
           if (length > 1) {
   
               const pt1 = this.points[length - 2];
               const pt2 = this.points[length - 1];
               if (!this.height) {
   
                   this.height = this.arrowLength*(2/3);
               };
               const vec = pt2.sub(pt1).normalize().mult(this.height / 2);
               const _vec = vec.clone().rotateBy(Math.PI / 2).normalize().mult(this.height / 2);
               this.angle = vec.angleTo2(McGeVector3d.kXAxis, McGeVector3d.kNegateZAxis);
               if (Math.PI * (3 / 2) > this.angle && this.angle > Math.PI / 2) {
   
                   this.angle += Math.PI;
                   _vec.negate();
               }
               if (this._alginType === alginType.start) {
   
                   // 在线端,只有上标文字
                   const position = pt2.clone().addvec(vec).subvec(_vec);
                   this.positionArr[0] = position;
               } else if (this._alginType === alginType.middle) {
   
                   // 齐线中,上下标文字居中
                   const distance = pt1.distanceTo(pt2);
                   const midPt = pt1.clone().addvec(vec.normalize().mult(distance / 2))
                   this.positionArr[1] = midPt.clone().subvec(_vec);
                   this.positionArr[0] = midPt.clone().addvec(_vec);
               } else if (this._alginType === alginType.end) {
   
                   // 齐线端,上下标文字在末尾
                   this.positionArr[1] = pt2.clone().addvec(vec).subvec(_vec);
                   this.positionArr[0] = pt2.clone().addvec(vec).addvec(_vec);
               }
           }
       }
       // 添加顶点
       public addVertex(pt: McGePoint3d) {
   
           this.assertWrite();
           this.points.push(pt);
           this.reCountData();
       }
       // 获取顶点数组
       public getPoints() {
   
           return this.points;
       }
   };

2.注册自定义类信息
运行代码:

new McDbTestArrowCitation().rxInit();

3.调用McDbTestArrowCitation自定义箭头引注类
3.1设置箭头样式,上下标文字内容及对齐方式
我们可以利用[MxCADUiPrString()]根据根据用户输入得到上下标文字内容,或者通过其他方式直接赋值。选择箭头样式或对齐方式时,我们可以通过[MxCADUiPrKeyWord()]根据用户选择的关键词来设置相应操作。

   // 设置箭头样式
   const getArrowStyle = new MxCADUiPrKeyWord()
   getArrowStyle.setMessage("请选着箭头样式:")
   getArrowStyle.setKeyWords("[箭头(A)/半箭头(HA)/点(P)/十字(C)/无(N)]")
   let arrowStyle = await getArrowStyle.go();
   // 转换arrowStyle的值
   switch (arrowStyle) {
   
       case 'A':
           arrowStyle = arrowType.arrow;
       case 'HA':
           arrowStyle = arrowType.halfArrow;
       case 'P':
           arrowStyle = arrowType.point;
       case 'C':
           arrowStyle = arrowType.cross;
       case 'N':
           arrowStyle = arrowType.none;
       default:
           arrowStyle = arrowType.arrow;
   }
   // 设置对齐方式
   const getAlignType = new MxCADUiPrKeyWord()
   getAlignType.setMessage("请选择上下标文字对齐方式:")
   getAlignType.setKeyWords("[在线端(S)/齐线中(M)/齐线端(E)]")
   let alignType = await getAlignType.go();
   // 转换alignType的值
   switch (alignType) {
   
       case 'S':
           alginType = alginType.start;
       case 'M':
           alginType = alginType.middle;
       case 'E':
           alginType = alginType.end;
       default:
           alginType = alginType.start;
   }
   /**
    设置上下标文字
    在线端只能设置上标文字
    */
   const getStrUp = new MxCADUiPrString();
   getStrUp.setMessage('请设置上标文字内容:');
   let strUp = await getStrUp.go();
   if (!strUp) strUp = "上";
   let strDown = "";
   if(alignType === "M" || alignType === "R"){
   
     const getStrDown = new MxCADUiPrString();
     getStrDown.setMessage('请设置下标文字内容:');
     strDown = await getStrDown.go();
     if (!strDown) strDown = "下";
   }

3.2.获取箭头起点,及引线顶点
我们可以利用取点对象[MxCADUiPrPoint]连续取点来获取箭头起点和引线的各个顶点。结合上述步骤中获取的箭头引注的信息,构造新的箭头引注对象,并动态绘制方便用户观察。

   const arrowCiatat = new McDbTestArrowCitation();
   arrowCiatat.textUp = strUp;
   arrowCiatat.textDown = strDown;
   arrowCiatat.arrowType = arrowStyle;
   arrowCiatat.alginType = alginType;
   const getPoint = new MxCADUiPrPoint();
   getPoint.setMessage('指定箭头起点:');
   const point = await getPoint.go();
   if (!point) return;
   arrowCiatat.addVertex(point);
   while (true) {
   
     const getPt = new MxCADUiPrPoint();
     getPt.setMessage('指定下一点或终点,右键完成');
     getPt.setUserDraw((pt, pw) => {
   
      const _clone = arrowCiatat.clone() as McDbTestArrowCitation;
      _clone.addVertex(pt);
      pw.drawMcDbEntity(_clone)
     })
     const pt = await getPt.go();
     if (!pt) break;
     arrowCiatat.addVertex(pt);
   }
   const mxcad = MxCpp.getCurrentMxCAD();
   mxcad.drawEntity(arrowCiatat);

功能实践

在线示例demo:https://demo.mxdraw3d.com:3000/mxcad/,操作流程如下图:
image-20241012134636925.png
image-20241012134745355.png
image-20241012134936560.png
image-20241012135139097.png

相关文章
|
编译器 Linux C++
【C++ 跨平台开发 】掌握 C++ 跨平台关键宏的使用
【C++ 跨平台开发 】掌握 C++ 跨平台关键宏的使用
271 3
hhc.exe本地制作、手动生成chm电子书
hhc.exe本地制作、手动生成chm电子书
|
11月前
|
Java
Java代码解释静态代理和动态代理的区别
### 静态代理与动态代理简介 **静态代理**:代理类在编译时已确定,目标对象和代理对象都实现同一接口。代理类包含对目标对象的引用,并在调用方法时添加额外操作。 **动态代理**:利用Java反射机制在运行时生成代理类,更加灵活。通过`Proxy`类和`InvocationHandler`接口实现,无需提前知道接口的具体实现细节。 示例代码展示了两种代理方式的实现,静态代理需要手动创建代理对象,而动态代理通过反射机制自动创建。
|
11月前
|
SQL JSON Java
没有数据库也能用 SQL
SPL(Structured Process Language)是一款开源软件,允许用户直接对CSV、XLS等文件进行SQL查询,无需将数据导入数据库。它提供了标准的JDBC驱动,支持复杂的SQL操作,如JOIN、子查询和WITH语句,还能处理非标准格式的文件和JSON数据。SPL不仅简化了数据查询,还提供了强大的计算能力和友好的IDE,适用于多种数据源的混合计算。
|
Web App开发 数据采集 JavaScript
利用无头浏览器爬取JavaScript生成的网页
利用无头浏览器爬取JavaScript生成的网页
|
机器人 开发工具 计算机视觉
基于 docker 环境的 LET-VINS 复现
该文介绍了在Docker环境下实践LET-NET的步骤。从GitHub克隆LET-NET代码,使用Docker运行vins-mono环境。安装ncnn,配置代理,更新源,安装依赖并验证安装成功。在运行LET-NET时遇到opencv版本不兼容,指出怎么安装指定版本的 opencv。解决Docker容器中的显示问题,通过`xhost +`命令运行LET-NET。在ROS中构建VINS-Mono时,cv_bridge找不到的问题,通过安装cv_bridge,设置ROS环境和工作空间,以及解决ncnn的配置问题,最终成功运行VINS-Mono。还需要下载UMA-VI数据集并修改相关路径以运行演示。
|
机器学习/深度学习 算法 Python
群智能算法:深入解读人工水母算法:原理、实现与应用
近年来,受自然界生物行为启发的优化算法备受关注。人工水母算法(AJSA)模拟水母在海洋中寻找食物的行为,是一种新颖的优化技术。本文详细解读其原理及实现步骤,并提供代码示例,帮助读者理解这一算法。在多模态、非线性优化问题中,AJSA表现出色,具有广泛应用前景。
|
机器学习/深度学习 数据采集 算法
大模型时代下的数据标注
大模型时代下的数据标注
575 2
大模型时代下的数据标注
|
自然语言处理 PyTorch 算法框架/工具
CLUE社区最新神器!PromptCLUE:大规模多任务Prompt预训练中文开源模型
CLUE社区最新神器!PromptCLUE:大规模多任务Prompt预训练中文开源模型
734 0
|
XML Java 数据库连接
【异常解决】解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题
【异常解决】解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题
382 0