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.0.0"
56 
57 #define DL_VERSION_MAJOR 3
58 #define DL_VERSION_MINOR 7
59 #define DL_VERSION_REV 0
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 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 writePolyline(DL_WriterA& dw,
222  const DL_PolylineData& data,
223  const DL_Attributes& attrib);
224  void writeVertex(DL_WriterA& dw,
225  const DL_VertexData& data);
226  void writePolylineEnd(DL_WriterA& dw);
227  void writeSpline(DL_WriterA& dw,
228  const DL_SplineData& data,
229  const DL_Attributes& attrib);
230  void writeControlPoint(DL_WriterA& dw,
231  const DL_ControlPointData& data);
232  void writeFitPoint(DL_WriterA& dw,
233  const DL_FitPointData& data);
234  void writeKnot(DL_WriterA& dw,
235  const DL_KnotData& data);
236  void writeCircle(DL_WriterA& dw,
237  const DL_CircleData& data,
238  const DL_Attributes& attrib);
239  void writeArc(DL_WriterA& dw,
240  const DL_ArcData& data,
241  const DL_Attributes& attrib);
242  void writeEllipse(DL_WriterA& dw,
243  const DL_EllipseData& data,
244  const DL_Attributes& attrib);
245  void writeSolid(DL_WriterA& dw,
246  const DL_SolidData& data,
247  const DL_Attributes& attrib);
248  void writeTrace(DL_WriterA& dw,
249  const DL_TraceData& data,
250  const DL_Attributes& attrib);
251  void write3dFace(DL_WriterA& dw,
252  const DL_3dFaceData& data,
253  const DL_Attributes& attrib);
254  void writeInsert(DL_WriterA& dw,
255  const DL_InsertData& data,
256  const DL_Attributes& attrib);
257  void writeMText(DL_WriterA& dw,
258  const DL_MTextData& data,
259  const DL_Attributes& attrib);
260  void writeText(DL_WriterA& dw,
261  const DL_TextData& data,
262  const DL_Attributes& attrib);
263  void writeAttribute(DL_WriterA& dw,
264  const DL_AttributeData& data,
265  const DL_Attributes& attrib);
266  void writeDimStyleOverrides(DL_WriterA& dw,
267  const DL_DimensionData& data);
268  void writeDimAligned(DL_WriterA& dw,
269  const DL_DimensionData& data,
270  const DL_DimAlignedData& edata,
271  const DL_Attributes& attrib);
272  void writeDimLinear(DL_WriterA& dw,
273  const DL_DimensionData& data,
274  const DL_DimLinearData& edata,
275  const DL_Attributes& attrib);
276  void writeDimRadial(DL_WriterA& dw,
277  const DL_DimensionData& data,
278  const DL_DimRadialData& edata,
279  const DL_Attributes& attrib);
280  void writeDimDiametric(DL_WriterA& dw,
281  const DL_DimensionData& data,
282  const DL_DimDiametricData& edata,
283  const DL_Attributes& attrib);
284  void writeDimAngular(DL_WriterA& dw,
285  const DL_DimensionData& data,
286  const DL_DimAngularData& edata,
287  const DL_Attributes& attrib);
288  void writeDimAngular3P(DL_WriterA& dw,
289  const DL_DimensionData& data,
290  const DL_DimAngular3PData& edata,
291  const DL_Attributes& attrib);
292  void writeDimOrdinate(DL_WriterA& dw,
293  const DL_DimensionData& data,
294  const DL_DimOrdinateData& edata,
295  const DL_Attributes& attrib);
296  void writeLeader(DL_WriterA& dw,
297  const DL_LeaderData& data,
298  const DL_Attributes& attrib);
299  void writeLeaderVertex(DL_WriterA& dw,
300  const DL_LeaderVertexData& data);
301  void writeHatch1(DL_WriterA& dw,
302  const DL_HatchData& data,
303  const DL_Attributes& attrib);
304  void writeHatch2(DL_WriterA& dw,
305  const DL_HatchData& data,
306  const DL_Attributes& attrib);
307  void writeHatchLoop1(DL_WriterA& dw,
308  const DL_HatchLoopData& data);
309  void writeHatchLoop2(DL_WriterA& dw,
310  const DL_HatchLoopData& data);
311  void writeHatchEdge(DL_WriterA& dw,
312  const DL_HatchEdgeData& data);
313 
314  int writeImage(DL_WriterA& dw,
315  const DL_ImageData& data,
316  const DL_Attributes& attrib);
317 
318  void writeImageDef(DL_WriterA& dw, int handle,
319  const DL_ImageData& data);
320 
321  void writeLayer(DL_WriterA& dw,
322  const DL_LayerData& data,
323  const DL_Attributes& attrib);
324 
325  void writeLinetype(DL_WriterA& dw,
326  const DL_LinetypeData& data);
327 
328  void writeAppid(DL_WriterA& dw, const std::string& name);
329 
330  void writeBlock(DL_WriterA& dw,
331  const DL_BlockData& data);
332  void writeEndBlock(DL_WriterA& dw, const std::string& name);
333 
334  void writeVPort(DL_WriterA& dw);
335  void writeStyle(DL_WriterA& dw, const DL_StyleData& style);
336  void writeView(DL_WriterA& dw);
337  void writeUcs(DL_WriterA& dw);
338  void writeDimStyle(DL_WriterA& dw,
339  double dimasz, double dimexe, double dimexo,
340  double dimgap, double dimtxt);
341  void writeBlockRecord(DL_WriterA& dw);
342  void writeBlockRecord(DL_WriterA& dw, const std::string& name);
343  void writeObjects(DL_WriterA& dw, const std::string& appDictionaryName = "");
344  void writeAppDictionary(DL_WriterA& dw);
345  int writeDictionaryEntry(DL_WriterA& dw, const std::string& name);
346  void writeXRecord(DL_WriterA& dw, int handle, int value);
347  void writeXRecord(DL_WriterA& dw, int handle, double value);
348  void writeXRecord(DL_WriterA& dw, int handle, bool value);
349  void writeXRecord(DL_WriterA& dw, int handle, const std::string& value);
350  void writeObjectsEnd(DL_WriterA& dw);
351 
352  void writeComment(DL_WriterA& dw, const std::string& comment);
353 
358  //static double toReal(const char* value, double def=0.0);
359 
364 // static int toInt(const char* value, int def=0) {
365 // if (value!=NULL && value[0] != '\0') {
366 // return atoi(value);
367 // }
368 
369 // return def;
370 // }
371 
376 // static const char* toString(const char* value, const char* def="") {
377 // if (value!=NULL && value[0] != '\0') {
378 // return value;
379 // } else {
380 // return def;
381 // }
382 // }
383 
384  static bool checkVariable(const char* var, DL_Codes::version version);
385 
387  return version;
388  }
389 
390  int getLibVersion(const std::string &str);
391 
392  static void test();
393 
394  bool hasValue(int code) {
395  return values.count(code)==1;
396  }
397 
398  int getIntValue(int code, int def) {
399  if (!hasValue(code)) {
400  return def;
401  }
402  return toInt(values[code]);
403  }
404 
405  int toInt(const std::string& str) {
406  char* p;
407  return strtol(str.c_str(), &p, 10);
408  }
409 
410  bool toBool(const std::string& str) {
411  char* p;
412  return (bool)strtol(str.c_str(), &p, 10);
413  }
414 
415  std::string getStringValue(int code, const std::string& def) {
416  if (!hasValue(code)) {
417  return def;
418  }
419  return values[code];
420  }
421 
422  double getRealValue(int code, double def) {
423  if (!hasValue(code)) {
424  return def;
425  }
426  return toReal(values[code]);
427  }
428 
429  double toReal(const std::string& str) {
430  double ret;
431  // make sure the real value uses '.' not ',':
432  std::string str2 = str;
433  std::replace(str2.begin(), str2.end(), ',', '.');
434  // make sure c++ expects '.' not ',':
435  std::istringstream istr(str2);
436  istr.imbue(std::locale("C"));
437  istr >> ret;
438  return ret;
439  }
440 
441 private:
443 
444  std::string polylineLayer;
445  double* vertices;
448 
449  double* knots;
450  int maxKnots;
452 
453  double* weights;
455 
456  double* controlPoints;
459 
460  double* fitPoints;
463 
464  double* leaderVertices;
467 
470  std::vector<std::vector<DL_HatchEdgeData> > hatchEdges;
471 
472  std::string xRecordHandle;
474 
475  // Only the useful part of the group code
476  std::string groupCodeTmp;
477  // ...same as integer
478  unsigned int groupCode;
479  // Only the useful part of the group value
480  std::string groupValue;
481  // Current entity type
483  // Value of the current setting
484  char settingValue[DL_DXF_MAXLINE+1];
485  // Key of the current setting (e.g. "$ACADVER")
486  std::string settingKey;
487  // Stores the group codes
488  std::map<int, std::string> values;
489  // First call of this method. We initialize all group values in
490  // the first call.
491  bool firstCall;
492  // Attributes of the current entity (layer, color, width, line type)
494  // library version. hex: 0x20003001 = 2.0.3.1
496  // app specific dictionary handle:
497  unsigned long appDictionaryHandle;
498  // handle of standard text style, referenced by dimstyle:
499  unsigned long styleHandleStd;
500 };
501 
502 #endif
503 
504 // EOF