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.3.4.0"
56 
57 #define DL_VERSION_MAJOR 3
58 #define DL_VERSION_MINOR 3
59 #define DL_VERSION_REV 4
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);
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);
134 
135  static bool stripWhiteSpace(char** s);
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 addBlock(DL_CreationInterface* creationInterface);
142  void endBlock(DL_CreationInterface* creationInterface);
143  void addTextStyle(DL_CreationInterface* creationInterface);
144 
145  void addPoint(DL_CreationInterface* creationInterface);
146  void addLine(DL_CreationInterface* creationInterface);
147  void addXLine(DL_CreationInterface* creationInterface);
148  void addRay(DL_CreationInterface* creationInterface);
149 
150  void addPolyline(DL_CreationInterface* creationInterface);
151  void addVertex(DL_CreationInterface* creationInterface);
152 
153  void addSpline(DL_CreationInterface* creationInterface);
154 
155  void addArc(DL_CreationInterface* creationInterface);
156  void addCircle(DL_CreationInterface* creationInterface);
157  void addEllipse(DL_CreationInterface* creationInterface);
158  void addInsert(DL_CreationInterface* creationInterface);
159 
160  void addTrace(DL_CreationInterface* creationInterface);
161  void add3dFace(DL_CreationInterface* creationInterface);
162  void addSolid(DL_CreationInterface* creationInterface);
163 
164  void addMText(DL_CreationInterface* creationInterface);
165  void addText(DL_CreationInterface* creationInterface);
166 
167  void addAttrib(DL_CreationInterface* creationInterface);
168 
169  DL_DimensionData getDimData();
170  void addDimLinear(DL_CreationInterface* creationInterface);
171  void addDimAligned(DL_CreationInterface* creationInterface);
172  void addDimRadial(DL_CreationInterface* creationInterface);
173  void addDimDiametric(DL_CreationInterface* creationInterface);
174  void addDimAngular(DL_CreationInterface* creationInterface);
175  void addDimAngular3P(DL_CreationInterface* creationInterface);
176  void addDimOrdinate(DL_CreationInterface* creationInterface);
177 
178  void addLeader(DL_CreationInterface* creationInterface);
179 
180  void addHatch(DL_CreationInterface* creationInterface);
181  void addHatchLoop();
182  void addHatchEdge();
183  bool handleHatchData(DL_CreationInterface* creationInterface);
184 
185  void addImage(DL_CreationInterface* creationInterface);
186  void addImageDef(DL_CreationInterface* creationInterface);
187 
188  void addComment(DL_CreationInterface* creationInterface, const std::string& comment);
189 
190  void addDictionary(DL_CreationInterface* creationInterface);
191  void addDictionaryEntry(DL_CreationInterface* creationInterface);
192 
193  bool handleXRecordData(DL_CreationInterface* creationInterface);
194  bool handleDictionaryData(DL_CreationInterface* creationInterface);
195 
196  bool handleXData(DL_CreationInterface *creationInterface);
197  bool handleMTextData(DL_CreationInterface* creationInterface);
198  bool handleLWPolylineData(DL_CreationInterface* creationInterface);
199  bool handleSplineData(DL_CreationInterface* creationInterface);
200  bool handleLeaderData(DL_CreationInterface* creationInterface);
201 
202  void endEntity(DL_CreationInterface* creationInterface);
203 
204  void endSequence(DL_CreationInterface* creationInterface);
205 
206  //int stringToInt(const char* s, bool* ok=NULL);
207 
208  DL_WriterA* out(const char* file,
210 
211  void writeHeader(DL_WriterA& dw);
212 
213  void writePoint(DL_WriterA& dw,
214  const DL_PointData& data,
215  const DL_Attributes& attrib);
216  void writeLine(DL_WriterA& dw,
217  const DL_LineData& data,
218  const DL_Attributes& attrib);
219  void writePolyline(DL_WriterA& dw,
220  const DL_PolylineData& data,
221  const DL_Attributes& attrib);
222  void writeVertex(DL_WriterA& dw,
223  const DL_VertexData& data);
224  void writePolylineEnd(DL_WriterA& dw);
225  void writeSpline(DL_WriterA& dw,
226  const DL_SplineData& data,
227  const DL_Attributes& attrib);
228  void writeControlPoint(DL_WriterA& dw,
229  const DL_ControlPointData& data);
230  void writeFitPoint(DL_WriterA& dw,
231  const DL_FitPointData& data);
232  void writeKnot(DL_WriterA& dw,
233  const DL_KnotData& data);
234  void writeCircle(DL_WriterA& dw,
235  const DL_CircleData& data,
236  const DL_Attributes& attrib);
237  void writeArc(DL_WriterA& dw,
238  const DL_ArcData& data,
239  const DL_Attributes& attrib);
240  void writeEllipse(DL_WriterA& dw,
241  const DL_EllipseData& data,
242  const DL_Attributes& attrib);
243  void writeSolid(DL_WriterA& dw,
244  const DL_SolidData& data,
245  const DL_Attributes& attrib);
246  void writeTrace(DL_WriterA& dw,
247  const DL_TraceData& data,
248  const DL_Attributes& attrib);
249  void write3dFace(DL_WriterA& dw,
250  const DL_3dFaceData& data,
251  const DL_Attributes& attrib);
252  void writeInsert(DL_WriterA& dw,
253  const DL_InsertData& data,
254  const DL_Attributes& attrib);
255  void writeMText(DL_WriterA& dw,
256  const DL_MTextData& data,
257  const DL_Attributes& attrib);
258  void writeText(DL_WriterA& dw,
259  const DL_TextData& data,
260  const DL_Attributes& attrib);
261  void writeDimStyleOverrides(DL_WriterA& dw,
262  const DL_DimensionData& data);
263  void writeDimAligned(DL_WriterA& dw,
264  const DL_DimensionData& data,
265  const DL_DimAlignedData& edata,
266  const DL_Attributes& attrib);
267  void writeDimLinear(DL_WriterA& dw,
268  const DL_DimensionData& data,
269  const DL_DimLinearData& edata,
270  const DL_Attributes& attrib);
271  void writeDimRadial(DL_WriterA& dw,
272  const DL_DimensionData& data,
273  const DL_DimRadialData& edata,
274  const DL_Attributes& attrib);
275  void writeDimDiametric(DL_WriterA& dw,
276  const DL_DimensionData& data,
277  const DL_DimDiametricData& edata,
278  const DL_Attributes& attrib);
279  void writeDimAngular(DL_WriterA& dw,
280  const DL_DimensionData& data,
281  const DL_DimAngularData& edata,
282  const DL_Attributes& attrib);
283  void writeDimAngular3P(DL_WriterA& dw,
284  const DL_DimensionData& data,
285  const DL_DimAngular3PData& edata,
286  const DL_Attributes& attrib);
287  void writeDimOrdinate(DL_WriterA& dw,
288  const DL_DimensionData& data,
289  const DL_DimOrdinateData& edata,
290  const DL_Attributes& attrib);
291  void writeLeader(DL_WriterA& dw,
292  const DL_LeaderData& data,
293  const DL_Attributes& attrib);
294  void writeLeaderVertex(DL_WriterA& dw,
295  const DL_LeaderVertexData& data);
296  void writeHatch1(DL_WriterA& dw,
297  const DL_HatchData& data,
298  const DL_Attributes& attrib);
299  void writeHatch2(DL_WriterA& dw,
300  const DL_HatchData& data,
301  const DL_Attributes& attrib);
302  void writeHatchLoop1(DL_WriterA& dw,
303  const DL_HatchLoopData& data);
304  void writeHatchLoop2(DL_WriterA& dw,
305  const DL_HatchLoopData& data);
306  void writeHatchEdge(DL_WriterA& dw,
307  const DL_HatchEdgeData& data);
308 
309  int writeImage(DL_WriterA& dw,
310  const DL_ImageData& data,
311  const DL_Attributes& attrib);
312 
313  void writeImageDef(DL_WriterA& dw, int handle,
314  const DL_ImageData& data);
315 
316  void writeLayer(DL_WriterA& dw,
317  const DL_LayerData& data,
318  const DL_Attributes& attrib);
319 
320  void writeLineType(DL_WriterA& dw,
321  const DL_LineTypeData& data);
322 
323  void writeAppid(DL_WriterA& dw, const std::string& name);
324 
325  void writeBlock(DL_WriterA& dw,
326  const DL_BlockData& data);
327  void writeEndBlock(DL_WriterA& dw, const std::string& name);
328 
329  void writeVPort(DL_WriterA& dw);
330  void writeStyle(DL_WriterA& dw, const DL_StyleData& style);
331  void writeView(DL_WriterA& dw);
332  void writeUcs(DL_WriterA& dw);
333  void writeDimStyle(DL_WriterA& dw,
334  double dimasz, double dimexe, double dimexo,
335  double dimgap, double dimtxt);
336  void writeBlockRecord(DL_WriterA& dw);
337  void writeBlockRecord(DL_WriterA& dw, const std::string& name);
338  void writeObjects(DL_WriterA& dw, const std::string& appDictionaryName = "");
339  void writeAppDictionary(DL_WriterA& dw);
340  int writeDictionaryEntry(DL_WriterA& dw, const std::string& name);
341  void writeXRecord(DL_WriterA& dw, int handle, int value);
342  void writeXRecord(DL_WriterA& dw, int handle, double value);
343  void writeXRecord(DL_WriterA& dw, int handle, bool value);
344  void writeXRecord(DL_WriterA& dw, int handle, const std::string& value);
345  void writeObjectsEnd(DL_WriterA& dw);
346 
347  void writeComment(DL_WriterA& dw, const std::string& comment);
348 
353  //static double toReal(const char* value, double def=0.0);
354 
359 // static int toInt(const char* value, int def=0) {
360 // if (value!=NULL && value[0] != '\0') {
361 // return atoi(value);
362 // }
363 
364 // return def;
365 // }
366 
371 // static const char* toString(const char* value, const char* def="") {
372 // if (value!=NULL && value[0] != '\0') {
373 // return value;
374 // } else {
375 // return def;
376 // }
377 // }
378 
379  static bool checkVariable(const char* var, DL_Codes::version version);
380 
382  return version;
383  }
384 
385  int getLibVersion(const std::string &str);
386 
387  static void test();
388 
389  bool hasValue(int code) {
390  return values.count(code)==1;
391  }
392 
393  int getIntValue(int code, int def) {
394  if (!hasValue(code)) {
395  return def;
396  }
397  return toInt(values[code]);
398  }
399 
400  int toInt(const std::string& str) {
401  char* p;
402  return strtol(str.c_str(), &p, 10);
403  }
404 
405  bool toBool(const std::string& str) {
406  char* p;
407  return (bool)strtol(str.c_str(), &p, 10);
408  }
409 
410  std::string getStringValue(int code, const std::string& def) {
411  if (!hasValue(code)) {
412  return def;
413  }
414  return values[code];
415  }
416 
417  double getRealValue(int code, double def) {
418  if (!hasValue(code)) {
419  return def;
420  }
421  return toReal(values[code]);
422  }
423 
424  double toReal(const std::string& str) {
425  double ret;
426  // make sure the real value uses '.' not ',':
427  std::string str2 = str;
428  std::replace(str2.begin(), str2.end(), ',', '.');
429  // make sure c++ expects '.' not ',':
430  std::istringstream istr(str2);
431  istr.imbue(std::locale("C"));
432  istr >> ret;
433  return ret;
434  }
435 
436 private:
438 
439  std::string polylineLayer;
440  double* vertices;
443 
444  double* knots;
445  int maxKnots;
447 
448  double* weights;
450 
451  double* controlPoints;
454 
455  double* fitPoints;
458 
459  double* leaderVertices;
462 
465  std::vector<std::vector<DL_HatchEdgeData> > hatchEdges;
466 
467  std::string xRecordHandle;
469 
470  // Only the useful part of the group code
471  std::string groupCodeTmp;
472  // ...same as integer
473  unsigned int groupCode;
474  // Only the useful part of the group value
475  std::string groupValue;
476  // Current entity type
478  // Value of the current setting
479  char settingValue[DL_DXF_MAXLINE+1];
480  // Key of the current setting (e.g. "$ACADVER")
481  std::string settingKey;
482  // Stores the group codes
483  std::map<int, std::string> values;
484  // First call of this method. We initialize all group values in
485  // the first call.
486  bool firstCall;
487  // Attributes of the current entity (layer, color, width, line type)
489  // library version. hex: 0x20003001 = 2.0.3.1
491  // app specific dictionary handle:
492  unsigned long appDictionaryHandle;
493  // handle of standard text style, referenced by dimstyle:
494  unsigned long styleHandleStd;
495 };
496 
497 #endif
498 
499 // EOF