明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 348|回复: 0

在线CAD(H5网页CAD SDK)实现阵列功能

[复制链接]
发表于 2023-9-12 14:25 | 显示全部楼层 |阅读模式
前言
在线CAD SDK的集成过程中,甲方客户可能有阵列功能的需求,作为开发者如何利用WEB CAD SDK展现此功能效果呢?本章节我们重点讲述一下。
首先看一下在线CAD的DEMO:https://demo.mxdraw3d.com:3000/mxcad/,阵列功能如下图:
环境搭建
首先按照mxcad入门文档https://mxcadx.gitee.io/mxcad_docs/zh/1.%E6%8C%87%E5%8D%97/1.%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8.html)创建环境,如下图:
基于mxcad库实现阵列功能
阵列功能是按一定规律对某个或多个图形组合进行复制偏移,生成出新的图形组合,在实现它之前我们要确保已经用mxcad库在页面中显示了一个cad图纸然后我们用鼠标点击一个或者多个图形, 就可以选中它们选中后执行以下代码:
  1. const rowNum = Number(prompt("输入行数"))

  2. const colNum = Number(prompt("输入列数"))

  3. if (isNaN(rowNum) || isNaN(colNum)) return



  4. alert("点击画布两点之间的距离作为偏移量")

  5. let offset = await MxCADUtility.getCorner("输入偏移距离");

  6. if (!offset) return

  7. let dColOffset = offset.pt2.x - offset.pt1.x;

  8. let dRowOffset = offset.pt2.y - offset.pt1.y;



  9. // 得到选中的图形

  10. let aryId = await MxCADUtility.userSelect("选择陈列对象");

  11. // 得到这些图形的包围盒

  12. let ext = MxCADUtility.getMcDbEntitysBoundingBox(aryId);

  13. if (!ext) return;



  14. let cenPt = new McGePoint3d();



  15. if (dColOffset > 0)

  16.     cenPt.x = ext.minPt.x;

  17. else

  18.     cenPt.x = ext.maxPt.x;



  19. if (dRowOffset > 0)

  20.     cenPt.y = ext.minPt.y;

  21. else

  22.     cenPt.y = ext.maxPt.y;

  23. // 角度

  24. let dAng = Number(prompt("输入角度"));

  25. if (isNaN(dAng)) return

  26. let matRot = new McGeMatrix3d().setToRotation(dAng * Math.PI / 180.0, McGeVector3d.kZAxis, cenPt);



  27. // 循环渲染

  28. let iMaxNum = 50000;

  29. let iCount = 0;

  30. for (let i = 0; i < rowNum; i++) {

  31.     // 行 平移矩阵

  32.     let yOffsetVec = new McGeVector3d(0.0, dRowOffset * i, 0.0);

  33.     let offsetMatY = new McGeMatrix3d().setToTranslation(yOffsetVec);



  34.     for (let j = 0; j < colNum; j++) {

  35.         if (i == 0 && j == 0)

  36.             continue;

  37.         // 列 平移矩阵

  38.         let xOffsetVec = new McGeVector3d(dColOffset * j, 0.0, 0.0);

  39.         let ofssetMatX = new McGeMatrix3d().setToTranslation(xOffsetVec);



  40.         let vecOffset = new McGePoint3d(cenPt.x, cenPt.y, cenPt.z);

  41.         // 应用对应矩阵

  42.         vecOffset.transformBy(offsetMatY);

  43.         vecOffset.transformBy(ofssetMatX);

  44.         vecOffset.transformBy(matRot);



  45.         // 最终的变换矩阵

  46.         let tmpMat = new McGeMatrix3d().setToTranslation(new McGeVector3d(vecOffset.x - cenPt.x, vecOffset.y - cenPt.y, vecOffset.z - cenPt.z));

  47.         for (let m = 0; m < aryId.length; m++) {

  48.             let tmp = aryId[m].clone() as McDbEntity

  49.             if (!tmp) {

  50.                 continue;

  51.             }

  52.             // 将该变换矩阵应用在图形对象上

  53.             tmp.TransformBy(tmpMat);

  54.             MxCpp.GetCurrentMxCAD().DrawEntity(tmp);

  55.             iCount++;

  56.             if (iCount > iMaxNum) {

  57.                 alert("超出最大阵列对象个数" + iMaxNum + "限制. \n");

  58.                 return;

  59.             }

  60.         }

  61.     }

  62. }
复制代码

代码中主要让用户输入一些行列数和偏移距离以及角度值, 然后得到选中图形, 得到其包围盒,并计算出变换矩阵, 复制这些对象然后绘制在画布,效果如下:
对于需要自己二次开发cad相关功能, 可以参考:

DEMO源码:
https://gitee.com/mxcadx/mxdraw-article/tree/master/%E5%AE%9E%E7%8E%B0%E9%98%B5%E5%88%97%E5%8A%9F%E8%83%BD

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2024-5-7 17:32 , Processed in 0.420563 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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