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 <iostream>
40#include <algorithm>
41
42#include "dl_attributes.h"
43#include "dl_codes.h"
44
45
46
57class DXFLIB_EXPORT DL_Writer {
58public:
62 DL_Writer(DL_Codes::version version) : m_handle(0x30) {
63 this->version = version;
64 modelSpaceHandle = 0;
65 paperSpaceHandle = 0;
66 paperSpace0Handle = 0;
67 }
68
69 virtual ~DL_Writer() {}
70 ;
71
81 void section(const char* name) const {
82 dxfString(0, "SECTION");
83 dxfString(2, name);
84 }
85
96 void sectionHeader() const {
97 section("HEADER");
98 }
99
110 void sectionTables() const {
111 section("TABLES");
112 }
113
124 void sectionBlocks() const {
125 section("BLOCKS");
126 }
127
138 void sectionEntities() const {
139 section("ENTITIES");
140 }
141
152 void sectionClasses() const {
153 section("CLASSES");
154 }
155
166 void sectionObjects() const {
167 section("OBJECTS");
168 }
169
178 void sectionEnd() const {
179 dxfString(0, "ENDSEC");
180 }
181
194 void table(const char* name, int num, int h=0) const {
195 dxfString(0, "TABLE");
196 dxfString(2, name);
197 if (version>=DL_VERSION_2000) {
198 if (h==0) {
199 handle();
200 }
201 else {
202 dxfHex(5, h);
203 }
204 dxfString(100, "AcDbSymbolTable");
205 }
206 dxfInt(70, num);
207 }
208
222 void tableLayers(int num) const {
223 table("LAYER", num, 2);
224 }
225
239 void tableLinetypes(int num) const {
240 //linetypeHandle = 5;
241 table("LTYPE", num, 5);
242 }
243
257 void tableAppid(int num) const {
258 table("APPID", num, 9);
259 }
260
274 void tableStyle(int num) const {
275 table("STYLE", num, 3);
276 }
277
286 void tableEnd() const {
287 dxfString(0, "ENDTAB");
288 }
289
298 void dxfEOF() const {
299 dxfString(0, "EOF");
300 }
301
310 void comment(const char* text) const {
311 dxfString(999, text);
312 }
313
324 void entity(const char* entTypeName) const {
325 dxfString(0, entTypeName);
326 if (version>=DL_VERSION_2000) {
327 handle();
328 }
329 }
330
345 void entityAttributes(const DL_Attributes& attrib) const {
346
347 // layer name:
348 dxfString(8, attrib.getLayer());
349
350 // R12 doesn't accept BYLAYER values. The value has to be missing
351 // in that case.
352 if (version>=DL_VERSION_2000 || attrib.getColor()!=256) {
353 dxfInt(62, attrib.getColor());
354 }
355 if (version>=DL_VERSION_2000 && attrib.getColor24()!=-1) {
356 dxfInt(420, attrib.getColor24());
357 }
358 if (version>=DL_VERSION_2000) {
359 dxfInt(370, attrib.getWidth());
360 }
361 if (version>=DL_VERSION_2000) {
362 dxfReal(48, attrib.getLinetypeScale());
363 }
364 std::string linetype = attrib.getLinetype();
365 std::transform(linetype.begin(), linetype.end(), linetype.begin(), ::toupper);
366 if (version>=DL_VERSION_2000 || linetype=="BYLAYER") {
367 dxfString(6, attrib.getLinetype());
368 }
369 }
370
374 void subClass(const char* sub) const {
375 dxfString(100, sub);
376 }
377
386 void tableLayerEntry(unsigned long int h=0) const {
387 dxfString(0, "LAYER");
388 if (version>=DL_VERSION_2000) {
389 if (h==0) {
390 handle();
391 } else {
392 dxfHex(5, h);
393 }
394 dxfString(100, "AcDbSymbolTableRecord");
395 dxfString(100, "AcDbLayerTableRecord");
396 }
397 }
398
407 void tableLinetypeEntry(unsigned long int h=0) const {
408 dxfString(0, "LTYPE");
409 if (version>=DL_VERSION_2000) {
410 if (h==0) {
411 handle();
412 } else {
413 dxfHex(5, h);
414 }
415 //dxfHex(330, 0x5);
416 dxfString(100, "AcDbSymbolTableRecord");
417 dxfString(100, "AcDbLinetypeTableRecord");
418 }
419 }
420
429 void tableAppidEntry(unsigned long int h=0) const {
430 dxfString(0, "APPID");
431 if (version>=DL_VERSION_2000) {
432 if (h==0) {
433 handle();
434 } else {
435 dxfHex(5, h);
436 }
437 //dxfHex(330, 0x9);
438 dxfString(100, "AcDbSymbolTableRecord");
439 dxfString(100, "AcDbRegAppTableRecord");
440 }
441 }
442
451 void sectionBlockEntry(unsigned long int h=0) const {
452 dxfString(0, "BLOCK");
453 if (version>=DL_VERSION_2000) {
454 if (h==0) {
455 handle();
456 } else {
457 dxfHex(5, h);
458 }
459 //dxfHex(330, blockHandle);
460 dxfString(100, "AcDbEntity");
461 if (h==0x1C) {
462 dxfInt(67, 1);
463 }
464 dxfString(8, "0"); // TODO: Layer for block
465 dxfString(100, "AcDbBlockBegin");
466 }
467 }
468
477 void sectionBlockEntryEnd(unsigned long int h=0) const {
478 dxfString(0, "ENDBLK");
479 if (version>=DL_VERSION_2000) {
480 if (h==0) {
481 handle();
482 } else {
483 dxfHex(5, h);
484 }
485 //dxfHex(330, blockHandle);
486 dxfString(100, "AcDbEntity");
487 if (h==0x1D) {
488 dxfInt(67, 1);
489 }
490 dxfString(8, "0"); // TODO: Layer for block
491 dxfString(100, "AcDbBlockEnd");
492 }
493 }
494
495 void color(int col=256) const {
496 dxfInt(62, col);
497 }
498 void linetype(const char *lt) const {
499 dxfString(6, lt);
500 }
501 void linetypeScale(double scale) const {
502 dxfReal(48, scale);
503 }
504 void lineWeight(int lw) const {
505 dxfInt(370, lw);
506 }
507
508 void coord(int gc, double x, double y, double z=0) const {
509 dxfReal(gc, x);
510 dxfReal(gc+10, y);
511 dxfReal(gc+20, z);
512 }
513
514 void coordTriplet(int gc, const double* value) const {
515 if (value) {
516 dxfReal(gc, *value++);
517 dxfReal(gc+10, *value++);
518 dxfReal(gc+20, *value++);
519 }
520 }
521
522 void resetHandle() const {
523 m_handle = 1;
524 }
525
529 unsigned long handle(int gc=5) const {
530 // handle has to be hex
531 dxfHex(gc, m_handle);
532 return m_handle++;
533 }
534
538 unsigned long getNextHandle() const {
539 return m_handle;
540 }
541
545 unsigned long incHandle() const {
546 return m_handle++;
547 }
548
553 void setModelSpaceHandle(unsigned long h) {
554 modelSpaceHandle = h;
555 }
556
557 unsigned long getModelSpaceHandle() {
558 return modelSpaceHandle;
559 }
560
565 void setPaperSpaceHandle(unsigned long h) {
566 paperSpaceHandle = h;
567 }
568
569 unsigned long getPaperSpaceHandle() {
570 return paperSpaceHandle;
571 }
572
577 void setPaperSpace0Handle(unsigned long h) {
578 paperSpace0Handle = h;
579 }
580
581 unsigned long getPaperSpace0Handle() {
582 return paperSpace0Handle;
583 }
584
592 virtual void dxfReal(int gc, double value) const = 0;
593
601 virtual void dxfInt(int gc, int value) const = 0;
602
610 virtual void dxfBool(int gc, bool value) const {
611 dxfInt(gc, (int)value);
612 }
613
621 virtual void dxfHex(int gc, int value) const = 0;
622
630 virtual void dxfString(int gc, const char* value) const = 0;
631
639 virtual void dxfString(int gc, const std::string& value) const = 0;
640
641protected:
642 mutable unsigned long m_handle;
643 mutable unsigned long modelSpaceHandle;
644 mutable unsigned long paperSpaceHandle;
645 mutable unsigned long paperSpace0Handle;
646
651private:
652};
653
654#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:57
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:298
void sectionTables() const
Section TABLES.
Definition: dl_writer.h:110
void setPaperSpace0Handle(unsigned long h)
Sets the handle of the paper space 0.
Definition: dl_writer.h:577
void tableEnd() const
End of a table.
Definition: dl_writer.h:286
void setModelSpaceHandle(unsigned long h)
Sets the handle of the model space.
Definition: dl_writer.h:553
void sectionClasses() const
Section CLASSES.
Definition: dl_writer.h:152
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:407
void sectionObjects() const
Section OBJECTS.
Definition: dl_writer.h:166
unsigned long getNextHandle() const
Definition: dl_writer.h:538
void section(const char *name) const
Generic section for section 'name'.
Definition: dl_writer.h:81
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:310
void tableLinetypes(int num) const
Table for line types.
Definition: dl_writer.h:239
void tableStyle(int num) const
Table for text style.
Definition: dl_writer.h:274
void entity(const char *entTypeName) const
Entity.
Definition: dl_writer.h:324
void tableLayers(int num) const
Table for layers.
Definition: dl_writer.h:222
void sectionBlockEntry(unsigned long int h=0) const
Block (must be in the section BLOCKS).
Definition: dl_writer.h:451
void sectionEnd() const
End of a section.
Definition: dl_writer.h:178
unsigned long incHandle() const
Increases handle, so that the handle returned remains available.
Definition: dl_writer.h:545
void sectionBlockEntryEnd(unsigned long int h=0) const
End of Block (must be in the section BLOCKS).
Definition: dl_writer.h:477
void tableLayerEntry(unsigned long int h=0) const
Layer (must be in the TABLES section LAYER).
Definition: dl_writer.h:386
void subClass(const char *sub) const
Subclass.
Definition: dl_writer.h:374
unsigned long handle(int gc=5) const
Writes a unique handle and returns it.
Definition: dl_writer.h:529
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:610
void sectionBlocks() const
Section BLOCKS.
Definition: dl_writer.h:124
void table(const char *name, int num, int h=0) const
Generic table for table 'name' with 'num' entries:
Definition: dl_writer.h:194
DL_Writer(DL_Codes::version version)
Definition: dl_writer.h:62
void tableAppid(int num) const
Table for application id.
Definition: dl_writer.h:257
void tableAppidEntry(unsigned long int h=0) const
Appid (must be in the TABLES section APPID).
Definition: dl_writer.h:429
void sectionHeader() const
Section HEADER.
Definition: dl_writer.h:96
void setPaperSpaceHandle(unsigned long h)
Sets the handle of the paper space.
Definition: dl_writer.h:565
void sectionEntities() const
Section ENTITIES.
Definition: dl_writer.h:138
void entityAttributes(const DL_Attributes &attrib) const
Attributes of an entity.
Definition: dl_writer.h:345
DL_Codes::version version
DXF version to be created.
Definition: dl_writer.h:650