@mlightcad/libdxfrw-converter 1.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 mlight-lee
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # @mlightcad/libdxfrw-converter
2
+
3
+ The `libdxfrw-converter` package provides a DWG file converter for the RealDWG-Web ecosystem, enabling reading and conversion of DWG files into the AutoCAD-like drawing database structure. It is based on the [libdxfrw](https://github.com/LibreDWG/libdxfrw) library compiled to WebAssembly.
4
+
5
+ ## Overview
6
+
7
+ This package implements a DWG file converter compatible with the RealDWG-Web data model. It allows you to register DWG file support in your application and convert DWG files into the in-memory drawing database.
8
+
9
+ ## Key Features
10
+
11
+ - **DWG File Support**: Read and convert DWG files to the drawing database
12
+ - **Integration**: Designed to work with the RealDWG-Web data model and converter manager
13
+ - **WebAssembly Powered**: Uses libdxfrw compiled to WASM for fast, browser-compatible parsing
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ npm install @mlightcad/libdxfrw-converter
19
+ ```
20
+
21
+ > **Peer dependencies:**
22
+ > - `@mlightcad/data-model`
23
+ > - `@mlightcad/libdxfrw-web`
24
+
25
+ ## Usage Example
26
+
27
+ ```typescript
28
+ import { AcDbDatabaseConverterManager, AcDbFileType } from '@mlightcad/data-model';
29
+ import { AcDbLibdxfrwConverter } from '@mlightcad/libdxfrw-converter';
30
+
31
+ // WASM module loading (async)
32
+ import('@mlightcad/libdxfrw-web/wasm/libdxfrw-web').then(libdxfrwModule => {
33
+ const dxfConverter = new AcDbLibdxfrwConverter(libdxfrwModule);
34
+ AcDbDatabaseConverterManager.instance.register(AcDbFileType.DWG, dxfConverter);
35
+ });
36
+ ```
37
+
38
+ ## API
39
+
40
+ - **AcDbLibdxfrwConverter**: Main converter class for DWG files (extends `AcDbDatabaseConverter`)
41
+
42
+ ## Dependencies
43
+
44
+ - **@mlightcad/data-model**: Drawing database and entity definitions
45
+ - **@mlightcad/libdxfrw-web**: WASM wrapper for libdxfrw
46
+
47
+ ## API Documentation
48
+
49
+ For detailed API documentation, visit the [RealDWG-Web documentation](https://mlight-lee.github.io/realdwg-web/).
50
+
51
+ ## Contributing
52
+
53
+ This package is part of the RealDWG-Web monorepo. Please refer to the main project README for contribution guidelines.
@@ -0,0 +1,489 @@
1
+ import { AcDbArc as S, AcDbCircle as z, AcGeVector3d as E, AcDbEllipse as R, AcDbBlockReference as L, AcDbHatch as C, AcGePolyline2d as W, AcGeLoop2d as N, AcGeLine2d as Y, AcGeCircArc2d as _, AcGeVector2d as F, AcGeEllipseArc2d as k, AcGeSpline3d as y, AcDbLine as H, AcGePoint3d as P, AcDbMText as V, AcDbPoint as j, AcDbPolyline as O, AcGePoint2d as M, AcDbRay as B, AcDbSpline as D, AcDbText as G, AcDbXline as X, AcDbRasterImageDef as $, AcDbDatabaseConverter as U, AcDbLinetypeTableRecord as q, AcDbTextStyleTableRecord as Z, AcDbDimStyleTableRecord as K, AcCmColor as J, AcDbLayerTableRecord as Q, AcDbViewportTableRecord as ee, AcDbBlockTableRecord as te, AcDbBatchProcessing as ie } from "@mlightcad/data-model";
2
+ class v {
3
+ convert(e) {
4
+ const i = this.createEntity(e);
5
+ return i && this.processCommonAttrs(e, i), i;
6
+ }
7
+ createEntity(e) {
8
+ const i = window.libdxfrw;
9
+ if (e.eType == i.DRW_ETYPE.ARC)
10
+ return this.convertArc(e);
11
+ if (e.eType == i.DRW_ETYPE.CIRCLE)
12
+ return this.convertCirle(e);
13
+ if (e.eType != i.DRW_ETYPE.DIMENSION) {
14
+ if (e.eType == i.DRW_ETYPE.ELLIPSE)
15
+ return this.convertEllipse(e);
16
+ if (e.eType == i.DRW_ETYPE.HATCH)
17
+ return this.convertHatch(e);
18
+ if (e.eType != i.DRW_ETYPE.IMAGE) {
19
+ if (e.eType != i.DRW_ETYPE.LEADER) {
20
+ if (e.eType == i.DRW_ETYPE.LINE)
21
+ return this.convertLine(e);
22
+ if (e.eType == i.DRW_ETYPE.MTEXT)
23
+ return this.convertMText(e);
24
+ if (e.eType != i.DRW_ETYPE.POLYLINE) {
25
+ if (e.eType == i.DRW_ETYPE.LWPOLYLINE)
26
+ return this.convertPolyline(e);
27
+ if (e.eType == i.DRW_ETYPE.POINT)
28
+ return this.convertPoint(e);
29
+ if (e.eType == i.DRW_ETYPE.RAY)
30
+ return this.convertRay(e);
31
+ if (e.eType == i.DRW_ETYPE.SPLINE)
32
+ return this.convertSpline(e);
33
+ if (e.eType == i.DRW_ETYPE.TEXT)
34
+ return this.convertText(e);
35
+ if (e.eType != i.DRW_ETYPE.SOLID) {
36
+ if (e.eType != i.DRW_ETYPE.VIEWPORT) {
37
+ if (e.eType == i.DRW_ETYPE.XLINE)
38
+ return this.convertXline(e);
39
+ if (e.eType == i.DRW_ETYPE.INSERT)
40
+ return this.convertBlockReference(e);
41
+ }
42
+ }
43
+ }
44
+ }
45
+ }
46
+ }
47
+ return null;
48
+ }
49
+ convertArc(e) {
50
+ return new S(
51
+ e.center(),
52
+ e.radius,
53
+ e.startAngle,
54
+ e.endAngle
55
+ );
56
+ }
57
+ convertCirle(e) {
58
+ return new z(e.basePoint, e.radius);
59
+ }
60
+ convertEllipse(e) {
61
+ const i = new E(e.secPoint), s = i.length();
62
+ return new R(
63
+ e.basePoint,
64
+ E.Z_AXIS,
65
+ i,
66
+ s,
67
+ s * e.ratio,
68
+ e.startAngle,
69
+ e.endAngle
70
+ );
71
+ }
72
+ convertBlockReference(e) {
73
+ const i = new L(e.name);
74
+ return e.basePoint && i.position.copy(e.basePoint), i.scaleFactors.x = e.xScale || 1, i.scaleFactors.y = e.yScale || 1, i.scaleFactors.z = e.zScale || 1, i.rotation = e.angle, i.normal.copy(e.extPoint), i;
75
+ }
76
+ convertHatch(e) {
77
+ const i = new C(), s = e.definitionLines;
78
+ for (let r = 0, t = s.size(); r < t; ++r) {
79
+ const o = s.get(r);
80
+ o != null && i.definitionLines.push({
81
+ angle: o.angle,
82
+ origin: o.base,
83
+ delta: o.offset,
84
+ dashPattern: this.toNumberArray(o.dashPattern)
85
+ });
86
+ }
87
+ i.hatchStyle = e.hatchStyle, i.patternName = e.name, i.patternType = e.patternType, i.patternAngle = e.angle, i.patternScale = e.scale;
88
+ const n = e.getLoopList();
89
+ for (let r = 0, t = n.size(); r < t; ++r) {
90
+ const o = n.get(r);
91
+ if (o != null)
92
+ if (o.type == 2) {
93
+ const d = o.getObjList().get(0);
94
+ if (d) {
95
+ const f = new W(), c = d.getVertexList();
96
+ for (let m = 0, p = c.size(); m < p; ++m) {
97
+ const a = c.get(m);
98
+ a != null && f.addVertexAt(m, {
99
+ x: a.x,
100
+ y: a.y,
101
+ bulge: a.bulge
102
+ });
103
+ }
104
+ i.add(f);
105
+ }
106
+ } else {
107
+ const d = window.libdxfrw, f = o.getObjList(), c = new N();
108
+ for (let m = 0, p = f.size(); m < p; ++m) {
109
+ const a = f.get(m);
110
+ if (a != null) {
111
+ if (a.eType == d.DRW_ETYPE.LINE) {
112
+ const l = a;
113
+ c.add(new Y(l.basePoint, l.secPoint));
114
+ } else if (a.eType == d.DRW_ETYPE.ARC) {
115
+ const l = a;
116
+ c.add(
117
+ new _(
118
+ l.center(),
119
+ l.radius,
120
+ l.startAngle,
121
+ l.endAngle,
122
+ !l.isccw
123
+ )
124
+ );
125
+ } else if (a.eType == d.DRW_ETYPE.ELLIPSE) {
126
+ const l = a, g = l.basePoint, b = l.secPoint, A = new F(b).length(), x = A * l.ratio;
127
+ let u = l.startAngle, h = l.endAngle;
128
+ const I = Math.atan2(b.y, b.x);
129
+ l.isCounterClockwise || (u = Math.PI * 2 - u, h = Math.PI * 2 - h), c.add(
130
+ new k(
131
+ { x: g.x, y: g.y, z: 0 },
132
+ A,
133
+ x,
134
+ u,
135
+ h,
136
+ !l.isCounterClockwise,
137
+ I
138
+ )
139
+ );
140
+ } else if (a.eType == d.DRW_ETYPE.SPLINE) {
141
+ const l = a;
142
+ if (l.numberOfControls > 0 && l.numberOfKnots > 0) {
143
+ const g = this.toPointArray(
144
+ l.getControlList(),
145
+ !0
146
+ ), b = this.toNumberArray(l.weights), T = this.toNumberArray(l.knots);
147
+ c.add(
148
+ new y(
149
+ g,
150
+ T,
151
+ b.length > 0 ? b : void 0
152
+ )
153
+ );
154
+ } else if (l.numberOfFits > 0) {
155
+ const g = this.toPointArray(
156
+ l.getFitList()
157
+ );
158
+ c.add(new y(g, "Uniform"));
159
+ }
160
+ }
161
+ }
162
+ }
163
+ i.add(c);
164
+ }
165
+ }
166
+ return i;
167
+ }
168
+ convertLine(e) {
169
+ const i = e.basePoint, s = e.secPoint;
170
+ return new H(
171
+ new P(i.x, i.y, i.z || 0),
172
+ new P(s.x, s.y, s.z || 0)
173
+ );
174
+ }
175
+ convertMText(e) {
176
+ const i = new V();
177
+ return i.contents = e.text, e.style != null && (i.styleName = e.style), i.height = e.height, i.lineSpacingFactor = e.interlin, i.rotation = e.angle || 0, i.location = e.basePoint, i;
178
+ }
179
+ convertPoint(e) {
180
+ const i = new j();
181
+ return i.position = e.basePoint, i;
182
+ }
183
+ convertPolyline(e) {
184
+ const i = new O();
185
+ i.closed = !!(e.flags & 1);
186
+ const s = e.getVertexList();
187
+ for (let n = 0, r = s.size(); n < r; ++n) {
188
+ const t = s.get(n);
189
+ t != null && i.addVertexAt(
190
+ n,
191
+ new M(t.x, t.y),
192
+ t.bulge,
193
+ t.startWidth,
194
+ t.endWidth
195
+ );
196
+ }
197
+ return i;
198
+ }
199
+ convertRay(e) {
200
+ const i = new B();
201
+ return i.basePoint.copy(e.basePoint), i.unitDir.copy(e.secPoint), i;
202
+ }
203
+ convertSpline(e) {
204
+ const i = this.toNumberArray(e.weights);
205
+ if (e.numberOfControls > 0 && e.numberOfKnots > 0) {
206
+ const s = new D(
207
+ this.toPointArray(e.getControlList(), !1),
208
+ this.toNumberArray(e.knots),
209
+ i.length > 0 ? i : void 0
210
+ );
211
+ return s.closed = !!(e.flags & 1), s;
212
+ } else if (e.numberOfFits > 0) {
213
+ const s = this.toPointArray(e.getFitList());
214
+ if (s.length > 0) {
215
+ const n = new D(s, "Uniform");
216
+ return n.closed = !!(e.flags & 1), n;
217
+ }
218
+ }
219
+ return null;
220
+ }
221
+ convertText(e) {
222
+ const i = new G();
223
+ return i.textString = e.text, i.styleName = e.style, i.height = e.height, i.position.copy(e.basePoint), i.rotation = e.angle || 0, i.oblique = e.oblique ?? 0, i.horizontalMode = e.alignH, i.verticalMode = e.alignV, i.widthFactor = e.widthScale ?? 1, i;
224
+ }
225
+ convertXline(e) {
226
+ const i = new X();
227
+ return i.basePoint.copy(e.basePoint), i.unitDir.copy(e.secPoint), i;
228
+ }
229
+ processCommonAttrs(e, i) {
230
+ i.layer = e.layer, i.objectId = e.handle.toString(), i.ownerId = e.parentHandle.toString(), e.lineType != null && (i.lineType = e.lineType), e.lWeight != null && (i.lineWeight = e.lWeight), e.ltypeScale != null && (i.linetypeScale = e.ltypeScale), e.color24 != null && (i.color.color = e.color24), e.color != null && (i.color.colorIndex = e.color), e.colorName != null && (i.color.colorName = e.colorName), e.visible != null && (i.visibility = e.visible), e.transparency != null && (i.transparency = e.transparency);
231
+ }
232
+ toNumberArray(e) {
233
+ const i = [];
234
+ for (let s = 0, n = e.size(); s < n; ++s) {
235
+ const r = e.get(s);
236
+ r != null && i.push(r);
237
+ }
238
+ return i;
239
+ }
240
+ toPointArray(e, i = !0) {
241
+ const s = [];
242
+ for (let n = 0, r = e.size(); n < r; ++n) {
243
+ const t = e.get(n);
244
+ t != null && s.push({ x: t.x, y: t.y, z: i ? 0 : t.z });
245
+ }
246
+ return s;
247
+ }
248
+ }
249
+ class ne {
250
+ convertImageDef(e) {
251
+ const i = new $();
252
+ return i.sourceFileName = e.path, i.objectId = e.handle.toString(), i.ownerId = e.parentHandle.toString(), i;
253
+ }
254
+ }
255
+ class oe extends U {
256
+ constructor(e) {
257
+ super(), this.librefrw = e;
258
+ }
259
+ onFinished() {
260
+ super.onFinished(), this.dwg && (this.dwg.delete(), this.dwg = void 0), this.database && (this.database.delete(), this.database = void 0), this.fileHandler && (this.fileHandler.delete(), this.fileHandler = void 0);
261
+ }
262
+ parse(e) {
263
+ if (this.librefrw == null)
264
+ throw new Error("librefrw is not loaded!");
265
+ const i = this.librefrw;
266
+ return this.database = new i.DRW_Database(), this.fileHandler = new i.DRW_FileHandler(), this.fileHandler.database = this.database, this.dwg = new i.DRW_DwgR(e), this.dwg.read(this.fileHandler, !1), this.database;
267
+ }
268
+ processLineTypes(e, i) {
269
+ const s = e.lineTypes;
270
+ for (let n = 0, r = s.size(); n < r; ++n) {
271
+ const t = s.get(n);
272
+ if (t != null) {
273
+ const o = {
274
+ name: t.name,
275
+ description: t.desc,
276
+ standardFlag: t.flags,
277
+ totalPatternLength: t.length,
278
+ pattern: this.convertLineTypePattern(t.path)
279
+ }, d = new q(o);
280
+ this.processCommonTableEntryAttrs(t, d), d.name = t.name, i.tables.linetypeTable.add(d);
281
+ }
282
+ }
283
+ }
284
+ convertLineTypePattern(e) {
285
+ const i = [];
286
+ for (let s = 0, n = e.size(); s < n; ++s)
287
+ i.push({
288
+ elementLength: e.get(s) || 0,
289
+ elementTypeFlag: 0
290
+ });
291
+ return i;
292
+ }
293
+ processTextStyles(e, i) {
294
+ const s = e.textStyles;
295
+ for (let n = 0, r = s.size(); n < r; ++n) {
296
+ const t = s.get(n);
297
+ if (t != null) {
298
+ const o = {
299
+ name: t.name,
300
+ standardFlag: t.flags,
301
+ fixedTextHeight: t.height,
302
+ widthFactor: t.width,
303
+ obliqueAngle: t.oblique,
304
+ textGenerationFlag: t.genFlag,
305
+ lastHeight: t.lastHeight,
306
+ font: t.font,
307
+ bigFont: t.bigFont
308
+ }, d = new Z(o);
309
+ this.processCommonTableEntryAttrs(t, d), i.tables.textStyleTable.add(d);
310
+ }
311
+ }
312
+ }
313
+ processDimStyles(e, i) {
314
+ const s = e.dimStyles;
315
+ for (let n = 0, r = s.size(); n < r; ++n) {
316
+ const t = s.get(n);
317
+ if (t != null) {
318
+ const o = {
319
+ name: t.name,
320
+ ownerId: t.parentHandle.toString(),
321
+ dimpost: t.dimpost || "",
322
+ dimapost: t.dimapost || "",
323
+ dimscale: t.dimscale,
324
+ dimasz: t.dimasz,
325
+ dimexo: t.dimexo,
326
+ dimdli: t.dimdli,
327
+ dimexe: t.dimexe,
328
+ dimrnd: t.dimrnd,
329
+ dimdle: t.dimdle,
330
+ dimtp: t.dimtp,
331
+ dimtm: t.dimtm,
332
+ dimtxt: t.dimtxt,
333
+ dimcen: t.dimcen,
334
+ dimtsz: t.dimtsz,
335
+ dimaltf: t.dimaltf,
336
+ dimlfac: t.dimlfac,
337
+ dimtvp: t.dimtvp,
338
+ dimtfac: t.dimtfac,
339
+ dimgap: t.dimgap,
340
+ dimaltrnd: t.dimaltrnd,
341
+ dimtol: t.dimtol == null || t.dimtol == 0 ? 0 : 1,
342
+ dimlim: t.dimlim == null || t.dimlim == 0 ? 0 : 1,
343
+ dimtih: t.dimtih == null || t.dimtih == 0 ? 0 : 1,
344
+ dimtoh: t.dimtoh == null || t.dimtoh == 0 ? 0 : 1,
345
+ dimse1: t.dimse1 == null || t.dimse1 == 0 ? 0 : 1,
346
+ dimse2: t.dimse2 == null || t.dimse2 == 0 ? 0 : 1,
347
+ dimtad: t.dimtad,
348
+ dimzin: t.dimzin,
349
+ dimazin: t.dimazin,
350
+ dimalt: t.dimalt,
351
+ dimaltd: t.dimaltd,
352
+ dimtofl: t.dimtofl,
353
+ dimsah: t.dimsah,
354
+ dimtix: t.dimtix,
355
+ dimsoxd: t.dimsoxd,
356
+ dimclrd: t.dimclrd,
357
+ dimclre: t.dimclre,
358
+ dimclrt: t.dimclrt,
359
+ dimadec: t.dimadec || 0,
360
+ dimunit: t.dimunit || 2,
361
+ dimdec: t.dimdec,
362
+ dimtdec: t.dimtdec,
363
+ dimaltu: t.dimaltu,
364
+ dimalttd: t.dimalttd,
365
+ dimaunit: t.dimaunit,
366
+ dimfrac: t.dimfrac,
367
+ dimlunit: t.dimlunit,
368
+ dimdsep: t.dimdsep.toString(),
369
+ dimtmove: t.dimtmove || 0,
370
+ dimjust: t.dimjust,
371
+ dimsd1: t.dimsd1,
372
+ dimsd2: t.dimsd2,
373
+ dimtolj: t.dimtolj,
374
+ dimtzin: t.dimtzin,
375
+ dimaltz: t.dimaltz,
376
+ dimalttz: t.dimaltttz,
377
+ dimfit: t.dimfit || 0,
378
+ dimupt: t.dimupt,
379
+ dimatfit: t.dimatfit,
380
+ dimtxsty: t.dimtxsty || "Standard",
381
+ dimldrblk: t.dimldrblk || "",
382
+ dimblk: t.dimblk || "",
383
+ dimblk1: t.dimblk1 || "",
384
+ dimblk2: t.dimblk2 || "",
385
+ dimlwd: t.dimlwd,
386
+ dimlwe: t.dimlwe
387
+ }, d = new K(o);
388
+ this.processCommonTableEntryAttrs(t, d), i.tables.dimStyleTable.add(d);
389
+ }
390
+ }
391
+ }
392
+ processLayers(e, i) {
393
+ const s = e.layers;
394
+ for (let n = 0, r = s.size(); n < r; ++n) {
395
+ const t = s.get(n);
396
+ if (t != null) {
397
+ const o = new J();
398
+ o.colorIndex = t.color;
399
+ const d = new Q({
400
+ name: t.name,
401
+ standardFlags: t.flags,
402
+ linetype: t.lineType,
403
+ lineWeight: t.lWeight,
404
+ isOff: t.color < 0,
405
+ color: o,
406
+ isPlottable: t.plotF,
407
+ materialId: t.handleMaterialS
408
+ });
409
+ this.processCommonTableEntryAttrs(t, d), i.tables.layerTable.add(d);
410
+ }
411
+ }
412
+ }
413
+ processViewports(e, i) {
414
+ const s = e.viewports;
415
+ for (let n = 0, r = s.size(); n < r; ++n) {
416
+ const t = s.get(n);
417
+ if (t != null) {
418
+ const o = new ee();
419
+ this.processCommonTableEntryAttrs(t, o), t.circleZoom && (o.circleSides = t.circleZoom), o.standardFlag = t.flags, o.center.copy(t.center), o.lowerLeftCorner.copy(t.lowerLeft), o.upperRightCorner.copy(t.upperRight), t.snapBase && o.snapBase.copy(t.snapBase), t.snapAngle && (o.snapAngle = t.snapAngle), o.snapIncrements && o.snapIncrements.copy(t.snapSpacing), t.gridSpacing && o.gridIncrements.copy(t.gridSpacing), o.gsView.center.copy(t.center), o.gsView.viewDirectionFromTarget.copy(t.viewDir), o.gsView.viewTarget.copy(t.viewTarget), t.lensHeight && (o.gsView.lensLength = t.lensHeight), t.height && (o.gsView.viewHeight = t.height), t.twistAngle && (o.gsView.viewTwistAngle = t.twistAngle), t.viewMode && (o.gsView.viewMode = t.viewMode), t.ucsIcon && (o.gsView.ucsIconSetting = t.ucsIcon), i.tables.viewportTable.add(o);
420
+ }
421
+ }
422
+ }
423
+ processBlockTables(e, i) {
424
+ const s = e.blocks;
425
+ for (let n = 0, r = s.size(); n < r; ++n) {
426
+ const t = s.get(n);
427
+ if (t != null) {
428
+ const o = new te();
429
+ o.objectId = t.handle.toString(), o.name = t.name.toUpperCase(), i.tables.blockTable.add(o), t.entities && this.processEntitiesInBlock(t.entities, o);
430
+ }
431
+ }
432
+ }
433
+ processCommonTableEntryAttrs(e, i) {
434
+ i.name = e.name, i.objectId = e.handle.toString(), i.ownerId = e.parentHandle.toString();
435
+ }
436
+ processHeader(e, i) {
437
+ const s = e.header;
438
+ let n = s.getVar("$CECOLOR");
439
+ i.cecolor.colorIndex = n ? n.getInt() : 256, n = s.getVar("$ANGDIR"), i.angDir = n ? n.getInt() : 0, n = s.getVar("$AUNITS"), i.aunits = n ? n.getInt() : 0, n = s.getVar("$INSUNITS"), i.insunits = n ? n.getInt() : 1, n = s.getVar("$PDMODE"), i.pdmode = n ? n.getInt() : 0, n = s.getVar("$PDSIZE"), i.pdsize = n ? n.getDouble() : 0;
440
+ }
441
+ processObjects(e, i) {
442
+ this.processImageDefs(e.images, i);
443
+ }
444
+ processImageDefs(e, i) {
445
+ const s = new ne(), n = i.dictionaries.imageDefs;
446
+ for (let r = 0, t = e.size(); r < t; ++r) {
447
+ const o = e.get(r);
448
+ if (o != null) {
449
+ const d = s.convertImageDef(o);
450
+ n.setAt(d.objectId, d);
451
+ }
452
+ }
453
+ }
454
+ async processEntitiesInBlock(e, i) {
455
+ const s = new v();
456
+ for (let n = 0, r = e.size(); n < r; ++n) {
457
+ const t = e.get(n);
458
+ if (t != null) {
459
+ const o = s.convert(t);
460
+ o && i.appendEntity(o);
461
+ }
462
+ }
463
+ }
464
+ async processEntities(e, i, s, n, r) {
465
+ if (e.mBlock) {
466
+ const t = new v(), o = e.mBlock.entities, d = o.size(), f = new ie(
467
+ d,
468
+ 100 - n.value,
469
+ s
470
+ ), c = i.tables.blockTable.modelSpace;
471
+ await f.processChunk(async (m, p) => {
472
+ for (let a = m; a < p; a++) {
473
+ const l = o.get(a);
474
+ if (l) {
475
+ const g = t.convert(l);
476
+ g && c.appendEntity(g);
477
+ }
478
+ }
479
+ if (r) {
480
+ let a = n.value + p / d * (100 - n.value);
481
+ a > 100 && (a = 100), await r(a, "ENTITY", "IN-PROGRESS");
482
+ }
483
+ });
484
+ }
485
+ }
486
+ }
487
+ export {
488
+ oe as AcDbLibdxfrwConverter
489
+ };
@@ -0,0 +1 @@
1
+ (function(g,r){typeof exports=="object"&&typeof module<"u"?r(exports,require("@mlightcad/data-model")):typeof define=="function"&&define.amd?define(["exports","@mlightcad/data-model"],r):(g=typeof globalThis<"u"?globalThis:g||self,r(g["libdxfrw-converter"]={},g.dataModel))})(this,function(g,r){"use strict";class A{convert(e){const i=this.createEntity(e);return i&&this.processCommonAttrs(e,i),i}createEntity(e){const i=window.libdxfrw;if(e.eType==i.DRW_ETYPE.ARC)return this.convertArc(e);if(e.eType==i.DRW_ETYPE.CIRCLE)return this.convertCirle(e);if(e.eType!=i.DRW_ETYPE.DIMENSION){if(e.eType==i.DRW_ETYPE.ELLIPSE)return this.convertEllipse(e);if(e.eType==i.DRW_ETYPE.HATCH)return this.convertHatch(e);if(e.eType!=i.DRW_ETYPE.IMAGE){if(e.eType!=i.DRW_ETYPE.LEADER){if(e.eType==i.DRW_ETYPE.LINE)return this.convertLine(e);if(e.eType==i.DRW_ETYPE.MTEXT)return this.convertMText(e);if(e.eType!=i.DRW_ETYPE.POLYLINE){if(e.eType==i.DRW_ETYPE.LWPOLYLINE)return this.convertPolyline(e);if(e.eType==i.DRW_ETYPE.POINT)return this.convertPoint(e);if(e.eType==i.DRW_ETYPE.RAY)return this.convertRay(e);if(e.eType==i.DRW_ETYPE.SPLINE)return this.convertSpline(e);if(e.eType==i.DRW_ETYPE.TEXT)return this.convertText(e);if(e.eType!=i.DRW_ETYPE.SOLID){if(e.eType!=i.DRW_ETYPE.VIEWPORT){if(e.eType==i.DRW_ETYPE.XLINE)return this.convertXline(e);if(e.eType==i.DRW_ETYPE.INSERT)return this.convertBlockReference(e)}}}}}}return null}convertArc(e){return new r.AcDbArc(e.center(),e.radius,e.startAngle,e.endAngle)}convertCirle(e){return new r.AcDbCircle(e.basePoint,e.radius)}convertEllipse(e){const i=new r.AcGeVector3d(e.secPoint),s=i.length();return new r.AcDbEllipse(e.basePoint,r.AcGeVector3d.Z_AXIS,i,s,s*e.ratio,e.startAngle,e.endAngle)}convertBlockReference(e){const i=new r.AcDbBlockReference(e.name);return e.basePoint&&i.position.copy(e.basePoint),i.scaleFactors.x=e.xScale||1,i.scaleFactors.y=e.yScale||1,i.scaleFactors.z=e.zScale||1,i.rotation=e.angle,i.normal.copy(e.extPoint),i}convertHatch(e){const i=new r.AcDbHatch,s=e.definitionLines;for(let l=0,t=s.size();l<t;++l){const o=s.get(l);o!=null&&i.definitionLines.push({angle:o.angle,origin:o.base,delta:o.offset,dashPattern:this.toNumberArray(o.dashPattern)})}i.hatchStyle=e.hatchStyle,i.patternName=e.name,i.patternType=e.patternType,i.patternAngle=e.angle,i.patternScale=e.scale;const n=e.getLoopList();for(let l=0,t=n.size();l<t;++l){const o=n.get(l);if(o!=null)if(o.type==2){const d=o.getObjList().get(0);if(d){const b=new r.AcGePolyline2d,m=d.getVertexList();for(let f=0,u=m.size();f<u;++f){const a=m.get(f);a!=null&&b.addVertexAt(f,{x:a.x,y:a.y,bulge:a.bulge})}i.add(b)}}else{const d=window.libdxfrw,b=o.getObjList(),m=new r.AcGeLoop2d;for(let f=0,u=b.size();f<u;++f){const a=b.get(f);if(a!=null){if(a.eType==d.DRW_ETYPE.LINE){const c=a;m.add(new r.AcGeLine2d(c.basePoint,c.secPoint))}else if(a.eType==d.DRW_ETYPE.ARC){const c=a;m.add(new r.AcGeCircArc2d(c.center(),c.radius,c.startAngle,c.endAngle,!c.isccw))}else if(a.eType==d.DRW_ETYPE.ELLIPSE){const c=a,p=c.basePoint,h=c.secPoint,P=new r.AcGeVector2d(h).length(),x=P*c.ratio;let w=c.startAngle,T=c.endAngle;const S=Math.atan2(h.y,h.x);c.isCounterClockwise||(w=Math.PI*2-w,T=Math.PI*2-T),m.add(new r.AcGeEllipseArc2d({x:p.x,y:p.y,z:0},P,x,w,T,!c.isCounterClockwise,S))}else if(a.eType==d.DRW_ETYPE.SPLINE){const c=a;if(c.numberOfControls>0&&c.numberOfKnots>0){const p=this.toPointArray(c.getControlList(),!0),h=this.toNumberArray(c.weights),E=this.toNumberArray(c.knots);m.add(new r.AcGeSpline3d(p,E,h.length>0?h:void 0))}else if(c.numberOfFits>0){const p=this.toPointArray(c.getFitList());m.add(new r.AcGeSpline3d(p,"Uniform"))}}}}i.add(m)}}return i}convertLine(e){const i=e.basePoint,s=e.secPoint;return new r.AcDbLine(new r.AcGePoint3d(i.x,i.y,i.z||0),new r.AcGePoint3d(s.x,s.y,s.z||0))}convertMText(e){const i=new r.AcDbMText;return i.contents=e.text,e.style!=null&&(i.styleName=e.style),i.height=e.height,i.lineSpacingFactor=e.interlin,i.rotation=e.angle||0,i.location=e.basePoint,i}convertPoint(e){const i=new r.AcDbPoint;return i.position=e.basePoint,i}convertPolyline(e){const i=new r.AcDbPolyline;i.closed=!!(e.flags&1);const s=e.getVertexList();for(let n=0,l=s.size();n<l;++n){const t=s.get(n);t!=null&&i.addVertexAt(n,new r.AcGePoint2d(t.x,t.y),t.bulge,t.startWidth,t.endWidth)}return i}convertRay(e){const i=new r.AcDbRay;return i.basePoint.copy(e.basePoint),i.unitDir.copy(e.secPoint),i}convertSpline(e){const i=this.toNumberArray(e.weights);if(e.numberOfControls>0&&e.numberOfKnots>0){const s=new r.AcDbSpline(this.toPointArray(e.getControlList(),!1),this.toNumberArray(e.knots),i.length>0?i:void 0);return s.closed=!!(e.flags&1),s}else if(e.numberOfFits>0){const s=this.toPointArray(e.getFitList());if(s.length>0){const n=new r.AcDbSpline(s,"Uniform");return n.closed=!!(e.flags&1),n}}return null}convertText(e){const i=new r.AcDbText;return i.textString=e.text,i.styleName=e.style,i.height=e.height,i.position.copy(e.basePoint),i.rotation=e.angle||0,i.oblique=e.oblique??0,i.horizontalMode=e.alignH,i.verticalMode=e.alignV,i.widthFactor=e.widthScale??1,i}convertXline(e){const i=new r.AcDbXline;return i.basePoint.copy(e.basePoint),i.unitDir.copy(e.secPoint),i}processCommonAttrs(e,i){i.layer=e.layer,i.objectId=e.handle.toString(),i.ownerId=e.parentHandle.toString(),e.lineType!=null&&(i.lineType=e.lineType),e.lWeight!=null&&(i.lineWeight=e.lWeight),e.ltypeScale!=null&&(i.linetypeScale=e.ltypeScale),e.color24!=null&&(i.color.color=e.color24),e.color!=null&&(i.color.colorIndex=e.color),e.colorName!=null&&(i.color.colorName=e.colorName),e.visible!=null&&(i.visibility=e.visible),e.transparency!=null&&(i.transparency=e.transparency)}toNumberArray(e){const i=[];for(let s=0,n=e.size();s<n;++s){const l=e.get(s);l!=null&&i.push(l)}return i}toPointArray(e,i=!0){const s=[];for(let n=0,l=e.size();n<l;++n){const t=e.get(n);t!=null&&s.push({x:t.x,y:t.y,z:i?0:t.z})}return s}}class D{convertImageDef(e){const i=new r.AcDbRasterImageDef;return i.sourceFileName=e.path,i.objectId=e.handle.toString(),i.ownerId=e.parentHandle.toString(),i}}class v extends r.AcDbDatabaseConverter{constructor(e){super(),this.librefrw=e}onFinished(){super.onFinished(),this.dwg&&(this.dwg.delete(),this.dwg=void 0),this.database&&(this.database.delete(),this.database=void 0),this.fileHandler&&(this.fileHandler.delete(),this.fileHandler=void 0)}parse(e){if(this.librefrw==null)throw new Error("librefrw is not loaded!");const i=this.librefrw;return this.database=new i.DRW_Database,this.fileHandler=new i.DRW_FileHandler,this.fileHandler.database=this.database,this.dwg=new i.DRW_DwgR(e),this.dwg.read(this.fileHandler,!1),this.database}processLineTypes(e,i){const s=e.lineTypes;for(let n=0,l=s.size();n<l;++n){const t=s.get(n);if(t!=null){const o={name:t.name,description:t.desc,standardFlag:t.flags,totalPatternLength:t.length,pattern:this.convertLineTypePattern(t.path)},d=new r.AcDbLinetypeTableRecord(o);this.processCommonTableEntryAttrs(t,d),d.name=t.name,i.tables.linetypeTable.add(d)}}}convertLineTypePattern(e){const i=[];for(let s=0,n=e.size();s<n;++s)i.push({elementLength:e.get(s)||0,elementTypeFlag:0});return i}processTextStyles(e,i){const s=e.textStyles;for(let n=0,l=s.size();n<l;++n){const t=s.get(n);if(t!=null){const o={name:t.name,standardFlag:t.flags,fixedTextHeight:t.height,widthFactor:t.width,obliqueAngle:t.oblique,textGenerationFlag:t.genFlag,lastHeight:t.lastHeight,font:t.font,bigFont:t.bigFont},d=new r.AcDbTextStyleTableRecord(o);this.processCommonTableEntryAttrs(t,d),i.tables.textStyleTable.add(d)}}}processDimStyles(e,i){const s=e.dimStyles;for(let n=0,l=s.size();n<l;++n){const t=s.get(n);if(t!=null){const o={name:t.name,ownerId:t.parentHandle.toString(),dimpost:t.dimpost||"",dimapost:t.dimapost||"",dimscale:t.dimscale,dimasz:t.dimasz,dimexo:t.dimexo,dimdli:t.dimdli,dimexe:t.dimexe,dimrnd:t.dimrnd,dimdle:t.dimdle,dimtp:t.dimtp,dimtm:t.dimtm,dimtxt:t.dimtxt,dimcen:t.dimcen,dimtsz:t.dimtsz,dimaltf:t.dimaltf,dimlfac:t.dimlfac,dimtvp:t.dimtvp,dimtfac:t.dimtfac,dimgap:t.dimgap,dimaltrnd:t.dimaltrnd,dimtol:t.dimtol==null||t.dimtol==0?0:1,dimlim:t.dimlim==null||t.dimlim==0?0:1,dimtih:t.dimtih==null||t.dimtih==0?0:1,dimtoh:t.dimtoh==null||t.dimtoh==0?0:1,dimse1:t.dimse1==null||t.dimse1==0?0:1,dimse2:t.dimse2==null||t.dimse2==0?0:1,dimtad:t.dimtad,dimzin:t.dimzin,dimazin:t.dimazin,dimalt:t.dimalt,dimaltd:t.dimaltd,dimtofl:t.dimtofl,dimsah:t.dimsah,dimtix:t.dimtix,dimsoxd:t.dimsoxd,dimclrd:t.dimclrd,dimclre:t.dimclre,dimclrt:t.dimclrt,dimadec:t.dimadec||0,dimunit:t.dimunit||2,dimdec:t.dimdec,dimtdec:t.dimtdec,dimaltu:t.dimaltu,dimalttd:t.dimalttd,dimaunit:t.dimaunit,dimfrac:t.dimfrac,dimlunit:t.dimlunit,dimdsep:t.dimdsep.toString(),dimtmove:t.dimtmove||0,dimjust:t.dimjust,dimsd1:t.dimsd1,dimsd2:t.dimsd2,dimtolj:t.dimtolj,dimtzin:t.dimtzin,dimaltz:t.dimaltz,dimalttz:t.dimaltttz,dimfit:t.dimfit||0,dimupt:t.dimupt,dimatfit:t.dimatfit,dimtxsty:t.dimtxsty||"Standard",dimldrblk:t.dimldrblk||"",dimblk:t.dimblk||"",dimblk1:t.dimblk1||"",dimblk2:t.dimblk2||"",dimlwd:t.dimlwd,dimlwe:t.dimlwe},d=new r.AcDbDimStyleTableRecord(o);this.processCommonTableEntryAttrs(t,d),i.tables.dimStyleTable.add(d)}}}processLayers(e,i){const s=e.layers;for(let n=0,l=s.size();n<l;++n){const t=s.get(n);if(t!=null){const o=new r.AcCmColor;o.colorIndex=t.color;const d=new r.AcDbLayerTableRecord({name:t.name,standardFlags:t.flags,linetype:t.lineType,lineWeight:t.lWeight,isOff:t.color<0,color:o,isPlottable:t.plotF,materialId:t.handleMaterialS});this.processCommonTableEntryAttrs(t,d),i.tables.layerTable.add(d)}}}processViewports(e,i){const s=e.viewports;for(let n=0,l=s.size();n<l;++n){const t=s.get(n);if(t!=null){const o=new r.AcDbViewportTableRecord;this.processCommonTableEntryAttrs(t,o),t.circleZoom&&(o.circleSides=t.circleZoom),o.standardFlag=t.flags,o.center.copy(t.center),o.lowerLeftCorner.copy(t.lowerLeft),o.upperRightCorner.copy(t.upperRight),t.snapBase&&o.snapBase.copy(t.snapBase),t.snapAngle&&(o.snapAngle=t.snapAngle),o.snapIncrements&&o.snapIncrements.copy(t.snapSpacing),t.gridSpacing&&o.gridIncrements.copy(t.gridSpacing),o.gsView.center.copy(t.center),o.gsView.viewDirectionFromTarget.copy(t.viewDir),o.gsView.viewTarget.copy(t.viewTarget),t.lensHeight&&(o.gsView.lensLength=t.lensHeight),t.height&&(o.gsView.viewHeight=t.height),t.twistAngle&&(o.gsView.viewTwistAngle=t.twistAngle),t.viewMode&&(o.gsView.viewMode=t.viewMode),t.ucsIcon&&(o.gsView.ucsIconSetting=t.ucsIcon),i.tables.viewportTable.add(o)}}}processBlockTables(e,i){const s=e.blocks;for(let n=0,l=s.size();n<l;++n){const t=s.get(n);if(t!=null){const o=new r.AcDbBlockTableRecord;o.objectId=t.handle.toString(),o.name=t.name.toUpperCase(),i.tables.blockTable.add(o),t.entities&&this.processEntitiesInBlock(t.entities,o)}}}processCommonTableEntryAttrs(e,i){i.name=e.name,i.objectId=e.handle.toString(),i.ownerId=e.parentHandle.toString()}processHeader(e,i){const s=e.header;let n=s.getVar("$CECOLOR");i.cecolor.colorIndex=n?n.getInt():256,n=s.getVar("$ANGDIR"),i.angDir=n?n.getInt():0,n=s.getVar("$AUNITS"),i.aunits=n?n.getInt():0,n=s.getVar("$INSUNITS"),i.insunits=n?n.getInt():1,n=s.getVar("$PDMODE"),i.pdmode=n?n.getInt():0,n=s.getVar("$PDSIZE"),i.pdsize=n?n.getDouble():0}processObjects(e,i){this.processImageDefs(e.images,i)}processImageDefs(e,i){const s=new D,n=i.dictionaries.imageDefs;for(let l=0,t=e.size();l<t;++l){const o=e.get(l);if(o!=null){const d=s.convertImageDef(o);n.setAt(d.objectId,d)}}}async processEntitiesInBlock(e,i){const s=new A;for(let n=0,l=e.size();n<l;++n){const t=e.get(n);if(t!=null){const o=s.convert(t);o&&i.appendEntity(o)}}}async processEntities(e,i,s,n,l){if(e.mBlock){const t=new A,o=e.mBlock.entities,d=o.size(),b=new r.AcDbBatchProcessing(d,100-n.value,s),m=i.tables.blockTable.modelSpace;await b.processChunk(async(f,u)=>{for(let a=f;a<u;a++){const c=o.get(a);if(c){const p=t.convert(c);p&&m.appendEntity(p)}}if(l){let a=n.value+u/d*(100-n.value);a>100&&(a=100),await l(a,"ENTITY","IN-PROGRESS")}})}}}g.AcDbLibdxfrwConverter=v,Object.defineProperty(g,Symbol.toStringTag,{value:"Module"})});
@@ -0,0 +1,23 @@
1
+ import { AcDbEntity } from '@mlightcad/data-model';
2
+ import { DRW_Entity } from '@mlightcad/libdxfrw-web';
3
+ export declare class AcDbEntityConverter {
4
+ convert(entity: DRW_Entity): AcDbEntity | null;
5
+ private createEntity;
6
+ private convertArc;
7
+ private convertCirle;
8
+ private convertEllipse;
9
+ private convertBlockReference;
10
+ private convertHatch;
11
+ private convertLine;
12
+ private convertMText;
13
+ private convertPoint;
14
+ private convertPolyline;
15
+ private convertRay;
16
+ private convertSpline;
17
+ private convertText;
18
+ private convertXline;
19
+ private processCommonAttrs;
20
+ private toNumberArray;
21
+ private toPointArray;
22
+ }
23
+ //# sourceMappingURL=AcDbEntitiyConverter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AcDbEntitiyConverter.d.ts","sourceRoot":"","sources":["../src/AcDbEntitiyConverter.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,UAAU,EAyBX,MAAM,uBAAuB,CAAA;AAC9B,OAAO,EAML,UAAU,EAYX,MAAM,yBAAyB,CAAA;AAEhC,qBAAa,mBAAmB;IAC9B,OAAO,CAAC,MAAM,EAAE,UAAU,GAAG,UAAU,GAAG,IAAI;IAQ9C,OAAO,CAAC,YAAY;IA8CpB,OAAO,CAAC,UAAU;IAUlB,OAAO,CAAC,YAAY;IAKpB,OAAO,CAAC,cAAc;IAetB,OAAO,CAAC,qBAAqB;IAY7B,OAAO,CAAC,YAAY;IA6HpB,OAAO,CAAC,WAAW;IAUnB,OAAO,CAAC,YAAY;IAqBpB,OAAO,CAAC,YAAY;IAMpB,OAAO,CAAC,eAAe;IA8BvB,OAAO,CAAC,UAAU;IAOlB,OAAO,CAAC,aAAa;IAqBrB,OAAO,CAAC,WAAW;IAenB,OAAO,CAAC,YAAY;IAOpB,OAAO,CAAC,kBAAkB;IA8B1B,OAAO,CAAC,aAAa;IAWrB,OAAO,CAAC,YAAY;CAUrB"}