Horizon
dl_dxf.h
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 <limits>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string>
34 #include <sstream>
35 #include <map>
36 
37 #include "dl_attributes.h"
38 #include "dl_codes.h"
39 #include "dl_entities.h"
40 #include "dl_writer_ascii.h"
41 
42 #ifdef _WIN32
43 #undef M_PI
44 #define M_PI 3.14159265358979323846
45 #pragma warning(disable : 4800)
46 #endif
47 
48 #ifndef M_PI
49 #define M_PI 3.1415926535897932384626433832795
50 #endif
51 
52 #ifndef DL_NANDOUBLE
53 #define DL_NANDOUBLE std::numeric_limits<double>::quiet_NaN()
54 #endif
55 
57 class DL_WriterA;
58 
59 
60 #define DL_VERSION "3.12.2.0"
61 
62 #define DL_VERSION_MAJOR 3
63 #define DL_VERSION_MINOR 12
64 #define DL_VERSION_REV 2
65 #define DL_VERSION_BUILD 0
66 
67 #define DL_UNKNOWN 0
68 #define DL_LAYER 10
69 #define DL_BLOCK 11
70 #define DL_ENDBLK 12
71 #define DL_LINETYPE 13
72 #define DL_STYLE 20
73 #define DL_SETTING 50
74 #define DL_ENTITY_POINT 100
75 #define DL_ENTITY_LINE 101
76 #define DL_ENTITY_POLYLINE 102
77 #define DL_ENTITY_LWPOLYLINE 103
78 #define DL_ENTITY_VERTEX 104
79 #define DL_ENTITY_SPLINE 105
80 #define DL_ENTITY_KNOT 106
81 #define DL_ENTITY_CONTROLPOINT 107
82 #define DL_ENTITY_ARC 108
83 #define DL_ENTITY_CIRCLE 109
84 #define DL_ENTITY_ELLIPSE 110
85 #define DL_ENTITY_INSERT 111
86 #define DL_ENTITY_TEXT 112
87 #define DL_ENTITY_MTEXT 113
88 #define DL_ENTITY_DIMENSION 114
89 #define DL_ENTITY_LEADER 115
90 #define DL_ENTITY_HATCH 116
91 #define DL_ENTITY_ATTRIB 117
92 #define DL_ENTITY_IMAGE 118
93 #define DL_ENTITY_IMAGEDEF 119
94 #define DL_ENTITY_TRACE 120
95 #define DL_ENTITY_SOLID 121
96 #define DL_ENTITY_3DFACE 122
97 #define DL_ENTITY_XLINE 123
98 #define DL_ENTITY_RAY 124
99 #define DL_ENTITY_SEQEND 125
100 #define DL_XRECORD 200
101 #define DL_DICTIONARY 210
102 
103 
121 class DXFLIB_EXPORT DL_Dxf {
122 public:
123  DL_Dxf();
124  ~DL_Dxf();
125 
126  bool in(const std::string& file,
127  DL_CreationInterface* creationInterface);
128  bool readDxfGroups(FILE* fp,
129  DL_CreationInterface* creationInterface);
130  static bool getStrippedLine(std::string& s, unsigned int size,
131  FILE* stream, bool stripSpace = true);
132 
133  bool readDxfGroups(std::stringstream& stream,
134  DL_CreationInterface* creationInterface);
135  bool in(std::stringstream &stream,
136  DL_CreationInterface* creationInterface);
137  static bool getStrippedLine(std::string& s, unsigned int size,
138  std::stringstream& stream, bool stripSpace = true);
139 
140  static bool stripWhiteSpace(char** s, bool stripSpaces = true);
141 
142  bool processDXFGroup(DL_CreationInterface* creationInterface,
143  int groupCode, const std::string& groupValue);
144  void addSetting(DL_CreationInterface* creationInterface);
145  void addLayer(DL_CreationInterface* creationInterface);
146  void addLinetype(DL_CreationInterface *creationInterface);
147  void addBlock(DL_CreationInterface* creationInterface);
148  void endBlock(DL_CreationInterface* creationInterface);
149  void addTextStyle(DL_CreationInterface* creationInterface);
150 
151  void addPoint(DL_CreationInterface* creationInterface);
152  void addLine(DL_CreationInterface* creationInterface);
153  void addXLine(DL_CreationInterface* creationInterface);
154  void addRay(DL_CreationInterface* creationInterface);
155 
156  void addPolyline(DL_CreationInterface* creationInterface);
157  void addVertex(DL_CreationInterface* creationInterface);
158 
159  void addSpline(DL_CreationInterface* creationInterface);
160 
161  void addArc(DL_CreationInterface* creationInterface);
162  void addCircle(DL_CreationInterface* creationInterface);
163  void addEllipse(DL_CreationInterface* creationInterface);
164  void addInsert(DL_CreationInterface* creationInterface);
165 
166  void addTrace(DL_CreationInterface* creationInterface);
167  void add3dFace(DL_CreationInterface* creationInterface);
168  void addSolid(DL_CreationInterface* creationInterface);
169 
170  void addMText(DL_CreationInterface* creationInterface);
171  void addText(DL_CreationInterface* creationInterface);
172 
173  void addAttribute(DL_CreationInterface* creationInterface);
174 
175  DL_DimensionData getDimData();
176  void addDimLinear(DL_CreationInterface* creationInterface);
177  void addDimAligned(DL_CreationInterface* creationInterface);
178  void addDimRadial(DL_CreationInterface* creationInterface);
179  void addDimDiametric(DL_CreationInterface* creationInterface);
180  void addDimAngular(DL_CreationInterface* creationInterface);
181  void addDimAngular3P(DL_CreationInterface* creationInterface);
182  void addDimOrdinate(DL_CreationInterface* creationInterface);
183 
184  void addLeader(DL_CreationInterface* creationInterface);
185 
186  void addHatch(DL_CreationInterface* creationInterface);
187  void addHatchLoop();
188  void addHatchEdge();
189  bool handleHatchData(DL_CreationInterface* creationInterface);
190 
191  void addImage(DL_CreationInterface* creationInterface);
192  void addImageDef(DL_CreationInterface* creationInterface);
193 
194  void addComment(DL_CreationInterface* creationInterface, const std::string& comment);
195 
196  void addDictionary(DL_CreationInterface* creationInterface);
197  void addDictionaryEntry(DL_CreationInterface* creationInterface);
198 
199  bool handleXRecordData(DL_CreationInterface* creationInterface);
200  bool handleDictionaryData(DL_CreationInterface* creationInterface);
201 
202  bool handleXData(DL_CreationInterface *creationInterface);
203  bool handleMTextData(DL_CreationInterface* creationInterface);
204  bool handleLWPolylineData(DL_CreationInterface* creationInterface);
205  bool handleSplineData(DL_CreationInterface* creationInterface);
206  bool handleLeaderData(DL_CreationInterface* creationInterface);
207  bool handleLinetypeData(DL_CreationInterface* creationInterface);
208 
209  void endEntity(DL_CreationInterface* creationInterface);
210 
211  void endSequence(DL_CreationInterface* creationInterface);
212 
213  //int stringToInt(const char* s, bool* ok=NULL);
214 
215  DL_WriterA* out(const char* file,
216  DL_Codes::version version=DL_VERSION_2000);
217 
218  void writeHeader(DL_WriterA& dw);
219 
220  void writePoint(DL_WriterA& dw,
221  const DL_PointData& data,
222  const DL_Attributes& attrib);
223  void writeLine(DL_WriterA& dw,
224  const DL_LineData& data,
225  const DL_Attributes& attrib);
226  void writeXLine(DL_WriterA& dw,
227  const DL_XLineData& data,
228  const DL_Attributes& attrib);
229  void writeRay(DL_WriterA& dw,
230  const DL_RayData& data,
231  const DL_Attributes& attrib);
232  void writePolyline(DL_WriterA& dw,
233  const DL_PolylineData& data,
234  const DL_Attributes& attrib);
235  void writeVertex(DL_WriterA& dw,
236  const DL_VertexData& data);
237  void writePolylineEnd(DL_WriterA& dw);
238  void writeSpline(DL_WriterA& dw,
239  const DL_SplineData& data,
240  const DL_Attributes& attrib);
241  void writeControlPoint(DL_WriterA& dw,
242  const DL_ControlPointData& data);
243  void writeFitPoint(DL_WriterA& dw,
244  const DL_FitPointData& data);
245  void writeKnot(DL_WriterA& dw,
246  const DL_KnotData& data);
247  void writeCircle(DL_WriterA& dw,
248  const DL_CircleData& data,
249  const DL_Attributes& attrib);
250  void writeArc(DL_WriterA& dw,
251  const DL_ArcData& data,
252  const DL_Attributes& attrib);
253  void writeEllipse(DL_WriterA& dw,
254  const DL_EllipseData& data,
255  const DL_Attributes& attrib);
256  void writeSolid(DL_WriterA& dw,
257  const DL_SolidData& data,
258  const DL_Attributes& attrib);
259  void writeTrace(DL_WriterA& dw,
260  const DL_TraceData& data,
261  const DL_Attributes& attrib);
262  void write3dFace(DL_WriterA& dw,
263  const DL_3dFaceData& data,
264  const DL_Attributes& attrib);
265  void writeInsert(DL_WriterA& dw,
266  const DL_InsertData& data,
267  const DL_Attributes& attrib);
268  void writeMText(DL_WriterA& dw,
269  const DL_MTextData& data,
270  const DL_Attributes& attrib);
271  void writeText(DL_WriterA& dw,
272  const DL_TextData& data,
273  const DL_Attributes& attrib);
274  void writeAttribute(DL_WriterA& dw,
275  const DL_AttributeData& data,
276  const DL_Attributes& attrib);
277  void writeDimStyleOverrides(DL_WriterA& dw,
278  const DL_DimensionData& data);
279  void writeDimAligned(DL_WriterA& dw,
280  const DL_DimensionData& data,
281  const DL_DimAlignedData& edata,
282  const DL_Attributes& attrib);
283  void writeDimLinear(DL_WriterA& dw,
284  const DL_DimensionData& data,
285  const DL_DimLinearData& edata,
286  const DL_Attributes& attrib);
287  void writeDimRadial(DL_WriterA& dw,
288  const DL_DimensionData& data,
289  const DL_DimRadialData& edata,
290  const DL_Attributes& attrib);
291  void writeDimDiametric(DL_WriterA& dw,
292  const DL_DimensionData& data,
293  const DL_DimDiametricData& edata,
294  const DL_Attributes& attrib);
295  void writeDimAngular(DL_WriterA& dw,
296  const DL_DimensionData& data,
297  const DL_DimAngularData& edata,
298  const DL_Attributes& attrib);
299  void writeDimAngular3P(DL_WriterA& dw,
300  const DL_DimensionData& data,
301  const DL_DimAngular3PData& edata,
302  const DL_Attributes& attrib);
303  void writeDimOrdinate(DL_WriterA& dw,
304  const DL_DimensionData& data,
305  const DL_DimOrdinateData& edata,
306  const DL_Attributes& attrib);
307  void writeLeader(DL_WriterA& dw,
308  const DL_LeaderData& data,
309  const DL_Attributes& attrib);
310  void writeLeaderVertex(DL_WriterA& dw,
311  const DL_LeaderVertexData& data);
312  void writeHatch1(DL_WriterA& dw,
313  const DL_HatchData& data,
314  const DL_Attributes& attrib);
315  void writeHatch2(DL_WriterA& dw,
316  const DL_HatchData& data,
317  const DL_Attributes& attrib);
318  void writeHatchLoop1(DL_WriterA& dw,
319  const DL_HatchLoopData& data);
320  void writeHatchLoop2(DL_WriterA& dw,
321  const DL_HatchLoopData& data);
322  void writeHatchEdge(DL_WriterA& dw,
323  const DL_HatchEdgeData& data);
324 
325  int writeImage(DL_WriterA& dw,
326  const DL_ImageData& data,
327  const DL_Attributes& attrib);
328 
329  void writeImageDef(DL_WriterA& dw, int handle,
330  const DL_ImageData& data);
331 
332  void writeLayer(DL_WriterA& dw,
333  const DL_LayerData& data,
334  const DL_Attributes& attrib);
335 
336  void writeLinetype(DL_WriterA& dw,
337  const DL_LinetypeData& data);
338 
339  void writeAppid(DL_WriterA& dw, const std::string& name);
340 
341  void writeBlock(DL_WriterA& dw,
342  const DL_BlockData& data);
343  void writeEndBlock(DL_WriterA& dw, const std::string& name);
344 
345  void writeVPort(DL_WriterA& dw);
346  void writeStyle(DL_WriterA& dw, const DL_StyleData& style);
347  void writeView(DL_WriterA& dw);
348  void writeUcs(DL_WriterA& dw);
349  void writeDimStyle(DL_WriterA& dw,
350  double dimasz, double dimexe, double dimexo,
351  double dimgap, double dimtxt);
352  void writeBlockRecord(DL_WriterA& dw);
353  void writeBlockRecord(DL_WriterA& dw, const std::string& name);
354  void writeObjects(DL_WriterA& dw, const std::string& appDictionaryName = "");
355  void writeAppDictionary(DL_WriterA& dw);
356  int writeDictionaryEntry(DL_WriterA& dw, const std::string& name);
357  void writeXRecord(DL_WriterA& dw, int handle, int value);
358  void writeXRecord(DL_WriterA& dw, int handle, double value);
359  void writeXRecord(DL_WriterA& dw, int handle, bool value);
360  void writeXRecord(DL_WriterA& dw, int handle, const std::string& value);
361  void writeObjectsEnd(DL_WriterA& dw);
362 
363  void writeComment(DL_WriterA& dw, const std::string& comment);
364 
369  //static double toReal(const char* value, double def=0.0);
370 
375 // static int toInt(const char* value, int def=0) {
376 // if (value!=NULL && value[0] != '\0') {
377 // return atoi(value);
378 // }
379 
380 // return def;
381 // }
382 
387 // static const char* toString(const char* value, const char* def="") {
388 // if (value!=NULL && value[0] != '\0') {
389 // return value;
390 // } else {
391 // return def;
392 // }
393 // }
394 
395  static bool checkVariable(const char* var, DL_Codes::version version);
396 
397  DL_Codes::version getVersion() {
398  return version;
399  }
400 
401  int getLibVersion(const std::string &str);
402 
403  static void test();
404 
405  bool hasValue(int code) {
406  return values.count(code)==1;
407  }
408 
409  int getIntValue(int code, int def) {
410  if (!hasValue(code)) {
411  return def;
412  }
413  return toInt(values[code]);
414  }
415 
416  int toInt(const std::string& str) {
417  char* p;
418  return strtol(str.c_str(), &p, 10);
419  }
420 
421  bool toBool(const std::string& str) {
422  char* p;
423  return (bool)strtol(str.c_str(), &p, 10);
424  }
425 
426  std::string getStringValue(int code, const std::string& def) {
427  if (!hasValue(code)) {
428  return def;
429  }
430  return values[code];
431  }
432 
433  double getRealValue(int code, double def) {
434  if (!hasValue(code)) {
435  return def;
436  }
437  return toReal(values[code]);
438  }
439 
440  double toReal(const std::string& str) {
441  double ret;
442  // make sure the real value uses '.' not ',':
443  std::string str2 = str;
444  std::replace(str2.begin(), str2.end(), ',', '.');
445  // make sure c++ expects '.' not ',':
446  std::istringstream istr(str2);
447  istr.imbue(std::locale("C"));
448  istr >> ret;
449  return ret;
450  }
451 
452 private:
453  DL_Codes::version version;
454 
455  std::string polylineLayer;
456  double* vertices;
457  int maxVertices;
458  int vertexIndex;
459 
460  double* knots;
461  int maxKnots;
462  int knotIndex;
463 
464  double* weights;
465  int weightIndex;
466 
467  double* controlPoints;
468  int maxControlPoints;
469  int controlPointIndex;
470 
471  double* fitPoints;
472  int maxFitPoints;
473  int fitPointIndex;
474 
475  double* leaderVertices;
476  int maxLeaderVertices;
477  int leaderVertexIndex;
478 
479  bool firstHatchLoop;
480  DL_HatchEdgeData hatchEdge;
481  std::vector<std::vector<DL_HatchEdgeData> > hatchEdges;
482 
483  std::string xRecordHandle;
484  bool xRecordValues;
485 
486  // Only the useful part of the group code
487  std::string groupCodeTmp;
488  // ...same as integer
489  unsigned int groupCode;
490  // Only the useful part of the group value
491  std::string groupValue;
492  // Current entity type
493  int currentObjectType;
494  // Value of the current setting
495  char settingValue[DL_DXF_MAXLINE+1];
496  // Key of the current setting (e.g. "$ACADVER")
497  std::string settingKey;
498  // Stores the group codes
499  std::map<int, std::string> values;
500  // First call of this method. We initialize all group values in
501  // the first call.
502  bool firstCall;
503  // Attributes of the current entity (layer, color, width, line type)
504  DL_Attributes attrib;
505  // library version. hex: 0x20003001 = 2.0.3.1
506  int libVersion;
507  // app specific dictionary handle:
508  unsigned long appDictionaryHandle;
509  // handle of standard text style, referenced by dimstyle:
510  unsigned long styleHandleStd;
511 };
512 
513 #endif
514 
515 // EOF
DL_WriterA
Implements functions defined in DL_Writer for writing low level DXF constructs to an ASCII format DXF...
Definition: dl_writer_ascii.h:49
DL_FitPointData
Spline fit point data.
Definition: dl_entities.h:597
DL_LineData
Line Data.
Definition: dl_entities.h:220
DL_HatchData
Hatch data.
Definition: dl_entities.h:1380
DL_VertexData
Vertex Data.
Definition: dl_entities.h:411
DL_PolylineData
Polyline Data.
Definition: dl_entities.h:381
DL_DimRadialData
Radial Dimension Data.
Definition: dl_entities.h:1099
DL_CircleData
Circle Data.
Definition: dl_entities.h:351
DL_EllipseData
Ellipse Data.
Definition: dl_entities.h:617
DL_LeaderData
Leader (arrow).
Definition: dl_entities.h:1309
DL_StyleData
Text style data.
Definition: dl_entities.h:129
DL_Dxf
Reading and writing of DXF files.
Definition: dl_dxf.h:121
DL_HatchLoopData
Hatch boundary path (loop) data.
Definition: dl_entities.h:1427
DL_LeaderVertexData
Leader Vertex Data.
Definition: dl_entities.h:1356
DL_SplineData
Spline Data.
Definition: dl_entities.h:506
DL_DimAngular3PData
Angular Dimension Data (3 points version).
Definition: dl_entities.h:1217
DL_MTextData
MText Data.
Definition: dl_entities.h:714
DL_LinetypeData
Line Type Data.
Definition: dl_entities.h:89
DL_ArcData
Arc Data.
Definition: dl_entities.h:314
DL_DimLinearData
Linear (rotated) Dimension Data.
Definition: dl_entities.h:1053
DL_TextData
Text Data.
Definition: dl_entities.h:795
DL_ImageData
Image Data.
Definition: dl_entities.h:1619
DL_BlockData
Block Data.
Definition: dl_entities.h:58
DL_DimensionData
Generic Dimension Data.
Definition: dl_entities.h:906
DL_Attributes
Storing and passing around attributes.
Definition: dl_attributes.h:41
DL_KnotData
Spline knot data.
Definition: dl_entities.h:551
DL_CreationInterface
Abstract class (interface) for the creation of new entities.
Definition: dl_creationinterface.h:47
DL_PointData
Point Data.
Definition: dl_entities.h:196
DL_DimOrdinateData
Ordinate Dimension Data.
Definition: dl_entities.h:1266
DL_AttributeData
Block attribute data.
Definition: dl_entities.h:866
DL_ControlPointData
Spline control point data.
Definition: dl_entities.h:570
DL_HatchEdgeData
Hatch edge data.
Definition: dl_entities.h:1449
DL_DimAngularData
Angular Dimension Data.
Definition: dl_entities.h:1157
DL_DimAlignedData
Aligned Dimension Data.
Definition: dl_entities.h:1016
DL_LayerData
Layer Data.
Definition: dl_entities.h:36
DL_RayData
Ray Data.
Definition: dl_entities.h:283
DL_InsertData
Insert Data.
Definition: dl_entities.h:664
DL_Codes::version
version
Version numbers for the DXF Format.
Definition: dl_codes.h:96
DL_XLineData
XLine Data.
Definition: dl_entities.h:254
DL_TraceData
Trace Data / solid data / 3d face data.
Definition: dl_entities.h:439
DL_Dxf::checkVariable
static bool checkVariable(const char *var, DL_Codes::version version)
Converts the given string into a double or returns the given default valud (def) if value is NULL or ...
DL_DimDiametricData
Diametric Dimension Data.
Definition: dl_entities.h:1128