QCAD
Open Source 2D CAD
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
dl_dxf.h
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2001-2013 RibbonSoft, GmbH. All rights reserved.
3 **
4 ** This file is part of the dxflib project.
5 **
6 ** This file is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 of the License, or
9 ** (at your option) any later version.
10 **
11 ** Licensees holding valid dxflib Professional Edition licenses may use
12 ** this file in accordance with the dxflib Commercial License
13 ** Agreement provided with the Software.
14 **
15 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17 **
18 ** See http://www.ribbonsoft.com for further details.
19 **
20 ** Contact info@ribbonsoft.com if any conditions of this licensing are
21 ** not clear to you.
22 **
23 **********************************************************************/
24 
25 #ifndef DL_DXF_H
26 #define DL_DXF_H
27 
28 #include "dl_global.h"
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string>
33 #include <sstream>
34 #include <map>
35 
36 #include "dl_attributes.h"
37 #include "dl_codes.h"
38 #include "dl_entities.h"
39 #include "dl_writer_ascii.h"
40 
41 #ifdef _WIN32
42 #undef M_PI
43 #define M_PI 3.14159265358979323846
44 #pragma warning(disable : 4800)
45 #endif
46 
47 #ifndef M_PI
48 #define M_PI 3.1415926535897932384626433832795
49 #endif
50 
52 class DL_WriterA;
53 
54 
55 #define DL_VERSION "3.7.5.0"
56 
57 #define DL_VERSION_MAJOR 3
58 #define DL_VERSION_MINOR 7
59 #define DL_VERSION_REV 5
60 #define DL_VERSION_BUILD 0
61 
62 #define DL_UNKNOWN 0
63 #define DL_LAYER 10
64 #define DL_BLOCK 11
65 #define DL_ENDBLK 12
66 #define DL_LINETYPE 13
67 #define DL_STYLE 20
68 #define DL_SETTING 50
69 #define DL_ENTITY_POINT 100
70 #define DL_ENTITY_LINE 101
71 #define DL_ENTITY_POLYLINE 102
72 #define DL_ENTITY_LWPOLYLINE 103
73 #define DL_ENTITY_VERTEX 104
74 #define DL_ENTITY_SPLINE 105
75 #define DL_ENTITY_KNOT 106
76 #define DL_ENTITY_CONTROLPOINT 107
77 #define DL_ENTITY_ARC 108
78 #define DL_ENTITY_CIRCLE 109
79 #define DL_ENTITY_ELLIPSE 110
80 #define DL_ENTITY_INSERT 111
81 #define DL_ENTITY_TEXT 112
82 #define DL_ENTITY_MTEXT 113
83 #define DL_ENTITY_DIMENSION 114
84 #define DL_ENTITY_LEADER 115
85 #define DL_ENTITY_HATCH 116
86 #define DL_ENTITY_ATTRIB 117
87 #define DL_ENTITY_IMAGE 118
88 #define DL_ENTITY_IMAGEDEF 119
89 #define DL_ENTITY_TRACE 120
90 #define DL_ENTITY_SOLID 121
91 #define DL_ENTITY_3DFACE 122
92 #define DL_ENTITY_XLINE 123
93 #define DL_ENTITY_RAY 124
94 #define DL_ENTITY_SEQEND 125
95 #define DL_XRECORD 200
96 #define DL_DICTIONARY 210
97 
98 
117 public:
118  DL_Dxf();
119  ~DL_Dxf();
120 
121  bool in(const std::string& file,
122  DL_CreationInterface* creationInterface);
123  bool readDxfGroups(FILE* fp,
124  DL_CreationInterface* creationInterface);
125  static bool getStrippedLine(std::string& s, unsigned int size,
126  FILE* stream, bool stripSpace = true);
127 
128  bool readDxfGroups(std::stringstream& stream,
129  DL_CreationInterface* creationInterface);
130  bool in(std::stringstream &stream,
131  DL_CreationInterface* creationInterface);
132  static bool getStrippedLine(std::string& s, unsigned int size,
133  std::stringstream& stream, bool stripSpace = true);
134 
135  static bool stripWhiteSpace(char** s, bool stripSpaces = true);
136 
137  bool processDXFGroup(DL_CreationInterface* creationInterface,
138  int groupCode, const std::string& groupValue);
139  void addSetting(DL_CreationInterface* creationInterface);
140  void addLayer(DL_CreationInterface* creationInterface);
141  void addLinetype(DL_CreationInterface *creationInterface);
142  void addBlock(DL_CreationInterface* creationInterface);
143  void endBlock(DL_CreationInterface* creationInterface);
144  void addTextStyle(DL_CreationInterface* creationInterface);
145 
146  void addPoint(DL_CreationInterface* creationInterface);
147  void addLine(DL_CreationInterface* creationInterface);
148  void addXLine(DL_CreationInterface* creationInterface);
149  void addRay(DL_CreationInterface* creationInterface);
150 
151  void addPolyline(DL_CreationInterface* creationInterface);
152  void addVertex(DL_CreationInterface* creationInterface);
153 
154  void addSpline(DL_CreationInterface* creationInterface);
155 
156  void addArc(DL_CreationInterface* creationInterface);
157  void addCircle(DL_CreationInterface* creationInterface);
158  void addEllipse(DL_CreationInterface* creationInterface);
159  void addInsert(DL_CreationInterface* creationInterface);
160 
161  void addTrace(DL_CreationInterface* creationInterface);
162  void add3dFace(DL_CreationInterface* creationInterface);
163  void addSolid(DL_CreationInterface* creationInterface);
164 
165  void addMText(DL_CreationInterface* creationInterface);
166  void addText(DL_CreationInterface* creationInterface);
167 
168  void addAttribute(DL_CreationInterface* creationInterface);
169 
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 
179  void addLeader(DL_CreationInterface* creationInterface);
180 
181  void addHatch(DL_CreationInterface* creationInterface);
182  void addHatchLoop();
183  void addHatchEdge();
184  bool handleHatchData(DL_CreationInterface* creationInterface);
185 
186  void addImage(DL_CreationInterface* creationInterface);
187  void addImageDef(DL_CreationInterface* creationInterface);
188 
189  void addComment(DL_CreationInterface* creationInterface, const std::string& comment);
190 
191  void addDictionary(DL_CreationInterface* creationInterface);
192  void addDictionaryEntry(DL_CreationInterface* creationInterface);
193 
194  bool handleXRecordData(DL_CreationInterface* creationInterface);
195  bool handleDictionaryData(DL_CreationInterface* creationInterface);
196 
197  bool handleXData(DL_CreationInterface *creationInterface);
198  bool handleMTextData(DL_CreationInterface* creationInterface);
199  bool handleLWPolylineData(DL_CreationInterface* creationInterface);
200  bool handleSplineData(DL_CreationInterface* creationInterface);
201  bool handleLeaderData(DL_CreationInterface* creationInterface);
202  bool handleLinetypeData(DL_CreationInterface* creationInterface);
203 
204  void endEntity(DL_CreationInterface* creationInterface);
205 
206  void endSequence(DL_CreationInterface* creationInterface);
207 
208  //int stringToInt(const char* s, bool* ok=NULL);
209 
210  DL_WriterA* out(const char* file,
212 
213  void writeHeader(DL_WriterA& dw);
214 
215  void writePoint(DL_WriterA& dw,
216  const DL_PointData& data,
217  const DL_Attributes& attrib);
218  void writeLine(DL_WriterA& dw,
219  const DL_LineData& data,
220  const DL_Attributes& attrib);
221  void writeXLine(DL_WriterA& dw,
222  const DL_XLineData& data,
223  const DL_Attributes& attrib);
224  void writeRay(DL_WriterA& dw,
225  const DL_RayData& data,
226  const DL_Attributes& attrib);
227  void writePolyline(DL_WriterA& dw,
228  const DL_PolylineData& data,
229  const DL_Attributes& attrib);
230  void writeVertex(DL_WriterA& dw,
231  const DL_VertexData& data);
232  void writePolylineEnd(DL_WriterA& dw);
233  void writeSpline(DL_WriterA& dw,
234  const DL_SplineData& data,
235  const DL_Attributes& attrib);
236  void writeControlPoint(DL_WriterA& dw,
237  const DL_ControlPointData& data);
238  void writeFitPoint(DL_WriterA& dw,
239  const DL_FitPointData& data);
240  void writeKnot(DL_WriterA& dw,
241  const DL_KnotData& data);
242  void writeCircle(DL_WriterA& dw,
243  const DL_CircleData& data,
244  const DL_Attributes& attrib);
245  void writeArc(DL_WriterA& dw,
246  const DL_ArcData& data,
247  const DL_Attributes& attrib);
248  void writeEllipse(DL_WriterA& dw,
249  const DL_EllipseData& data,
250  const DL_Attributes& attrib);
251  void writeSolid(DL_WriterA& dw,
252  const DL_SolidData& data,
253  const DL_Attributes& attrib);
254  void writeTrace(DL_WriterA& dw,
255  const DL_TraceData& data,
256  const DL_Attributes& attrib);
257  void write3dFace(DL_WriterA& dw,
258  const DL_3dFaceData& data,
259  const DL_Attributes& attrib);
260  void writeInsert(DL_WriterA& dw,
261  const DL_InsertData& data,
262  const DL_Attributes& attrib);
263  void writeMText(DL_WriterA& dw,
264  const DL_MTextData& data,
265  const DL_Attributes& attrib);
266  void writeText(DL_WriterA& dw,
267  const DL_TextData& data,
268  const DL_Attributes& attrib);
269  void writeAttribute(DL_WriterA& dw,
270  const DL_AttributeData& data,
271  const DL_Attributes& attrib);
272  void writeDimStyleOverrides(DL_WriterA& dw,
273  const DL_DimensionData& data);
274  void writeDimAligned(DL_WriterA& dw,
275  const DL_DimensionData& data,
276  const DL_DimAlignedData& edata,
277  const DL_Attributes& attrib);
278  void writeDimLinear(DL_WriterA& dw,
279  const DL_DimensionData& data,
280  const DL_DimLinearData& edata,
281  const DL_Attributes& attrib);
282  void writeDimRadial(DL_WriterA& dw,
283  const DL_DimensionData& data,
284  const DL_DimRadialData& edata,
285  const DL_Attributes& attrib);
286  void writeDimDiametric(DL_WriterA& dw,
287  const DL_DimensionData& data,
288  const DL_DimDiametricData& edata,
289  const DL_Attributes& attrib);
290  void writeDimAngular(DL_WriterA& dw,
291  const DL_DimensionData& data,
292  const DL_DimAngularData& edata,
293  const DL_Attributes& attrib);
294  void writeDimAngular3P(DL_WriterA& dw,
295  const DL_DimensionData& data,
296  const DL_DimAngular3PData& edata,
297  const DL_Attributes& attrib);
298  void writeDimOrdinate(DL_WriterA& dw,
299  const DL_DimensionData& data,
300  const DL_DimOrdinateData& edata,
301  const DL_Attributes& attrib);
302  void writeLeader(DL_WriterA& dw,
303  const DL_LeaderData& data,
304  const DL_Attributes& attrib);
305  void writeLeaderVertex(DL_WriterA& dw,
306  const DL_LeaderVertexData& data);
307  void writeHatch1(DL_WriterA& dw,
308  const DL_HatchData& data,
309  const DL_Attributes& attrib);
310  void writeHatch2(DL_WriterA& dw,
311  const DL_HatchData& data,
312  const DL_Attributes& attrib);
313  void writeHatchLoop1(DL_WriterA& dw,
314  const DL_HatchLoopData& data);
315  void writeHatchLoop2(DL_WriterA& dw,
316  const DL_HatchLoopData& data);
317  void writeHatchEdge(DL_WriterA& dw,
318  const DL_HatchEdgeData& data);
319 
320  int writeImage(DL_WriterA& dw,
321  const DL_ImageData& data,
322  const DL_Attributes& attrib);
323 
324  void writeImageDef(DL_WriterA& dw, int handle,
325  const DL_ImageData& data);
326 
327  void writeLayer(DL_WriterA& dw,
328  const DL_LayerData& data,
329  const DL_Attributes& attrib);
330 
331  void writeLinetype(DL_WriterA& dw,
332  const DL_LinetypeData& data);
333 
334  void writeAppid(DL_WriterA& dw, const std::string& name);
335 
336  void writeBlock(DL_WriterA& dw,
337  const DL_BlockData& data);
338  void writeEndBlock(DL_WriterA& dw, const std::string& name);
339 
340  void writeVPort(DL_WriterA& dw);
341  void writeStyle(DL_WriterA& dw, const DL_StyleData& style);
342  void writeView(DL_WriterA& dw);
343  void writeUcs(DL_WriterA& dw);
344  void writeDimStyle(DL_WriterA& dw,
345  double dimasz, double dimexe, double dimexo,
346  double dimgap, double dimtxt);
347  void writeBlockRecord(DL_WriterA& dw);
348  void writeBlockRecord(DL_WriterA& dw, const std::string& name);
349  void writeObjects(DL_WriterA& dw, const std::string& appDictionaryName = "");
350  void writeAppDictionary(DL_WriterA& dw);
351  int writeDictionaryEntry(DL_WriterA& dw, const std::string& name);
352  void writeXRecord(DL_WriterA& dw, int handle, int value);
353  void writeXRecord(DL_WriterA& dw, int handle, double value);
354  void writeXRecord(DL_WriterA& dw, int handle, bool value);
355  void writeXRecord(DL_WriterA& dw, int handle, const std::string& value);
356  void writeObjectsEnd(DL_WriterA& dw);
357 
358  void writeComment(DL_WriterA& dw, const std::string& comment);
359 
364  //static double toReal(const char* value, double def=0.0);
365 
370 // static int toInt(const char* value, int def=0) {
371 // if (value!=NULL && value[0] != '\0') {
372 // return atoi(value);
373 // }
374 
375 // return def;
376 // }
377 
382 // static const char* toString(const char* value, const char* def="") {
383 // if (value!=NULL && value[0] != '\0') {
384 // return value;
385 // } else {
386 // return def;
387 // }
388 // }
389 
390  static bool checkVariable(const char* var, DL_Codes::version version);
391 
393  return version;
394  }
395 
396  int getLibVersion(const std::string &str);
397 
398  static void test();
399 
400  bool hasValue(int code) {
401  return values.count(code)==1;
402  }
403 
404  int getIntValue(int code, int def) {
405  if (!hasValue(code)) {
406  return def;
407  }
408  return toInt(values[code]);
409  }
410 
411  int toInt(const std::string& str) {
412  char* p;
413  return strtol(str.c_str(), &p, 10);
414  }
415 
416  bool toBool(const std::string& str) {
417  char* p;
418  return (bool)strtol(str.c_str(), &p, 10);
419  }
420 
421  std::string getStringValue(int code, const std::string& def) {
422  if (!hasValue(code)) {
423  return def;
424  }
425  return values[code];
426  }
427 
428  double getRealValue(int code, double def) {
429  if (!hasValue(code)) {
430  return def;
431  }
432  return toReal(values[code]);
433  }
434 
435  double toReal(const std::string& str) {
436  double ret;
437  // make sure the real value uses '.' not ',':
438  std::string str2 = str;
439  std::replace(str2.begin(), str2.end(), ',', '.');
440  // make sure c++ expects '.' not ',':
441  std::istringstream istr(str2);
442  istr.imbue(std::locale("C"));
443  istr >> ret;
444  return ret;
445  }
446 
447 private:
449 
450  std::string polylineLayer;
451  double* vertices;
454 
455  double* knots;
456  int maxKnots;
458 
459  double* weights;
461 
462  double* controlPoints;
465 
466  double* fitPoints;
469 
470  double* leaderVertices;
473 
476  std::vector<std::vector<DL_HatchEdgeData> > hatchEdges;
477 
478  std::string xRecordHandle;
480 
481  // Only the useful part of the group code
482  std::string groupCodeTmp;
483  // ...same as integer
484  unsigned int groupCode;
485  // Only the useful part of the group value
486  std::string groupValue;
487  // Current entity type
489  // Value of the current setting
490  char settingValue[DL_DXF_MAXLINE+1];
491  // Key of the current setting (e.g. "$ACADVER")
492  std::string settingKey;
493  // Stores the group codes
494  std::map<int, std::string> values;
495  // First call of this method. We initialize all group values in
496  // the first call.
497  bool firstCall;
498  // Attributes of the current entity (layer, color, width, line type)
500  // library version. hex: 0x20003001 = 2.0.3.1
502  // app specific dictionary handle:
503  unsigned long appDictionaryHandle;
504  // handle of standard text style, referenced by dimstyle:
505  unsigned long styleHandleStd;
506 };
507 
508 #endif
509 
510 // EOF