1 /****************************************************************************
2 ** $Id: dl_dxf.h 2398 2005-06-06 18:12:14Z andrew $
4 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
6 ** This file is part of the dxflib project.
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.
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.
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.
20 ** See http://www.ribbonsoft.com for further details.
22 ** Contact info@ribbonsoft.com if any conditions of this licensing are
25 **********************************************************************/
37 #include "dl_attributes.h"
39 #include "dl_entities.h"
40 #include "dl_writer_ascii.h"
44 #define M_PI 3.14159265358979323846
45 #pragma warning(disable : 4800)
49 #define M_PI 3.1415926535897932384626433832795
52 class DL_CreationInterface;
56 #define DL_VERSION "2.0.4.8"
62 #define DL_LINETYPE 13
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
90 * Reading and writing of DXF files.
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.
96 * It can also be used to write DXF files to a certain extent.
98 * When saving entities, special values for colors and linetypes
101 * Special colors are 0 (=BYBLOCK) and 256 (=BYLAYER).
102 * Special linetypes are "BYLAYER" and "BYBLOCK".
104 * @author Andrew Mustun
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,
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);
130 static bool stripWhiteSpace(char** s);
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);
139 void addPoint(DL_CreationInterface* creationInterface);
140 void addLine(DL_CreationInterface* creationInterface);
142 void addPolyline(DL_CreationInterface* creationInterface);
143 void addVertex(DL_CreationInterface* creationInterface);
145 void addSpline(DL_CreationInterface* creationInterface);
146 //void addKnot(DL_CreationInterface* creationInterface);
147 //void addControlPoint(DL_CreationInterface* creationInterface);
149 void addArc(DL_CreationInterface* creationInterface);
150 void addCircle(DL_CreationInterface* creationInterface);
151 void addEllipse(DL_CreationInterface* creationInterface);
152 void addInsert(DL_CreationInterface* creationInterface);
154 void addTrace(DL_CreationInterface* creationInterface);
155 void addSolid(DL_CreationInterface* creationInterface);
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);
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);
178 void endEntity(DL_CreationInterface* creationInterface);
180 void endSequence(DL_CreationInterface* creationInterface);
182 int stringToInt(const char* s, bool* ok=NULL);
184 DL_WriterA* out(const char* file,
185 DL_Codes::version version=VER_2000);
187 void writeHeader(DL_WriterA& dw);
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);
268 int writeImage(DL_WriterA& dw,
269 const DL_ImageData& data,
270 const DL_Attributes& attrib);
272 void writeImageDef(DL_WriterA& dw, int handle,
273 const DL_ImageData& data);
275 void writeLayer(DL_WriterA& dw,
276 const DL_LayerData& data,
277 const DL_Attributes& attrib);
279 void writeLineType(DL_WriterA& dw,
280 const DL_LineTypeData& data);
282 void writeAppid(DL_WriterA& dw, const string& name);
284 void writeBlock(DL_WriterA& dw,
285 const DL_BlockData& data);
286 void writeEndBlock(DL_WriterA& dw, const string& name);
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);
301 * Converts the given string into a double or returns the given
302 * default valud (def) if value is NULL or empty.
304 static double toReal(const char* value, double def=0.0) {
305 if (value!=NULL && value[0] != '\0') {
307 if (strchr(value, ',') != NULL) {
308 char* tmp = new char[strlen(value)+1];
310 DL_WriterA::strReplace(tmp, ',', '.');
323 * Converts the given string into an int or returns the given
324 * default valud (def) if value is NULL or empty.
326 static int toInt(const char* value, int def=0) {
327 if (value!=NULL && value[0] != '\0') {
334 * Converts the given string into a string or returns the given
335 * default valud (def) if value is NULL or empty.
337 static const char* toString(const char* value, const char* def="") {
338 if (value!=NULL && value[0] != '\0') {
345 static bool checkVariable(const char* var, DL_Codes::version version);
347 DL_Codes::version getVersion() {
351 int getLibVersion(const char* str);
356 DL_Codes::version version;
357 unsigned long styleHandleStd;
359 string polylineLayer;
368 double* controlPoints;
369 int maxControlPoints;
370 int controlPointIndex;
372 double* leaderVertices;
373 int maxLeaderVertices;
374 int leaderVertexIndex;
376 // array of hatch loops
377 DL_HatchLoopData* hatchLoops;
380 // array in format [loop#][edge#]
381 DL_HatchEdgeData** hatchEdges;
386 // Bulge for the next vertex.
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
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
406 // Attributes of the current entity (layer, color, width, line type)
407 DL_Attributes attrib;
408 // library version. hex: 0x20003001 = 2.0.3.1