Read DXF File

设计类图如下所示:

定义代码为:
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 }

时间: 2024-08-24 12:38:41

Read DXF File的相关文章

Open Cascade DataExchange DXF

Open Cascade DataExchange DXF eryar@163.com 摘要Abstract:对DXF文本格式进行详细介绍,并介绍了如何使用开源库dxflib对DXF文件进行读写操作,并将DXF文件中图形导入到OpenCascade. 关键字Key Words:DXF.Open Cascade.Data Exchange, dxflib 一.引言 Introduction 目前市面上的CAD系统都有自己的数据文件,各个系统之间的数据结构和格式各不相同,这样极大影响了设计和制造部门

RvmTranslator5.0 - Translate RVM to AutoCAD DXF

RvmTranslator5.0 - Translate RVM to AutoCAD DXF eryar@163.com RvmTranslator5.0 is released! This version support to translate RVM to AutoCAD DXF file. Figure 1. PDMS model in AutoCAD Download RvmTranslator5.0: http://yun.baidu.com/pcloud/album/file?a

IsoAlgo User Guide

IsoAlgo User Guide V1.0 IsoAlgo@gmail.com April 28, 2014 Disclaimer This program is free software, you can redistribute it and/or modify it. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the im

DXF是什么

AutoCAD(Drawing Interchange Format或者Drawing Exchange Format) 绘图交换文件.DXF 是Autodesk公司开发的用于AutoCAD与其它软件之间进行CAD数据交换的CAD数据文件格式.DXF是一种开放的矢量数据格式,可以分为两类:ASCII格式和二进制格式;ASCII具有可读性好,但占有空间较大;二进制格式占有空间小.读取速度快.由于Autocad现在是最流行的cad系统,DXF也被广泛使用,成为事实上的标准.绝大多数CAD系统都能读入

android开发-安卓开发中read-only file system

问题描述 安卓开发中read-only file system android模拟机上不能加文件提示read only file system 是什么问题啊 mount -o remount ,rw /都试过了 ,没什么用额. 使用重新挂载还是无用 mount -o remount,rw rootfs /system/ mount -o remount,rw rootfs /system/ cd sdcard cd sdcard mkdir a mkdir a mkdir failed for

AutoCAD File Merger

AutoCAD File Merger eryar@163.com Abstract. There are many drawing files during the ship design or other design process, so somebody want to merge them to one DWG file. The DwgMerger is a AutoCADE plugin developed by AutoCAD .NET, its purpose is to m

Starting MySQL.Manager of pid-file quit without updating file.[FAILED]

Starting MySQL...... ERROR! Manager of pid-file quit without updating file. 可选拍错思路: my.cnf 配置文件参数错误. 刚装完MySQL,启动时报如下错误: Starting MySQL.Manager of pid-file quit without updating file.[FAILED] 依次尝试了从谷姐找到的各种方法,发现只要执行如下MySQL初始化命令即可解决: 1 /usr/local/mysql/

mvc-asp.net MVC怎么给图input type=file传过来的图片加水印

问题描述 asp.net MVC怎么给图input type=file传过来的图片加水印 解决方案 加水印后,在controller里面传到服务器里面保存 解决方案二: 这是controller的代码 public ActionResult AddNewsPic(string id) { if (Request.Files.Count > 0) { if (!string.IsNullOrEmpty(Request.Files[0].FileName)) { Stream fileDataStr

Linux中提示No such file or directory解决方法

  问题描述 解决方法 分析原因,可能因为我平台迁移碰到权限问题我们来进行权限转换 1)在Windows下转换: 利用一些编辑器如UltraEdit或EditPlus等工具先将脚本编码转换,再放到Linux中执行.转换方式如下(UltraEdit):File-->Conversions-->DOS->UNIX即可. 2)方法 用vim打开该sh文件,输入: [plain] :set ff 回车,显示fileformat=dos,重新设置下文件格式: [plain] :set ff=uni