@mlightcad/libredwg-converter 3.0.7 → 3.0.9
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/dist/libredwg-converter.js +84 -53
- package/dist/libredwg-converter.umd.cjs +1 -1
- package/dist/libredwg-parser-worker.js +339 -339
- package/lib/AcDbLibreDwgConverter.d.ts +14 -0
- package/lib/AcDbLibreDwgConverter.d.ts.map +1 -1
- package/lib/AcDbLibreDwgConverter.js +60 -7
- package/lib/AcDbLibreDwgConverter.js.map +1 -1
- package/package.json +2 -2
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AcDbArc as
|
|
1
|
+
import { AcDbArc as S, AcDbCircle as E, AcGeVector3d as g, AcDbEllipse as P, AcDbLine as L, AcGePoint3d as I, AcDbSpline as w, AcDbPoint as C, AcDbTrace as x, AcDbPolyline as b, AcGePoint2d as A, AcDbHatch as v, AcGePolyline2d as O, AcGeLoop2d as k, AcGeLine2d as R, AcGeCircArc2d as N, AcGeVector2d as V, AcGeEllipseArc2d as m, AcGeSpline3d as y, AcDbTable as B, AcDbText as F, AcDbMText as j, AcDbLeader as z, AcDbAlignedDimension as H, AcDb3PointAngularDimension as W, AcDbOrdinateDimension as X, AcDbRadialDimension as G, AcDbDiametricDimension as U, AcDbRasterImage as Y, AcDbViewport as Z, AcDbRay as K, AcDbXline as _, AcDbBlockReference as q, AcDbDatabaseConverter as J, createWorkerApi as Q, AcDbLinetypeTableRecord as $, AcDbTextStyleTableRecord as ee, AcDbDimStyleTableRecord as ne, AcCmColor as te, AcDbLayerTableRecord as oe, AcDbViewportTableRecord as se, AcDbBlockTableRecord as re, AcDbBatchProcessing as ie, AcDbLayout as ce, AcDbRasterImageDef as ae } from "@mlightcad/data-model";
|
|
2
2
|
class T {
|
|
3
3
|
convert(e) {
|
|
4
4
|
const n = this.createEntity(e);
|
|
@@ -13,7 +13,7 @@ class T {
|
|
|
13
13
|
return e.type == "ARC" ? this.convertArc(e) : e.type == "CIRCLE" ? this.convertCirle(e) : e.type == "DIMENSION" ? this.convertDimension(e) : e.type == "ELLIPSE" ? this.convertEllipse(e) : e.type == "HATCH" ? this.convertHatch(e) : e.type == "IMAGE" ? this.convertImage(e) : e.type == "LEADER" ? this.convertLeader(e) : e.type == "LINE" ? this.convertLine(e) : e.type == "LWPOLYLINE" ? this.convertLWPolyline(e) : e.type == "MTEXT" ? this.convertMText(e) : e.type == "POINT" ? this.convertPoint(e) : e.type == "POLYLINE" ? this.convertPolyline(e) : e.type == "RAY" ? this.convertRay(e) : e.type == "SPLINE" ? this.convertSpline(e) : e.type == "ACAD_TABLE" ? this.convertTable(e) : e.type == "TEXT" ? this.convertText(e) : e.type == "SOLID" ? this.convertSolid(e) : e.type == "VIEWPORT" ? this.convertViewport(e) : e.type == "XLINE" ? this.convertXline(e) : e.type == "INSERT" ? this.convertBlockReference(e) : null;
|
|
14
14
|
}
|
|
15
15
|
convertArc(e) {
|
|
16
|
-
return new
|
|
16
|
+
return new S(
|
|
17
17
|
e.center,
|
|
18
18
|
e.radius,
|
|
19
19
|
e.startAngle,
|
|
@@ -21,13 +21,13 @@ class T {
|
|
|
21
21
|
);
|
|
22
22
|
}
|
|
23
23
|
convertCirle(e) {
|
|
24
|
-
return new
|
|
24
|
+
return new E(e.center, e.radius);
|
|
25
25
|
}
|
|
26
26
|
convertEllipse(e) {
|
|
27
|
-
const n = new
|
|
28
|
-
return new
|
|
27
|
+
const n = new g(e.majorAxisEndPoint), s = n.length();
|
|
28
|
+
return new P(
|
|
29
29
|
e.center,
|
|
30
|
-
|
|
30
|
+
g.Z_AXIS,
|
|
31
31
|
n,
|
|
32
32
|
s,
|
|
33
33
|
s * e.axisRatio,
|
|
@@ -38,18 +38,18 @@ class T {
|
|
|
38
38
|
convertLine(e) {
|
|
39
39
|
const n = e.startPoint, s = e.endPoint;
|
|
40
40
|
return new L(
|
|
41
|
-
new
|
|
42
|
-
new
|
|
41
|
+
new I(n.x, n.y, n.z),
|
|
42
|
+
new I(s.x, s.y, s.z)
|
|
43
43
|
);
|
|
44
44
|
}
|
|
45
45
|
convertSpline(e) {
|
|
46
|
-
return e.numberOfControlPoints > 0 && e.numberOfKnots > 0 ? new
|
|
46
|
+
return e.numberOfControlPoints > 0 && e.numberOfKnots > 0 ? new w(
|
|
47
47
|
e.controlPoints,
|
|
48
48
|
e.knots,
|
|
49
49
|
e.weights,
|
|
50
50
|
e.degree,
|
|
51
51
|
!!(e.flag & 1)
|
|
52
|
-
) : e.numberOfFitPoints > 0 ? new
|
|
52
|
+
) : e.numberOfFitPoints > 0 ? new w(
|
|
53
53
|
e.fitPoints,
|
|
54
54
|
"Uniform",
|
|
55
55
|
e.degree,
|
|
@@ -68,11 +68,11 @@ class T {
|
|
|
68
68
|
), n.thickness = e.thickness, n;
|
|
69
69
|
}
|
|
70
70
|
convertLWPolyline(e) {
|
|
71
|
-
const n = new
|
|
71
|
+
const n = new b();
|
|
72
72
|
return n.closed = !!(e.flag & 512), e.vertices.forEach((s, t) => {
|
|
73
73
|
n.addVertexAt(
|
|
74
74
|
t,
|
|
75
|
-
new
|
|
75
|
+
new A(s.x, s.y),
|
|
76
76
|
s.bulge,
|
|
77
77
|
s.startWidth,
|
|
78
78
|
s.endWidth
|
|
@@ -80,11 +80,11 @@ class T {
|
|
|
80
80
|
}), n;
|
|
81
81
|
}
|
|
82
82
|
convertPolyline(e) {
|
|
83
|
-
const n = new
|
|
83
|
+
const n = new b();
|
|
84
84
|
return n.closed = !!(e.flag & 1), e.vertices.forEach((s, t) => {
|
|
85
85
|
n.addVertexAt(
|
|
86
86
|
t,
|
|
87
|
-
new
|
|
87
|
+
new A(s.x, s.y),
|
|
88
88
|
s.bulge,
|
|
89
89
|
s.startWidth,
|
|
90
90
|
s.endWidth
|
|
@@ -131,48 +131,48 @@ class T {
|
|
|
131
131
|
} else if (l.type == 3) {
|
|
132
132
|
const c = l;
|
|
133
133
|
new V().subVectors(c.end, c.center);
|
|
134
|
-
const
|
|
134
|
+
const p = Math.sqrt(
|
|
135
135
|
Math.pow(c.end.x, 2) + Math.pow(c.end.y, 2)
|
|
136
|
-
),
|
|
137
|
-
let
|
|
138
|
-
const
|
|
139
|
-
c.isCCW || (
|
|
136
|
+
), f = p * c.lengthOfMinorAxis;
|
|
137
|
+
let d = c.startAngle, h = c.endAngle;
|
|
138
|
+
const D = Math.atan2(c.end.y, c.end.x);
|
|
139
|
+
c.isCCW || (d = Math.PI * 2 - d, h = Math.PI * 2 - h), i.add(
|
|
140
140
|
new m(
|
|
141
141
|
{ ...c.center, z: 0 },
|
|
142
|
-
d,
|
|
143
142
|
p,
|
|
144
143
|
f,
|
|
145
|
-
|
|
144
|
+
d,
|
|
145
|
+
h,
|
|
146
146
|
!c.isCCW,
|
|
147
|
-
|
|
147
|
+
D
|
|
148
148
|
)
|
|
149
149
|
);
|
|
150
150
|
} else if (l.type == 4) {
|
|
151
151
|
const c = l;
|
|
152
152
|
if (c.numberOfControlPoints > 0 && c.numberOfKnots > 0) {
|
|
153
153
|
const u = c.controlPoints.map(
|
|
154
|
-
(
|
|
155
|
-
x:
|
|
156
|
-
y:
|
|
154
|
+
(d) => ({
|
|
155
|
+
x: d.x,
|
|
156
|
+
y: d.y,
|
|
157
157
|
z: 0
|
|
158
158
|
})
|
|
159
159
|
);
|
|
160
|
-
let
|
|
161
|
-
const
|
|
160
|
+
let p = !0;
|
|
161
|
+
const f = c.controlPoints.map((d) => (d.weight == null && (p = !1), d.weight || 1));
|
|
162
162
|
i.add(
|
|
163
|
-
new
|
|
163
|
+
new y(
|
|
164
164
|
u,
|
|
165
165
|
c.knots,
|
|
166
|
-
|
|
166
|
+
p ? f : void 0
|
|
167
167
|
)
|
|
168
168
|
);
|
|
169
169
|
} else if (c.numberOfFitData > 0) {
|
|
170
|
-
const u = c.fitDatum.map((
|
|
171
|
-
x:
|
|
172
|
-
y:
|
|
170
|
+
const u = c.fitDatum.map((p) => ({
|
|
171
|
+
x: p.x,
|
|
172
|
+
y: p.y,
|
|
173
173
|
z: 0
|
|
174
174
|
}));
|
|
175
|
-
i.add(new
|
|
175
|
+
i.add(new y(u, "Uniform"));
|
|
176
176
|
}
|
|
177
177
|
}
|
|
178
178
|
}), n.add(i);
|
|
@@ -180,7 +180,7 @@ class T {
|
|
|
180
180
|
}), n;
|
|
181
181
|
}
|
|
182
182
|
convertTable(e) {
|
|
183
|
-
const n = new
|
|
183
|
+
const n = new B(
|
|
184
184
|
e.name,
|
|
185
185
|
e.rowCount,
|
|
186
186
|
e.columnCount
|
|
@@ -198,8 +198,8 @@ class T {
|
|
|
198
198
|
return n.textString = e.text, n.styleName = e.styleName, n.height = e.textHeight, n.position.copy(e.startPoint), n.rotation = e.rotation, n.oblique = e.obliqueAngle ?? 0, n.thickness = e.thickness, n.horizontalMode = e.halign, n.verticalMode = e.valign, n.widthFactor = e.xScale ?? 1, n;
|
|
199
199
|
}
|
|
200
200
|
convertMText(e) {
|
|
201
|
-
const n = new
|
|
202
|
-
return n.contents = e.text, e.styleName != null && (n.styleName = e.styleName), n.height = e.textHeight, n.width = e.rectWidth, n.rotation = e.rotation || 0, n.location = e.insertionPoint, n.attachmentPoint = e.attachmentPoint, e.direction && (n.direction = new
|
|
201
|
+
const n = new j();
|
|
202
|
+
return n.contents = e.text, e.styleName != null && (n.styleName = e.styleName), n.height = e.textHeight, n.width = e.rectWidth, n.rotation = e.rotation || 0, n.location = e.insertionPoint, n.attachmentPoint = e.attachmentPoint, e.direction && (n.direction = new g(e.direction)), n.drawingDirection = e.drawingDirection, n;
|
|
203
203
|
}
|
|
204
204
|
convertLeader(e) {
|
|
205
205
|
const n = new z();
|
|
@@ -248,7 +248,7 @@ class T {
|
|
|
248
248
|
}
|
|
249
249
|
processImage(e, n) {
|
|
250
250
|
n.position.copy(e.position), n.brightness = e.brightness, n.contrast = e.contrast, n.fade = e.fade, n.imageDefId = e.imageDefHandle.toString(), n.isClipped = (e.flags | 4) > 0, n.isImageShown = (e.flags | 3) > 0, n.isImageTransparent = (e.flags | 8) > 0, e.clippingBoundaryPath.forEach((s) => {
|
|
251
|
-
n.clipBoundary.push(new
|
|
251
|
+
n.clipBoundary.push(new A(s));
|
|
252
252
|
}), n.clipBoundaryType = e.clippingBoundaryType, n.width = Math.sqrt(
|
|
253
253
|
e.uPixel.x ** 2 + e.uPixel.y ** 2 + e.uPixel.z ** 2
|
|
254
254
|
) * e.imageSize.x, n.height = Math.sqrt(
|
|
@@ -292,9 +292,15 @@ class de extends J {
|
|
|
292
292
|
super(e), e.useWorker = !0, e.parserWorkerUrl || (e.parserWorkerUrl = "/assets/libredwg-parser-worker.js");
|
|
293
293
|
}
|
|
294
294
|
async parse(e) {
|
|
295
|
-
if (this.config.useWorker && this.config.parserWorkerUrl)
|
|
296
|
-
|
|
297
|
-
|
|
295
|
+
if (this.config.useWorker && this.config.parserWorkerUrl) {
|
|
296
|
+
const n = Q({
|
|
297
|
+
workerUrl: this.config.parserWorkerUrl,
|
|
298
|
+
// One concurrent worker needed for parser
|
|
299
|
+
maxConcurrentWorkers: 1
|
|
300
|
+
}), s = await n.execute(e);
|
|
301
|
+
return n.destroy(), s.data;
|
|
302
|
+
} else
|
|
303
|
+
throw new Error("dwg converter can run in web worker only!");
|
|
298
304
|
}
|
|
299
305
|
/**
|
|
300
306
|
* Gets all of fonts used by entities in model space and paper space
|
|
@@ -471,11 +477,12 @@ class de extends J {
|
|
|
471
477
|
processBlocks(e, n) {
|
|
472
478
|
}
|
|
473
479
|
async processEntitiesInBlock(e, n) {
|
|
474
|
-
const s = new T(), t = e.length;
|
|
475
|
-
for (let
|
|
476
|
-
const
|
|
477
|
-
|
|
480
|
+
const s = new T(), t = e.length, o = [];
|
|
481
|
+
for (let r = 0; r < t; r++) {
|
|
482
|
+
const a = e[r], i = s.convert(a);
|
|
483
|
+
i && o.push(i);
|
|
478
484
|
}
|
|
485
|
+
n.appendEntity(o);
|
|
479
486
|
}
|
|
480
487
|
/**
|
|
481
488
|
* Breaks up the work into smaller chunks that are executed asynchronously. This is often referred to
|
|
@@ -483,19 +490,24 @@ class de extends J {
|
|
|
483
490
|
* smaller pieces and executed in small intervals to allow the UI to remain responsive.
|
|
484
491
|
*/
|
|
485
492
|
async processEntities(e, n, s, t, o) {
|
|
486
|
-
const r = new T()
|
|
493
|
+
const r = new T();
|
|
494
|
+
let a = e.entities;
|
|
495
|
+
const i = a.length, l = new ie(
|
|
487
496
|
i,
|
|
488
497
|
100 - t.value,
|
|
489
498
|
s
|
|
490
|
-
)
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
499
|
+
);
|
|
500
|
+
this.config.convertByEntityType && (a = this.groupAndFlattenByType(a));
|
|
501
|
+
const c = n.tables.blockTable.modelSpace;
|
|
502
|
+
await l.processChunk(async (u, p) => {
|
|
503
|
+
const f = [];
|
|
504
|
+
for (let d = u; d < p; d++) {
|
|
505
|
+
const h = a[d], D = r.convert(h);
|
|
506
|
+
D && f.push(D);
|
|
495
507
|
}
|
|
496
|
-
if (o) {
|
|
497
|
-
let
|
|
498
|
-
|
|
508
|
+
if (c.appendEntity(f), o) {
|
|
509
|
+
let d = t.value + p / i * (100 - t.value);
|
|
510
|
+
d > 100 && (d = 100), await o(d, "ENTITY", "IN-PROGRESS");
|
|
499
511
|
}
|
|
500
512
|
});
|
|
501
513
|
}
|
|
@@ -534,6 +546,25 @@ class de extends J {
|
|
|
534
546
|
processCommonObjectAttrs(e, n) {
|
|
535
547
|
n.objectId = e.handle.toString(), n.ownerId = e.ownerHandle.toString();
|
|
536
548
|
}
|
|
549
|
+
/**
|
|
550
|
+
* Groups entities by their `type` property and flattens the result into a single array.
|
|
551
|
+
*
|
|
552
|
+
* The order of `type` groups follows the order in which they first appear in the input array.
|
|
553
|
+
* Items within each group preserve their original order.
|
|
554
|
+
*
|
|
555
|
+
* This runs in O(n) time, which is generally faster than sorting when you
|
|
556
|
+
* don't care about alphabetical order of types.
|
|
557
|
+
*
|
|
558
|
+
* @param entities - The array of entities to group and flatten.
|
|
559
|
+
*
|
|
560
|
+
* @returns A new array of entities grouped by their `type` property.
|
|
561
|
+
*/
|
|
562
|
+
groupAndFlattenByType(e) {
|
|
563
|
+
const n = {}, s = [];
|
|
564
|
+
for (const t of e)
|
|
565
|
+
n[t.type] || (n[t.type] = [], s.push(t.type)), n[t.type].push(t);
|
|
566
|
+
return s.flatMap((t) => n[t]);
|
|
567
|
+
}
|
|
537
568
|
}
|
|
538
569
|
export {
|
|
539
570
|
de as AcDbLibreDwgConverter
|
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(D,o){typeof exports=="object"&&typeof module<"u"?o(exports,require("@mlightcad/data-model")):typeof define=="function"&&define.amd?define(["exports","@mlightcad/data-model"],o):(D=typeof globalThis<"u"?globalThis:D||self,o(D["libredwg-converter"]={},D.dataModel))})(this,function(D,o){"use strict";class A{convert(e){const n=this.createEntity(e);return n&&this.processCommonAttrs(e,n),n}createEntity(e){return e.type=="ARC"?this.convertArc(e):e.type=="CIRCLE"?this.convertCirle(e):e.type=="DIMENSION"?this.convertDimension(e):e.type=="ELLIPSE"?this.convertEllipse(e):e.type=="HATCH"?this.convertHatch(e):e.type=="IMAGE"?this.convertImage(e):e.type=="LEADER"?this.convertLeader(e):e.type=="LINE"?this.convertLine(e):e.type=="LWPOLYLINE"?this.convertLWPolyline(e):e.type=="MTEXT"?this.convertMText(e):e.type=="POINT"?this.convertPoint(e):e.type=="POLYLINE"?this.convertPolyline(e):e.type=="RAY"?this.convertRay(e):e.type=="SPLINE"?this.convertSpline(e):e.type=="ACAD_TABLE"?this.convertTable(e):e.type=="TEXT"?this.convertText(e):e.type=="SOLID"?this.convertSolid(e):e.type=="VIEWPORT"?this.convertViewport(e):e.type=="XLINE"?this.convertXline(e):e.type=="INSERT"?this.convertBlockReference(e):null}convertArc(e){return new o.AcDbArc(e.center,e.radius,e.startAngle,e.endAngle)}convertCirle(e){return new o.AcDbCircle(e.center,e.radius)}convertEllipse(e){const n=new o.AcGeVector3d(e.majorAxisEndPoint),r=n.length();return new o.AcDbEllipse(e.center,o.AcGeVector3d.Z_AXIS,n,r,r*e.axisRatio,e.startAngle,e.endAngle)}convertLine(e){const n=e.startPoint,r=e.endPoint;return new o.AcDbLine(new o.AcGePoint3d(n.x,n.y,n.z),new o.AcGePoint3d(r.x,r.y,r.z))}convertSpline(e){return e.numberOfControlPoints>0&&e.numberOfKnots>0?new o.AcDbSpline(e.controlPoints,e.knots,e.weights,e.degree,!!(e.flag&1)):e.numberOfFitPoints>0?new o.AcDbSpline(e.fitPoints,"Uniform",e.degree,!!(e.flag&1)):null}convertPoint(e){const n=new o.AcDbPoint;return n.position=e.position,n}convertSolid(e){const n=new o.AcDbTrace;return n.setPointAt(0,{...e.corner1,z:0}),n.setPointAt(1,{...e.corner2,z:0}),n.setPointAt(2,{...e.corner3,z:0}),n.setPointAt(3,e.corner4?{...e.corner4,z:0}:{...e.corner3,z:0}),n.thickness=e.thickness,n}convertLWPolyline(e){const n=new o.AcDbPolyline;return n.closed=!!(e.flag&512),e.vertices.forEach((r,t)=>{n.addVertexAt(t,new o.AcGePoint2d(r.x,r.y),r.bulge,r.startWidth,r.endWidth)}),n}convertPolyline(e){const n=new o.AcDbPolyline;return n.closed=!!(e.flag&1),e.vertices.forEach((r,t)=>{n.addVertexAt(t,new o.AcGePoint2d(r.x,r.y),r.bulge,r.startWidth,r.endWidth)}),n}convertHatch(e){var t;const n=new o.AcDbHatch;return(t=e.definitionLines)==null||t.forEach(s=>{n.definitionLines.push({angle:s.angle,origin:s.base,delta:s.offset,dashPattern:s.numberOfDashLengths>0?s.dashLengths:[]})}),n.hatchStyle=e.hatchStyle,n.patternName=e.patternName,n.patternType=e.patternType,n.patternAngle=e.patternAngle==null?0:e.patternAngle,n.patternScale=e.patternScale==null?0:e.patternScale,e.boundaryPaths.forEach(s=>{if(s.boundaryPathTypeFlag&2){const l=s,c=new o.AcGePolyline2d;c.closed=l.isClosed,l.vertices.forEach((p,a)=>{c.addVertexAt(a,{x:p.x,y:p.y,bulge:p.bulge})}),n.add(c)}else{const l=s,c=new o.AcGeLoop2d;l.edges.forEach(p=>{if(p.type==1){const a=p;c.add(new o.AcGeLine2d(a.start,a.end))}else if(p.type==2){const a=p;c.add(new o.AcGeCircArc2d(a.center,a.radius,a.startAngle,a.endAngle,!a.isCCW))}else if(p.type==3){const a=p;new o.AcGeVector2d().subVectors(a.end,a.center);const d=Math.sqrt(Math.pow(a.end.x,2)+Math.pow(a.end.y,2)),f=d*a.lengthOfMinorAxis;let u=a.startAngle,g=a.endAngle;const w=Math.atan2(a.end.y,a.end.x);a.isCCW||(u=Math.PI*2-u,g=Math.PI*2-g),c.add(new o.AcGeEllipseArc2d({...a.center,z:0},d,f,u,g,!a.isCCW,w))}else if(p.type==4){const a=p;if(a.numberOfControlPoints>0&&a.numberOfKnots>0){const h=a.controlPoints.map(u=>({x:u.x,y:u.y,z:0}));let d=!0;const f=a.controlPoints.map(u=>(u.weight==null&&(d=!1),u.weight||1));c.add(new o.AcGeSpline3d(h,a.knots,d?f:void 0))}else if(a.numberOfFitData>0){const h=a.fitDatum.map(d=>({x:d.x,y:d.y,z:0}));c.add(new o.AcGeSpline3d(h,"Uniform"))}}}),n.add(c)}}),n}convertTable(e){const n=new o.AcDbTable(e.name,e.rowCount,e.columnCount);return n.attachmentPoint=e.attachmentPoint,n.position.copy(e.startPoint),e.columnWidthArr.forEach((r,t)=>n.setColumnWidth(t,r)),e.rowHeightArr.forEach((r,t)=>n.setRowHeight(t,r)),e.cells.forEach((r,t)=>{n.setCell(t,r)}),n}convertText(e){const n=new o.AcDbText;return n.textString=e.text,n.styleName=e.styleName,n.height=e.textHeight,n.position.copy(e.startPoint),n.rotation=e.rotation,n.oblique=e.obliqueAngle??0,n.thickness=e.thickness,n.horizontalMode=e.halign,n.verticalMode=e.valign,n.widthFactor=e.xScale??1,n}convertMText(e){const n=new o.AcDbMText;return n.contents=e.text,e.styleName!=null&&(n.styleName=e.styleName),n.height=e.textHeight,n.width=e.rectWidth,n.rotation=e.rotation||0,n.location=e.insertionPoint,n.attachmentPoint=e.attachmentPoint,e.direction&&(n.direction=new o.AcGeVector3d(e.direction)),n.drawingDirection=e.drawingDirection,n}convertLeader(e){const n=new o.AcDbLeader;return e.vertices.forEach(r=>{n.appendVertex(r)}),n.hasArrowHead=e.isArrowheadEnabled,n.hasHookLine=e.isHooklineExists,n.isSplined=e.isSpline,n.dimensionStyle=e.styleName,n.annoType=e.leaderCreationFlag,n}convertDimension(e){if(e.subclassMarker=="AcDbAlignedDimension"||e.subclassMarker=="AcDbRotatedDimension"){const n=e,r=new o.AcDbAlignedDimension(n.subDefinitionPoint1,n.subDefinitionPoint2,n.definitionPoint);return r.rotation=n.rotationAngle,this.processDimensionCommonAttrs(e,r),r}else if(e.subclassMarker=="AcDb3PointAngularDimension"){const n=e,r=new o.AcDb3PointAngularDimension(n.centerPoint,n.subDefinitionPoint1,n.subDefinitionPoint2,n.definitionPoint);return this.processDimensionCommonAttrs(e,r),r}else if(e.subclassMarker=="AcDbOrdinateDimension"){const n=e,r=new o.AcDbOrdinateDimension(n.subDefinitionPoint1,n.subDefinitionPoint2);return this.processDimensionCommonAttrs(e,r),r}else if(e.subclassMarker=="AcDbRadialDimension"){const n=e,r=new o.AcDbRadialDimension(n.definitionPoint,n.centerPoint,n.leaderLength);return this.processDimensionCommonAttrs(e,r),r}else if(e.subclassMarker=="AcDbDiametricDimension"){const n=e,r=new o.AcDbDiametricDimension(n.definitionPoint,n.centerPoint,n.leaderLength);return this.processDimensionCommonAttrs(e,r),r}return null}processImage(e,n){n.position.copy(e.position),n.brightness=e.brightness,n.contrast=e.contrast,n.fade=e.fade,n.imageDefId=e.imageDefHandle.toString(),n.isClipped=(e.flags|4)>0,n.isImageShown=(e.flags|3)>0,n.isImageTransparent=(e.flags|8)>0,e.clippingBoundaryPath.forEach(r=>{n.clipBoundary.push(new o.AcGePoint2d(r))}),n.clipBoundaryType=e.clippingBoundaryType,n.width=Math.sqrt(e.uPixel.x**2+e.uPixel.y**2+e.uPixel.z**2)*e.imageSize.x,n.height=Math.sqrt(e.vPixel.x**2+e.vPixel.y**2+e.vPixel.z**2)*e.imageSize.y,n.rotation=Math.atan2(e.uPixel.y,e.uPixel.x)}convertImage(e){const n=new o.AcDbRasterImage;return this.processImage(e,n),n}convertViewport(e){const n=new o.AcDbViewport;return n.number=e.viewportId,n.centerPoint.copy(e.viewportCenter),n.height=e.height,n.width=e.width,n.viewCenter.copy(e.displayCenter),n.viewHeight=e.viewHeight,n}convertRay(e){const n=new o.AcDbRay;return n.basePoint.copy(e.firstPoint),n.unitDir.copy(e.unitDirection),n}convertXline(e){const n=new o.AcDbXline;return n.basePoint.copy(e.firstPoint),n.unitDir.copy(e.unitDirection),n}convertBlockReference(e){const n=new o.AcDbBlockReference(e.name);return e.insertionPoint&&n.position.copy(e.insertionPoint),n.scaleFactors.x=e.xScale,n.scaleFactors.y=e.yScale,n.scaleFactors.z=e.zScale,n.rotation=e.rotation,n.normal.copy(e.extrusionDirection),n}processDimensionCommonAttrs(e,n){n.dimBlockId=e.name,n.textPosition.copy(e.textPoint),n.textRotation=e.textRotation||0,e.textLineSpacingFactor&&(n.textLineSpacingFactor=e.textLineSpacingFactor),e.textLineSpacingStyle&&(n.textLineSpacingStyle=e.textLineSpacingStyle),n.dimensionStyleName=e.styleName,n.dimensionText=e.text||"",n.measurement=e.measurement}processCommonAttrs(e,n){n.layer=e.layer,n.objectId=e.handle.toString(),n.ownerId=e.ownerBlockRecordSoftId.toString(),e.lineType!=null&&(n.lineType=e.lineType),e.lineweight!=null&&(n.lineWeight=e.lineweight),e.lineTypeScale!=null&&(n.linetypeScale=e.lineTypeScale),e.color!=null&&(n.color.color=e.color),e.colorIndex!=null&&(n.color.colorIndex=e.colorIndex),e.colorName!=null&&(n.color.colorName=e.colorName),e.isVisible!=null&&(n.visibility=e.isVisible),e.transparency!=null&&(n.transparency=e.transparency)}}class I extends o.AcDbDatabaseConverter{constructor(e={}){super(e),e.useWorker=!0,e.parserWorkerUrl||(e.parserWorkerUrl="/assets/libredwg-parser-worker.js")}async parse(e){if(this.config.useWorker&&this.config.parserWorkerUrl)return(await o.createWorkerApi({workerUrl:this.config.parserWorkerUrl}).execute(e)).data;throw new Error("dwg converter can run in web worker only!")}getFonts(e){const n=new Map;e.tables.BLOCK_RECORD.entries.forEach(i=>{n.set(i.name,i)});const r=new Map,t=i=>{if(i){const l=i.lastIndexOf(".");return l>=0?i.substring(0,l).toLowerCase():i.toLowerCase()}};e.tables.STYLE.entries.forEach(i=>{const l=[];let c=t(i.font);c&&l.push(c),c=t(i.bigFont),c&&l.push(c),r.set(i.name,l)});const s=new Set;return this.getFontsInBlock(e.entities,n,r,s),Array.from(s)}getFontsInBlock(e,n,r,t){const s=/\\f(.*?)\|/g;e.forEach(i=>{if(i.type=="MTEXT"){const l=i;[...l.text.matchAll(s)].forEach(p=>{t.add(p[1].toLowerCase())});const c=r.get(l.styleName);c==null||c.forEach(p=>t.add(p))}else if(i.type=="TEXT"){const l=i,c=r.get(l.styleName);c==null||c.forEach(p=>t.add(p))}else if(i.type=="INSERT"){const l=i,c=n.get(l.name);c&&this.getFontsInBlock(c.entities,n,r,t)}})}processLineTypes(e,n){e.tables.LTYPE.entries.forEach(t=>{const s={name:t.name,description:t.description,standardFlag:t.standardFlag,totalPatternLength:t.totalPatternLength,pattern:t.pattern},i=new o.AcDbLinetypeTableRecord(s);this.processCommonTableEntryAttrs(t,i),i.name=t.name,n.tables.linetypeTable.add(i)})}processTextStyles(e,n){e.tables.STYLE.entries.forEach(t=>{const s=new o.AcDbTextStyleTableRecord(t);this.processCommonTableEntryAttrs(t,s),n.tables.textStyleTable.add(s)})}processDimStyles(e,n){e.tables.DIMSTYLE.entries.forEach(t=>{const s={name:t.name,ownerId:t.ownerHandle.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,dimtmove:t.DIMTMOVE||0,dimjust:t.DIMJUST,dimsd1:t.DIMSD1,dimsd2:t.DIMSD2,dimtolj:t.DIMTOLJ,dimtzin:t.DIMTZIN,dimaltz:t.DIMALTZ,dimalttz:t.DIMALTTZ,dimfit:t.DIMFIT||0,dimupt:t.DIMUPT,dimatfit:t.DIMATFIT,dimtxsty:"Standard",dimldrblk:"",dimblk:t.DIMBLK||"",dimblk1:t.DIMBLK1||"",dimblk2:t.DIMBLK2||"",dimlwd:t.DIMLWD,dimlwe:t.DIMLWE},i=new o.AcDbDimStyleTableRecord(s);this.processCommonTableEntryAttrs(t,i),n.tables.dimStyleTable.add(i)})}processLayers(e,n){e.tables.LAYER.entries.forEach(t=>{const s=new o.AcCmColor;s.colorIndex=t.colorIndex;const i=new o.AcDbLayerTableRecord({name:t.name,standardFlags:t.standardFlag,linetype:t.lineType,lineWeight:t.lineweight,isOff:t.off,color:s,isPlottable:t.plotFlag!=0});this.processCommonTableEntryAttrs(t,i),n.tables.layerTable.add(i)})}processViewports(e,n){e.tables.VPORT.entries.forEach(t=>{const s=new o.AcDbViewportTableRecord;this.processCommonTableEntryAttrs(t,s),t.circleSides&&(s.circleSides=t.circleSides),s.standardFlag=t.standardFlag,s.center.copy(t.center),s.lowerLeftCorner.copy(t.lowerLeftCorner),s.upperRightCorner.copy(t.upperRightCorner),t.snapBasePoint&&s.snapBase.copy(t.snapBasePoint),t.snapRotationAngle&&(s.snapAngle=t.snapRotationAngle),t.snapSpacing&&s.snapIncrements.copy(t.snapSpacing),t.majorGridLines&&(s.gridMajor=t.majorGridLines),t.gridSpacing&&s.gridIncrements.copy(t.gridSpacing),t.backgroundObjectId&&(s.backgroundObjectId=t.backgroundObjectId),s.gsView.center.copy(t.center),s.gsView.viewDirectionFromTarget.copy(t.viewDirectionFromTarget),s.gsView.viewTarget.copy(t.viewTarget),t.lensLength&&(s.gsView.lensLength=t.lensLength),t.frontClippingPlane&&(s.gsView.frontClippingPlane=t.frontClippingPlane),t.backClippingPlane&&(s.gsView.backClippingPlane=t.backClippingPlane),t.viewHeight&&(s.gsView.viewHeight=t.viewHeight),t.viewTwistAngle&&(s.gsView.viewTwistAngle=t.viewTwistAngle),t.frozenLayers&&(s.gsView.frozenLayers=t.frozenLayers),t.styleSheet&&(s.gsView.styleSheet=t.styleSheet),t.renderMode&&(s.gsView.renderMode=t.renderMode),t.viewMode&&(s.gsView.viewMode=t.viewMode),t.ucsIconSetting&&(s.gsView.ucsIconSetting=t.ucsIconSetting),t.ucsOrigin&&s.gsView.ucsOrigin.copy(t.ucsOrigin),t.ucsXAxis&&s.gsView.ucsXAxis.copy(t.ucsXAxis),t.ucsYAxis&&s.gsView.ucsYAxis.copy(t.ucsYAxis),t.orthographicType&&(s.gsView.orthographicType=t.orthographicType),t.shadePlotSetting&&(s.gsView.shadePlotSetting=t.shadePlotSetting),t.shadePlotObjectId&&(s.gsView.shadePlotObjectId=t.shadePlotObjectId),t.visualStyleObjectId&&(s.gsView.visualStyleObjectId=t.visualStyleObjectId),t.isDefaultLightingOn&&(s.gsView.isDefaultLightingOn=t.isDefaultLightingOn),t.defaultLightingType&&(s.gsView.defaultLightingType=t.defaultLightingType),t.brightness&&(s.gsView.brightness=t.brightness),t.contrast&&(s.gsView.contrast=t.contrast),t.ambientColor&&(s.gsView.ambientColor=t.ambientColor),n.tables.viewportTable.add(s)})}processBlockTables(e,n){e.tables.BLOCK_RECORD.entries.forEach(t=>{let s=n.tables.blockTable.getAt(t.name);s||(s=new o.AcDbBlockTableRecord,s.objectId=t.handle.toString(),s.name=t.name,s.ownerId=t.ownerHandle.toString(),s.origin.copy(t.basePoint),s.layoutId=t.layout.toString(),n.tables.blockTable.add(s)),!s.isModelSapce&&!s.isPaperSapce&&t.entities&&t.entities.length>0&&this.processEntitiesInBlock(t.entities,s)})}processBlocks(e,n){}async processEntitiesInBlock(e,n){const r=new A,t=e.length;for(let s=0;s<t;s++){const i=e[s],l=r.convert(i);l&&n.appendEntity(l)}}async processEntities(e,n,r,t,s){const i=new A,l=e.entities,c=l.length,p=new o.AcDbBatchProcessing(c,100-t.value,r),a=n.tables.blockTable.modelSpace;await p.processChunk(async(h,d)=>{for(let f=h;f<d;f++){const u=l[f],g=i.convert(u);g&&a.appendEntity(g)}if(s){let f=t.value+d/c*(100-t.value);f>100&&(f=100),await s(f,"ENTITY","IN-PROGRESS")}})}processHeader(e,n){const r=e.header;n.cecolor.colorIndex=r.CECOLOR||256,n.angBase=r.ANGBASE??0,n.angDir=r.ANGDIR??0,n.aunits=r.AUNITS??0,n.extmax=r.EXTMAX,n.extmin=r.EXTMIN,n.insunits=r.INSUNITS??1,n.pdmode=r.PDMODE??0,n.pdsize=r.PDSIZE??0}processCommonTableEntryAttrs(e,n){n.name=e.name,n.objectId=e.handle.toString(),n.ownerId=e.ownerHandle.toString()}processObjects(e,n){this.processLayouts(e,n),this.processImageDefs(e,n)}processLayouts(e,n){const r=n.dictionaries.layouts;e.objects.LAYOUT.forEach(s=>{const i=new o.AcDbLayout;i.layoutName=s.layoutName,i.tabOrder=s.tabOrder;const l=n.tables.blockTable.newIterator();i.objectId=s.handle.toString();for(const c of l)if(c.layoutId===i.objectId){i.blockTableRecordId=c.objectId;break}i.limits.min.copy(s.minLimit),i.limits.max.copy(s.maxLimit),i.extents.min.copy(s.minExtent),i.extents.max.copy(s.maxExtent),this.processCommonObjectAttrs(s,i),r.setAt(i.layoutName,i)})}processImageDefs(e,n){const r=n.dictionaries.imageDefs;e.objects.IMAGEDEF.forEach(s=>{const i=new o.AcDbRasterImageDef;i.sourceFileName=s.fileName,this.processCommonObjectAttrs(s,i),r.setAt(i.objectId,i)})}processCommonObjectAttrs(e,n){n.objectId=e.handle.toString(),n.ownerId=e.ownerHandle.toString()}}D.AcDbLibreDwgConverter=I,Object.defineProperty(D,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(f,r){typeof exports=="object"&&typeof module<"u"?r(exports,require("@mlightcad/data-model")):typeof define=="function"&&define.amd?define(["exports","@mlightcad/data-model"],r):(f=typeof globalThis<"u"?globalThis:f||self,r(f["libredwg-converter"]={},f.dataModel))})(this,function(f,r){"use strict";class b{convert(e){const n=this.createEntity(e);return n&&this.processCommonAttrs(e,n),n}createEntity(e){return e.type=="ARC"?this.convertArc(e):e.type=="CIRCLE"?this.convertCirle(e):e.type=="DIMENSION"?this.convertDimension(e):e.type=="ELLIPSE"?this.convertEllipse(e):e.type=="HATCH"?this.convertHatch(e):e.type=="IMAGE"?this.convertImage(e):e.type=="LEADER"?this.convertLeader(e):e.type=="LINE"?this.convertLine(e):e.type=="LWPOLYLINE"?this.convertLWPolyline(e):e.type=="MTEXT"?this.convertMText(e):e.type=="POINT"?this.convertPoint(e):e.type=="POLYLINE"?this.convertPolyline(e):e.type=="RAY"?this.convertRay(e):e.type=="SPLINE"?this.convertSpline(e):e.type=="ACAD_TABLE"?this.convertTable(e):e.type=="TEXT"?this.convertText(e):e.type=="SOLID"?this.convertSolid(e):e.type=="VIEWPORT"?this.convertViewport(e):e.type=="XLINE"?this.convertXline(e):e.type=="INSERT"?this.convertBlockReference(e):null}convertArc(e){return new r.AcDbArc(e.center,e.radius,e.startAngle,e.endAngle)}convertCirle(e){return new r.AcDbCircle(e.center,e.radius)}convertEllipse(e){const n=new r.AcGeVector3d(e.majorAxisEndPoint),o=n.length();return new r.AcDbEllipse(e.center,r.AcGeVector3d.Z_AXIS,n,o,o*e.axisRatio,e.startAngle,e.endAngle)}convertLine(e){const n=e.startPoint,o=e.endPoint;return new r.AcDbLine(new r.AcGePoint3d(n.x,n.y,n.z),new r.AcGePoint3d(o.x,o.y,o.z))}convertSpline(e){return e.numberOfControlPoints>0&&e.numberOfKnots>0?new r.AcDbSpline(e.controlPoints,e.knots,e.weights,e.degree,!!(e.flag&1)):e.numberOfFitPoints>0?new r.AcDbSpline(e.fitPoints,"Uniform",e.degree,!!(e.flag&1)):null}convertPoint(e){const n=new r.AcDbPoint;return n.position=e.position,n}convertSolid(e){const n=new r.AcDbTrace;return n.setPointAt(0,{...e.corner1,z:0}),n.setPointAt(1,{...e.corner2,z:0}),n.setPointAt(2,{...e.corner3,z:0}),n.setPointAt(3,e.corner4?{...e.corner4,z:0}:{...e.corner3,z:0}),n.thickness=e.thickness,n}convertLWPolyline(e){const n=new r.AcDbPolyline;return n.closed=!!(e.flag&512),e.vertices.forEach((o,t)=>{n.addVertexAt(t,new r.AcGePoint2d(o.x,o.y),o.bulge,o.startWidth,o.endWidth)}),n}convertPolyline(e){const n=new r.AcDbPolyline;return n.closed=!!(e.flag&1),e.vertices.forEach((o,t)=>{n.addVertexAt(t,new r.AcGePoint2d(o.x,o.y),o.bulge,o.startWidth,o.endWidth)}),n}convertHatch(e){var t;const n=new r.AcDbHatch;return(t=e.definitionLines)==null||t.forEach(s=>{n.definitionLines.push({angle:s.angle,origin:s.base,delta:s.offset,dashPattern:s.numberOfDashLengths>0?s.dashLengths:[]})}),n.hatchStyle=e.hatchStyle,n.patternName=e.patternName,n.patternType=e.patternType,n.patternAngle=e.patternAngle==null?0:e.patternAngle,n.patternScale=e.patternScale==null?0:e.patternScale,e.boundaryPaths.forEach(s=>{if(s.boundaryPathTypeFlag&2){const l=s,c=new r.AcGePolyline2d;c.closed=l.isClosed,l.vertices.forEach((p,a)=>{c.addVertexAt(a,{x:p.x,y:p.y,bulge:p.bulge})}),n.add(c)}else{const l=s,c=new r.AcGeLoop2d;l.edges.forEach(p=>{if(p.type==1){const a=p;c.add(new r.AcGeLine2d(a.start,a.end))}else if(p.type==2){const a=p;c.add(new r.AcGeCircArc2d(a.center,a.radius,a.startAngle,a.endAngle,!a.isCCW))}else if(p.type==3){const a=p;new r.AcGeVector2d().subVectors(a.end,a.center);const u=Math.sqrt(Math.pow(a.end.x,2)+Math.pow(a.end.y,2)),D=u*a.lengthOfMinorAxis;let d=a.startAngle,g=a.endAngle;const A=Math.atan2(a.end.y,a.end.x);a.isCCW||(d=Math.PI*2-d,g=Math.PI*2-g),c.add(new r.AcGeEllipseArc2d({...a.center,z:0},u,D,d,g,!a.isCCW,A))}else if(p.type==4){const a=p;if(a.numberOfControlPoints>0&&a.numberOfKnots>0){const h=a.controlPoints.map(d=>({x:d.x,y:d.y,z:0}));let u=!0;const D=a.controlPoints.map(d=>(d.weight==null&&(u=!1),d.weight||1));c.add(new r.AcGeSpline3d(h,a.knots,u?D:void 0))}else if(a.numberOfFitData>0){const h=a.fitDatum.map(u=>({x:u.x,y:u.y,z:0}));c.add(new r.AcGeSpline3d(h,"Uniform"))}}}),n.add(c)}}),n}convertTable(e){const n=new r.AcDbTable(e.name,e.rowCount,e.columnCount);return n.attachmentPoint=e.attachmentPoint,n.position.copy(e.startPoint),e.columnWidthArr.forEach((o,t)=>n.setColumnWidth(t,o)),e.rowHeightArr.forEach((o,t)=>n.setRowHeight(t,o)),e.cells.forEach((o,t)=>{n.setCell(t,o)}),n}convertText(e){const n=new r.AcDbText;return n.textString=e.text,n.styleName=e.styleName,n.height=e.textHeight,n.position.copy(e.startPoint),n.rotation=e.rotation,n.oblique=e.obliqueAngle??0,n.thickness=e.thickness,n.horizontalMode=e.halign,n.verticalMode=e.valign,n.widthFactor=e.xScale??1,n}convertMText(e){const n=new r.AcDbMText;return n.contents=e.text,e.styleName!=null&&(n.styleName=e.styleName),n.height=e.textHeight,n.width=e.rectWidth,n.rotation=e.rotation||0,n.location=e.insertionPoint,n.attachmentPoint=e.attachmentPoint,e.direction&&(n.direction=new r.AcGeVector3d(e.direction)),n.drawingDirection=e.drawingDirection,n}convertLeader(e){const n=new r.AcDbLeader;return e.vertices.forEach(o=>{n.appendVertex(o)}),n.hasArrowHead=e.isArrowheadEnabled,n.hasHookLine=e.isHooklineExists,n.isSplined=e.isSpline,n.dimensionStyle=e.styleName,n.annoType=e.leaderCreationFlag,n}convertDimension(e){if(e.subclassMarker=="AcDbAlignedDimension"||e.subclassMarker=="AcDbRotatedDimension"){const n=e,o=new r.AcDbAlignedDimension(n.subDefinitionPoint1,n.subDefinitionPoint2,n.definitionPoint);return o.rotation=n.rotationAngle,this.processDimensionCommonAttrs(e,o),o}else if(e.subclassMarker=="AcDb3PointAngularDimension"){const n=e,o=new r.AcDb3PointAngularDimension(n.centerPoint,n.subDefinitionPoint1,n.subDefinitionPoint2,n.definitionPoint);return this.processDimensionCommonAttrs(e,o),o}else if(e.subclassMarker=="AcDbOrdinateDimension"){const n=e,o=new r.AcDbOrdinateDimension(n.subDefinitionPoint1,n.subDefinitionPoint2);return this.processDimensionCommonAttrs(e,o),o}else if(e.subclassMarker=="AcDbRadialDimension"){const n=e,o=new r.AcDbRadialDimension(n.definitionPoint,n.centerPoint,n.leaderLength);return this.processDimensionCommonAttrs(e,o),o}else if(e.subclassMarker=="AcDbDiametricDimension"){const n=e,o=new r.AcDbDiametricDimension(n.definitionPoint,n.centerPoint,n.leaderLength);return this.processDimensionCommonAttrs(e,o),o}return null}processImage(e,n){n.position.copy(e.position),n.brightness=e.brightness,n.contrast=e.contrast,n.fade=e.fade,n.imageDefId=e.imageDefHandle.toString(),n.isClipped=(e.flags|4)>0,n.isImageShown=(e.flags|3)>0,n.isImageTransparent=(e.flags|8)>0,e.clippingBoundaryPath.forEach(o=>{n.clipBoundary.push(new r.AcGePoint2d(o))}),n.clipBoundaryType=e.clippingBoundaryType,n.width=Math.sqrt(e.uPixel.x**2+e.uPixel.y**2+e.uPixel.z**2)*e.imageSize.x,n.height=Math.sqrt(e.vPixel.x**2+e.vPixel.y**2+e.vPixel.z**2)*e.imageSize.y,n.rotation=Math.atan2(e.uPixel.y,e.uPixel.x)}convertImage(e){const n=new r.AcDbRasterImage;return this.processImage(e,n),n}convertViewport(e){const n=new r.AcDbViewport;return n.number=e.viewportId,n.centerPoint.copy(e.viewportCenter),n.height=e.height,n.width=e.width,n.viewCenter.copy(e.displayCenter),n.viewHeight=e.viewHeight,n}convertRay(e){const n=new r.AcDbRay;return n.basePoint.copy(e.firstPoint),n.unitDir.copy(e.unitDirection),n}convertXline(e){const n=new r.AcDbXline;return n.basePoint.copy(e.firstPoint),n.unitDir.copy(e.unitDirection),n}convertBlockReference(e){const n=new r.AcDbBlockReference(e.name);return e.insertionPoint&&n.position.copy(e.insertionPoint),n.scaleFactors.x=e.xScale,n.scaleFactors.y=e.yScale,n.scaleFactors.z=e.zScale,n.rotation=e.rotation,n.normal.copy(e.extrusionDirection),n}processDimensionCommonAttrs(e,n){n.dimBlockId=e.name,n.textPosition.copy(e.textPoint),n.textRotation=e.textRotation||0,e.textLineSpacingFactor&&(n.textLineSpacingFactor=e.textLineSpacingFactor),e.textLineSpacingStyle&&(n.textLineSpacingStyle=e.textLineSpacingStyle),n.dimensionStyleName=e.styleName,n.dimensionText=e.text||"",n.measurement=e.measurement}processCommonAttrs(e,n){n.layer=e.layer,n.objectId=e.handle.toString(),n.ownerId=e.ownerBlockRecordSoftId.toString(),e.lineType!=null&&(n.lineType=e.lineType),e.lineweight!=null&&(n.lineWeight=e.lineweight),e.lineTypeScale!=null&&(n.linetypeScale=e.lineTypeScale),e.color!=null&&(n.color.color=e.color),e.colorIndex!=null&&(n.color.colorIndex=e.colorIndex),e.colorName!=null&&(n.color.colorName=e.colorName),e.isVisible!=null&&(n.visibility=e.isVisible),e.transparency!=null&&(n.transparency=e.transparency)}}class I extends r.AcDbDatabaseConverter{constructor(e={}){super(e),e.useWorker=!0,e.parserWorkerUrl||(e.parserWorkerUrl="/assets/libredwg-parser-worker.js")}async parse(e){if(this.config.useWorker&&this.config.parserWorkerUrl){const n=r.createWorkerApi({workerUrl:this.config.parserWorkerUrl,maxConcurrentWorkers:1}),o=await n.execute(e);return n.destroy(),o.data}else throw new Error("dwg converter can run in web worker only!")}getFonts(e){const n=new Map;e.tables.BLOCK_RECORD.entries.forEach(i=>{n.set(i.name,i)});const o=new Map,t=i=>{if(i){const l=i.lastIndexOf(".");return l>=0?i.substring(0,l).toLowerCase():i.toLowerCase()}};e.tables.STYLE.entries.forEach(i=>{const l=[];let c=t(i.font);c&&l.push(c),c=t(i.bigFont),c&&l.push(c),o.set(i.name,l)});const s=new Set;return this.getFontsInBlock(e.entities,n,o,s),Array.from(s)}getFontsInBlock(e,n,o,t){const s=/\\f(.*?)\|/g;e.forEach(i=>{if(i.type=="MTEXT"){const l=i;[...l.text.matchAll(s)].forEach(p=>{t.add(p[1].toLowerCase())});const c=o.get(l.styleName);c==null||c.forEach(p=>t.add(p))}else if(i.type=="TEXT"){const l=i,c=o.get(l.styleName);c==null||c.forEach(p=>t.add(p))}else if(i.type=="INSERT"){const l=i,c=n.get(l.name);c&&this.getFontsInBlock(c.entities,n,o,t)}})}processLineTypes(e,n){e.tables.LTYPE.entries.forEach(t=>{const s={name:t.name,description:t.description,standardFlag:t.standardFlag,totalPatternLength:t.totalPatternLength,pattern:t.pattern},i=new r.AcDbLinetypeTableRecord(s);this.processCommonTableEntryAttrs(t,i),i.name=t.name,n.tables.linetypeTable.add(i)})}processTextStyles(e,n){e.tables.STYLE.entries.forEach(t=>{const s=new r.AcDbTextStyleTableRecord(t);this.processCommonTableEntryAttrs(t,s),n.tables.textStyleTable.add(s)})}processDimStyles(e,n){e.tables.DIMSTYLE.entries.forEach(t=>{const s={name:t.name,ownerId:t.ownerHandle.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,dimtmove:t.DIMTMOVE||0,dimjust:t.DIMJUST,dimsd1:t.DIMSD1,dimsd2:t.DIMSD2,dimtolj:t.DIMTOLJ,dimtzin:t.DIMTZIN,dimaltz:t.DIMALTZ,dimalttz:t.DIMALTTZ,dimfit:t.DIMFIT||0,dimupt:t.DIMUPT,dimatfit:t.DIMATFIT,dimtxsty:"Standard",dimldrblk:"",dimblk:t.DIMBLK||"",dimblk1:t.DIMBLK1||"",dimblk2:t.DIMBLK2||"",dimlwd:t.DIMLWD,dimlwe:t.DIMLWE},i=new r.AcDbDimStyleTableRecord(s);this.processCommonTableEntryAttrs(t,i),n.tables.dimStyleTable.add(i)})}processLayers(e,n){e.tables.LAYER.entries.forEach(t=>{const s=new r.AcCmColor;s.colorIndex=t.colorIndex;const i=new r.AcDbLayerTableRecord({name:t.name,standardFlags:t.standardFlag,linetype:t.lineType,lineWeight:t.lineweight,isOff:t.off,color:s,isPlottable:t.plotFlag!=0});this.processCommonTableEntryAttrs(t,i),n.tables.layerTable.add(i)})}processViewports(e,n){e.tables.VPORT.entries.forEach(t=>{const s=new r.AcDbViewportTableRecord;this.processCommonTableEntryAttrs(t,s),t.circleSides&&(s.circleSides=t.circleSides),s.standardFlag=t.standardFlag,s.center.copy(t.center),s.lowerLeftCorner.copy(t.lowerLeftCorner),s.upperRightCorner.copy(t.upperRightCorner),t.snapBasePoint&&s.snapBase.copy(t.snapBasePoint),t.snapRotationAngle&&(s.snapAngle=t.snapRotationAngle),t.snapSpacing&&s.snapIncrements.copy(t.snapSpacing),t.majorGridLines&&(s.gridMajor=t.majorGridLines),t.gridSpacing&&s.gridIncrements.copy(t.gridSpacing),t.backgroundObjectId&&(s.backgroundObjectId=t.backgroundObjectId),s.gsView.center.copy(t.center),s.gsView.viewDirectionFromTarget.copy(t.viewDirectionFromTarget),s.gsView.viewTarget.copy(t.viewTarget),t.lensLength&&(s.gsView.lensLength=t.lensLength),t.frontClippingPlane&&(s.gsView.frontClippingPlane=t.frontClippingPlane),t.backClippingPlane&&(s.gsView.backClippingPlane=t.backClippingPlane),t.viewHeight&&(s.gsView.viewHeight=t.viewHeight),t.viewTwistAngle&&(s.gsView.viewTwistAngle=t.viewTwistAngle),t.frozenLayers&&(s.gsView.frozenLayers=t.frozenLayers),t.styleSheet&&(s.gsView.styleSheet=t.styleSheet),t.renderMode&&(s.gsView.renderMode=t.renderMode),t.viewMode&&(s.gsView.viewMode=t.viewMode),t.ucsIconSetting&&(s.gsView.ucsIconSetting=t.ucsIconSetting),t.ucsOrigin&&s.gsView.ucsOrigin.copy(t.ucsOrigin),t.ucsXAxis&&s.gsView.ucsXAxis.copy(t.ucsXAxis),t.ucsYAxis&&s.gsView.ucsYAxis.copy(t.ucsYAxis),t.orthographicType&&(s.gsView.orthographicType=t.orthographicType),t.shadePlotSetting&&(s.gsView.shadePlotSetting=t.shadePlotSetting),t.shadePlotObjectId&&(s.gsView.shadePlotObjectId=t.shadePlotObjectId),t.visualStyleObjectId&&(s.gsView.visualStyleObjectId=t.visualStyleObjectId),t.isDefaultLightingOn&&(s.gsView.isDefaultLightingOn=t.isDefaultLightingOn),t.defaultLightingType&&(s.gsView.defaultLightingType=t.defaultLightingType),t.brightness&&(s.gsView.brightness=t.brightness),t.contrast&&(s.gsView.contrast=t.contrast),t.ambientColor&&(s.gsView.ambientColor=t.ambientColor),n.tables.viewportTable.add(s)})}processBlockTables(e,n){e.tables.BLOCK_RECORD.entries.forEach(t=>{let s=n.tables.blockTable.getAt(t.name);s||(s=new r.AcDbBlockTableRecord,s.objectId=t.handle.toString(),s.name=t.name,s.ownerId=t.ownerHandle.toString(),s.origin.copy(t.basePoint),s.layoutId=t.layout.toString(),n.tables.blockTable.add(s)),!s.isModelSapce&&!s.isPaperSapce&&t.entities&&t.entities.length>0&&this.processEntitiesInBlock(t.entities,s)})}processBlocks(e,n){}async processEntitiesInBlock(e,n){const o=new b,t=e.length,s=[];for(let i=0;i<t;i++){const l=e[i],c=o.convert(l);c&&s.push(c)}n.appendEntity(s)}async processEntities(e,n,o,t,s){const i=new b;let l=e.entities;const c=l.length,p=new r.AcDbBatchProcessing(c,100-t.value,o);this.config.convertByEntityType&&(l=this.groupAndFlattenByType(l));const a=n.tables.blockTable.modelSpace;await p.processChunk(async(h,u)=>{const D=[];for(let d=h;d<u;d++){const g=l[d],A=i.convert(g);A&&D.push(A)}if(a.appendEntity(D),s){let d=t.value+u/c*(100-t.value);d>100&&(d=100),await s(d,"ENTITY","IN-PROGRESS")}})}processHeader(e,n){const o=e.header;n.cecolor.colorIndex=o.CECOLOR||256,n.angBase=o.ANGBASE??0,n.angDir=o.ANGDIR??0,n.aunits=o.AUNITS??0,n.extmax=o.EXTMAX,n.extmin=o.EXTMIN,n.insunits=o.INSUNITS??1,n.pdmode=o.PDMODE??0,n.pdsize=o.PDSIZE??0}processCommonTableEntryAttrs(e,n){n.name=e.name,n.objectId=e.handle.toString(),n.ownerId=e.ownerHandle.toString()}processObjects(e,n){this.processLayouts(e,n),this.processImageDefs(e,n)}processLayouts(e,n){const o=n.dictionaries.layouts;e.objects.LAYOUT.forEach(s=>{const i=new r.AcDbLayout;i.layoutName=s.layoutName,i.tabOrder=s.tabOrder;const l=n.tables.blockTable.newIterator();i.objectId=s.handle.toString();for(const c of l)if(c.layoutId===i.objectId){i.blockTableRecordId=c.objectId;break}i.limits.min.copy(s.minLimit),i.limits.max.copy(s.maxLimit),i.extents.min.copy(s.minExtent),i.extents.max.copy(s.maxExtent),this.processCommonObjectAttrs(s,i),o.setAt(i.layoutName,i)})}processImageDefs(e,n){const o=n.dictionaries.imageDefs;e.objects.IMAGEDEF.forEach(s=>{const i=new r.AcDbRasterImageDef;i.sourceFileName=s.fileName,this.processCommonObjectAttrs(s,i),o.setAt(i.objectId,i)})}processCommonObjectAttrs(e,n){n.objectId=e.handle.toString(),n.ownerId=e.ownerHandle.toString()}groupAndFlattenByType(e){const n={},o=[];for(const t of e)n[t.type]||(n[t.type]=[],o.push(t.type)),n[t.type].push(t);return o.flatMap(t=>n[t])}}f.AcDbLibreDwgConverter=I,Object.defineProperty(f,Symbol.toStringTag,{value:"Module"})});
|