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