明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 465|回复: 0

(web cad viewer)Web CAD SDK Integration Method

[复制链接]
发表于 2025-10-22 14:00:06 | 显示全部楼层 |阅读模式
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:

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.
  1.    // Introduce the mxcad-app style
  2.    import "mxcad-app/style";
  3.    // Introduction MxCADView
  4.    import { MxCADView } from "mxcad-app";
  5.    // Create a default mxcad project
  6.    new MxCADView().create();
复制代码
Step 2: Add the configuration resources related to the MxCAD project in `vite.config.js`
  1. import { defineConfig } from "vite";
  2.    import { mxcadAssetsPlugin } from "mxcad-app/vite";
  3.    export default defineConfig({
  4.      plugins: [
  5.          ...
  6.          mxcadAssetsPlugin(),
  7.          ...
  8.      ],
  9.    });
复制代码

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.
  1.    // Introduce the mxcad-app style
  2.    import "mxcad-app/style";
  3.    // Introduce MxCADView
  4.    import { MxCADView } from "mxcad-app";
  5.    // Create a default mxcad project
  6.    new MxCADView().create();
复制代码

Step 2: Add the configuration resources related to the MxCAD project in `vite.config.js`
  1.    npm install style-loader css-loader
复制代码
  1.    const { MxCadAssetsWebpackPlugin } = require("mxcad-app/webpack");
  2.    // webpack.config.js
  3.    const webpack = require("webpack");
  4.    module.exports = {
  5.      // ...
  6.      mode: "development",
  7.      module: {
  8.        rules: [
  9.          {
  10.            test: / \.css$/, // Match all .css 文件
  11.            use: [
  12.              "style-loader", // Step 2: Inject the CSS code into the <style> tag within the DOM.
  13.              "css-loader", // Step 1: Parse the CSS file and handle @import and url()
  14.            ],
  15.            include: [path.resolve(__dirname, "src"), path.resolve(__dirname, "node_modules/mxcad-app")], // Optional: Only process the CSS files under the "src" directory.
  16.          },
  17.        ],
  18.      },
  19.      plugins: [
  20.        new webpack.ContextReplacementPlugin(
  21.          /mxcad-app/, // Match the module path that includes "mxcad-app"
  22.          path.resolve(__dirname, "src") // Limit the scope of context search
  23.        ),
  24.        new MxCadAssetsWebpackPlugin(),
  25.      ],
  26.      // ...
  27.      devServer: {
  28.        static: "./dist",
  29.        port: 3000,
  30.      },
  31.    };
复制代码

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`.
  1. <div id="myMxCAD" style="width: 50vw; height: 50vh"></div>
复制代码
  1. // Custom container
  2.    import { MxCADView, mxcadApp } from "mxcad-app";
  3.    /**
  4. * openFile:The path of the file that needs to be opened
  5.     * rootContainer:mxcad Project container name
  6.     * map:Do you want to display the map mode?
  7.     */
  8.    new MxCADView({
  9.    // mxcadApp.getStaticAssetPath()).toString() Obtain the static resource path of mxcad-app. The default used static resource isnodemodules/mxcad-app/dist/mxcadAppAssets
  10.      openFile: new URL("test.mxweb", mxcadApp.getStaticAssetPath()).toString(),
  11.      rootContainer: "#myMxCAD",
  12.    }).create();
复制代码
If you need to modify the static resource path within the MxCAD project, you can do so by calling the `setStaticAssetPath()` method.  
  1. import { mxcadApp } from "mxcad-app";
  2. 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:  
  1. import { mxcadAssetsPlugin } from "mxcad-app/vite";
  2.    // vite.config.js
  3.    export default {
  4.      plugins: [
  5.        mxcadAssetsPlugin({
  6.          isWasmSt:true,
  7.          libraryNames: ["vue"],
  8.          outputDir:'testName',
  9.                // Modify UI configuration
  10.          transformMxUiConfig: (config) => {
  11.            config.title = "My CAD"; // Modified title
  12.            return config;
  13.          },
  14.          // Modify the server configuration
  15.          transformMxServerConfig: (config) => {
  16.            config.serverUrl = "/api/cad"; // Modify the API address
  17.            return config;
  18.          },
  19.          // Modify shortcut command (command alias)
  20.          // transformMxQuickCommand: (config) => config
  21.          // Modify the configuration of the sketch and annotation UI mode
  22.          // transformMxSketchesAndNotesUiConfig: (config) => config
  23.          // Modify the Vuetify theme configuration
  24.          // transformVuetifyThemeConfig: (config) => config
  25.        }),
  26.      ],
  27.    };
Configuration based on webpack:
  1.    import { MxCadAssetsWebpackPlugin } from "mxcad-app/webpack";   
  2.    module.exports = {
  3.      plugins: [
  4.        new MxCadAssetsWebpackPlugin({
  5.          isWasmSt:true,
  6.          libraryNames: ["vue"],
  7.          outputDir:'testName',
  8.          transformMxServerConfig: (config) => {
  9.            if (process.env.NODE_ENV === 'production') {
  10.              config.serverUrl = 'https://api.prod.com/cad';
  11.            }
  12.            return config;
  13.          }
  14.            ...
  15.        })
  16.     ]
  17.    };
复制代码

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.
  1. /** Whether to load WASM in single-thread mode (default is to use multi-threading and load)*/
  2.   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.
  1. libraryNames: ["vue","axios"...]
复制代码
  1.   // 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.
  2.   import { ref } from "vue";
  3.   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.
  1. 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.
  1. // Modify UI Configuration
  2.   /** For more UI configuration options, click inside the config to view */
  3.   transformMxUiConfig: (config) => {
  4.       config.title = "My CAD"; // Modify the title
  5.       config.mTopButtonBarData.push({
  6.           "icon": "textIcon",
  7.           "prompt": "test",
  8.           "cmd": "Mx_test"
  9.       });// Add top button bar button
  10.       ...
  11.       return config;
  12.   }
  13.   // The configuration for modifying the Sketch and Annotations UI mode is the same as above
  14.      // transformMxSketchesAndNotesUiConfig: (config) => config
复制代码
  1.    // Modify CAD Quick Commands (Command Aliases)
  2.      /** For more CAD quick command configuration options, click inside the config to view */
  3.      transformMxQuickCommand: (config) => {
  4.          // Add aliases '_test', 't' for command Mx_test
  5.          // config is the internal MxCAD named alias array object
  6.          config.push(['Mx_test','_test','t'])
  7.       return config
  8.      }
复制代码
  1.      // 修改服务器配置  
  2.      /** 更多修改服务器配置可点击config内部查看 */
  3.      transformMxServerConfig: (config) => {
  4.          config.serverUrl = "/api/cad"; // 修改API地址
  5.          config.font.push('txt.shx', 'gdt.shx');// 添加MxCAD项目初始需要加载的字体文件
  6.          ...
  7.       return config;
  8.      }
复制代码
  1.    // Modify Vuetify Theme Configuration
  2.      /** For more Vuetify theme configuration options, click inside the config to view */
  3.      transformVuetifyThemeConfig: (config) => {
  4.          config.defaultTheme = 'light';//dark or light
  5.       return config
  6.      }
复制代码

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.
  1. import { MxCpp } from 'mxcad'
  2. // 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`.
  1. import { MxFun } from 'mxdraw'
  2. // Output command line content
  3. 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`.
IIIExample 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
  1. import { MxCpp, McCmColor } from "mxcad";
  2.    function Mx_Test_DrawLine() {
  3.      let mxcad = MxCpp.getCurrentMxCAD();
  4.      // Clear current display content
  5.      mxcad.newFile();
  6.      // Change color back to black and white
  7.      mxcad.drawColorIndex = 0;
  8.      // Change linetype to solid line
  9.      mxcad.drawLinetype = "";
  10.      // Set line width 4
  11.      mxcad.drawLineWidth = 0;
  12.      // Create a layer named "LineLayer"
  13.      mxcad.addLayer("LineLayer");
  14.      // Set current layer to "LineLayer"
  15.      mxcad.drawLayer = "LineLayer";
  16.      // Directly draw a solid line
  17.      // 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
  18.      mxcad.drawLine(0, 0, 100, 0);
  19.      // Draw a solid diagonal line
  20.      mxcad.drawLine(200, 0, 300, 100);
  21.      //----------------------------------------------------------------------------------------------------------
  22.      // Draw a dashed line
  23.      // 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.
  24.      mxcad.addLinetype("MyLineType", "6,-10");
  25.      // Set current linetype to "MyLineType"
  26.      mxcad.drawLinetype = "MyLineType";
  27.      // Draw a dashed line
  28.      mxcad.drawLine(0, 30, 100, 30);
  29.      // Draw a diagonal dashed line
  30.      mxcad.drawLine(200, 30, 300, 130);
  31.      //---------------------------------------------------------------------------------------------------------
  32.      // 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.
  33.      mxcad.drawColor = new McCmColor(0, 0, 255);
  34.      // Draw a blue dashed line
  35.      mxcad.drawLine(0, 60, 100, 60);
  36.      // Draw a blue diagonal dashed line
  37.      mxcad.drawLine(200, 60, 300, 160);
  38.      //---------------------------------------------------------------------------------------------------------
  39.      // Change color back to black and white
  40.      mxcad.drawColorIndex = 0;
  41.      // Change linetype to solid line
  42.      mxcad.drawLinetype = "";
  43.      // Set line width 4
  44.      mxcad.drawLineWidth = 4;
  45.      // Draw a line with width
  46.      mxcad.drawLine(0, 90, 100, 90);
  47.      // Draw a diagonal line with width
  48.      mxcad.drawLine(200, 90, 300, 190);
  49.      //---------------------------------------------------------------------------------------------------------
  50.      // Draw a center line (dash-dot line)
  51.      mxcad.addLinetype("MyLineType2", "10,-2,3,-2");
  52.      // Change linetype to center line
  53.      mxcad.drawLinetype = "MyLineType2";
  54.      // 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.
  55.      mxcad.drawColor = new McCmColor(255, 0, 0);
  56.      // Draw a red center line with width
  57.      mxcad.drawLine(0, 120, 100, 120);
  58.      // Draw a red diagonal center line with width
  59.      mxcad.drawLine(200, 120, 300, 220);
  60.      //---------------------------------------------------------------------------------------------------------
  61.      // Add a linetype with shape
  62.      mxcad.addTextStyle("MyLineTypeTextStyle", "txt.shx", "hztxt.shx", 1);
  63.      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");
  64.      mxcad.drawLinetype = "MyLineType3";
  65.      mxcad.drawLineWidth = 0;
  66.      // Draw a red center line with width
  67.      mxcad.drawLine(350, 120, 600, 120);
  68.      //---------------------------------------------------------------------------------------------------------
  69.      // Add a linetype with shape
  70.      // Change color back to black and white
  71.      mxcad.drawColorIndex = 0;
  72.      mxcad.drawLineWidth = 4;
  73.      // Draw a red center line with width
  74.      mxcad.drawLine(350, 220, 600, 220);                                       
  75.      // Zoom all entities to current display viewport
  76.      mxcad.zoomAll();
  77.      // Update viewport display
  78.      mxcad.updateDisplay();
  79.    }
复制代码

3.2、Register drawing command
  1. import { MxFun } from 'mxdraw';
  2. MxFun.addCommand("Mx_Test_DrawLine", Mx_Test_DrawLine);
复制代码
3.3、Binding call logic (taking the example of clicking a button)
  1. <button onclick="MyTestFun('Mx_Test_DrawLine')"> Draw a straight line </button>
复制代码
  1. const MyTestFun = (str:string)=> MxFun.sendStringToExecute(str);
复制代码

3.4、Function Effect Demonstration:
For more examples of mxcad-app related projects, you can download our mxdraw cloud chart development package to learn more details.

本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-5 06:14 , Processed in 0.176041 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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