Skip to content

Commit

Permalink
Added robustness
Browse files Browse the repository at this point in the history
Changes from CURA-10619

CURA-10992
  • Loading branch information
saumyaj3 committed Oct 18, 2023
1 parent 76fdd60 commit f9712fd
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 78 deletions.
23 changes: 5 additions & 18 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.23)
cmake_minimum_required(VERSION 3.25)
project(curaengine_plugin_infill_generate)

include(CheckIPOSupported)
Expand All @@ -13,7 +13,7 @@ find_package(range-v3 REQUIRED)
find_package(clipper REQUIRED)
find_package(ctre REQUIRED)
find_package(neargye-semver REQUIRED)
find_package(standardprojectsettings REQUIRED)

add_executable(curaengine_plugin_infill_generate src/main.cpp)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
Expand All @@ -25,23 +25,10 @@ else()
message(STATUS "IPO / LTO not supported: <${error}>")
endif()

set(HDRS include/plugin/broadcast.h
include/plugin/cmdline.h
include/plugin/handshake.h
include/plugin/metadata.h
include/plugin/generate.h
include/plugin/plugin.h
include/plugin/settings.h)

add_library(curaengine_plugin_infill_generate_lib INTERFACE ${HDRS})

use_threads(curaengine_plugin_infill_generate_lib)
target_link_libraries(curaengine_plugin_infill_generate_lib INTERFACE asio-grpc::asio-grpc curaengine_grpc_definitions::curaengine_grpc_definitions boost::boost clipper::clipper ctre::ctre spdlog::spdlog docopt_s range-v3::range-v3 neargye-semver::neargye-semver)
target_include_directories(curaengine_plugin_infill_generate_lib
INTERFACE
target_include_directories(curaengine_plugin_infill_generate
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)

target_link_libraries(curaengine_plugin_infill_generate PUBLIC curaengine_plugin_infill_generate_lib)

target_link_libraries(curaengine_plugin_infill_generate PUBLIC asio-grpc::asio-grpc curaengine_grpc_definitions::curaengine_grpc_definitions boost::boost clipper::clipper ctre::ctre spdlog::spdlog docopt_s range-v3::range-v3 neargye-semver::neargye-semver)
2 changes: 1 addition & 1 deletion CuraEngineInfillGenerate/infill_settings.def.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"tile_size": {
"label": "Tile Size",
"description": "Size of the tiles.",
"description": "Size of the tiles. This eventually affects the infill density of the object",
"unit": "mm",
"type": "float",
"minimum_value": "0.5",
Expand Down
2 changes: 0 additions & 2 deletions CuraEngineInfillGenerate/tiles/lightning.wkt

This file was deleted.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This Engine plugin extends the current infill patterns in CURA with:
- Continuous Honeycomb
- Normal Honey Comb
- Cura
- Fill
- Honeycomb Fill

NOTE: Please note that this plugin is Experimental and adding custom infills is not possible at the moment.

Expand All @@ -30,4 +30,4 @@ cd CuraEngine_plugin_infill_generate
conan install . --build=missing --update -s build_type=Debug/Release
```

[For more info](https://img.shields.io/badge/Internals-00979D?style=for-the-badge&logoColor=white&logo=CodeReview)
[For more info](https://github.com/Ultimaker/CuraEngine/wiki/Building-CuraEngine-From-Source)
2 changes: 0 additions & 2 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ def configure(self):

def layout(self):
cmake_layout(self)
self.cpp.package.resdirs = [os.path.join("res", "plugins", self._cura_plugin_name).replace("\\", "/"),
os.path.join("res", "bundled_packages").replace("\\", "/")]

def requirements(self):
self.requires("curaengine_grpc_definitions/latest@ultimaker/testing")
Expand Down
109 changes: 61 additions & 48 deletions include/plugin/generate.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "plugin/settings.h"

#include <boost/asio/awaitable.hpp>
#include <fmt/format.h>
#include <spdlog/spdlog.h>

#if __has_include(<coroutine>)
Expand Down Expand Up @@ -43,33 +44,40 @@ struct Generate
cura::plugins::slots::infill::v0::generate::CallRequest request;
grpc::ServerAsyncResponseWriter<Rsp> writer{ &server_context };
co_await agrpc::request(&T::RequestCall, *generate_service, server_context, request, writer, boost::asio::use_awaitable);
const auto pattern = Settings::getPattern(request.pattern(), metadata->plugin_name);
const auto pattern_setting = Settings::getPattern(request.pattern(), metadata->plugin_name);
const auto tile_type_setting = Settings::retrieveSettings("tile_shape", request, metadata);
const auto tile_size_setting = Settings::retrieveSettings("tile_size", request, metadata);
const auto absolute_tiles_setting = Settings::retrieveSettings("absolute_tiles", request, metadata);

auto tile_type_getter = [&](std::string tile_info_)
if (! pattern_setting.has_value() || ! tile_type_setting.has_value() || ! tile_size_setting.has_value() || ! absolute_tiles_setting.has_value())
{
const auto tile_info = Settings::retrieveSettings(tile_info_, request, metadata);
if (tile_info.has_value())
{
status = grpc::Status::OK;
return Settings::getTileType(tile_info.value());
}
status = grpc::Status(grpc::StatusCode::INTERNAL, "Plugin could not find the key: " + tile_info_);
spdlog::info("Plugin could not find the key {}", tile_info_);

return infill::TileType::NONE;
};

const auto tile_type = tile_type_getter("tile_shape");
spdlog::error(
"pattern: {}, tile_shape: {}, tile size: {}, absolute tiles: {}",
pattern_setting.has_value(),
tile_type_setting.has_value(),
tile_size_setting.has_value(),
absolute_tiles_setting.has_value());
status = grpc::Status(
grpc::StatusCode::INTERNAL,
fmt::format(
"Plugin could not retrieve settings! pattern: {}, tile_shape: {}, tile size: {}, absolute tiles: {}",
pattern_setting.has_value(),
tile_type_setting.has_value(),
tile_size_setting.has_value(),
absolute_tiles_setting.has_value()));
}

if (! status.ok()) {
if (! status.ok())
{
co_await agrpc::finish_with_error(writer, status, boost::asio::use_awaitable);
continue;
}

const int64_t tile_size = std::stoll(Settings::retrieveSettings("tile_size", request, metadata).value()) * 1000;
const bool absolute_tiles = (Settings::retrieveSettings("absolute_tiles", request, metadata).value()) == "True";

Rsp response;
const auto tile_type = Settings::getTileType(tile_type_setting.value());
const int64_t tile_size = std::stoll(tile_size_setting.value()) * 1000;
const bool absolute_tiles = absolute_tiles_setting.value() == "True" || absolute_tiles_setting.value() == "true";
spdlog::info(tile_size);
spdlog::info(absolute_tiles);
auto client_metadata = getUuid(server_context);

auto outlines = std::vector<infill::geometry::polygon_outer<>>{};
Expand All @@ -92,52 +100,57 @@ struct Generate
}
}

Rsp response;

ClipperLib::Paths lines;
ClipperLib::Paths polys;
try
{
auto [lines, polys] = generator.generate(outlines, pattern, tile_size, absolute_tiles, tile_type);

// convert poly_lines to protobuf response
auto* poly_lines_msg = response.mutable_poly_lines();
for (auto& poly_line : lines)
{
auto* path_msg = poly_lines_msg->add_paths();
for (auto& point : poly_line)
{
auto* point_msg = path_msg->add_path();
point_msg->set_x(point.X);
point_msg->set_y(point.Y);
}
}

auto* polygons_msg = response.mutable_polygons();
auto [lines_, polys_] = generator.generate(outlines, pattern_setting.value(), tile_size, absolute_tiles, tile_type);

for (const auto& pp : polys)
{
auto* path_msg = polygons_msg->add_polygons()->mutable_outline();
for (const auto& point : pp)
{
auto* point_msg = path_msg->add_path();
point_msg->set_x(point.X);
point_msg->set_y(point.Y);
}
}
}
catch (const std::exception& e)
{
spdlog::error("Error: {}", e.what());
status = grpc::Status(grpc::StatusCode::INTERNAL, static_cast<std::string>(e.what()));
}

if (! status.ok())
{
co_await agrpc::finish_with_error(writer, status, boost::asio::use_awaitable);
continue;
}

// convert poly_lines to protobuf response
auto* poly_lines_msg = response.mutable_poly_lines();
for (auto& poly_line : lines)
{
auto* path_msg = poly_lines_msg->add_paths();
for (auto& point : poly_line)
{
auto* point_msg = path_msg->add_path();
point_msg->set_x(point.X);
point_msg->set_y(point.Y);
}
}

auto* polygons_msg = response.mutable_polygons();

for (const auto& pp : polys)
{
auto* path_msg = polygons_msg->add_polygons()->mutable_outline();
for (const auto& point : pp)
{
auto* point_msg = path_msg->add_path();
point_msg->set_x(point.X);
point_msg->set_y(point.Y);
}
}

co_await agrpc::finish(writer, response, status, boost::asio::use_awaitable);
}
}
};

} // namespace plugin::infill_generate

#endif // PLUGIN_GENERATE_H
#endif // PLUGIN_GENERATE_H
12 changes: 8 additions & 4 deletions include/plugin/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ struct Settings
line_distance = std::stoll(global_settings.at("infill_line_distance"));
}

static constexpr std::string_view getPattern(std::string_view pattern, std::string_view name)
{
static constexpr std::optional<std::string_view> getPattern(std::string_view pattern, std::string_view name)
{
if (auto [_, setting_namespace, plugin_name, plugin_version, pattern_name] = ctre::match<"^(.*?)::(.*?)@(.*?)::(.*?)$">(pattern);
setting_namespace == "PLUGIN" && plugin_name == name)
{
return pattern_name;
}
return pattern;
return std::nullopt;
}

static constexpr infill::TileType getTileType(std::string_view tile_type)
Expand All @@ -45,7 +45,11 @@ struct Settings
{
return infill::TileType::SQUARE;
}
return infill::TileType::HEXAGON;
if (tile_type == "hexagon")
{
return infill::TileType::HEXAGON;
}
return infill::TileType::NONE;
}


Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ int main(int argc, const char** argv)
cura::plugins::slots::infill::v0::generate::CallRequest>;

plugin::Plugin<generate_t> plugin{ args.at("--address").asString(), args.at("--port").asString(), grpc::InsecureServerCredentials() };
plugin.addHandshakeService(plugin::Handshake{ .metadata = plugin.metadata });
plugin.addHandshakeService(plugin::Handshake{ .metadata = plugin.metadata, .broadcast_subscriptions = { cura::plugins::v0::SlotID::SETTINGS_BROADCAST } });

auto broadcast_settings = std::make_shared<plugin::Broadcast::settings_t>();
plugin.addBroadcastService(plugin::Broadcast{ .settings = broadcast_settings });
Expand Down

0 comments on commit f9712fd

Please sign in to comment.