Read the DXF format file OpenGL is the latest open three-dimensional graphics software interface for the US SGI, which is suitable for a wide range of computer environments. From your personal computer to the workstation, OpenGL can achieve high performance 3D graphics features. OpenGL itself not only provides operational and control of simple primitives, but also provides many functions for modeling of complex objects. However, we usually prefer tools such as AutoCAD and 3DS and 3Dmax to create a model, and we already have a lot of such models, then how can we resource sharing, avoid duplication of labor? With the CAD graphic standard data exchange format -DXF format, we can easily achieve resource sharing without the need for repeated modeling. The structure of the DXF file is clear, the specific information is as follows: 1. Title Section (Header) The general information about graphics can be found in this section of the DXF file, each parameter has a variable name and a correlation value. 2. The definition of the specified item included in the table segment, which includes: a, line table (LYER) B, layer table (LYER) C, Style D, view table (View) E, user coordinate system table (UCS) F, Window Configuration Table (VPORT) G, Targeting Font Table (APPID) 3. Blocks This section contains block definition entities, and these entities describe the graphic composition of each Block entity. 4. Entity (enttive) This section contains an entity, including any block calls. 5. End of file (End) The following is a basic structure of DXF to illustrate: 0 0 Next section indicates that this is the start 2 2 of a segment 2 2 Segment name Header Description This section is Header Segment (Title Segment) The 9 $ Acadver file is the 1 AC1008 9 9 generated by AutoCAD $ UCSORG $ UCSORG User Coordinate System The origin of the coordinate 10 10 in the world coordinate system corresponds to the value of X 0.0 x Value 20 20 corresponds to the value of Y 0.0 y 30 30 Corresponde 0.0 z value 9 $ ucsxdir This is a less relevant part, slightly 10 1.0 .... 9 9 After $ EXTMIN $ EXTMIN Description 3D entity model in the world coordinate system Value 10 10 Values of X -163.925293 X 20 20 Correspondence Y -18.5415860.0 Y Value 30 30 Corresponding to Z 78.350945 Z Narre 9 9 Reconnected $ EXTMAN $ EXTMAX Description 3D Entity Model In World Coordinate System Maximum 10 10 corresponds to the value of X 202.492279 x Value 20 20 Correspondence 30 30 Corresponds z 169.945602 z value 0 0 LED ENDSEC ENDSEC Description This section ended 0 0 After the section section indicated that this is a segment start 2 2 Connected is the paragraph TABLES. This section is a TABLES segment (table) ... ... This section is not related to us, this is slightly not described 0 0 Later Endsec Endsec Description This section ends 0 0 Next section section indicates that this is the start 2 2 of a segment 2 2 The paragraph Entities indicates that the paragraph is an Entities section (physical segment) This is the paragraph I want to explain in detail, this section contains The coordinates of all entities The coordinates of the Polyline point of the entity and the order of the formation.
0 Losing POLYLINE 8 indicates that the following data is for a new entity; Object01 8 The following string is the name of this entity 66 1 70 From 66 1 to 70 64 64 Description This entity is 71 38 71 consisting of many small planes 38 Description This entity has a total of 38 points 72 72 72 72 Describes that the entity consists of 72 triangles 0 0 Vertex Vertex indicates that the rear followed is the entity data 8 Object01 10 corresponds to the value of X coordinate -163.925293 x Value 20 corresponds to Y coordinate - 17.772665 Y worth Z coordinate 128.929947 z 0 70 192 192 Indicates that the above data information is a point coordinate 0 Each small section of Vertex from 0 Vertex to 70 192 is a point coordinate ... ... 70 192 0 Vertex 8 Object01 10 0 20 0 30 0 When 70 follows 128, the coordinate data of each point of the entity has been recorded 70, and the following is what is recorded? The way is combined into 128 triangles. The values followed by the following two, the second, fourth point is composed of a triangular, and the order of the point is in the order 72 order in accordance with the memory of the DXF file. When a certain value is negative, it indicates that the line does not draw the line, 1 If you want to draw a three-dimensional entity, this feature must be used, otherwise the line 73 will disorder. -4 0 Vertex ... ... 0 0 After SEQEND indicates that the data of the entity has all been recorded in SEQEND 8 Object01 0 Polyline 0 Next, Polyline indicates that the following is a new entity ... ... 0 Endsec 0 Back Endsec Indicates that this is the end of this section 0 EOF 0 Release EOF indicates that this DXF file ends in the DXF file, we are most concerned about how to get the model on the model. The coordinates and use these points into many tricals, constitute faces, and plotted throughout the model. In the structure of the DXF file, we have seen that the DXF file first narrates the coordinates of the individual points on the entity, and then describes how many faces on the entity, which points for each side. In this way, we need at least 2 arrays to store an entity information, a coordinates for storage points, one for storage point, we can put these 2 arrays in a structure, if the number of entities in the model is not limited One is, we use this structure to define an array. In this article, we use Visual C 6.0 to write a small program that read the DXF file. In practical applications, the number of entities in the model and the number of entities and the number of norms are uncertain. In order to effectively utilize memory, we select the object Vertex, Sequence created by the aggregated class Cobarray class in the MFC class library to store and Manage the point coordinates and dot sequences of the entity. The CobArray class is a polymeric class for storing array classes. It can automatically perform its own high speed according to the array (or structure) you want to save, and this class itself has the member function makes us more operations to its objects. Convenient, fast, and it is easy to read. A portion of the information in the model information of the three-dimensional entity model can be read in the title segment. Three variables named $ ucsorg can be obtained by reading the variable name of $ ucsorg. 3D entities can obtain three-dimensional entity coordinate. By reading $ extMax, $ extmin can know the range of 3D entities in the world coordinate system, while other parts of the information can only be determined by calculating all DXF files. For all point coordinates of the three-dimensional entity model, in order, you can read out the basic structure of the DXF file described earlier in the physical segment. Now let's start writing this program.
First create a header foot.h definition: Vertex, Sequence, and Class CVertex, Csequence. Typedef struct {float x, y, z;} Vertex; Structure Vertex is used to store the coordinate typef struct {Int A, B, C;} Sequence; Structure SEQUENCE is used to store the composition of the entity TypedEf struct {char Obname [ 20]; Define Structure MyVertex to store the name of the entity, the coordinates of the point, Cobarray Vertex; where the point coordinates and faces are the objects defined by the aggregated class Cobarray; in storage, we can Add the Vertex structure and the Sequence structure to} myvertex; save the CLASS CVERTEX: Public COBJECT {Because the Cobarray class can only join the object derived by COBject, protected: We also need to build a COBJECT class CVERTEX class. In CVERTEX CVERTEX (); there is a variable of a Vertex structure: m_vertex, the information is actually stored in this declare_dyncreate (CVertex) variable. Virtual ~ CVERTEX (); // attributes public: We also need to build a CVERTEX class derived from the COBJECT class. In Cvertex Class Cvertex (Vertex & Ver), there is a variable of a Vertex structure: m_vertex, the information is actually stored in this variable, the function CVERTEX (Vertex & Ver) puts the Vertex structure Vertex M_Vertex; in the COBARRAY object. }; Class Csequence: Public cobject {This is also a class, role and just CVERTEX classes, protected: only Csequence class is the composition (order) of the middle of the entity. Csequence (); DECLARE_DYNCREATE (CSEQUENCE) Virtual ~ csequence (); public: csequence (sequence & sequ); sequence m_sequence;}; declaration structure and class, we also need to establish a .cpp file to define several functions. Implement_dyncreate (CVERTEX, COBJECT) CVERTEX :: CVERTEX () {} CVERTEX :: ~ CVERTEX () Constructor and Destruction Functions are empty {} cvertex :: CVERTEX (Vertex & Ver) {This function is: put a Vertex Structure data storage variable m_vertex m_vertex = ver; it is the most important ring in this class. } IMPLEMENT_DYNCREATE (CSequence, CObject) class definition as CVertex CSequence :: CSequence () {} Csequence class, only in a m_sequence therein CVertex class parameter type and parameters are not the same type of my_vertex CSequence :: ~ CSequence ( {} Csequence :: csequence (sequence & sequ) {m_sequence = sequ;} Then define a pointer * MyData with the structure MyVertex (as defined above), with the purpose of assigning the appropriate memory to the pointer according to how much the entity in the model, It has become a structural array.
Define a function that is used to determine how many entities in the model, the return value of the function is the number of entities. INT Cjupiterview :: getObjectnumber () {char str1 [10], str2 [10]; char name [] = "thefirst"; int Num; Num = 0; file * fp; fp = fopen ("data.dxf", " R "); Open the DXF file, Data.dxf While (! Feof (fp) &&! ferror (fp)) This function is based on the name of the entity to determine the number of the number of the entity, so the function is only read, once A new entity name, FSCANF (FP, "% S / N", STR1); the number of solids plus one. IF (strCMP (str1, "vertex") == 0) {fscanf (fp, "% s / n", str2); open DXF file, DATA.DXF FSCANF (FP, "% S / N", STR2); This function is based on the name of the entity to determine the number of IF (strCMP (name, str2)! = 0), so the function reads only the name of the entity, once the new entity name, the {entity number is added. STRCPY (Name, Str2); Num ;}}} fclose (fp); Return Num;} The following is the program code of the coordinates and dot sequences of the physical point. In this program, the coordinates of the model in the model are read. Maximum value and minimum value, entity name, point coordinates, and order.
Void cjupiterview :: onfileinput () {// Todo: add your commnd handler code here file * fp, * fp2; int i, k, j; float tempx, tempy, tempz; float xmin, ymin, zmin, xmax, ymax, Zmax, Max; Int Lab; Char Str1 [20], STR2 [20], STR [20], Ht; Char MyName [20]; int myNumber; Vertex TempvertEx; sequence tempsequence; typedef struct {float x, y, z, Max;} max; ht = 9; Objectnumber = getObjectnumber (); mydata = new myvertex [objectnumber]; fp = fopen (filename, "r"); i = 0; j = 0; k = 0; MyNumber = -1; Strcpy (MyName, "Objectname"); While (! Feof (fp) &&! Ferror (fp)) {fscanf (FP, "% S / N", STR); if (strcmp (str, "$ Extmin ") == 0) {FSCANF (FP,"% S / N ", STR1); FSCANF (FP,"% f / n ", & xmin); fscanf (FP,"% S / N ", STR1); FSCANF (FP, "% f / n", & ymin); FSCANF (FP, "% S / N", STR1); FSCANF (FP, "% f / n", & zmin);} IF (strcmp (str, " $ ExtMax ") == 0) {fscanf (fp,"% s / n ", str1); fscanf (fp,"% f / n ", & xmax); fscanf (fp,"% s / n ", str1) FSCANF (FP, "% f / n", & ymax; fscanf (fp, "% s / n", str1); fscanf (fp, "% f / n", & zmax); max.x = max (ABS (XMAX), ABS (Xmin)); Max.y = max (ABS (Ymax), ABS (Ymin)); max.z = max (ABS (Zmax), ABS (ZMIN )); max.max = max (max.x, max.y); max.max = max (max.max, max.z);} if (strcmp (str, "vertex") == 0) {fscanf (FP, "% S / N", STR1); FSCANF (FP, "% S / N", STR1); IF (strcmp (myname, str1)! = 0) {MyNumber ; strcpy (MyName, Str1); strcpy ((MyData MyNumber) -> ObName, MyName);} fscanf (fp, "% s / n", str2); fscanf (fp, "% f / n", & tempx); fscanf (fp, "% s / n ", str2); fscanf (fp,"% f / n ", & tempy; fscanf (fp,"% s / n ", str2); fscanf (fp,"% f / n ", & tempz);