Label objects: integrating the recently merged PR:

- code from @jschuh's functions moved into respective functions in LabelObjects class
- name no longer contains object_id, instance_id is newly 1-based
- replacing banned characters using std::replace_if
This commit is contained in:
Lukas Matena 2023-09-13 13:43:46 +02:00
parent e659e96b04
commit 501cfb64f9
2 changed files with 28 additions and 40 deletions

View File

@ -130,43 +130,6 @@ namespace Slic3r {
return ok;
}
inline std::string make_klipper_exclude_object_name(const std::string &name, int object_id, int instance_id)
{
constexpr char banned_chars[] = "-. \r\n\v\t\f";
std::string cleaned_name = name;
const char *next = cleaned_name.c_str();
while (next = std::strpbrk(next, banned_chars))
cleaned_name[next - cleaned_name.c_str()] = '_';
return cleaned_name + "_id_" + std::to_string(object_id) + "_copy_" + std::to_string(instance_id);
}
// Label excluded objects for Klipper
std::string make_klipper_exclude_object_header(const Print &print) {
std::string output;
int object_id = 0;
for (auto object : print.objects()) {
int instance_id = 0;
for (auto instance : object->instances()) {
char buffer[64];
output += "EXCLUDE_OBJECT_DEFINE NAME=";
output += make_klipper_exclude_object_name(object->model_object()->name, object_id, instance_id++);
Polygon outline = object->model_object()->convex_hull_2d(instance.model_instance->get_matrix());
outline.douglas_peucker(50000.f);
auto center = outline.centroid();
std::snprintf(buffer, sizeof(buffer) - 1, " CENTER=%.3f,%.3f", unscale<float>(center[0]), unscale<float>(center[1]));
output += buffer + std::string(" POLYGON=[");
for (auto point : outline) {
std::snprintf(buffer, sizeof(buffer) - 1, "[%.3f,%.3f],", unscale<float>(point[0]), unscale<float>(point[1]));
output += buffer;
}
output.pop_back();
output += "]\n";
}
++object_id;
}
return output;
}
std::string OozePrevention::pre_toolchange(GCodeGenerator &gcodegen)
{
std::string gcode;

View File

@ -46,6 +46,10 @@ void LabelObjects::init(const Print& print)
if (object_has_more_instances)
name += " (Instance " + std::to_string(instance_id) + ")";
if (m_flavor == gcfKlipper) {
const std::string banned = "-. \r\n\v\t\f";
std::replace_if(name.begin(), name.end(), [&banned](char c) { return banned.find(c) != std::string::npos; }, '_');
}
}
m_label_data.emplace(pi, LabelData{name, unique_id});
@ -71,8 +75,25 @@ std::string LabelObjects::all_objects_header() const
out += "\n";
for (const auto& [print_instance, label] : label_data_sorted) {
out += start_object(*print_instance, IncludeName::Yes);
out += stop_object(*print_instance);
if (m_flavor == gcfKlipper) {
char buffer[64];
out += "EXCLUDE_OBJECT_DEFINE NAME=" + label.name;
Polygon outline = print_instance->model_instance->get_object()->convex_hull_2d(print_instance->model_instance->get_matrix());
assert(! outline.empty());
outline.douglas_peucker(50000.f);
Point center = outline.centroid();
std::snprintf(buffer, sizeof(buffer) - 1, " CENTER=%.3f,%.3f", unscale<float>(center[0]), unscale<float>(center[1]));
out += buffer + std::string(" POLYGON=[");
for (const Point& point : outline) {
std::snprintf(buffer, sizeof(buffer) - 1, "[%.3f,%.3f],", unscale<float>(point[0]), unscale<float>(point[1]));
out += buffer;
}
out.pop_back();
out += "]\n";
} else {
out += start_object(*print_instance, IncludeName::Yes);
out += stop_object(*print_instance);
}
}
out += "\n";
return out;
@ -97,7 +118,9 @@ std::string LabelObjects::start_object(const PrintInstance& print_instance, Incl
out += std::string("M486 A");
out += (m_flavor == GCodeFlavor::gcfRepRapFirmware ? (std::string("\"") + label.name + "\"") : label.name) + "\n";
}
} else {
} else if (m_flavor == gcfKlipper)
out += "EXCLUDE_OBJECT_START NAME=" + label.name + "\n";
else {
// Not supported by / implemented for the other firmware flavors.
}
}
@ -119,6 +142,8 @@ std::string LabelObjects::stop_object(const PrintInstance& print_instance) const
else if (m_label_objects_style == LabelObjectsStyle::Firmware)
if (m_flavor == GCodeFlavor::gcfMarlinFirmware || m_flavor == GCodeFlavor::gcfMarlinLegacy || m_flavor == GCodeFlavor::gcfRepRapFirmware)
out += std::string("M486 S-1\n");
else if (m_flavor ==gcfKlipper)
out += "EXCLUDE_OBJECT_END NAME=" + label.name + "\n";
else {
// Not supported by / implemented for the other firmware flavors.
}