@mlightcad/libdxfrw-converter
Version:
The `libdxfrw-converter` package provides a DWG file converter for the RealDWG-Web ecosystem, enabling reading and conversion of DWG files into the AutoCAD-like drawing database structure. It is based on the [libdxfrw](https://github.com/LibreDWG/libdxfrw
490 lines (489 loc) • 17.5 kB
JavaScript
import { AcDbArc as S, AcDbCircle as z, AcGeVector3d as E, AcDbEllipse as R, AcDbBlockReference as L, AcDbHatch as C, AcGePolyline2d as W, AcGeLoop2d as N, AcGeLine2d as Y, AcGeCircArc2d as _, AcGeVector2d as F, AcGeEllipseArc2d as k, AcGeSpline3d as y, AcDbLine as H, AcGePoint3d as P, AcDbMText as V, AcDbPoint as j, AcDbPolyline as O, AcGePoint2d as M, AcDbRay as B, AcDbSpline as D, AcDbText as G, AcDbXline as X, AcDbRasterImageDef as $, AcDbDatabaseConverter as U, AcDbLinetypeTableRecord as q, AcDbTextStyleTableRecord as Z, AcDbDimStyleTableRecord as K, AcCmColor as J, AcDbLayerTableRecord as Q, AcDbViewportTableRecord as ee, AcDbBlockTableRecord as te, AcDbBatchProcessing as ie } from "@mlightcad/data-model";
class v {
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 S(
e.center(),
e.radius,
e.startAngle,
e.endAngle
);
}
convertCirle(e) {
return new z(e.basePoint, e.radius);
}
convertEllipse(e) {
const i = new E(e.secPoint), s = i.length();
return new R(
e.basePoint,
E.Z_AXIS,
i,
s,
s * e.ratio,
e.startAngle,
e.endAngle
);
}
convertBlockReference(e) {
const i = new L(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 C(), s = e.definitionLines;
for (let r = 0, t = s.size(); r < t; ++r) {
const o = s.get(r);
o != null && i.definitionLines.push({
angle: o.angle,
origin: o.base,
delta: o.offset,
dashPattern: this.toNumberArray(o.dashPattern)
});
}
i.hatchStyle = e.hatchStyle, i.patternName = e.name, i.patternType = e.patternType, i.patternAngle = e.angle, i.patternScale = e.scale;
const n = e.getLoopList();
for (let r = 0, t = n.size(); r < t; ++r) {
const o = n.get(r);
if (o != null)
if (o.type == 2) {
const d = o.getObjList().get(0);
if (d) {
const f = new W(), c = d.getVertexList();
for (let m = 0, p = c.size(); m < p; ++m) {
const a = c.get(m);
a != null && f.addVertexAt(m, {
x: a.x,
y: a.y,
bulge: a.bulge
});
}
i.add(f);
}
} else {
const d = window.libdxfrw, f = o.getObjList(), c = new N();
for (let m = 0, p = f.size(); m < p; ++m) {
const a = f.get(m);
if (a != null) {
if (a.eType == d.DRW_ETYPE.LINE) {
const l = a;
c.add(new Y(l.basePoint, l.secPoint));
} else if (a.eType == d.DRW_ETYPE.ARC) {
const l = a;
c.add(
new _(
l.center(),
l.radius,
l.startAngle,
l.endAngle,
!l.isccw
)
);
} else if (a.eType == d.DRW_ETYPE.ELLIPSE) {
const l = a, g = l.basePoint, b = l.secPoint, A = new F(b).length(), x = A * l.ratio;
let u = l.startAngle, h = l.endAngle;
const I = Math.atan2(b.y, b.x);
l.isCounterClockwise || (u = Math.PI * 2 - u, h = Math.PI * 2 - h), c.add(
new k(
{ x: g.x, y: g.y, z: 0 },
A,
x,
u,
h,
!l.isCounterClockwise,
I
)
);
} else if (a.eType == d.DRW_ETYPE.SPLINE) {
const l = a;
if (l.numberOfControls > 0 && l.numberOfKnots > 0) {
const g = this.toPointArray(
l.getControlList(),
!0
), b = this.toNumberArray(l.weights), T = this.toNumberArray(l.knots);
c.add(
new y(
g,
T,
b.length > 0 ? b : void 0
)
);
} else if (l.numberOfFits > 0) {
const g = this.toPointArray(
l.getFitList()
);
c.add(new y(g, "Uniform"));
}
}
}
}
i.add(c);
}
}
return i;
}
convertLine(e) {
const i = e.basePoint, s = e.secPoint;
return new H(
new P(i.x, i.y, i.z || 0),
new P(s.x, s.y, s.z || 0)
);
}
convertMText(e) {
const i = new V();
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 j();
return i.position = e.basePoint, i;
}
convertPolyline(e) {
const i = new O();
i.closed = !!(e.flags & 1);
const s = e.getVertexList();
for (let n = 0, r = s.size(); n < r; ++n) {
const t = s.get(n);
t != null && i.addVertexAt(
n,
new M(t.x, t.y),
t.bulge,
t.startWidth,
t.endWidth
);
}
return i;
}
convertRay(e) {
const i = new B();
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 D(
this.toPointArray(e.getControlList(), !1),
this.toNumberArray(e.knots),
i.length > 0 ? i : void 0,
e.degree,
// Default degree
!!(e.flags & 1)
);
if (e.numberOfFits > 0) {
const s = this.toPointArray(e.getFitList());
if (s.length > 0)
return new D(s, "Uniform", e.degree, !!(e.flags & 1));
}
return null;
}
convertText(e) {
const i = new G();
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 X();
return i.basePoint.copy(e.basePoint), i.unitDir.copy(e.secPoint), i;
}
processCommonAttrs(e, i) {
i.layer = e.layer, i.objectId = e.handle.toString(), i.ownerId = e.parentHandle.toString(), e.lineType != null && (i.lineType = e.lineType), e.lWeight != null && (i.lineWeight = e.lWeight), e.ltypeScale != null && (i.linetypeScale = e.ltypeScale), e.color24 != null && (i.color.color = e.color24), e.color != null && (i.color.colorIndex = e.color), e.colorName != null && (i.color.colorName = e.colorName), e.visible != null && (i.visibility = e.visible), e.transparency != null && (i.transparency = e.transparency);
}
toNumberArray(e) {
const i = [];
for (let s = 0, n = e.size(); s < n; ++s) {
const r = e.get(s);
r != null && i.push(r);
}
return i;
}
toPointArray(e, i = !0) {
const s = [];
for (let n = 0, r = e.size(); n < r; ++n) {
const t = e.get(n);
t != null && s.push({ x: t.x, y: t.y, z: i ? 0 : t.z });
}
return s;
}
}
class ne {
convertImageDef(e) {
const i = new $();
return i.sourceFileName = e.path, i.objectId = e.handle.toString(), i.ownerId = e.parentHandle.toString(), i;
}
}
class oe extends U {
constructor(e) {
super(), this.librefrw = e;
}
onFinished() {
super.onFinished(), this.dwg && (this.dwg.delete(), this.dwg = void 0), this.database && (this.database.delete(), this.database = void 0), this.fileHandler && (this.fileHandler.delete(), this.fileHandler = void 0);
}
parse(e) {
if (this.librefrw == null)
throw new Error("librefrw is not loaded!");
const i = this.librefrw;
return this.database = new i.DRW_Database(), this.fileHandler = new i.DRW_FileHandler(), this.fileHandler.database = this.database, this.dwg = new i.DRW_DwgR(e), this.dwg.read(this.fileHandler, !1), this.database;
}
processLineTypes(e, i) {
const s = e.lineTypes;
for (let n = 0, r = s.size(); n < r; ++n) {
const t = s.get(n);
if (t != null) {
const o = {
name: t.name,
description: t.desc,
standardFlag: t.flags,
totalPatternLength: t.length,
pattern: this.convertLineTypePattern(t.path)
}, d = new q(o);
this.processCommonTableEntryAttrs(t, d), d.name = t.name, i.tables.linetypeTable.add(d);
}
}
}
convertLineTypePattern(e) {
const i = [];
for (let s = 0, n = e.size(); s < n; ++s)
i.push({
elementLength: e.get(s) || 0,
elementTypeFlag: 0
});
return i;
}
processTextStyles(e, i) {
const s = e.textStyles;
for (let n = 0, r = s.size(); n < r; ++n) {
const t = s.get(n);
if (t != null) {
const o = {
name: t.name,
standardFlag: t.flags,
fixedTextHeight: t.height,
widthFactor: t.width,
obliqueAngle: t.oblique,
textGenerationFlag: t.genFlag,
lastHeight: t.lastHeight,
font: t.font,
bigFont: t.bigFont
}, d = new Z(o);
this.processCommonTableEntryAttrs(t, d), i.tables.textStyleTable.add(d);
}
}
}
processDimStyles(e, i) {
const s = e.dimStyles;
for (let n = 0, r = s.size(); n < r; ++n) {
const t = s.get(n);
if (t != null) {
const o = {
name: t.name,
ownerId: t.parentHandle.toString(),
dimpost: t.dimpost || "",
dimapost: t.dimapost || "",
dimscale: t.dimscale,
dimasz: t.dimasz,
dimexo: t.dimexo,
dimdli: t.dimdli,
dimexe: t.dimexe,
dimrnd: t.dimrnd,
dimdle: t.dimdle,
dimtp: t.dimtp,
dimtm: t.dimtm,
dimtxt: t.dimtxt,
dimcen: t.dimcen,
dimtsz: t.dimtsz,
dimaltf: t.dimaltf,
dimlfac: t.dimlfac,
dimtvp: t.dimtvp,
dimtfac: t.dimtfac,
dimgap: t.dimgap,
dimaltrnd: t.dimaltrnd,
dimtol: t.dimtol == null || t.dimtol == 0 ? 0 : 1,
dimlim: t.dimlim == null || t.dimlim == 0 ? 0 : 1,
dimtih: t.dimtih == null || t.dimtih == 0 ? 0 : 1,
dimtoh: t.dimtoh == null || t.dimtoh == 0 ? 0 : 1,
dimse1: t.dimse1 == null || t.dimse1 == 0 ? 0 : 1,
dimse2: t.dimse2 == null || t.dimse2 == 0 ? 0 : 1,
dimtad: t.dimtad,
dimzin: t.dimzin,
dimazin: t.dimazin,
dimalt: t.dimalt,
dimaltd: t.dimaltd,
dimtofl: t.dimtofl,
dimsah: t.dimsah,
dimtix: t.dimtix,
dimsoxd: t.dimsoxd,
dimclrd: t.dimclrd,
dimclre: t.dimclre,
dimclrt: t.dimclrt,
dimadec: t.dimadec || 0,
dimunit: t.dimunit || 2,
dimdec: t.dimdec,
dimtdec: t.dimtdec,
dimaltu: t.dimaltu,
dimalttd: t.dimalttd,
dimaunit: t.dimaunit,
dimfrac: t.dimfrac,
dimlunit: t.dimlunit,
dimdsep: t.dimdsep.toString(),
dimtmove: t.dimtmove || 0,
dimjust: t.dimjust,
dimsd1: t.dimsd1,
dimsd2: t.dimsd2,
dimtolj: t.dimtolj,
dimtzin: t.dimtzin,
dimaltz: t.dimaltz,
dimalttz: t.dimaltttz,
dimfit: t.dimfit || 0,
dimupt: t.dimupt,
dimatfit: t.dimatfit,
dimtxsty: t.dimtxsty || "Standard",
dimldrblk: t.dimldrblk || "",
dimblk: t.dimblk || "",
dimblk1: t.dimblk1 || "",
dimblk2: t.dimblk2 || "",
dimlwd: t.dimlwd,
dimlwe: t.dimlwe
}, d = new K(o);
this.processCommonTableEntryAttrs(t, d), i.tables.dimStyleTable.add(d);
}
}
}
processLayers(e, i) {
const s = e.layers;
for (let n = 0, r = s.size(); n < r; ++n) {
const t = s.get(n);
if (t != null) {
const o = new J();
o.colorIndex = t.color;
const d = new Q({
name: t.name,
standardFlags: t.flags,
linetype: t.lineType,
lineWeight: t.lWeight,
isOff: t.color < 0,
color: o,
isPlottable: t.plotF,
materialId: t.handleMaterialS
});
this.processCommonTableEntryAttrs(t, d), i.tables.layerTable.add(d);
}
}
}
processViewports(e, i) {
const s = e.viewports;
for (let n = 0, r = s.size(); n < r; ++n) {
const t = s.get(n);
if (t != null) {
const o = new ee();
this.processCommonTableEntryAttrs(t, o), t.circleZoom && (o.circleSides = t.circleZoom), o.standardFlag = t.flags, o.center.copy(t.center), o.lowerLeftCorner.copy(t.lowerLeft), o.upperRightCorner.copy(t.upperRight), t.snapBase && o.snapBase.copy(t.snapBase), t.snapAngle && (o.snapAngle = t.snapAngle), o.snapIncrements && o.snapIncrements.copy(t.snapSpacing), t.gridSpacing && o.gridIncrements.copy(t.gridSpacing), o.gsView.center.copy(t.center), o.gsView.viewDirectionFromTarget.copy(t.viewDir), o.gsView.viewTarget.copy(t.viewTarget), t.lensHeight && (o.gsView.lensLength = t.lensHeight), t.height && (o.gsView.viewHeight = t.height), t.twistAngle && (o.gsView.viewTwistAngle = t.twistAngle), t.viewMode && (o.gsView.viewMode = t.viewMode), t.ucsIcon && (o.gsView.ucsIconSetting = t.ucsIcon), i.tables.viewportTable.add(o);
}
}
}
processBlockTables(e, i) {
const s = e.blocks;
for (let n = 0, r = s.size(); n < r; ++n) {
const t = s.get(n);
if (t != null) {
const o = new te();
o.objectId = t.handle.toString(), o.name = t.name.toUpperCase(), i.tables.blockTable.add(o), t.entities && this.processEntitiesInBlock(t.entities, o);
}
}
}
processCommonTableEntryAttrs(e, i) {
i.name = e.name, i.objectId = e.handle.toString(), i.ownerId = e.parentHandle.toString();
}
processHeader(e, i) {
const s = e.header;
let n = s.getVar("$CECOLOR");
i.cecolor.colorIndex = n ? n.getInt() : 256, n = s.getVar("$ANGDIR"), i.angDir = n ? n.getInt() : 0, n = s.getVar("$AUNITS"), i.aunits = n ? n.getInt() : 0, n = s.getVar("$INSUNITS"), i.insunits = n ? n.getInt() : 1, n = s.getVar("$PDMODE"), i.pdmode = n ? n.getInt() : 0, n = s.getVar("$PDSIZE"), i.pdsize = n ? n.getDouble() : 0;
}
processObjects(e, i) {
this.processImageDefs(e.images, i);
}
processImageDefs(e, i) {
const s = new ne(), n = i.dictionaries.imageDefs;
for (let r = 0, t = e.size(); r < t; ++r) {
const o = e.get(r);
if (o != null) {
const d = s.convertImageDef(o);
n.setAt(d.objectId, d);
}
}
}
async processEntitiesInBlock(e, i) {
const s = new v();
for (let n = 0, r = e.size(); n < r; ++n) {
const t = e.get(n);
if (t != null) {
const o = s.convert(t);
o && i.appendEntity(o);
}
}
}
async processEntities(e, i, s, n, r) {
if (e.mBlock) {
const t = new v(), o = e.mBlock.entities, d = o.size(), f = new ie(
d,
100 - n.value,
s
), c = i.tables.blockTable.modelSpace;
await f.processChunk(async (m, p) => {
for (let a = m; a < p; a++) {
const l = o.get(a);
if (l) {
const g = t.convert(l);
g && c.appendEntity(g);
}
}
if (r) {
let a = n.value + p / d * (100 - n.value);
a > 100 && (a = 100), await r(a, "ENTITY", "IN-PROGRESS");
}
});
}
}
}
export {
oe as AcDbLibdxfrwConverter
};