JavaScript library for writing DXF files
npm install dxf-docJavaScript
const { DxfDocument } = require('dxf-doc');
const { Line, Circle, LwPolyline, Text, Hatch, Ellipse, Arc } = require('dxf-doc/entities');
const { HatchPattern, PolylineBoundaryPath } = require('dxf-doc/entities/hatch');
const fs = require('fs');
// Load patterns from the acad.pat file
const patternLines = fs.readFileSync(__dirname + '/acad.pat')
.toString()
.split('\n');
const patterns = HatchPattern.parse(patternLines);
const pts = [[150, 10], [160, 60], [190, 70], [190, 10]];
const dxf = new DxfDocument();
dxf.extents([0, 0], [250, 100]);
dxf.limits([0, 0], [250, 100]);
// Add entities
dxf.addEntities(
new Line(dxf, 10, 10, 70, 70),
new Circle(dxf, 110, 40, 30),
new LwPolyline(dxf, pts, true),
new Hatch(dxf, [
new PolylineBoundaryPath(pts)
], patterns[0]),
new Text(dxf, 'Hello World!', 5, [200, 50]),
new Ellipse(dxf, 280, 40, 30, 10, 0.5),
new Arc(dxf, 360, 40, 30, 0, 270)
);
// Save to file
fs.writeFileSync(__dirname + '/example.dxf', dxf.dxf());
``