# DXFLib

# About Library

DXFLib is a library written in MATLAB that allows to create simple AutoCAD DXF files. Autocad DXF is a popular file format that can be interpreted in many CAD-related applications. AutoCAD DXF can be also converted to a PDF file containing 3D geometry using any 3D printer driver (for example the one bundled in the free Bentley View application).

The DXFLib handles DXF layers and ACI Colors (RGB colors are automatically converted to their ACI counterparts). It is capable of exporting point, polylines, polysurfaces, text, various 3D primitives and 2D markers.

# Download

Current version of DXFLib:

### DXFLib-0.9.1

DXF MATLAB Library (Version: 0.9.1). ...

693.10 KB 570# Installation

Just unpack zipped m-files to your working directory or create a separate directory and add its location to MATLAB path. All m-files are located in to root directory. The /examples directory contains DXF files which are generated by dxf_test.m script. Some DXF files are converted to 3D PDF files using Bentley View 3D printer driver.

# How to use it?

The easiest way is to run the **dxf_test.m** script and then open it in MATLAB editor and compare generated sample DXF files with the code in the script. Obviously, you’ll need any **DXF viewer** that is capable to handle 3D content. I recommend to use the free Bentley View software (http://www.bentley.com/en-US/Products/Bentley+View/). The advantage of using Bentley View is that this software is capable to **convert** DXF files into **PDF** files containing 3D content (examples of DXF/PDF files are included in the library, you can also download them separately from this page).

# Examples

Example DXF and 3D PDF files are located in the */examples* subdirectory of DXFLib. You can also create the DXF files by calling **dxf_test.m** routine.

## Example 1 – Simple cloud of points

We create a very simple DXF file containing randombly generated points. Firstly we have to open DXF file:

FID = dxf_open('example1_points.dxf');

The **dxf_open** returns FID which is a structure containing handle to DXF file and parameters that are used by remaining routines. DXFLib is a ‘state’ library. That means that certain values of parameters (e.g. color, layer) are preserved between the following calls to various routines unless they are changed. By default, the color is ACI color 255 (white) and the layer number is 0. You can modify state parameters using **dxf_set** routine.

FID = dxf_set(FID,'Color',[1 1 0],'Layer',20);

We set up different color and layer. NOTE that **dxf_set** always returns FID structure back to you!!! You must always use the **dxf_set** in the way presented above in order to preserve changes made to the state parameters. As you see we set current Color to yellow and current layer to 20. This will be used in future calls to any DXF routine until changed.

Now let’s produce a cloud of points using **dxf_point** routine:

dxf_point(FID,10*rand(1,2000),10*rand(1,2000),10*rand(1,2000));

Finally, we have to close the DXF file:

dxf_close(FID);

### Download

**Example 1 - Points 82 downloads 0**

## Example 2 – Polymesh

Let’s open the file and set up basic properties for color and layer:

FID = dxf_open('example3_polymesh.dxf'); FID = dxf_set(FID,'Color',[1 1 0],'Layer',10);

Now we generate a mesh using standard MATLAB routines. We will create a mesh of trianges using the very-famous MATLAB’s **peaks** routine. Then we convert it to the patch structure using **surf2patch**:

[x,y,z] = peaks; z = z/5; fvc = surf2patch(x,y,z,'triangles');

The returned fvc structure array contains two important elements – vertices and faces fields. We use these matrices to create a polymesh and then we’ll close the DXF file:

dxf_polymesh(FID, fvc.vertices, fvc.faces); dxf_close(FID);

### Download

**Example 3 - Polymesh 48 downloads 0**

**Example 3 - Polymesh 101 downloads 0**

## Example 3 – Using primitives

The **dxf_primitive** creates 3D primitives at specified points (X,Y,Z). Additionally, we can specify the size of each primitive and its color. Here we create four differnt types (families) of primitives at random locations. Notice that because you specify color directly in the **dxf_primitive** routines, the color state variable is ignored.

FID = dxf_open('example4_primitives.dxf'); FID = dxf_set(FID,'Layer',1); % We put each family to different layers. FID = dxf_primitive(FID,'triangle',rand(1,10)*20-10, ... rand(1,10)*20-10,rand(1,10)*20-10,rand(1,10)*3,1:10); FID = dxf_set(FID,'Layer',2); FID = dxf_primitive(FID,'box',rand(1,10)*20+20, ... rand(1,10)*20+20,rand(1,10)*20-10,rand(1,10)*3,1:10);

For the last-but-one family we use the common size equal to 5:

FID = dxf_set(FID,'Layer',3); FID = dxf_primitive(FID,'tetrahedron',rand(1,10)*20-10, ... rand(1,10)*20+20,rand(1,10)*20-10,5,1:10);

However, for the last family we specify the color using **dxf_set** routine:

FID = dxf_set(FID,'Layer',4,'Color',[0 1 0]); FID = dxf_primitive(FID,'sphere',rand(1,10)*20+20, ... rand(1,10)*20-10,rand(1,10)*20-10,rand(1,10)*3); dxf_close(FID);

### Download

**Example 4 - Primitives 24 downloads 0**

**Example 4 - Primitives 50 downloads 0**

## Example 4 – ACI Colors

Let’s play with the dxf_polymesh and ACI color together. In most calls to DXFLib you can specify either RBG color or so-called ACI color. RGB colors are always automatically converted to the closest matching ACI color. ACI color stands for AutoCAD Color Index and essentially it is an indexed table of 256 colors. This example plots the whole ACI color table:

FID = dxf_open('example7_ACIcolors.dxf'); i = 1; for x=1:16 for y=(1:16)*2 FID = dxf_set(FID,'Color',i); dxf_polymesh(FID,[x y 0; x+0.9 y 0; x+0.9 y+0.9 0; x y+0.9 0],... [1 2 3; 1 3 4]); FID = dxf_set(FID,'Color',255); dxf_text(FID,x,y+1,0,i,'TextHeight',0.3); i = i + 1; end end dxf_close(FID);

### Download

**Example 7 - ACI Colors 22 downloads 0**

**Example 7 - ACI Colors 37 downloads 0**

## Example 5 – Colorful polymesh

This example is a bit tricky. This is because apparently the DXF format does not support per-vertex color matrix (what is the typical output from MATLAB’s **surf2patch** function). Therefore, we have to prepare our own PER-FACE color matrix using Z coordinates:

[X,Y,Z] = peaks(100); fvc = surf2patch(X,Y,Z/5,'triangles'); % re-meshing to triangles. F = fvc.faces; V = fvc.vertices;

We have faces and vertices, but unfortunately we cannot use C=fvc.colors as it is per-vertex color matrix which is apparently NOT supported by DXF (please let me know if it is not the case!). Therefore, we create our own per-face matrix of per-face colors:

C = mean([V(F(:,1),3) V(F(:,2),3) V(F(:,3),3)],2); C = (C - min(C(:))) ./ (max(C(:)) - min(C(:))); C = 1 + round(C*255) ; CMAP = jet(256); C = CMAP(C,:); % now we have (N faces x 3) color matrix.

What we’re doing above is we calculate the average value of Z component for each triangular face. Then, we scale this Z value to 1-255 range and transfer to appropriate RGB color taken from **jet** colormap. Now we convert RGB per-face color matrix C into ACI color using **dxf_rgb2aci** routine:

C = dxf_rgb2aci(C);

Finally we produce our polymesh with colors:

FID = dxf_open('example9_polymesh_colored.dxf'); dxf_polymesh(FID,V,F,C); dxf_close(FID);

### Download

**Example 9 - Polymesh Colored 31 downloads 0**

**Example 9 - Polymesh Colored 59 downloads 0**