Implemented "from/to imperial units conversation" for loaded objects and volumes

This commit is contained in:
YuSanka 2020-05-20 20:07:31 +02:00
parent 0b60d45cf3
commit 8afd273c4b
6 changed files with 131 additions and 2 deletions

View File

@ -991,6 +991,56 @@ void ModelObject::scale_mesh_after_creation(const Vec3d &versor)
this->invalidate_bounding_box();
}
void ModelObject::convert_units(ModelObjectPtrs& new_objects, bool from_imperial, std::vector<int> volume_idxs)
{
BOOST_LOG_TRIVIAL(trace) << "ModelObject::convert_units - start";
ModelObject* new_object = new_clone(*this);
double koef = from_imperial ? 25.4 : 0.0393700787;
const Vec3d versor = Vec3d(koef, koef, koef);
new_object->set_model(nullptr);
new_object->sla_support_points.clear();
new_object->sla_drain_holes.clear();
new_object->sla_points_status = sla::PointsStatus::NoPoints;
new_object->clear_volumes();
new_object->input_file.clear();
int vol_idx = 0;
for (ModelVolume* volume : volumes)
{
volume->m_supported_facets.clear();
if (!volume->mesh().empty()) {
TriangleMesh mesh(volume->mesh());
mesh.require_shared_vertices();
ModelVolume* vol = new_object->add_volume(mesh);
vol->name = volume->name;
// Don't copy the config's ID.
static_cast<DynamicPrintConfig&>(vol->config) = static_cast<const DynamicPrintConfig&>(volume->config);
assert(vol->config.id().valid());
assert(vol->config.id() != volume->config.id());
vol->set_material(volume->material_id(), *volume->material());
// Perform conversion
if (volume_idxs.empty() ||
std::find(volume_idxs.begin(), volume_idxs.end(), vol_idx) != volume_idxs.end()) {
vol->scale_geometry_after_creation(versor);
vol->set_offset(versor.cwiseProduct(vol->get_offset()));
}
else
vol->set_offset(volume->get_offset());
}
vol_idx ++;
}
new_object->invalidate_bounding_box();
new_objects.push_back(new_object);
BOOST_LOG_TRIVIAL(trace) << "ModelObject::convert_units - end";
}
size_t ModelObject::materials_count() const
{
std::set<t_model_material_id> material_ids;

View File

@ -281,6 +281,7 @@ public:
// This method could only be called before the meshes of this ModelVolumes are not shared!
void scale_mesh_after_creation(const Vec3d& versor);
void convert_units(ModelObjectPtrs&new_objects, bool from_imperial, std::vector<int> volume_idxs);
size_t materials_count() const;
size_t facets_count() const;

View File

@ -333,6 +333,34 @@ void ObjectList::get_selected_item_indexes(int& obj_idx, int& vol_idx, const wxD
vol_idx = type & itVolume ? m_objects_model->GetVolumeIdByItem(item) : -1;
}
void ObjectList::get_selection_indexes(std::vector<int>& obj_idxs, std::vector<int>& vol_idxs)
{
wxDataViewItemArray sels;
GetSelections(sels);
assert(!sels.IsEmpty());
if (m_objects_model->GetItemType(sels[0]) & itVolume) {
for (wxDataViewItem item : sels) {
obj_idxs.emplace_back(m_objects_model->GetIdByItem(m_objects_model->GetTopParent(item)));
assert(m_objects_model->GetItemType(item) & itVolume);
vol_idxs.emplace_back(m_objects_model->GetVolumeIdByItem(item));
}
}
else {
for (wxDataViewItem item : sels) {
const ItemType type = m_objects_model->GetItemType(item);
assert(type & itObject | itInstance | itInstanceRoot);
obj_idxs.emplace_back(type & itObject ? m_objects_model->GetIdByItem(item) :
m_objects_model->GetIdByItem(m_objects_model->GetTopParent(item)));
}
}
std::sort(obj_idxs.begin(), obj_idxs.end(), std::greater<int>());
obj_idxs.erase(std::unique(obj_idxs.begin(), obj_idxs.end()), obj_idxs.end());
}
int ObjectList::get_mesh_errors_count(const int obj_idx, const int vol_idx /*= -1*/) const
{
if (obj_idx < 0)
@ -1744,6 +1772,15 @@ void ObjectList::append_menu_item_scale_selection_to_fit_print_volume(wxMenu* me
[](wxCommandEvent&) { wxGetApp().plater()->scale_selection_to_fit_print_volume(); }, "", menu);
}
void ObjectList::append_menu_items_convert_unit(wxMenu* menu)
{
append_menu_item(menu, wxID_ANY, _L("Convert from imperial unit"), _L("Convert from imperial unit"),
[](wxCommandEvent&) { wxGetApp().plater()->convert_unit(true); }, "", menu);
append_menu_item(menu, wxID_ANY, _L("Convert to imperial unit"), _L("Convert to imperial unit"),
[](wxCommandEvent&) { wxGetApp().plater()->convert_unit(false); }, "", menu);
}
void ObjectList::create_object_popupmenu(wxMenu *menu)
{
#ifdef __WXOSX__
@ -1751,6 +1788,7 @@ void ObjectList::create_object_popupmenu(wxMenu *menu)
#endif // __WXOSX__
append_menu_item_reload_from_disk(menu);
append_menu_items_convert_unit(menu);
append_menu_item_export_stl(menu);
append_menu_item_fix_through_netfabb(menu);
append_menu_item_scale_selection_to_fit_print_volume(menu);
@ -1775,6 +1813,7 @@ void ObjectList::create_sla_object_popupmenu(wxMenu *menu)
#endif // __WXOSX__
append_menu_item_reload_from_disk(menu);
append_menu_items_convert_unit(menu);
append_menu_item_export_stl(menu);
append_menu_item_fix_through_netfabb(menu);
// rest of a object_sla_menu will be added later in:
@ -1788,6 +1827,7 @@ void ObjectList::create_part_popupmenu(wxMenu *menu)
#endif // __WXOSX__
append_menu_item_reload_from_disk(menu);
append_menu_items_convert_unit(menu);
append_menu_item_export_stl(menu);
append_menu_item_fix_through_netfabb(menu);
@ -4050,6 +4090,8 @@ void ObjectList::show_multi_selection_menu()
return wxGetApp().plater()->can_reload_from_disk();
}, wxGetApp().plater());
append_menu_items_convert_unit(menu);
wxGetApp().plater()->PopupMenu(menu);
}

View File

@ -210,6 +210,7 @@ public:
// Get obj_idx and vol_idx values for the selected (by default) or an adjusted item
void get_selected_item_indexes(int& obj_idx, int& vol_idx, const wxDataViewItem& item = wxDataViewItem(0));
void get_selection_indexes(std::vector<int>& obj_idxs, std::vector<int>& vol_idxs);
// Get count of errors in the mesh
int get_mesh_errors_count(const int obj_idx, const int vol_idx = -1) const;
/* Get list of errors in the mesh. Return value is a string, used for the tooltip
@ -252,6 +253,7 @@ public:
void append_menu_item_change_extruder(wxMenu* menu);
void append_menu_item_delete(wxMenu* menu);
void append_menu_item_scale_selection_to_fit_print_volume(wxMenu* menu);
void append_menu_items_convert_unit(wxMenu* menu);
void create_object_popupmenu(wxMenu *menu);
void create_sla_object_popupmenu(wxMenu*menu);
void create_part_popupmenu(wxMenu*menu);

View File

@ -3724,6 +3724,9 @@ bool Plater::priv::init_common_menu(wxMenu* menu, const bool is_part/* = false*/
menu->AppendSeparator();
// "Scale to print volume" makes a sense just for whole object
sidebar->obj_list()->append_menu_item_scale_selection_to_fit_print_volume(menu);
q->Bind(wxEVT_UPDATE_UI, [this](wxUpdateUIEvent& evt) {
const Selection& selection = get_selection();
int instance_idx = selection.get_instance_idx();
@ -3736,10 +3739,9 @@ bool Plater::priv::init_common_menu(wxMenu* menu, const bool is_part/* = false*/
}, menu_item_printable->GetId());
}
sidebar->obj_list()->append_menu_items_convert_unit(menu);
sidebar->obj_list()->append_menu_item_fix_through_netfabb(menu);
sidebar->obj_list()->append_menu_item_scale_selection_to_fit_print_volume(menu);
wxMenu* mirror_menu = new wxMenu();
if (mirror_menu == nullptr)
return false;
@ -4565,6 +4567,37 @@ void Plater::scale_selection_to_fit_print_volume()
p->scale_selection_to_fit_print_volume();
}
void Plater::convert_unit(bool from_imperial_unit)
{
std::vector<int> obj_idxs, volume_idxs;
wxGetApp().obj_list()->get_selection_indexes(obj_idxs, volume_idxs);
if (obj_idxs.empty() && volume_idxs.empty())
return;
TakeSnapshot snapshot(this, from_imperial_unit ? _L("Convert from imperial units") : _L("Convert to imperial units"));
wxBusyCursor wait;
ModelObjectPtrs objects;
for (int obj_idx : obj_idxs) {
ModelObject *object = p->model.objects[obj_idx];
object->convert_units(objects, from_imperial_unit, volume_idxs);
remove(obj_idx);
}
p->load_model_objects(objects);
Selection& selection = p->view3D->get_canvas3d()->get_selection();
size_t last_obj_idx = p->model.objects.size() - 1;
if (volume_idxs.empty()) {
for (size_t i = 0; i < objects.size(); ++i)
selection.add_object((unsigned int)(last_obj_idx - i), i == 0);
}
else {
for (int vol_idx : volume_idxs)
selection.add_volume(last_obj_idx, vol_idx, 0, false);
}
}
void Plater::cut(size_t obj_idx, size_t instance_idx, coordf_t z, bool keep_upper, bool keep_lower, bool rotate_lower)
{
wxCHECK_RET(obj_idx < p->model.objects.size(), "obj_idx out of bounds");

View File

@ -212,6 +212,7 @@ public:
void set_number_of_copies(/*size_t num*/);
bool is_selection_empty() const;
void scale_selection_to_fit_print_volume();
void convert_unit(bool from_imperial_unit);
void cut(size_t obj_idx, size_t instance_idx, coordf_t z, bool keep_upper = true, bool keep_lower = true, bool rotate_lower = false);