Save/load printable flag to/from 3mf and amf

This commit is contained in:
YuSanka 2019-08-06 14:54:38 +02:00
parent 0520da3241
commit 8a2362587d
3 changed files with 39 additions and 10 deletions

View File

@ -71,6 +71,7 @@ const char* V2_ATTR = "v2";
const char* V3_ATTR = "v3"; const char* V3_ATTR = "v3";
const char* OBJECTID_ATTR = "objectid"; const char* OBJECTID_ATTR = "objectid";
const char* TRANSFORM_ATTR = "transform"; const char* TRANSFORM_ATTR = "transform";
const char* PRINTABLE_ATTR = "printable";
const char* KEY_ATTR = "key"; const char* KEY_ATTR = "key";
const char* VALUE_ATTR = "value"; const char* VALUE_ATTR = "value";
@ -131,6 +132,12 @@ int get_attribute_value_int(const char** attributes, unsigned int attributes_siz
return (text != nullptr) ? ::atoi(text) : 0; return (text != nullptr) ? ::atoi(text) : 0;
} }
bool get_attribute_value_bool(const char** attributes, unsigned int attributes_size, const char* attribute_key)
{
const char* text = get_attribute_value_charptr(attributes, attributes_size, attribute_key);
return (text != nullptr) ? (bool)::atoi(text) : true;
}
Slic3r::Transform3d get_transform_from_string(const std::string& mat_str) Slic3r::Transform3d get_transform_from_string(const std::string& mat_str)
{ {
if (mat_str.empty()) if (mat_str.empty())
@ -428,7 +435,7 @@ namespace Slic3r {
bool _handle_start_metadata(const char** attributes, unsigned int num_attributes); bool _handle_start_metadata(const char** attributes, unsigned int num_attributes);
bool _handle_end_metadata(); bool _handle_end_metadata();
bool _create_object_instance(int object_id, const Transform3d& transform, unsigned int recur_counter); bool _create_object_instance(int object_id, const Transform3d& transform, const bool printable, unsigned int recur_counter);
void _apply_transform(ModelInstance& instance, const Transform3d& transform); void _apply_transform(ModelInstance& instance, const Transform3d& transform);
@ -1367,8 +1374,9 @@ namespace Slic3r {
int object_id = get_attribute_value_int(attributes, num_attributes, OBJECTID_ATTR); int object_id = get_attribute_value_int(attributes, num_attributes, OBJECTID_ATTR);
Transform3d transform = get_transform_from_string(get_attribute_value_string(attributes, num_attributes, TRANSFORM_ATTR)); Transform3d transform = get_transform_from_string(get_attribute_value_string(attributes, num_attributes, TRANSFORM_ATTR));
int printable = get_attribute_value_bool(attributes, num_attributes, PRINTABLE_ATTR);
return _create_object_instance(object_id, transform, 1); return _create_object_instance(object_id, transform, printable, 1);
} }
bool _3MF_Importer::_handle_end_item() bool _3MF_Importer::_handle_end_item()
@ -1396,7 +1404,7 @@ namespace Slic3r {
return true; return true;
} }
bool _3MF_Importer::_create_object_instance(int object_id, const Transform3d& transform, unsigned int recur_counter) bool _3MF_Importer::_create_object_instance(int object_id, const Transform3d& transform, const bool printable, unsigned int recur_counter)
{ {
static const unsigned int MAX_RECURSIONS = 10; static const unsigned int MAX_RECURSIONS = 10;
@ -1432,6 +1440,7 @@ namespace Slic3r {
add_error("Unable to add object instance"); add_error("Unable to add object instance");
return false; return false;
} }
instance->printable = printable;
m_instances.emplace_back(instance, transform); m_instances.emplace_back(instance, transform);
} }
@ -1441,7 +1450,7 @@ namespace Slic3r {
// recursively process nested components // recursively process nested components
for (const Component& component : it->second) for (const Component& component : it->second)
{ {
if (!_create_object_instance(component.object_id, transform * component.transform, recur_counter + 1)) if (!_create_object_instance(component.object_id, transform * component.transform, printable, recur_counter + 1))
return false; return false;
} }
} }
@ -1655,10 +1664,12 @@ namespace Slic3r {
{ {
unsigned int id; unsigned int id;
Transform3d transform; Transform3d transform;
bool printable;
BuildItem(unsigned int id, const Transform3d& transform) BuildItem(unsigned int id, const Transform3d& transform, const bool printable)
: id(id) : id(id)
, transform(transform) , transform(transform)
, printable(printable)
{ {
} }
}; };
@ -1951,7 +1962,7 @@ namespace Slic3r {
Transform3d t = instance->get_matrix(); Transform3d t = instance->get_matrix();
// instance_id is just a 1 indexed index in build_items. // instance_id is just a 1 indexed index in build_items.
assert(instance_id == build_items.size() + 1); assert(instance_id == build_items.size() + 1);
build_items.emplace_back(instance_id, t); build_items.emplace_back(instance_id, t, instance->printable);
stream << " </" << OBJECT_TAG << ">\n"; stream << " </" << OBJECT_TAG << ">\n";
@ -2059,7 +2070,7 @@ namespace Slic3r {
stream << " "; stream << " ";
} }
} }
stream << "\" />\n"; stream << "\" printable =\"" << item.printable << "\" />\n";
} }
stream << " </" << BUILD_TAG << ">\n"; stream << " </" << BUILD_TAG << ">\n";

View File

@ -137,6 +137,7 @@ struct AMFParserContext
NODE_TYPE_MIRRORX, // amf/constellation/instance/mirrorx NODE_TYPE_MIRRORX, // amf/constellation/instance/mirrorx
NODE_TYPE_MIRRORY, // amf/constellation/instance/mirrory NODE_TYPE_MIRRORY, // amf/constellation/instance/mirrory
NODE_TYPE_MIRRORZ, // amf/constellation/instance/mirrorz NODE_TYPE_MIRRORZ, // amf/constellation/instance/mirrorz
NODE_TYPE_PRINTABLE, // amf/constellation/instance/mirrorz
NODE_TYPE_METADATA, // anywhere under amf/*/metadata NODE_TYPE_METADATA, // anywhere under amf/*/metadata
}; };
@ -145,7 +146,8 @@ struct AMFParserContext
: deltax_set(false), deltay_set(false), deltaz_set(false) : deltax_set(false), deltay_set(false), deltaz_set(false)
, rx_set(false), ry_set(false), rz_set(false) , rx_set(false), ry_set(false), rz_set(false)
, scalex_set(false), scaley_set(false), scalez_set(false) , scalex_set(false), scaley_set(false), scalez_set(false)
, mirrorx_set(false), mirrory_set(false), mirrorz_set(false) {} , mirrorx_set(false), mirrory_set(false), mirrorz_set(false)
, printable(true) {}
// Shift in the X axis. // Shift in the X axis.
float deltax; float deltax;
bool deltax_set; bool deltax_set;
@ -178,6 +180,8 @@ struct AMFParserContext
bool mirrory_set; bool mirrory_set;
float mirrorz; float mirrorz;
bool mirrorz_set; bool mirrorz_set;
// printable property
bool printable;
bool anything_set() const { return deltax_set || deltay_set || deltaz_set || bool anything_set() const { return deltax_set || deltay_set || deltaz_set ||
rx_set || ry_set || rz_set || rx_set || ry_set || rz_set ||
@ -321,6 +325,8 @@ void AMFParserContext::startElement(const char *name, const char **atts)
node_type_new = NODE_TYPE_MIRRORY; node_type_new = NODE_TYPE_MIRRORY;
else if (strcmp(name, "mirrorz") == 0) else if (strcmp(name, "mirrorz") == 0)
node_type_new = NODE_TYPE_MIRRORZ; node_type_new = NODE_TYPE_MIRRORZ;
else if (strcmp(name, "printable") == 0)
node_type_new = NODE_TYPE_PRINTABLE;
} }
else if (m_path[2] == NODE_TYPE_LAYER_CONFIG && strcmp(name, "range") == 0) { else if (m_path[2] == NODE_TYPE_LAYER_CONFIG && strcmp(name, "range") == 0) {
assert(m_object); assert(m_object);
@ -397,7 +403,8 @@ void AMFParserContext::characters(const XML_Char *s, int len)
m_path.back() == NODE_TYPE_SCALE || m_path.back() == NODE_TYPE_SCALE ||
m_path.back() == NODE_TYPE_MIRRORX || m_path.back() == NODE_TYPE_MIRRORX ||
m_path.back() == NODE_TYPE_MIRRORY || m_path.back() == NODE_TYPE_MIRRORY ||
m_path.back() == NODE_TYPE_MIRRORZ) m_path.back() == NODE_TYPE_MIRRORZ ||
m_path.back() == NODE_TYPE_PRINTABLE)
m_value[0].append(s, len); m_value[0].append(s, len);
break; break;
case 6: case 6:
@ -507,6 +514,11 @@ void AMFParserContext::endElement(const char * /* name */)
m_instance->mirrorz_set = true; m_instance->mirrorz_set = true;
m_value[0].clear(); m_value[0].clear();
break; break;
case NODE_TYPE_PRINTABLE:
assert(m_instance);
m_instance->printable = bool(atoi(m_value[0].c_str()));
m_value[0].clear();
break;
// Object vertices: // Object vertices:
case NODE_TYPE_VERTEX: case NODE_TYPE_VERTEX:
@ -685,6 +697,7 @@ void AMFParserContext::endDocument()
mi->set_rotation(Vec3d(instance.rx_set ? (double)instance.rx : 0.0, instance.ry_set ? (double)instance.ry : 0.0, instance.rz_set ? (double)instance.rz : 0.0)); mi->set_rotation(Vec3d(instance.rx_set ? (double)instance.rx : 0.0, instance.ry_set ? (double)instance.ry : 0.0, instance.rz_set ? (double)instance.rz : 0.0));
mi->set_scaling_factor(Vec3d(instance.scalex_set ? (double)instance.scalex : 1.0, instance.scaley_set ? (double)instance.scaley : 1.0, instance.scalez_set ? (double)instance.scalez : 1.0)); mi->set_scaling_factor(Vec3d(instance.scalex_set ? (double)instance.scalex : 1.0, instance.scaley_set ? (double)instance.scaley : 1.0, instance.scalez_set ? (double)instance.scalez : 1.0));
mi->set_mirror(Vec3d(instance.mirrorx_set ? (double)instance.mirrorx : 1.0, instance.mirrory_set ? (double)instance.mirrory : 1.0, instance.mirrorz_set ? (double)instance.mirrorz : 1.0)); mi->set_mirror(Vec3d(instance.mirrorx_set ? (double)instance.mirrorx : 1.0, instance.mirrory_set ? (double)instance.mirrory : 1.0, instance.mirrorz_set ? (double)instance.mirrorz : 1.0));
mi->printable = instance.printable;
} }
} }
} }
@ -1037,6 +1050,7 @@ bool store_amf(const char *path, Model *model, const DynamicPrintConfig *config)
" <mirrorx>%lf</mirrorx>\n" " <mirrorx>%lf</mirrorx>\n"
" <mirrory>%lf</mirrory>\n" " <mirrory>%lf</mirrory>\n"
" <mirrorz>%lf</mirrorz>\n" " <mirrorz>%lf</mirrorz>\n"
" <printable>%d</printable>\n"
" </instance>\n", " </instance>\n",
object_id, object_id,
instance->get_offset(X), instance->get_offset(X),
@ -1050,7 +1064,8 @@ bool store_amf(const char *path, Model *model, const DynamicPrintConfig *config)
instance->get_scaling_factor(Z), instance->get_scaling_factor(Z),
instance->get_mirror(X), instance->get_mirror(X),
instance->get_mirror(Y), instance->get_mirror(Y),
instance->get_mirror(Z)); instance->get_mirror(Z),
instance->printable);
//FIXME missing instance->scaling_factor //FIXME missing instance->scaling_factor
instances.append(buf); instances.append(buf);

View File

@ -2326,6 +2326,9 @@ std::vector<size_t> Plater::priv::load_files(const std::vector<fs::path>& input_
// automatic selection of added objects // automatic selection of added objects
if (!obj_idxs.empty() && (view3D != nullptr)) if (!obj_idxs.empty() && (view3D != nullptr))
{ {
// update printable state for new volumes on canvas3D
wxGetApp().plater()->canvas3D()->update_instance_printable_state_for_objects(obj_idxs);
Selection& selection = view3D->get_canvas3d()->get_selection(); Selection& selection = view3D->get_canvas3d()->get_selection();
selection.clear(); selection.clear();
for (size_t idx : obj_idxs) for (size_t idx : obj_idxs)