@mlightcad/libredwg-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 +21 -0
- package/README.md +53 -0
- package/dist/libredwg-converter.js +537 -0
- package/dist/libredwg-converter.umd.cjs +1 -0
- package/lib/AcDbEntitiyConverter.d.ts +35 -0
- package/lib/AcDbEntitiyConverter.d.ts.map +1 -0
- package/lib/AcDbEntitiyConverter.js +477 -0
- package/lib/AcDbEntitiyConverter.js.map +1 -0
- package/lib/AcDbLibreDwgConverter.d.ts +46 -0
- package/lib/AcDbLibreDwgConverter.d.ts.map +1 -0
- package/lib/AcDbLibreDwgConverter.js +547 -0
- package/lib/AcDbLibreDwgConverter.js.map +1 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +2 -0
- package/lib/index.js.map +1 -0
- package/package.json +44 -0
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/libredwg-converter
|
2
|
+
|
3
|
+
The `libredwg-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 [LibreDWG](https://www.gnu.org/software/libredwg/) 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 LibreDWG compiled to WASM for fast, browser-compatible parsing
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
```bash
|
18
|
+
npm install @mlightcad/libredwg-converter
|
19
|
+
```
|
20
|
+
|
21
|
+
> **Peer dependencies:**
|
22
|
+
> - `@mlightcad/data-model`
|
23
|
+
> - `@mlightcad/libredwg-web`
|
24
|
+
|
25
|
+
## Usage Example
|
26
|
+
|
27
|
+
```typescript
|
28
|
+
import { AcDbDatabaseConverterManager, AcDbFileType } from '@mlightcad/data-model';
|
29
|
+
import { AcDbLibreDwgConverter } from '@mlightcad/libredwg-converter';
|
30
|
+
|
31
|
+
// WASM module loading (async)
|
32
|
+
import('@mlightcad/libredwg-web/wasm/libredwg-web').then(libredwgModule => {
|
33
|
+
const dwgConverter = new AcDbLibreDwgConverter(libredwgModule);
|
34
|
+
AcDbDatabaseConverterManager.instance.register(AcDbFileType.DWG, dwgConverter);
|
35
|
+
});
|
36
|
+
```
|
37
|
+
|
38
|
+
## API
|
39
|
+
|
40
|
+
- **AcDbLibreDwgConverter**: Main converter class for DWG files (extends `AcDbDatabaseConverter`)
|
41
|
+
|
42
|
+
## Dependencies
|
43
|
+
|
44
|
+
- **@mlightcad/data-model**: Drawing database and entity definitions
|
45
|
+
- **@mlightcad/libredwg-web**: WASM wrapper for LibreDWG
|
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,537 @@
|
|
1
|
+
import { AcDbArc as M, AcDbCircle as P, AcGeVector3d as h, AcDbEllipse as E, AcDbLine as L, AcGePoint3d as A, AcDbSpline as w, AcDbPoint as C, AcDbTrace as v, AcDbPolyline as b, AcGePoint2d as u, AcDbHatch as x, AcGePolyline2d as O, AcGeLoop2d as R, AcGeLine2d as N, AcGeCircArc2d as V, AcGeVector2d as m, AcGeEllipseArc2d as k, AcGeSpline3d as I, AcDbTable as F, AcDbText as B, AcDbMText as j, AcDbLeader as z, AcDbAlignedDimension as H, AcDb3PointAngularDimension as W, AcDbOrdinateDimension as G, AcDbRadialDimension as X, AcDbDiametricDimension as Y, AcDbRasterImage as U, AcDbViewport as _, AcDbRay as Z, AcDbXline as K, AcDbBlockReference as q, AcDbDatabaseConverter as J, AcDbLinetypeTableRecord as Q, AcDbTextStyleTableRecord as $, AcDbDimStyleTableRecord as ee, AcCmColor as ne, AcDbLayerTableRecord as te, AcDbViewportTableRecord as oe, AcDbBlockTableRecord as se, AcDbBatchProcessing as re, AcDbLayout as ie, AcDbRasterImageDef as ce } from "@mlightcad/data-model";
|
2
|
+
import { LibreDwg as ae, Dwg_File_Type as le } from "@mlightcad/libredwg-web";
|
3
|
+
class T {
|
4
|
+
convert(e) {
|
5
|
+
const n = this.createEntity(e);
|
6
|
+
return n && this.processCommonAttrs(e, n), n;
|
7
|
+
}
|
8
|
+
/**
|
9
|
+
* Create the corresponding drawing database entity from data in dxf format
|
10
|
+
* @param entity Input entity data in dxf format
|
11
|
+
* @returns Return the converted drawing database entity
|
12
|
+
*/
|
13
|
+
createEntity(e) {
|
14
|
+
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;
|
15
|
+
}
|
16
|
+
convertArc(e) {
|
17
|
+
return new M(
|
18
|
+
e.center,
|
19
|
+
e.radius,
|
20
|
+
e.startAngle,
|
21
|
+
e.endAngle
|
22
|
+
);
|
23
|
+
}
|
24
|
+
convertCirle(e) {
|
25
|
+
return new P(e.center, e.radius);
|
26
|
+
}
|
27
|
+
convertEllipse(e) {
|
28
|
+
const n = new h(e.majorAxisEndPoint), s = n.length();
|
29
|
+
return new E(
|
30
|
+
e.center,
|
31
|
+
h.Z_AXIS,
|
32
|
+
n,
|
33
|
+
s,
|
34
|
+
s * e.axisRatio,
|
35
|
+
e.startAngle,
|
36
|
+
e.endAngle
|
37
|
+
);
|
38
|
+
}
|
39
|
+
convertLine(e) {
|
40
|
+
const n = e.startPoint, s = e.endPoint;
|
41
|
+
return new L(
|
42
|
+
new A(n.x, n.y, n.z),
|
43
|
+
new A(s.x, s.y, s.z)
|
44
|
+
);
|
45
|
+
}
|
46
|
+
convertSpline(e) {
|
47
|
+
if (e.numberOfControlPoints > 0 && e.numberOfKnots > 0) {
|
48
|
+
const n = new w(
|
49
|
+
e.controlPoints,
|
50
|
+
e.knots,
|
51
|
+
e.weights
|
52
|
+
);
|
53
|
+
return n.closed = !!(e.flag & 1), n;
|
54
|
+
} else if (e.numberOfFitPoints > 0) {
|
55
|
+
const n = new w(e.fitPoints, "Uniform");
|
56
|
+
return n.closed = !!(e.flag & 1), n;
|
57
|
+
}
|
58
|
+
return null;
|
59
|
+
}
|
60
|
+
convertPoint(e) {
|
61
|
+
const n = new C();
|
62
|
+
return n.position = e.position, n;
|
63
|
+
}
|
64
|
+
convertSolid(e) {
|
65
|
+
const n = new v();
|
66
|
+
return n.setPointAt(0, { ...e.corner1, z: 0 }), n.setPointAt(1, { ...e.corner2, z: 0 }), n.setPointAt(2, { ...e.corner3, z: 0 }), n.setPointAt(
|
67
|
+
3,
|
68
|
+
e.corner4 ? { ...e.corner4, z: 0 } : { ...e.corner3, z: 0 }
|
69
|
+
), n.thickness = e.thickness, n;
|
70
|
+
}
|
71
|
+
convertLWPolyline(e) {
|
72
|
+
const n = new b();
|
73
|
+
return n.closed = !!(e.flag & 512), e.vertices.forEach((s, t) => {
|
74
|
+
n.addVertexAt(
|
75
|
+
t,
|
76
|
+
new u(s.x, s.y),
|
77
|
+
s.bulge,
|
78
|
+
s.startWidth,
|
79
|
+
s.endWidth
|
80
|
+
);
|
81
|
+
}), n;
|
82
|
+
}
|
83
|
+
convertPolyline(e) {
|
84
|
+
const n = new b();
|
85
|
+
return n.closed = !!(e.flag & 1), e.vertices.forEach((s, t) => {
|
86
|
+
n.addVertexAt(
|
87
|
+
t,
|
88
|
+
new u(s.x, s.y),
|
89
|
+
s.bulge,
|
90
|
+
s.startWidth,
|
91
|
+
s.endWidth
|
92
|
+
);
|
93
|
+
}), n;
|
94
|
+
}
|
95
|
+
convertHatch(e) {
|
96
|
+
var t;
|
97
|
+
const n = new x();
|
98
|
+
return (t = e.definitionLines) == null || t.forEach((o) => {
|
99
|
+
n.definitionLines.push({
|
100
|
+
angle: o.angle,
|
101
|
+
origin: o.base,
|
102
|
+
delta: o.offset,
|
103
|
+
dashPattern: o.numberOfDashLengths > 0 ? o.dashLengths : []
|
104
|
+
});
|
105
|
+
}), 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((o) => {
|
106
|
+
if (o.boundaryPathTypeFlag & 2) {
|
107
|
+
const a = o, c = new O();
|
108
|
+
c.closed = a.isClosed, a.vertices.forEach((l, i) => {
|
109
|
+
c.addVertexAt(i, {
|
110
|
+
x: l.x,
|
111
|
+
y: l.y,
|
112
|
+
bulge: l.bulge
|
113
|
+
});
|
114
|
+
}), n.add(c);
|
115
|
+
} else {
|
116
|
+
const a = o, c = new R();
|
117
|
+
a.edges.forEach((l) => {
|
118
|
+
if (l.type == 1) {
|
119
|
+
const i = l;
|
120
|
+
c.add(new N(i.start, i.end));
|
121
|
+
} else if (l.type == 2) {
|
122
|
+
const i = l;
|
123
|
+
c.add(
|
124
|
+
new V(
|
125
|
+
i.center,
|
126
|
+
i.radius,
|
127
|
+
i.startAngle,
|
128
|
+
i.endAngle,
|
129
|
+
!i.isCCW
|
130
|
+
)
|
131
|
+
);
|
132
|
+
} else if (l.type == 3) {
|
133
|
+
const i = l;
|
134
|
+
new m().subVectors(i.end, i.center);
|
135
|
+
const d = Math.sqrt(
|
136
|
+
Math.pow(i.end.x, 2) + Math.pow(i.end.y, 2)
|
137
|
+
), p = d * i.lengthOfMinorAxis;
|
138
|
+
let D = i.startAngle, g = i.endAngle;
|
139
|
+
const S = Math.atan2(i.end.y, i.end.x);
|
140
|
+
i.isCCW || (D = Math.PI * 2 - D, g = Math.PI * 2 - g), c.add(
|
141
|
+
new k(
|
142
|
+
{ ...i.center, z: 0 },
|
143
|
+
d,
|
144
|
+
p,
|
145
|
+
D,
|
146
|
+
g,
|
147
|
+
!i.isCCW,
|
148
|
+
S
|
149
|
+
)
|
150
|
+
);
|
151
|
+
} else if (l.type == 4) {
|
152
|
+
const i = l;
|
153
|
+
if (i.numberOfControlPoints > 0 && i.numberOfKnots > 0) {
|
154
|
+
const f = i.controlPoints.map(
|
155
|
+
(D) => ({
|
156
|
+
x: D.x,
|
157
|
+
y: D.y,
|
158
|
+
z: 0
|
159
|
+
})
|
160
|
+
);
|
161
|
+
let d = !0;
|
162
|
+
const p = i.controlPoints.map((D) => (D.weight == null && (d = !1), D.weight || 1));
|
163
|
+
c.add(
|
164
|
+
new I(
|
165
|
+
f,
|
166
|
+
i.knots,
|
167
|
+
d ? p : void 0
|
168
|
+
)
|
169
|
+
);
|
170
|
+
} else if (i.numberOfFitData > 0) {
|
171
|
+
const f = i.fitDatum.map((d) => ({
|
172
|
+
x: d.x,
|
173
|
+
y: d.y,
|
174
|
+
z: 0
|
175
|
+
}));
|
176
|
+
c.add(new I(f, "Uniform"));
|
177
|
+
}
|
178
|
+
}
|
179
|
+
}), n.add(c);
|
180
|
+
}
|
181
|
+
}), n;
|
182
|
+
}
|
183
|
+
convertTable(e) {
|
184
|
+
const n = new F(
|
185
|
+
e.name,
|
186
|
+
e.rowCount,
|
187
|
+
e.columnCount
|
188
|
+
);
|
189
|
+
return n.attachmentPoint = e.attachmentPoint, n.position.copy(e.startPoint), e.columnWidthArr.forEach(
|
190
|
+
(s, t) => n.setColumnWidth(t, s)
|
191
|
+
), e.rowHeightArr.forEach(
|
192
|
+
(s, t) => n.setRowHeight(t, s)
|
193
|
+
), e.cells.forEach((s, t) => {
|
194
|
+
n.setCell(t, s);
|
195
|
+
}), n;
|
196
|
+
}
|
197
|
+
convertText(e) {
|
198
|
+
const n = new B();
|
199
|
+
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;
|
200
|
+
}
|
201
|
+
convertMText(e) {
|
202
|
+
const n = new j();
|
203
|
+
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 h(e.direction)), n.drawingDirection = e.drawingDirection, n;
|
204
|
+
}
|
205
|
+
convertLeader(e) {
|
206
|
+
const n = new z();
|
207
|
+
return e.vertices.forEach((s) => {
|
208
|
+
n.appendVertex(s);
|
209
|
+
}), n.hasArrowHead = e.isArrowheadEnabled, n.hasHookLine = e.isHooklineExists, n.isSplined = e.isSpline, n.dimensionStyle = e.styleName, n.annoType = e.leaderCreationFlag, n;
|
210
|
+
}
|
211
|
+
convertDimension(e) {
|
212
|
+
if (e.subclassMarker == "AcDbAlignedDimension" || e.subclassMarker == "AcDbRotatedDimension") {
|
213
|
+
const n = e, s = new H(
|
214
|
+
n.subDefinitionPoint1,
|
215
|
+
n.subDefinitionPoint2,
|
216
|
+
n.definitionPoint
|
217
|
+
);
|
218
|
+
return s.rotation = n.rotationAngle, this.processDimensionCommonAttrs(e, s), s;
|
219
|
+
} else if (e.subclassMarker == "AcDb3PointAngularDimension") {
|
220
|
+
const n = e, s = new W(
|
221
|
+
n.centerPoint,
|
222
|
+
n.subDefinitionPoint1,
|
223
|
+
n.subDefinitionPoint2,
|
224
|
+
n.definitionPoint
|
225
|
+
);
|
226
|
+
return this.processDimensionCommonAttrs(e, s), s;
|
227
|
+
} else if (e.subclassMarker == "AcDbOrdinateDimension") {
|
228
|
+
const n = e, s = new G(
|
229
|
+
n.subDefinitionPoint1,
|
230
|
+
n.subDefinitionPoint2
|
231
|
+
);
|
232
|
+
return this.processDimensionCommonAttrs(e, s), s;
|
233
|
+
} else if (e.subclassMarker == "AcDbRadialDimension") {
|
234
|
+
const n = e, s = new X(
|
235
|
+
n.definitionPoint,
|
236
|
+
n.centerPoint,
|
237
|
+
n.leaderLength
|
238
|
+
);
|
239
|
+
return this.processDimensionCommonAttrs(e, s), s;
|
240
|
+
} else if (e.subclassMarker == "AcDbDiametricDimension") {
|
241
|
+
const n = e, s = new Y(
|
242
|
+
n.definitionPoint,
|
243
|
+
n.centerPoint,
|
244
|
+
n.leaderLength
|
245
|
+
);
|
246
|
+
return this.processDimensionCommonAttrs(e, s), s;
|
247
|
+
}
|
248
|
+
return null;
|
249
|
+
}
|
250
|
+
processImage(e, n) {
|
251
|
+
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) => {
|
252
|
+
n.clipBoundary.push(new u(s));
|
253
|
+
}), n.clipBoundaryType = e.clippingBoundaryType, n.width = Math.sqrt(
|
254
|
+
e.uPixel.x ** 2 + e.uPixel.y ** 2 + e.uPixel.z ** 2
|
255
|
+
) * e.imageSize.x, n.height = Math.sqrt(
|
256
|
+
e.vPixel.x ** 2 + e.vPixel.y ** 2 + e.vPixel.z ** 2
|
257
|
+
) * e.imageSize.y, n.rotation = Math.atan2(e.uPixel.y, e.uPixel.x);
|
258
|
+
}
|
259
|
+
convertImage(e) {
|
260
|
+
const n = new U();
|
261
|
+
return this.processImage(e, n), n;
|
262
|
+
}
|
263
|
+
// private convertWipeout(wipeout: WipeoutEntity) {
|
264
|
+
// const dbWipeout = new AcDbWipeout()
|
265
|
+
// this.processImage(wipeout, dbWipeout)
|
266
|
+
// return dbWipeout
|
267
|
+
// }
|
268
|
+
convertViewport(e) {
|
269
|
+
const n = new _();
|
270
|
+
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;
|
271
|
+
}
|
272
|
+
convertRay(e) {
|
273
|
+
const n = new Z();
|
274
|
+
return n.basePoint.copy(e.firstPoint), n.unitDir.copy(e.unitDirection), n;
|
275
|
+
}
|
276
|
+
convertXline(e) {
|
277
|
+
const n = new K();
|
278
|
+
return n.basePoint.copy(e.firstPoint), n.unitDir.copy(e.unitDirection), n;
|
279
|
+
}
|
280
|
+
convertBlockReference(e) {
|
281
|
+
const n = new q(e.name);
|
282
|
+
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;
|
283
|
+
}
|
284
|
+
processDimensionCommonAttrs(e, n) {
|
285
|
+
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;
|
286
|
+
}
|
287
|
+
processCommonAttrs(e, n) {
|
288
|
+
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);
|
289
|
+
}
|
290
|
+
}
|
291
|
+
class De extends J {
|
292
|
+
constructor(e) {
|
293
|
+
super(), this.libredwg = ae.createByWasmInstance(e);
|
294
|
+
}
|
295
|
+
parse(e) {
|
296
|
+
if (this.libredwg == null)
|
297
|
+
throw new Error("libredwg is not loaded!");
|
298
|
+
const n = this.libredwg.dwg_read_data(e, le.DWG);
|
299
|
+
if (n == null)
|
300
|
+
throw new Error("Failed to read dwg data!");
|
301
|
+
const s = this.libredwg.convert(n);
|
302
|
+
return this.libredwg.dwg_free(n), s;
|
303
|
+
}
|
304
|
+
/**
|
305
|
+
* Gets all of fonts used by entities in model space and paper space
|
306
|
+
* @param dwg dwg database model
|
307
|
+
* @returns Returns all of fonts used by entities in model space and paper space
|
308
|
+
*/
|
309
|
+
getFonts(e) {
|
310
|
+
const n = /* @__PURE__ */ new Map();
|
311
|
+
e.tables.BLOCK_RECORD.entries.forEach((r) => {
|
312
|
+
n.set(r.name, r);
|
313
|
+
});
|
314
|
+
const s = /* @__PURE__ */ new Map(), t = (r) => {
|
315
|
+
if (r) {
|
316
|
+
const a = r.lastIndexOf(".");
|
317
|
+
return a >= 0 ? r.substring(0, a).toLowerCase() : r.toLowerCase();
|
318
|
+
}
|
319
|
+
};
|
320
|
+
e.tables.STYLE.entries.forEach((r) => {
|
321
|
+
const a = [];
|
322
|
+
let c = t(r.font);
|
323
|
+
c && a.push(c), c = t(r.bigFont), c && a.push(c), s.set(r.name, a);
|
324
|
+
});
|
325
|
+
const o = /* @__PURE__ */ new Set();
|
326
|
+
return this.getFontsInBlock(e.entities, n, s, o), Array.from(o);
|
327
|
+
}
|
328
|
+
/**
|
329
|
+
* Iterate entities in model space to get fonts used by text, mtext and insert entities
|
330
|
+
*/
|
331
|
+
getFontsInBlock(e, n, s, t) {
|
332
|
+
const o = /\\f(.*?)\|/g;
|
333
|
+
e.forEach((r) => {
|
334
|
+
if (r.type == "MTEXT") {
|
335
|
+
const a = r;
|
336
|
+
[...a.text.matchAll(o)].forEach((l) => {
|
337
|
+
t.add(l[1].toLowerCase());
|
338
|
+
});
|
339
|
+
const c = s.get(a.styleName);
|
340
|
+
c == null || c.forEach((l) => t.add(l));
|
341
|
+
} else if (r.type == "TEXT") {
|
342
|
+
const a = r, c = s.get(a.styleName);
|
343
|
+
c == null || c.forEach((l) => t.add(l));
|
344
|
+
} else if (r.type == "INSERT") {
|
345
|
+
const a = r, c = n.get(a.name);
|
346
|
+
c && this.getFontsInBlock(c.entities, n, s, t);
|
347
|
+
}
|
348
|
+
});
|
349
|
+
}
|
350
|
+
processLineTypes(e, n) {
|
351
|
+
e.tables.LTYPE.entries.forEach((t) => {
|
352
|
+
const o = {
|
353
|
+
name: t.name,
|
354
|
+
description: t.description,
|
355
|
+
standardFlag: t.standardFlag,
|
356
|
+
totalPatternLength: t.totalPatternLength,
|
357
|
+
pattern: t.pattern
|
358
|
+
}, r = new Q(o);
|
359
|
+
this.processCommonTableEntryAttrs(t, r), r.name = t.name, n.tables.linetypeTable.add(r);
|
360
|
+
});
|
361
|
+
}
|
362
|
+
processTextStyles(e, n) {
|
363
|
+
e.tables.STYLE.entries.forEach((t) => {
|
364
|
+
const o = new $(t);
|
365
|
+
this.processCommonTableEntryAttrs(t, o), n.tables.textStyleTable.add(o);
|
366
|
+
});
|
367
|
+
}
|
368
|
+
processDimStyles(e, n) {
|
369
|
+
e.tables.DIMSTYLE.entries.forEach((t) => {
|
370
|
+
const o = {
|
371
|
+
name: t.name,
|
372
|
+
ownerId: t.ownerHandle.toString(),
|
373
|
+
dimpost: t.DIMPOST || "",
|
374
|
+
dimapost: t.DIMAPOST || "",
|
375
|
+
dimscale: t.DIMSCALE,
|
376
|
+
dimasz: t.DIMASZ,
|
377
|
+
dimexo: t.DIMEXO,
|
378
|
+
dimdli: t.DIMDLI,
|
379
|
+
dimexe: t.DIMEXE,
|
380
|
+
dimrnd: t.DIMRND,
|
381
|
+
dimdle: t.DIMDLE,
|
382
|
+
dimtp: t.DIMTP,
|
383
|
+
dimtm: t.DIMTM,
|
384
|
+
dimtxt: t.DIMTXT,
|
385
|
+
dimcen: t.DIMCEN,
|
386
|
+
dimtsz: t.DIMTSZ,
|
387
|
+
dimaltf: t.DIMALTF,
|
388
|
+
dimlfac: t.DIMLFAC,
|
389
|
+
dimtvp: t.DIMTVP,
|
390
|
+
dimtfac: t.DIMTFAC,
|
391
|
+
dimgap: t.DIMGAP,
|
392
|
+
dimaltrnd: t.DIMALTRND,
|
393
|
+
dimtol: t.DIMTOL == null || t.DIMTOL == 0 ? 0 : 1,
|
394
|
+
dimlim: t.DIMLIM == null || t.DIMLIM == 0 ? 0 : 1,
|
395
|
+
dimtih: t.DIMTIH == null || t.DIMTIH == 0 ? 0 : 1,
|
396
|
+
dimtoh: t.DIMTOH == null || t.DIMTOH == 0 ? 0 : 1,
|
397
|
+
dimse1: t.DIMSE1 == null || t.DIMSE1 == 0 ? 0 : 1,
|
398
|
+
dimse2: t.DIMSE2 == null || t.DIMSE2 == 0 ? 0 : 1,
|
399
|
+
dimtad: t.DIMTAD,
|
400
|
+
dimzin: t.DIMZIN,
|
401
|
+
dimazin: t.DIMAZIN,
|
402
|
+
dimalt: t.DIMALT,
|
403
|
+
dimaltd: t.DIMALTD,
|
404
|
+
dimtofl: t.DIMTOFL,
|
405
|
+
dimsah: t.DIMSAH,
|
406
|
+
dimtix: t.DIMTIX,
|
407
|
+
dimsoxd: t.DIMSOXD,
|
408
|
+
dimclrd: t.DIMCLRD,
|
409
|
+
dimclre: t.DIMCLRE,
|
410
|
+
dimclrt: t.DIMCLRT,
|
411
|
+
dimadec: t.DIMADEC || 0,
|
412
|
+
dimunit: t.DIMUNIT || 2,
|
413
|
+
dimdec: t.DIMDEC,
|
414
|
+
dimtdec: t.DIMTDEC,
|
415
|
+
dimaltu: t.DIMALTU,
|
416
|
+
dimalttd: t.DIMALTTD,
|
417
|
+
dimaunit: t.DIMAUNIT,
|
418
|
+
dimfrac: t.DIMFRAC,
|
419
|
+
dimlunit: t.DIMLUNIT,
|
420
|
+
dimdsep: t.DIMDSEP,
|
421
|
+
dimtmove: t.DIMTMOVE || 0,
|
422
|
+
dimjust: t.DIMJUST,
|
423
|
+
dimsd1: t.DIMSD1,
|
424
|
+
dimsd2: t.DIMSD2,
|
425
|
+
dimtolj: t.DIMTOLJ,
|
426
|
+
dimtzin: t.DIMTZIN,
|
427
|
+
dimaltz: t.DIMALTZ,
|
428
|
+
dimalttz: t.DIMALTTZ,
|
429
|
+
dimfit: t.DIMFIT || 0,
|
430
|
+
dimupt: t.DIMUPT,
|
431
|
+
dimatfit: t.DIMATFIT,
|
432
|
+
dimtxsty: "Standard",
|
433
|
+
// TODO: Set correct value
|
434
|
+
dimldrblk: "",
|
435
|
+
// TODO: Set correct value
|
436
|
+
dimblk: t.DIMBLK || "",
|
437
|
+
// TODO: Set correct value
|
438
|
+
dimblk1: t.DIMBLK1 || "",
|
439
|
+
// TODO: Set correct value
|
440
|
+
dimblk2: t.DIMBLK2 || "",
|
441
|
+
// TODO: Set correct value
|
442
|
+
dimlwd: t.DIMLWD,
|
443
|
+
dimlwe: t.DIMLWE
|
444
|
+
}, r = new ee(o);
|
445
|
+
this.processCommonTableEntryAttrs(t, r), n.tables.dimStyleTable.add(r);
|
446
|
+
});
|
447
|
+
}
|
448
|
+
processLayers(e, n) {
|
449
|
+
e.tables.LAYER.entries.forEach((t) => {
|
450
|
+
const o = new ne();
|
451
|
+
o.colorIndex = t.colorIndex;
|
452
|
+
const r = new te({
|
453
|
+
name: t.name,
|
454
|
+
standardFlags: t.standardFlag,
|
455
|
+
linetype: t.lineType,
|
456
|
+
lineWeight: t.lineweight,
|
457
|
+
isOff: t.off,
|
458
|
+
color: o,
|
459
|
+
isPlottable: t.plotFlag != 0
|
460
|
+
});
|
461
|
+
this.processCommonTableEntryAttrs(t, r), n.tables.layerTable.add(r);
|
462
|
+
});
|
463
|
+
}
|
464
|
+
processViewports(e, n) {
|
465
|
+
e.tables.VPORT.entries.forEach((t) => {
|
466
|
+
const o = new oe();
|
467
|
+
this.processCommonTableEntryAttrs(t, o), t.circleSides && (o.circleSides = t.circleSides), o.standardFlag = t.standardFlag, o.center.copy(t.center), o.lowerLeftCorner.copy(t.lowerLeftCorner), o.upperRightCorner.copy(t.upperRightCorner), t.snapBasePoint && o.snapBase.copy(t.snapBasePoint), t.snapRotationAngle && (o.snapAngle = t.snapRotationAngle), t.snapSpacing && o.snapIncrements.copy(t.snapSpacing), t.majorGridLines && (o.gridMajor = t.majorGridLines), t.gridSpacing && o.gridIncrements.copy(t.gridSpacing), t.backgroundObjectId && (o.backgroundObjectId = t.backgroundObjectId), o.gsView.center.copy(t.center), o.gsView.viewDirectionFromTarget.copy(t.viewDirectionFromTarget), o.gsView.viewTarget.copy(t.viewTarget), t.lensLength && (o.gsView.lensLength = t.lensLength), t.frontClippingPlane && (o.gsView.frontClippingPlane = t.frontClippingPlane), t.backClippingPlane && (o.gsView.backClippingPlane = t.backClippingPlane), t.viewHeight && (o.gsView.viewHeight = t.viewHeight), t.viewTwistAngle && (o.gsView.viewTwistAngle = t.viewTwistAngle), t.frozenLayers && (o.gsView.frozenLayers = t.frozenLayers), t.styleSheet && (o.gsView.styleSheet = t.styleSheet), t.renderMode && (o.gsView.renderMode = t.renderMode), t.viewMode && (o.gsView.viewMode = t.viewMode), t.ucsIconSetting && (o.gsView.ucsIconSetting = t.ucsIconSetting), t.ucsOrigin && o.gsView.ucsOrigin.copy(t.ucsOrigin), t.ucsXAxis && o.gsView.ucsXAxis.copy(t.ucsXAxis), t.ucsYAxis && o.gsView.ucsYAxis.copy(t.ucsYAxis), t.orthographicType && (o.gsView.orthographicType = t.orthographicType), t.shadePlotSetting && (o.gsView.shadePlotSetting = t.shadePlotSetting), t.shadePlotObjectId && (o.gsView.shadePlotObjectId = t.shadePlotObjectId), t.visualStyleObjectId && (o.gsView.visualStyleObjectId = t.visualStyleObjectId), t.isDefaultLightingOn && (o.gsView.isDefaultLightingOn = t.isDefaultLightingOn), t.defaultLightingType && (o.gsView.defaultLightingType = t.defaultLightingType), t.brightness && (o.gsView.brightness = t.brightness), t.contrast && (o.gsView.contrast = t.contrast), t.ambientColor && (o.gsView.ambientColor = t.ambientColor), n.tables.viewportTable.add(o);
|
468
|
+
});
|
469
|
+
}
|
470
|
+
processBlockTables(e, n) {
|
471
|
+
e.tables.BLOCK_RECORD.entries.forEach((t) => {
|
472
|
+
let o = n.tables.blockTable.getAt(t.name);
|
473
|
+
o || (o = new se(), o.objectId = t.handle.toString(), o.name = t.name, n.tables.blockTable.add(o)), !o.isModelSapce && !o.isPaperSapce && t.entities && t.entities.length > 0 && this.processEntitiesInBlock(t.entities, o);
|
474
|
+
});
|
475
|
+
}
|
476
|
+
processBlocks(e, n) {
|
477
|
+
}
|
478
|
+
async processEntitiesInBlock(e, n) {
|
479
|
+
const s = new T(), t = e.length;
|
480
|
+
for (let o = 0; o < t; o++) {
|
481
|
+
const r = e[o], a = s.convert(r);
|
482
|
+
a && n.appendEntity(a);
|
483
|
+
}
|
484
|
+
}
|
485
|
+
/**
|
486
|
+
* Breaks up the work into smaller chunks that are executed asynchronously. This is often referred to
|
487
|
+
* as "batch processing" or "cooperative multitasking," where the time-consuming task is broken into
|
488
|
+
* smaller pieces and executed in small intervals to allow the UI to remain responsive.
|
489
|
+
*/
|
490
|
+
async processEntities(e, n, s, t, o) {
|
491
|
+
const r = new T(), a = e.entities, c = a.length, l = new re(
|
492
|
+
c,
|
493
|
+
100 - t.value,
|
494
|
+
s
|
495
|
+
), i = n.tables.blockTable.modelSpace;
|
496
|
+
await l.processChunk(async (f, d) => {
|
497
|
+
for (let p = f; p < d; p++) {
|
498
|
+
const D = a[p], g = r.convert(D);
|
499
|
+
g && i.appendEntity(g);
|
500
|
+
}
|
501
|
+
if (o) {
|
502
|
+
let p = t.value + d / c * (100 - t.value);
|
503
|
+
p > 100 && (p = 100), await o(p, "ENTITY", "IN-PROGRESS");
|
504
|
+
}
|
505
|
+
});
|
506
|
+
}
|
507
|
+
processHeader(e, n) {
|
508
|
+
const s = e.header;
|
509
|
+
n.cecolor.colorIndex = s.CECOLOR || 256, n.angBase = s.ANGBASE ?? 0, n.angDir = s.ANGDIR ?? 0, n.aunits = s.AUNITS ?? 0, n.insunits = s.INSUNITS ?? 1, n.pdmode = s.PDMODE ?? 0, n.pdsize = s.PDSIZE ?? 0;
|
510
|
+
}
|
511
|
+
processCommonTableEntryAttrs(e, n) {
|
512
|
+
n.name = e.name, n.objectId = e.handle.toString(), n.ownerId = e.ownerHandle.toString();
|
513
|
+
}
|
514
|
+
processObjects(e, n) {
|
515
|
+
this.processLayouts(e, n), this.processImageDefs(e, n);
|
516
|
+
}
|
517
|
+
processLayouts(e, n) {
|
518
|
+
const s = n.dictionaries.layouts;
|
519
|
+
e.objects.LAYOUT.forEach((o) => {
|
520
|
+
const r = new ie();
|
521
|
+
r.layoutName = o.layoutName, r.tabOrder = o.tabOrder, r.blockTableRecordId = o.ownerHandle.toString(), r.limits.min.copy(o.minLimit), r.limits.max.copy(o.maxLimit), r.extents.min.copy(o.minExtent), r.extents.max.copy(o.maxExtent), this.processCommonObjectAttrs(o, r), s.setAt(r.layoutName, r);
|
522
|
+
});
|
523
|
+
}
|
524
|
+
processImageDefs(e, n) {
|
525
|
+
const s = n.dictionaries.imageDefs;
|
526
|
+
e.objects.IMAGEDEF.forEach((o) => {
|
527
|
+
const r = new ce();
|
528
|
+
r.sourceFileName = o.fileName, this.processCommonObjectAttrs(o, r), s.setAt(r.objectId, r);
|
529
|
+
});
|
530
|
+
}
|
531
|
+
processCommonObjectAttrs(e, n) {
|
532
|
+
n.objectId = e.handle.toString(), n.ownerId = e.ownerHandle.toString();
|
533
|
+
}
|
534
|
+
}
|
535
|
+
export {
|
536
|
+
De as AcDbLibreDwgConverter
|
537
|
+
};
|
@@ -0,0 +1 @@
|
|
1
|
+
(function(g,r){typeof exports=="object"&&typeof module<"u"?r(exports,require("@mlightcad/data-model"),require("@mlightcad/libredwg-web")):typeof define=="function"&&define.amd?define(["exports","@mlightcad/data-model","@mlightcad/libredwg-web"],r):(g=typeof globalThis<"u"?globalThis:g||self,r(g["libredwg-converter"]={},g.dataModel,g.libredwgWeb))})(this,function(g,r,A){"use strict";class w{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){if(e.numberOfControlPoints>0&&e.numberOfKnots>0){const n=new r.AcDbSpline(e.controlPoints,e.knots,e.weights);return n.closed=!!(e.flag&1),n}else if(e.numberOfFitPoints>0){const n=new r.AcDbSpline(e.fitPoints,"Uniform");return n.closed=!!(e.flag&1),n}return 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 a=s,l=new r.AcGePolyline2d;l.closed=a.isClosed,a.vertices.forEach((d,c)=>{l.addVertexAt(c,{x:d.x,y:d.y,bulge:d.bulge})}),n.add(l)}else{const a=s,l=new r.AcGeLoop2d;a.edges.forEach(d=>{if(d.type==1){const c=d;l.add(new r.AcGeLine2d(c.start,c.end))}else if(d.type==2){const c=d;l.add(new r.AcGeCircArc2d(c.center,c.radius,c.startAngle,c.endAngle,!c.isCCW))}else if(d.type==3){const c=d;new r.AcGeVector2d().subVectors(c.end,c.center);const p=Math.sqrt(Math.pow(c.end.x,2)+Math.pow(c.end.y,2)),f=p*c.lengthOfMinorAxis;let D=c.startAngle,u=c.endAngle;const T=Math.atan2(c.end.y,c.end.x);c.isCCW||(D=Math.PI*2-D,u=Math.PI*2-u),l.add(new r.AcGeEllipseArc2d({...c.center,z:0},p,f,D,u,!c.isCCW,T))}else if(d.type==4){const c=d;if(c.numberOfControlPoints>0&&c.numberOfKnots>0){const h=c.controlPoints.map(D=>({x:D.x,y:D.y,z:0}));let p=!0;const f=c.controlPoints.map(D=>(D.weight==null&&(p=!1),D.weight||1));l.add(new r.AcGeSpline3d(h,c.knots,p?f:void 0))}else if(c.numberOfFitData>0){const h=c.fitDatum.map(p=>({x:p.x,y:p.y,z:0}));l.add(new r.AcGeSpline3d(h,"Uniform"))}}}),n.add(l)}}),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 b extends r.AcDbDatabaseConverter{constructor(e){super(),this.libredwg=A.LibreDwg.createByWasmInstance(e)}parse(e){if(this.libredwg==null)throw new Error("libredwg is not loaded!");const n=this.libredwg.dwg_read_data(e,A.Dwg_File_Type.DWG);if(n==null)throw new Error("Failed to read dwg data!");const o=this.libredwg.convert(n);return this.libredwg.dwg_free(n),o}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 a=i.lastIndexOf(".");return a>=0?i.substring(0,a).toLowerCase():i.toLowerCase()}};e.tables.STYLE.entries.forEach(i=>{const a=[];let l=t(i.font);l&&a.push(l),l=t(i.bigFont),l&&a.push(l),o.set(i.name,a)});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 a=i;[...a.text.matchAll(s)].forEach(d=>{t.add(d[1].toLowerCase())});const l=o.get(a.styleName);l==null||l.forEach(d=>t.add(d))}else if(i.type=="TEXT"){const a=i,l=o.get(a.styleName);l==null||l.forEach(d=>t.add(d))}else if(i.type=="INSERT"){const a=i,l=n.get(a.name);l&&this.getFontsInBlock(l.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,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 w,t=e.length;for(let s=0;s<t;s++){const i=e[s],a=o.convert(i);a&&n.appendEntity(a)}}async processEntities(e,n,o,t,s){const i=new w,a=e.entities,l=a.length,d=new r.AcDbBatchProcessing(l,100-t.value,o),c=n.tables.blockTable.modelSpace;await d.processChunk(async(h,p)=>{for(let f=h;f<p;f++){const D=a[f],u=i.convert(D);u&&c.appendEntity(u)}if(s){let f=t.value+p/l*(100-t.value);f>100&&(f=100),await s(f,"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.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,i.blockTableRecordId=s.ownerHandle.toString(),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()}}g.AcDbLibreDwgConverter=b,Object.defineProperty(g,Symbol.toStringTag,{value:"Module"})});
|