Page 1 of 1

Creating AutoCad DXF in C++ Rate Topic: ****- 1 Votes

#1 nirvanarupali  Icon User is offline

  • D.I.C Stomach
  • member icon

Reputation: 14
  • View blog
  • Posts: 1,120
  • Joined: 01-August 07

Posted 22 November 2007 - 06:26 PM

AutoCad can be reprogrammed by its AutoLisp Language. You can customize your commands and menus. You can exploit it, such as by entering circle command to draw a line or rectangle command to draw a circle. This is a crazy thing for the people who wants to exploit the AutoLisp language. But after examining the codes I wrote in Lisp routines, I found out that the program would not be useful outside AutoCad environment, so I was thinking of making a program in AutoCad but can be used outside AutoCad

I make DXF file in C++

DXF overview

DXF stands for Drawing Interchange Format. A subset of the AutoCad containing
only block reference, attribute, and end-of-sequence objects. This can be read in
any text editor. The primary purpose of the dxf files are to be used in
CNC (Computer Numerical Control) machines. These are only AutoCad file extension that
CNC machine database can read. This is very much useful to the companies which are using CAD/CAM .

Here we go.
First I create a simple class
class CreateDxf 
{
public:
	CreateDxf();
	~CreateDxf();

	
	void DxfBegin ();
	void DxfEnd   ();
	// you can add more functions to create more drawings. 
	void Circle(float,float,float);
};


Implementation of the class CreateDxf with its comments.
CreateDxf::~CreateDxf()
{
   cout <<"Destructor called..." << endl;
	
}
void  CreateDxf::DxfBegin ()
{
	// Creation of an output stream object in text mode.
	// Header section of every dxf file. 
	ofstream To_Dxf ("Drawing1.dxf", ios::out);

	To_Dxf << 0		  << endl;
	To_Dxf << "SECTION"  << endl;
	To_Dxf << 2		  << endl;
	To_Dxf << "ENTITIES" << endl;

	To_Dxf.close();
}
void  CreateDxf::DxfEnd ()
{
	// Creation of an output stream objet in text mode.
	// end of sequence objects of dxf file.
	ofstream To_Dxf ("Drawing1.dxf", ios::app);
	
	To_Dxf << 0		  << endl;
	To_Dxf << "ENDSEC"   << endl;
	To_Dxf << 0		  << endl;
	To_Dxf << "EOF";

	To_Dxf.close();
}

void CreateDxf::Circle (float radius, float x, float y)
{
	// Propeties of a circle not bound in any AutoCAd version
	//In AutoCAD 2000 we can have more less 4000 lines of code here. 
	
	// Creation of an output stream objet in text mode.
	
	ofstream To_Dxf ("Drawing1.dxf", ios::app);

	// Draw the circle
	To_Dxf << 0		  << endl;
	To_Dxf << "CIRCLE"   << endl;
	To_Dxf << 8		  << endl;	
	To_Dxf << 0		  << endl;	// Layer number (default layer in autocad)
	To_Dxf << 10		 << endl;	// XYZ is the Center point of circle
	To_Dxf << x		 << endl;	// X in UCS (User Coordinate System)coordinates
	To_Dxf << 20		 << endl;
	To_Dxf << y		  << endl;	// Y in UCS (User Coordinate System)coordinates
	To_Dxf << 30		 << endl;
	To_Dxf << 0.0		<< endl;	// Z in UCS (User Coordinate System)coordinates
	To_Dxf << 40		 << endl;	
	To_Dxf << radius	 << endl;	// radius of circle

	To_Dxf.close();

}

UCS is the coordinate system that AutoCad is using. And World UCS (user coordinate system) is the default every time you open AutoCad, that is the Y axis is the vertical X axis is the horizontal if you are looking directly from Top view. You can modify your UCS, rotate it but I will not tackle more on UCS, that topic is beyond the scope of this tutorial.



Driver program to create such object.
int main()
//Driver program.
int main()

{   string str;
	float rad, x = 0.0,y = 0.0; 
	
	CreateDxf Draw;
	
	cout <<"Enter radius of circle" <<endl;
	getline (cin,str);
	stringstream(str) >> rad;
	
	// This is 2D Drawing so no need to enter Z axis coordinate
	cout <<"Enter X coordinate of circle" <<endl;
	getline (cin,str);
	stringstream(str) >> x;
	
	cout <<"Enter Y coordinate of circle" <<endl;
	getline (cin,str);
	stringstream(str) >> y;
	
	Draw.DxfBegin();
	Draw.Circle (rad,x,y );
	Draw.DxfEnd();
   
	return 0;
	
}




You need to have an AutoCad application to open the file, or any application that can read AutoCad dxf such as CorelDraw. Use AutoCad 2000 or higher to open it.

How to open in AutoCad

1.Run AutoCad.
2.Open file, choose dxf in the File Type.
3.Click Open.
You cannot see anything, don't panic it is there, AutoCad model environment is
infinite, you can even draw the entire solar system and use light year as your drawing unit.
4.In the command prompt. Type: Zoom,
5.Type Extent or All (for zoom options).

The entire codes:
 #include <fstream>		
#include <iostream>
#include <string>
#include <sstream>  // To avoid bug if the user will enter letters or words.
using namespace std;


class CreateDxf 
{
public:
	CreateDxf();
	~CreateDxf();

	
	void DxfBegin ();
	void DxfEnd   ();
	// you can add more functions to create more drawings. 
	void Circle(float,float,float);
};


CreateDxf::CreateDxf()
{
   cout <<"constructor called..." << endl;
	
}

CreateDxf::~CreateDxf()
{
   cout <<"Destructor called..." << endl;
	
}
void  CreateDxf::DxfBegin ()
{
	// Creation of an output stream object in text mode.
	// Header section of every dxf file. 
	ofstream To_Dxf ("Drawing1.dxf", ios::out);

	To_Dxf << 0		  << endl;
	To_Dxf << "SECTION"  << endl;
	To_Dxf << 2		  << endl;
	To_Dxf << "ENTITIES" << endl;

	To_Dxf.close();
}
void  CreateDxf::DxfEnd ()
{
	// Creation of an output stream objet in text mode.
	// end of sequence objects of dxf file.
	ofstream To_Dxf ("Drawing1.dxf", ios::app);
	
	To_Dxf << 0		  << endl;
	To_Dxf << "ENDSEC"   << endl;
	To_Dxf << 0		  << endl;
	To_Dxf << "EOF";

	To_Dxf.close();
}

void CreateDxf::Circle (float radius, float x, float y)
{
	// Propeties of a circle not bound in any AutoCAd version
	//In AutoCAD 2000 we can have more less 4000 lines of code here. 
	
	// Creation of an output stream objet in text mode.
	
	ofstream To_Dxf ("Drawing1.dxf", ios::app);

	// Draw the circle
	To_Dxf << 0		  << endl;
	To_Dxf << "CIRCLE"   << endl;
	To_Dxf << 8		  << endl;	
	To_Dxf << 0		  << endl;	// Layer number (default layer in autocad)
	To_Dxf << 10		 << endl;	// XYZ is the Center point of circle
	To_Dxf << x			 << endl;	// X in UCS (User Coordinate System)coordinates
	To_Dxf << 20		 << endl;
	To_Dxf << y			 << endl;	// Y in UCS (User Coordinate System)coordinates
	To_Dxf << 30		 << endl;
	To_Dxf << 0.0		<< endl;	// Z in UCS (User Coordinate System)coordinates
	To_Dxf << 40		 << endl;	
	To_Dxf << radius	  << endl;	// radius of circle

	To_Dxf.close();

}
 //Driver program.
int main()

{   string str;
	float rad, x = 0.0,y = 0.0; 
	
	CreateDxf Draw;
	
	cout <<"Enter radius of circle" <<endl;
	getline (cin,str);
	stringstream(str) >> rad;
	
	// This is 2D Drawing so no need to enter Z axis coordinate
	cout <<"Enter X coordinate of circle" <<endl;
	getline (cin,str);
	stringstream(str) >> x;
	
	cout <<"Enter Y coordinate of circle" <<endl;
	getline (cin,str);
	stringstream(str) >> y;
	
	Draw.DxfBegin();
	Draw.Circle (rad,x,y );
	Draw.DxfEnd();
   
	return 0;
	
}



Next time, I will write a tutorial on ARX (AutoCad Runtime Extension) of Mechanical Desktop.

For DXF reference:

http://www.autodesk....0/dxf/index.htm

http://usa.autodesk....?...&id=8446698 <--- the official referances
http://www.wotsit.or...xf&button=GO%21 <--- the wosit's file format library.

NOTE: Thanks to Nick Max comments....

Is This A Good Question/Topic? 0
  • +

Replies To: Creating AutoCad DXF in C++

#2 mr_anry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-January 08

Posted 07 March 2008 - 06:59 AM

hello im using your code which Creating AutoCad DXF in C++ as below

class CreateDxf
{
public:
CreateDxf();
~CreateDxf();


void DxfBegin ();
void DxfEnd ();
// you can add more functions to create more drawings.
void Circle(float,float,float);
};

it sucess but have an error.it say :

[Linker Error] Error: Unresolved external '__InitVCL' referenced from C:\PROGRAM FILES\BORLAND\BDS\4.0\LIB\CP32MTI.LIB|crtlvcl

can u guide me to solve this problem..
Was This Post Helpful? 0
  • +
  • -

#3 nirvanarupali  Icon User is offline

  • D.I.C Stomach
  • member icon

Reputation: 14
  • View blog
  • Posts: 1,120
  • Joined: 01-August 07

Posted 16 September 2008 - 01:11 AM

This codes works fine to me. I am using Borland 5.5 command line compiler.
Maybe there is something wrong with your compiler.

This codes works fine to me. I am using Borland 5.5 command line compiler.
Maybe there is something wrong with your compiler.
Was This Post Helpful? 0
  • +
  • -

#4 08Chavan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-January 10

Posted 06 January 2010 - 03:40 AM

"You can modify your UCS, rotate it but I will not tackle more on UCS, that topic is beyond the scope of this tutorial."

Hello,
I am working on a C program that has to export a DXF file. I have exactly the problem you mention here: how should I write my DXF file to create an UCS? THe object i export is very simply define, no headers, no block, no tables, just entities (lines). How should I write the specific UCS i need?
Thank you very much for your help!
Was This Post Helpful? 0
  • +
  • -

#5 nirvanarupali  Icon User is offline

  • D.I.C Stomach
  • member icon

Reputation: 14
  • View blog
  • Posts: 1,120
  • Joined: 01-August 07

Posted 20 January 2010 - 07:19 AM

 To_Dxf << x             << endl;    // X in UCS 
    
    To_Dxf << y             << endl;    // Y in UCS 
    
 


You can specify you exact location here.
Was This Post Helpful? 0
  • +
  • -

#6 riahc3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 26-June 17

Posted 26 June 2017 - 12:51 AM

Extending this code for lines, arc, etc. would be nice.

But thank you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1