]> Shamusworld >> Repos - architektonas/blob - dxflib/src/dl_dxf.h
Refactored CAD tool bars to use predefined actions.
[architektonas] / dxflib / src / dl_dxf.h
1 /****************************************************************************
2 ** $Id: dl_dxf.h 2398 2005-06-06 18:12:14Z andrew $
3 **
4 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
5 **
6 ** This file is part of the dxflib project.
7 **
8 ** This file may be distributed and/or modified under the terms of the
9 ** GNU General Public License version 2 as published by the Free Software
10 ** Foundation and appearing in the file LICENSE.GPL included in the
11 ** packaging of this file.
12 **
13 ** Licensees holding valid dxflib Professional Edition licenses may use 
14 ** this file in accordance with the dxflib Commercial License
15 ** Agreement provided with the Software.
16 **
17 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 **
20 ** See http://www.ribbonsoft.com for further details.
21 **
22 ** Contact info@ribbonsoft.com if any conditions of this licensing are
23 ** not clear to you.
24 **
25 **********************************************************************/
26
27 #ifndef DL_DXF_H
28 #define DL_DXF_H
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #ifndef __GCC2x__
34 #include <sstream>
35 #endif
36
37 #include "dl_attributes.h"
38 #include "dl_codes.h"
39 #include "dl_entities.h"
40 #include "dl_writer_ascii.h"
41
42 #ifdef _WIN32
43 #undef M_PI
44 #define M_PI   3.14159265358979323846
45 #pragma warning(disable : 4800)
46 #endif
47
48 #ifndef M_PI
49 #define M_PI 3.1415926535897932384626433832795
50 #endif
51
52 class DL_CreationInterface;
53 class DL_WriterA;
54
55
56 #define DL_VERSION     "2.0.4.8"
57
58 #define DL_UNKNOWN               0
59 #define DL_LAYER                10
60 #define DL_BLOCK                11
61 #define DL_ENDBLK               12
62 #define DL_LINETYPE             13
63 #define DL_SETTING              50
64 #define DL_ENTITY_POINT        100
65 #define DL_ENTITY_LINE         101
66 #define DL_ENTITY_POLYLINE     102
67 #define DL_ENTITY_LWPOLYLINE   103
68 #define DL_ENTITY_VERTEX       104
69 #define DL_ENTITY_SPLINE       105
70 #define DL_ENTITY_KNOT         106
71 #define DL_ENTITY_CONTROLPOINT 107
72 #define DL_ENTITY_ARC          108
73 #define DL_ENTITY_CIRCLE       109
74 #define DL_ENTITY_ELLIPSE      110
75 #define DL_ENTITY_INSERT       111
76 #define DL_ENTITY_TEXT         112
77 #define DL_ENTITY_MTEXT        113
78 #define DL_ENTITY_DIMENSION    114
79 #define DL_ENTITY_LEADER       115
80 #define DL_ENTITY_HATCH        116
81 #define DL_ENTITY_ATTRIB       117
82 #define DL_ENTITY_IMAGE        118
83 #define DL_ENTITY_IMAGEDEF     119
84 #define DL_ENTITY_TRACE        120
85 #define DL_ENTITY_SOLID        121
86 #define DL_ENTITY_SEQEND       122
87
88
89 /**
90  * Reading and writing of DXF files.
91  *
92  * This class can read in a DXF file and calls methods from the 
93  * interface DL_EntityContainer to add the entities to the
94  * contianer provided by the user of the library.
95  *
96  * It can also be used to write DXF files to a certain extent.
97  *
98  * When saving entities, special values for colors and linetypes 
99  * can be used:
100  *
101  * Special colors are 0 (=BYBLOCK) and 256 (=BYLAYER).
102  * Special linetypes are "BYLAYER" and "BYBLOCK".
103  *
104  * @author Andrew Mustun
105  */
106 class DL_Dxf {
107 public:
108     DL_Dxf();
109     ~DL_Dxf();
110
111     bool in(const string& file,
112             DL_CreationInterface* creationInterface);
113     bool readDxfGroups(FILE* fp,
114                        DL_CreationInterface* creationInterface,
115                                            int* errorCounter = NULL);
116     static bool getChoppedLine(char* s, unsigned int size,
117                                FILE *stream);
118
119 #ifndef __GCC2x__
120
121     bool readDxfGroups(std::stringstream &stream,
122                        DL_CreationInterface* creationInterface,
123                                            int* errorCounter = NULL);
124     bool in(std::stringstream &stream,
125             DL_CreationInterface* creationInterface);
126     static bool getChoppedLine(char *s, unsigned int size,
127                                std::stringstream &stream);
128 #endif
129
130     static bool stripWhiteSpace(char** s);
131
132     bool processDXFGroup(DL_CreationInterface* creationInterface,
133                          int groupCode, const char* groupValue);
134     void addSetting(DL_CreationInterface* creationInterface);
135     void addLayer(DL_CreationInterface* creationInterface);
136     void addBlock(DL_CreationInterface* creationInterface);
137     void endBlock(DL_CreationInterface* creationInterface);
138
139     void addPoint(DL_CreationInterface* creationInterface);
140     void addLine(DL_CreationInterface* creationInterface);
141         
142     void addPolyline(DL_CreationInterface* creationInterface);
143     void addVertex(DL_CreationInterface* creationInterface);
144         
145     void addSpline(DL_CreationInterface* creationInterface);
146     //void addKnot(DL_CreationInterface* creationInterface);
147     //void addControlPoint(DL_CreationInterface* creationInterface);
148         
149     void addArc(DL_CreationInterface* creationInterface);
150     void addCircle(DL_CreationInterface* creationInterface);
151     void addEllipse(DL_CreationInterface* creationInterface);
152     void addInsert(DL_CreationInterface* creationInterface);
153     
154     void addTrace(DL_CreationInterface* creationInterface);
155     void addSolid(DL_CreationInterface* creationInterface);
156
157     void addMText(DL_CreationInterface* creationInterface);
158         bool handleMTextData(DL_CreationInterface* creationInterface);
159         bool handleLWPolylineData(DL_CreationInterface* creationInterface);
160         bool handleSplineData(DL_CreationInterface* creationInterface);
161         bool handleLeaderData(DL_CreationInterface* creationInterface);
162         bool handleHatchData(DL_CreationInterface* creationInterface);
163         
164     void addText(DL_CreationInterface* creationInterface);
165     void addAttrib(DL_CreationInterface* creationInterface);
166     DL_DimensionData getDimData();
167     void addDimLinear(DL_CreationInterface* creationInterface);
168     void addDimAligned(DL_CreationInterface* creationInterface);
169     void addDimRadial(DL_CreationInterface* creationInterface);
170     void addDimDiametric(DL_CreationInterface* creationInterface);
171     void addDimAngular(DL_CreationInterface* creationInterface);
172     void addDimAngular3P(DL_CreationInterface* creationInterface);
173     void addLeader(DL_CreationInterface* creationInterface);
174     void addHatch(DL_CreationInterface* creationInterface);
175     void addImage(DL_CreationInterface* creationInterface);
176     void addImageDef(DL_CreationInterface* creationInterface);
177
178         void endEntity(DL_CreationInterface* creationInterface);
179         
180     void endSequence(DL_CreationInterface* creationInterface);
181         
182         int  stringToInt(const char* s, bool* ok=NULL); 
183
184     DL_WriterA* out(const char* file,
185                     DL_Codes::version version=VER_2000);
186
187     void writeHeader(DL_WriterA& dw);
188
189     void writePoint(DL_WriterA& dw,
190                     const DL_PointData& data,
191                     const DL_Attributes& attrib);
192     void writeLine(DL_WriterA& dw,
193                    const DL_LineData& data,
194                    const DL_Attributes& attrib);
195     void writePolyline(DL_WriterA& dw,
196                        const DL_PolylineData& data,
197                        const DL_Attributes& attrib);
198     void writeVertex(DL_WriterA& dw,
199                      const DL_VertexData& data);
200     void writePolylineEnd(DL_WriterA& dw);
201     void writeSpline(DL_WriterA& dw,
202                        const DL_SplineData& data,
203                        const DL_Attributes& attrib);
204     void writeControlPoint(DL_WriterA& dw,
205                      const DL_ControlPointData& data);
206     void writeKnot(DL_WriterA& dw,
207                      const DL_KnotData& data);
208     void writeCircle(DL_WriterA& dw,
209                      const DL_CircleData& data,
210                      const DL_Attributes& attrib);
211     void writeArc(DL_WriterA& dw,
212                   const DL_ArcData& data,
213                   const DL_Attributes& attrib);
214     void writeEllipse(DL_WriterA& dw,
215                       const DL_EllipseData& data,
216                       const DL_Attributes& attrib);
217     void writeInsert(DL_WriterA& dw,
218                      const DL_InsertData& data,
219                      const DL_Attributes& attrib);
220     void writeMText(DL_WriterA& dw,
221                     const DL_MTextData& data,
222                     const DL_Attributes& attrib);
223     void writeText(DL_WriterA& dw,
224                     const DL_TextData& data,
225                     const DL_Attributes& attrib);
226     void writeDimAligned(DL_WriterA& dw,
227                          const DL_DimensionData& data,
228                          const DL_DimAlignedData& edata,
229                          const DL_Attributes& attrib);
230     void writeDimLinear(DL_WriterA& dw,
231                         const DL_DimensionData& data,
232                         const DL_DimLinearData& edata,
233                         const DL_Attributes& attrib);
234     void writeDimRadial(DL_WriterA& dw,
235                         const DL_DimensionData& data,
236                         const DL_DimRadialData& edata,
237                         const DL_Attributes& attrib);
238     void writeDimDiametric(DL_WriterA& dw,
239                            const DL_DimensionData& data,
240                            const DL_DimDiametricData& edata,
241                            const DL_Attributes& attrib);
242     void writeDimAngular(DL_WriterA& dw,
243                          const DL_DimensionData& data,
244                          const DL_DimAngularData& edata,
245                          const DL_Attributes& attrib);
246     void writeDimAngular3P(DL_WriterA& dw,
247                            const DL_DimensionData& data,
248                            const DL_DimAngular3PData& edata,
249                            const DL_Attributes& attrib);
250     void writeLeader(DL_WriterA& dw,
251                      const DL_LeaderData& data,
252                      const DL_Attributes& attrib);
253     void writeLeaderVertex(DL_WriterA& dw,
254                            const DL_LeaderVertexData& data);
255     void writeHatch1(DL_WriterA& dw,
256                      const DL_HatchData& data,
257                      const DL_Attributes& attrib);
258     void writeHatch2(DL_WriterA& dw,
259                      const DL_HatchData& data,
260                      const DL_Attributes& attrib);
261     void writeHatchLoop1(DL_WriterA& dw,
262                          const DL_HatchLoopData& data);
263     void writeHatchLoop2(DL_WriterA& dw,
264                          const DL_HatchLoopData& data);
265     void writeHatchEdge(DL_WriterA& dw,
266                         const DL_HatchEdgeData& data);
267
268     int writeImage(DL_WriterA& dw,
269                    const DL_ImageData& data,
270                    const DL_Attributes& attrib);
271
272     void writeImageDef(DL_WriterA& dw, int handle,
273                        const DL_ImageData& data);
274
275     void writeLayer(DL_WriterA& dw,
276                     const DL_LayerData& data,
277                     const DL_Attributes& attrib);
278
279     void writeLineType(DL_WriterA& dw,
280                        const DL_LineTypeData& data);
281
282     void writeAppid(DL_WriterA& dw, const string& name);
283
284     void writeBlock(DL_WriterA& dw,
285                     const DL_BlockData& data);
286     void writeEndBlock(DL_WriterA& dw, const string& name);
287
288     void writeVPort(DL_WriterA& dw);
289     void writeStyle(DL_WriterA& dw);
290     void writeView(DL_WriterA& dw);
291     void writeUcs(DL_WriterA& dw);
292     void writeDimStyle(DL_WriterA& dw, 
293                            double dimasz, double dimexe, double dimexo,
294                                            double dimgap, double dimtxt);
295     void writeBlockRecord(DL_WriterA& dw);
296     void writeBlockRecord(DL_WriterA& dw, const string& name);
297     void writeObjects(DL_WriterA& dw);
298     void writeObjectsEnd(DL_WriterA& dw);
299
300     /**
301      * Converts the given string into a double or returns the given 
302      * default valud (def) if value is NULL or empty.
303      */
304     static double toReal(const char* value, double def=0.0) {
305        if (value!=NULL && value[0] != '\0') {
306             double ret;
307             if (strchr(value, ',') != NULL) {
308                char* tmp = new char[strlen(value)+1];
309                strcpy(tmp, value);
310                DL_WriterA::strReplace(tmp, ',', '.');
311                ret = atof(tmp);
312                    delete[] tmp;
313             }
314             else {
315                ret = atof(value);
316             }
317                         return ret;
318         } else {
319             return def;
320         }
321     }
322     /**
323      * Converts the given string into an int or returns the given 
324      * default valud (def) if value is NULL or empty.
325      */
326     static int toInt(const char* value, int def=0) {
327         if (value!=NULL && value[0] != '\0') {
328             return atoi(value);
329         } else {
330             return def;
331         }
332     }
333     /**
334      * Converts the given string into a string or returns the given 
335      * default valud (def) if value is NULL or empty.
336      */
337     static const char* toString(const char* value, const char* def="") {
338         if (value!=NULL && value[0] != '\0') {
339             return value;
340         } else {
341             return def;
342         }
343     }
344
345         static bool checkVariable(const char* var, DL_Codes::version version);
346
347         DL_Codes::version getVersion() {
348                 return version;
349         }
350
351         int getLibVersion(const char* str);
352
353         static void test();
354
355 private:
356     DL_Codes::version version;
357     unsigned long styleHandleStd;
358
359         string polylineLayer;
360     double* vertices;
361     int maxVertices;
362     int vertexIndex;
363         
364     double* knots;
365     int maxKnots;
366     int knotIndex;
367         
368     double* controlPoints;
369     int maxControlPoints;
370     int controlPointIndex;
371
372     double* leaderVertices;
373     int maxLeaderVertices;
374     int leaderVertexIndex;
375
376     // array of hatch loops
377     DL_HatchLoopData* hatchLoops;
378     int maxHatchLoops;
379     int hatchLoopIndex;
380     // array in format [loop#][edge#]
381     DL_HatchEdgeData** hatchEdges;
382     int* maxHatchEdges;
383     int* hatchEdgeIndex;
384     bool dropEdges;
385
386     // Bulge for the next vertex.
387     double bulge;
388
389     // Only the useful part of the group code
390     char groupCodeTmp[DL_DXF_MAXLINE+1];
391     // ...same as integer
392     unsigned int groupCode;
393     // Only the useful part of the group value
394     char groupValue[DL_DXF_MAXLINE+1];
395     // Current entity type
396     int currentEntity;
397     // Value of the current setting
398     char settingValue[DL_DXF_MAXLINE+1];
399     // Key of the current setting (e.g. "$ACADVER")
400     char settingKey[DL_DXF_MAXLINE+1];
401     // Stores the group codes
402     char values[DL_DXF_MAXGROUPCODE][DL_DXF_MAXLINE+1];
403     // First call of this method. We initialize all group values in
404     //  the first call.
405     bool firstCall;
406     // Attributes of the current entity (layer, color, width, line type)
407     DL_Attributes attrib;
408         // library version. hex: 0x20003001 = 2.0.3.1
409         int libVersion;
410 };
411
412 #endif
413
414 // EOF