@mlightcad/cad-simple-viewer 1.0.14 → 1.0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -87
- package/dist/index.js +2916 -2367
- package/dist/index.umd.cjs +21 -21
- package/dist/libredwg-parser-worker.js +15161 -0
- package/lib/app/AcApGlobalFunc.d.ts +24 -0
- package/lib/app/AcApGlobalFunc.d.ts.map +1 -0
- package/lib/app/AcApGlobalFunc.js +91 -0
- package/lib/app/AcApGlobalFunc.js.map +1 -0
- package/lib/app/index.d.ts +1 -0
- package/lib/app/index.d.ts.map +1 -1
- package/lib/app/index.js +1 -0
- package/lib/app/index.js.map +1 -1
- package/package.json +5 -4
package/README.md
CHANGED
|
@@ -44,8 +44,6 @@ Firstly, add the following dependencies into your package.json.
|
|
|
44
44
|
|
|
45
45
|
- @mlightcad/cad-simple-viewer
|
|
46
46
|
- @mlightcad/data-model
|
|
47
|
-
- @mlightcad/libredwg-converter
|
|
48
|
-
- @mlightcad/libredwg-web
|
|
49
47
|
|
|
50
48
|
Secondly, add one canvas element in your html.
|
|
51
49
|
|
|
@@ -59,7 +57,10 @@ Thirdly, add the following code in the entry point of cad-simple-viewer integrat
|
|
|
59
57
|
|
|
60
58
|
```typescript
|
|
61
59
|
import { AcApDocManager } from '@mlightcad/cad-simple-viewer'
|
|
62
|
-
import { AcDbDatabaseConverterManager, AcDbFileType } from '@mlightcad/data-model'
|
|
60
|
+
import { AcDbDatabaseConverterManager, AcDbFileType, registerConverters } from '@mlightcad/data-model'
|
|
61
|
+
|
|
62
|
+
// Register DXF and DWG converters
|
|
63
|
+
registerConverters()
|
|
63
64
|
|
|
64
65
|
// Get canvas DOM element by its id
|
|
65
66
|
const canvas = document.getElementById('canvas') as HTMLCanvasElement
|
|
@@ -89,119 +90,50 @@ const success = await AcApDocManager.instance.openDocument(
|
|
|
89
90
|
......
|
|
90
91
|
```
|
|
91
92
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
By default, cad-simple-viewer registers DXF converter only and can view DXF file only. If you want to view DWG file, you need to register DWG converter. The following example code shows how to register DWG converter.
|
|
93
|
+
Finally, copy web worker javascript file to `dist/assets` folder.
|
|
95
94
|
|
|
96
|
-
|
|
97
|
-
import {
|
|
98
|
-
AcDbDatabaseConverterManager,
|
|
99
|
-
AcDbFileType
|
|
100
|
-
} from '@mlightcad/data-model'
|
|
101
|
-
import { AcDbLibreDwgConverter } from '@mlightcad/libredwg-converter'
|
|
102
|
-
|
|
103
|
-
const registerConverters = async () => {
|
|
104
|
-
try {
|
|
105
|
-
const isDev = import.meta.env.DEV
|
|
106
|
-
if (!isDev) {
|
|
107
|
-
// Production mode - use dynamic import with explicit chunk name
|
|
108
|
-
const instance = await import(
|
|
109
|
-
/* webpackChunkName: "libredwg-web" */ '@mlightcad/libredwg-web'
|
|
110
|
-
)
|
|
111
|
-
const converter = new AcDbLibreDwgConverter(await instance.createModule())
|
|
112
|
-
AcDbDatabaseConverterManager.instance.register(
|
|
113
|
-
AcDbFileType.DWG,
|
|
114
|
-
converter
|
|
115
|
-
)
|
|
116
|
-
}
|
|
117
|
-
} catch (error) {
|
|
118
|
-
console.error('Failed to load libredwg-web: ', error)
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
```
|
|
95
|
+
Web worker are used to parser dxf/dwg file so that UI not blocked. You can copy the following web worker files to folder `dist/assets` manually.
|
|
122
96
|
|
|
123
|
-
|
|
97
|
+
- `./node_modules/@mlightcad/data-model/dist/dxf-parser-worker.js`
|
|
98
|
+
- `./node_modules/@mlightcad/cad-simple-viewer/dist/libredwg-parser-worker.js`
|
|
124
99
|
|
|
125
|
-
|
|
100
|
+
However, `vite-plugin-static-copy` is recommended to make your lefe easier.
|
|
126
101
|
|
|
127
102
|
```typescript
|
|
128
103
|
import { resolve } from 'path'
|
|
129
|
-
import { defineConfig
|
|
104
|
+
import { defineConfig } from 'vite'
|
|
130
105
|
import { viteStaticCopy } from 'vite-plugin-static-copy'
|
|
131
106
|
|
|
132
|
-
export default defineConfig((
|
|
107
|
+
export default defineConfig(() => {
|
|
133
108
|
return {
|
|
134
|
-
|
|
135
|
-
port: 3000
|
|
136
|
-
},
|
|
109
|
+
base: './',
|
|
137
110
|
build: {
|
|
138
|
-
target: 'es2020',
|
|
139
111
|
modulePreload: false,
|
|
140
112
|
rollupOptions: {
|
|
141
113
|
// Main entry point for the app
|
|
142
114
|
input: {
|
|
143
115
|
main: resolve(__dirname, 'index.html')
|
|
144
|
-
},
|
|
145
|
-
output: {
|
|
146
|
-
manualChunks: id => {
|
|
147
|
-
if (id.includes('@mlightcad/libredwg-web')) {
|
|
148
|
-
return 'libredwg-web'
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
116
|
}
|
|
152
117
|
}
|
|
153
118
|
},
|
|
154
119
|
plugins: [
|
|
155
|
-
|
|
120
|
+
viteStaticCopy({
|
|
156
121
|
targets: [
|
|
157
122
|
{
|
|
158
|
-
src: './node_modules/@mlightcad/
|
|
123
|
+
src: './node_modules/@mlightcad/data-model/dist/dxf-parser-worker.js',
|
|
124
|
+
dest: 'assets'
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
src: './node_modules/@mlightcad/cad-simple-viewer/dist/libredwg-parser-worker.js',
|
|
159
128
|
dest: 'assets'
|
|
160
129
|
}
|
|
161
130
|
]
|
|
162
|
-
})
|
|
131
|
+
})
|
|
163
132
|
]
|
|
164
133
|
}
|
|
165
134
|
})
|
|
166
135
|
```
|
|
167
136
|
|
|
168
|
-
Secondly, add the following code in your html page such as `index.html` to dynamically import `libredwg-web` and trigger one event `libredwg-ready` after loaded.
|
|
169
|
-
|
|
170
|
-
```html
|
|
171
|
-
<script type="module" defer>
|
|
172
|
-
if (import.meta.env.DEV) {
|
|
173
|
-
(async () => {
|
|
174
|
-
// Create a script element to load the module
|
|
175
|
-
const script = document.createElement("script");
|
|
176
|
-
script.type = "module";
|
|
177
|
-
script.src = "/assets/libredwg-web.js";
|
|
178
|
-
script.async = true;
|
|
179
|
-
|
|
180
|
-
script.onload = async () => {
|
|
181
|
-
// Import dynamically after script is loaded
|
|
182
|
-
const actualModule = await import(/* @vite-ignore */script.src);
|
|
183
|
-
const libredwg = await actualModule.createModule();
|
|
184
|
-
window.dispatchEvent(new CustomEvent("libredwg-ready", { detail: libredwg }));
|
|
185
|
-
};
|
|
186
|
-
|
|
187
|
-
document.body.appendChild(script);
|
|
188
|
-
})();
|
|
189
|
-
}
|
|
190
|
-
</script>
|
|
191
|
-
```
|
|
192
|
-
|
|
193
|
-
Thirdly, listen event `libredwg-ready` to register DWG converter `AcDbLibreDwgConverter` after `libredwg-ready` loaded.
|
|
194
|
-
|
|
195
|
-
```typescript
|
|
196
|
-
// This is for development mode only. In production mode, the library is bundled
|
|
197
|
-
window.addEventListener('libredwg-ready', event => {
|
|
198
|
-
// @ts-expect-error this is one custom event and you can get details in index.html
|
|
199
|
-
const instance = event.detail as LibreDwgEx
|
|
200
|
-
const converter = new AcDbLibreDwgConverter(instance)
|
|
201
|
-
AcDbDatabaseConverterManager.instance.register(AcDbFileType.DWG, converter)
|
|
202
|
-
})
|
|
203
|
-
```
|
|
204
|
-
|
|
205
137
|
### Beyond a Viewer
|
|
206
138
|
|
|
207
139
|
While `cad-simple-viewer` doesn't support saving drawings to DWG/DXF files, it provides comprehensive support for **modifying drawings in real-time**. You can add, edit, and delete entities within the drawing, and the viewer will automatically update to reflect these changes.
|