明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 297|回复: 0

WEB端三维CAD中创建一个装配体

[复制链接]
发表于 6 天前 | 显示全部楼层 |阅读模式
前言
在网页CAD中有些相同的零件可以只建一个模型实例,其余用到的地方均为实例的引用,然后将引用组合起来形成装配体。mxcad3d提供了丰富的三维建模功能和便捷的API,接下来聊一下如何利用mxcad3d来创建小车装配体模型。

快速入门
首先我们需要学习mxcad的基本使用方法,可以通过官方的入门教程来搭建一个最基本的项目模板
开发环境准备(对前端开发不熟悉的一定要看)安装Node.jsVS Code然后创建最基本的mxcad开发项目API文档使用说明

本次教程最后完成的完整测试项目压缩包压缩包下载解压后需要在项目目录下打开cmd命令行,然后在命令行中执行npm install来安装依赖,然后再按照本教程中的方式来运行项目查看效果

编写创建装配体小车的代码
1.按照上面前言中第2条中的方式,根据官方快速入门教程来创建一个名为Test3dAssemblyCar的项目,如下图:
2.编写绘制装配体小车的代码
index.html中插入一个按钮"绘制装配体小车", index.html的完整代码如下:
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="UTF-8">  
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">  
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">  
  7.     <title>vite use mxcad</title>  
  8. </head>  
  9. <body>  
  10.     <div style="height: 800px; overflow: hidden;"> <canvas id="myCanvas"></canvas></div>  
  11.     <button>绘制装配体小车</button>  
  12.     <script type="module" src="./src/index.ts"></script>  
  13. </body>  
  14. </html>
复制代码
src/index.ts中编写绘制装配体小车的函数,src/index.ts的完整代码如下
  1. import { MdGe, Mx3dGeAxis, Mx3dGeColor, Mx3dGeCSYSR, Mx3dGeDir, Mx3dGeLocation, Mx3dGePoint, Mx3dGeTrsf, Mx3dGeVec, Mx3dMkCylinder, Mx3dMkFace, Mx3dMkPolygon, Mx3dMkPrism, MxCAD3DObject } from "mxcad"\
  2. // 创建mxcad3d对象\
  3. const mxcad3d = new MxCAD3DObject()\
  4. // 初始化mxcad3d对象\
  5. mxcad3d.create({\
  6.     // canvas元素的css选择器字符串(示例中是id选择器),或canvas元素对象\
  7.     canvas: "#myCanvas",\
  8.     // 获取加载wasm相关文件(wasm/js/worker.js)路径位置\
  9.     locateFile: (fileName)=> new URL(`/node_modules/mxcad/dist/wasm/3d/${fileName}`, import.meta.url).href,\
  10. })\
  11. // 初始化完成\
  12. mxcad3d.on("init", ()=>{\
  13.     console.log("初始化完成");\
  14. });\
  15. function drawAssemblyCar() {\
  16.   // 轮子形状\
  17.   const pt = new Mx3dGePoint(0, 0, 0);  // 中心点\
  18.   const dir = new Mx3dGeDir(0, 0, 1); // 方向\
  19.   const csysr = new Mx3dGeCSYSR(pt, dir); // 根据点和方向创建一个右手坐标系\
  20.   const wheel = new Mx3dMkCylinder(csysr, 20, 10);  // 轮子(宽扁的圆柱体)\
  21.   let wheelShape = wheel.Shape(); // 获取轮子拓扑形状\
  22.   // 轴形状\
  23.   const axle = new Mx3dMkCylinder(csysr, 5, 100); // 轴(细长的圆柱体)\
  24.   const axleShape = axle.Shape(); // 获取轴拓扑形状\
  25.   wheelShape = wheelShape.cut(axleShape); // 切割掉轮子的轴孔\
  26.   // 车体形状\
  27.   const bodyPts:Mx3dGePoint\[] = \[]\
  28.   bodyPts.push(new Mx3dGePoint(0, 0, 0));\
  29.   bodyPts.push(new Mx3dGePoint(0, 50, 0));\
  30.   bodyPts.push(new Mx3dGePoint(160, 50, 0));\
  31.   bodyPts.push(new Mx3dGePoint(160, 0, 0));\
  32.   const bodyPoly = new Mx3dMkPolygon(); // 创建多段线(车体的截面轮廓)\
  33.   bodyPts.forEach((pt) => {\
  34.     bodyPoly.Add(pt);\
  35.   });\
  36.   bodyPoly.Close(); // 闭合多段线\
  37.   const bodyWire = bodyPoly.Wire(); // 获取多段线(车体的截面轮廓)的拓扑Wire\
  38.   const bodyMkFace = new Mx3dMkFace(bodyWire); // 用Wire生成面\
  39.   const bodyFace = bodyMkFace.Face(); // 获取面\
  40.   const bodyPrism = new Mx3dMkPrism(bodyFace, new Mx3dGeVec(0, 0, 100)); // 用面生成车体实体\
  41.   let bodyShape = bodyPrism.Shape(); // 获取车体实体形状\
  42.   bodyShape.TranslateBy2Points(new Mx3dGePoint(30, 0, 0), new Mx3dGePoint(0, 0, 0)); // 移动到合适位置,方便装配\
  43.   const wheelForCut = new Mx3dMkCylinder(csysr, 25, 15); // 车体嵌入车轮的地方,用车体切掉,用于放入轮子\
  44.   // 以下是切出四个放轮子的空间\
  45.   const wheelForCutShape = wheelForCut.Shape();\
  46.   bodyShape = bodyShape.cut(wheelForCutShape);\
  47.   bodyShape = bodyShape.cut(wheelForCutShape.TranslatedByVec(new Mx3dGeVec(0, 0, 85)));\
  48.   bodyShape = bodyShape.cut(wheelForCutShape.TranslatedByVec(new Mx3dGeVec(100, 0, 0)));\
  49.   bodyShape = bodyShape.cut(wheelForCutShape.TranslatedByVec(new Mx3dGeVec(100, 0, 85)));\
  50.   const axleForCut = new Mx3dMkCylinder(csysr, 6, 100); // 车体嵌入轴的地方,用车体切掉,用于放入轴\
  51.   // 以下是切出两个放轴的空间\
  52.   const axleForCutShape = axleForCut.Shape();\
  53.   bodyShape = bodyShape.cut(axleForCutShape);\
  54.   bodyShape = bodyShape.cut(axleForCutShape.TranslatedByVec(new Mx3dGeVec(100, 0, 0)));\
  55.   // 获取文档\
  56.   const doc = mxcad3d.getDocument();\
  57.   // 车子装配体标签\
  58.   const carLabel = doc.addShapeLabel();\
  59.   // 轮子实例标签\
  60.   const wheelLabel = doc.addShapeLabel();\
  61.   // 车轴实例标签\
  62.   const axleLabel = doc.addShapeLabel();\
  63.   // 轮轴装配体实例标签\
  64.   const wheelAxleLabel = doc.addShapeLabel();\
  65.   // 车壳实例标签\
  66.   const bodyLabel = doc.addShapeLabel();\
  67.   // 轮子、轴、车体形状都添加到模型文档的标签中(同时为不同的形状设置不同的颜色)\
  68.   wheelLabel.setShape(wheelShape);\
  69.   wheelLabel.setColor(new Mx3dGeColor(MdGe.MxNameOfColor.Color\_NOC\_BLACK));\
  70.   axleLabel.setShape(axleShape);\
  71.   axleLabel.setColor(new Mx3dGeColor(MdGe.MxNameOfColor.Color\_NOC\_RED));\
  72.   bodyLabel.setShape(bodyShape);\
  73.   bodyLabel.setColor(new Mx3dGeColor(MdGe.MxNameOfColor.Color\_NOC\_BLUE2));\
  74.   // 轮轴装配体(轮轴装配体需要两个轮子、一个轴)\
  75.   wheelAxleLabel.addComponent(wheelLabel, new Mx3dGeLocation()); // 添加第一个轮子,没有位置(原位置,轮子模型创建的位置)\
  76.   const wheel\_2\_trsf = new Mx3dGeTrsf();\
  77.   wheel\_2\_trsf.SetTranslationPart(new Mx3dGeVec(0, 0, 90));\
  78.   wheelAxleLabel.addComponent(wheelLabel, new Mx3dGeLocation(wheel\_2\_trsf)); // 添加第二个轮子,有位置(向Z轴正方向移动90之后的位置)\
  79.   wheelAxleLabel.addComponent(axleLabel, new Mx3dGeLocation()); // 添加轴,没有位置(原位置,轴模型创建的位置)\
  80.   // 车子装配体(车子装配体需要两个轮轴装配体、一个车体)\
  81.   const wheelAxle\_1\_trsf = new Mx3dGeTrsf();\
  82.   wheelAxle\_1\_trsf.SetRotation(new Mx3dGeAxis(new Mx3dGePoint(0, 0, 0), new Mx3dGeDir(1, 0, 0)), Math.PI / 2);\
  83.   carLabel.addComponent(wheelAxleLabel, new Mx3dGeLocation(wheelAxle\_1\_trsf)); // 添加第一个轮轴装配体,有位置(绕X轴旋转90度之后的位置)\
  84.   let wheelAxle\_2\_trsf = new Mx3dGeTrsf();\
  85.   wheelAxle\_2\_trsf.SetTranslationPart(new Mx3dGeVec(100, 0, 0));\
  86.   wheelAxle\_2\_trsf = wheelAxle\_2\_trsf.Multiplied(wheelAxle\_1\_trsf); // 矩阵相乘,得到第二个轮轴装配体的位置,相乘后的矩阵代表先绕X轴旋转90度,然后再向X轴正方形平移100\
  87.   carLabel.addComponent(wheelAxleLabel, new Mx3dGeLocation(wheelAxle\_2\_trsf)); // 添加第二个轮轴装配体,有位置(绕X轴旋转90度,然后再向X轴正方形平移100之后的位置)\
  88.   carLabel.addComponent(bodyLabel, new Mx3dGeLocation(wheelAxle\_1\_trsf)); // 添加车体,有位置(绕X轴旋转90度之后的位置)\
  89.   // 更新新图显示模型,会将文档中的模型显示到当前视图中\
  90.   mxcad3d.update();\
  91. }\
  92. // 给button添加点击事件,点击后调用drawAssemblyCar函数\
  93. // 立即执行函数\
  94. (function addEventToButton(){\
  95.   const btn = document.querySelector("button");\
  96.   if (btn) {\
  97.     btn.addEventListener("click", () => {\
  98.       drawAssemblyCar();\
  99.     });\
  100.   }\
  101. })()
复制代码
按照官方快速入门教程,新建终端,运行npx vite命令来运行项目,观察效果如下图:

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2024-10-22 20:34 , Processed in 0.160198 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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