@mlightcad/cad-simple-viewer 1.0.0 → 1.0.2

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.
Files changed (136) hide show
  1. package/README.md +296 -2
  2. package/dist/index.js +1348 -122
  3. package/dist/index.umd.cjs +2 -2
  4. package/lib/app/AcApContext.d.ts +45 -3
  5. package/lib/app/AcApContext.d.ts.map +1 -1
  6. package/lib/app/AcApContext.js +43 -3
  7. package/lib/app/AcApContext.js.map +1 -1
  8. package/lib/app/AcApDocCreator.d.ts +68 -2
  9. package/lib/app/AcApDocCreator.d.ts.map +1 -1
  10. package/lib/app/AcApDocCreator.js +67 -2
  11. package/lib/app/AcApDocCreator.js.map +1 -1
  12. package/lib/app/AcApDocManager.d.ts +227 -12
  13. package/lib/app/AcApDocManager.d.ts.map +1 -1
  14. package/lib/app/AcApDocManager.js +229 -17
  15. package/lib/app/AcApDocManager.js.map +1 -1
  16. package/lib/app/AcApDocument.d.ts +77 -3
  17. package/lib/app/AcApDocument.d.ts.map +1 -1
  18. package/lib/app/AcApDocument.js +75 -3
  19. package/lib/app/AcApDocument.js.map +1 -1
  20. package/lib/app/AcApFontLoader.d.ts +32 -0
  21. package/lib/app/AcApFontLoader.d.ts.map +1 -1
  22. package/lib/app/AcApFontLoader.js +30 -0
  23. package/lib/app/AcApFontLoader.js.map +1 -1
  24. package/lib/app/AcApSettingManager.d.ts +191 -3
  25. package/lib/app/AcApSettingManager.d.ts.map +1 -1
  26. package/lib/app/AcApSettingManager.js +161 -0
  27. package/lib/app/AcApSettingManager.js.map +1 -1
  28. package/lib/command/AcApConvertToSvgCmd.d.ts +26 -0
  29. package/lib/command/AcApConvertToSvgCmd.d.ts.map +1 -1
  30. package/lib/command/AcApConvertToSvgCmd.js +26 -0
  31. package/lib/command/AcApConvertToSvgCmd.js.map +1 -1
  32. package/lib/command/AcApOpenCmd.d.ts +31 -0
  33. package/lib/command/AcApOpenCmd.d.ts.map +1 -1
  34. package/lib/command/AcApOpenCmd.js +31 -0
  35. package/lib/command/AcApOpenCmd.js.map +1 -1
  36. package/lib/command/AcApPanCmd.d.ts +25 -0
  37. package/lib/command/AcApPanCmd.d.ts.map +1 -1
  38. package/lib/command/AcApPanCmd.js +25 -0
  39. package/lib/command/AcApPanCmd.js.map +1 -1
  40. package/lib/command/AcApQNewCmd.d.ts +26 -0
  41. package/lib/command/AcApQNewCmd.d.ts.map +1 -1
  42. package/lib/command/AcApQNewCmd.js +26 -0
  43. package/lib/command/AcApQNewCmd.js.map +1 -1
  44. package/lib/command/AcApSelectCmd.d.ts +26 -0
  45. package/lib/command/AcApSelectCmd.d.ts.map +1 -1
  46. package/lib/command/AcApSelectCmd.js +26 -0
  47. package/lib/command/AcApSelectCmd.js.map +1 -1
  48. package/lib/command/AcApSvgConvertor.d.ts +48 -0
  49. package/lib/command/AcApSvgConvertor.d.ts.map +1 -1
  50. package/lib/command/AcApSvgConvertor.js +48 -0
  51. package/lib/command/AcApSvgConvertor.js.map +1 -1
  52. package/lib/command/AcApZoomCmd.d.ts +29 -0
  53. package/lib/command/AcApZoomCmd.d.ts.map +1 -1
  54. package/lib/command/AcApZoomCmd.js +29 -0
  55. package/lib/command/AcApZoomCmd.js.map +1 -1
  56. package/lib/command/AcApZoomToBoxCmd.d.ts +57 -0
  57. package/lib/command/AcApZoomToBoxCmd.d.ts.map +1 -1
  58. package/lib/command/AcApZoomToBoxCmd.js +57 -0
  59. package/lib/command/AcApZoomToBoxCmd.js.map +1 -1
  60. package/lib/editor/command/AcEdCommand.d.ts +118 -9
  61. package/lib/editor/command/AcEdCommand.d.ts.map +1 -1
  62. package/lib/editor/command/AcEdCommand.js +113 -9
  63. package/lib/editor/command/AcEdCommand.js.map +1 -1
  64. package/lib/editor/command/AcEdCommandStack.d.ts +59 -5
  65. package/lib/editor/command/AcEdCommandStack.d.ts.map +1 -1
  66. package/lib/editor/command/AcEdCommandStack.js +48 -5
  67. package/lib/editor/command/AcEdCommandStack.js.map +1 -1
  68. package/lib/editor/global/eventBus.d.ts +76 -7
  69. package/lib/editor/global/eventBus.d.ts.map +1 -1
  70. package/lib/editor/global/eventBus.js +38 -0
  71. package/lib/editor/global/eventBus.js.map +1 -1
  72. package/lib/editor/input/AcEdBaseInput.d.ts +65 -1
  73. package/lib/editor/input/AcEdBaseInput.d.ts.map +1 -1
  74. package/lib/editor/input/AcEdBaseInput.js +62 -1
  75. package/lib/editor/input/AcEdBaseInput.js.map +1 -1
  76. package/lib/editor/input/AcEdBoxSelector.d.ts +81 -0
  77. package/lib/editor/input/AcEdBoxSelector.d.ts.map +1 -1
  78. package/lib/editor/input/AcEdBoxSelector.js +80 -0
  79. package/lib/editor/input/AcEdBoxSelector.js.map +1 -1
  80. package/lib/editor/input/AcEdCursorManager.d.ts +95 -8
  81. package/lib/editor/input/AcEdCursorManager.d.ts.map +1 -1
  82. package/lib/editor/input/AcEdCursorManager.js +95 -11
  83. package/lib/editor/input/AcEdCursorManager.js.map +1 -1
  84. package/lib/editor/input/AcEdInputPoint.d.ts +38 -1
  85. package/lib/editor/input/AcEdInputPoint.d.ts.map +1 -1
  86. package/lib/editor/input/AcEdInputPoint.js +38 -1
  87. package/lib/editor/input/AcEdInputPoint.js.map +1 -1
  88. package/lib/editor/input/AcEdJig.d.ts +166 -0
  89. package/lib/editor/input/AcEdJig.d.ts.map +1 -1
  90. package/lib/editor/input/AcEdJig.js +164 -0
  91. package/lib/editor/input/AcEdJig.js.map +1 -1
  92. package/lib/editor/input/AcEdJigLoop.d.ts +49 -0
  93. package/lib/editor/input/AcEdJigLoop.d.ts.map +1 -1
  94. package/lib/editor/input/AcEdJigLoop.js +48 -0
  95. package/lib/editor/input/AcEdJigLoop.js.map +1 -1
  96. package/lib/editor/input/AcEdSelectionSet.d.ts +122 -2
  97. package/lib/editor/input/AcEdSelectionSet.d.ts.map +1 -1
  98. package/lib/editor/input/AcEdSelectionSet.js +117 -1
  99. package/lib/editor/input/AcEdSelectionSet.js.map +1 -1
  100. package/lib/editor/input/AcEditor.d.ts +90 -6
  101. package/lib/editor/input/AcEditor.d.ts.map +1 -1
  102. package/lib/editor/input/AcEditor.js +86 -6
  103. package/lib/editor/input/AcEditor.js.map +1 -1
  104. package/lib/editor/view/AcEdBaseView.d.ts +163 -16
  105. package/lib/editor/view/AcEdBaseView.d.ts.map +1 -1
  106. package/lib/editor/view/AcEdBaseView.js +94 -5
  107. package/lib/editor/view/AcEdBaseView.js.map +1 -1
  108. package/lib/util/AcTrGeometryUtil.d.ts +65 -0
  109. package/lib/util/AcTrGeometryUtil.d.ts.map +1 -1
  110. package/lib/util/AcTrGeometryUtil.js +65 -0
  111. package/lib/util/AcTrGeometryUtil.js.map +1 -1
  112. package/lib/view/AcTrLayer.d.ts +40 -5
  113. package/lib/view/AcTrLayer.d.ts.map +1 -1
  114. package/lib/view/AcTrLayer.js +34 -5
  115. package/lib/view/AcTrLayer.js.map +1 -1
  116. package/lib/view/AcTrLayout.d.ts +122 -23
  117. package/lib/view/AcTrLayout.d.ts.map +1 -1
  118. package/lib/view/AcTrLayout.js +103 -23
  119. package/lib/view/AcTrLayout.js.map +1 -1
  120. package/lib/view/AcTrLayoutView.d.ts +74 -16
  121. package/lib/view/AcTrLayoutView.d.ts.map +1 -1
  122. package/lib/view/AcTrLayoutView.js +65 -16
  123. package/lib/view/AcTrLayoutView.js.map +1 -1
  124. package/lib/view/AcTrLayoutViewManager.d.ts +58 -12
  125. package/lib/view/AcTrLayoutViewManager.d.ts.map +1 -1
  126. package/lib/view/AcTrLayoutViewManager.js +56 -12
  127. package/lib/view/AcTrLayoutViewManager.js.map +1 -1
  128. package/lib/view/AcTrScene.d.ts +48 -4
  129. package/lib/view/AcTrScene.d.ts.map +1 -1
  130. package/lib/view/AcTrScene.js +44 -4
  131. package/lib/view/AcTrScene.js.map +1 -1
  132. package/lib/view/AcTrView2d.d.ts +71 -0
  133. package/lib/view/AcTrView2d.d.ts.map +1 -1
  134. package/lib/view/AcTrView2d.js +65 -0
  135. package/lib/view/AcTrView2d.js.map +1 -1
  136. package/package.json +8 -4
package/README.md CHANGED
@@ -11,6 +11,15 @@ This module doesn't depend on any UI framework and doesn't provide any UI except
11
11
  - Performance-focused settings and context management
12
12
  - Framework-agnostic design for maximum flexibility
13
13
 
14
+ ## When Should You Choose cad-simple-viewer?
15
+
16
+ Use `cad-simple-viewer` if you need **core CAD logic only** (document management, command stack, rendering engine integration) without any UI framework dependencies. This package is ideal if:
17
+
18
+ - You want to build your **own custom UI** or integrate CAD functionality into a non-Vue or non-web environment.
19
+ - You require maximum flexibility and performance for handling large CAD files, and plan to connect the logic to your own rendering or UI layer.
20
+ - You want a framework-agnostic solution that provides only the essential CAD operations and canvas rendering.
21
+
22
+ **Recommended for:** Custom integrations, headless CAD processing, or advanced users building highly tailored CAD solutions.
14
23
 
15
24
  ## Directory Structure (partial)
16
25
  - `src/app/` – Document/context management, settings
@@ -19,8 +28,293 @@ This module doesn't depend on any UI framework and doesn't provide any UI except
19
28
  - `src/view/` – Layout and scene management
20
29
  - `src/util/` – Utilities
21
30
 
31
+ ## Installation
32
+
33
+ ```bash
34
+ npm install @mlightcad/cad-simple-viewer
35
+ ```
36
+
22
37
  ## Usage
23
- Entry point: `src/app/index.ts`. Used by the main application to manage documents and commands.
38
+
39
+ Please refer to sub-package `cad-simple-viewer-example` as one example.
40
+
41
+ ### Basic Usage
42
+
43
+ Firstly, add the following dependencies into your package.json.
44
+
45
+ - @mlightcad/cad-simple-viewer
46
+ - @mlightcad/data-model
47
+ - @mlightcad/libredwg-converter
48
+ - @mlightcad/libredwg-web
49
+
50
+ Secondly, add one canvas element in your html.
51
+
52
+ ```html
53
+ <body>
54
+ <canvas id="canvas"></canvas>
55
+ </body>
56
+ ```
57
+
58
+ Thirdly, add the following code in the entry point of cad-simple-viewer integration page.
59
+
60
+ ```typescript
61
+ import { AcApDocManager } from '@mlightcad/cad-simple-viewer'
62
+ import { AcDbDatabaseConverterManager, AcDbFileType } from '@mlightcad/data-model'
63
+
64
+ // Get canvas DOM element by its id
65
+ const canvas = document.getElementById('canvas') as HTMLCanvasElement
66
+ AcApDocManager.createInstance(canvas)
67
+
68
+ // Although it is optional, it is better to call this method to load the default fonts.
69
+ // The default fonts are used if fonts used in drawing are not found.
70
+ await AcApDocManager.instance.loadDefaultFonts()
71
+
72
+ // Read the file content
73
+ const fileContent = await this.readFile(file)
74
+
75
+ // Set database options
76
+ const options: AcDbOpenDatabaseOptions = {
77
+ minimumChunkSize: 1000,
78
+ readOnly: true
79
+ }
80
+
81
+ // Open the document
82
+ const success = await AcApDocManager.instance.openDocument(
83
+ file.name,
84
+ fileContent,
85
+ options
86
+ )
87
+
88
+ // Your application logic here...
89
+ ......
90
+ ```
91
+
92
+ ### Register DWG Converter to Display Drawings in DWG Format
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.
95
+
96
+ ```typescript
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
+ ```
122
+
123
+ However, the code above works in production mode only even if removing check on 'isDev'. So you still need to make additional changes in order to make it work in Vite dev mode.
124
+
125
+ Firstly, update `vite.config.ts` to copy `libredwg-web.js` to folder `assets` using `vite-plugin-static-copy`. Do remember adding `vite-plugin-static-copy` as your package devDependencies.
126
+
127
+ ```typescript
128
+ import { resolve } from 'path'
129
+ import { defineConfig, type ConfigEnv } from 'vite'
130
+ import { viteStaticCopy } from 'vite-plugin-static-copy'
131
+
132
+ export default defineConfig(({ command }: ConfigEnv) => {
133
+ return {
134
+ server: {
135
+ port: 3000
136
+ },
137
+ build: {
138
+ target: 'es2020',
139
+ modulePreload: false,
140
+ rollupOptions: {
141
+ // Main entry point for the app
142
+ input: {
143
+ 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
+ }
152
+ }
153
+ },
154
+ plugins: [
155
+ command === 'serve' ? viteStaticCopy({
156
+ targets: [
157
+ {
158
+ src: './node_modules/@mlightcad/libredwg-web/dist/libredwg-web.js',
159
+ dest: 'assets'
160
+ }
161
+ ]
162
+ }) : undefined
163
+ ]
164
+ }
165
+ })
166
+ ```
167
+
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
+ ### Beyond a Viewer
206
+
207
+ 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.
208
+
209
+ When you modify entities, you're working directly with the underlying drawing database. The viewer automatically detects these changes and updates the display accordingly. This real-time synchronization ensures that:
210
+
211
+ - All modifications are immediately visible
212
+ - The command stack properly tracks changes for undo/redo operations. This will be implemented soon.
213
+
214
+ This capability makes `cad-simple-viewer` suitable for applications that need to not only display CAD files but also allow users to interact with and modify the drawing content.
215
+
216
+ **Important Note**: The usage patterns in `cad-simple-viewer` are **very similar to AutoCAD RealDWG**. If you're familiar with AutoCAD RealDWG development, you'll find the API structure and workflow nearly identical. The main difference is that we use the [**realdwg-web API**](https://mlight-lee.github.io/realdwg-web/) instead of the native RealDWG libraries.
217
+
218
+ #### Example: Adding Entities
219
+
220
+ The following code demonstrates how to add entities, following the same pattern you'd use in AutoCAD RealDWG:
221
+
222
+ ```typescript
223
+ import { AcApDocManager } from '@mlightcad/cad-simple-viewer'
224
+ import { AcDbLine, AcDbCircle, AcDbText, AcGePoint3d, AcGeVector3d } from '@mlightcad/data-model'
225
+
226
+ // Get the current document (same as RealDWG)
227
+ const doc = AcApDocManager.instance.activeDocument
228
+ if (!doc) return
229
+
230
+ // Get the model space (identical to RealDWG pattern)
231
+ const modelSpace = doc.database.modelSpace
232
+
233
+ // Add a line (same constructor and property setting as RealDWG)
234
+ const startPoint = new AcGePoint3d(0, 0, 0)
235
+ const endPoint = new AcGePoint3d(100, 100, 0)
236
+ const line = new AcDbLine(startPoint, endPoint)
237
+ line.layer = '0' // Set layer (same property name as RealDWG)
238
+ line.color = 1 // Red color (same color index as RealDWG)
239
+ modelSpace.appendEntity(line)
240
+
241
+ // Add a circle (identical API to RealDWG)
242
+ const centerPoint = new AcGePoint3d(50, 50, 0)
243
+ const radius = 25
244
+ const circle = new AcDbCircle(centerPoint, radius)
245
+ circle.layer = '0'
246
+ circle.color = 2 // Yellow color
247
+ modelSpace.appendEntity(circle)
248
+
249
+ // Add text (same constructor pattern as RealDWG)
250
+ const textPoint = new AcGePoint3d(0, -50, 0)
251
+ const text = new AcDbText(textPoint, 'Sample Text', 10) // position, text, height
252
+ text.layer = '0'
253
+ text.color = 3 // Green color
254
+ modelSpace.appendEntity(text)
255
+ ```
256
+
257
+ #### Example: Editing Entities
258
+
259
+ TBD
260
+
261
+ #### Example: Deleting Entities
262
+
263
+ TBD
264
+
265
+ #### Example: Working with Layers
266
+
267
+ TBD
268
+
269
+ ## Available Exports
270
+
271
+ ### Core Classes
272
+
273
+ - `AcApContext` - Main application context
274
+ - `AcApDocManager` - Document management
275
+ - `AcApDocument` - Individual document handling
276
+ - `AcApSettingManager` - Settings management
277
+
278
+ ### Commands
279
+
280
+ - `AcApOpenCmd` - Open file command
281
+ - `AcApZoomCmd` - Zoom command
282
+ - `AcApPanCmd` - Pan command
283
+ - `AcApSelectCmd` - Selection command
284
+ - `AcApConvertToSvgCmd` - SVG conversion command
285
+
286
+ ### Editor Components
287
+
288
+ - `AcEditor` - Main editor class
289
+ - `AcEdCommandStack` - Command stack management
290
+ - `AcEdSelectionSet` - Selection handling
291
+ - `AcEdInputPoint` - Input point management
292
+
293
+ ### View Components
294
+
295
+ - `AcTrScene` - Scene management
296
+ - `AcTrLayoutView` - Layout view handling
297
+ - `AcTrView2d` - 2D view management
298
+
299
+ ## Development
300
+
301
+ ```bash
302
+ # Install dependencies
303
+ pnpm install
304
+
305
+ # Start development server
306
+ pnpm dev
307
+
308
+ # Build the library
309
+ pnpm build
310
+
311
+ # Preview the build
312
+ pnpm preview
313
+ ```
24
314
 
25
315
  ## Role in MLightCAD
26
- This package acts as the core logic layer, connecting the frontend UI with the rendering engines and managing all document-related operations.
316
+ This package acts as the core logic layer, connecting the frontend UI with the rendering engines and managing all document-related operations.
317
+
318
+ ## License
319
+
320
+ MIT