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....