| 1 |
/* |
| 2 |
* @file main.cpp |
| 3 |
*/ |
| 4 |
|
| 5 |
/***************************************************************************** |
| 6 |
** $Id: main.cpp 3591 2006-10-18 21:23:25Z andrew $ |
| 7 |
** |
| 8 |
** This is part of the dxflib library |
| 9 |
** Copyright (C) 2000-2001 Andrew Mustun |
| 10 |
** |
| 11 |
** This program is free software; you can redistribute it and/or modify |
| 12 |
** it under the terms of the GNU Library General Public License as |
| 13 |
** published by the Free Software Foundation. |
| 14 |
** |
| 15 |
** This program is distributed in the hope that it will be useful, |
| 16 |
** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
** GNU Library General Public License for more details. |
| 19 |
** |
| 20 |
** You should have received a copy of the GNU Library General Public License |
| 21 |
** along with this program; if not, write to the Free Software |
| 22 |
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 23 |
******************************************************************************/ |
| 24 |
|
| 25 |
#include <iostream> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <stdio.h> |
| 28 |
|
| 29 |
#include "dl_dxf.h" |
| 30 |
#include "dl_creationadapter.h" |
| 31 |
|
| 32 |
#include "test_creationclass.h" |
| 33 |
|
| 34 |
void usage(); |
| 35 |
void testReading(char* file); |
| 36 |
void testWriting(); |
| 37 |
|
| 38 |
|
| 39 |
/* |
| 40 |
* @brief Main function for DXFLib test program. |
| 41 |
* |
| 42 |
* @param argc Number of delimited items on command line, |
| 43 |
* including program name. |
| 44 |
* @param argv Pointer to array of command line items |
| 45 |
* |
| 46 |
* @retval 0 if missing input file argument or |
| 47 |
* file couldn't be opened |
| 48 |
* @retval 1 if file opened |
| 49 |
*/ |
| 50 |
int main(int argc, char** argv) { |
| 51 |
|
| 52 |
// Check given arguments: |
| 53 |
if (argc<2) { |
| 54 |
usage(); |
| 55 |
return 0; |
| 56 |
} |
| 57 |
|
| 58 |
testReading(argv[1]); |
| 59 |
|
| 60 |
testWriting(); |
| 61 |
|
| 62 |
return 0; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
/* |
| 68 |
* @brief Prints error message if file name not specified as command |
| 69 |
* line argument. |
| 70 |
*/ |
| 71 |
void usage() { |
| 72 |
std::cout << "\nUsage: test <DXF file>\n\n"; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
void testReading(char* file) { |
| 77 |
// Load DXF file into memory: |
| 78 |
std::cout << "Reading file " << file << "...\n"; |
| 79 |
Test_CreationClass* creationClass = new Test_CreationClass(); |
| 80 |
DL_Dxf* dxf = new DL_Dxf(); |
| 81 |
if (!dxf->in(file, creationClass)) { // if file open failed |
| 82 |
std::cerr << file << " could not be opened.\n"; |
| 83 |
return; |
| 84 |
} |
| 85 |
delete dxf; |
| 86 |
delete creationClass; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
void testWriting() { |
| 92 |
DL_Dxf* dxf = new DL_Dxf(); |
| 93 |
DL_Codes::version exportVersion = DL_Codes::AC1015; |
| 94 |
DL_WriterA* dw = dxf->out("myfile.dxf", exportVersion); |
| 95 |
if (dw==NULL) { |
| 96 |
printf("Cannot open file 'myfile.dxf' \ |
| 97 |
for writing."); |
| 98 |
// abort function e.g. with return |
| 99 |
} |
| 100 |
|
| 101 |
dxf->writeHeader(*dw); |
| 102 |
/* |
| 103 |
// int variable: |
| 104 |
dw->dxfString(9, "$INSUNITS"); |
| 105 |
dw->dxfInt(70, 4); |
| 106 |
// real (double, float) variable: |
| 107 |
dw->dxfString(9, "$DIMEXE"); |
| 108 |
dw->dxfReal(40, 1.25); |
| 109 |
// string variable: |
| 110 |
dw->dxfString(9, "$TEXTSTYLE"); |
| 111 |
dw->dxfString(7, "Standard"); |
| 112 |
// vector variable: |
| 113 |
dw->dxfString(9, "$LIMMIN"); |
| 114 |
dw->dxfReal(10, 0.0); |
| 115 |
dw->dxfReal(20, 0.0); |
| 116 |
*/ |
| 117 |
dw->sectionEnd(); |
| 118 |
dw->sectionTables(); |
| 119 |
dxf->writeVPort(*dw); |
| 120 |
|
| 121 |
dw->tableLinetypes(3); |
| 122 |
dxf->writeLinetype(*dw, DL_LinetypeData("BYBLOCK", "BYBLOCK", 0, 0, 0.0)); |
| 123 |
dxf->writeLinetype(*dw, DL_LinetypeData("BYLAYER", "BYLAYER", 0, 0, 0.0)); |
| 124 |
dxf->writeLinetype(*dw, DL_LinetypeData("CONTINUOUS", "Continuous", 0, 0, 0.0)); |
| 125 |
dw->tableEnd(); |
| 126 |
|
| 127 |
int numberOfLayers = 3; |
| 128 |
dw->tableLayers(numberOfLayers); |
| 129 |
|
| 130 |
dxf->writeLayer(*dw, |
| 131 |
DL_LayerData("0", 0), |
| 132 |
DL_Attributes( |
| 133 |
std::string(""), // leave empty |
| 134 |
DL_Codes::black, // default color |
| 135 |
100, // default width |
| 136 |
"CONTINUOUS", 1.0)); // default line style |
| 137 |
|
| 138 |
dxf->writeLayer(*dw, |
| 139 |
DL_LayerData("mainlayer", 0), |
| 140 |
DL_Attributes( |
| 141 |
std::string(""), |
| 142 |
DL_Codes::red, |
| 143 |
100, |
| 144 |
"CONTINUOUS", 1.0)); |
| 145 |
|
| 146 |
dxf->writeLayer(*dw, |
| 147 |
DL_LayerData("anotherlayer", 0), |
| 148 |
DL_Attributes( |
| 149 |
std::string(""), |
| 150 |
DL_Codes::black, |
| 151 |
100, |
| 152 |
"CONTINUOUS", 1.0)); |
| 153 |
|
| 154 |
dw->tableEnd(); |
| 155 |
|
| 156 |
dw->tableStyle(1); |
| 157 |
dxf->writeStyle(*dw, DL_StyleData("standard", 0, 2.5, 1.0, 0.0, 0, 2.5, "txt", "")); |
| 158 |
dw->tableEnd(); |
| 159 |
|
| 160 |
dxf->writeView(*dw); |
| 161 |
dxf->writeUcs(*dw); |
| 162 |
|
| 163 |
dw->tableAppid(1); |
| 164 |
dxf->writeAppid(*dw, "ACAD"); |
| 165 |
dw->tableEnd(); |
| 166 |
|
| 167 |
dxf->writeDimStyle(*dw, 1, 1, 1, 1, 1); |
| 168 |
|
| 169 |
dxf->writeBlockRecord(*dw); |
| 170 |
dxf->writeBlockRecord(*dw, "myblock1"); |
| 171 |
dxf->writeBlockRecord(*dw, "myblock2"); |
| 172 |
dw->tableEnd(); |
| 173 |
|
| 174 |
dw->sectionEnd(); |
| 175 |
|
| 176 |
dw->sectionBlocks(); |
| 177 |
dxf->writeBlock(*dw, DL_BlockData("*Model_Space", 0, 0.0, 0.0, 0.0)); |
| 178 |
dxf->writeEndBlock(*dw, "*Model_Space"); |
| 179 |
dxf->writeBlock(*dw, DL_BlockData("*Paper_Space", 0, 0.0, 0.0, 0.0)); |
| 180 |
dxf->writeEndBlock(*dw, "*Paper_Space"); |
| 181 |
dxf->writeBlock(*dw, DL_BlockData("*Paper_Space0", 0, 0.0, 0.0, 0.0)); |
| 182 |
dxf->writeEndBlock(*dw, "*Paper_Space0"); |
| 183 |
|
| 184 |
dxf->writeBlock(*dw, DL_BlockData("myblock1", 0, 0.0, 0.0, 0.0)); |
| 185 |
// ... |
| 186 |
// write block entities e.g. with dxf->writeLine(), .. |
| 187 |
// ... |
| 188 |
dxf->writeEndBlock(*dw, "myblock1"); |
| 189 |
|
| 190 |
dxf->writeBlock(*dw, DL_BlockData("myblock2", 0, 0.0, 0.0, 0.0)); |
| 191 |
// ... |
| 192 |
// write block entities e.g. with dxf->writeLine(), .. |
| 193 |
// ... |
| 194 |
dxf->writeEndBlock(*dw, "myblock2"); |
| 195 |
|
| 196 |
dw->sectionEnd(); |
| 197 |
dw->sectionEntities(); |
| 198 |
|
| 199 |
// write all entities in model space: |
| 200 |
dxf->writePoint( |
| 201 |
*dw, |
| 202 |
DL_PointData(10.0, |
| 203 |
45.0, |
| 204 |
0.0), |
| 205 |
DL_Attributes("mainlayer", 256, -1, "BYLAYER", 1.0)); |
| 206 |
|
| 207 |
dxf->writeLine( |
| 208 |
*dw, |
| 209 |
DL_LineData(25.0, // start point |
| 210 |
30.0, |
| 211 |
0.0, |
| 212 |
100.0, // end point |
| 213 |
120.0, |
| 214 |
0.0), |
| 215 |
DL_Attributes("mainlayer", 256, -1, "BYLAYER", 1.0)); |
| 216 |
|
| 217 |
dw->sectionEnd(); |
| 218 |
|
| 219 |
dxf->writeObjects(*dw); |
| 220 |
dxf->writeObjectsEnd(*dw); |
| 221 |
|
| 222 |
dw->dxfEOF(); |
| 223 |
dw->close(); |
| 224 |
delete dw; |
| 225 |
delete dxf; |
| 226 |
} |
| 227 |
|