Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v16
1 change: 1 addition & 0 deletions .nvmrc
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ node.js

```sh
npm install
npm run build
node samples/node/parseSync
node samples/node/parseStream
npm run prepublishOnly
node samples/node/parse-sync.js
node samples/node/parse-stream.js
```

browser - the [three-dxf repo](https://github.com/gdsestimating/three-dxf) has a sample for viewing dxf cad in the browser
Expand Down
12,657 changes: 8,830 additions & 3,827 deletions package-lock.json

Large diffs are not rendered by default.

19 changes: 12 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
"type": "module",
"main": "./commonjs/index.js",
"module": "./esm/index.js",
"engines": {
"node": ">=16",
"npm": ">6"
},
"exports": {
"import": "./esm/index.js",
"require": "./commonjs/index.js"
Expand Down Expand Up @@ -32,17 +36,18 @@
"license": "MIT",
"devDependencies": {
"@babel/core": "^7.10.4",
"@babel/preset-env": "^7.10.4",
"@babel/register": "^7.10.4",
"approvals": "^3.0.5",
"mocha": "^8.0.1",
"@babel/preset-env": "^7.20.2",
"@babel/register": "^7.21.0",
"@types/node": "^16.18.15",
"approvals": "^6.0.0",
"mocha": "^10.2.0",
"should": "^13.2.3",
"typescript": "^4.4.3",
"webpack": "^5.52.1",
"webpack-cli": "^4.8.0"
"webpack": "^5.76.1",
"webpack-cli": "^5.0.1"
},
"dependencies": {
"loglevel": "^1.7.1"
"loglevel": "^1.8.1"
},
"keywords": [
"dxf",
Expand Down
12 changes: 6 additions & 6 deletions src/DxfArrayScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default class DxfArrayScanner {
* in the array. The first is the code, the second is the value.
* @returns {{code: Number}|*}
*/
public next() {
public next(): IGroup {
if (!this.hasNext()) {
if (!this._eof)
throw new Error('Unexpected end of input: EOF group not read before end of file. Ended on code ' + this._data[this._pointer]);
Expand Down Expand Up @@ -79,7 +79,7 @@ export default class DxfArrayScanner {
* Returns true if there is another code/value pair (2 elements in the array).
* @returns {boolean}
*/
public hasNext() {
public hasNext(): boolean {
// Check if we have read EOF group code
if (this._eof) {
return false;
Expand All @@ -96,7 +96,7 @@ export default class DxfArrayScanner {
* Returns true if the scanner is at the end of the array
* @returns {boolean}
*/
public isEOF() {
public isEOF(): boolean {
return this._eof;
}
}
Expand All @@ -109,7 +109,7 @@ export default class DxfArrayScanner {
* @param value
* @returns {*}
*/
function parseGroupValue(code: number, value: string) {
function parseGroupValue(code: number, value: string): IGroup['value'] {
if (code <= 9) return value;
if (code >= 10 && code <= 59) return parseFloat(value);
if (code >= 60 && code <= 99) return parseInt(value);
Expand Down Expand Up @@ -143,8 +143,8 @@ function parseGroupValue(code: number, value: string) {
* @param str
* @returns {boolean}
*/
function parseBoolean(str: '0' | '1') {
function parseBoolean(str: '0' | '1'): boolean {
if (str === '0') return false;
if (str === '1') return true;
throw TypeError('String \'' + str + '\' cannot be cast to Boolean type');
}
}
22 changes: 11 additions & 11 deletions src/DxfParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export default class DxfParser {
});
}

private _parse(dxfString: string) {
private _parse(dxfString: string): IDxf {
const dxf = {} as IDxf;
let lastHandle = 0;
const dxfLinesArray = dxfString.split(/\r\n|\r|\n/g);
Expand Down Expand Up @@ -268,7 +268,7 @@ export default class DxfParser {
*
* @return {object} header
*/
function parseHeader() {
function parseHeader(): Record<string, IPoint | number> {
// interesting variables:
// $ACADVER, $VIEWDIR, $VIEWSIZE, $VIEWCTR, $TDCREATE, $TDUPDATE
// http://www.autodesk.com/techpubs/autocad/acadr14/dxf/header_section_al_u05_c.htm
Expand Down Expand Up @@ -413,7 +413,7 @@ export default class DxfParser {
* parseTables
* @return {Object} Object representing tables
*/
function parseTables() {
function parseTables(): ITables {
const tables = {} as ITables;
curr = scanner.next();
while (curr.value !== 'EOF') {
Expand Down Expand Up @@ -770,8 +770,8 @@ export default class DxfParser {
* should be on the start of the first entity already.
* @return {Array} the resulting entities
*/
function parseEntities(forBlock: boolean) {
const entities = [] as IEntity[];
function parseEntities(forBlock: boolean): IEntity[] {
const entities: IEntity[] = [];

const endingOnValue = forBlock ? 'ENDBLK' : 'ENDSEC';

Expand Down Expand Up @@ -814,7 +814,7 @@ export default class DxfParser {
* y. The parser will determine if there is a z point automatically.
* @return {Object} The 2D or 3D point as an object with x, y[, z]
*/
function parsePoint(curr: IGroup) {
function parsePoint(curr: IGroup): IPoint {
const point = {} as IPoint;
let code = curr.code;

Expand All @@ -838,7 +838,7 @@ export default class DxfParser {
return point;
}

function ensureHandle(entity: IEntity | IBlock) {
function ensureHandle(entity: IEntity | IBlock): void {
if (!entity) throw new TypeError('entity cannot be undefined or null');

if (!entity.handle) entity.handle = lastHandle++;
Expand All @@ -849,24 +849,24 @@ export default class DxfParser {
}
}

function groupIs(group: IGroup, code: number, value: string | number | boolean) {
function groupIs(group: IGroup, code: number, value: string | number | boolean): boolean {
return group.code === code && group.value === value;
}

function logUnhandledGroup(curr: IGroup) {
function logUnhandledGroup(curr: IGroup): void {
log.debug('unhandled group ' + debugCode(curr));
}


function debugCode(curr: IGroup) {
function debugCode(curr: IGroup): string {
return curr.code + ':' + curr.value;
}

/**
* Returns the truecolor value of the given AutoCad color index value
* @return {Number} truecolor value as a number
*/
function getAcadColor(index: number) {
function getAcadColor(index: number): number {
return AUTO_CAD_COLOR_INDEX[index];
}

Expand Down
2 changes: 1 addition & 1 deletion src/ParseHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { IEntity, IPoint } from './entities/geomtry.js';
* Returns the truecolor value of the given AutoCad color index value
* @return {Number} truecolor value as a number
*/
export function getAcadColor(index: number) {
export function getAcadColor(index: number): number {
return AUTO_CAD_COLOR_INDEX[index];
}

Expand Down
4 changes: 2 additions & 2 deletions src/entities/3dface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export default class ThreeDface implements IGeometry {
}
}

function parse3dFaceVertices(scanner:DxfArrayScanner, curr:IGroup) {
var vertices = [];
function parse3dFaceVertices(scanner:DxfArrayScanner, curr:IGroup): I3DfaceEntity['vertices'] {
var vertices: I3DfaceEntity['vertices'] = [];
var vertexIsStarted = false;
var vertexIsFinished = false;
var verticesPer3dFace = 4; // there can be up to four vertices per face, although 3 is most used for TIN
Expand Down
2 changes: 1 addition & 1 deletion test/DxfParser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,4 @@ function verifyDxf(sourceFilePath) {
var dxf = parser.parse(file);

approvals.verifyAsJSON(sourceDirectory, baseName, dxf);
}
}