@mlightcad/data-model
Version:
The data-model package provides the core classes for interacting with AutoCAD's database and entities. This package mimics AutoCAD ObjectARX's AcDb (Database) classes and implements the drawing database structure that AutoCAD developers are familiar with.
85 lines • 3.7 kB
JavaScript
import DxfParser from '@mlightcad/dxf-json';
// Please don't modify the following two lines to import from '../database' and
// import from '../misc' so that treeshaking can include classes really needed.
import { AcDbDwgVersion } from '../database/AcDbDwgVersion';
import { AcDbCodePage, dwgCodePageToEncoding } from '../misc/AcDbCodePage';
/**
* DXF parsing worker
*/
var AcDbDxfParser = /** @class */ (function () {
function AcDbDxfParser() {
}
AcDbDxfParser.prototype.parse = function (data) {
var parser = new DxfParser();
// Use our own parser to parse version and code page information only to avoid
// parsing the whole dxf file in order to imporve performance
var headerInfo = this.getDxfInfoFromBuffer(data);
// If the version is less than or equal to AutoCAD 2000 format (AC1015),
// we need to decode it to string.
var text = '';
if (headerInfo.version &&
headerInfo.version.value <= 23 &&
headerInfo.encoding) {
text = new TextDecoder(headerInfo.encoding).decode(data);
}
else {
text = new TextDecoder().decode(data);
}
return parser.parseSync(text);
};
/**
* Reads a DXF ArrayBuffer and returns its version and code page.
* @param buffer The ArrayBuffer containing DXF file content.
*/
AcDbDxfParser.prototype.getDxfInfoFromBuffer = function (buffer) {
var _a, _b, _c, _d;
var chunkSize = 64 * 1024; // 64 KB
var decoder = new TextDecoder('utf-8');
var offset = 0;
var leftover = '';
var version = null;
var encoding = null;
var inHeader = false;
while (offset < buffer.byteLength) {
var end = Math.min(offset + chunkSize, buffer.byteLength);
var chunk = buffer.slice(offset, end);
offset = end;
var text = leftover + decoder.decode(chunk, { stream: true });
var lines = text.split(/\r?\n/);
leftover = (_a = lines.pop()) !== null && _a !== void 0 ? _a : ''; // last incomplete line (if any)
for (var i = 0; i < lines.length; i++) {
var line = lines[i].trim();
// detect start of HEADER
if (line === 'SECTION' && ((_b = lines[i + 2]) === null || _b === void 0 ? void 0 : _b.trim()) === 'HEADER') {
inHeader = true;
}
// detect end of HEADER
else if (line === 'ENDSEC' && inHeader) {
return { version: version, encoding: encoding };
}
// parse version
if (inHeader && line === '$ACADVER') {
var value = (_c = lines[i + 2]) === null || _c === void 0 ? void 0 : _c.trim();
if (value)
version = new AcDbDwgVersion(value);
}
// parse code page
else if (inHeader && line === '$DWGCODEPAGE') {
var value = (_d = lines[i + 2]) === null || _d === void 0 ? void 0 : _d.trim();
if (value) {
var codePage = AcDbCodePage[value];
encoding = dwgCodePageToEncoding(codePage);
}
}
if (version && encoding) {
return { version: version, encoding: encoding };
}
}
}
// return even if header not complete
return { version: version, encoding: encoding };
};
return AcDbDxfParser;
}());
export { AcDbDxfParser };
//# sourceMappingURL=AcDbDxfParser.js.map