dxf-writer

repository·master·Indexed 41 days ago

https://github.com/tarikjabiri/js-dxf

A simple JavaScript library for generating 2D and 3D DXF (Drawing Exchange Format) files, compatible with Node.js and browser environments. Version 1.18.4 supports a wide range of entities including arcs, circles, ellipses, lines, polylines, splines, and 3DFaces, along with custom line types, layers, and various measurement units.

Tokens
3.8K
Snippets
15
Records
19
Agent score
52%

What's inside dxf-writer

  1. Initialize and export a DXF drawing

    master

    To create a DXF file, instantiate the Drawing class, add your desired entities using the drawing methods, and call generateAutocadExtras() to ensure compatibility with AutoCAD products. Finally, call toDxfString() to get the serialized DXF content.

    Important: You must call generateAutocadExtras() before exporting to ensure required metadata (like ACADVER, VPORT, and STYLE tables) is included.

    const Drawing = require('js-dxf');
    
    const drawing = new Drawing();
    
    // 1. Add entities
    drawing.drawLine(0, 0, 10, 10);
    
    // 2. CRITICAL: Generate AutoCAD compatibility metadata
    drawing.generateAutocadExtras();
    
    // 3. Export to string
    const dxfString = drawing.toDxfString();
  2. Use dxf-writer in Node.js

    master

    In a Node.js environment, you can create a new Drawing instance, define layers, draw various entities, and export the result as a DXF string. The API supports both standard method calls and a fluent interface for chaining commands like addLayer and setActiveLayer.

    const Drawing = require('dxf-writer');
    const fs = require('fs');
    
    let d = new Drawing();
    
    d.setUnits('Decimeters');
    d.drawText(10, 0, 10, 0, 'Hello World'); // draw text in the default layer named "0"
    d.addLayer('l_green', Drawing.ACI.GREEN, 'CONTINUOUS');
    d.setActiveLayer('l_green');
    d.drawText(20, -70, 10, 0, 'go green!');
    
    //or fluent
    d.addLayer('l_yellow', Drawing.ACI.YELLOW, 'DOTTED')
     .setActiveLayer('l_yellow')
     .drawCircle(50, -30, 25);
    
    fs.writeFileSync(__filename + '.dxf', d.toDxfString());
  3. Define custom line types

    master

    The library provides three built-in line types: CONTINUOUS, DASHED, and DOTTED. You can also define custom line types using the addLineType method.

    let d = new Drawing();
    d.addLineType('DASHDOT', '_ . _ ', [0.5, -0.5, 0.0, -0.5]);
  4. Supported colors and units

    master

    Supported Colors

    • red
    • green
    • cyan
    • blue
    • magenta
    • white

    Supported Units

    • Unitless
    • Inches
    • Feet
    • Miles
    • Millimeters
    • Centimeters
    • Meters
    • Kilometers
    • Microinches
    • Mils
    • Yards
    • Angstroms
    • Nanometers
    • Microns
    • Decimeters
    • Decameters
    • Hectometers
    • Gigameters
    • Astronomical units
    • Light years
    • Parsecs
  5. Manage layers and colors in a Drawing

    master

    Entities are drawn on the activeLayer. You can create new layers using addLayer(name, colorNumber, lineTypeName) and switch between them using setActiveLayer(name).

    To set the color of the current layer using AutoCAD Color Index (ACI), use setTrueColor(trueColor). You can use the Drawing.ACI constant for standard colors.

    Available ACI Colors:

    • Drawing.ACI.LAYER (0)
    • Drawing.ACI.RED (1)
    • Drawing.ACI.YELLOW (2)
    • Drawing.ACI.GREEN (3)
    • Drawing.ACI.CYAN (4)
    • Drawing.ACI.BLUE (5)
    • Drawing.ACI.MAGENTA (6)
    • Drawing.ACI.WHITE (7)
    const drawing = new Drawing();
    
    // Add a custom layer
    drawing.addLayer("Construction", Drawing.ACI.RED, "CONTINUOUS");
    
    // Switch to it
    drawing.setActiveLayer("Construction");
    
    // Draw on that layer
    drawing.drawLine(0, 0, 5, 5);
  6. Draw 2D and 3D entities

    master

    The Drawing class provides several methods to add geometric shapes to the active layer. Most methods return this to allow for method chaining.

    2D Entities

    • drawLine(x1, y1, x2, y2)
    • drawPoint(x, y)
    • drawRect(x1, y1, x2, y2, cornerLength, cornerBulge): Draws a rectangle. If cornerLength is provided, it creates a rounded rectangle.
    • drawPolygon(x, y, numberOfSides, radius, rotation = 0, circumscribed = false)
    • drawArc(x1, y1, r, startAngle, endAngle)
    • drawCircle(x1, y1, r)
    • drawPolyline(points, closed = false, startWidth = 0, endWidth = 0): points is an array of [x, y].
    • drawText(x1, y1, height, rotation, value, horizontalAlignment = "left", verticalAlignment = "baseline")
    • drawEllipse(x1, y1, majorAxisX, majorAxisY, axisRatio, startAngle = 0, endAngle = 2 * Math.PI)
    • drawSpline(controlPoints, degree = 3, knots = null, weights = null, fitPoints = [])

    3D Entities

    • drawLine3d(x1, y1, z1, x2, y2, z2)
    • drawPolyline3d(points): points is an array of [x, y, z].
    • drawCylinder(x1, y1, z1, r, thickness, extrusionDirectionX, extrusionDirectionY, extrusionDirectionZ)
    • drawFace(x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4)
  7. Use the Drawing class to create DXF files

    master

    The js-dxf library provides a Drawing class as its primary entry point. You can instantiate this class to create a new DXF drawing and then use its methods to add various geometric entities (like lines, circles, arcs, etc.) to the file. Once the drawing is populated, you can export it as a DXF-formatted string.

    const Drawing = require('js-dxf');
    
    const drawing = new Drawing();
    // Add entities to the drawing here...
    const dxfString = drawing.toDXFString();
  8. Create a Spline entity

    master

    The Spline class allows you to create spline entities for DXF files. You can define the spline using control points, a specific degree (quadratic or cubic), an optional knot vector, and optional weights for rational splines. If no knot vector is provided, a uniform knot vector is automatically generated.

    Constraints:

    • The number of controlPoints must be at least degree + 1.
    • If weights are provided, there must be exactly one weight for each control point.
    • If knots are provided, the length must be exactly controlPoints.length + degree + 1.
    const Spline = require('dxf-writer').Spline; // Assuming standard export pattern
    
    const controlPoints = [[0, 0], [1, 1], [2, 0]];
    const degree = 2; // Quadratic spline
    
    const spline = new Spline(controlPoints, degree);
  9. Configure Drawing units

    master

    Set the drawing units using setUnits(unit). The unit string must correspond to one of the keys in Drawing.UNITS.

    Supported Units:

    • Unitless (0)
    • Inches (1)
    • Feet (2)
    • Miles (3)
    • Millimeters (4)
    • Centimeters (5)
    • Meters (6)
    • Kilometers (7)
    • Microinches (8)
    • Mils (9)
    • Yards (10)
    • Angstroms (11)
    • Nanometers (12)
    • Microns (13)
    • Decimeters (14)
    • Decameters (15)
    • Hectometers (16)
    • Gigameters (17)
    • Astronomical units (18)
    • Light years (19)
    • Parsecs (20)
    const drawing = new Drawing();
    drawing.setUnits("Millimeters");
  10. Spline constructor parameters

    master

    The Spline constructor accepts the following parameters:

    ParameterTypeDescription
    controlPointsArray<[number, number]>An array of control points, e.g., [[x1, y1], [x2, y2]].
    degreenumberThe degree of the spline. 2 for quadratic, 3 for cubic. Defaults to 3.
    knotsArray<number>The knot vector array. If null, a uniform knot vector is used. Defaults to null.
    weightsArray<number>Control point weights. If provided, must have one weight per control point. Defaults to null.
    fitPointsArray<[number, number]>An array of fit points. Defaults to [].