UNPKG

autocad-dxf

Version:

A module which can be used to parse AutoCAD dxf files and to make programmatic and geometric operations on the AutoCAD drawing entities.

127 lines (108 loc) 3.18 kB
const BSpline = require("./BSpline"); const ErrorMessages = require("./ErrorMessages.json"); module.exports = (entity, plane, getAxes, tolerance) => { if (typeof entity != "object") { throw new Error(ErrorMessages.INCORRECT_PARAMS); return; } let [ax1, ax2] = getAxes(plane); if (plane && ax1 === undefined && ax2 === undefined) { throw new Error(ErrorMessages.INCORRECT_PARAMS); return; } const etype = entity.subclass; if (etype == "AcDbLine") { const x1 = entity[`start_${ax1}`]; const y1 = entity[`start_${ax2}`]; const x2 = entity[`end_${ax1}`]; const y2 = entity[`end_${ax2}`]; return Math.sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2)); } else if (etype == "AcDbPolyline") { const vertices = JSON.parse(JSON.stringify(entity.vertices)); if (entity.type == "Closed") vertices.push(vertices[0]); let length = 0; for (let i = 1; i < vertices.length; i++) { const x1 = vertices[i - 1][`${ax1}`]; const y1 = vertices[i - 1][`${ax2}`]; const x2 = vertices[i][`${ax1}`]; const y2 = vertices[i][`${ax2}`]; length = length + Math.sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2)); } return length; } else if (etype == "AcDbCircle") { const start_angle = entity.start_angle; const end_angle = entity.end_angle; if (start_angle !== undefined && end_angle !== undefined) { let d; if (start_angle < end_angle) { d = Math.abs(start_angle - end_angle); } else { d = 360 - Math.abs(start_angle - end_angle); } return 2*Math.PI*(entity.radius)*d/360; } else { return 2*Math.PI*entity.radius; } } else if (etype == "AcDbEllipse") { const t0 = entity.start_parameter; const t1 = entity.end_parameter; const dx = entity[`major_end_d${ax1}`]; const dy = entity[`major_end_d${ax2}`]; const ratio = entity.minorToMajor; const a = Math.sqrt(dx*dx + dy*dy); const b = ratio * a; const x = [ -0.989400934991650, -0.944575023073233, -0.865631202387832, -0.755404408355003, -0.617876244402644, -0.458016777657227, -0.281603550779259, -0.095012509837637, 0.095012509837637, 0.281603550779259, 0.458016777657227, 0.617876244402644, 0.755404408355003, 0.865631202387832, 0.944575023073233, 0.989400934991650 ]; const w = [ 0.027152459411754, 0.062253523938648, 0.095158511682493, 0.124628971255534, 0.149595988816577, 0.169156519395003, 0.182603415044924, 0.189450610455068, 0.189450610455068, 0.182603415044924, 0.169156519395003, 0.149595988816577, 0.124628971255534, 0.095158511682493, 0.062253523938648, 0.027152459411754 ]; const half = 0.5 * (t1 - t0); const center = 0.5 * (t1 + t0); let sum = 0; for (let i = 0; i < x.length; i++) { const t = half * x[i] + center; const s = Math.sin(t); const c = Math.cos(t); sum += w[i] * Math.sqrt( a*a*s*s + b*b*c*c ); } return half * sum; //return Math.PI*(a + b)*(1 + 3*h/(10 + Math.sqrt(4 - 3*h))); // previous Ramanujan's second formula } else if (etype == "AcDbSpline") { const result = BSpline(entity); return result.length; //Approximate } return; };