-
Notifications
You must be signed in to change notification settings - Fork 3
/
data_converter.cpp
151 lines (141 loc) · 5.19 KB
/
data_converter.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "data_converter.h"
namespace nesting {
void read_from_csv(const std::string& path,
std::vector<nesting::geo::Polygon_with_holes_2>& polygons,
std::vector<uint32_t>& allowed_rotations,
std::vector<uint32_t>& quantity) {
rapidcsv::Document document(path);
auto pgns = document.GetColumn<std::string>("polygon");
auto rots = document.GetColumn<uint32_t>("allowed_rotations");
auto quat = document.GetColumn<uint32_t>("quantity");
for (auto& s : pgns) {
polygons.emplace_back();
std::istringstream(s) >> polygons.back();
}
allowed_rotations.swap(rots);
quantity.swap(quat);
}
void read_from_cad(const std::string& path,
std::vector<nesting::geo::Polygon_with_holes_2>& polygons,
std::vector<uint32_t>& allowed_rotations,
std::vector<uint32_t>& quantity) {
dx_iface iface;
polygons.emplace_back();
iface.pwh = &polygons.back();
auto file = path.c_str();
auto ok = iface.fileImport(file, false);
if (!ok) {
throw std::runtime_error("Error read_from_cad(): can not import cad file");
}
allowed_rotations.push_back(1);
quantity.push_back(1);
}
void read_from_svg(const std::string& path,
std::vector<nesting::geo::Polygon_with_holes_2>& polygons,
std::vector<uint32_t>& allowed_rotations,
std::vector<uint32_t>& quantity) {
auto pth = QString::fromStdString(path);
QSvgRenderer svgRenderer(pth);
if (!svgRenderer.isValid()) {
throw std::runtime_error("Error read_from_svg(): invalid svg file");
}
SvgHelper svgHelper;
svgHelper.parseSvgImage(pth);
auto list = svgHelper.getSvgPointList();
for (auto& pgn : list) {
polygons.emplace_back();
// 最后一个点可能冗余
if (pgn.first() == pgn.last()) {
pgn.removeLast();
}
auto& outer = polygons.back().outer_boundary();
for (auto& v : pgn) {
outer.push_back(nesting::geo::Point_2(v.x(), v.y()));
}
allowed_rotations.push_back(1);
quantity.push_back(1);
}
qDebug() << list;
}
bool write_to_txt(const std::string& path,
const double& best_utilization,
const double& sheet_width,
const double& sheet_length,
const std::vector<geo::Polygon_with_holes_2>& res) {
// 打开文件
std::ofstream outputFile(path);
// 检查文件是否成功打开
if (!outputFile.is_open()) {
std::cerr << "Error opening file." << std::endl;
return false;
}
outputFile << std::setiosflags(std::ios::fixed) << std::setprecision(8);
outputFile << "Best Utilization: " << best_utilization << std::endl;
outputFile << "Sheet (W, L): "
<< "(" << sheet_width << ", " << sheet_length << ")" << std::endl;
outputFile << "Placement: " << std::endl;
for (auto& pwh : res) {
outputFile << pwh << std::endl;
}
// 关闭文件
outputFile.close();
return true;
}
void polygon_to_svg(std::ofstream& ofs, const Polygon_with_holes_2& p) {
ofs << "<polygon fill=\"grey\" stroke=\"black\" points=\"";
auto& outer = p.outer_boundary();
for (auto& v : outer) {
ofs << v.x() << "," << v.y() << " ";
}
ofs << "\"/> " << std::endl;
for (auto& h : p.holes()) {
ofs << "<polygon fill=\"white\" stroke=\"black\" points=\"";
for (auto& v : outer) {
ofs << v.x() << "," << v.y() << " ";
}
ofs << "\"/> " << std::endl;
}
}
bool write_to_svg(const std::string& path,
const double& best_utilization,
const double& sheet_width,
const double& sheet_length,
const std::vector<geo::Polygon_with_holes_2>& res) {
// 打开文件
std::ofstream outputFile(path);
// 检查文件是否成功打开
if (!outputFile.is_open()) {
std::cerr << "Error opening file." << std::endl;
return false;
}
outputFile << std::setiosflags(std::ios::fixed) << std::setprecision(8);
// svg头部
outputFile << "<svg width=\"" << sheet_length << "\" height=\"" << sheet_width
<< "\" viewBox=\"" << -0.25 * sheet_length << ", "
<< -0.25 * sheet_width << ", " << 1.5 * sheet_length << ", "
<< 1.5 * sheet_width << "\" xmlns=\"http://www.w3.org/2000/svg\">"
<< std::endl;
// sheet
outputFile << "<rect width=\"" << sheet_length << "\" height=\""
<< sheet_width << "\" fill=\"white\" stroke=\"black\"/>"
<< std::endl;
// polygons
for (auto& pwh : res) {
polygon_to_svg(outputFile, pwh);
}
// svg尾部
outputFile << "</svg>" << std::endl;
// 关闭文件
outputFile.close();
return true;
}
bool write_to_dxf(const std::string& path,
const double& best_utilization,
const double& sheet_width,
const double& sheet_length,
const std::vector<geo::Polygon_with_holes_2>& res) {
dx_iface iface;
iface.solutions = const_cast<std::vector<geo::Polygon_with_holes_2>*>(&res);
return iface.fileExport(path, DRW::Version::AC1014, false, false);
}
} // namespace nesting