From 619088b58ff5efa84aea0f377842c03252dfebb2 Mon Sep 17 00:00:00 2001 From: saumyaj3 Date: Thu, 10 Oct 2024 11:04:49 +0200 Subject: [PATCH 1/7] Add engine info handler for getting engine version and hash Implemented a handler for the `--engine_info_cb` flag to handle engine information retrieval. Added functionality to create and send engine info messages, which include the Cura engine version and hash, to Emscripten. Updated CMake and conan files to pass Cura engine hash. NP-349 --- CMakeLists.txt | 1 + conanfile.py | 1 + .../communication/EmscriptenCommunication.h | 4 ++- src/communication/CommandLine.cpp | 2 +- src/communication/EmscriptenCommunication.cpp | 30 +++++++++++++++++++ 5 files changed, 36 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d99b58a178..daf6022fbb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -189,6 +189,7 @@ target_compile_definitions(_CuraEngine $<$,$>:ENABLE_REMOTE_PLUGINS> $<$:OLDER_APPLE_CLANG> CURA_ENGINE_VERSION=\"${CURA_ENGINE_VERSION}\" + CURA_ENGINE_HASH=\"${CURA_ENGINE_HASH}\" $<$:BUILD_TESTS> PRIVATE $<$:NOMINMAX> diff --git a/conanfile.py b/conanfile.py index d40e9c70c4..9f2b96c9c7 100644 --- a/conanfile.py +++ b/conanfile.py @@ -140,6 +140,7 @@ def generate(self): tc = CMakeToolchain(self) tc.variables["CURA_ENGINE_VERSION"] = self.version + tc.variables["CURA_ENGINE_HASH"] = self.conan_data["commit"] tc.variables["ENABLE_ARCUS"] = self.options.enable_arcus tc.variables["ENABLE_TESTING"] = not self.conf.get("tools.build:skip_test", False, check_type=bool) tc.variables["ENABLE_BENCHMARKS"] = self.options.enable_benchmarks diff --git a/include/communication/EmscriptenCommunication.h b/include/communication/EmscriptenCommunication.h index 5144b96dc6..30b09dd622 100644 --- a/include/communication/EmscriptenCommunication.h +++ b/include/communication/EmscriptenCommunication.h @@ -23,12 +23,14 @@ class EmscriptenCommunication : public CommandLine std::string progress_handler_; ///< Handler for progress messages. std::string gcode_header_handler_; ///< Handler for getting the GCode handler. std::string slice_info_handler_; ///< Handler for slice information messages. - + std::string engine_info_handler_; ///< Handler for curaengine info : version and hash. /** * \brief Creates a message containing slice information. * \return A string containing the slice information message. */ [[nodiscard]] static std::string createSliceInfoMessage(); + [[nodiscard]] static std::string createEngineInfoMessage(); + public: /** diff --git a/src/communication/CommandLine.cpp b/src/communication/CommandLine.cpp index e5f8b4340a..ae30bfae88 100644 --- a/src/communication/CommandLine.cpp +++ b/src/communication/CommandLine.cpp @@ -186,7 +186,7 @@ void CommandLine::sliceNext() force_read_parent = false; force_read_nondefault = false; } - else if (argument.starts_with("--progress_cb") || argument.starts_with("--slice_info_cb") || argument.starts_with("--gcode_header_cb")) + else if (argument.starts_with("--progress_cb") || argument.starts_with("--slice_info_cb") || argument.starts_with("--gcode_header_cb") || argument.starts_with("engine_info_cb")) { // Unused in command line slicing, but used in EmscriptenCommunication. argument_index++; diff --git a/src/communication/EmscriptenCommunication.cpp b/src/communication/EmscriptenCommunication.cpp index 8155290ee9..820b95623e 100644 --- a/src/communication/EmscriptenCommunication.cpp +++ b/src/communication/EmscriptenCommunication.cpp @@ -38,6 +38,10 @@ EmscriptenCommunication::EmscriptenCommunication(const std::vector& { gcode_header_handler_ = *ranges::next(gcode_header_flag); } + if (auto engine_info_flag = ranges::find(arguments_, "--engine_info_cb"); engine_info_flag != arguments_.end()) + { + engine_info_handler_ = *ranges::next(engine_info_flag); + } } void EmscriptenCommunication::sendGCodePrefix(const std::string& prefix) const @@ -109,9 +113,35 @@ std::string EmscriptenCommunication::createSliceInfoMessage() return buffer.GetString(); } +std::string EmscriptenCommunication::createEngineInfoMessage() +{ + // Construct a string with rapidjson containing the engine information + rapidjson::Document doc; + auto& allocator = doc.GetAllocator(); + doc.SetObject(); + + // Set the slicer version + rapidjson::Value version("version", allocator); + rapidjson::Value version_value(CURA_ENGINE_VERSION, allocator); + doc.AddMember(version, version_value, allocator); + + // Set the hash + rapidjson::Value hash("hash", allocator); + rapidjson::Value hash_value(CURA_ENGINE_HASH, allocator); + doc.AddMember(hash, hash_value, allocator); + + // Serialize the JSON document to a string + rapidjson::StringBuffer buffer; + rapidjson::Writer writer(buffer); + doc.Accept(writer); + return buffer.GetString(); +} + void EmscriptenCommunication::sliceNext() { CommandLine::sliceNext(); + auto engine_info = createEngineInfoMessage(); + emscripten_run_script(fmt::format("globalThis[\"{}\"]({})", engine_info_handler_, engine_info).c_str()); auto slice_info = createSliceInfoMessage(); emscripten_run_script(fmt::format("globalThis[\"{}\"]({})", slice_info_handler_, slice_info).c_str()); }; From 4ae78aa48763cf1ed7235e7a7f84eff4c092c55a Mon Sep 17 00:00:00 2001 From: saumyaj3 Date: Thu, 10 Oct 2024 09:05:29 +0000 Subject: [PATCH 2/7] Applied clang-format. --- src/communication/CommandLine.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/communication/CommandLine.cpp b/src/communication/CommandLine.cpp index ae30bfae88..def4b7864f 100644 --- a/src/communication/CommandLine.cpp +++ b/src/communication/CommandLine.cpp @@ -186,7 +186,9 @@ void CommandLine::sliceNext() force_read_parent = false; force_read_nondefault = false; } - else if (argument.starts_with("--progress_cb") || argument.starts_with("--slice_info_cb") || argument.starts_with("--gcode_header_cb") || argument.starts_with("engine_info_cb")) + else if ( + argument.starts_with("--progress_cb") || argument.starts_with("--slice_info_cb") || argument.starts_with("--gcode_header_cb") + || argument.starts_with("engine_info_cb")) { // Unused in command line slicing, but used in EmscriptenCommunication. argument_index++; From f9ebce702a1efd261150df3f9f95f9265472075f Mon Sep 17 00:00:00 2001 From: saumyaj3 Date: Thu, 10 Oct 2024 11:53:48 +0200 Subject: [PATCH 3/7] Add beginGCode function to EmscriptenCommunication Introduced a new function `beginGCode` to indicate the start of g-code sending with curaengine info. NP-349 --- include/communication/EmscriptenCommunication.h | 5 +++++ src/communication/EmscriptenCommunication.cpp | 7 +++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/include/communication/EmscriptenCommunication.h b/include/communication/EmscriptenCommunication.h index 30b09dd622..91901d8955 100644 --- a/include/communication/EmscriptenCommunication.h +++ b/include/communication/EmscriptenCommunication.h @@ -50,6 +50,11 @@ class EmscriptenCommunication : public CommandLine */ void sendGCodePrefix(const std::string& prefix) const override; + /** + * \brief Indicate that we're beginning to send g-code. + */ + void beginGCode() override; + /** * \brief Initiates the slicing of the next item. */ diff --git a/src/communication/EmscriptenCommunication.cpp b/src/communication/EmscriptenCommunication.cpp index 820b95623e..6b84fae2af 100644 --- a/src/communication/EmscriptenCommunication.cpp +++ b/src/communication/EmscriptenCommunication.cpp @@ -137,11 +137,14 @@ std::string EmscriptenCommunication::createEngineInfoMessage() return buffer.GetString(); } -void EmscriptenCommunication::sliceNext() +void EmscriptionCommunication::beginGCode() { - CommandLine::sliceNext(); auto engine_info = createEngineInfoMessage(); emscripten_run_script(fmt::format("globalThis[\"{}\"]({})", engine_info_handler_, engine_info).c_str()); +} +void EmscriptenCommunication::sliceNext() +{ + CommandLine::sliceNext(); auto slice_info = createSliceInfoMessage(); emscripten_run_script(fmt::format("globalThis[\"{}\"]({})", slice_info_handler_, slice_info).c_str()); }; From dc900d426353b01bdd86b05f666996a4d24651ad Mon Sep 17 00:00:00 2001 From: saumyaj3 Date: Thu, 10 Oct 2024 09:54:27 +0000 Subject: [PATCH 4/7] Applied clang-format. --- include/communication/EmscriptenCommunication.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/communication/EmscriptenCommunication.h b/include/communication/EmscriptenCommunication.h index 91901d8955..b1c7e7c903 100644 --- a/include/communication/EmscriptenCommunication.h +++ b/include/communication/EmscriptenCommunication.h @@ -50,7 +50,7 @@ class EmscriptenCommunication : public CommandLine */ void sendGCodePrefix(const std::string& prefix) const override; - /** + /** * \brief Indicate that we're beginning to send g-code. */ void beginGCode() override; From 45c585e3b3669f90b15e1f0084e184a0efd1ed23 Mon Sep 17 00:00:00 2001 From: saumyaj3 Date: Thu, 10 Oct 2024 12:05:52 +0200 Subject: [PATCH 5/7] Add commit hash to conandata.yml Include a 'commit' field in conandata.yml to store the commit hash. NP-349 --- conandata.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/conandata.yml b/conandata.yml index 909c921748..9d41341189 100644 --- a/conandata.yml +++ b/conandata.yml @@ -1,4 +1,5 @@ version: "5.9.0-alpha.0" +commit: "unknown" requirements: - "scripta/0.1.0@ultimaker/testing" requirements_arcus: From 916a92aabe7fada3fbfa2f1507969eb8edbaa2af Mon Sep 17 00:00:00 2001 From: saumyaj3 Date: Thu, 10 Oct 2024 12:48:04 +0200 Subject: [PATCH 6/7] fix typo NP-349 --- src/communication/EmscriptenCommunication.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/communication/EmscriptenCommunication.cpp b/src/communication/EmscriptenCommunication.cpp index 6b84fae2af..e62e68cf32 100644 --- a/src/communication/EmscriptenCommunication.cpp +++ b/src/communication/EmscriptenCommunication.cpp @@ -137,7 +137,7 @@ std::string EmscriptenCommunication::createEngineInfoMessage() return buffer.GetString(); } -void EmscriptionCommunication::beginGCode() +void EmscriptenCommunication::beginGCode() { auto engine_info = createEngineInfoMessage(); emscripten_run_script(fmt::format("globalThis[\"{}\"]({})", engine_info_handler_, engine_info).c_str()); From 6c4fad3399212ac85815623e2a560b02c5151def Mon Sep 17 00:00:00 2001 From: saumyaj3 Date: Thu, 10 Oct 2024 13:03:21 +0200 Subject: [PATCH 7/7] fix argument NP-349 --- src/communication/CommandLine.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/communication/CommandLine.cpp b/src/communication/CommandLine.cpp index def4b7864f..da4bef97e5 100644 --- a/src/communication/CommandLine.cpp +++ b/src/communication/CommandLine.cpp @@ -188,7 +188,7 @@ void CommandLine::sliceNext() } else if ( argument.starts_with("--progress_cb") || argument.starts_with("--slice_info_cb") || argument.starts_with("--gcode_header_cb") - || argument.starts_with("engine_info_cb")) + || argument.starts_with("--engine_info_cb")) { // Unused in command line slicing, but used in EmscriptenCommunication. argument_index++;