-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparts_list.cpp
More file actions
160 lines (138 loc) · 5.01 KB
/
parts_list.cpp
File metadata and controls
160 lines (138 loc) · 5.01 KB
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
152
153
154
155
156
157
158
159
160
#include "pstack/files/stl.hpp"
#include "pstack/gui/parts_list.hpp"
#include <wx/string.h>
#include <charconv>
#include <cmath>
#include <filesystem>
namespace pstack::gui {
namespace {
calc::part make_part(std::string mesh_file, bool mirrored) {
calc::part part;
part.mesh_file = std::move(mesh_file);
part.name = std::filesystem::path(part.mesh_file).stem().string();
part.mesh = files::from_stl(part.mesh_file);
part.mesh.set_baseline({ 0, 0, 0 });
part.base_quantity = [name = part.name]() mutable -> std::optional<int> {
char looking_for = '.';
if (name.ends_with(')')) {
name.pop_back();
looking_for = '(';
}
std::size_t number_length = 0;
while (name.size() > number_length and std::isdigit(name[name.size() - number_length - 1])) {
++number_length;
}
if (number_length == 0 or not (name.size() > number_length and name[name.size() - number_length - 1] == looking_for)) {
return std::nullopt;
}
std::string_view number{ name.data() + (name.size() - number_length), name.data() + name.size() };
int out{-1};
std::from_chars(number.data(), number.data() + number.size(), out);
return out;
}();
part.quantity = part.base_quantity.value_or(1);
auto volume_and_centroid = part.mesh.volume_and_centroid();
part.volume = volume_and_centroid.volume;
part.centroid = volume_and_centroid.centroid;
part.triangle_count = part.mesh.triangles().size();
part.mirrored = mirrored;
part.min_hole = 1;
part.rotation_index = 1;
part.rotate_min_box = false;
return part;
}
wxString quantity_string(const calc::part& part, const bool show_extra) {
if (not part.base_quantity.has_value()) {
return wxString::Format("%d", part.quantity);
}
static const wxString up_arrow = wxString::FromUTF8(" \xe2\x86\x91"); // ↑
static const wxString down_arrow = wxString::FromUTF8(" \xe2\x86\x93"); // ↓
static const wxString empty = "";
const int diff = part.quantity - *part.base_quantity;
if (diff > 0 and show_extra) {
return wxString::Format("%d + %d%s", *part.base_quantity, diff, up_arrow);
} else {
auto& arrow_suffix = (diff > 0) ? up_arrow : (diff < 0) ? down_arrow : empty;
return wxString::Format("%d%s", part.quantity, arrow_suffix);
}
}
} // namespace
void parts_list::initialize(wxWindow* parent) {
list_view::initialize(parent, {
{ "Name", 105 },
{ "Quantity", 60 },
{ "Volume", 60 },
{ "Triangles", 60 },
{ "Comment", 90 },
});
_label = new wxStaticText(parent, wxID_ANY, "");
update_label();
}
void parts_list::append(std::string mesh_file) {
append(make_part(std::move(mesh_file), false));
}
void parts_list::append(calc::part part) {
list_view::append({
part.name,
quantity_string(part, _show_extra),
wxString::Format("%.2f", part.volume / 1000),
std::to_string(part.triangle_count),
(part.mirrored ? "Mirrored" : "")
});
_parts.push_back(std::make_shared<calc::part>(std::move(part)));
}
void parts_list::change(std::string mesh_file, const std::size_t row) {
calc::part& part = *_parts.at(row);
part = make_part(std::move(mesh_file), part.mirrored);
reload_text(row);
list_view::deselect(row);
}
void parts_list::reload_file(const std::size_t row) {
change(std::move(_parts.at(row)->mesh_file), row);
}
void parts_list::reload_text(const std::size_t row) {
const calc::part& part = *_parts.at(row);
list_view::replace(row, {
part.name,
quantity_string(part, _show_extra),
wxString::Format("%.2f", part.volume / 1000),
std::to_string(part.triangle_count),
(part.mirrored ? "Mirrored" : "")
});
}
void parts_list::reload_all_text() {
for (std::size_t row = 0; row != rows(); ++row) {
reload_text(row);
}
}
void parts_list::reload_quantity(std::size_t row) {
list_view::set_text(row, 1, quantity_string(*_parts.at(row), _show_extra));
update_label();
}
void parts_list::delete_all() {
list_view::delete_all();
_parts.clear();
update_label();
}
void parts_list::delete_selected() {
list_view::delete_selected(_parts);
}
std::vector<std::shared_ptr<const calc::part>> parts_list::get_all() const {
std::vector<std::shared_ptr<const calc::part>> out{};
out.reserve(_parts.size());
std::ranges::copy(_parts, std::back_inserter(out));
return out;
}
void parts_list::update_label() {
_total_parts = 0;
_total_volume = 0;
_total_triangles = 0;
for (std::shared_ptr<const calc::part> part : _parts) {
_total_parts += part->quantity;
_total_volume += part->quantity * part->volume;
_total_triangles += part->quantity * part->triangle_count;
}
_label->SetLabelText(wxString::Format("Files: %zu - Parts: %zu - Volume: %.1f - Triangles: %zu",
_parts.size(), _total_parts, _total_volume / 1000, _total_triangles));
}
} // namespace pstack::gui