@mlightcad/libdxfrw-converter 3.1.3 → 3.1.5

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.
@@ -0,0 +1,528 @@
1
+ import { AcDbArc as S, AcDbCircle as z, AcGeVector3d as A, AcDbEllipse as R, AcDbBlockReference as L, AcDbHatch as C, AcGePolyline2d as W, AcGeLoop2d as N, AcGeLine2d as F, AcGeCircArc2d as Y, AcGeVector2d as _, AcGeEllipseArc2d as k, AcGeSpline3d as E, AcDbLine as H, AcGePoint3d as P, AcDbMText as V, AcDbPoint as j, AcDbPolyline as O, AcGePoint2d as B, AcDbRay as M, AcDbSpline as v, 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 D {
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 A(e.secPoint), o = i.length();
62
+ return new R(
63
+ e.basePoint,
64
+ A.Z_AXIS,
65
+ i,
66
+ o,
67
+ o * 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(), o = e.definitionLines;
78
+ for (let r = 0, t = o.size(); r < t; ++r) {
79
+ const s = o.get(r);
80
+ s != null && i.definitionLines.push({
81
+ angle: s.angle,
82
+ origin: s.base,
83
+ delta: s.offset,
84
+ dashPattern: this.toNumberArray(s.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 s = n.get(r);
91
+ if (s != null)
92
+ if (s.type == 2) {
93
+ const l = s.getObjList().get(0);
94
+ if (l) {
95
+ const f = new W(), m = l.getVertexList();
96
+ for (let g = 0, p = m.size(); g < p; ++g) {
97
+ const a = m.get(g);
98
+ a != null && f.addVertexAt(g, {
99
+ x: a.x,
100
+ y: a.y,
101
+ bulge: a.bulge
102
+ });
103
+ }
104
+ i.add(f);
105
+ }
106
+ } else {
107
+ const l = window.libdxfrw, f = s.getObjList(), m = new N();
108
+ for (let g = 0, p = f.size(); g < p; ++g) {
109
+ const a = f.get(g);
110
+ if (a != null) {
111
+ if (a.eType == l.DRW_ETYPE.LINE) {
112
+ const d = a;
113
+ m.add(new F(d.basePoint, d.secPoint));
114
+ } else if (a.eType == l.DRW_ETYPE.ARC) {
115
+ const d = a;
116
+ m.add(
117
+ new Y(
118
+ d.center(),
119
+ d.radius,
120
+ d.startAngle,
121
+ d.endAngle,
122
+ !d.isccw
123
+ )
124
+ );
125
+ } else if (a.eType == l.DRW_ETYPE.ELLIPSE) {
126
+ const d = a, c = d.basePoint, u = d.secPoint, y = new _(u).length(), x = y * d.ratio;
127
+ let h = d.startAngle, w = d.endAngle;
128
+ const I = Math.atan2(u.y, u.x);
129
+ d.isCounterClockwise || (h = Math.PI * 2 - h, w = Math.PI * 2 - w), m.add(
130
+ new k(
131
+ { x: c.x, y: c.y, z: 0 },
132
+ y,
133
+ x,
134
+ h,
135
+ w,
136
+ !d.isCounterClockwise,
137
+ I
138
+ )
139
+ );
140
+ } else if (a.eType == l.DRW_ETYPE.SPLINE) {
141
+ const d = a;
142
+ if (d.numberOfControls > 0 && d.numberOfKnots > 0) {
143
+ const c = this.toPointArray(
144
+ d.getControlList(),
145
+ !0
146
+ ), u = this.toNumberArray(d.weights), b = this.toNumberArray(d.knots);
147
+ m.add(
148
+ new E(
149
+ c,
150
+ b,
151
+ u.length > 0 ? u : void 0
152
+ )
153
+ );
154
+ } else if (d.numberOfFits > 0) {
155
+ const c = this.toPointArray(
156
+ d.getFitList()
157
+ );
158
+ m.add(new E(c, "Uniform"));
159
+ }
160
+ }
161
+ }
162
+ }
163
+ i.add(m);
164
+ }
165
+ }
166
+ return i;
167
+ }
168
+ convertLine(e) {
169
+ const i = e.basePoint, o = e.secPoint;
170
+ return new H(
171
+ new P(i.x, i.y, i.z || 0),
172
+ new P(o.x, o.y, o.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 o = e.getVertexList();
187
+ for (let n = 0, r = o.size(); n < r; ++n) {
188
+ const t = o.get(n);
189
+ t != null && i.addVertexAt(
190
+ n,
191
+ new B(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 M();
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
+ return new v(
207
+ this.toPointArray(e.getControlList(), !1),
208
+ this.toNumberArray(e.knots),
209
+ i.length > 0 ? i : void 0,
210
+ e.degree,
211
+ // Default degree
212
+ !!(e.flags & 1)
213
+ );
214
+ if (e.numberOfFits > 0) {
215
+ const o = this.toPointArray(e.getFitList());
216
+ if (o.length > 0)
217
+ return new v(
218
+ o,
219
+ "Uniform",
220
+ e.degree,
221
+ !!(e.flags & 1)
222
+ );
223
+ }
224
+ return null;
225
+ }
226
+ convertText(e) {
227
+ const i = new G();
228
+ 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;
229
+ }
230
+ convertXline(e) {
231
+ const i = new X();
232
+ return i.basePoint.copy(e.basePoint), i.unitDir.copy(e.secPoint), i;
233
+ }
234
+ processCommonAttrs(e, i) {
235
+ 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);
236
+ }
237
+ toNumberArray(e) {
238
+ const i = [];
239
+ for (let o = 0, n = e.size(); o < n; ++o) {
240
+ const r = e.get(o);
241
+ r != null && i.push(r);
242
+ }
243
+ return i;
244
+ }
245
+ toPointArray(e, i = !0) {
246
+ const o = [];
247
+ for (let n = 0, r = e.size(); n < r; ++n) {
248
+ const t = e.get(n);
249
+ t != null && o.push({ x: t.x, y: t.y, z: i ? 0 : t.z });
250
+ }
251
+ return o;
252
+ }
253
+ }
254
+ class ne {
255
+ convertImageDef(e) {
256
+ const i = new $();
257
+ return i.sourceFileName = e.path, i.objectId = e.handle.toString(), i.ownerId = e.parentHandle.toString(), i;
258
+ }
259
+ }
260
+ class oe extends U {
261
+ constructor(e) {
262
+ super(), this.librefrw = e;
263
+ }
264
+ onFinished() {
265
+ 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);
266
+ }
267
+ async parse(e) {
268
+ if (this.librefrw == null)
269
+ throw new Error("librefrw is not loaded!");
270
+ const i = this.librefrw;
271
+ 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), {
272
+ model: this.database,
273
+ data: {
274
+ unknownEntityCount: 0
275
+ }
276
+ };
277
+ }
278
+ processLineTypes(e, i) {
279
+ const o = e.lineTypes;
280
+ for (let n = 0, r = o.size(); n < r; ++n) {
281
+ const t = o.get(n);
282
+ if (t != null) {
283
+ const s = {
284
+ name: t.name,
285
+ description: t.desc,
286
+ standardFlag: t.flags,
287
+ totalPatternLength: t.length,
288
+ pattern: this.convertLineTypePattern(t.path)
289
+ }, l = new q(s);
290
+ this.processCommonTableEntryAttrs(t, l), l.name = t.name, i.tables.linetypeTable.add(l);
291
+ }
292
+ }
293
+ }
294
+ convertLineTypePattern(e) {
295
+ const i = [];
296
+ for (let o = 0, n = e.size(); o < n; ++o)
297
+ i.push({
298
+ elementLength: e.get(o) || 0,
299
+ elementTypeFlag: 0
300
+ });
301
+ return i;
302
+ }
303
+ processTextStyles(e, i) {
304
+ const o = e.textStyles;
305
+ for (let n = 0, r = o.size(); n < r; ++n) {
306
+ const t = o.get(n);
307
+ if (t != null) {
308
+ const s = {
309
+ name: t.name,
310
+ standardFlag: t.flags,
311
+ fixedTextHeight: t.height,
312
+ widthFactor: t.width,
313
+ obliqueAngle: t.oblique,
314
+ textGenerationFlag: t.genFlag,
315
+ lastHeight: t.lastHeight,
316
+ font: t.font,
317
+ bigFont: t.bigFont
318
+ }, l = new Z(s);
319
+ this.processCommonTableEntryAttrs(t, l), i.tables.textStyleTable.add(l);
320
+ }
321
+ }
322
+ }
323
+ processDimStyles(e, i) {
324
+ const o = e.dimStyles;
325
+ for (let n = 0, r = o.size(); n < r; ++n) {
326
+ const t = o.get(n);
327
+ if (t != null) {
328
+ const s = {
329
+ name: t.name,
330
+ ownerId: t.parentHandle.toString(),
331
+ dimpost: t.dimpost || "",
332
+ dimapost: t.dimapost || "",
333
+ dimscale: t.dimscale,
334
+ dimasz: t.dimasz,
335
+ dimexo: t.dimexo,
336
+ dimdli: t.dimdli,
337
+ dimexe: t.dimexe,
338
+ dimrnd: t.dimrnd,
339
+ dimdle: t.dimdle,
340
+ dimtp: t.dimtp,
341
+ dimtm: t.dimtm,
342
+ dimtxt: t.dimtxt,
343
+ dimcen: t.dimcen,
344
+ dimtsz: t.dimtsz,
345
+ dimaltf: t.dimaltf,
346
+ dimlfac: t.dimlfac,
347
+ dimtvp: t.dimtvp,
348
+ dimtfac: t.dimtfac,
349
+ dimgap: t.dimgap,
350
+ dimaltrnd: t.dimaltrnd,
351
+ dimtol: t.dimtol == null || t.dimtol == 0 ? 0 : 1,
352
+ dimlim: t.dimlim == null || t.dimlim == 0 ? 0 : 1,
353
+ dimtih: t.dimtih == null || t.dimtih == 0 ? 0 : 1,
354
+ dimtoh: t.dimtoh == null || t.dimtoh == 0 ? 0 : 1,
355
+ dimse1: t.dimse1 == null || t.dimse1 == 0 ? 0 : 1,
356
+ dimse2: t.dimse2 == null || t.dimse2 == 0 ? 0 : 1,
357
+ dimtad: t.dimtad,
358
+ dimzin: t.dimzin,
359
+ dimazin: t.dimazin,
360
+ dimalt: t.dimalt,
361
+ dimaltd: t.dimaltd,
362
+ dimtofl: t.dimtofl,
363
+ dimsah: t.dimsah,
364
+ dimtix: t.dimtix,
365
+ dimsoxd: t.dimsoxd,
366
+ dimclrd: t.dimclrd,
367
+ dimclre: t.dimclre,
368
+ dimclrt: t.dimclrt,
369
+ dimadec: t.dimadec || 0,
370
+ dimunit: t.dimunit || 2,
371
+ dimdec: t.dimdec,
372
+ dimtdec: t.dimtdec,
373
+ dimaltu: t.dimaltu,
374
+ dimalttd: t.dimalttd,
375
+ dimaunit: t.dimaunit,
376
+ dimfrac: t.dimfrac,
377
+ dimlunit: t.dimlunit,
378
+ dimdsep: t.dimdsep.toString(),
379
+ dimtmove: t.dimtmove || 0,
380
+ dimjust: t.dimjust,
381
+ dimsd1: t.dimsd1,
382
+ dimsd2: t.dimsd2,
383
+ dimtolj: t.dimtolj,
384
+ dimtzin: t.dimtzin,
385
+ dimaltz: t.dimaltz,
386
+ dimalttz: t.dimaltttz,
387
+ dimfit: t.dimfit || 0,
388
+ dimupt: t.dimupt,
389
+ dimatfit: t.dimatfit,
390
+ dimtxsty: t.dimtxsty || "Standard",
391
+ dimldrblk: t.dimldrblk || "",
392
+ dimblk: t.dimblk || "",
393
+ dimblk1: t.dimblk1 || "",
394
+ dimblk2: t.dimblk2 || "",
395
+ dimlwd: t.dimlwd,
396
+ dimlwe: t.dimlwe
397
+ }, l = new K(s);
398
+ this.processCommonTableEntryAttrs(t, l), i.tables.dimStyleTable.add(l);
399
+ }
400
+ }
401
+ }
402
+ processLayers(e, i) {
403
+ const o = e.layers;
404
+ for (let n = 0, r = o.size(); n < r; ++n) {
405
+ const t = o.get(n);
406
+ if (t != null) {
407
+ const s = new J();
408
+ s.colorIndex = t.color;
409
+ const l = new Q({
410
+ name: t.name,
411
+ standardFlags: t.flags,
412
+ linetype: t.lineType,
413
+ lineWeight: t.lWeight,
414
+ isOff: t.color < 0,
415
+ color: s,
416
+ isPlottable: t.plotF,
417
+ materialId: t.handleMaterialS
418
+ });
419
+ this.processCommonTableEntryAttrs(t, l), i.tables.layerTable.add(l);
420
+ }
421
+ }
422
+ }
423
+ processViewports(e, i) {
424
+ const o = e.viewports;
425
+ for (let n = 0, r = o.size(); n < r; ++n) {
426
+ const t = o.get(n);
427
+ if (t != null) {
428
+ const s = new ee();
429
+ this.processCommonTableEntryAttrs(t, s), t.circleZoom && (s.circleSides = t.circleZoom), s.standardFlag = t.flags, s.center.copy(t.center), s.lowerLeftCorner.copy(t.lowerLeft), s.upperRightCorner.copy(t.upperRight), t.snapBase && s.snapBase.copy(t.snapBase), t.snapAngle && (s.snapAngle = t.snapAngle), s.snapIncrements && s.snapIncrements.copy(t.snapSpacing), t.gridSpacing && s.gridIncrements.copy(t.gridSpacing), s.gsView.center.copy(t.center), s.gsView.viewDirectionFromTarget.copy(t.viewDir), s.gsView.viewTarget.copy(t.viewTarget), t.lensHeight && (s.gsView.lensLength = t.lensHeight), t.height && (s.gsView.viewHeight = t.height), t.twistAngle && (s.gsView.viewTwistAngle = t.twistAngle), t.viewMode && (s.gsView.viewMode = t.viewMode), t.ucsIcon && (s.gsView.ucsIconSetting = t.ucsIcon), i.tables.viewportTable.add(s);
430
+ }
431
+ }
432
+ }
433
+ processBlockTables(e, i) {
434
+ const o = e.blocks;
435
+ for (let n = 0, r = o.size(); n < r; ++n) {
436
+ const t = o.get(n);
437
+ if (t != null) {
438
+ const s = new te();
439
+ s.objectId = t.handle.toString(), s.name = t.name.toUpperCase(), i.tables.blockTable.add(s), t.entities && this.processEntitiesInBlock(t.entities, s);
440
+ }
441
+ }
442
+ }
443
+ processCommonTableEntryAttrs(e, i) {
444
+ i.name = e.name, i.objectId = e.handle.toString(), i.ownerId = e.parentHandle.toString();
445
+ }
446
+ processHeader(e, i) {
447
+ const o = e.header;
448
+ let n = o.getVar("$CECOLOR");
449
+ i.cecolor.colorIndex = n ? n.getInt() : 256, n = o.getVar("$ANGDIR"), i.angDir = n ? n.getInt() : 0, n = o.getVar("$AUNITS"), i.aunits = n ? n.getInt() : 0, n = o.getVar("$INSUNITS"), i.insunits = n ? n.getInt() : 1, n = o.getVar("$PDMODE"), i.pdmode = n ? n.getInt() : 0, n = o.getVar("$PDSIZE"), i.pdsize = n ? n.getDouble() : 0;
450
+ }
451
+ processObjects(e, i) {
452
+ this.processImageDefs(e.images, i);
453
+ }
454
+ processImageDefs(e, i) {
455
+ const o = new ne(), n = i.dictionaries.imageDefs;
456
+ for (let r = 0, t = e.size(); r < t; ++r) {
457
+ const s = e.get(r);
458
+ if (s != null) {
459
+ const l = o.convertImageDef(s);
460
+ n.setAt(l.objectId, l);
461
+ }
462
+ }
463
+ }
464
+ async processEntitiesInBlock(e, i) {
465
+ const o = new D(), n = [];
466
+ for (let r = 0, t = e.size(); r < t; ++r) {
467
+ const s = e.get(r);
468
+ if (s != null) {
469
+ const l = o.convert(s);
470
+ l && n.push(l);
471
+ }
472
+ }
473
+ i.appendEntity(n);
474
+ }
475
+ async processEntities(e, i, o, n, r) {
476
+ if (e.mBlock) {
477
+ const t = new D();
478
+ let s = [];
479
+ const l = e.mBlock.entities, f = l.size();
480
+ for (let p = 0; p < f; p++) {
481
+ const a = l.get(p);
482
+ a && s.push(a);
483
+ }
484
+ const m = new ie(
485
+ s.length,
486
+ 100 - n.value,
487
+ o
488
+ );
489
+ this.config.convertByEntityType && (s = this.groupAndFlattenByType(s));
490
+ const g = i.tables.blockTable.modelSpace;
491
+ await m.processChunk(async (p, a) => {
492
+ const d = [];
493
+ for (let c = p; c < a; c++) {
494
+ const u = s[c], b = t.convert(u);
495
+ b && d.push(b);
496
+ }
497
+ if (g.appendEntity(d), r) {
498
+ let c = n.value + a / f * (100 - n.value);
499
+ c > 100 && (c = 100), await r(c, "ENTITY", "IN-PROGRESS");
500
+ }
501
+ });
502
+ }
503
+ }
504
+ /**
505
+ * Groups entities by their `type` property and flattens the result into a single array.
506
+ *
507
+ * The order of `type` groups follows the order in which they first appear in the input array.
508
+ * Items within each group preserve their original order.
509
+ *
510
+ * This runs in O(n) time, which is generally faster than sorting when you
511
+ * don't care about alphabetical order of types.
512
+ *
513
+ * @param entities - The array of entities to group and flatten.
514
+ *
515
+ * @returns A new array of entities grouped by their `type` property.
516
+ */
517
+ groupAndFlattenByType(e) {
518
+ const i = {}, o = [], n = e.length;
519
+ for (let r = 0; r < n; r++) {
520
+ const t = e[r], s = t.eType.value;
521
+ i[s] || (i[s] = [], o.push(s)), i[s].push(t);
522
+ }
523
+ return o.flatMap((r) => i[r]);
524
+ }
525
+ }
526
+ export {
527
+ oe as AcDbLibdxfrwConverter
528
+ };
@@ -0,0 +1 @@
1
+ (function(h,l){typeof exports=="object"&&typeof module<"u"?l(exports,require("@mlightcad/data-model")):typeof define=="function"&&define.amd?define(["exports","@mlightcad/data-model"],l):(h=typeof globalThis<"u"?globalThis:h||self,l(h["libdxfrw-converter"]={},h.dataModel))})(this,function(h,l){"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 l.AcDbArc(e.center(),e.radius,e.startAngle,e.endAngle)}convertCirle(e){return new l.AcDbCircle(e.basePoint,e.radius)}convertEllipse(e){const i=new l.AcGeVector3d(e.secPoint),o=i.length();return new l.AcDbEllipse(e.basePoint,l.AcGeVector3d.Z_AXIS,i,o,o*e.ratio,e.startAngle,e.endAngle)}convertBlockReference(e){const i=new l.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 l.AcDbHatch,o=e.definitionLines;for(let r=0,t=o.size();r<t;++r){const s=o.get(r);s!=null&&i.definitionLines.push({angle:s.angle,origin:s.base,delta:s.offset,dashPattern:this.toNumberArray(s.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 r=0,t=n.size();r<t;++r){const s=n.get(r);if(s!=null)if(s.type==2){const c=s.getObjList().get(0);if(c){const u=new l.AcGePolyline2d,f=c.getVertexList();for(let p=0,g=f.size();p<g;++p){const a=f.get(p);a!=null&&u.addVertexAt(p,{x:a.x,y:a.y,bulge:a.bulge})}i.add(u)}}else{const c=window.libdxfrw,u=s.getObjList(),f=new l.AcGeLoop2d;for(let p=0,g=u.size();p<g;++p){const a=u.get(p);if(a!=null){if(a.eType==c.DRW_ETYPE.LINE){const d=a;f.add(new l.AcGeLine2d(d.basePoint,d.secPoint))}else if(a.eType==c.DRW_ETYPE.ARC){const d=a;f.add(new l.AcGeCircArc2d(d.center(),d.radius,d.startAngle,d.endAngle,!d.isccw))}else if(a.eType==c.DRW_ETYPE.ELLIPSE){const d=a,m=d.basePoint,b=d.secPoint,P=new l.AcGeVector2d(b).length(),x=P*d.ratio;let T=d.startAngle,y=d.endAngle;const S=Math.atan2(b.y,b.x);d.isCounterClockwise||(T=Math.PI*2-T,y=Math.PI*2-y),f.add(new l.AcGeEllipseArc2d({x:m.x,y:m.y,z:0},P,x,T,y,!d.isCounterClockwise,S))}else if(a.eType==c.DRW_ETYPE.SPLINE){const d=a;if(d.numberOfControls>0&&d.numberOfKnots>0){const m=this.toPointArray(d.getControlList(),!0),b=this.toNumberArray(d.weights),w=this.toNumberArray(d.knots);f.add(new l.AcGeSpline3d(m,w,b.length>0?b:void 0))}else if(d.numberOfFits>0){const m=this.toPointArray(d.getFitList());f.add(new l.AcGeSpline3d(m,"Uniform"))}}}}i.add(f)}}return i}convertLine(e){const i=e.basePoint,o=e.secPoint;return new l.AcDbLine(new l.AcGePoint3d(i.x,i.y,i.z||0),new l.AcGePoint3d(o.x,o.y,o.z||0))}convertMText(e){const i=new l.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 l.AcDbPoint;return i.position=e.basePoint,i}convertPolyline(e){const i=new l.AcDbPolyline;i.closed=!!(e.flags&1);const o=e.getVertexList();for(let n=0,r=o.size();n<r;++n){const t=o.get(n);t!=null&&i.addVertexAt(n,new l.AcGePoint2d(t.x,t.y),t.bulge,t.startWidth,t.endWidth)}return i}convertRay(e){const i=new l.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)return new l.AcDbSpline(this.toPointArray(e.getControlList(),!1),this.toNumberArray(e.knots),i.length>0?i:void 0,e.degree,!!(e.flags&1));if(e.numberOfFits>0){const o=this.toPointArray(e.getFitList());if(o.length>0)return new l.AcDbSpline(o,"Uniform",e.degree,!!(e.flags&1))}return null}convertText(e){const i=new l.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 l.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 o=0,n=e.size();o<n;++o){const r=e.get(o);r!=null&&i.push(r)}return i}toPointArray(e,i=!0){const o=[];for(let n=0,r=e.size();n<r;++n){const t=e.get(n);t!=null&&o.push({x:t.x,y:t.y,z:i?0:t.z})}return o}}class v{convertImageDef(e){const i=new l.AcDbRasterImageDef;return i.sourceFileName=e.path,i.objectId=e.handle.toString(),i.ownerId=e.parentHandle.toString(),i}}class D extends l.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)}async 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),{model:this.database,data:{unknownEntityCount:0}}}processLineTypes(e,i){const o=e.lineTypes;for(let n=0,r=o.size();n<r;++n){const t=o.get(n);if(t!=null){const s={name:t.name,description:t.desc,standardFlag:t.flags,totalPatternLength:t.length,pattern:this.convertLineTypePattern(t.path)},c=new l.AcDbLinetypeTableRecord(s);this.processCommonTableEntryAttrs(t,c),c.name=t.name,i.tables.linetypeTable.add(c)}}}convertLineTypePattern(e){const i=[];for(let o=0,n=e.size();o<n;++o)i.push({elementLength:e.get(o)||0,elementTypeFlag:0});return i}processTextStyles(e,i){const o=e.textStyles;for(let n=0,r=o.size();n<r;++n){const t=o.get(n);if(t!=null){const s={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},c=new l.AcDbTextStyleTableRecord(s);this.processCommonTableEntryAttrs(t,c),i.tables.textStyleTable.add(c)}}}processDimStyles(e,i){const o=e.dimStyles;for(let n=0,r=o.size();n<r;++n){const t=o.get(n);if(t!=null){const s={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},c=new l.AcDbDimStyleTableRecord(s);this.processCommonTableEntryAttrs(t,c),i.tables.dimStyleTable.add(c)}}}processLayers(e,i){const o=e.layers;for(let n=0,r=o.size();n<r;++n){const t=o.get(n);if(t!=null){const s=new l.AcCmColor;s.colorIndex=t.color;const c=new l.AcDbLayerTableRecord({name:t.name,standardFlags:t.flags,linetype:t.lineType,lineWeight:t.lWeight,isOff:t.color<0,color:s,isPlottable:t.plotF,materialId:t.handleMaterialS});this.processCommonTableEntryAttrs(t,c),i.tables.layerTable.add(c)}}}processViewports(e,i){const o=e.viewports;for(let n=0,r=o.size();n<r;++n){const t=o.get(n);if(t!=null){const s=new l.AcDbViewportTableRecord;this.processCommonTableEntryAttrs(t,s),t.circleZoom&&(s.circleSides=t.circleZoom),s.standardFlag=t.flags,s.center.copy(t.center),s.lowerLeftCorner.copy(t.lowerLeft),s.upperRightCorner.copy(t.upperRight),t.snapBase&&s.snapBase.copy(t.snapBase),t.snapAngle&&(s.snapAngle=t.snapAngle),s.snapIncrements&&s.snapIncrements.copy(t.snapSpacing),t.gridSpacing&&s.gridIncrements.copy(t.gridSpacing),s.gsView.center.copy(t.center),s.gsView.viewDirectionFromTarget.copy(t.viewDir),s.gsView.viewTarget.copy(t.viewTarget),t.lensHeight&&(s.gsView.lensLength=t.lensHeight),t.height&&(s.gsView.viewHeight=t.height),t.twistAngle&&(s.gsView.viewTwistAngle=t.twistAngle),t.viewMode&&(s.gsView.viewMode=t.viewMode),t.ucsIcon&&(s.gsView.ucsIconSetting=t.ucsIcon),i.tables.viewportTable.add(s)}}}processBlockTables(e,i){const o=e.blocks;for(let n=0,r=o.size();n<r;++n){const t=o.get(n);if(t!=null){const s=new l.AcDbBlockTableRecord;s.objectId=t.handle.toString(),s.name=t.name.toUpperCase(),i.tables.blockTable.add(s),t.entities&&this.processEntitiesInBlock(t.entities,s)}}}processCommonTableEntryAttrs(e,i){i.name=e.name,i.objectId=e.handle.toString(),i.ownerId=e.parentHandle.toString()}processHeader(e,i){const o=e.header;let n=o.getVar("$CECOLOR");i.cecolor.colorIndex=n?n.getInt():256,n=o.getVar("$ANGDIR"),i.angDir=n?n.getInt():0,n=o.getVar("$AUNITS"),i.aunits=n?n.getInt():0,n=o.getVar("$INSUNITS"),i.insunits=n?n.getInt():1,n=o.getVar("$PDMODE"),i.pdmode=n?n.getInt():0,n=o.getVar("$PDSIZE"),i.pdsize=n?n.getDouble():0}processObjects(e,i){this.processImageDefs(e.images,i)}processImageDefs(e,i){const o=new v,n=i.dictionaries.imageDefs;for(let r=0,t=e.size();r<t;++r){const s=e.get(r);if(s!=null){const c=o.convertImageDef(s);n.setAt(c.objectId,c)}}}async processEntitiesInBlock(e,i){const o=new A,n=[];for(let r=0,t=e.size();r<t;++r){const s=e.get(r);if(s!=null){const c=o.convert(s);c&&n.push(c)}}i.appendEntity(n)}async processEntities(e,i,o,n,r){if(e.mBlock){const t=new A;let s=[];const c=e.mBlock.entities,u=c.size();for(let g=0;g<u;g++){const a=c.get(g);a&&s.push(a)}const f=new l.AcDbBatchProcessing(s.length,100-n.value,o);this.config.convertByEntityType&&(s=this.groupAndFlattenByType(s));const p=i.tables.blockTable.modelSpace;await f.processChunk(async(g,a)=>{const d=[];for(let m=g;m<a;m++){const b=s[m],w=t.convert(b);w&&d.push(w)}if(p.appendEntity(d),r){let m=n.value+a/u*(100-n.value);m>100&&(m=100),await r(m,"ENTITY","IN-PROGRESS")}})}}groupAndFlattenByType(e){const i={},o=[],n=e.length;for(let r=0;r<n;r++){const t=e[r],s=t.eType.value;i[s]||(i[s]=[],o.push(s)),i[s].push(t)}return o.flatMap(r=>i[r])}}h.AcDbLibdxfrwConverter=D,Object.defineProperty(h,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;IAwBrB,OAAO,CAAC,WAAW;IAenB,OAAO,CAAC,YAAY;IAOpB,OAAO,CAAC,kBAAkB;IA8B1B,OAAO,CAAC,aAAa;IAWrB,OAAO,CAAC,YAAY;CAUrB"}