Engineering Knowledge

Free software, projects, tutorials & more!
Home     Links     Site Map     Contact Us     About      

 You are here: Home > Tutorials > DXF to BMP Algorithm 

 

DXF to BMP Algorithm

By Engineering Knowledge administrator Rev1.0

Copyright©  Engineering Knowledge 2007. All rights reserved. 

 

 

The overall organization of a DXF  

file is as follows:
HEADER section. General information about the drawing is found in this section. It consists of an AutoCAD database version number and a number of system variables. Each parameter contains a variable name and its associated value.
CLASSES section. Holds the information for application-defined classes, whose instances appear in the BLOCKS, ENTITIES, and OBJECTS sections of the database. A class definition is permanently fixed in class hierarchy.
TABLES section. This section contains definitions for the following symbol tables.
APPID (application identification table)
BLOCK_RECORD (block reference table)
DIMSTYLE (dimension style table)
LAYER (layer table)
LTYPE (linetype table)
STYLE (text style table)
UCS (User Coordinate System table)
VIEW (view table)
VPORT (viewport configuration table)
BLOCKS section. Contains block definition and drawing entities that make up each block reference in the drawing.
ENTITIES section. This section contains the graphical objects (entities) in the drawing, including block references (insert entities).
OBJECTS section. Contains the nongraphical objects in the drawing. All objects that are not entities or symbol table records or symbol tables are stored in this section. Examples of entries in the OBJECTS section are dictionaries that contain mline styles and groups. 

BMP header description: 

00- 01 $00 -$01 ASCII 2-byte "BM" bitmap identifier.
02- 05 $02 -$05 Total length of bitmap file in bytes.
Four byte integer, LSB first.
06- 09 $06 -$09 Reserved, possibly for image id or revision.
Four byte integer, LSB first.
10-13 $0A-$0D Offset to start of actual pixel data.
Four byte integer, LSB first.
14-17 $0A-$11 Size of data header, usually 40 bytes.
Four byte integer, LSB first.
18- 21 $12 -$15 Width of bitmap in pixels.
Four byte integer, LSB first.
22- 25 $16 -$19 Height of bitmap in pixels.
Four byte integer, LSB first.
26-27 $1A-$1B Number of color planes. Usually 01
Two byte integer, LSB first.
28- 29 $1C -$1D Number of bits per pixel. Sets color mode.
Two byte integer, LSB first.
1 - Monochrome
4 - 16 lookup colors
8 - 256 lookup colors
16 - 65,536 lookup colors
24 - 16,777,216 RGB colors
32 - 16,777,216 RGB colors + alpha
30- 33 $1E -$21 Non-lossy compression mode in use
Four byte integer, LSB first.
0 - None
1 - 8-bit run length encoded
1 - 4-bit run length encoded
34- 37 $22 -$25 Size of stored pixel data
Four byte integer, LSB first.
38- 41 $26 -$29 Width resolution in pixels per meter
Four byte integer, LSB first.
42-45 $2A-$2D Height resolution in pixels per meter
Four byte integer, LSB first.

If you want to code a program to access a file generated by CAD software, DXF format is the ideal source to start the job "Please refer to the right section for a brief description of DXF format". This tutorial will provide you the basics of converting CAD file into an image file, entities discussed here are Point, Line, Circle and Ellipse.

When you consider the structure of BMP image format, you can realize immediately that this is the best format for the output image, because each pixel is usually independently available for any modification. 255 indicate white pixel and 0 indicate black pixel.

If the programming language that you are using does not support writing image files directly, you have to write the output image manually by writing file header then data. For more information about BMP header, please refer to "BMP header description" at the right side.

 

 Tip:

To adjust the scale exactly between the DXF drawing and the output image, you should consider a multiplier for each entity, "Take 17.833 as default value and don't forget to round the result!"

In order to generate image data, program should go through the following algorithm:

-Generate an array with same drawing dimension, to hold image data. All values should be "255"
-Read each entity vector data from DXF file under "ENTITIES" section, look for a Subclass marker and take the entities value under each Group code as show in entities table bellow.
-Apply these in the appropriate equation to generate (x, y) values, write "0" in image array for each (x, y) point.
-Write this array on the disk as an image file.


 GroupDescription
100

 (AcDbPoint)

10

X value

20

Y value


Points:

 
Group codes Description
Set Array(x, y)=0 for each point.

 

 

 
 

 

 Group Description
100

 (AcDbLine)

10

Start point X 

20Start point Y
11 End point X
21

End point Y 

Lines:

 

If there is a slope in the line:


for x=x1 to x2
y=round((-y1*x2-y2*x+y2*x1+y1*x)/(-x2+x1));
array(x,y)=0;
end

 



Circle:
 Group Description
100

 (AcDbCircle)

10

Center X "xc" 

20Center Y "yc"
40Radius "r"
21

End point Y 

 

Apply the following algorithm to get the circle:

For x=xc-r to xc+r
y1=round(yc+(-x^2+2*x*xc-xc^2+r^2)^(1/2));
y2=round(yc-(-x^2+2*x*xc-xc^2+r^2)^(1/2));
array(x,y1)=0;
array(x,y2)=0;
Next
For y=yc-r to yc+r
x1=round(xc+(2*y*yc-yc^2-y^2+r^2)^(1/2));
x2=round(xc-(2*y*yc-yc^2-y^2+r^2)^(1/2));
array(x1,y)=0;
array(x2,y)=0;
Next

 


 

Ellipses:


 

 Group Description
100

 (AcDbEllipse)

10

Center point X

20Center point Y
11 X endpoint of major radius "ax"
21  Y endpoint of major radius "ay"
40

 Ratio of minor to major radius "radius_ratio"


To find the length of major and minor radius:
a=sqrt(ax^2+ay^2);
b=a*radius_ratio;

Apply the following algorithm to get the ellipse:
for y=yc-b to yc+b
x1=round((b*xc+(2*a^2*y*yc-a^2*yc^2-a^2*y^2+a^2*b^2)^(1/2))/b); (1)

x2=round((b*xc-(2*a^2*y*yc-a^2*yc^2-a^2*y^2+a^2*b^2)^(1/2))/b);(2)

if y>0 && x1>0

array(x1,y)=0;
end
if y>0 && x2>0
array(x2,y)
=0;
end
next
for x=xc-a to xc+a
y1=round((a*yc+(-b^2*x^2+2*b^2*x*xc-b^2*xc^2+a^2*b^2)^(1/2))/a); (3)
y2=round((a*yc-(-b^2*x^2+2*b^2*x*xc-b^2*xc^2+a^2*b^2)^(1/2))/a);(4)
if y1>0 && x>0
array(x,y1)=0;
end
if y2>0 && x>0
array(x,y2)=0;
end

next


Please refer to Fig 1 to find out the result of processing ellipse.dxf drawing and substituting in above ellipse equations(1,2,3 and 4)

 

 

Check out the free ("Live DXF2BMP") software

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Tutorials

Rotary Incremental Optical Encoder
Shaft Absolute Encoder Using Gray Code

DXF to BMP Algorithm

Only for Office Live Webmasters!


Free Engineering Software

Live DXF2BMP


Resources

Engineering Terms Definitions

Projects
µC Interfacing with 7-Segment

µC Interfacing & Decoding Keyboard

µC Interfacing with LCD & PC Through RS232

µC Interfacing & Controlling Room Light

DC Motor Control & Interfacing
Power Supply Design

Actions

Ask, comment or feedback

Subscribe

Tell A Friend

 

 

 

DXF to BMP Algorithm

By Engineering Knowledge administrator Rev1.0

Copyright©  Engineering Knowledge 2007. All rights reserved. 

 

 You are here: Home > Tutorials > DXF to BMP Algorithm