惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Last Watchdog
The Last Watchdog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
GbyAI
GbyAI
Y
Y Combinator Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
博客园_首页
小众软件
小众软件
I
InfoQ
J
Java Code Geeks
月光博客
月光博客
S
Secure Thoughts
Microsoft Security Blog
Microsoft Security Blog
V
Visual Studio Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Stack Overflow Blog
Stack Overflow Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
N
News and Events Feed by Topic
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Cloudflare Blog
T
Threat Research - Cisco Blogs
A
About on SuperTechFans
H
Help Net Security
MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Latest news
Latest news
G
GRAHAM CLULEY
IT之家
IT之家
C
Cisco Blogs
Last Week in AI
Last Week in AI
Engineering at Meta
Engineering at Meta
L
LangChain Blog
The Register - Security
The Register - Security
SecWiki News
SecWiki News
M
MIT News - Artificial intelligence
NISL@THU
NISL@THU
T
Tenable Blog
博客园 - Franky
美团技术团队
I
Intezer
U
Unit 42
雷峰网
雷峰网
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
SegmentFault 最新的问题
C
Cyber Attacks, Cyber Crime and Cyber Security

博客园 - 梦想CAD控件

网页CAD图纸保存与数据持久化(BS架构CAD插件) 网页CAD中命令行驱动与参数化绘图的使用教程 在线CAD视图控制与图层管理二开实战 Web CAD图形引擎实现DWG图纸网页批注和查看 开源网页实时协同设计平台-在线编辑CAD图纸 在线CAD SDK开发包图纸转换功能使用教程(在线CAD API) 统信麒麟鸿蒙系统软件集成CAD功能(国产系统CAD控件) 网页CAD开发包结构与功能说明(网页CAD实时协同编辑) 网页CAD结合AI实现自然语言设计CAD图纸 (JS实现预览)在线CAD中的“组”功能开发 (在线CAD)html在线预览cad图纸SDK集成方法 (JS编辑DWG文件)在线CAD二开实现图纸表格提取到excel 网页版CAD与gis结合实现在线地图和CAD编辑 (html在线预览cad图纸插件)在线CAD实现形位公差标注 (WEB CAD API )网页CAD插件如何开发焊接符号 (HTML VUE在线编辑DWG)网页CAD利用AI实现多行文本功能的开发 (web cad sdk)在线CAD中配置属性的使用教程 (WEB CAD二次开发)在线CAD SDK调用内部弹框的方法 在线CAD插件通过上下文对象MxPluginContext修改UI界面的方法 国产操作系统中好用的CAD软件(统信,麒麟,信创环境安装CAD) (pdm集成CAD SDK)在线CAD绘制条形码、二维码的教程 在线CAD绘制门和窗(WEB CAD二次开发家装设计软件) 在线CAD绘制墙体(WEB端开发室内设计软件) (在线三维参数化建模)网页三维CAD中加载和保存STEP模型 (H5在线CAD SDK)在线CAD中线型表的使用方法
(web查看三维CAD图纸)在三维网页CAD中绘制一个窗户模型
梦想CAD控件 · 2024-10-30 · via 博客园 - 梦想CAD控件

本文使用mxcad3d在网页中创建一个简单的三维窗户模型,mxcad3d提供了丰富的三维建模功能和便捷的API,使得创建各种三维模型变得简单方便,最终效果如下图

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

1. 根据官方快速入门教程来创建一个名为`Test3dWindow`的项目,如下图:

2. 编写绘制窗户模型的代码

   <!DOCTYPE html>
   <html lang="en">
   <head>
       <meta charset="UTF-8">
       <meta http-equiv="X-UA-Compatible" content="IE=edge">
       <meta name="viewport" content="width=device-width, initial-scale=1.0">
       <title>vite use mxcad</title>
   </head>
   <body>
       <div style="height: 800px; overflow: hidden;"> <canvas id="myCanvas"></canvas></div>
       <button>绘制窗户模型</button>
       <script type="module" src="./src/index.ts"></script>
   </body>
   </html>
   import { MdGe, Mx3dGeAxis, Mx3dGeColor, Mx3dGeDir, Mx3dGeMaterial, Mx3dGeomPlane, Mx3dGePoint, Mx3dGeVec, Mx3dMkBox, Mx3dMkFace, Mx3dMkPolygon, Mx3dMkPrism, MxCAD3DObject } from "mxcad"
   // 创建mxcad3d对象
   const mxcad3d = new MxCAD3DObject()
   // 初始化mxcad3d对象
   mxcad3d.create({
       // canvas元素的css选择器字符串(示例中是id选择器),或canvas元素对象
       canvas: "#myCanvas",
       // 获取加载wasm相关文件(wasm/js/worker.js)路径位置
       locateFile: (fileName)=> new URL(`/node_modules/mxcad/dist/wasm/3d/${fileName}`, import.meta.url).href,
   })
   // 初始化完成
   mxcad3d.on("init", ()=>{
       console.log("初始化完成");
       // 修改背景颜色
       const color1 = new Mx3dGeColor(61/255, 139/255, 221/255, MdGe.MxTypeOfColor.Color_TOC_sRGB);
       const color2 = new Mx3dGeColor(203/255, 223/255, 247/255, MdGe.MxTypeOfColor.Color_TOC_sRGB);
       mxcad3d.setGradientBgColor(color1, color2, MdGe.MxGradientFillMethod.GFM_VER);
       // 设置透视投影
       mxcad3d.setProjectionType(MdGe.MxCameraProjection.CProjection_Perspective);
       // 打开光照阴影
       mxcad3d.enableDirLightSrc(true);
   });
   function drawWindow(){
     // 窗户边框横截面轮廓点
     const pts: Mx3dGePoint[] = [];
     pts.push(new Mx3dGePoint(0, 0, 0));
     pts.push(new Mx3dGePoint(1, 0, 0));
     pts.push(new Mx3dGePoint(1, 0, 2));
     pts.push(new Mx3dGePoint(4, 0, 2));
     pts.push(new Mx3dGePoint(4, 0, 0));
     pts.push(new Mx3dGePoint(5, 0, 0));
     pts.push(new Mx3dGePoint(5, 0, 10));
     pts.push(new Mx3dGePoint(3, 0, 10));
     pts.push(new Mx3dGePoint(3, 0, 8));
     pts.push(new Mx3dGePoint(2, 0, 8));
     pts.push(new Mx3dGePoint(2, 0, 10));
     pts.push(new Mx3dGePoint(0, 0, 10));
     // 窗户边框横截面轮廓多段线
     const polygon = new Mx3dMkPolygon();
     pts.forEach((pt) => polygon.Add(pt));
     polygon.Close();
     // 窗户边框横截面轮廓线
     const wire = polygon.Wire();
     // 窗户边框横截面轮廓线生成窗框横截面
     const makeface = new Mx3dMkFace(wire);
     const face = makeface.Face();
     const vec = new Mx3dGeVec(0, 100, 0);
     // 横截面拉伸出窗框体形状
     const frame = new Mx3dMkPrism(face, vec);
     let frameShape = frame.Shape();
     // 构造两个平面用于分割窗户边框(边框两端的斜45度角)
     const pt = new Mx3dGePoint(0, 0, 0);
     const dir = new Mx3dGeDir(0, -1, 1);
     const plane = new Mx3dGeomPlane(pt, dir);
     // 平面1
     const planeFace = plane.Face(1e-5);
     pt.setXYZ(0, 50, 0);
     dir.SetXYZ(0, 0, 1);
     const axis = new Mx3dGeAxis(pt, dir);;
     // 平面2
     const planeFace2 = planeFace.MirroredByAxis(axis);
     // 分割窗户边框(分割成了两个斜45度的小三角形部分和中间的一个梯形部分)
     const parts = frameShape.spliter([planeFace, planeFace2]);
     // 筛选出中间那个梯形的部分(这里是通过质心的位置来判断的)
     parts.forEach((shape)=>{
       // 这里Centroid的参数添填的MdGe.MxQuantAspect.Quant_Volume这个枚举,是因为shape是实体,它的质心是体质心,所以这里填MdGe.MxQuantAspect.Quant_Volume
       // 如果shape是面,它的质心是面质心,所以要填MdGe.MxQuantAspect.Quant_Area
       // 如果shape是线,它的质心是线质心,所以要填MdGe.MxQuantAspect.Quant_Length
       const centroid = shape.Centroid(MdGe.MxQuantAspect.Quant_Volume);
       if (centroid.Y() > 45 && centroid.Y() < 55) {
         frameShape = shape;
       }
     });
     // 通过旋转得到另外三个边的边框
     const frameShape2 = frameShape.Rotated(new Mx3dGeAxis([0, 50, 50], [1, 0, 0]), Math.PI / 2);
     const frameShape3 = frameShape.Rotated(new Mx3dGeAxis([0, 50, 50], [1, 0, 0]), Math.PI);
     const frameShape4 = frameShape2.Rotated(new Mx3dGeAxis([0, 50, 50], [1, 0, 0]), Math.PI);
     // 合并四个边框,获得边框整体形状
     frameShape = frameShape.fuse(frameShape2).fuse(frameShape3).fuse(frameShape4);
     // 窗框颜色
     const frameColor = new Mx3dGeColor(0.5, 0.5, 0.5, MdGe.MxTypeOfColor.Color_TOC_RGB);
     // 窗框材质
     const frameMaterial = new Mx3dGeMaterial(MdGe.MxNameOfMaterial.Material_NOM_ShinyPlastified);
     // 玻璃
     const glass = new Mx3dMkBox([2, 8, 8], [3, 92, 92]);
     // 玻璃形状
     const glassShape = glass.Shape();
     // 玻璃颜色
     const glassColor = new Mx3dGeColor(0, 0.9, 0.549, MdGe.MxTypeOfColor.Color_TOC_RGB);
     // 玻璃材质,看起来是透明的
     const glassMaterial = new Mx3dGeMaterial(MdGe.MxNameOfMaterial.Material_NOM_Glass);
     // 获取模型文档
     const doc = mxcad3d.getDocument();
     // 新增一个形状标签用于保存边框形状
     const frameLabel = doc.addShapeLabel();
     frameLabel.setShape(frameShape);
     frameLabel.setColor(frameColor)
     frameLabel.setMaterial(frameMaterial);
     // 新增一个形状标签用于保存玻璃形状
     const glassLabel = doc.addShapeLabel();
     glassLabel.setShape(glassShape);
     glassLabel.setColor(glassColor)
     glassLabel.setMaterial(glassMaterial);
     // 更新视图显示
     mxcad3d.update();
   }
   // 给button添加点击事件,点击后调用drawWindow函数,进行窗户模型的绘制
   // 立即执行函数
   (function addEventToButton(){
     const btn = document.querySelector("button");
     if (btn) {
       btn.addEventListener("click", () => {
         drawWindow();
       });
     }
   })()

3. 新建终端,运行`npx vite`命令来运行项目,效果如下图: