Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin gcode modify patches #1979

Merged
merged 10 commits into from
Sep 30, 2024
Merged
51 changes: 47 additions & 4 deletions src/LayerPlan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2096,7 +2096,11 @@ void LayerPlan::writeGCode(GCodeExport& gcode)
}
}
}
gcode.writePrepareFansForExtrusion(extruder_plan.getFanSpeed());
// Fan speed may already be set by a plugin. Prevents two fan speed commands without move in between.
if(!extruder_plan.paths_.empty() && extruder_plan.paths_.front().fan_speed == -1)
{
gcode.writePrepareFansForExtrusion(extruder_plan.getFanSpeed());
}
std::vector<GCodePath>& paths = extruder_plan.paths_;

extruder_plan.inserts_.sort();
Expand All @@ -2119,6 +2123,13 @@ void LayerPlan::writeGCode(GCodeExport& gcode)

GCodePath& path = paths[path_idx];

// If travel paths have a non default fan speed for some reason set it as fan speed as such modification could be made by a plugin.
if(!path.isTravelPath() || path.fan_speed >= 0)
{
const double path_fan_speed = path.getFanSpeed();
gcode.writeFanCommand(path_fan_speed != GCodePathConfig::FAN_SPEED_DEFAULT ? path_fan_speed : extruder_plan.getFanSpeed());
}

if (path.perform_prime)
{
gcode.writePrimeTrain(extruder.settings_.get<Velocity>("speed_travel"));
Expand Down Expand Up @@ -2291,9 +2302,6 @@ void LayerPlan::writeGCode(GCodeExport& gcode)
bool spiralize = path.spiralize;
if (! spiralize) // normal (extrusion) move (with coasting)
{
// if path provides a valid (in range 0-100) fan speed, use it
const double path_fan_speed = path.getFanSpeed();
gcode.writeFanCommand(path_fan_speed != GCodePathConfig::FAN_SPEED_DEFAULT ? path_fan_speed : extruder_plan.getFanSpeed());

bool coasting = extruder.settings_.get<bool>("coasting_enable");
if (coasting)
Expand Down Expand Up @@ -2576,6 +2584,7 @@ bool LayerPlan::writePathWithCoasting(

void LayerPlan::applyModifyPlugin()
{
bool handled_initial_travel = false;
for (auto& extruder_plan : extruder_plans_)
{
scripta::log(
Expand All @@ -2598,6 +2607,40 @@ void LayerPlan::applyModifyPlugin()

extruder_plan.paths_ = slots::instance().modify<plugins::v0::SlotID::GCODE_PATHS_MODIFY>(extruder_plan.paths_, extruder_plan.extruder_nr_, layer_nr_);

// Check if the plugin changed first_travel_destination and update it accordingly if it has
if (! handled_initial_travel)
{
for (auto& path : extruder_plan.paths_)
{
if (path.isTravelPath() && path.points.size() > 0)
{
if (path.points.front() != first_travel_destination_)
{
first_travel_destination_ = path.points.front();
}
handled_initial_travel = true;
break;
}
}
}

size_t removed_count = std::erase_if(
extruder_plan.paths_,
[](GCodePath& path)
{
return path.points.empty();
});
if (removed_count > 0)
{
spdlog::warn("Removed {} empty paths after plugin slot GCODE_PATHS_MODIFY was executed", removed_count);
}
// Ensure that the output is at least valid enough to not cause crashes.
if (extruder_plan.paths_.size() == 0)
{
GCodePath* reinstated_path = getLatestPathWithConfig(configs_storage_.travel_config_per_extruder[getExtruder()], SpaceFillType::None);
addTravel_simple(first_travel_destination_.value_or(getLastPlannedPositionOrStartingPosition()), reinstated_path);
}

scripta::log(
"extruder_plan_1",
extruder_plan.paths_,
Expand Down
24 changes: 20 additions & 4 deletions src/plugins/converters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,28 @@ broadcast_settings_request::value_type broadcast_settings_request::operator()(co
}

auto* object_settings = message.mutable_object_settings();
for (const auto& object : slice_message.object_lists())
for (const auto& mesh_group : slice_message.object_lists())
{
auto* settings = object_settings->Add()->mutable_settings();
for (const auto& setting : object.settings())
std::unordered_map<std::string, std::string> mesh_group_settings;
for (const auto& setting : mesh_group.settings())
{
settings->emplace(setting.name(), setting.value());
mesh_group_settings[setting.name()] = setting.value();
}
for (const auto& object : mesh_group.objects())
{
std::unordered_map<std::string, std::string> per_object_settings = mesh_group_settings;
for (const auto& setting : object.settings())
{
per_object_settings[setting.name()] = setting.value();
}

per_object_settings["mesh_name"] = object.name();

auto* settings = object_settings->Add()->mutable_settings();
for (const auto& key_value_pair : per_object_settings)
{
settings->emplace(key_value_pair.first, key_value_pair.second);
}
}
}

Expand Down
Loading