eryar

PipeCAD - Plant Piping Design Software.
RvmTranslator - Translate AVEVA RVM to OBJ, glTF, etc.
posts - 603, comments - 590, trackbacks - 0, articles - 0

Read DXF File

Posted on 2011-12-17 18:38 eryar 阅读(4833) 评论(3)  编辑 收藏 引用 所属分类: 2.OpenCASCADE

设计类图如下所示:


定义代码为:
File: Entity.h
 1 
 2 #ifndef _ENTITY_H_
 3 #define _ENTITY_H_
 4 
 5 #include <iostream>
 6 #include <list>
 7 #include <fstream>
 8 #include <string>
 9 using namespace std;
10 
11 class CEntity
12 {
13 public:
14     CEntity(void);
15     ~CEntity(void);
16 
17     // 显示数据
18     virtual void Show(void= 0;
19 
20 private:
21 
22 };
23 
24 #endif // _ENTITY_H_

File : Entity.cpp
 1 #include "Entity.h"
 2 
 3 CEntity::CEntity(void)
 4 {
 5 }
 6 
 7 CEntity::~CEntity(void)
 8 {
 9 }
10 
File : Line.h
 1 
 2 #ifndef _LINE_H_
 3 #define _LINE_H_
 4 
 5 #include "Entity.h"
 6 
 7 class CLine : public CEntity
 8 {
 9 public:
10     CLine(void);
11     ~CLine(void);
12 
13     // 显示数据
14     void Show(void);
15 
16     // 设值
17     void SetX1(double x);
18     void SetY1(double y);
19     void SetZ1(double z);
20     void SetX2(double x);
21     void SetY2(double y);
22     void SetZ2(double z);
23 
24 private:
25     // 直线起点x: 10
26     double    m_x1;
27     // 直线起点y: 20
28     double    m_y1;
29     // 直线起点z: 30
30     double    m_z1;
31     // 直线终点x: 11
32     double    m_x2;
33     // 直线终点y: 21
34     double    m_y2;
35     // 直线终点z: 31
36     double    m_z2;
37 
38 };
39 
40 #endif // _LINE_H_
File: Line.cpp
 1 #include "Line.h"
 2 
 3 CLine::CLine(void)
 4 {
 5     m_x1 = m_x2 = m_y1 = m_y2 = m_z1 = m_z2 = 0;
 6 }
 7 
 8 CLine::~CLine(void)
 9 {
10 }
11 
12 void CLine::Show( void )
13 {
14     cout<<"-----------------LINE------------------"<<endl;
15     cout<<"Start : ("<<m_x1<<","<<m_y1<<","<<m_z1<<")"<<endl;
16     cout<<"End   : ("<<m_x2<<","<<m_y2<<","<<m_z2<<")"<<endl;
17     cout<<"======================================="<<endl;
18 }
19 
20 void CLine::SetX1( double x )
21 {
22     m_x1    = x;
23 }
24 
25 void CLine::SetY1( double y )
26 {
27     m_y1    = y;
28 }
29 
30 void CLine::SetZ1( double z )
31 {
32     m_z1    = z;
33 }
34 
35 void CLine::SetX2( double x )
36 {
37     m_x2    = x;
38 }
39 
40 void CLine::SetY2( double y )
41 {
42     m_y2    = y;
43 }
44 
45 void CLine::SetZ2( double z )
46 {
47     m_z2    = z;
48 }
File : Circle.h
 1 
 2 #ifndef _CIRCLE_H_
 3 #define _CIRCLE_H_
 4 
 5 #include "Entity.h"
 6 
 7 class CCircle : public CEntity
 8 {
 9 public:
10     CCircle(void);
11     ~CCircle(void);
12 
13     // 显示数据
14     void Show(void);
15 
16     // 设值
17     void SetX(double x);
18     void SetY(double y);
19     void SetZ(double z);
20     void SetRadius(double r);
21 
22 private:
23     // 圆心坐标x: 10
24     double    m_x;
25     // 圆心坐标x: 20
26     double    m_y;
27     // 圆心坐标x: 30
28     double    m_z;
29     // 半径R: 40
30     double    m_Radius;
31 };
32 
33 #endif // _CIRCLE_H_
File : Circle.cpp
 1 #include "Circle.h"
 2 
 3 CCircle::CCircle(void)
 4 {
 5     m_x    = m_y = m_z = m_Radius = 0;
 6 }
 7 
 8 CCircle::~CCircle(void)
 9 {
10 }
11 
12 void CCircle::Show( void )
13 {
14     cout<<"----------------CIRCLE-----------------"<<endl;
15     cout<<"Center : ("<<m_x<<","<<m_y<<","<<m_z<<")"<<endl;
16     cout<<"Radius : "<<m_Radius<<endl;
17     cout<<"======================================="<<endl;
18 }
19 
20 void CCircle::SetX( double x )
21 {
22     m_x    = x;
23 }
24 
25 void CCircle::SetY( double y )
26 {
27     m_y    = y;
28 }
29 
30 void CCircle::SetZ( double z )
31 {
32     m_z    = z;
33 }
34 
35 void CCircle::SetRadius( double r )
36 {
37     m_Radius    = r;
38 }
File. : Arc.h
 1 
 2 #ifndef _ARC_H_
 3 #define _ARC_H_
 4 
 5 #include "Entity.h"
 6 
 7 class CArc : public CEntity
 8 {
 9 public:
10     CArc(void);
11     ~CArc(void);
12 
13     // 显示数据
14     void Show(void);
15 
16     // 设值
17     void SetStartAngle(double s);
18     void SetEndAngle(double e);
19 
20 private:
21     // 起点角度: 50
22     double    m_StartAngle;
23     // 端点角度: 51
24     double    m_EndAngle;
25 };
26 
27 #endif // _ARC_H_
File : Arc.cpp
 1 #include "Arc.h"
 2 
 3 CArc::CArc(void)
 4 {
 5     m_StartAngle = 0;
 6     m_EndAngle     = 0;
 7 }
 8 
 9 CArc::~CArc(void)
10 {
11 }
12 
13 void CArc::Show( void )
14 {
15     cout<<"-----------------ARC-------------------"<<endl;
16     cout<<"Start : "<<m_StartAngle<<endl;
17     cout<<"End   : "<<m_EndAngle<<endl;
18     cout<<"======================================="<<endl;
19 }
20 
21 void CArc::SetStartAngle( double s )
22 {
23     m_StartAngle    = s;
24 }
25 
26 void CArc::SetEndAngle( double e )
27 {
28     m_EndAngle    = e;
29 }
Put it all together :
File : main.cpp
  1 
  2 #include "Arc.h"
  3 #include "Line.h"
  4 #include "Circle.h"
  5 
  6 
  7 int main(int argc, char* argv[])
  8 {
  9     char            szFileName[_MAX_FNAME];
 10     int                iGroupCode = 0;
 11     string            strGroupValue;
 12     string            strBuffer;
 13     list<CEntity*>    EntitiesList;
 14 
 15     cout<<"Input a DXF file name :";
 16     cin>>szFileName;
 17 
 18     CLine*        pLine    = NULL;
 19     CCircle*    pCircle    = NULL;
 20     CArc*        pArc    = NULL;
 21 
 22     ifstream    iDxfFile(szFileName);
 23 
 24     if (!iDxfFile.is_open())
 25     {
 26         cout<<"Open DXF file "<<szFileName<<" failed!"<<endl;
 27         return -1;
 28     }
 29 
 30     while ( !iDxfFile.eof())
 31     {
 32         getline(iDxfFile, strBuffer);
 33         iGroupCode    = atoi(strBuffer.c_str());
 34         getline(iDxfFile, strGroupValue);
 35 
 36         // Just Read Entities Section.
 37         if (iGroupCode == 2 && strGroupValue.compare("ENTITIES"== 0)
 38         {
 39             getline(iDxfFile, strBuffer);
 40             getline(iDxfFile, strGroupValue);
 41             iGroupCode    = atoi(strBuffer.c_str());
 42 
 43             while (strGroupValue.compare("ENDSEC"!= 0)
 44             {
 45                 // Read Line Data
 46                 if (iGroupCode == 0 && strGroupValue.compare("LINE"== 0)
 47                 {
 48                     pLine    = new CLine;
 49                     getline(iDxfFile, strBuffer);
 50                     getline(iDxfFile, strGroupValue);
 51                     iGroupCode = atoi(strBuffer.c_str());
 52 
 53                     while (iGroupCode)
 54                     {
 55                         // 
 56                         getline(iDxfFile, strBuffer);
 57                         getline(iDxfFile, strGroupValue);
 58                         iGroupCode = atoi(strBuffer.c_str());
 59                         switch(iGroupCode)
 60                         {
 61                         case 10: pLine->SetX1(atof(strGroupValue.c_str())); break;
 62                         case 20: pLine->SetY1(atof(strGroupValue.c_str())); break;
 63                         case 30: pLine->SetZ1(atof(strGroupValue.c_str())); break;
 64                         case 11: pLine->SetX2(atof(strGroupValue.c_str())); break;
 65                         case 21: pLine->SetY2(atof(strGroupValue.c_str())); break;
 66                         case 31: pLine->SetZ2(atof(strGroupValue.c_str())); break;
 67                         }
 68                     }
 69 
 70                     EntitiesList.push_front(pLine);
 71                 }
 72 
 73                 // Read Circle Data
 74                 else if (iGroupCode == 0 && strGroupValue.compare("CIRCLE"== 0)
 75                 {
 76                     pCircle    = new CCircle;
 77                     getline(iDxfFile, strBuffer);
 78                     getline(iDxfFile, strGroupValue);
 79                     iGroupCode = atoi(strBuffer.c_str());
 80 
 81                     while (iGroupCode)
 82                     {
 83                         // 
 84                         getline(iDxfFile, strBuffer);
 85                         getline(iDxfFile, strGroupValue);
 86                         iGroupCode = atoi(strBuffer.c_str());
 87                         switch(iGroupCode)
 88                         {
 89                         case 10: pCircle->SetX(atof(strGroupValue.c_str()));        break;
 90                         case 20: pCircle->SetY(atof(strGroupValue.c_str()));        break;
 91                         case 30: pCircle->SetZ(atof(strGroupValue.c_str()));        break;
 92                         case 40: pCircle->SetRadius(atof(strGroupValue.c_str()));    break;
 93                         }
 94                     }
 95 
 96                     EntitiesList.push_front(pCircle);
 97                 }
 98 
 99                 // Read Arc Data
100                 else if (iGroupCode == 0 && strGroupValue.compare("ARC"== 0)
101                 {
102                     pArc    = new CArc;
103                     getline(iDxfFile, strBuffer);
104                     getline(iDxfFile, strGroupValue);
105                     iGroupCode = atoi(strBuffer.c_str());
106 
107                     while (iGroupCode)
108                     {
109                         // 
110                         getline(iDxfFile, strBuffer);
111                         getline(iDxfFile, strGroupValue);
112                         iGroupCode = atoi(strBuffer.c_str());
113                         switch(iGroupCode)
114                         {
115                         case 50: pArc->SetStartAngle(atof(strGroupValue.c_str()));    break;
116                         case 51: pArc->SetEndAngle(atof(strGroupValue.c_str()));    break;
117                         }
118                     }
119 
120                     EntitiesList.push_front(pArc);
121                 }
122 
123                 // Other entities
124                 else
125                 {
126                     // Move to next two lines
127                     getline(iDxfFile, strBuffer);
128                     getline(iDxfFile, strGroupValue);
129                     /*iGroupCode = atoi(strBuffer.c_str());*/
130                 }
131             }
132         }
133 
134     }
135 
136     // Output Entities
137     for (list<CEntity*>::iterator iter = EntitiesList.begin(); 
138         iter != EntitiesList.end(); 
139         iter++)
140     {
141         (*iter)->Show();
142     }
143 
144     return 0;
145 }

Feedback

# re: Read DXF File  回复  更多评论   

2013-03-26 16:33 by 王婷
很厉害啊,牛掰哥哥,广大单身MM可疯狂追之...

# re: Read DXF File[未登录]  回复  更多评论   

2014-08-14 09:34 by 张辉
哥们,看了你很多文章,很佩服你!!

# re: Read DXF File  回复  更多评论   

2014-08-14 10:36 by eryar
过奖了,
我也是在学习中……

@张辉

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理