OrcaSlicer/src/slic3r/GUI/Jobs/FillBedJob.cpp
Noisyfox 933aa3050b
Port Emboss & SVG gizmo from PrusaSlicer (#2819)
* Rework UI jobs to make them more understandable and flexible.

* Update Orca specific jobs

* Fix progress issue

* Fix dark mode and window radius

* Update cereal version from 1.2.2 to 1.3.0

(cherry picked from commit prusa3d/PrusaSlicer@057232a275)

* Initial port of Emboss gizmo

* Bump up CGAL version to 5.4

(cherry picked from commit prusa3d/PrusaSlicer@1bf9dee3e7)

* Fix text rotation

* Fix test dragging

* Add text gizmo to right click menu

* Initial port of SVG gizmo

* Fix text rotation

* Fix Linux build

* Fix "from surface"

* Fix -90 rotation

* Fix icon path

* Fix loading font with non-ascii name

* Fix storing non-utf8 font descriptor in 3mf file

* Fix filtering with non-utf8 characters

* Emboss: Use Orca style input dialog

* Fix build on macOS

* Fix tooltip color in light mode

* InputText: fixed incorrect padding when FrameBorder > 0. (ocornut/imgui#4794, ocornut/imgui#3781)
InputTextMultiline: fixed vertical tracking with large values of FramePadding.y. (ocornut/imgui#3781, ocornut/imgui#4794)

(cherry picked from commit ocornut/imgui@072caa4a90)
(cherry picked from commit ocornut/imgui@bdd2a94315)

* SVG: Use Orca style input dialog

* Fix job progress update

* Fix crash when select editing text in preview screen

* Use Orca checkbox style

* Fix issue that toolbar icons are kept regenerated

* Emboss: Fix text & icon alignment

* SVG: Fix text & icon alignment

* Emboss: fix toolbar icon mouse hover state

* Add a simple subtle outline effect by drawing back faces using wireframe mode

* Disable selection outlines

* Show outline in white if the model color is too dark

* Make the outline algorithm more reliable

* Enable cull face, which fix render on Linux

* Fix `disable_cullface`

* Post merge fix

* Optimize selection rendering

* Fix scale gizmo

* Emboss: Fix text rotation if base object is scaled

* Fix volume synchronize

* Fix emboss rotation

* Emboss: Fix advance toggle

* Fix text position after reopened the project

* Make font style preview darker

* Make font style preview selector height shorter

---------

Co-authored-by: tamasmeszaros <meszaros.q@gmail.com>
Co-authored-by: ocornut <omarcornut@gmail.com>
Co-authored-by: SoftFever <softfeverever@gmail.com>
2023-12-09 22:46:18 +08:00

320 lines
13 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

///|/ Copyright (c) Prusa Research 2020 - 2023 Tomáš Mészáros @tamasmeszaros
///|/
///|/ PrusaSlicer is released under the terms of the AGPLv3 or higher
///|/
#include "FillBedJob.hpp"
#include "libslic3r/Model.hpp"
#include "libslic3r/ClipperUtils.hpp"
#include "libslic3r/ModelArrange.hpp"
#include "slic3r/GUI/Plater.hpp"
#include "slic3r/GUI/GLCanvas3D.hpp"
#include "slic3r/GUI/GUI_ObjectList.hpp"
#include "libnest2d/common.hpp"
#include <numeric>
namespace Slic3r {
namespace GUI {
//BBS: add partplate related logic
void FillBedJob::prepare()
{
PartPlateList& plate_list = m_plater->get_partplate_list();
m_locked.clear();
m_selected.clear();
m_unselected.clear();
m_bedpts.clear();
params = init_arrange_params(m_plater);
m_object_idx = m_plater->get_selected_object_idx();
if (m_object_idx == -1)
return;
//select current plate at first
int sel_id = m_plater->get_selection().get_instance_idx();
sel_id = std::max(sel_id, 0);
int sel_ret = plate_list.select_plate_by_obj(m_object_idx, sel_id);
BOOST_LOG_TRIVIAL(debug) << __FUNCTION__ << boost::format(":select plate obj_id %1%, ins_id %2%, ret %3%}") % m_object_idx % sel_id % sel_ret;
PartPlate* plate = plate_list.get_curr_plate();
Model& model = m_plater->model();
BoundingBox plate_bb = plate->get_bounding_box_crd();
int plate_cols = plate_list.get_plate_cols();
int cur_plate_index = plate->get_index();
ModelObject *model_object = m_plater->model().objects[m_object_idx];
if (model_object->instances.empty()) return;
const Slic3r::DynamicPrintConfig& global_config = wxGetApp().preset_bundle->full_config();
m_selected.reserve(model_object->instances.size());
for (size_t oidx = 0; oidx < model.objects.size(); ++oidx)
{
ModelObject* mo = model.objects[oidx];
for (size_t inst_idx = 0; inst_idx < mo->instances.size(); ++inst_idx)
{
bool selected = (oidx == m_object_idx);
ArrangePolygon ap = get_instance_arrange_poly(mo->instances[inst_idx], global_config);
BoundingBox ap_bb = ap.transformed_poly().contour.bounding_box();
ap.height = 1;
ap.name = mo->name;
if (selected)
{
if (mo->instances[inst_idx]->printable)
{
++ap.priority;
ap.itemid = m_selected.size();
m_selected.emplace_back(ap);
}
else
{
if (plate_bb.contains(ap_bb))
{
ap.bed_idx = 0;
ap.itemid = m_unselected.size();
ap.row = cur_plate_index / plate_cols;
ap.col = cur_plate_index % plate_cols;
ap.translation(X) -= bed_stride_x(m_plater) * ap.col;
ap.translation(Y) += bed_stride_y(m_plater) * ap.row;
m_unselected.emplace_back(ap);
}
else
{
ap.bed_idx = PartPlateList::MAX_PLATES_COUNT;
ap.itemid = m_locked.size();
m_locked.emplace_back(ap);
}
}
}
else
{
if (plate_bb.contains(ap_bb))
{
ap.bed_idx = 0;
ap.itemid = m_unselected.size();
ap.row = cur_plate_index / plate_cols;
ap.col = cur_plate_index % plate_cols;
ap.translation(X) -= bed_stride_x(m_plater) * ap.col;
ap.translation(Y) += bed_stride_y(m_plater) * ap.row;
m_unselected.emplace_back(ap);
}
else
{
ap.bed_idx = PartPlateList::MAX_PLATES_COUNT;
ap.itemid = m_locked.size();
m_locked.emplace_back(ap);
}
}
}
}
/*
for (ModelInstance *inst : model_object->instances)
if (inst->printable) {
ArrangePolygon ap = get_arrange_poly(inst);
// Existing objects need to be included in the result. Only
// the needed amount of object will be added, no more.
++ap.priority;
m_selected.emplace_back(ap);
}*/
if (m_selected.empty()) return;
//add the virtual object into unselect list if has
double scaled_exclusion_gap = scale_(1);
plate_list.preprocess_exclude_areas(params.excluded_regions, 1, scaled_exclusion_gap);
plate_list.preprocess_exclude_areas(m_unselected);
m_bedpts = get_bed_shape(*m_plater->config());
auto &objects = m_plater->model().objects;
/*BoundingBox bedbb = get_extents(m_bedpts);
for (size_t idx = 0; idx < objects.size(); ++idx)
if (int(idx) != m_object_idx)
for (ModelInstance *mi : objects[idx]->instances) {
ArrangePolygon ap = get_arrange_poly(mi);
auto ap_bb = ap.transformed_poly().contour.bounding_box();
if (ap.bed_idx == 0 && !bedbb.contains(ap_bb))
ap.bed_idx = arrangement::UNARRANGED;
m_unselected.emplace_back(ap);
}*/
if (auto wt = get_wipe_tower_arrangepoly(*m_plater))
m_unselected.emplace_back(std::move(*wt));
double sc = scaled<double>(1.) * scaled(1.);
const GLCanvas3D::ArrangeSettings& settings = static_cast<const GLCanvas3D*>(m_plater->canvas3D())->get_arrange_settings();
auto polys = offset_ex(m_selected.front().poly, scaled(settings.distance) / 2);
ExPolygon poly = polys.empty() ? m_selected.front().poly : polys.front();
double poly_area = poly.area() / sc;
double unsel_area = std::accumulate(m_unselected.begin(),
m_unselected.end(), 0.,
[cur_plate_index](double s, const auto &ap) {
//BBS: m_unselected instance is in the same partplate
return s + (ap.bed_idx == cur_plate_index) * ap.poly.area();
//return s + (ap.bed_idx == 0) * ap.poly.area();
}) / sc;
double fixed_area = unsel_area + m_selected.size() * poly_area;
double bed_area = Polygon{m_bedpts}.area() / sc;
// This is the maximum number of items, the real number will always be close but less.
int needed_items = (bed_area - fixed_area) / poly_area;
//int sel_id = m_plater->get_selection().get_instance_idx();
// if the selection is not a single instance, choose the first as template
//sel_id = std::max(sel_id, 0);
ModelInstance *mi = model_object->instances[sel_id];
ArrangePolygon template_ap = get_instance_arrange_poly(mi, global_config);
for (int i = 0; i < needed_items; ++i) {
ArrangePolygon ap = template_ap;
ap.poly = m_selected.front().poly;
ap.bed_idx = PartPlateList::MAX_PLATES_COUNT;
ap.height = 1;
ap.itemid = -1;
ap.setter = [this, mi](const ArrangePolygon &p) {
ModelObject *mo = m_plater->model().objects[m_object_idx];
ModelObject* newObj = m_plater->model().add_object(*mo);
newObj->name = mo->name +" "+ std::to_string(p.itemid);
for (ModelInstance *newInst : newObj->instances) { newInst->apply_arrange_result(p.translation.cast<double>(), p.rotation); }
//m_plater->sidebar().obj_list()->paste_objects_into_list({m_plater->model().objects.size()-1});
};
m_selected.emplace_back(ap);
}
m_status_range = m_selected.size();
// The strides have to be removed from the fixed items. For the
// arrangeable (selected) items bed_idx is ignored and the
// translation is irrelevant.
//BBS: remove logic for unselected object
/*double stride = bed_stride(m_plater);
for (auto &p : m_unselected)
if (p.bed_idx > 0)
p.translation(X) -= p.bed_idx * stride;*/
}
void FillBedJob::process(Ctl &ctl)
{
auto statustxt = _u8L("Filling");
ctl.call_on_main_thread([this] { prepare(); }).wait();
ctl.update_status(0, statustxt);
if (m_object_idx == -1 || m_selected.empty()) return;
update_arrange_params(params, m_plater->config(), m_selected);
m_bedpts = get_shrink_bedpts(m_plater->config(), params);
auto &partplate_list = m_plater->get_partplate_list();
auto &print = wxGetApp().plater()->get_partplate_list().get_current_fff_print();
const Slic3r::DynamicPrintConfig& global_config = wxGetApp().preset_bundle->full_config();
PresetBundle* preset_bundle = wxGetApp().preset_bundle;
const bool is_bbl = wxGetApp().preset_bundle->is_bbl_vendor();
if (is_bbl && params.avoid_extrusion_cali_region && global_config.opt_bool("scan_first_layer"))
partplate_list.preprocess_nonprefered_areas(m_unselected, MAX_NUM_PLATES);
update_selected_items_inflation(m_selected, m_plater->config(), params);
update_unselected_items_inflation(m_unselected, m_plater->config(), params);
bool do_stop = false;
params.stopcondition = [&ctl, &do_stop]() {
return ctl.was_canceled() || do_stop;
};
params.progressind = [this, &ctl, &statustxt](unsigned st,std::string str="") {
if (st > 0)
ctl.update_status(st * 100 / status_range(), statustxt + " " + str);
};
params.on_packed = [&do_stop] (const ArrangePolygon &ap) {
do_stop = ap.bed_idx > 0 && ap.priority == 0;
};
// final align用的是凸包在有fixed item的情况下可能找到的参考点位置是错的这里就不做了。见STUDIO-3265
params.do_final_align = !is_bbl;
arrangement::arrange(m_selected, m_unselected, m_bedpts, params);
// finalize just here.
ctl.update_status(100, ctl.was_canceled() ?
_u8L("Bed filling canceled.") :
_u8L("Bed filling done."));
}
FillBedJob::FillBedJob() : m_plater{wxGetApp().plater()} {}
void FillBedJob::finalize(bool canceled, std::exception_ptr &eptr)
{
// Ignore the arrange result if aborted.
if (canceled || eptr)
return;
if (m_object_idx == -1) return;
ModelObject *model_object = m_plater->model().objects[m_object_idx];
if (model_object->instances.empty()) return;
//BBS: partplate
PartPlateList& plate_list = m_plater->get_partplate_list();
int plate_cols = plate_list.get_plate_cols();
int cur_plate = plate_list.get_curr_plate_index();
size_t inst_cnt = model_object->instances.size();
int added_cnt = std::accumulate(m_selected.begin(), m_selected.end(), 0, [](int s, auto &ap) {
return s + int(ap.priority == 0 && ap.bed_idx == 0);
});
int oldSize = m_plater->model().objects.size();
if (added_cnt > 0) {
//BBS: adjust the selected instances
for (ArrangePolygon& ap : m_selected) {
if (ap.bed_idx != 0) {
BOOST_LOG_TRIVIAL(debug) << __FUNCTION__ << boost::format(":skipped: bed_id %1%, trans {%2%,%3%}") % ap.bed_idx % unscale<double>(ap.translation(X)) % unscale<double>(ap.translation(Y));
/*if (ap.itemid == -1)*/
continue;
ap.bed_idx = plate_list.get_plate_count();
}
else
ap.bed_idx = cur_plate;
ap.row = ap.bed_idx / plate_cols;
ap.col = ap.bed_idx % plate_cols;
ap.translation(X) += bed_stride_x(m_plater) * ap.col;
ap.translation(Y) -= bed_stride_y(m_plater) * ap.row;
ap.apply();
BOOST_LOG_TRIVIAL(debug) << __FUNCTION__ << boost::format(":selected: bed_id %1%, trans {%2%,%3%}") % ap.bed_idx % unscale<double>(ap.translation(X)) % unscale<double>(ap.translation(Y));
}
int newSize = m_plater->model().objects.size();
auto obj_list = m_plater->sidebar().obj_list();
for (size_t i = oldSize; i < newSize; i++) {
obj_list->add_object_to_list(i, true, true, false);
obj_list->update_printable_state(i, 0);
}
BOOST_LOG_TRIVIAL(debug) << __FUNCTION__ << ": paste_objects_into_list";
/*for (ArrangePolygon& ap : m_selected) {
if (ap.bed_idx != arrangement::UNARRANGED && (ap.priority != 0 || ap.bed_idx == 0))
ap.apply();
}*/
//model_object->ensure_on_bed();
//BOOST_LOG_TRIVIAL(debug) << __FUNCTION__ << ": model_object->ensure_on_bed()";
m_plater->update();
}
}
}} // namespace Slic3r::GUI