From befe80e578502b0485210861c28254d0c615d3fc Mon Sep 17 00:00:00 2001 From: imatrisciano Date: Sun, 3 Nov 2024 15:40:34 +0100 Subject: [PATCH] Allow sheets to have holes in holes --- python/packaide/packaide.py | 5 ++--- src/bindings.cpp | 19 +++++++++++++++++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/python/packaide/packaide.py b/python/packaide/packaide.py index e56e52e..ca530ec 100644 --- a/python/packaide/packaide.py +++ b/python/packaide/packaide.py @@ -8,7 +8,7 @@ from xml.dom import minidom from PackaideBindings import Point, Polygon, PolygonWithHoles, Sheet, State, Placement -from PackaideBindings import pack_decreasing, sheet_add_holes +from PackaideBindings import pack_decreasing, sheet_add_holes_with_holes # We want to preserve presentation and identification (e.g., id, name, class) attributes # when flattening the SVG elements and writing them into the output, so that the packed @@ -252,8 +252,7 @@ def pack(sheet_svgs, shapes, offset = 1, tolerance = 1, partial_solution = False sheet = Sheet() _, holes = extract_polygons(svg_string, tolerance, offset) sheet.height, sheet.width = get_sheet_dimensions(svg_string) - holes = [hole.boundary for hole in holes] - sheet_add_holes(sheet, holes, state) + sheet_add_holes_with_holes(sheet, holes, state) sheets.append(sheet) # Run the packing algorithm diff --git a/src/bindings.cpp b/src/bindings.cpp index 2b30a6a..ee66bf0 100644 --- a/src/bindings.cpp +++ b/src/bindings.cpp @@ -69,17 +69,31 @@ packaide::PolygonWithHoles boost_polygon_with_holes_convert(const Polygon_with_h // ------------------------------------------------------ // Helper functions -// Given a sheet object and a list of polyons, add those +// Given a sheet object and a list of polygons, add those // polygons as holes to the sheet void sheet_add_holes_bind(packaide::Sheet& sheet, boost::python::list polygons, packaide::State& state){ std::vector pgons; for(boost::python::ssize_t i=0; i(polygons[i]))); + auto python_polygon = boost::python::extract(polygons[i]); + auto packaide_polygon = packaide_polygon_convert(python_polygon); + auto hole = Polygon_with_holes_2(packaide_polygon); pgons.push_back(hole); } sheet.holes = pgons; } +// Given a sheet object and a list of polygons with holes, add those +// polygons as holes to the sheet +void sheet_add_holes_with_holes_bind(packaide::Sheet& sheet, boost::python::list polygons_with_holes, packaide::State& state){ + std::vector pgons; + for(boost::python::ssize_t i=0; i(polygons_with_holes[i]); + auto packaide_polygon = packaide_polygon_with_holes_convert(python_polygon); + pgons.push_back(packaide_polygon); + } + sheet.holes = pgons; +} + // ------------------------------------------------------ // Main packing function @@ -172,5 +186,6 @@ BOOST_PYTHON_MODULE(PackaideBindings) { class_("State", init<>()); def("sheet_add_holes", sheet_add_holes_bind); + def("sheet_add_holes_with_holes", sheet_add_holes_with_holes_bind); def("pack_decreasing", pack_decreasing_bind); }