Label objects: more refactoring (name composed only once)

This commit is contained in:
Lukas Matena 2023-09-13 12:32:36 +02:00
parent fe3cf27394
commit 6016c6b021
2 changed files with 22 additions and 21 deletions

View File

@ -30,14 +30,26 @@ void LabelObjects::init(const Print& print)
const ModelObjectPtrs& model_objects = model_object->get_model()->objects; const ModelObjectPtrs& model_objects = model_object->get_model()->objects;
int object_id = int(std::find(model_objects.begin(), model_objects.end(), model_object) - model_objects.begin()); int object_id = int(std::find(model_objects.begin(), model_objects.end(), model_object) - model_objects.begin());
for (const PrintInstance* const pi : print_instances) { for (const PrintInstance* const pi : print_instances) {
bool object_has_more_instances = print_instances.size() > 1u;
int instance_id = int(std::find(model_object->instances.begin(), model_object->instances.end(), pi->model_instance) - model_object->instances.begin()); int instance_id = int(std::find(model_object->instances.begin(), model_object->instances.end(), pi->model_instance) - model_object->instances.begin());
if (m_label_objects_style != LabelObjectsStyle::Octoprint) {
// OctoPrint comments have always indexed instances from 0, let's keep it that way. // Now compose the name of the object and define whether indexing is 0 or 1-based.
// In the other cases, we will use one-based indexing so the indices match with the one in PrusaSlicer. std::string name = model_object->name;
++instance_id; if (m_label_objects_style == LabelObjectsStyle::Octoprint) {
// use zero-based indexing for objects and instances, as we always have done
name += " id:" + std::to_string(object_id) + " copy " + std::to_string(instance_id);
} }
m_label_data.emplace(pi, LabelData{model_object->name, unique_id, object_id, instance_id, print_instances.size() > 1}); else if (m_label_objects_style == LabelObjectsStyle::Firmware) {
++unique_id; // use one-based indexing for objects and instances so indices match what we see in PrusaSlicer.
++object_id;
++instance_id;
if (object_has_more_instances)
name += " (Instance " + std::to_string(instance_id) + ")";
}
m_label_data.emplace(pi, LabelData{name, unique_id});
++unique_id;
} }
} }
} }
@ -63,7 +75,7 @@ std::string LabelObjects::all_objects_header() const
out += stop_object(*print_instance); out += stop_object(*print_instance);
} }
out += "\n"; out += "\n";
return out; return out;
} }
@ -75,19 +87,15 @@ std::string LabelObjects::start_object(const PrintInstance& print_instance, Incl
const LabelData& label = m_label_data.at(&print_instance); const LabelData& label = m_label_data.at(&print_instance);
std::string name = label.name;
if (m_label_objects_style == LabelObjectsStyle::Firmware && label.object_has_more_instances)
name += " (copy " + std::to_string(label.instance_id) + ")";
std::string out; std::string out;
if (m_label_objects_style == LabelObjectsStyle::Octoprint) if (m_label_objects_style == LabelObjectsStyle::Octoprint)
out += std::string("; printing object ") + name + " id:" + std::to_string(label.object_id) + " copy " + std::to_string(label.instance_id) + "\n"; out += std::string("; printing object ") + label.name + "\n";
else if (m_label_objects_style == LabelObjectsStyle::Firmware) { else if (m_label_objects_style == LabelObjectsStyle::Firmware) {
if (m_flavor == GCodeFlavor::gcfMarlinFirmware || m_flavor == GCodeFlavor::gcfMarlinLegacy || m_flavor == GCodeFlavor::gcfRepRapFirmware) { if (m_flavor == GCodeFlavor::gcfMarlinFirmware || m_flavor == GCodeFlavor::gcfMarlinLegacy || m_flavor == GCodeFlavor::gcfRepRapFirmware) {
out += std::string("M486 S") + std::to_string(label.unique_id) + "\n"; out += std::string("M486 S") + std::to_string(label.unique_id) + "\n";
if (include_name == IncludeName::Yes) { if (include_name == IncludeName::Yes) {
out += std::string("M486 A"); out += std::string("M486 A");
out += (m_flavor == GCodeFlavor::gcfRepRapFirmware ? (std::string("\"") + name + "\"") : name) + "\n"; out += (m_flavor == GCodeFlavor::gcfRepRapFirmware ? (std::string("\"") + label.name + "\"") : label.name) + "\n";
} }
} else { } else {
// Not supported by / implemented for the other firmware flavors. // Not supported by / implemented for the other firmware flavors.
@ -107,7 +115,7 @@ std::string LabelObjects::stop_object(const PrintInstance& print_instance) const
std::string out; std::string out;
if (m_label_objects_style == LabelObjectsStyle::Octoprint) if (m_label_objects_style == LabelObjectsStyle::Octoprint)
out += std::string("; stop printing object ") + label.name + " id:" + std::to_string(label.object_id) + " copy " + std::to_string(label.instance_id) + "\n"; out += std::string("; stop printing object ") + label.name + "\n";
else if (m_label_objects_style == LabelObjectsStyle::Firmware) else if (m_label_objects_style == LabelObjectsStyle::Firmware)
if (m_flavor == GCodeFlavor::gcfMarlinFirmware || m_flavor == GCodeFlavor::gcfMarlinLegacy || m_flavor == GCodeFlavor::gcfRepRapFirmware) if (m_flavor == GCodeFlavor::gcfMarlinFirmware || m_flavor == GCodeFlavor::gcfMarlinLegacy || m_flavor == GCodeFlavor::gcfRepRapFirmware)
out += std::string("M486 S-1\n"); out += std::string("M486 S-1\n");

View File

@ -11,10 +11,6 @@ enum class LabelObjectsStyle;
namespace GCode { namespace GCode {
//std::string label_object_start(LabelObjectsStyle label_object_style, GCodeFlavor flavor, const SpanOfConstPtrs<PrintObject>& objects, int object_id, int instance_id);
//std::string label_object_stop(LabelObjectsStyle label_object_style, GCodeFlavor flavor, int object_id, int instance_id, const std::string& name);
//std::string label_all_objects(LabelObjectsStyle label_objects_style, GCodeFlavor flavor, const Print& print);
class LabelObjects { class LabelObjects {
public: public:
@ -31,9 +27,6 @@ private:
struct LabelData { struct LabelData {
std::string name; std::string name;
int unique_id; int unique_id;
int object_id;
int instance_id;
bool object_has_more_instances;
}; };
LabelObjectsStyle m_label_objects_style; LabelObjectsStyle m_label_objects_style;