-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdxf2svg.cpp
94 lines (75 loc) · 3.74 KB
/
dxf2svg.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
85
86
87
88
89
90
91
92
93
94
/*
* Build a SVG from an dxf, will support conversion to Inkscape types
*
* Author:
* Matt Squires <[email protected]>
*
* Copyright (C) 2005 Matt Squires
*
* Released under GNU GPL and LGPL, read the file 'GPL.txt' and 'LGPL.txt' for details
*/
#include <fstream>
#include <iostream>
#include "read_dxf.h"
#include "entities.h"
#include "blocks.h"
#include "entities2elements.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc,char *argv[]) {
// Later include options for different conversions like converting as much as possible into paths
int ink = 1; // Assume for now there is no inkscape stuff to add extra
if (argc <= 1)
{
std::cerr << "usage: dxf2svg input.dxf > output.svg\n";
return 1;
}
const double scaling = 90; // converstion from in to pt
// Read the DXF file
//std::cout << "About to read file \n" << std::endl;
std::vector<std::vector<dxfpair> > output = dxf_get_sections(argv[1]);
//std::cout << "Finished reading file \n" << std::endl;
const std::vector<std::vector<dxfpair> > entities_info = separate_parts(output[4]); // Entities is the 5th part of the file.
entities ents(entities_info); // Sort entities into their respective parts
const std::vector<std::vector<dxfpair> > tables_info = separate_parts(output[2]); // Tables is the 3rd part of a dxf file.
tables tbls(tables_info); // Sort the information in the tables
const std::vector<std::vector<dxfpair> > blocks_info = separate_parts(output[3]); // Tables is the 4th part of a dxf file.
blocks blks(blocks_info); // Sort the information in the tables
// Get the various file informations
const std::vector<layer> layers = tbls.ret_layers();
const char *units = "in";
if (ink < 1) {
// Write a general svg header
std::cout << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n\t\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<svg xmlns=\"http://www.w3.org/2000/svg\"\n\txmlns:xlink=\"http://www.w3.org/1999/xlink\">\n";
std::cout << "\tx=\"0.00000000\"\n\ty=\"0.00000000\"\n\twidth=\"744.09448\"\n\theight=\"-1052.3622\"" << std::endl;
}
else{
std::cout << "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" << std::endl;
std::cout << "<!-- Created with dxf2svg -->" << std::endl;
std::cout << "<svg" << std::endl;
std::cout << "\txmlns:dc=\"http://purl.org/dc/elements/1.1/\"" << std::endl;
std::cout << "\txmlns:cc=\"http://web.resource.org/cc/\"" << std::endl;
std::cout << "\txmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"" << std::endl;
std::cout << "\txmlns:svg=\"http://www.w3.org/2000/svg\"" << std::endl;
std::cout << "\txmlns=\"http://www.w3.org/2000/svg\"" << std::endl;
std::cout << "\txmlns:sodipodi=\"http://inkscape.sourceforge.net/DTD/sodipodi-0.dtd\"" << std::endl;
std::cout << "\txmlns:inkscape=\"http://www.inkscape.org/namespaces/inkscape\"" << std::endl;
std::cout << "\t>" << std::endl;
}
// Now write SVG elements to file
if (layers.size() < 1) {
write_all(ents, tbls, blks, scaling, units);
}
else
{
for (size_t i = 0; i < layers.size(); i++) {
std::cout << "\t<g\n\t\tinkscape:label=\"" << layers[i].name() << "\"\n\t\tinkscape:groupmode=\"layer\"\n\t\tid=\"layer" << i+1 << "\">" << std::endl;
write_by_layer(ents, tbls, blks, scaling, units, layers[i].name());
std::cout << "\t</g>" << std::endl;
}
}
// Close SVG
std::cout << "</svg>";
return 0;
}