DXF文件格式读取(VC例子)LP_第1页
DXF文件格式读取(VC例子)LP_第2页
DXF文件格式读取(VC例子)LP_第3页
DXF文件格式读取(VC例子)LP_第4页
DXF文件格式读取(VC例子)LP_第5页
已阅读5页,还剩26页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、 download demo project - 167 kb introductionwhat is dxf?drawing interchange format (dxf) files enable the interchange of drawings between autocad and other programs. dxf files can be either ascii or binary formats. because ascii dxf files are more common than the binary format, cadlib uses ascii dxf

2、 format.what is cadlib?the cadlib is not a computer aided design (cad) program. it is a tool for creating dxf files that are used in the cad programs. it consists of two parts. one of them is a dynamic link library to create the dxf file. the other part is the programming interface. it is a class th

3、at integrates the cadio.dll functions. it can be used in microsoft visual c+ projects. in addition, the cadio.dll can be used in other win32 programs.why use cadlib?in some programs, it is needed to create a drawing output for use in other programs such as autocad. for example, in a building detail

4、sheet generator program, the program needs to create a drawing output. and the most standard format for communicating drawing data is dxf.dxf file structurethe dxf format is a tagged data representation of all the information contained in a drawing file. tagged data means that each data element in t

5、he file is preceded by an integer number that is called a group code. a group codes value indicates what type of data element follows. this value also indicates the meaning of a data element for a given object (or record) type. virtually all user-specified information in a drawing file can be repres

6、ented in dxf format. (from autocads dxf reference)a dxf file consists of some sections. each section has some drawing data in itself. the cadlib uses the following sections:1. header 2. tables 3. blocks 4. entities the main reference for dxf file structure that is used for cadlib is the autocads dxf

7、 reference. you can find more information about dxf file structure here.classesthe classes are interfaces between cadio.dll and the main program. test has come with cadlib to demonstrate how to generate a dxf file with cdxffilewrite and cdrawing classes.cdxffilewrite classcdxffilewrite gathers all t

8、he commands needed to directly create a dxf file. usage of cdxffilewrite is as follows:1. create the dxf file collapse | copy codecdxffilewrite dxffile;dxffile.create( d:test.dxf );2. begin and end the header section. its here for compatibility with some cad programs. others work without having head

9、er section. collapse | copy code/ header section -dxffile.beginsection(sec_header);dxffile.endsection();/ close header section -3. begin the tables section and put the layer, ltype, style, dimstyle table-types as many as you want and then close the section collapse | copy code/ tables section -dxffi

10、le.beginsection(sec_tables);/ ltype table type -dxffile.begintabletype(tab_ltype);dxfltype ltype;double elem4;/ continuouszeromemory(ype, sizeof(ltype);ltype.name = continuous;ltype.descriptivetext = solid line;dxffile.addlinetype(ype);/ dashdot2zeromemory(ype, sizeof(ltype);ltype.name = dashdot2;lt

11、ype.descriptivetext = dash dot (.5x) _._._._._._._._._._._._._._._.;ltype.elementsnumber = 4;ltype.totalpatternlength = 0.5;ltype.elements = elem;elem0 = 0.25;elem1 = -0.125;elem2 = 0.0;elem3 = -0.125;dxffile.addlinetype(ype);dxffile.endtabletype();/ close ltype table type -/ layer table type -resul

12、t &= dxffile.begintabletype(tab_layer);result &= dxffile.addlayer(layer1, 1, continuous);result &= dxffile.addlayer(layer2, 2, continuous);result &= dxffile.addlayer(layer3, 3, continuous);result &= dxffile.addlayer(layer4, 4, continuous);result &= dxffile.endtabletype();/ close layer table type -/

13、style table type -dxffile.begintabletype(tab_style);dxfstyle tstyle;zeromemory(&tstyle, sizeof(tstyle);tstyle.name = style1;tstyle.primaryfontfilename = times.ttf;tstyle.height = 0.3;tstyle.widthfactor = 1;dxffile.addtextstyle(&tstyle);dxffile.endtabletype();/ close style table type -/ dimstyle tabl

14、e type -dxffile.begintabletype(tab_dimstyle);dxfdimstyle dimstyle;/ dim1zeromemory(&dimstyle, sizeof(dimstyle);dimstyle.name = dim1; / dimstyle namedimstyle.dimclrd = 2; / dimension line & arrow heads colordimstyle.dimdle = 0.0000; / dimension line size after extensionlinedimstyle.dimclre = 2; / ext

15、ension line colordimstyle.dimexe = 0.1800; / extension line size after dimlinedimstyle.dimexo = 0.0625; / offset from origindimstyle.dimblk1 = closedfilled;/ 1st arrow headdimstyle.dimblk2 = closedfilled;/ 2nd arrow headdimstyle.dimasz = 0.1800; / arrow sizedimstyle.dimtxsty = style1; / text styledi

16、mstyle.dimclrt = 3; / text colordimstyle.dimtxt = 0.1800; / text heightdimstyle.dimtad = 1; / vertical text placementdimstyle.dimgap = 0.0900; / offset from dimension linedxffile.adddimstyle(&dimstyle);dxffile.endtabletype();/ close dimstyle table type -dxffile.endsection();/ close tables section -4

17、. begin entities section and put entities data (line, circle, solid, text, arc, point, dimlinear) and finally close the section collapse | copy code/ entities section -dxffile.beginsection(sec_entities);/ set current layer to layer2dxffile.setcurrentlayer(layer2);/ draw a linedxffile.line(1.2, 3.3,

18、7.5, 7.7);/ draw a circledxffile.circle(7.8, 4.3, 1.75);/ set current layer to layer4dxffile.setcurrentlayer(layer4);/ draw a solidrealpoint points4;points0.x = 10.4; points0.y = 7.2;points1.x = 13.6; points1.y = 7.4;points2.x = 13.1; points2.y = 4.9;points3.x = 10.9; points3.y = 5.9;solid(4, points

19、);/ set current textstyle to style1dxffile.setcurrenttextstyle(style1);/ draw textdxffile.text(sample text, 5.9, 6.7, 0.3, 35);/ draw a dimension linedxffile.setcurrentdimstyle(dim1);dxffile.dimlinear(6.05, 3, 9.55, 3, 9.55, 2, 0, 3.50);dxffile.endsection();/ close entities section -5. close the dxf

20、 file collapse | copy codedxffile.close();cdrawing classcdrawing class has all the commands to create a drawing in memory and save it as a dxf file. usage of cdrawing is as follows:1. create the on-memory drawing collapse | copy codecdrawing drw;drw.create( );2. create new layer, ltype, style, dimst

21、yle table-types as many as you want. collapse | copy code/ tables section -/ ltype table type -ltype ltype;objhandle objhandle1;/ continuouszeromemory(ype, sizeof(ltype);strcpy(ltype.name, continuous);strcpy(ltype.descriptivetext, solid line);objhandle1 = drw.addlinetype(ype);/ dashdot2zeromemory(yp

22、e, sizeof(ltype);strcpy(ltype.name, dashdot2);strcpy(ltype.descriptivetext, dash dot (.5x) _._._._._._._._._._._._._._._.);ltype.elementsnumber = 4;ltype.patternlength = 0.5;ltype.elements0 = 0.25;ltype.elements1 = -0.125;ltype.elements2 = 0.0;ltype.elements3 = -0.125;drw.addlinetype(ype);/ layer ta

23、ble type -layer layer;/ layer1zeromemory(&layer, sizeof(layer);strcpy(layer.name, layer1);layer.color = 1;layer.linetypeobjhandle = objhandle1; / continuousdrw.addlayer(&layer);/ layer2zeromemory(&layer, sizeof(layer);strcpy(layer.name, layer2);layer.color = 2;layer.linetypeobjhandle = objhandle1; /

24、 continuousdrw.addlayer(&layer);/ style table type -style style;zeromemory(&style, sizeof(style);strcpy(style.name, style1);strcpy(style.primaryfontfilename, times.ttf);style.lastheightused = 0.3;style.widthfactor = 1;objhandle1 = drw.addtextstyle(&style);/ dimstyle table type - dimstyle dimstyle;/

25、dim1zeromemory(&dimstyle, sizeof(dimstyle);strcpy(dimstyle.name, dim1); / dimstyle namedimstyle.dimclrd = 2; / dimension line & arrow heads colordimstyle.dimdle = 0.0000; / dimension line size after extensionlinedimstyle.dimclre = 2; / extension line colordimstyle.dimexe = 0.1800; / extension line s

26、ize after dimlinedimstyle.dimexo = 0.0625; / offset from originstrcpy(dimstyle.dimblk1, closedfilled);/ 1st arrow headstrcpy(dimstyle.dimblk2, closedfilled);/ 2nd arrow headdimstyle.dimasz = 0.1800; / arrow sizedimstyle.dimtxstyobjhandle = objhandle1;/ text styledimstyle.dimclrt = 3; / text colordim

27、style.dimtxt = 0.1800; / text heightdimstyle.dimtad = 1; / vertical text placementdimstyle.dimgap = 0.0900; / offset from dimension linedrw.adddimstyle(&dimstyle);3. make entities data (line, circle, solid, text, arc, point, dimlinear, polyline). collapse | copy code/ entities section -/ set current

28、 layer to layer2drw.setlayer(layer2);/ draw a linedrw.line(1.2, 3.3, 7.5, 7.7);/ draw a circledrw.circle(7.8, 4.3, 1.75);/ set current layer to layer1drw.setlayer(layer1);/ draw a solidrealpoint points4;points0.x = 10.4; points0.y = 7.2;points1.x = 13.6; points1.y = 7.4;points2.x = 13.1; points2.y =

29、 4.9;points3.x = 10.9; points3.y = 5.9;drw.solid(points0, points1, points2, points3);/ set current textstyle to style1drw.settextstyle(style1);/ draw textdrw.text(sample text, 5.9, 6.7, 0.3, 35);/ draw a dimension linedrw.setdimstyle(dim1);drw.dimlinear(6.05, 3, 9.55, 3, 9.55, 2, 0, 3.50);4. save da

30、ta to a dxf file. collapse | copy codedrw.savedxffile(dxffilename);5. destroy cdrawing and free allocated memory. collapse | copy codedrw.destroy();loading data from a dxf file1. create the on-memory drawing. collapse | copy codecdrawing drw;drw.create( );2. use loaddxffile member function to load d

31、xf file into memory. collapse | copy codedrw.loaddxffile(sample.dxf);thats all!conclusionsince i am a civil engineer, i decided to write a program to generate a beam or columns detail sheet without the use of autocad. i have written a program that, with a little data about beam or column, will creat

32、e the detail sheet automatically. output of this program is a dxf file and it can be shown in autocad or it can be plotted with it. this program can save the time for drawing the detail sheet with autocad. if you are an autocad operator, you will understand the meaning of words that are used in this

33、 article, or if you are a programmer who wants to write a program to create dxf files, first you need a little knowledge about autocad or the drawing programs such as is mentioned above. this code can be useful for programmers who need to create dxf files from their programs. cadlib is not the best

34、one and also there are many commercial software for creating dxf files but they are not open source. feel free to change the code. your comments in regards to this article will cause the improvement of cadlib.history 20 dec 2002 o first release of cadlib 19 jan 2003 o some bug fixes o added dimensio

35、n-line support. its a combination of other entity commands like line and solid o added blocks section support o added arc, point and insertblock commands for entities section o text command has been improved 11 may 2003 o added cdrawing class to store drawing data in memory and change the data befor

36、e saving it as a dxf file. 28 june 2003 o added dxf read capability to cdrawing class o some bug fixes of cdrawing class when writing data to a dxf file 22 nov 2003 (cadlib version 2.00) o added drawing view capability o added polyline command (by tran duy dung) o improved dxf loading speed o some b

37、ug fixes of drawing memory management functions 24 aug 2004 (cadlib version 2.10) o added zoomextents function o improved viewing functions to show dashed lines o added changeentity & deleteentity commands o added dimension view capability o fixed a bug occures when viewing a rotated block o improve

38、d viewing of textslicensethis article, along with any associated source code and files, is licensed under the code project open license (cpol)about the author用c读取dxf文件author: 摘要:本文简要介绍了一下dxf文件的组成。重点讲述了怎样使用c语言来读取dxf文件中的实体信息。关键字:c、dxf abstract: the paper present the basic parts of dxf fil

39、e. and focus on how to use c read the entitys information from dxf file.key words: c, dxf dxf是drawing exchange file的缩写,意思为图形交换文件,在工程制图中有广泛的应用,掌握了dxf文件的读写对编写cad软件时的图形信息的交换有重要意义。它有两种格式:一种是ascii dxf格式;一种是二进制dxf格式。ascii dxf文件格式是ascii 文字格式的autocad图形的完整表示,这种文件格式易于被其它程序处理。二进制格式的dxf文件与ascii格式的dxf文件包含的信息相同,但

40、格式上二进制格式比ascii格式更精简,能够节省百分之二十五的文件空间。autocad能够更快地对其执行读写操作(通常能快五倍)。这可能是对ascii格式的dxf文件操作时有ascii与二进制形式的转换,因而花费时间较多。本文主要讨论ascii格式的dxf文件,因为它可读性强。一、ascii格式的dxf文件的组成先来介绍一下ascii格式的dxf文件的组成。(小提示:打开autocad,新建一个空的文件,然后再输出为dxf文件,并用记事本打开dxf文件,就可以看到它的所有代码了,这样有助于你更好地理解dxf文件的组成。还有按一下f1,打开autocad的帮助文件,找到dxf参考,它是权威具体的

41、资料。)用记事本打开一个dxf文件,你可以发现它里面有这样一些代码: 0section 2header 9$acadver 1ac1015即里面总是数字和字符串/数字在交替的出现。数字就叫做代码(通常称为组码),紧跟组码数字的称为关联值对。(以下内容来自dxf参考)dxf文件本质上由代码及关联值对组成。代码(通常称为组码)表明其后的值的类型。使用这些组码和值对,可以将dxf文件组织到由记录组成的区域中,这些记录由组码和数据项目组成。在dxf文件,每个组码和值各占一行。表1为组码值类型表的部分:表1 组码值类型表(部分) 一个完整的ascii格式的dxf文件结构如下:l header段。它包含图

42、形的基本信息。它由autocad数据库版本号和一些系统变量组成。每个参数都包含一个变量名称及其关联的值。l classes段。包含应用程序定义的类的信息,这些类的实例出现在数据库的blocks、entities和objects段中。类定义在类的层次结构中是固定不变的。l tables段。包含以下符号表的定义:appid(应用程序标识表)block_record(块参照表)dimstyle(标注样式表)layer(图层表)ltype(线型表)style(文字样式表)ucs(用户坐标系表)view(视图表)vport(视口配置表)l blocks段。包含构成图形中每个块参照的块定义和图形图元。l

43、entities段。包含图形中的图形对象(图元),其中包括块参照(插入图元)。这里的信息很重要。l objects段。包含图形中的非图形对象。除图元、符号表记录以及符号表以外的所有对象都存储在此段。objects段中的条目样例是包含多线样式和组的词典。l thumbnailimage段。包含图形的预览图像数据。此段为可选。每个段都以一个后跟字符串section的组码0开始,其后是组码2和表示该段名称的字符串(例如,header)。每个段都由定义其元素的组码和值组成。每个段都以一个后跟字符串endsec的组码0结束。举两个例子:1.以下是 dxf? 文件 header 段的样例:0 header

44、 段的开始section2header 9 每出现一个标题变量便重复一次 $ 0 header 段的结尾 endsec 2.以下是 dxf 文件 entities 段的样例:0 entities 段的开始section2entities 0 每个图元定义有一个条目,如line,circle5330100acdbentity8100acdb. . 0 entities 段的结尾endsec 因此你需要什么信息就可以在相应的段中寻找。例如你需要得到dxf文件的版本信息就可在header段中寻找。需要图形的信息就可到entities段中寻找。再强调一下实体段:实体段记录了除块段出现的实体以外的所有绘

45、图实体内容,包括每个实体的名称、所在图层、线型、颜色代码等等。由于定义一个实体所有组码在某一实体的任意组码在其值与默认值相同时可以省略不写。因此用户在读取dxf文件时应注意:1 定义一个实体的数据是以“0”组码开始,而以另一个“0”组码的出现表示结束;2 某一实体的定义数据顺序不固定。因此用户在编写dxf文件处理程序时不能按顺序固定的格式处理,而只能按组码的同现来记录数据。二、读取dxf文件流程有了以上知识就可读懂dxf文件并从中提取我们所需要的信息了,而我们所需要的信息大多在entities段中。先讲一下大概的处理方法。输入dxf文件名 打开dxf文件 读取一个记录 header table

46、s entities blocks eof 处理header 处理tables 处理entities 处理blocks 结束 图2 dxf文件处理流程 如图2所示为dxf文件处理流程。可以从dxf文件中检索,当检索到与某个段时就转到那个段的处理程序去处理。如检索到header段就转入header 段的处理程序去处理。图形的大部分信息都在实体entities段中,因此读取实体段的内容很重要。读取实体段的数据首先要考虑读取数据的存储方式,然后再进行后一步的处理或存入数据文件中。此处用链表结构来存储。各个实体的数据分成两块:公共数据块和特殊数据块。公共数据块存储每个实体都具有特征参数,如所在图层,实

47、体标识,线型名特殊数据块存储每个实体特有的数据,如实体line,它里面有两个端点的坐标值;实体circle中有圆心坐标值和半径值等。单个实体的数据处理方法:读取一个实体的数据首先根据组码“0”后的实体标识字符串来确定其为哪一种实体,然后再根据这个实体的具体情况来读取数据。下面为用c具体实现的代码。因为是处理ascii文件,只需要用到c文件处理的两个标准函数:fprintf()和fscanf()。文件中有一个位置指针,指向当前读写位置。如果顺序读写一个文件,每次读写完一个字符后,该指针自动指向下一个字符的位置。 三、读取dxf文件信息的小程序 先来看一个用c来读取header段中的版本号的小程序

48、。/*- * header.c *读取dxf文件中header段中dxf文件版本号的小程序。 * 02-05-08 18:55 *-*/ #include #include #include #define strlen 30 int main(int argc, char *argv) int code; /*存储组码*/ char codevaluestrlen; /*存储组码对应的值*/ file *dxf; /*文件指针*/ char filenamestrlen; /*文件名*/ char suffix6 = .dxf; /*只输入文件名打开dxf文件,不用输后缀.*/ printf(请输入文件名:); gets(filename); strcat(filename,suffix); dxf = fopen(filename,r); /*打开文件进行读操作*/ if(!dxf) pri

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论