autocad-dxf

1.1.0 • Public • Published

autocad-dxf

npm version npm downloads

This module is used to parse AutoCAD dxf files and to make programmatic and geometric operations on the AutoCAD drawing entities.

Getting Started

Installation

$ npm install autocad-dxf --save

Usage

Import

import Entities from 'autocad-dxf';

Front-end example

import Entities from 'autocad-dxf';


function fileEventListener() {  
  const [file] = document.querySelector("input[type=file]").files;
  const reader = new FileReader();

  reader.addEventListener(
    "load",
    () => {  
		const tolerance = 0.00001;		
		const res = new Entities(reader.result, tolerance);
		const lines = res.filter({etype: ["line"], layer: ["0"]});
		console.log(lines); 
    },
    false,
  );

  if (file) {
    reader.readAsText(file);
  }
}

Back-end example

const fs = require('fs');
const Entities = require("autocad-dxf");

fs.readFile('C:\\test\\example.dxf', 'utf8', (err, data) => {
  if (err) {
    console.error(err);
    return;
  }
  const tolerance = 0.00001;
  const res = new Entities(data, tolerance);
  const lines = res.filter({etype: ["line"], layer: ["0"]});
  console.log(lines);
});

New updates in this version

  • subclass property is added to all entities. It specifies the subclass marker as per the AutoCAD specification. It replaces the etype parameter in the earlier versions. The etype parameter still exists but defines the type of the entity as given here
  • color, visibility, line_type and text properties are added to parameter keys of filter function.
  • z-coordinates of entities are added to the existing x and y-coordinates.
  • the filter, getCorners, checkEccentric and checkConcentric functions accept additional parameter to specify the coordinate planes on which the geometrical operations will be performed as appropriate.
  • AcDbHelix, AcDbXline, AcDbRasterImage, AcDbLeader, AcDbBlockReference, AcDbModelerGeometry and AcDbFace are included for parsing.

Constructor

The constructor of the Entities class takes two parameters.

const res = new Entities(data, tolerance);

data - is a string data which is read from the dxf file. This is an optional parameter. If not provided, user defined data can be used to call the built-in functions via the Entities class object. The user defined data should be as per the custom keys defined in this module. The list of these custom keys can be accessed via the KEYS property,
tolerance - is a tolerable numerical difference between two numbers to be considered equal. This is an optional parameter. The default value is 0.0001.

Get all parsed data

The entities property holds the parsed data in JSON format.

const Entities = require("autocad-dxf");
const data = "DATA_FROM_DXF_FILE";
const res = new Entities(data);
console.log(res.entities);

Get AutoCAD document information

The tables property holds the list of all AutoCAD file related information. It includes the list of all layers, dimension styles, text styles, line types, blocks, coordinates systems and view ports. The tables property is a json with the following keys: LTYPE, LAYER, VIEW, UCS, APPID, DIMSTYLE, BLOCK_RECORD, VPORT and STYLE with each key having a value of an array of json data associated with the respective key. For instantance, the list of all AutoCAD layers can be printed in the following way.

const Entities = require("autocad-dxf");
const data = "DATA_FROM_DXF_FILE";
const res = new Entities(data);
console.log(res.tables.LAYER);

Get AutoCAD block data

The blocks property holds the list of all blocks along with their details. The blocks property is an array with json elements. Each json element contains four keys: name (name of the block), layer (the name of the layer where the block is defined),base_point(a json with the x-y coordinates of the base point of the block) and entities (an array of the entities forming the block). For instantance, the list of all AutoCAD blocks can be printed in the following way.

const Entities = require("autocad-dxf");
const data = "DATA_FROM_DXF_FILE";
const res = new Entities(data);
console.log(res.blocks);

Custom keys

For the ease of convenience, the blocks, entities and tables properties discussed above use custom keys instead of the AutoCAD dxf codes. If desired, all the custom keys used in this module can be accessed using KEYS link property while the corresponding AutoCAD codes can be referred via the CODES linkproperty.

const Entities = require("autocad-dxf");
const data = "DATA_FROM_DXF_FILE";
const res = new Entities(data);
console.log(res.KEYS);  // list of all keys for all blocks, entities and tables
console.log(res.CODES); // list of all keys along with the descriptions and 
						// corresponding AutoCAD dxf codes for all blocks, 
						// entities and tables

Functions

The following built-in functions can be called on the object of Entities class.

🔷 filter(criteria :object [, entities :Array, plane :string]):

This function is used to filter entities based on a certain criteria. An array of entities can be those which are read from the DXF file or provided as a second parameter. It returns an array of filtered entities.

The criteria parameter has the following properties

Property Type Description
[etype] array The set of entity types to be filtered. The possible values for the elements of the array are: line,mline,circle,polyline,dimension,text,mtext,spline,ellipse,arc. If not given, all entity types are considered.
[layer] array An array of strings (layer names) to filter from.
[color] string/number A string (ByBlock or ByLayer) or a number from 0 to 256 representing AutoCAD color number.
[visibility] string A string (visible or invisible).
[line_type] string A string representing the line type.
[text] object An object which is used to filter texts. It has equals, notequals, starts, notstarts, ends, notends, contains, notcontains,regex,height,style, rotation, operator and i keys. The equals, starts,ends, contains and notcontains keys take string value where: equals filters texts equal to the given text. starts filters texts which start with the given text. ends filters texts which end with the given text. contains filters texts which contain the given text. The notequals, notstarts, notends and notcontains are the corresponding negations. If these properties are provided at the same time, the operator property is used to specify which logical operator (&& or ```
[between] object The bounding coordinates of the entities to be filtered. It has six optional properies: xmin,xmax, ymin,ymax,zmin and zmax. The default value for xmin, ymin and zmin is -Infinity. The default value for xmax, ymax and zmax is Infinity.
[radius] number A radius value, if the etype propery contains circle or arc.
[arc] object The degree of the arc and the unit of the arc angle if the etype propery contains arc. It contains two properties angle which is a number representing the arc angle and unit which is a string which can be either radians or degrees.
[nsides] object A comparison for the number of sides of the entities to be filtered. It contains two properties value and comparison. The value property contains the numerical value of the number of sides to compare and its default value is 1. Whereas comparison is a string which can be one of eq for equal to, gt for greater than, gte for greater than or equal to (default value), lt for less than, lte for less than, ne for not equal to. This property is applicable for polygons (AcDbPolyline) only.

The optional entities parameter can be part or the whole of entities property or a custom made list of entities (json) with keys from this list. The optional plane parameter specifies on which plane that the filterning will be performed and it is applicable when nsides property is defined. Its possible values are x-y (or y-x), y-z(or z-y), and x-z(or z-x). If not given, x-y is used.

Example

Filter lines and texts on layers 'dims' and 'titles';

	const Entities = require("autocad-dxf");
	const data = "DATA_FROM_DXF_FILE";
	
	const res = new Entities(data);
	const filtered = res.filter({
		etype: ["line", "text"], 
		layer: ["dims", "texts"]
	});
	
	console.log(filtered);

Example

Filter all texts which contain the string "2nd" OR which end with the string "floor" (case insensitive);

	const Entities = require("autocad-dxf");
	const data = "DATA_FROM_DXF_FILE";
	
	const res = new Entities(data);
	const filtered = res.filter({
		text: {contains: "2nd", ends: "floor", operator: "or", i: true}
	});
	
	console.log(filtered);

🔷 getCorners(entity :object [, plane :string]):

This function is used to determine the coordinates of corner points (vertices with bends) of a polyline. Only polylines (AcDbPolyline) are supported. Hence, the passed parameter has to be a custom polyline object or a polyline element from the entities property of Entities class object. The function returns an array of corner points or null if the passed entity object is not supported. The optional plane parameter specifies the applicable plane. Its possible values are x-y (or y-x), y-z(or z-y), and x-z(or z-x). If not given, x-y is used.

Example

Get corners of a custom polyline

	const Entities = require("autocad-dxf");
	const data = "DATA_FROM_DXF_FILE";
	
	const res = new Entities(data);
	const polygon = {
		etype: "LWPOLYLINE",
		subclass: "AcDbPolyline",
		number_of_vertices: 7,
		type: "Closed",
		vertices: [{ x: 1099.271933374087, y: 341.0072904353435 },
		   { x: 1103.241801372366, y: 154.6366036367878 },
           { x: 1331.509237026766, y: 122.9139333484436 },
           { x: 1506.18344898143, y: 396.5219634399462 },
           { x: 1597.76655981698, y: 539.9768802531387 },
           { x: 1444.650486423653, y: 539.9768802531387 },
           { x: 1200.503578776138, y: 539.9768802531387 }]		
	};

	const corners = res.getCorners(polygon);
	
	console.log(corners);
	/* returns 
		[
			{ x: 1099.271933374087, y: 341.0072904353435 },
			{ x: 1103.241801372366, y: 154.6366036367878 },
			{ x: 1331.509237026766, y: 122.9139333484436 },
			{ x: 1597.76655981698, y: 539.9768802531387 },
			{ x: 1200.503578776138, y: 539.9768802531387 }
		]
	*/

🔷 checkConcentric(entity1 :object, entity2 :object [, plane :string]):

This function is used to check if two circle objects entity1 and entity2 are concentric. The optional plane parameter specifies the applicable plane. Its possible values are x-y (or y-x), y-z(or z-y), and x-z(or z-x). If not given, x-y is used.

Example

Check if the first and the second elements of the entities property of the object of Entities class are concentric circles.

	const Entities = require("autocad-dxf");
	const data = "DATA_FROM_DXF_FILE";
	
	const res = new Entities(data);
	const areConcentric = res.checkConcentric(res.entities[0], res.entities[1]);
	
	console.log(areConcentric);

🔷 checkEccentric(entity1 :object, entity2 :object [, plane :string]):

This function is used to check if two circle objects entity1 and entity2 are eccentric. The optional plane parameter specifies the applicable plane. Its possible values are x-y (or y-x), y-z(or z-y), and x-z(or z-x). If not given, x-y is used.

Example

Check if the first and the second elements of the entities property of the object of Entities class are eccentric circles.

	const Entities = require("autocad-dxf");
	const data = "DATA_FROM_DXF_FILE";
	
	const res = new Entities(data);
	const areEccentric = res.checkEccentric(res.entities[0], res.entities[1]);
	
	console.log(areEccentric);

Issues or suggestions?

If you have any issues or want to suggest something , your can write it here.

Package Sidebar

Install

npm i autocad-dxf

Weekly Downloads

58

Version

1.1.0

License

MIT

Unpacked Size

186 kB

Total Files

10

Last publish

Collaborators

  • asaye