Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BedShapeDialog.cpp « GUI « slic3r « src « xs - github.com/prusa3d/PrusaSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e04f2b3700c4956a6d9907aed8da04822de6f237 (plain)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include "BedShapeDialog.hpp"

#include <wx/sizer.h>
#include <wx/statbox.h>
#include <wx/wx.h> 
#include "Polygon.hpp"
#include "BoundingBox.hpp"
#include <wx/numformatter.h>
#include "Model.hpp"
#include "boost/nowide/iostream.hpp"

#include <algorithm>

namespace Slic3r {
namespace GUI {

void BedShapeDialog::build_dialog(ConfigOptionPoints* default_pt)
{
	m_panel = new BedShapePanel(this);
	m_panel->build_panel(default_pt);

	auto main_sizer = new wxBoxSizer(wxVERTICAL);
	main_sizer->Add(m_panel, 1, wxEXPAND);
	main_sizer->Add(CreateButtonSizer(wxOK | wxCANCEL), 0, wxALIGN_CENTER_HORIZONTAL | wxBOTTOM, 10);

	SetSizer(main_sizer);
	SetMinSize(GetSize());
	main_sizer->SetSizeHints(this);

	// needed to actually free memory
	this->Bind(wxEVT_CLOSE_WINDOW, ([this](wxCloseEvent e){
		EndModal(wxID_OK);
		Destroy();
	}));
}

void BedShapePanel::build_panel(ConfigOptionPoints* default_pt)
{
//  on_change(nullptr);

	auto box = new wxStaticBox(this, wxID_ANY, _(L("Shape")));
	auto sbsizer = new wxStaticBoxSizer(box, wxVERTICAL);

	// shape options
	m_shape_options_book = new wxChoicebook(this, wxID_ANY, wxDefaultPosition, wxSize(300, -1), wxCHB_TOP);
	sbsizer->Add(m_shape_options_book);

	auto optgroup = init_shape_options_page(_(L("Rectangular")));
		ConfigOptionDef def;
		def.type = coPoints;
		def.default_value = new ConfigOptionPoints{ Vec2d(200, 200) };
		def.label = L("Size");
		def.tooltip = L("Size in X and Y of the rectangular plate.");
		Option option(def, "rect_size");
		optgroup->append_single_option_line(option);

		def.type = coPoints;
		def.default_value = new ConfigOptionPoints{ Vec2d(0, 0) };
		def.label = L("Origin");
		def.tooltip = L("Distance of the 0,0 G-code coordinate from the front left corner of the rectangle.");
		option = Option(def, "rect_origin");
		optgroup->append_single_option_line(option);

		optgroup = init_shape_options_page(_(L("Circular")));
		def.type = coFloat;
		def.default_value = new ConfigOptionFloat(200);
		def.sidetext = L("mm");
		def.label = L("Diameter");
		def.tooltip = L("Diameter of the print bed. It is assumed that origin (0,0) is located in the center.");
		option = Option(def, "diameter");
		optgroup->append_single_option_line(option);

		optgroup = init_shape_options_page(_(L("Custom")));
		Line line{ "", "" };
		line.full_width = 1;
		line.widget = [this](wxWindow* parent) {
			auto btn = new wxButton(parent, wxID_ANY, _(L("Load shape from STL...")), wxDefaultPosition, wxDefaultSize);
			
			auto sizer = new wxBoxSizer(wxHORIZONTAL);
			sizer->Add(btn);

			btn->Bind(wxEVT_BUTTON, ([this](wxCommandEvent e)
			{
				load_stl();
			}));

			return sizer;
		};
		optgroup->append_line(line);

	Bind(wxEVT_CHOICEBOOK_PAGE_CHANGED, ([this](wxCommandEvent e)
	{
		update_shape();
	}));

	// right pane with preview canvas
	m_canvas = new Bed_2D(this);
	m_canvas->m_bed_shape = default_pt->values;

	// main sizer
	auto top_sizer = new wxBoxSizer(wxHORIZONTAL);
	top_sizer->Add(sbsizer, 0, wxEXPAND | wxLeft | wxTOP | wxBOTTOM, 10);
	if (m_canvas)
		top_sizer->Add(m_canvas, 1, wxEXPAND | wxALL, 10) ;

	SetSizerAndFit(top_sizer);

	set_shape(default_pt);
	update_preview();
}

#define SHAPE_RECTANGULAR	0
#define SHAPE_CIRCULAR		1
#define SHAPE_CUSTOM		2

// Called from the constructor.
// Create a panel for a rectangular / circular / custom bed shape.
ConfigOptionsGroupShp BedShapePanel::init_shape_options_page(wxString title){

	auto panel = new wxPanel(m_shape_options_book);
	ConfigOptionsGroupShp optgroup;
	optgroup = std::make_shared<ConfigOptionsGroup>(panel, _(L("Settings")));

	optgroup->label_width = 100;
	optgroup->m_on_change = [this](t_config_option_key opt_key, boost::any value){
		update_shape();
	};
		
	m_optgroups.push_back(optgroup);
	panel->SetSizerAndFit(optgroup->sizer);
	m_shape_options_book->AddPage(panel, title);

	return optgroup;
}

// Called from the constructor.
// Set the initial bed shape from a list of points.
// Deduce the bed shape type(rect, circle, custom)
// This routine shall be smart enough if the user messes up
// with the list of points in the ini file directly.
void BedShapePanel::set_shape(ConfigOptionPoints* points)
{
	auto polygon = Polygon::new_scale(points->values);

	// is this a rectangle ?
	if (points->size() == 4) {
		auto lines = polygon.lines();
		if (lines[0].parallel_to(lines[2]) && lines[1].parallel_to(lines[3])) {
			// okay, it's a rectangle
			// find origin
            coordf_t x_min, x_max, y_min, y_max;
            x_max = x_min = points->values[0](0);
			y_max = y_min = points->values[0](1);
			for (auto pt : points->values)
            {
                x_min = std::min(x_min, pt(0));
                x_max = std::max(x_max, pt(0));
                y_min = std::min(y_min, pt(1));
                y_max = std::max(y_max, pt(1));
            }

            auto origin = new ConfigOptionPoints{ Vec2d(-x_min, -y_min) };

			m_shape_options_book->SetSelection(SHAPE_RECTANGULAR);
			auto optgroup = m_optgroups[SHAPE_RECTANGULAR];
			optgroup->set_value("rect_size", new ConfigOptionPoints{ Vec2d(x_max - x_min, y_max - y_min) });//[x_max - x_min, y_max - y_min]);
			optgroup->set_value("rect_origin", origin);
			update_shape();
			return;
		}
	}

	// is this a circle ?
	{
		// Analyze the array of points.Do they reside on a circle ?
		auto center = polygon.bounding_box().center();
		std::vector<double> vertex_distances;
		double avg_dist = 0;
		for (auto pt: polygon.points)
		{
			double distance = (pt - center).cast<double>().norm();
			vertex_distances.push_back(distance);
			avg_dist += distance;
		}

		avg_dist /= vertex_distances.size();
		bool defined_value = true;
		for (auto el: vertex_distances)
		{
			if (abs(el - avg_dist) > 10 * SCALED_EPSILON)
				defined_value = false;
			break;
		}
		if (defined_value) {
			// all vertices are equidistant to center
			m_shape_options_book->SetSelection(SHAPE_CIRCULAR);
			auto optgroup = m_optgroups[SHAPE_CIRCULAR];
			boost::any ret = wxNumberFormatter::ToString(unscale<double>(avg_dist * 2), 0);
 			optgroup->set_value("diameter", ret);
			update_shape();
			return;
		}
	}

	if (points->size() < 3) {
		// Invalid polygon.Revert to default bed dimensions.
		m_shape_options_book->SetSelection(SHAPE_RECTANGULAR);
		auto optgroup = m_optgroups[SHAPE_RECTANGULAR];
		optgroup->set_value("rect_size", new ConfigOptionPoints{ Vec2d(200, 200) });
		optgroup->set_value("rect_origin", new ConfigOptionPoints{ Vec2d(0, 0) });
		update_shape();
		return;
	}

	// This is a custom bed shape, use the polygon provided.
	m_shape_options_book->SetSelection(SHAPE_CUSTOM);
	// Copy the polygon to the canvas, make a copy of the array.
	m_canvas->m_bed_shape = points->values;
	update_shape();
}

void BedShapePanel::update_preview()
{
	if (m_canvas) m_canvas->Refresh();
	Refresh();
}

// Update the bed shape from the dialog fields.
void BedShapePanel::update_shape()
{
	auto page_idx = m_shape_options_book->GetSelection();
	if (page_idx == SHAPE_RECTANGULAR) {
		Vec2d rect_size(Vec2d::Zero());
		Vec2d rect_origin(Vec2d::Zero());
		try{
			rect_size = boost::any_cast<Vec2d>(m_optgroups[SHAPE_RECTANGULAR]->get_value("rect_size")); }
		catch (const std::exception &e){
			return;
		}
		try{
			rect_origin = boost::any_cast<Vec2d>(m_optgroups[SHAPE_RECTANGULAR]->get_value("rect_origin"));
		}
		catch (const std::exception &e){
			return;}
		
		auto x = rect_size(0);
		auto y = rect_size(1);
		// empty strings or '-' or other things
		if (x == 0 || y == 0)	return;
		double x0 = 0.0;
		double y0 = 0.0;
		double x1 = x;
		double y1 = y;

		auto dx = rect_origin(0);
		auto dy = rect_origin(1);

		x0 -= dx;
		x1 -= dx;
		y0 -= dy;
		y1 -= dy;
		m_canvas->m_bed_shape = {	Vec2d(x0, y0),
									Vec2d(x1, y0),
									Vec2d(x1, y1),
									Vec2d(x0, y1)};
	} 
	else if(page_idx == SHAPE_CIRCULAR) {
		double diameter;
		try{
			diameter = boost::any_cast<double>(m_optgroups[SHAPE_CIRCULAR]->get_value("diameter"));
		}
		catch (const std::exception &e){
			return;
		} 
 		if (diameter == 0.0) return ;
		auto r = diameter / 2;
		auto twopi = 2 * PI;
		auto edges = 60;
		std::vector<Vec2d> points;
		for (size_t i = 1; i <= 60; ++i){
			auto angle = i * twopi / edges;
			points.push_back(Vec2d(r*cos(angle), r*sin(angle)));
		}
		m_canvas->m_bed_shape = points;
	}

//	$self->{on_change}->();
	update_preview();
}

// Loads an stl file, projects it to the XY plane and calculates a polygon.
void BedShapePanel::load_stl()
{
	t_file_wild_card vec_FILE_WILDCARDS = get_file_wild_card();
    std::vector<std::string> file_types = { "known", "stl", "obj", "amf", "3mf", "prusa" };
    wxString MODEL_WILDCARD;
	for (auto file_type: file_types)
		MODEL_WILDCARD += vec_FILE_WILDCARDS.at(file_type) + "|";

	auto dialog = new wxFileDialog(this, _(L("Choose a file to import bed shape from (STL/OBJ/AMF/3MF/PRUSA):")), "", "",
		MODEL_WILDCARD, wxFD_OPEN | wxFD_FILE_MUST_EXIST);
	if (dialog->ShowModal() != wxID_OK) {
		dialog->Destroy();
		return;
	}
	wxArrayString input_file;
	dialog->GetPaths(input_file);
	dialog->Destroy();

	std::string file_name = input_file[0].ToStdString();

	Model model;
	try {
		model = Model::read_from_file(file_name);
	}
	catch (std::exception &e) {
		auto msg = _(L("Error! ")) + file_name + " : " + e.what() + ".";
		show_error(this, msg);
		exit(1);
	}

	auto mesh = model.mesh();
	auto expolygons = mesh.horizontal_projection();

	if (expolygons.size() == 0) {
		show_error(this, _(L("The selected file contains no geometry.")));
		return;
	}
	if (expolygons.size() > 1) {
		show_error(this, _(L("The selected file contains several disjoint areas. This is not supported.")));
		return;
	}

	auto polygon = expolygons[0].contour;
	std::vector<Vec2d> points;
	for (auto pt : polygon.points)
		points.push_back(unscale(pt));
	m_canvas->m_bed_shape = points;
	update_preview();
}

} // GUI
} // Slic3r