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