Horizon
dl_writer.h
1 /****************************************************************************
2 ** Copyright (C) 2001-2013 RibbonSoft, GmbH. All rights reserved.
3 ** Copyright (C) 2001 Robert J. Campbell Jr.
4 **
5 ** This file is part of the dxflib project.
6 **
7 ** This file is free software; you can redistribute it and/or modify
8 ** it under the terms of the GNU General Public License as published by
9 ** the Free Software Foundation; either version 2 of the License, or
10 ** (at your option) any later version.
11 **
12 ** Licensees holding valid dxflib Professional Edition licenses may use
13 ** this file in accordance with the dxflib Commercial License
14 ** Agreement provided with the Software.
15 **
16 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
17 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18 **
19 ** See http://www.ribbonsoft.com for further details.
20 **
21 ** Contact info@ribbonsoft.com if any conditions of this licensing are
22 ** not clear to you.
23 **
24 **********************************************************************/
25 
26 #ifndef DL_WRITER_H
27 #define DL_WRITER_H
28 
29 #include "dl_global.h"
30 
31 #ifndef _WIN32
32 #include <strings.h>
33 #endif
34 
35 #if _MSC_VER > 1000
36 #pragma once
37 #endif // _MSC_VER > 1000
38 
39 #include <cstring>
40 #include <iostream>
41 #include <algorithm>
42 
43 #include "dl_attributes.h"
44 #include "dl_codes.h"
45 
46 
47 
58 class DXFLIB_EXPORT DL_Writer {
59 public:
63  DL_Writer(DL_Codes::version version) : m_handle(0x30) {
64  this->version = version;
65  modelSpaceHandle = 0;
66  paperSpaceHandle = 0;
67  paperSpace0Handle = 0;
68  }
69 
70  virtual ~DL_Writer() {}
71  ;
72 
82  void section(const char* name) const {
83  dxfString(0, "SECTION");
84  dxfString(2, name);
85  }
86 
97  void sectionHeader() const {
98  section("HEADER");
99  }
100 
111  void sectionTables() const {
112  section("TABLES");
113  }
114 
125  void sectionBlocks() const {
126  section("BLOCKS");
127  }
128 
139  void sectionEntities() const {
140  section("ENTITIES");
141  }
142 
153  void sectionClasses() const {
154  section("CLASSES");
155  }
156 
167  void sectionObjects() const {
168  section("OBJECTS");
169  }
170 
179  void sectionEnd() const {
180  dxfString(0, "ENDSEC");
181  }
182 
195  void table(const char* name, int num, int h=0) const {
196  dxfString(0, "TABLE");
197  dxfString(2, name);
198  if (version>=DL_VERSION_2000) {
199  if (h==0) {
200  handle();
201  }
202  else {
203  dxfHex(5, h);
204  }
205  dxfString(100, "AcDbSymbolTable");
206  }
207  dxfInt(70, num);
208  }
209 
223  void tableLayers(int num) const {
224  table("LAYER", num, 2);
225  }
226 
240  void tableLinetypes(int num) const {
241  //linetypeHandle = 5;
242  table("LTYPE", num, 5);
243  }
244 
258  void tableAppid(int num) const {
259  table("APPID", num, 9);
260  }
261 
275  void tableStyle(int num) const {
276  table("STYLE", num, 3);
277  }
278 
287  void tableEnd() const {
288  dxfString(0, "ENDTAB");
289  }
290 
299  void dxfEOF() const {
300  dxfString(0, "EOF");
301  }
302 
311  void comment(const char* text) const {
312  dxfString(999, text);
313  }
314 
325  void entity(const char* entTypeName) const {
326  dxfString(0, entTypeName);
327  if (version>=DL_VERSION_2000) {
328  handle();
329  }
330  }
331 
346  void entityAttributes(const DL_Attributes& attrib) const {
347 
348  // layer name:
349  dxfString(8, attrib.getLayer());
350 
351  // R12 doesn't accept BYLAYER values. The value has to be missing
352  // in that case.
353  if (version>=DL_VERSION_2000 || attrib.getColor()!=256) {
354  dxfInt(62, attrib.getColor());
355  }
356  if (version>=DL_VERSION_2000 && attrib.getColor24()!=-1) {
357  dxfInt(420, attrib.getColor24());
358  }
359  if (version>=DL_VERSION_2000) {
360  dxfInt(370, attrib.getWidth());
361  }
362  if (version>=DL_VERSION_2000) {
363  dxfReal(48, attrib.getLinetypeScale());
364  }
365  std::string linetype = attrib.getLinetype();
366  std::transform(linetype.begin(), linetype.end(), linetype.begin(), ::toupper);
367  if (version>=DL_VERSION_2000 || linetype=="BYLAYER") {
368  dxfString(6, attrib.getLinetype());
369  }
370  }
371 
375  void subClass(const char* sub) const {
376  dxfString(100, sub);
377  }
378 
387  void tableLayerEntry(unsigned long int h=0) const {
388  dxfString(0, "LAYER");
389  if (version>=DL_VERSION_2000) {
390  if (h==0) {
391  handle();
392  } else {
393  dxfHex(5, h);
394  }
395  dxfString(100, "AcDbSymbolTableRecord");
396  dxfString(100, "AcDbLayerTableRecord");
397  }
398  }
399 
408  void tableLinetypeEntry(unsigned long int h=0) const {
409  dxfString(0, "LTYPE");
410  if (version>=DL_VERSION_2000) {
411  if (h==0) {
412  handle();
413  } else {
414  dxfHex(5, h);
415  }
416  //dxfHex(330, 0x5);
417  dxfString(100, "AcDbSymbolTableRecord");
418  dxfString(100, "AcDbLinetypeTableRecord");
419  }
420  }
421 
430  void tableAppidEntry(unsigned long int h=0) const {
431  dxfString(0, "APPID");
432  if (version>=DL_VERSION_2000) {
433  if (h==0) {
434  handle();
435  } else {
436  dxfHex(5, h);
437  }
438  //dxfHex(330, 0x9);
439  dxfString(100, "AcDbSymbolTableRecord");
440  dxfString(100, "AcDbRegAppTableRecord");
441  }
442  }
443 
452  void sectionBlockEntry(unsigned long int h=0) const {
453  dxfString(0, "BLOCK");
454  if (version>=DL_VERSION_2000) {
455  if (h==0) {
456  handle();
457  } else {
458  dxfHex(5, h);
459  }
460  //dxfHex(330, blockHandle);
461  dxfString(100, "AcDbEntity");
462  if (h==0x1C) {
463  dxfInt(67, 1);
464  }
465  dxfString(8, "0"); // TODO: Layer for block
466  dxfString(100, "AcDbBlockBegin");
467  }
468  }
469 
478  void sectionBlockEntryEnd(unsigned long int h=0) const {
479  dxfString(0, "ENDBLK");
480  if (version>=DL_VERSION_2000) {
481  if (h==0) {
482  handle();
483  } else {
484  dxfHex(5, h);
485  }
486  //dxfHex(330, blockHandle);
487  dxfString(100, "AcDbEntity");
488  if (h==0x1D) {
489  dxfInt(67, 1);
490  }
491  dxfString(8, "0"); // TODO: Layer for block
492  dxfString(100, "AcDbBlockEnd");
493  }
494  }
495 
496  void color(int col=256) const {
497  dxfInt(62, col);
498  }
499  void linetype(const char *lt) const {
500  dxfString(6, lt);
501  }
502  void linetypeScale(double scale) const {
503  dxfReal(48, scale);
504  }
505  void lineWeight(int lw) const {
506  dxfInt(370, lw);
507  }
508 
509  void coord(int gc, double x, double y, double z=0) const {
510  dxfReal(gc, x);
511  dxfReal(gc+10, y);
512  dxfReal(gc+20, z);
513  }
514 
515  void coordTriplet(int gc, const double* value) const {
516  if (value) {
517  dxfReal(gc, *value++);
518  dxfReal(gc+10, *value++);
519  dxfReal(gc+20, *value++);
520  }
521  }
522 
523  void resetHandle() const {
524  m_handle = 1;
525  }
526 
530  unsigned long handle(int gc=5) const {
531  // handle has to be hex
532  dxfHex(gc, m_handle);
533  return m_handle++;
534  }
535 
539  unsigned long getNextHandle() const {
540  return m_handle;
541  }
542 
550  virtual void dxfReal(int gc, double value) const = 0;
551 
559  virtual void dxfInt(int gc, int value) const = 0;
560 
568  virtual void dxfBool(int gc, bool value) const {
569  dxfInt(gc, (int)value);
570  }
571 
579  virtual void dxfHex(int gc, int value) const = 0;
580 
588  virtual void dxfString(int gc, const char* value) const = 0;
589 
597  virtual void dxfString(int gc, const std::string& value) const = 0;
598 
599 protected:
600  mutable unsigned long m_handle;
601  mutable unsigned long modelSpaceHandle;
602  mutable unsigned long paperSpaceHandle;
603  mutable unsigned long paperSpace0Handle;
604 
609 private:
610 };
611 
612 #endif
Storing and passing around attributes.
Definition: dl_attributes.h:41
std::string getLinetype() const
Definition: dl_attributes.h:198
int getColor() const
Definition: dl_attributes.h:149
int getWidth() const
Definition: dl_attributes.h:172
std::string getLayer() const
Definition: dl_attributes.h:122
int getColor24() const
Definition: dl_attributes.h:158
version
Version numbers for the DXF Format.
Definition: dl_codes.h:96
Defines interface for writing low level DXF constructs to a file.
Definition: dl_writer.h:58
virtual void dxfInt(int gc, int value) const =0
Must be overwritten by the implementing class to write an int value to the file.
virtual void dxfString(int gc, const char *value) const =0
Must be overwritten by the implementing class to write a string to the file.
void dxfEOF() const
End of the DXF file.
Definition: dl_writer.h:299
void sectionTables() const
Section TABLES.
Definition: dl_writer.h:111
void tableEnd() const
End of a table.
Definition: dl_writer.h:287
void sectionClasses() const
Section CLASSES.
Definition: dl_writer.h:153
virtual void dxfReal(int gc, double value) const =0
Must be overwritten by the implementing class to write a real value to the file.
void tableLinetypeEntry(unsigned long int h=0) const
Line type (must be in the TABLES section LTYPE).
Definition: dl_writer.h:408
void sectionObjects() const
Section OBJECTS.
Definition: dl_writer.h:167
unsigned long getNextHandle() const
Definition: dl_writer.h:539
void section(const char *name) const
Generic section for section 'name'.
Definition: dl_writer.h:82
virtual void dxfString(int gc, const std::string &value) const =0
Must be overwritten by the implementing class to write a string to the file.
void comment(const char *text) const
Comment.
Definition: dl_writer.h:311
void tableLinetypes(int num) const
Table for line types.
Definition: dl_writer.h:240
void tableStyle(int num) const
Table for text style.
Definition: dl_writer.h:275
void entity(const char *entTypeName) const
Entity.
Definition: dl_writer.h:325
void tableLayers(int num) const
Table for layers.
Definition: dl_writer.h:223
void sectionBlockEntry(unsigned long int h=0) const
Block (must be in the section BLOCKS).
Definition: dl_writer.h:452
void sectionEnd() const
End of a section.
Definition: dl_writer.h:179
void sectionBlockEntryEnd(unsigned long int h=0) const
End of Block (must be in the section BLOCKS).
Definition: dl_writer.h:478
void tableLayerEntry(unsigned long int h=0) const
Layer (must be in the TABLES section LAYER).
Definition: dl_writer.h:387
void subClass(const char *sub) const
Subclass.
Definition: dl_writer.h:375
unsigned long handle(int gc=5) const
Writes a unique handle and returns it.
Definition: dl_writer.h:530
virtual void dxfHex(int gc, int value) const =0
Must be overwritten by the implementing class to write an int value (hex) to the file.
virtual void dxfBool(int gc, bool value) const
Can be overwritten by the implementing class to write a bool value to the file.
Definition: dl_writer.h:568
void sectionBlocks() const
Section BLOCKS.
Definition: dl_writer.h:125
void table(const char *name, int num, int h=0) const
Generic table for table 'name' with 'num' entries:
Definition: dl_writer.h:195
DL_Writer(DL_Codes::version version)
Definition: dl_writer.h:63
void tableAppid(int num) const
Table for application id.
Definition: dl_writer.h:258
void tableAppidEntry(unsigned long int h=0) const
Appid (must be in the TABLES section APPID).
Definition: dl_writer.h:430
void sectionHeader() const
Section HEADER.
Definition: dl_writer.h:97
void sectionEntities() const
Section ENTITIES.
Definition: dl_writer.h:139
void entityAttributes(const DL_Attributes &attrib) const
Attributes of an entity.
Definition: dl_writer.h:346
DL_Codes::version version
DXF version to be created.
Definition: dl_writer.h:608
_t< detail::transform_< list< Args... > >> transform
Return a new meta::list constructed by transforming all the elements in L with the unary invocable Fn...
Definition: meta.hpp:1855