Skip to content

dotoritos-kim/dxf-json

Repository files navigation

DXF-JSON

License: GPL v3

GitHubGitHub Actions

Visual Studio CodeTypeScriptWebpack

NPM

Perfect Dxf Parser

A library that reliably parses DXF files into JSON files without missing data.

  • It is typesafe.
  • Parse dxf at high speed.
  • Lightweight library

Features

  • Synchronous parsing, asynchronous parsing, and url fetch are possible.
  • It was created for the purpose of parsing the AutoCad Dxf format. Dxf ref
  • It is modularized and divided into Header, Classes, Tables, Blocks, Entities, and Objects sections.
  • It's very simple to use.
  • When I input dxf file it returns json data.

The goal of this library is to fully analyze dxf files from AutoCad and convert them to json files.

Current Coverage

For dev branch status, see #52

Based on AutoCAD 2024 DXF Reference

  • HEADER Section
  • CLASSES Section
  • TABLES Section
    • APPID
    • BLOCK_RECORD
    • DIMSTYLE
    • LAYER
    • LTYPE
    • STYLE
    • UCS
    • VIEW
    • VPORT
  • BLOCKS Section
  • ENTITIES Section
    • 3DFACE
    • 3DSOLID
    • ACAD_PROXY_ENTITY
    • ARC
    • ATTDEF
    • ATTRIB
    • BODY
    • CIRCLE
    • COORDINATION MODEL
    • DIMENSION
    • ELLIPSE
    • HATCH
    • HELIX
    • IMAGE
    • INSERT
    • LEADER
    • LIGHT
    • LINE
    • LWPOLYLINE
    • MESH
    • MLEADER
    • MLEADERSTYLE
    • MLINE
    • MTEXT
    • OLEFRAME
    • OLE2FRAME
    • POINT
    • POLYLINE
    • RAY
    • REGION
    • SECTION
    • SEQEND
    • SHAPE
    • SOLID
    • SPLINE
    • SUND
    • SURFACE
    • TABLE
    • TEXT
    • TOLERANCE
    • TRACE
    • UNDERLAY
    • VERTEX
    • VIEWPORT
    • WIPEOUT
    • XLINE
  • OBJECTS Section
    • DATATABLE
    • DICTIONARY
    • DICTIONARYVAR
    • DIMASSOC
    • FIELD
    • GEODATA
    • GROUP
    • IDBUFFER
    • IMAGEDEF
    • IMAGEDEF_REACTOR
    • LAYER_FILTER
    • LAYER_INDEX
    • LAYOUT
    • LIGHTLIST
    • MATERIAL
    • MLINESTYLE
    • OBJECT_PTR
    • PLOTSETTINGS
    • RASTERVARIABLES
    • RENDER
    • SECTION
    • SORTENSTABLE
    • SPATIAL_FILTER
    • SPATIAL_INDEX
    • SUNSTUDY
    • TABLESTYLE
    • UNDERLAYDEFINITION
    • VBA_PROJECT
    • VISUALSTYLE
    • WIPEOUTVARIABLES
    • XRECORD
  • THUMBNAILIMAGE Section

Reference

I was able to get a lot of ideas from the dxf-parser library.

How do I use it?

npm package:

npm i dxf-json

build:

npm install
npm run build

test:

npm install
npm run test:parser

parseSync:

const parser = new DxfParser()
return parser.parseSync(buffer)

parseStream:

import fs from 'fs'
const parser = new DxfParser();
const fileStream = fs.createReadStream("dxf file path", { encoding: 'utf8' });
return await parser.parseStream(fileStream);

parseUrl:

const parser = new DxfParser();
return await parser.parseFromUrl(url, encoding, RequestInit);

Type

ParsedDxf:

interface ParsedDxf {
    header: DxfHeader;
    blocks: Record<string, DxfBlock>;
    entities: CommonDxfEntity[];
    tables: {
        BLOCK_RECORD?: DxfTable<BlockRecordTableEntry>;
        DIMSTYLE?: DxfTable<DimStylesTableEntry>;
        STYLE?: DxfTable<StyleTableEntry>;
        LAYER?: DxfTable<LayerTableEntry>;
        LTYPE?: DxfTable<LTypeTableEntry>;
        VPORT?: DxfTable<VPortTableEntry>;
    };
    objects: {
        byName: Record<string, CommonDXFObject[]>;
        byTree?: DxfObject;
    };
}

header

DxfHeader:

type DxfHeaderVariable =
...
    | 'DRAGVS'
    | 'INTERFERECOLOR'
    | 'INTERFEREOBJVS'
    | 'INTERFEREVPVS'
    | 'OBSLTYPE'
    | 'SHADEDIF'
    | 'MEASUREMENT';

export type DxfHeader = typeof DefaultDxfHeaderVariables & {
    MEASUREMENT: Measurement;
} & Record<string, any>;

blocks

DxfBlock:

interface DxfBlock {
    type: number;
    name: string;
    name2: string;
    handle: string;
    ownerHandle: string;
    layer: string;
    position: Point3D;
    paperSpace: boolean;
    xrefPath: string;
    entities?: CommonDxfEntity[];
}

entities

CommonDxfEntity:

interface CommonDxfEntity {
    type: string;
    handle: string;
    ownerBlockRecordSoftId?: string;
    isInPaperSpace?: boolean;
    layer: string;
    lineType?: string;
    materialObjectHardId?: string;
    colorIndex?: ColorIndex;
    lineweight?: number;
    lineTypeScale?: number;
    isVisible?: boolean;
    proxyByte?: number;
    proxyEntity?: string;
    color?: ColorInstance;
    colorName?: string;
    transparency?: number;
    plotStyleHardId?: string;
    shadowMode?: ShadowMode;
    xdata?: XData;
    ownerdictionaryHardId?: string | number | boolean;
    ownerDictionarySoftId?: string | number | boolean;
}

tables

tables: {
    BLOCK_RECORD?: DxfTable<BlockRecordTableEntry>;
    DIMSTYLE?: DxfTable<DimStylesTableEntry>;
    STYLE?: DxfTable<StyleTableEntry>;
    LAYER?: DxfTable<LayerTableEntry>;
    LTYPE?: DxfTable<LTypeTableEntry>;
    VPORT?: DxfTable<VPortTableEntry>;
};

DxfTable

interface DxfTable<T extends CommonDxfTableEntry> {
    subclassMarker: 'AcDbSymbolTable';
    name: string;
    handle: string;
    ownerDictionaryIds?: string[];
    ownerObjectId: string;
    maxNumberOfEntries: number;
    entries: T[];
}
interface CommonDxfTableEntry {
    name: string;
    handle: string;
    ownerObjectId: string;
}

BLOCK_RECORD

BlockRecordTableEntry:

interface BlockRecordTableEntry extends CommonDxfTableEntry {
    subclassMarker: 'AcDbBlockTableRecord';
    name: string;
    layoutObjects: string;
    insertionUnits: number;
    explodability: number;
    scalability: number;
    bmpPreview: string;
}

DIMSTYLE

type DimStyleVariable =
    | 'DIMPOST'
    | 'DIMAPOST'
    | 'DIMBLK_OBSOLETE'
    | 'DIMBLK1_OBSOLETE'
    | 'DIMBLK2_OBSOLETE'
    | 'DIMSCALE'
    | 'DIMASZ'
    | 'DIMEXO'
    | 'DIMDLI'
    | 'DIMEXE'
    | 'DIMRND'
    | 'DIMDLE'
    | 'DIMTP'
    | 'DIMTM'
    | 'DIMTXT'
    | 'DIMCEN'
    | 'DIMTSZ'
    | 'DIMALTF'
    | 'DIMLFAC'
    | 'DIMTVP'
    | 'DIMTFAC'
    | 'DIMGAP'
    | 'DIMALTRND'
    | 'DIMTOL'
    | 'DIMLIM'
    | 'DIMTIH'
    | 'DIMTOH'
    | 'DIMSE1'
    | 'DIMSE2'
    | 'DIMTAD'
    | 'DIMZIN'
    | 'DIMAZIN'
    | 'DIMALT'
    | 'DIMALTD'
    | 'DIMTOFL'
    | 'DIMSAH'
    | 'DIMTIX'
    | 'DIMSOXD'
    | 'DIMCLRD'
    | 'DIMCLRE'
    | 'DIMCLRT'
    | 'DIMADEC'
    | 'DIMUNIT'
    | 'DIMDEC'
    | 'DIMTDEC'
    | 'DIMALTU'
    | 'DIMALTTD'
    | 'DIMAUNIT'
    | 'DIMFRAC'
    | 'DIMLUNIT'
    | 'DIMDSEP'
    | 'DIMTMOVE'
    | 'DIMJUST'
    | 'DIMSD1'
    | 'DIMSD2'
    | 'DIMTOLJ'
    | 'DIMTZIN'
    | 'DIMALTZ'
    | 'DIMALTTZ'
    | 'DIMFIT'
    | 'DIMUPT'
    | 'DIMATFIT'
    | 'DIMTXSTY'
    | 'DIMLDRBLK'
    | 'DIMBLK'
    | 'DIMBLK1'
    | 'DIMBLK2'
    | 'DIMLWD'
    | 'DIMLWE';

interface DimStyleVariableSchema {
    name: string;
    code: number;
    defaultValue?: string | number;
    defaultValueImperial?: string | number;
}

type DimStylesTableEntry = {
    subclassMarker: 'AcDbDimStyleTableRecord';
    styleName: string;
    DIMPOST?: string;
    DIMAPOST?: string;
    DIMBLK_OBSOLETE?: string;
    DIMBLK1_OBSOLETE?: string;
    DIMBLK2_OBSOLETE?: string;
    DIMSCALE: number;
    DIMASZ: number;
    DIMEXO: number;
    DIMDLI: number;
    DIMEXE: number;
    DIMRND: number;
    DIMDLE: number;
    DIMTP: number;
    DIMTM: number;
    DIMTXT: number;
    DIMCEN: number;
    DIMTSZ: number;
    DIMALTF: number;
    DIMLFAC: number;
    DIMTVP: number;
    DIMTFAC: number;
    DIMGAP: number;
    DIMALTRND: number;
    DIMTOL: number;
    DIMLIM: number;
    DIMTIH: number;
    DIMTOH: number;
    DIMSE1: 0 | 1;
    DIMSE2: 0 | 1;
    DIMTAD: DimensionTextVertical;
    DIMZIN: DimensionZeroSuppression;
    DIMAZIN: DimensionZeroSuppressionAngular;
    DIMALT: 0 | 1;
    DIMALTD: number;
    DIMTOFL: 0 | 1;
    DIMSAH: 0 | 1;
    DIMTIX: 0 | 1;
    DIMSOXD: 0 | 1;
    DIMCLRD: number;
    DIMCLRE: number;
    DIMCLRT: number;
    DIMADEC?: number;
    DIMUNIT?: number;
    DIMDEC: number;
    DIMTDEC: number;
    DIMALTU: number;
    DIMALTTD: number;
    DIMAUNIT: number;
    DIMFRAC: number;
    DIMLUNIT: number;
    DIMDSEP: string;
    DIMTMOVE: undefined; // 미사용
    DIMJUST: DimensionTextHorizontal;
    DIMSD1: 0 | 1;
    DIMSD2: 0 | 1;
    DIMTOLJ: DimensionTextVertical;
    DIMTZIN: DimensionZeroSuppression;
    DIMALTZ: DimensionZeroSuppression;
    DIMALTTZ: DimensionZeroSuppression;
    DIMFIT?: number;
    DIMUPT: number;
    DIMATFIT: number;
    DIMTXSTY?: string;
    DIMLDRBLK?: string;
    DIMBLK?: string;
    DIMBLK1?: string;
    DIMBLK2?: string;
    DIMLWD: number;
    DIMLWE: number;
} & CommonDxfTableEntry;

type StyleResolver = <Name extends DimStyleVariable>(
    variableName: Name,
) => DimStylesTableEntry[Name];

STYLE

interface StyleTableEntry extends CommonDxfTableEntry {
    subclassMarker: 'AcDbTextStyleTableRecord';
    name: string;
    standardFlag: number;
    fixedTextHeight: number;
    widthFactor: number;
    obliqueAngle: number;
    textGenerationFlag: number;
    lastHeight: number;
    font: string;
    bigFont: string;
    extendedFont?: string;
}

LAYER

interface LayerTableEntry extends CommonDxfTableEntry {
    subclassMarker: 'AcDbLayerTableRecord';
    name: string;
    standardFlag: number;
    colorIndex: ColorIndex;
    lineType: string;
    isPlotting: boolean;
    lineweight: number;
    plotStyleNameObjectId?: string;
    materialObjectId?: string;
}

LTYPE

interface LTypeTableEntry extends CommonDxfTableEntry {
    subclassMarker: 'AcDbLinetypeTableRecord';
    name: string;
    standardFlag: number;
    description: string;
    numberOfLineTypes: number;
    totalPatternLength: number;
    pattern?: LineTypeElement[];
}
interface LineTypeElement {
    elementLength: number;
    elementTypeFlag: number;
    shapeNumber?: number;
    styleObjectId?: string;
    scale?: number;
    rotation?: number;
    offsetX?: number;
    offsetY?: number;
    text?: string;
}

VPORT

interface VPortTableEntry extends CommonDxfTableEntry {
    subclassMarker: 'AcDbViewportTableRecord';
    name: string;
    standardFlag: number;
    lowerLeftCorner: Point2D;
    upperRightCorner: Point2D;
    center: Point2D;
    snapBasePoint: Point2D;
    snapSpacing: Point2D;
    gridSpacing: Point2D;
    viewDirectionFromTarget: Point3D;
    viewTarget: Point3D;
    lensLength: number;
    frontClippingPlane: number;
    backClippingPlane: number;
    viewHeight: number;
    snapRotationAngle: number;
    viewTwistAngle: number;
    circleSides: number;
    frozenLayers: string[];
    styleSheet: string;
    renderMode: RenderMode;
    viewMode: number;
    ucsIconSetting: number;
    ucsOrigin: Point3D;
    ucsXAxis: Point3D;
    ucsYAxis: Point3D;
    orthographicType: OrthographicType;
    elevation: number;
    shadePlotSetting: number;
    majorGridLines: number;
    backgroundObjectId?: string;
    shadePlotObjectId?: string;
    visualStyleObjectId?: string;
    isDefaultLightingOn: boolean;
    defaultLightingType: DefaultLightingType;
    brightness: number;
    contrast: number;
    ambientColor?: number;
}

objects

objects: {
    byName: Record<string, CommonDXFObject[]>;
    byTree?: DxfObject;
};

byName

CommonDXFObject:

interface CommonDXFObject {
    ownerObjectId: string;
    ownerDictionaryIdHard: string;
    ownerDictionaryIdSoft: string;
    handle: string;
}

DxfObject

interface DxfObject {
    name: string;
    handle: string;
    ownerDictionaryIdSoft: string;
    ownerDictionaryIdHard?: string;
    ownerObjectId?: string;
}

Please refer to the dxf reference for undefined elements and leave them as issues!

License

GPL-3.0 license

Sponsor this project

 

Contributors 4

  •  
  •  
  •  
  •