From 187baae4eef8e4d1284fe4541058262fc054bbef Mon Sep 17 00:00:00 2001 From: Kris Austin Date: Thu, 29 Oct 2026 07:03:15 -0500 Subject: [PATCH] fix: Print plate and Send to Moonraker printers upload a 3MF zip as G-code (#16968) With "Use printer agents" on, Print plate and Send on Moonraker-based printer agents uploaded the plate's .gcode.3mf bundle under a .gcode name, so Klipper read a zip as G-code and cancelled the print. Both paths now upload the plate's G-code from the bundle, named after the job. Printers whose profile sets use_3mf keep getting the bundle, named .gcode.3mf, and the Qidi agent's print start names the plate with plateindex. The print start quotes the file name, so job names keep spaces, apostrophes and non-ASCII letters. Picks up #16453 by DIEHARDave, who wrote the plate G-code extraction for Print plate. Fixes #16954 Co-authored-by: DIEHARDave --- src/slic3r/Utils/MoonrakerPrinterAgent.cpp | 163 ++++++++++-- src/slic3r/Utils/MoonrakerPrinterAgent.hpp | 2 + src/slic3r/Utils/QidiPrinterAgent.cpp | 12 +- src/slic3r/Utils/QidiPrinterAgent.hpp | 4 + .../test_moonraker_printer_agent.cpp | 245 ++++++++++++++++++ tests/slic3rutils/test_qidi_printer_agent.cpp | 13 + 6 files changed, 415 insertions(+), 24 deletions(-) diff --git a/src/slic3r/Utils/MoonrakerPrinterAgent.cpp b/src/slic3r/Utils/MoonrakerPrinterAgent.cpp index de6ecc320d..814169f9bb 100644 --- a/src/slic3r/Utils/MoonrakerPrinterAgent.cpp +++ b/src/slic3r/Utils/MoonrakerPrinterAgent.cpp @@ -5,9 +5,12 @@ #include "IPrinterAgent.hpp" #include "NetworkAgentFactory.hpp" #include "bambu_networking.hpp" +#include "libslic3r/Config.hpp" +#include "libslic3r/Format/bbs_3mf.hpp" #include "libslic3r/Preset.hpp" #include "libslic3r/PresetBundle.hpp" #include "libslic3r/Utils.hpp" +#include "libslic3r/miniz_extension.hpp" #include "slic3r/GUI/GUI_App.hpp" #include "slic3r/GUI/I18N.hpp" #include "slic3r/GUI/DeviceManager.hpp" @@ -49,7 +52,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -57,15 +62,18 @@ #include #include #include +#include #include #include #include #include #include #include +#include #include #include #include +#include #include #include #include @@ -154,6 +162,117 @@ uint64_t steady_now_ms() std::chrono::duration_cast(std::chrono::steady_clock::now().time_since_epoch()).count()); } +bool extract_plate_gcode(const std::string& archive_path, int plate_index, const boost::filesystem::path& gcode_path) +{ + static const std::regex plate_gcode(R"(Metadata/plate_\d+\.gcode)"); + + mz_zip_archive archive; + mz_zip_zero_struct(&archive); + if (!Slic3r::open_zip_reader(&archive, archive_path)) + return false; + + int index = mz_zip_reader_locate_file(&archive, (boost::format(GCODE_FILE_FORMAT) % plate_index).str().c_str(), nullptr, 0); + if (index < 0) { + int plates = 0; + for (mz_uint i = 0, n = mz_zip_reader_get_num_files(&archive); i < n; ++i) { + mz_zip_archive_file_stat stat; + if (mz_zip_reader_file_stat(&archive, i, &stat) && std::regex_match(stat.m_filename, plate_gcode)) { + index = int(i); + ++plates; + } + } + // Another plate's G-code would print the wrong part + if (plates != 1) + index = -1; + } + const bool extracted = index >= 0 && Slic3r::extract_zip_entry_to_file(&archive, mz_uint(index), gcode_path); + Slic3r::close_zip_reader(&archive); + return extracted; +} + +// Klipper reads any uploaded file as G-code, so unless the printer prints 3MF, a .gcode.3mf bundle is uploaded as the +// plate's G-code inside it. +class UploadFile +{ +public: + UploadFile(const std::string& path, const Slic3r::PrintParams& params, bool printer_accepts_3mf) : m_source(path), m_path(path) + { + if (!boost::iends_with(path, ".3mf")) + return; + if (printer_accepts_3mf) { + m_sends_bundle = true; + return; + } + m_path.clear(); + m_failed = true; + boost::system::error_code ec; + const boost::filesystem::path temp_dir = boost::filesystem::temp_directory_path(ec); + const boost::filesystem::path name = ec ? boost::filesystem::path() : + boost::filesystem::unique_path("orca-upload-%%%%-%%%%-%%%%.gcode", ec); + if (ec) { + BOOST_LOG_TRIVIAL(error) << "MoonrakerPrinterAgent: no temp file to extract the plate G-code to: " << ec.message(); + return; + } + m_extracted = temp_dir / name; + // plate_index is read only for a bundle, because the verify uploads leave it unset + if (!extract_plate_gcode(path, params.plate_index, m_extracted)) { + BOOST_LOG_TRIVIAL(error) << "MoonrakerPrinterAgent: no G-code for plate " << params.plate_index << " in " << path; + return; + } + m_path = m_extracted.string(); + m_failed = false; + } + ~UploadFile() + { + if (!m_extracted.empty()) { + boost::system::error_code ec; + boost::filesystem::remove(m_extracted, ec); + } + } + UploadFile(const UploadFile&) = delete; + UploadFile& operator=(const UploadFile&) = delete; + + // ok() is false when a bundle's plate G-code could not be extracted. + bool ok() const { return !m_failed; } + const std::string& path() const { return m_path; } + + std::string upload_name(std::string name) const + { + if (name.empty()) + name = boost::filesystem::path(m_source).filename().string(); + for (const char* extension : {".3mf", ".gcode"}) + if (boost::iends_with(name, extension)) + name.erase(name.size() - std::strlen(extension)); + // Klipper's print start can't take # ; * or control characters, newer curl uploads " as %22, and / \ would make + // the name a path + std::replace_if(name.begin(), name.end(), [](char c) { + const auto byte = static_cast(c); + return byte < 0x20 || byte == 0x7F || std::string_view("\"#;*/\\").find(c) != std::string_view::npos; + }, '_'); + // A leading dot hides the file in Fluidd and Mainsail, and a leading space may be trimmed from the stored name + name.erase(0, name.find_first_not_of(". ")); + // Printer file systems allow 255 bytes per name + const std::size_t max_stem = 255 - std::strlen(".gcode.3mf"); + if (name.size() > max_stem) { + std::size_t cut = max_stem; + while (cut > 0 && (static_cast(name[cut]) & 0xC0) == 0x80) + --cut; + name.resize(cut); + } + if (name.empty()) + name = "print"; + // A bundle is named .gcode.3mf, as the print host upload names it + return name + (m_sends_bundle ? ".gcode.3mf" : ".gcode"); + } + +private: + std::string m_source; + std::string m_path; + boost::filesystem::path m_extracted; + bool m_sends_bundle = false; + bool m_failed = false; +}; + } // namespace namespace Slic3r { @@ -339,19 +458,13 @@ int MoonrakerPrinterAgent::start_send_gcode_to_sdcard(PrintParams params, if (update_fn) update_fn(PrintingStageCreate, 0, _u8L("Preparing...")); - std::string filename = params.filename; - if (filename.empty()) { - filename = params.task_name; + UploadFile file(params.filename, params, accepts_3mf()); + if (!file.ok()) { + return BAMBU_NETWORK_ERR_FILE_NOT_EXIST; } - if (!boost::iends_with(filename, ".gcode")) { - filename += ".gcode"; - } - - // Sanitize filename to prevent path traversal attacks - std::string safe_filename = sanitize_filename(filename); // Upload only, don't start print - if (!upload_gcode(params.filename, safe_filename, device_info.base_url, device_info.api_key, update_fn, cancel_fn)) { + if (!upload_gcode(file.path(), file.upload_name(params.project_name), device_info.base_url, device_info.api_key, update_fn, cancel_fn)) { return BAMBU_NETWORK_ERR_PRINT_SG_UPLOAD_FTP_FAILED; } @@ -376,7 +489,6 @@ int MoonrakerPrinterAgent::start_local_print(PrintParams params, OnUpdateStatusF gcode_path = params.dst_file; } - // Check if file exists and has .gcode extension namespace fs = boost::filesystem; fs::path source_path(gcode_path); if (!fs::exists(source_path)) { @@ -384,18 +496,17 @@ int MoonrakerPrinterAgent::start_local_print(PrintParams params, OnUpdateStatusF return BAMBU_NETWORK_ERR_FILE_NOT_EXIST; } - // Extract filename for upload (relative to gcodes root) - std::string upload_filename = source_path.filename().string(); - if (!boost::iends_with(upload_filename, ".gcode")) { - upload_filename += ".gcode"; + UploadFile file(gcode_path, params, accepts_3mf()); + if (!file.ok()) { + return BAMBU_NETWORK_ERR_FILE_NOT_EXIST; } - // Sanitize filename to prevent path traversal attacks (extra safety) - upload_filename = sanitize_filename(upload_filename); + + const std::string upload_filename = file.upload_name(params.project_name); // Upload file if (update_fn) update_fn(PrintingStageUpload, 0, _u8L("Uploading G-code...")); - if (!upload_gcode(gcode_path, upload_filename, device_info.base_url, device_info.api_key, update_fn, cancel_fn)) { + if (!upload_gcode(file.path(), upload_filename, device_info.base_url, device_info.api_key, update_fn, cancel_fn)) { return BAMBU_NETWORK_ERR_PRINT_LP_UPLOAD_FTP_FAILED; } @@ -417,8 +528,15 @@ int MoonrakerPrinterAgent::start_local_print(PrintParams params, OnUpdateStatusF bool MoonrakerPrinterAgent::print_uploaded_file(const PrintParams& /*params*/, const std::string& filename) { - // Start print via gcode script (simpler than JSON-RPC) - return send_gcode_sync(device_info.dev_id, "SDCARD_PRINT_FILE FILENAME=" + filename); + // Start print via gcode script (simpler than JSON-RPC); quoted so spaces and apostrophes stay part of the name + return send_gcode_sync(device_info.dev_id, "SDCARD_PRINT_FILE FILENAME=\"" + filename + "\""); +} + +bool MoonrakerPrinterAgent::accepts_3mf() const +{ + const PresetBundle* bundle = GUI::wxGetApp().preset_bundle; + const ConfigOptionBool* use_3mf = bundle ? bundle->printers.get_edited_preset().config.option("use_3mf") : nullptr; + return use_3mf != nullptr && use_3mf->value; } int MoonrakerPrinterAgent::start_sdcard_print(PrintParams params, OnUpdateStatusFn update_fn, WasCancelledFn cancel_fn) @@ -2460,9 +2578,8 @@ std::string MoonrakerPrinterAgent::sanitize_filename(const std::string& filename if (filename.empty()) { return "print.gcode"; } - namespace fs = boost::filesystem; - fs::path p(filename); - std::string basename = p.filename().string(); + // boost's Windows path parsing would also split at ':', which printer file names may contain + const std::string basename = filename.substr(filename.find_last_of("/\\") + 1); if (basename.empty() || basename == "." || basename == "..") { return "print.gcode"; } diff --git a/src/slic3r/Utils/MoonrakerPrinterAgent.hpp b/src/slic3r/Utils/MoonrakerPrinterAgent.hpp index 0bccb58f2b..75ea6c6a37 100644 --- a/src/slic3r/Utils/MoonrakerPrinterAgent.hpp +++ b/src/slic3r/Utils/MoonrakerPrinterAgent.hpp @@ -133,6 +133,8 @@ protected: const std::function& cancelled) const; // Starts the print of a file already uploaded to the gcodes root virtual bool print_uploaded_file(const PrintParams& params, const std::string& filename); + // Whether the printer prints a .gcode.3mf bundle as is (its use_3mf option) + virtual bool accepts_3mf() const; void start_status_stream(const std::string& dev_id, const std::string& base_url, const std::string& api_key); void stop_status_stream(); diff --git a/src/slic3r/Utils/QidiPrinterAgent.cpp b/src/slic3r/Utils/QidiPrinterAgent.cpp index 1c4799c68a..3cb7c3e682 100644 --- a/src/slic3r/Utils/QidiPrinterAgent.cpp +++ b/src/slic3r/Utils/QidiPrinterAgent.cpp @@ -8,6 +8,7 @@ #include "nlohmann/json.hpp" #include +#include #include #include #include @@ -129,7 +130,16 @@ bool QidiPrinterAgent::print_uploaded_file(const PrintParams& params, const std: // On the X-Max 4 and Q2, Klipper only opens the copy Moonraker's print start makes, so SDCARD_PRINT_FILE can't find the upload std::string response; - return send_jsonrpc_command(device_info.base_url, device_info.api_key, {{"filename", filename}}, response); + return send_jsonrpc_command(device_info.base_url, device_info.api_key, print_start_request(params, filename), response); +} + +nlohmann::json QidiPrinterAgent::print_start_request(const PrintParams& params, const std::string& filename) +{ + nlohmann::json request{{"filename", filename}}; + // Qidi's Moonraker prints Metadata/plate_.gcode from a 3MF, defaulting to plate 1 + if (boost::iends_with(filename, ".3mf")) + request["plateindex"] = std::to_string(params.plate_index); + return request; } bool QidiPrinterAgent::fetch_slot_info(const std::string& base_url, diff --git a/src/slic3r/Utils/QidiPrinterAgent.hpp b/src/slic3r/Utils/QidiPrinterAgent.hpp index ec729da909..67fb683e3e 100644 --- a/src/slic3r/Utils/QidiPrinterAgent.hpp +++ b/src/slic3r/Utils/QidiPrinterAgent.hpp @@ -9,6 +9,8 @@ #include #include +#include + namespace Slic3r { class QidiPrinterAgent final : public MoonrakerPrinterAgent @@ -25,6 +27,8 @@ public: // Builds the SAVE_VARIABLE lines that make each T macro load the Box slot picked for filament n static std::string build_tool_slot_script(const std::string& ams_mapping); + // Builds the body of Moonraker's print start for an uploaded file, naming the plate to print when it is a 3MF + static nlohmann::json print_start_request(const PrintParams& params, const std::string& filename); protected: // Saves the Box slot picked for each filament in the print dialog, then starts the print through Moonraker diff --git a/tests/slic3rutils/test_moonraker_printer_agent.cpp b/tests/slic3rutils/test_moonraker_printer_agent.cpp index 4bdd741db3..283c06d1b3 100644 --- a/tests/slic3rutils/test_moonraker_printer_agent.cpp +++ b/tests/slic3rutils/test_moonraker_printer_agent.cpp @@ -6,10 +6,14 @@ #include #include #include +#include #include #include +#include +#include #include #include +#include #include #include #include @@ -35,11 +39,21 @@ #include #include #include +#include +#include +#include +#include +#include +#include +#include #include +#include "libslic3r/miniz_extension.hpp" #include "slic3r/GUI/DeviceCore/DevStorage.h" #include "slic3r/Utils/IPrinterAgent.hpp" #include "slic3r/Utils/MoonrakerPrinterAgent.hpp" +#include "slic3r/Utils/bambu_networking.hpp" +#include "test_utils.hpp" using namespace Slic3r; @@ -242,6 +256,20 @@ public: m_disk_usage = std::move(disk_usage); } + // The multipart body of the last file upload. + std::string upload() + { + std::lock_guard lock(m_mutex); + return m_upload; + } + + // The last G-code script sent to the printer. + std::string script() + { + std::lock_guard lock(m_mutex); + return m_script; + } + // Hangs up every connection that is waiting. void release() { @@ -369,6 +397,17 @@ private: } response.result(http::status::ok); response.body() = nlohmann::json{{"result", result}}.dump(); + } else if (request.target() == "/server/files/upload") { + std::lock_guard lock(m_mutex); + m_upload = request.body(); + response.result(http::status::created); + response.body() = "{}"; + } else if (request.target() == "/printer/gcode/script") { + std::lock_guard lock(m_mutex); + const nlohmann::json body = nlohmann::json::parse(request.body(), nullptr, false); + m_script = body.is_object() ? body.value("script", "") : ""; + response.result(http::status::ok); + response.body() = R"({"result": "ok"})"; } response.prepare_payload(); beast::error_code ec; @@ -454,6 +493,8 @@ private: int m_close_frames = 0; bool m_released = false; nlohmann::json m_disk_usage = {{"total", 8e9}, {"used", 2e9}, {"free", 6e9}}; + std::string m_upload; + std::string m_script; }; // Records when the agent dispatches a status report. @@ -527,6 +568,69 @@ template bool destroys_within(std::unique_ptr& agent, std::c return destroyed.wait_for(limit) == std::future_status::ready; } +// Uploads to a printer without connecting to it, and records the file it is told to print. +class UploadAgent : public MoonrakerPrinterAgent +{ +public: + explicit UploadAgent(const FakePrinter& printer) : MoonrakerPrinterAgent("") { device_info.base_url = printer.base_url(); } + std::string printed; + bool takes_3mf = false; + +protected: + bool accepts_3mf() const override { return takes_3mf; } + + bool print_uploaded_file(const PrintParams& params, const std::string& filename) override + { + printed = filename; + return MoonrakerPrinterAgent::print_uploaded_file(params, filename); + } +}; + +// Writes a .gcode.3mf holding one line of G-code for each of the given plate numbers. +void write_bundle(const std::string& path, const std::vector& plates) +{ + mz_zip_archive zip; + mz_zip_zero_struct(&zip); + REQUIRE(open_zip_writer(&zip, path)); + for (const std::string& plate : plates) { + const std::string gcode = "; plate " + plate + "\n"; + REQUIRE(mz_zip_writer_add_mem(&zip, ("Metadata/plate_" + plate + ".gcode").c_str(), gcode.data(), gcode.size(), MZ_DEFAULT_COMPRESSION)); + } + REQUIRE(mz_zip_writer_finalize_archive(&zip)); + REQUIRE(close_zip_writer(&zip)); +} + +// Makes a fresh directory the system temp folder for its lifetime, so a test sees only what the agent leaves there. +class ScopedSystemTemp : public ScopedTemporaryDir +{ +public: + ScopedSystemTemp() : ScopedTemporaryDir("moonraker-temp") + { +#ifdef _WIN32 + const std::string dir = boost::nowide::narrow(path().wstring()); +#else + const std::string dir = string(); +#endif + for (const char* name : {"TMP", "TEMP", "TMPDIR"}) { + const char* value = boost::nowide::getenv(name); + m_saved.emplace_back(name, value ? std::optional(value) : std::nullopt); + boost::nowide::setenv(name, dir.c_str(), 1); + } + } + ~ScopedSystemTemp() + { + for (const auto& [name, value] : m_saved) { + if (value) + boost::nowide::setenv(name.c_str(), value->c_str(), 1); + else + boost::nowide::unsetenv(name.c_str()); + } + } + +private: + std::vector>> m_saved; +}; + } // namespace TEST_CASE("Moonraker discovery waits for a complete device identity", "[MoonrakerPrinterAgent][Discovery]") @@ -837,3 +941,144 @@ TEST_CASE("Destroying the agent does not wait out a connection retry's delay", " CHECK(destroys_within(agent, 1s)); } + +TEST_CASE("A .gcode.3mf goes to the printer as the printed plate's G-code unless the printer takes 3MF", "[MoonrakerPrinterAgent][Upload]") +{ + const bool takes_3mf = GENERATE(false, true); + CAPTURE(takes_3mf); + const std::string name = takes_3mf ? "Cube.gcode.3mf" : "Cube.gcode"; + + ScopedTemporaryDir dir("moonraker-upload"); + const std::string bundle = (dir.path() / ".1234.0.3mf").string(); + write_bundle(bundle, {"1", "2"}); + ScopedSystemTemp temp; + REQUIRE(boost::filesystem::equivalent(boost::filesystem::temp_directory_path(), temp.path())); + + FakePrinter printer(Behavior::AnswersPings); + UploadAgent agent(printer); + agent.takes_3mf = takes_3mf; + PrintParams params{}; + params.filename = bundle; + params.plate_index = 2; + + SECTION("Print plate") + { + params.project_name = "Cube"; + REQUIRE(agent.start_local_print(params, nullptr, nullptr) == BAMBU_NETWORK_SUCCESS); + CHECK(agent.printed == name); + } + SECTION("Send to printer") + { + params.project_name = "Cube.gcode.3mf"; + REQUIRE(agent.start_send_gcode_to_sdcard(params, nullptr, nullptr, nullptr) == BAMBU_NETWORK_SUCCESS); + CHECK(agent.printed.empty()); + } + + const std::string upload = printer.upload(); + CHECK(upload.find("filename=\"" + name + "\"") != std::string::npos); + if (takes_3mf) { + CHECK(upload.find("PK\x03\x04") != std::string::npos); + } else { + CHECK(upload.find("; plate 2") != std::string::npos); + CHECK(upload.find("; plate 1") == std::string::npos); + } + // Neither the source folder nor the temp folder keeps an extracted copy + CHECK(std::distance(boost::filesystem::directory_iterator(dir.path()), boost::filesystem::directory_iterator()) == 1); + CHECK(boost::filesystem::is_empty(temp.path())); +} + +TEST_CASE("A bundle without the printed plate's G-code is uploaded only when it holds a single plate", "[MoonrakerPrinterAgent][Upload]") +{ + auto [plates, uploaded] = GENERATE(table, bool>({ + {{"1"}, true}, + {{"1", "3"}, false}, + })); + CAPTURE(plates.size()); + + ScopedTemporaryDir dir("moonraker-upload"); + const std::string bundle = (dir.path() / ".1234.0.3mf").string(); + write_bundle(bundle, plates); + + FakePrinter printer(Behavior::AnswersPings); + UploadAgent agent(printer); + PrintParams params{}; + params.filename = bundle; + params.plate_index = 2; + params.project_name = "Cube"; + + const int result = agent.start_local_print(params, nullptr, nullptr); + CHECK((result == BAMBU_NETWORK_SUCCESS) == uploaded); + CHECK((printer.upload().find("; plate 1") != std::string::npos) == uploaded); +} + +TEST_CASE("The job name reaches the print start as a file name the printer can store and list", "[MoonrakerPrinterAgent][Upload]") +{ + // A long name is cut to fit 255 bytes with room for .gcode.3mf, at a whole 2-byte character + std::string long_name; + for (int i = 0; i < 150; ++i) + long_name += "\xC3\xA9"; + const std::size_t max_stem = 255 - std::strlen(".gcode.3mf"); + const std::string cut_long_name = long_name.substr(0, max_stem - max_stem % 2); + + auto [job_name, file_name] = GENERATE_COPY(table({ + {"Cable clip #2; v\"3\"*", "Cable clip _2_ v_3__.gcode"}, + {"Bracket 1/2", "Bracket 1_2.gcode"}, + {"A: frame", "A: frame.gcode"}, + {"Bob's", "Bob's.gcode"}, + {"Bob's \"best\" frame (v2) [final] {\xC3\xA9} |y| ?!&%$@=+,~`", "Bob's _best_ frame (v2) [final] {\xC3\xA9} |y| ?!&%$@=+,~`.gcode"}, + {"x\nM104 S300", "x_M104 S300.gcode"}, + {" .Cube", "Cube.gcode"}, + {"", "1234.0.gcode"}, + {long_name, cut_long_name + ".gcode"}, + })); + CAPTURE(job_name); + + ScopedTemporaryDir dir("moonraker-upload"); + const std::string bundle = (dir.path() / ".1234.0.3mf").string(); + write_bundle(bundle, {"1"}); + + FakePrinter printer(Behavior::AnswersPings); + UploadAgent agent(printer); + PrintParams params{}; + params.filename = bundle; + params.plate_index = 1; + params.project_name = job_name; + + REQUIRE(agent.start_local_print(params, nullptr, nullptr) == BAMBU_NETWORK_SUCCESS); + CHECK(printer.upload().find("filename=\"" + file_name + "\"") != std::string::npos); + CHECK(printer.script() == "SDCARD_PRINT_FILE FILENAME=\"" + file_name + "\""); +} + +TEST_CASE("A file that is not a 3MF is uploaded as it is, named after the job when there is one", "[MoonrakerPrinterAgent][Upload]") +{ + auto [job_name, file_name] = GENERATE(table({ + {"Cube", "Cube.gcode"}, + {"", "part.gcode"}, + })); + CAPTURE(job_name); + + ScopedTemporaryDir dir("moonraker-upload"); + const boost::filesystem::path gcode = dir.path() / "part.gcode"; + boost::filesystem::ofstream(gcode, std::ios::binary) << "; plain G-code\n"; + + FakePrinter printer(Behavior::AnswersPings); + UploadAgent agent(printer); + PrintParams params{}; + params.filename = gcode.string(); + params.project_name = job_name; + + SECTION("Print plate") + { + REQUIRE(agent.start_local_print(params, nullptr, nullptr) == BAMBU_NETWORK_SUCCESS); + CHECK(agent.printed == file_name); + } + SECTION("Send to printer") + { + REQUIRE(agent.start_send_gcode_to_sdcard(params, nullptr, nullptr, nullptr) == BAMBU_NETWORK_SUCCESS); + } + + const std::string upload = printer.upload(); + CHECK(upload.find("filename=\"" + file_name + "\"") != std::string::npos); + CHECK(upload.find("; plain G-code") != std::string::npos); + CHECK(std::distance(boost::filesystem::directory_iterator(dir.path()), boost::filesystem::directory_iterator()) == 1); +} diff --git a/tests/slic3rutils/test_qidi_printer_agent.cpp b/tests/slic3rutils/test_qidi_printer_agent.cpp index ee5bb4b575..ab7b24dd66 100644 --- a/tests/slic3rutils/test_qidi_printer_agent.cpp +++ b/tests/slic3rutils/test_qidi_printer_agent.cpp @@ -4,7 +4,10 @@ #include +#include + #include "slic3r/Utils/QidiPrinterAgent.hpp" +#include "slic3r/Utils/bambu_networking.hpp" using namespace Slic3r; @@ -46,3 +49,13 @@ TEST_CASE("Filaments past the firmware's last tool variable are skipped", "[Qidi CHECK(QidiPrinterAgent::build_tool_slot_script(mapping) == "SAVE_VARIABLE VARIABLE=value_t15 VALUE=\"'slot1'\""); } + +TEST_CASE("The print start of a 3MF names the plate to print", "[QidiPrinterAgent]") +{ + PrintParams params; + params.plate_index = 2; + + CHECK(QidiPrinterAgent::print_start_request(params, "Cube.gcode.3mf") == + nlohmann::json{{"filename", "Cube.gcode.3mf"}, {"plateindex", "2"}}); + CHECK(QidiPrinterAgent::print_start_request(params, "Cube.gcode") == nlohmann::json{{"filename", "Cube.gcode"}}); +}