(web cad drawing)Web CAD SDK Integration Method

news/2025/10/22 11:13:56/文章来源:https://www.cnblogs.com/MxCAD/p/19157511

Preface

We have created an online CAD project based on mxcad, which includes various CAD functions such as previewing, editing drawings, and operating the drawing database. After user integration, it supports secondary development. Currently, we offer two integration methods: Scheme 1: Integrate the mxcad project through iframe; Scheme 2: Integrate the mxcad-app plugin directly in the project.
Now, let's explain in detail the second mxcad-app integration method. This method is more convenient compared to iframe nested integration, and after integration, only one current system project needs to be maintained.
The initial interface of the MxCAD project is as follows:
image-20250910140348013

I、Basic Integration Steps

1.1、Based on the integration of vite with mxcad-app

Step 1: In main.js, import the style files required by the mxCad project and create the initial MxCAD project.

   // Introduce the mxcad-app style
   import "mxcad-app/style";
   // Introduction MxCADView
   import { MxCADView } from "mxcad-app";
   // Create a default mxcad project
   new MxCADView().create();

Step 2: Add the configuration resources related to the MxCAD project in vite.config.js

   import { defineConfig } from "vite";
   import { mxcadAssetsPlugin } from "mxcad-app/vite";
   export default defineConfig({
     plugins: [
         ...
         mxcadAssetsPlugin(),
         ...
     ],
   });

1.2. Integrating mxcad-app with webpack

The first step is to import the required style files for the mxcad project in main.js, and create the initial MxCAD project.

   // Introduce the mxcad-app style
   import "mxcad-app/style";
   // Introduce MxCADView
   import { MxCADView } from "mxcad-app";
   // Create a default mxcad project
   new MxCADView().create();

Step 2: Add the configuration resources related to the MxCAD project in vite.config.js

npm install style-loader css-loader
   const { MxCadAssetsWebpackPlugin } = require("mxcad-app/webpack");
   // webpack.config.js
   const webpack = require("webpack");
   module.exports = {
     // ...
     mode: "development",
     module: {
       rules: [
         {
           test: / \.css$/, // Match all .css 文件
           use: [
             "style-loader", // Step 2: Inject the CSS code into the <style> tag within the DOM.
             "css-loader", // Step 1: Parse the CSS file and handle @import and url()
           ],
           include: [path.resolve(__dirname, "src"), path.resolve(__dirname, "node_modules/mxcad-app")], // Optional: Only process the CSS files under the "src" directory.
         },
       ],
     },
     plugins: [
       new webpack.ContextReplacementPlugin(
         /mxcad-app/, // Match the module path that includes "mxcad-app"
         path.resolve(__dirname, "src") // Limit the scope of context search
       ),
       new MxCadAssetsWebpackPlugin(),
     ],
     // ...
     devServer: {
       static: "./dist",
       port: 3000,
     },
   };

II、 Higher-order Calls

2.1、Custom Interface Container

If the container element for the page is not specified, mxcad-app will automatically create a container with a width and height of 100vw and 100vh directly on the project interface, and the mxcad project will be displayed in full screen. In some situations, we need to dynamically control the visibility or display range of the mxcad project. Therefore, we have set the following related configurations to specify the specific interface container for mxcad-app.

<div id="myMxCAD" style="width: 50vw; height: 50vh"></div>
// Custom container
   import { MxCADView, mxcadApp } from "mxcad-app";
   /**
* openFile:The path of the file that needs to be opened
    * rootContainer:mxcad Project container name
    * map:Do you want to display the map mode?
    */
   new MxCADView({
   // mxcadApp.getStaticAssetPath()).toString() Obtain the static resource path of mxcad-app. The default used static resource isnodemodules/mxcad-app/dist/mxcadAppAssets
     openFile: new URL("test.mxweb", mxcadApp.getStaticAssetPath()).toString(),
     rootContainer: "#myMxCAD",
   }).create();

If you need to modify the static resource path within the MxCAD project, you can do so by calling the setStaticAssetPath() method.

import { mxcadApp } from "mxcad-app";
mxcadApp.setStaticAssetPath("https://unpkg.com/mxcad-app/dist/mxcadAppAssets");

2.2、Configure settings

The mxcad-app plugin provides the mxcadAssetsPlugin method to set the loading method of wasm for MxCAD projects, third-party dependencies, subdirectory names for resource storage, interface UI, shortcut commands, service configuration, theme styles, etc. Users can modify the internal configuration of the MxCAD project according to their own needs in different scenarios. Based on the Vite configuration:

import { mxcadAssetsPlugin } from "mxcad-app/vite";
   // vite.config.js
   export default {
     plugins: [
       mxcadAssetsPlugin({
         isWasmSt:true,
         libraryNames: ["vue"],
         outputDir:'testName',
               // Modify UI configuration
         transformMxUiConfig: (config) => {
           config.title = "My CAD"; // Modified title
           return config;
         },
         // Modify the server configuration
         transformMxServerConfig: (config) => {
           config.serverUrl = "/api/cad"; // Modify the API address
           return config;
         },
         // Modify shortcut command (command alias)
         // transformMxQuickCommand: (config) => config
         // Modify the configuration of the sketch and annotation UI mode
         // transformMxSketchesAndNotesUiConfig: (config) => config
         // Modify the Vuetify theme configuration
         // transformVuetifyThemeConfig: (config) => config
       }),
     ],
   };

Configuration based on webpack:

   import { MxCadAssetsWebpackPlugin } from "mxcad-app/webpack";   
   module.exports = {
     plugins: [
       new MxCadAssetsWebpackPlugin({
         isWasmSt:true,
         libraryNames: ["vue"],
         outputDir:'testName',
         transformMxServerConfig: (config) => {
           if (process.env.NODE_ENV === 'production') {
             config.serverUrl = 'https://api.prod.com/cad';
           }
           return config;
         }
           ...
       })
    ]
   };

Set the loading method for WASM:
In the MxCAD project, multi-threaded loading of wasm resources is the default setting. If you need to set it to single-threaded loading, you can modify the isWasmSt property in the mxcadAssetsPlugin method.

/** Whether to load WASM in single-thread mode (default is to use multi-threading and load)*/
isWasmSt:true

Third-party dependency
Users can directly incorporate the mxcad and mxdraw modules used internally by mxcad-app. If users need to use other dependencies within mxcad-app, they can simply add these external npm libraries to the libraryNames attribute in the mxcadAssetsPlugin method, and then use them directly.
The currently supported dependency mapping libraries include vue, axios, vuetify, vuetify/components, mapboxgl, and pinia. You can also access window.MXCADAPP_EXTERNALLIBRARIES to obtain all the provided dependency libraries, thus avoiding the need to use a build tool.

  libraryNames: ["vue","axios"...]
  // After adding it to the configuration file, you can use the Vue module in mxcad-app (the internal Vue module packaged by mxcad-app) normally.
  import { ref } from "vue";
  const n = ref(1);

Construct the subdirectory name for storing the static resources of the "mxcad-app" after packaging.
After installing mxcad-app in your own project and importing the MxCAD project, when building and packaging, a folder named "mxcadAppAssets" will be automatically created to store all the static resources related to MxCAD. If the user needs to modify the folder name where the static resources are placed, they can directly call the value of the outputDir attribute in the mxcadAssetsPlugin method.

outputDir:'testName'

Modify the interface UI, CAD shortcut commands, service configuration, theme styles, etc.
Call the provided transform method within the mxcadAssetsPlugin method to deeply configure the MxCAD project.

// Modify UI Configuration
  /** For more UI configuration options, click inside the config to view */
  transformMxUiConfig: (config) => {
      config.title = "My CAD"; // Modify the title
      config.mTopButtonBarData.push({
          "icon": "textIcon",
          "prompt": "test",
          "cmd": "Mx_test"
      });// Add top button bar button
      ...
      return config;
  }
  // The configuration for modifying the Sketch and Annotations UI mode is the same as above
     // transformMxSketchesAndNotesUiConfig: (config) => config
   // Modify CAD Quick Commands (Command Aliases)
     /** For more CAD quick command configuration options, click inside the config to view */
     transformMxQuickCommand: (config) => {
         // Add aliases '_test', 't' for command Mx_test
         // config is the internal MxCAD named alias array object
         config.push(['Mx_test','_test','t'])
      return config
     }
     // 修改服务器配置  
     /** 更多修改服务器配置可点击config内部查看 */
     transformMxServerConfig: (config) => {
         config.serverUrl = "/api/cad"; // 修改API地址
         config.font.push('txt.shx', 'gdt.shx');// 添加MxCAD项目初始需要加载的字体文件
         ...
      return config;
     }
   // Modify Vuetify Theme Configuration
     /** For more Vuetify theme configuration options, click inside the config to view */
     transformVuetifyThemeConfig: (config) => {
         config.defaultTheme = 'light';//dark or light
      return config
     }

2.3、Core dependency library

The mxcad-app includes the core library of [mxcad]. Users can directly use mxcad without having to install the mxcad plugin in the project again. If not using in a modular way, mxcad is mounted in window.MxCAD and you can directly use MxCAD to access the required methods and classes.

import { MxCpp } from 'mxcad'
// Obtain the current mxcad object const mxcad = MxCpp.getCurrentMxCAD();

mxcad relies on mxdraw, and users can also directly use mxdraw in the project. If not using in a modular way, mxdraw is mounted in window.Mx. You can directly access the required methods and classes by using Mx.

import { MxFun } from 'mxdraw'
// Output command line content
MxFun.acutPrintf('Test output')

To directly import the mxcad and mxdraw modules, it is necessary to build using the build tool. We have provided plugins for webpack and vite to support modular development. As long as you use the plugin, you can directly import the mxcad and mxdraw modules using import.

III. Example of Secondary Development of the MxCAD Project

Based on the above operations, we have integrated the MxCAD project into our project and completed the initialization configuration. Next, we can directly conduct secondary development based on this CAD project. As an example, let's take the implementation of parametric drawing of multiple straight lines in the project. After achieving the drawing command in our own system, register it, and then call our command for drawing straight lines in the MxCAD project and execute the corresponding parameter operations.

3.1、Add methods for drawing various types of straight lines

import { MxCpp, McCmColor } from "mxcad";
   function Mx_Test_DrawLine() {
     let mxcad = MxCpp.getCurrentMxCAD();
     // Clear current display content
     mxcad.newFile();
     // Change color back to black and white
     mxcad.drawColorIndex = 0;
     // Change linetype to solid line
     mxcad.drawLinetype = "";
     // Set line width 4
     mxcad.drawLineWidth = 0;
     // Create a layer named "LineLayer"
     mxcad.addLayer("LineLayer");
     // Set current layer to "LineLayer"
     mxcad.drawLayer = "LineLayer";
     // Directly draw a solid line
     // Parameter 1: start point x coordinate of the line, parameter 2: start point y coordinate of the line, parameter 3: end point x coordinate of the line, parameter 4: end point y coordinate of the line
     mxcad.drawLine(0, 0, 100, 0);
     // Draw a solid diagonal line
     mxcad.drawLine(200, 0, 300, 100);
     //----------------------------------------------------------------------------------------------------------
     // Draw a dashed line
     // Define dashed line data, "MyLineType" is the linetype name, "6,-8" is the unit definition of the dashed line, 6 is the solid line length, -8 is the space length.
     mxcad.addLinetype("MyLineType", "6,-10");
     // Set current linetype to "MyLineType"
     mxcad.drawLinetype = "MyLineType";
     // Draw a dashed line
     mxcad.drawLine(0, 30, 100, 30);
     // Draw a diagonal dashed line
     mxcad.drawLine(200, 30, 300, 130);
     //---------------------------------------------------------------------------------------------------------
     // Change drawing color to 16711680 (blue), 16711680 converted to hexadecimal is 0xFF 00 00, where FF is blue, 00 is green, and the second 00 is red.
     mxcad.drawColor = new McCmColor(0, 0, 255);
     // Draw a blue dashed line
     mxcad.drawLine(0, 60, 100, 60);
     // Draw a blue diagonal dashed line
     mxcad.drawLine(200, 60, 300, 160);
     //---------------------------------------------------------------------------------------------------------
     // Change color back to black and white
     mxcad.drawColorIndex = 0;
     // Change linetype to solid line
     mxcad.drawLinetype = "";
     // Set line width 4
     mxcad.drawLineWidth = 4;
     // Draw a line with width
     mxcad.drawLine(0, 90, 100, 90);
     // Draw a diagonal line with width
     mxcad.drawLine(200, 90, 300, 190);
     //---------------------------------------------------------------------------------------------------------
     // Draw a center line (dash-dot line)
     mxcad.addLinetype("MyLineType2", "10,-2,3,-2");
     // Change linetype to center line
     mxcad.drawLinetype = "MyLineType2";
     // Change drawing color to 255 (red), 255 converted to hexadecimal is 0x00 00 FF, where 00 is blue, the second 00 is green, FF is red.
     mxcad.drawColor = new McCmColor(255, 0, 0);
     // Draw a red center line with width
     mxcad.drawLine(0, 120, 100, 120);
     // Draw a red diagonal center line with width
     mxcad.drawLine(200, 120, 300, 220);
     //---------------------------------------------------------------------------------------------------------
     // Add a linetype with shape
     mxcad.addTextStyle("MyLineTypeTextStyle", "txt.shx", "hztxt.shx", 1);
     mxcad.addLinetypeEx("MyLineType3", "(12.7,(\"T=MxDraw\",\"S=2.54\",\"L=-5.08\",\"R=0.0\",\"X=-2.54\",\"Y=-1.27\"),-10.08)", "MyLineTypeTextStyle");
     mxcad.drawLinetype = "MyLineType3";
     mxcad.drawLineWidth = 0;
     // Draw a red center line with width
     mxcad.drawLine(350, 120, 600, 120);
     //---------------------------------------------------------------------------------------------------------
     // Add a linetype with shape
     // Change color back to black and white
     mxcad.drawColorIndex = 0;
     mxcad.drawLineWidth = 4;
     // Draw a red center line with width
     mxcad.drawLine(350, 220, 600, 220);                                       
     // Zoom all entities to current display viewport
     mxcad.zoomAll();
     // Update viewport display
     mxcad.updateDisplay();
   }

3.2、Register drawing command

import { MxFun } from 'mxdraw';
MxFun.addCommand("Mx_Test_DrawLine", Mx_Test_DrawLine);

3.3、Binding call logic (taking the example of clicking a button)

<button onclick="MyTestFun('Mx_Test_DrawLine')"> Draw a straight line </button>
const MyTestFun = (str:string)=> MxFun.sendStringToExecute(str);

3.4、Function Effect Demonstration:

image-20250911103421880
For more examples of mxcad-app related projects, you can download our mxdraw cloud chart development package to learn more details.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/943200.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

记一次 .NET 某药品缺陷高速检测系统 卡慢分析

一:背景 1. 讲故事 上个月有位朋友找到我,说他们公司的程序当内存达到一定阈值(2g+)之后,业务逻辑明显变慢导致下位机超时报警,想让我看下到底怎么回事,这种问题其实抓dump比较难搞,但朋友也说了有一个增长阈值,…

0254-CLAP-参数默认值

环境Time 2022-12-02 WSL-Ubuntu 22.04 CLAP 4.0.29前言 说明 参考:https://docs.rs/clap/latest/clap/index.html 目标 如果没有提供参数,使用默认值。 Cargo.toml [package] edition = "2021" name = &q…

得物火山引擎:Data Agent驱动财务管理智能升级

作为新一代品质生活购物社区,得物 App 以正品电商和品质生活社区作为两大核心服务,成立十年来,得物 App 始终致力于帮助用户得到美好生活,90后用户占比超9成,已成为年轻用户重要的品质生活购物平台。随着 AI 时代…

WPF/C#:使用Stylet中的IWindowManager用于显示等待窗体、对话框与消息框

前言 显示等待框意义 在创建WPF应用的时候,如果我们要执行一个耗时的操作,那么给用户显示一个等待窗体是很常见的需求,通过显示一个等待窗体让用户明白运行的这个软件并没有崩溃,能有效消除用户的焦虑与不确定性,…

2025年钢花钢管厂家最新行业资讯推荐,注浆钢管/超前小导钢管/袖阀钢管/地质钢管/管棚钢管/岩心钢管/基建与矿业升级驱动需求,高品质钢管如何选?最新实力厂商推荐榜发布

随着国家在基础设施建设、能源勘探及矿山开发等领域的持续投入,2025年市场对高品质、高规格钢管的需求预计将进一步提升。尤其在隧道支护、地质加固、精密设备制造等场景中,钢管的材质、精度与耐久性直接关系到工程安…

训练常用

1 利用 Screen 保持 VSCode 连接远程任务持续运行 1.1 远程连接 在 Linux 上使用 screen 是一种保持进程持续运行的便捷方式,即使用户断开 SSH 连接,进程也不会中断。连接远程服务器 首先使用 VSCode 或者 PyCharm 连…

《Vuejs设计与实现》第 18 章(同构渲染)(上) - 详解

《Vuejs设计与实现》第 18 章(同构渲染)(上) - 详解pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas&…

配置git

1 从项目中去掉现有的 Git 信息,并重新建立新的 Git 仓库 删除原有的 Git 信息:打开命令行工具,进入项目所在的目录。 执行以下命令以删除 .git 文件夹,这将移除所有的 Git 版本控制信息:对于 Linux 和 macOS: rm…

0253-CLAP-统计参数出现次数

环境Time 2022-12-02 WSL-Ubuntu 22.04 CLAP 4.0.29前言 说明 参考:https://docs.rs/clap/latest/clap/index.html 目标 统计参数出现的次数。 Cargo.toml [package] edition = "2021" name = "game&q…

什么情况下有必要使用抽象基类ABC?

在面向对象编程中,抽象基类(Abstract Base Class,简称ABC) 是一种不能被实例化的特殊类,其主要作用是定义一组子类必须实现的接口(方法或属性),从而强制子类遵循统一的规范。 抽象基类的核心特点:不能实例化:…

实用指南:TensorFlow2 Python深度学习 - 深度学习概述

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

HTTP/2协议漏洞引发史上最大DDoS攻击——Rapid Reset技术深度解析

本文深度解析基于HTTP/2协议CVE-2023-44487漏洞的Rapid Reset DDoS攻击技术细节。该攻击峰值达每秒3.98亿请求,仅用2万台机器就打破历史记录,文章还探讨了TCP连接终止等防御方案。史上最大DDoS攻击:Rapid Reset技术…

因果机器学习模型实战测试与比较

本文通过实际案例对比传统机器学习模型与专门设计的因果机器学习模型在效果评估上的差异,探讨了因果ML如何弥补预测性模型的局限性,并介绍了PyWhy和因果森林等工具的应用场景。因果机器学习模型实战测试与比较 因果机…

Berry.Live:开箱即用的.NET直播流媒体服务器

🚀 Berry.Live:开箱即用的.NET直播流媒体服务器想要快速搭建自己的直播平台?厌倦了复杂的流媒体服务器配置?Berry.Live 为你提供了一个简单、强大、开源的解决方案!🎯 什么是 Berry.Live? Berry.Live 是一个基…

Vscode误删文件如何恢复(二)?

如果是刚刚删除的,那么可以打开Source Control, 看到changes里面有刚刚删除的文件,拓宽视界窗口,可以看到文件后面有三个图标,选中第二个,即Discard Changes, 弹出提示框,询问你是否恢复该文件,点击Restore F…

01-C程序设计语言-第2版-第1章导言笔记

一、入门 1、编写的第一个程序:打印出“hello, world”点击查看代码 #include <stdio.h> //包含标准库信息 int main() //定义名为main函数,没有参数值 {printf("hello, world\n"); //显示字符re…

0252-CLAP-标记类型的参数

环境Time 2022-12-02 WSL-Ubuntu 22.04 CLAP 4.0.29前言 说明 参考:https://docs.rs/clap/latest/clap/index.html 目标 使用标记类型的参数。 Cargo.toml [package] edition = "2021" name = "game&q…

中国企业DevOps工具链选型标准深度解析:云原生与开源生态的博弈

中国企业DevOps工具链选型标准深度解析:云原生与开源生态的博弈 在数字化转型浪潮席卷各行各业之际,DevOps工具链的选择已成为中国企业技术战略中的关键决策。随着国内企业对于自主可控需求的日益增长,DevOps工具的…

AI智能外呼系统的工作原理解析

在很多企业看来,AI智能外呼系统已经成为销售线索跟进、客户回访、通知提醒等环节中不可或缺的工具。但在真正投入使用前,企业往往会产生疑问:AI外呼系统究竟是怎么“智能”的?它与传统自动拨号器或人工外呼有何不同…

HTTP状态码全览

HTTP状态码是用于表示HTTP请求消息的处理状态的代码。它们被分为五大类,每类都有不同的含义。以下是一些常见的HTTP状态码及其含义:1xx(信息性状态码):接收的请求正在处理100 Continue:服务器已收到请求头且客户…