Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 109,295 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,209 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Creating AutoCad DXF in C++

 
Reply to this topicStart new topic

> Creating AutoCad DXF in C++

Rating  4
nirvanarupali
Group Icon



post 22 Nov, 2007 - 06:26 PM
Post #1


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
CODE
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.
CODE
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.
CODE
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:
CODE
#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.com/techpubs/autocad/a...0/dxf/index.htm

http://usa.autodesk.com/adsk/servlet/item?...&id=8446698 <--- the official referances
http://www.wotsit.org/list.asp?search=dxf&button=GO%21 <--- the wosit's file format library.

NOTE: Thanks to Nick Max comments....
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

mr_anry
*



post 7 Mar, 2008 - 06:59 AM
Post #2
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..
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 9/6/08 09:28AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month