autocad-dxf

1.1.1 • 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

  • length and distance functions are added.

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 will be 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 will be 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 will be 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 will be 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);

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

This function is used to determine the shortest distance between two entities: entity1 and entity2 as described below. Both or one of these entities can be a one-dimensional array with a format of: [x, y].

  • If both entity1 and entity2 are points (AcDbPoint) or texts(AcDbText/AcDbMText) or vertices(AcDbVertex) or arrays or any combination of these, the distance between the two points will be returned.
  • If both entity1 and entity2 are circles or arcs or ellipses or any combination of these, the distance between the centers will be returned.
  • If either entity1 or entity2 is a array/point/circle/text/vertex/ellipse and the other parameter is a line, the perpendicular distance between the point/center to (extension of) the line will be returned.
  • If either entity1 or entity2 is a array/point/circle/text/vertex/ellipse and the other parameter is a polyline, the perpendicular distance between the point/center to (extension of) the closest edge will be returned.
  • If both entity1 or entity2 are lines which are parallel, the perpendicular distance between (extensions of) the lines will be returned. If the lines are not parallel, undefined is returned.
  • If the passed parameters are none of the above combinations, undefined is returned.

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 will be used.

Example

Get the closest distance between a circle and a line.

	const Entities = require("autocad-dxf");
	const data = "DATA_FROM_DXF_FILE";
	
	const res = new Entities(data);
	const line = {
		etype: 'LINE',
		line_type: 'ByLayer',
		color: 'ByLayer',
		layer: 'Layer1',
		subclass: 'AcDbLine',
		start_x: 76.48716497852402,
		start_y: -120.4229218048302,
		start_z: 0,
		end_x: 888.3252621940712,
		end_y: -29.22024472584008,
		end_z: 0
	};
	const circle = {
		etype: 'CIRCLE',
		line_type: 'ByLayer',
		color: 'ByLayer',
		layer: 'Layer1',
		subclass: 'AcDbCircle',
		x: 493.8669949609207,
		y: 505.568641983396,
		z: 0,
		radius: 165.089285258525
	};
	const distance = res.distance(line, circle);
	
	console.log(distance);  // prints 575.4826584729997

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

This function is used to determine the length of an entity as described below.

  • If entity is a line/circle/arc, the length/circumference/arc length of the line/circle/arc will be returned.
  • If entity is a polyline, the sum of the lengths of each sides of the polyline will be returned.
  • If entity is a full ellipse, an approximate circumference of the ellipse using Ramanujan's second formula will be returned.

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 will be used.

Example

Get the circumference of a circle.

	const Entities = require("autocad-dxf");
	const data = "DATA_FROM_DXF_FILE";
	
	const res = new Entities(data);
	
	const circle = {
		etype: 'CIRCLE',
		line_type: 'ByLayer',
		color: 'ByLayer',
		layer: 'Layer1',
		subclass: 'AcDbCircle',
		x: 493.8669949609207,
		y: 505.568641983396,
		z: 0,
		radius: 165.089285258525
	};
	const length = res.length(circle);
	
	console.log(length);  // prints 1037.2865715091436

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

28

Version

1.1.1

License

MIT

Unpacked Size

195 kB

Total Files

10

Last publish

Collaborators

  • asaye