dxf Documentation

website·Indexed 30 days ago

https://dxf.vercel.app

A 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.

Tokens
4.6K
Snippets
33
Records
35
Agent score
75%

What's inside dxf

  1. Install @tarikjabiri/dxf

    Install the @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/dxf
  2. Define and create reusable Blocks in DXF

    Blocks 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:

    • Block names must not contain the following characters: `< > / \ " : ; ? * | = ``. If these characters are present, they are automatically removed by the library.
    • The default blocks *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");
  3. Define Line Types (LTYPE) and patterns

    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:

    • Descriptive String: Uses underscores (_) for solid units, spaces ( ) for transparent units, and dots (.) for dotted units.
    • Elements Array: Defines the length of each segment. Positive numbers are solid, negative numbers are transparent, and 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]);
  4. Create and manage custom dictionaries

    Use the 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");
  5. Add Extended Data (XDATA) to a DXF entity

    Extended data (XDATA) allows you to attach custom application-specific metadata to a DXF entity as a list of name-value pairs. To use XDATA, you must first register an Application ID (APPID) in the DXF tables, create an entity, and then add an XDATA object to that entity using the registered APPID name.
    // 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)
  6. Insert a Block into a DXF drawing

    To place a previously defined block into the drawing, use the 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));
  7. Create a HATCH entity

    Hatch entities fill enclosed areas. You must define a boundary path (either 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 });
  8. Manage the root dictionary in DXF objects

    The 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");
  9. Add entities to a DXF Block

    Once a block reference is created via 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));
  10. Common options for DXF entities

    All graphical entities in the library share a set of common configuration options used to control appearance and visibility: trueColor, colorNumber, layerName, visible, lineType, and lineTypeScale.
  11. Add a 3DFACE entity

    A 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 }
    );
  12. Add an ATTRIB entity

    The 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);