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