Label objects refactoring (enum->enum class, initial header export separated in a dedicated function)

This commit is contained in:
Lukas Matena 2023-09-05 15:43:13 +02:00
parent 6d41a76af7
commit 5d50a91c30
3 changed files with 32 additions and 23 deletions

View File

@ -112,13 +112,13 @@ namespace Slic3r {
unique_id += instance_id; unique_id += instance_id;
std::string name = objects[object_id]->model_object()->name; std::string name = objects[object_id]->model_object()->name;
if (label_object_style == loMarlin && objects[object_id]->model_object()->instances.size() > 1u) if (label_object_style == LabelObjects::Marlin && objects[object_id]->model_object()->instances.size() > 1u)
name += " (copy " + std::to_string(instance_id) + ")"; name += " (copy " + std::to_string(instance_id) + ")";
std::string out; std::string out;
if (label_object_style == loOctoprint) if (label_object_style == LabelObjects::Octoprint)
out += std::string("; printing object ") + name + " id:" + std::to_string(object_id) + " copy " + std::to_string(instance_id) + "\n"; out += std::string("; printing object ") + name + " id:" + std::to_string(object_id) + " copy " + std::to_string(instance_id) + "\n";
else if (label_object_style == loMarlin) { else if (label_object_style == LabelObjects::Marlin) {
out += std::string("M486 S") + std::to_string(unique_id) + "\n"; out += std::string("M486 S") + std::to_string(unique_id) + "\n";
out += std::string("M486 N") + name + "\n"; out += std::string("M486 N") + name + "\n";
} }
@ -129,14 +129,32 @@ namespace Slic3r {
static std::string label_object_stop(LabelObjects label_object_style, int object_id, int instance_id, const std::string& name) static std::string label_object_stop(LabelObjects label_object_style, int object_id, int instance_id, const std::string& name)
{ {
std::string out; std::string out;
if (label_object_style == loOctoprint) if (label_object_style == LabelObjects::Octoprint)
out += std::string("; stop printing object ") + name + " id:" + std::to_string(object_id) + " copy " + std::to_string(instance_id) + "\n"; out += std::string("; stop printing object ") + name + " id:" + std::to_string(object_id) + " copy " + std::to_string(instance_id) + "\n";
else if (label_object_style == loMarlin) else if (label_object_style == LabelObjects::Marlin)
out += std::string("M486 S-1\n"); out += std::string("M486 S-1\n");
return out; return out;
} }
static std::string label_all_objects(LabelObjects label_objects_style, const Print& print)
{
std::string out;
if (label_objects_style != LabelObjects::Disabled) {
out += "\n";
for (size_t object_idx = 0; object_idx < print.objects().size(); ++object_idx) {
for (size_t inst_idx = 0; inst_idx < print.objects()[object_idx]->model_object()->instances.size(); ++inst_idx) {
out += label_object_start(label_objects_style, print.objects(), object_idx, inst_idx);
out += label_object_stop(label_objects_style, object_idx, inst_idx, print.objects()[object_idx]->model_object()->name);
}
}
out += "\n";
}
return out;
}
// Return true if tch_prefix is found in custom_gcode // Return true if tch_prefix is found in custom_gcode
static bool custom_gcode_changes_tool(const std::string& custom_gcode, const std::string& tch_prefix, unsigned next_extruder) static bool custom_gcode_changes_tool(const std::string& custom_gcode, const std::string& tch_prefix, unsigned next_extruder)
@ -1251,16 +1269,7 @@ void GCodeGenerator::_do_export(Print& print, GCodeOutputStream &file, Thumbnail
file.write(this->preamble()); file.write(this->preamble());
// Label all objects so printer knows about them since the start. // Label all objects so printer knows about them since the start.
if (config().gcode_label_objects != loDisabled) { file.write(label_all_objects(config().gcode_label_objects, print));
file.write("\n");
for (size_t object_idx = 0; object_idx < print.objects().size(); ++object_idx) {
for (size_t inst_idx = 0; inst_idx < print.objects()[object_idx]->model_object()->instances.size(); ++inst_idx) {
file.write(label_object_start(config().gcode_label_objects, print.objects(), object_idx, inst_idx));
file.write(label_object_stop(config().gcode_label_objects, object_idx, inst_idx, print.objects()[object_idx]->model_object()->name));
}
}
file.write("\n");
}
print.throw_if_canceled(); print.throw_if_canceled();
@ -2401,7 +2410,7 @@ void GCodeGenerator::process_layer_single_object(
m_avoid_crossing_perimeters.use_external_mp_once(); m_avoid_crossing_perimeters.use_external_mp_once();
m_last_obj_copy = this_object_copy; m_last_obj_copy = this_object_copy;
this->set_origin(unscale(offset)); this->set_origin(unscale(offset));
if (this->config().gcode_label_objects != loDisabled) { if (this->config().gcode_label_objects != LabelObjects::Disabled) {
for (const PrintObject* po : print_object.print()->objects()) { for (const PrintObject* po : print_object.print()->objects()) {
if (po == &print_object) if (po == &print_object)
break; break;
@ -2585,7 +2594,7 @@ void GCodeGenerator::process_layer_single_object(
} }
} }
} }
if (! first && config().gcode_label_objects != loDisabled) if (! first && config().gcode_label_objects != LabelObjects::Disabled)
gcode += label_object_stop(config().gcode_label_objects, object_id, print_instance.instance_id, print_object.model_object()->name); gcode += label_object_stop(config().gcode_label_objects, object_id, print_instance.instance_id, print_object.model_object()->name);
} }

View File

@ -230,9 +230,9 @@ static const t_config_enum_values s_keys_map_DraftShield = {
CONFIG_OPTION_ENUM_DEFINE_STATIC_MAPS(DraftShield) CONFIG_OPTION_ENUM_DEFINE_STATIC_MAPS(DraftShield)
static const t_config_enum_values s_keys_map_LabelObjects = { static const t_config_enum_values s_keys_map_LabelObjects = {
{ "disabled", loDisabled }, { "disabled", int(LabelObjects::Disabled) },
{ "octoprint", loOctoprint }, { "octoprint", int(LabelObjects::Octoprint) },
{ "marlin", loMarlin } { "marlin", int(LabelObjects::Marlin) }
}; };
CONFIG_OPTION_ENUM_DEFINE_STATIC_MAPS(LabelObjects) CONFIG_OPTION_ENUM_DEFINE_STATIC_MAPS(LabelObjects)
@ -1511,7 +1511,7 @@ void PrintConfigDef::init_fff_params()
{ "marlin", L("Marlin (M486)") } { "marlin", L("Marlin (M486)") }
}); });
def->mode = comAdvanced; def->mode = comAdvanced;
def->set_default_value(new ConfigOptionEnum<LabelObjects>(loDisabled)); def->set_default_value(new ConfigOptionEnum<LabelObjects>(LabelObjects::Disabled));
def = this->add("gcode_substitutions", coStrings); def = this->add("gcode_substitutions", coStrings);
def->label = L("G-code substitutions"); def->label = L("G-code substitutions");

View File

@ -147,8 +147,8 @@ enum DraftShield {
dsDisabled, dsLimited, dsEnabled dsDisabled, dsLimited, dsEnabled
}; };
enum LabelObjects { enum class LabelObjects {
loDisabled, loOctoprint, loMarlin Disabled, Octoprint, Marlin
}; };
enum class PerimeterGeneratorType enum class PerimeterGeneratorType