-
Notifications
You must be signed in to change notification settings - Fork 10
/
AutoCADConverter.cpp
executable file
·84 lines (74 loc) · 2.46 KB
/
AutoCADConverter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "InputFormatDXF.h"
#include "InputFormatDWG.h"
#include "OutputFormatText.h"
#include "OutputFormatConsole.h"
#include <sys/stat.h>
#include <stdio.h>
void usage(char * me) {
printf("Usage: %s inputfile outputfile\n", me);
printf("Input formats supported:\n");
printf("\tAutoCAD DXF (*.dxf)\n");
printf("\tAutoCAD DWG (*.dwg)\n");
printf("Output formats supported:\n");
printf("\tText (*.txt)\n");
printf("\tConsole output (don't specify second parameter)\n");
printf("\nInput format and output format are determined automatically by file extension.\n");
}
int main(int argc, char * argv[]) {
if (argc < 2) {
usage(argv[0]);
return 1;
}
OutputFormat * output = NULL;
InputFormat * input = NULL;
struct stat fileExists;
// Set up output format first...
std::string outFile = argc < 3 ? "" : argv[2];
if (outFile != "" && outFile.rfind('.') == std::string::npos) {
fprintf(stderr, "I couldn't make sense of your output filename's extension: %s. Please use filename.shp, filename.kml, or filename.txt.\n", argv[2]);
return 1;
}
if (outFile.substr(outFile.rfind('.')+1) == "txt") {
printf("Setting up output file %s...\n", argv[2]);
output = new OutputFormatText(argv[2]);
} else if(outFile == "") {
output = new OutputFormatConsole();
}
// Next grab the input file
std::string inFile = argv[1];
if (inFile.rfind('.') == std::string::npos) {
fprintf(stderr, "I couldn't make sense of your input filename's extension: %s. Please use filename.dxf or filename.dwg.\n", argv[1]);
delete output;
return 1;
}
if (stat(argv[1], &fileExists) != 0) {
fprintf(stderr, "The specified input file does not exist or is not accessible: %s\n", argv[1]);
return 1;
}
if (inFile.substr(inFile.rfind('.')+1) == "dxf") {
input = new InputFormatDXF(argv[1]);
if(outFile != "")
printf("Setting up input file %s...\n", argv[1]);
} else if (inFile.substr(inFile.rfind('.')+1) == "dwg") {
input = new InputFormatDWG(argv[1]);
if(outFile != "")
printf("Setting up input file %s...\n", argv[1]);
}
if (!input) {
fprintf(stderr, "The input file was not recognized or could not be opened.\n");
return 1;
}
if (!output) {
fprintf(stderr, "The output file was not recognized or could not be opened.\n");
return 1;
}
if(outFile != "")
printf("Converting file...\n");
input->readFeaturesInto(output);
output->finalizeOutput();
if(outFile != "")
printf("Done!\n");
if(input != NULL) delete input;
if(output != NULL) delete output;
return 0;
}