From 9e34ca536b322a08ac1d9ba6447c2f75f9419edc Mon Sep 17 00:00:00 2001 From: Ian Chua Date: Tue, 22 Sep 2026 21:11:03 +0800 Subject: [PATCH] fix: port BBL implementations from #15711 --- src/slic3r/Utils/BBLPrinterAgent.cpp | 91 +++++++++++++++++++++++++++- src/slic3r/Utils/BBLPrinterAgent.hpp | 8 ++- src/slic3r/Utils/IPrinterAgent.hpp | 24 +++++++- 3 files changed, 120 insertions(+), 3 deletions(-) diff --git a/src/slic3r/Utils/BBLPrinterAgent.cpp b/src/slic3r/Utils/BBLPrinterAgent.cpp index 0c6225cc55..48bd70d4b4 100644 --- a/src/slic3r/Utils/BBLPrinterAgent.cpp +++ b/src/slic3r/Utils/BBLPrinterAgent.cpp @@ -1,15 +1,21 @@ #include "BBLPrinterAgent.hpp" #include "BBLNetworkPlugin.hpp" +#include "IPrinterAgent.hpp" #include "NetworkAgentFactory.hpp" +#include "NetworkAgent.hpp" #include "libslic3r/Utils.hpp" +#include #include #include #include +#include +#include using json = nlohmann::json; #include #include +#include namespace Slic3r { @@ -133,7 +139,7 @@ BBLPrinterAgent::~BBLPrinterAgent() = default; void BBLPrinterAgent::set_cloud_agent(std::shared_ptr cloud) { - m_cloud_agent = cloud; + (void) cloud; // BBL DLL manages tokens internally, so this is just for interface compliance } @@ -141,6 +147,89 @@ void BBLPrinterAgent::set_cloud_agent(std::shared_ptr cloud) // Communication // ============================================================================ +int BBLPrinterAgent::command_ams_refresh_rfid(std::string dev_id, std::string tray_id, int sequence_id, bool lan_mode) +{ + const std::string gcode = (boost::format("M620 R%1% \n") % tray_id).str(); + BOOST_LOG_TRIVIAL(trace) << "ams_debug: gcode_cmd" << gcode; + nlohmann::json j; + j["print"]["command"] = "gcode_line"; + j["print"]["param"] = gcode; + j["print"]["sequence_id"] = std::to_string(sequence_id); + return publish(dev_id, j, lan_mode); +} + +int BBLPrinterAgent::command_ams_calibrate(std::string dev_id, int ams_id, int sequence_id, bool lan_mode) +{ + const std::string gcode = (boost::format("M620 C%1% \n") % ams_id).str(); + BOOST_LOG_TRIVIAL(trace) << "ams_debug: gcode_cmd" << gcode; + nlohmann::json j; + j["print"]["command"] = "gcode_line"; + j["print"]["param"] = gcode; + j["print"]["sequence_id"] = std::to_string(sequence_id); + return publish(dev_id, j, lan_mode); +} + +int BBLPrinterAgent::command_ams_select_tray(std::string dev_id, std::string tray_id, int sequence_id, bool lan_mode) +{ + const std::string gcode = (boost::format("M620 P%1% \n") % tray_id).str(); + BOOST_LOG_TRIVIAL(trace) << "ams_debug: gcode_cmd" << gcode; + nlohmann::json j; + j["print"]["command"] = "gcode_line"; + j["print"]["param"] = gcode; + j["print"]["sequence_id"] = std::to_string(sequence_id); + return publish(dev_id, j, lan_mode); +} + +int BBLPrinterAgent::command_axis_control(std::string dev_id, std::string axis, double unit, double input_val, int speed, + bool is_core_xy, bool supports_mqtt_axis_control, int sequence_id, bool lan_mode) +{ + nlohmann::json j; + j["print"]["sequence_id"] = std::to_string(sequence_id); + + if (supports_mqtt_axis_control) { + int dir = input_val > 0 ? 1 : -1; + // i3-arch printers move the bed for Y/Z, so the on-screen direction is + // reversed -- same negation the g-code fallback below applies. + if (!is_core_xy && (axis == "Y" || axis == "Z")) + dir = -dir; + + j["print"]["command"] = "xyz_ctrl"; + j["print"]["axis"] = axis; + j["print"]["dir"] = dir; + j["print"]["mode"] = (std::abs(input_val) >= 10) ? 1 : 0; + return publish(dev_id, j, lan_mode); + } + + double value = input_val; + if (!is_core_xy && (axis == "Y" || axis == "Z")) + value = -input_val; + + std::string value_str = (boost::format("%.1f") % (value * unit)).str(); + std::string gcode; + if (axis == "X" || axis == "Y" || axis == "Z") { + gcode = (boost::format("M211 S \nM211 X1 Y1 Z1\nM1002 push_ref_mode\nG91 \nG1 %1%%2% F%3%\nM1002 pop_ref_mode\nM211 R\n") + % axis % value_str % speed).str(); + } else if (axis == "E") { + gcode = (boost::format("M83 \nG0 %1%%2% F%3%\n") % axis % value_str % speed).str(); + } else { + return -1; + } + + j["print"]["command"] = "gcode_line"; + j["print"]["param"] = gcode; + return publish(dev_id, j, lan_mode); +} + +int BBLPrinterAgent::publish(const std::string& dev_id, const nlohmann::json& j, bool lan_mode) +{ + const int rtn = lan_mode ? send_message_to_printer(dev_id, j.dump(), 0, 0) : send_message(dev_id, j.dump(), 0, 0); + if (rtn == 0) + BOOST_LOG_TRIVIAL(info) << "publish_json: " << j.dump() << " code: " << rtn; + else + BOOST_LOG_TRIVIAL(error) << "publish_json: " << j.dump() << " code: " << rtn; + return rtn; +} + int BBLPrinterAgent::send_message(std::string dev_id, std::string json_str, int qos, int flag) { json_str = from_orca_payload(std::move(json_str)); diff --git a/src/slic3r/Utils/BBLPrinterAgent.hpp b/src/slic3r/Utils/BBLPrinterAgent.hpp index 5ac6ef0bd0..aaec01006e 100644 --- a/src/slic3r/Utils/BBLPrinterAgent.hpp +++ b/src/slic3r/Utils/BBLPrinterAgent.hpp @@ -5,6 +5,7 @@ #include "ICloudServiceAgent.hpp" #include #include +#include namespace Slic3r { @@ -28,6 +29,11 @@ public: // Communication int send_message(std::string dev_id, std::string json_str, int qos, int flag) override; + int command_ams_refresh_rfid(std::string dev_id, std::string tray_id, int sequence_id, bool lan_mode) override; + int command_ams_calibrate(std::string dev_id, int ams_id, int sequence_id, bool lan_mode) override; + int command_ams_select_tray(std::string dev_id, std::string tray_id, int sequence_id, bool lan_mode) override; + int command_axis_control(std::string dev_id, std::string axis, double unit, double input_val, int speed, + bool is_core_xy, bool supports_mqtt_axis_control, int sequence_id, bool lan_mode) override; std::string default_lan_username() const override { return "bblp"; } int connect_printer(std::string dev_id, std::string dev_ip, std::string username, std::string password, bool use_ssl) override; int disconnect_printer() override; @@ -100,7 +106,7 @@ public: static std::string from_orca_payload(std::string json_text); private: - std::shared_ptr m_cloud_agent; + int publish(const std::string& dev_id, const nlohmann::json& j, bool lan_mode); }; } // namespace Slic3r diff --git a/src/slic3r/Utils/IPrinterAgent.hpp b/src/slic3r/Utils/IPrinterAgent.hpp index 6915e27559..67e36b38d9 100644 --- a/src/slic3r/Utils/IPrinterAgent.hpp +++ b/src/slic3r/Utils/IPrinterAgent.hpp @@ -172,7 +172,29 @@ public: virtual int command_axis_control(std::string dev_id, std::string axis, double unit, double input_val, int speed, bool is_core_xy, bool supports_mqtt_axis_control, int sequence_id, bool lan_mode) - { return ORCA_NETWORK_ERR_CMD_NOT_SUPPORTED; } + { + (void) supports_mqtt_axis_control; + + double value = input_val; + if (!is_core_xy && (axis == "Y" || axis == "Z")) + value = -input_val; + + std::string value_str = (boost::format("%.1f") % (value * unit)).str(); + std::string gcode; + if (axis == "X" || axis == "Y" || axis == "Z") { + gcode = (boost::format("G91 \nG1 %1%%2% F%3%\nG90 \n") % axis % value_str % speed).str(); + } else if (axis == "E") { + gcode = (boost::format("M83 \nG0 %1%%2% F%3%\n") % axis % value_str % speed).str(); + } else { + return -1; + } + + nlohmann::json j; + j["print"]["command"] = "gcode_line"; + j["print"]["param"] = gcode; + j["print"]["sequence_id"] = std::to_string(sequence_id); + return publish_command_json(dev_id, j, lan_mode); + } /** * Default LAN account username for this agent's protocol, if it has a fixed one.