UNPKG

@plastichub/osr-cad

Version:

This is a CLI(CommandLineInterface) toolset to convert 3D files, using Solidworks and other software.

53 lines 1.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const fs = require("fs"); const dxf = require('dxf-parser'); const write_1 = require("@plastichub/fs/write"); function distanceBetweenPoints(p1, p2) { return Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2)); } function arcLength(radius, startAngle, endAngle) { return Math.abs(endAngle - startAngle) * radius; } function calculateEntityLength(entity) { switch (entity.type) { //case 'LINE': // return distanceBetweenPoints(entity.start!, entity.end!); case 'LWPOLYLINE': case 'LINE': let length = 0; for (let i = 0; i < entity.vertices.length - 1; i++) { try { length += distanceBetweenPoints(entity.vertices[i], entity.vertices[i + 1]); } catch (e) { console.log('error', entity, e); } } return length; case 'CIRCLE': return 2 * Math.PI * entity.radius; case 'ARC': return arcLength(entity.radius, entity.startAngle, entity.endAngle); default: return 0; } } function calculateTotalDxfEntitiesLength(filePath) { const parser = new dxf(); const dxfData = parser.parseSync(fs.readFileSync(filePath, 'utf-8')); const ret = dxfData.entities.reduce((totalLength, entity) => { console.log('entity', entity); const length = calculateEntityLength(entity); return totalLength + length; }, 0); (0, write_1.sync)(filePath + '.json', JSON.stringify(dxfData, null, 2)); return ret; } const test = () => { // Usage example const filePath = './tests/dxf/square-200-bore.DXF'; const totalLength = calculateTotalDxfEntitiesLength(filePath); console.log(`Total length of all entities: ${totalLength}`); }; //# sourceMappingURL=dxf.js.map