Install @tarikjabiri/dxf
@tarikjabiri/dxf package using your preferred package manager (pnpm, npm, or yarn).# Using pnpm
pnpm add @tarikjabiri/dxf
# Using npm
npm i @tarikjabiri/dxf
# Using yarn
yarn add @tarikjabiri/dxfwebsite·Indexed 30 days ago
https://dxf.vercel.appA library for working with DXF files, featuring support for entities like ARC and LWPOLYLINE, blocks, objects, tables, and extended data (XDATA). Includes guides on installation, header variables, and using DxfWriter.
@tarikjabiri/dxf package using your preferred package manager (pnpm, npm, or yarn).# Using pnpm
pnpm add @tarikjabiri/dxf
# Using npm
npm i @tarikjabiri/dxf
# Using yarn
yarn add @tarikjabiri/dxfBlocks are reusable definitions stored in the BLOCKS section of a DXF file and are utilized by the INSERT entity. Use the addBlock() factory function to create a block, store it in the DXF writer, and obtain a reference to it.
Constraints:
*Model_Space and *Paper_Space cannot be used with the INSERT entity.import { DxfWriter } from "@tarikjabiri/dxf";
const dxf = new DxfWriter();
const myBlock = dxf.addBlock("myBlock");Line types are stored in the LTYPE table. Every line type used in a file must be defined here to be valid in CAD software. ByBlock, ByLayer, and Continuous are added by default.
Pattern Logic:
_) for solid units, spaces ( ) for transparent units, and dots (.) for dotted units.0 is used for dots.Example: For a descriptive string '____ _ ' with unit length 1, the elements array is [4, -1, 1, -1] (4 solid, 1 transparent, 1 solid, 1 transparent).
import { DxfWriter } from "@tarikjabiri/dxf";
const dxf = new DxfWriter();
// addLType(name, descriptive, elements)
const axesLType = dxf.addLType("AXES", "____ _ ", [4, -1, 1, -1]);addDictionary() method to create a new dictionary object within the DXF objects section. Once created, you can add specific entries to that dictionary using the addEntry(key, value) method.import { DxfWriter } from "@tarikjabiri/dxf";
const dxf = new DxfWriter();
const dic = dxf.objects.addDictionary();
// Add an entry to the dictionary
dic.addEntry("example", "1B");// 1. Register the Application ID
const appIdTest = dxf.tables.addAppId("TEST_APPID")
// 2. Create an entity (e.g., a line)
const line = dxf.addLine(point3d(), point3d(100, 100))
// 3. Associate XDATA with the entity using the APPID
const xdataTest = line.addXData(appIdTest.name)addInsert() method. This requires the name of the block and the 3D point where it should be inserted.import { DxfWriter, point3d } from "@tarikjabiri/dxf";
const dxf = new DxfWriter();
// 1. Define the block
const myBlock = dxf.addBlock("myBlock");
myBlock.addCircle(point3d(0, 0, 0), 20);
// 2. Insert the block into the drawing
dxf.addInsert(myBlock.name, point3d(0, 0, 0));HatchPolylineBoundary or HatchEdgesTypeData, but not both) and a fill pattern (solid or gradient). Note that this method creates non-associative hatches.import { DxfWriter, HatchPolylineBoundary, vertex, HatchBoundaryPaths, pattern, HatchPredefinedPatterns } from "@tarikjabiri/dxf";
const dxf = new DxfWriter();
// 1. Define Boundary
const polyline = new HatchPolylineBoundary();
polyline.add(vertex(0, 0));
polyline.add(vertex(0, 10000));
polyline.add(vertex(10000, 10000));
polyline.add(vertex(10000, 0));
const boundary = new HatchBoundaryPaths();
boundary.addPolylineBoundary(polyline);
// 2. Define Pattern (Solid example)
const solid = pattern({ name: HatchPredefinedPatterns.SOLID });
// 3. Add Hatch
const hatch = dxf.addHatch(boundary, solid, { colorNumber: 3 });objects section of a DXF file contains a root dictionary, which is the first object in the section. It can own subsequent objects and contain multiple entries. You can access the root dictionary via dxf.objects.root or add entries to it directly using addEntryToRoot(key, value).import { DxfWriter } from "@tarikjabiri/dxf";
const dxf = new DxfWriter();
// Get reference to root dictionary
const root = dxf.objects.root;
// Add an entry to the root dictionary
dxf.objects.addEntryToRoot("ACAD_IMAGE_DICT", "1A");addBlock(), you can populate it with entities (such as circles, lines, etc.) by calling the entity methods directly on the block object.import { DxfWriter, point3d } from "@tarikjabiri/dxf";
const dxf = new DxfWriter();
const myBlock = dxf.addBlock("myBlock");
// Add entities to the block definition
myBlock.addCircle(point3d(0, 0, 0), 20);
myBlock.addLine(point3d(0, 0, 0), point3d(0, 20, 0));trueColor, colorNumber, layerName, visible, lineType, and lineTypeScale.3DFACE is a flat surface with 4 corners. To create a 3-cornered face, set the fourth corner to the same coordinates as the third. You can control edge visibility via methods or the invisibleEdges option using InvisibleEdgeFlags.import { DxfWriter, point3d, InvisibleEdgeFlags } from "@tarikjabiri/dxf";
const dxf = new DxfWriter();
// Basic 3D Face
const my3dFace = dxf.add3dFace(
point3d(0, 0), point3d(0, 100), point3d(100, 100), point3d(100, 0)
);
// Control visibility via methods
my3dFace.setEdgesVisible(true);
// Control visibility via options
dxf.add3dFace(
point3d(0, 0), point3d(0, 1000), point3d(100, 1000), point3d(100, 0),
{ invisibleEdges: InvisibleEdgeFlags.First | InvisibleEdgeFlags.Fourth }
);ATTRIB entity represents a text value associated with a tag. While it can be a standalone entity, it is most commonly appended to an Insert entity and should be added immediately after the Insert call.import { DxfWriter, point3d } from "@tarikjabiri/dxf";
const dxf = new DxfWriter();
const doc = dxf.document;
const blockA = doc.addBlock('blockNameA');
const msp = doc.modelSpace;
const insertA = msp.addInsert(blockA.name, point3d(0, 0));
const attribA = msp.addAttrib(point3d(0,0), 100, 'attributeName', 'attributeValue', insertA);