1 /****************************************************************************
2 ** $Id: dl_writer.h 2398 2005-06-06 18:12:14Z andrew $
4 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
5 ** Copyright (C) 2001 Robert J. Campbell Jr.
7 ** This file is part of the dxflib project.
9 ** This file may be distributed and/or modified under the terms of the
10 ** GNU General Public License version 2 as published by the Free Software
11 ** Foundation and appearing in the file LICENSE.GPL included in the
12 ** packaging of this file.
14 ** Licensees holding valid dxflib Professional Edition licenses may use
15 ** this file in accordance with the dxflib Commercial License
16 ** Agreement provided with the Software.
18 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
19 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 ** See http://www.ribbonsoft.com for further details.
23 ** Contact info@ribbonsoft.com if any conditions of this licensing are
26 **********************************************************************/
33 #endif // _MSC_VER > 1000
35 #if defined(__OS2__)||defined(__EMX__)||defined(_WIN32)
36 #define strcasecmp(s,t) stricmp(s,t)
42 #include "dl_attributes.h"
48 * Defines interface for writing low level DXF constructs to
49 * a file. Implementation is defined in derived classes that write
50 * to binary or ASCII files.
52 * Implements functions that write higher level constructs in terms of
55 * @todo Add error checking for string/entry length.
60 * @para version DXF version. Defaults to VER_2002.
62 DL_Writer(DL_Codes::version version) : m_handle(0x30) {
63 this->version = version;
66 paperSpace0Handle = 0;
69 virtual ~DL_Writer() {}
72 /** Generic section for section 'name'.
81 void section(const char* name) const {
82 dxfString(0, "SECTION");
96 void sectionHeader() const {
110 void sectionTables() const {
124 void sectionBlocks() const {
138 void sectionEntities() const {
152 void sectionClasses() const {
166 void sectionObjects() const {
178 void sectionEnd() const {
179 dxfString(0, "ENDSEC");
183 * Generic table for table 'name' with 'num' entries:
194 void table(const char* name, int num, int handle) const {
195 dxfString(0, "TABLE");
197 if (version>=VER_2000) {
199 dxfString(100, "AcDbSymbolTable");
204 /** Table for layers.
206 * @param num Number of layers in total.
217 void tableLayers(int num) const {
218 table("LAYER", num, 2);
221 /** Table for line types.
223 * @param num Number of line types in total.
234 void tableLineTypes(int num) const {
235 //lineTypeHandle = 5;
236 table("LTYPE", num, 5);
239 /** Table for application id.
241 * @param num Number of registered applications in total.
252 void tableAppid(int num) const {
253 table("APPID", num, 9);
264 void tableEnd() const {
265 dxfString(0, "ENDTAB");
269 * End of the DXF file.
276 void dxfEOF() const {
288 void comment(const char* text) const {
289 dxfString(999, text);
300 * @return Unique handle or 0.
302 void entity(const char* entTypeName) const {
303 dxfString(0, entTypeName);
304 if (version>=VER_2000) {
310 * Attributes of an entity.
323 void entityAttributes(const DL_Attributes& attrib) const {
326 dxfString(8, attrib.getLayer());
328 // R12 doesn't accept BYLAYER values. The value has to be missing
330 if (version>=VER_2000 ||
331 attrib.getColor()!=256) {
332 dxfInt(62, attrib.getColor());
334 if (version>=VER_2000) {
335 dxfInt(370, attrib.getWidth());
337 if (version>=VER_2000 ||
338 strcasecmp(attrib.getLineType().c_str(), "BYLAYER")) {
339 dxfString(6, attrib.getLineType());
346 void subClass(const char* sub) const {
351 * Layer (must be in the TABLES section LAYER).
358 void tableLayerEntry(unsigned long int h=0) const {
359 dxfString(0, "LAYER");
360 if (version>=VER_2000) {
366 dxfString(100, "AcDbSymbolTableRecord");
367 dxfString(100, "AcDbLayerTableRecord");
372 * Line type (must be in the TABLES section LTYPE).
379 void tableLineTypeEntry(unsigned long int h=0) const {
380 dxfString(0, "LTYPE");
381 if (version>=VER_2000) {
388 dxfString(100, "AcDbSymbolTableRecord");
389 dxfString(100, "AcDbLinetypeTableRecord");
394 * Appid (must be in the TABLES section APPID).
401 void tableAppidEntry(unsigned long int h=0) const {
402 dxfString(0, "APPID");
403 if (version>=VER_2000) {
410 dxfString(100, "AcDbSymbolTableRecord");
411 dxfString(100, "AcDbRegAppTableRecord");
416 * Block (must be in the section BLOCKS).
423 void sectionBlockEntry(unsigned long int h=0) const {
424 dxfString(0, "BLOCK");
425 if (version>=VER_2000) {
431 //dxfHex(330, blockHandle);
432 dxfString(100, "AcDbEntity");
436 dxfString(8, "0"); // TODO: Layer for block
437 dxfString(100, "AcDbBlockBegin");
442 * End of Block (must be in the section BLOCKS).
449 void sectionBlockEntryEnd(unsigned long int h=0) const {
450 dxfString(0, "ENDBLK");
451 if (version>=VER_2000) {
457 //dxfHex(330, blockHandle);
458 dxfString(100, "AcDbEntity");
462 dxfString(8, "0"); // TODO: Layer for block
463 dxfString(100, "AcDbBlockEnd");
467 void color(int col=256) const {
470 void lineType(const char *lt) const {
473 void lineTypeScale(double scale) const {
476 void lineWeight(int lw) const {
480 void coord(int gc, double x, double y, double z=0) const {
486 void coordTriplet(int gc, const double* value) const {
488 dxfReal(gc, *value++);
489 dxfReal(gc+10, *value++);
490 dxfReal(gc+20, *value++);
494 void resetHandle() const {
499 * Writes a unique handle and returns it.
501 unsigned long handle(int gc=5) const {
502 // handle has to be hex
503 dxfHex(gc, m_handle);
508 * @return Next handle that will be written.
510 unsigned long getNextHandle() const {
515 * Increases handle, so that the handle returned remains available.
517 unsigned long incHandle() const {
522 * Sets the handle of the model space. Entities refer to
525 void setModelSpaceHandle(unsigned long h) {
526 modelSpaceHandle = h;
529 unsigned long getModelSpaceHandle() {
530 return modelSpaceHandle;
534 * Sets the handle of the paper space. Some special blocks refer to
537 void setPaperSpaceHandle(unsigned long h) {
538 paperSpaceHandle = h;
541 unsigned long getPaperSpaceHandle() {
542 return paperSpaceHandle;
546 * Sets the handle of the paper space 0. Some special blocks refer to
549 void setPaperSpace0Handle(unsigned long h) {
550 paperSpace0Handle = h;
553 unsigned long getPaperSpace0Handle() {
554 return paperSpace0Handle;
558 * Must be overwritten by the implementing class to write a
559 * real value to the file.
561 * @param gc Group code.
562 * @param value The real value.
564 virtual void dxfReal(int gc, double value) const = 0;
567 * Must be overwritten by the implementing class to write an
568 * int value to the file.
570 * @param gc Group code.
571 * @param value The int value.
573 virtual void dxfInt(int gc, int value) const = 0;
576 * Must be overwritten by the implementing class to write an
577 * int value (hex) to the file.
579 * @param gc Group code.
580 * @param value The int value.
582 virtual void dxfHex(int gc, int value) const = 0;
585 * Must be overwritten by the implementing class to write a
586 * string to the file.
588 * @param gc Group code.
589 * @param value The string.
591 virtual void dxfString(int gc, const char* value) const = 0;
594 * Must be overwritten by the implementing class to write a
595 * string to the file.
597 * @param gc Group code.
598 * @param value The string.
600 virtual void dxfString(int gc, const string& value) const = 0;
603 mutable unsigned long m_handle;
604 mutable unsigned long modelSpaceHandle;
605 mutable unsigned long paperSpaceHandle;
606 mutable unsigned long paperSpace0Handle;
609 * DXF version to be created.
611 DL_Codes::version version;