diff --git a/src/bindings/bnd_extensions.cpp b/src/bindings/bnd_extensions.cpp index 2c71cf0d..cb5a2159 100644 --- a/src/bindings/bnd_extensions.cpp +++ b/src/bindings/bnd_extensions.cpp @@ -954,7 +954,6 @@ BND_Bitmap* BND_File3dmBitmapTable::FindId(BND_UUID id) return nullptr; } - int BND_File3dmLayerTable::Add(const BND_Layer& layer) { const ON_Layer* l = layer.m_layer; @@ -971,6 +970,13 @@ int BND_File3dmLayerTable::AddLayer(std::wstring name, BND_Color color) return rc; } +bool BND_File3dmLayerTable::Delete(BND_UUID id) +{ + ON_UUID _id = Binding_to_ON_UUID(id); + ON_ModelComponentReference compref = m_model->RemoveModelComponent(ON_ModelComponent::Type::Layer, _id); + return !compref.IsEmpty(); +} + BND_Layer* BND_File3dmLayerTable::FindName(std::wstring name, BND_UUID parentId) { ON_UUID id = Binding_to_ON_UUID(parentId); @@ -1776,6 +1782,7 @@ void initExtensionsBindings(rh3dmpymodule& m) #endif .def("Add", &BND_File3dmLayerTable::Add, py::arg("layer")) .def("AddLayer", &BND_File3dmLayerTable::AddLayer, py::arg("name"), py::arg("color")) + .def("Delete", &BND_File3dmLayerTable::Delete, py::arg("id")) .def("FindName", &BND_File3dmLayerTable::FindName, py::arg("name"), py::arg("parentId")) .def("FindIndex", &BND_File3dmLayerTable::FindIndex, py::arg("index")) .def("FindId", &BND_File3dmLayerTable::FindId, py::arg("id")) @@ -2089,6 +2096,7 @@ void initExtensionsBindings(void*) .function("get", &BND_File3dmLayerTable::FindIndex, allow_raw_pointers()) .function("add", &BND_File3dmLayerTable::Add) .function("addLayer", &BND_File3dmLayerTable::AddLayer) + .function("delete", &BND_File3dmLayerTable::Delete) .function("findName", &BND_File3dmLayerTable::FindName, allow_raw_pointers()) .function("findIndex", &BND_File3dmLayerTable::FindIndex, allow_raw_pointers()) .function("findId", &BND_File3dmLayerTable::FindId, allow_raw_pointers()) diff --git a/src/bindings/bnd_extensions.h b/src/bindings/bnd_extensions.h index f672df68..12ef4a50 100644 --- a/src/bindings/bnd_extensions.h +++ b/src/bindings/bnd_extensions.h @@ -153,6 +153,7 @@ class BND_File3dmLayerTable int Count() const { return m_model.get()->ActiveComponentCount(ON_ModelComponent::Type::Layer); } int Add(const class BND_Layer& layer); int AddLayer(std::wstring name, BND_Color color); + bool Delete(BND_UUID id); class BND_Layer* FindName(std::wstring name, BND_UUID parentId); //BND_Layer* FindNameHash(NameHash nameHash) class BND_Layer* FindIndex(int index); diff --git a/src/bindings/bnd_uuid.cpp b/src/bindings/bnd_uuid.cpp index 2c9fd824..7df614ad 100644 --- a/src/bindings/bnd_uuid.cpp +++ b/src/bindings/bnd_uuid.cpp @@ -20,7 +20,11 @@ BND_UUID ON_UUID_to_Binding(const ON_UUID& id) ON_UUID Binding_to_ON_UUID(const BND_UUID& id) { +#if !defined(NANOBIND) + std::string s = pybind11::str(id); +#else std::string s = py::cast(id); +#endif return ON_UuidFromString(s.c_str()); } diff --git a/tests/python/test_File3dm_LayerTable.py b/tests/python/test_File3dm_LayerTable.py index e1796725..f11fb0cc 100644 --- a/tests/python/test_File3dm_LayerTable.py +++ b/tests/python/test_File3dm_LayerTable.py @@ -30,6 +30,33 @@ def test_createFileWithLayers(self): self.assertTrue(qtyLayers == 2 and qtyLayers2 == 2) + def test_deleteLayer(self): + file3dm = rhino3dm.File3dm() + file3dm.ApplicationName = 'python' + file3dm.ApplicationDetails = 'rhino3dm-tests-deleteLayer' + file3dm.ApplicationUrl = 'https://rhino3d.com' + + #create layers + layer1 = rhino3dm.Layer() + layer1.Name = 'layer1' + layer1.Color = (255,0,255,255) + + layer2 = rhino3dm.Layer() + layer2.Name = 'layer2' + + index1 = file3dm.Layers.Add(layer1) + index2 = file3dm.Layers.Add(layer2) + + qtyLayers = len(file3dm.Layers) + + id1 = file3dm.Layers[index1].Id + + file3dm.Layers.Delete(id1) + + qtyLayers2 = len(file3dm.Layers) + + self.assertTrue(qtyLayers == 2 and qtyLayers2 == 1) + if __name__ == '__main__': print("running tests") unittest.main() diff --git a/tests/python/test_File3dm_ObjectTable.py b/tests/python/test_File3dm_ObjectTable.py index 80c3a2f1..5b3ab06a 100644 --- a/tests/python/test_File3dm_ObjectTable.py +++ b/tests/python/test_File3dm_ObjectTable.py @@ -24,7 +24,6 @@ def test_addPolyline(self): self.assertTrue(objqty == 2 and isCurve1 and isCurve2 and len1 == 15 and len2 == 15) - def test_negativeIndexing(self) -> None: """Tests for indexing `ObjectTable`. """ @@ -44,6 +43,29 @@ def test_negativeIndexing(self) -> None: with self.subTest(msg="Test negative indexing"): self.assertEqual(file_3dm.Objects[-2].Geometry.Location, rhino3dm.Point3d(0, 0, 0)) + def test_deleteObject(self): + file3dm = rhino3dm.File3dm() + file3dm.ApplicationName = 'python' + file3dm.ApplicationDetails = 'rhino3dm-tests-deleteLayer' + file3dm.ApplicationUrl = 'https://rhino3d.com' + + #create objects + circle = rhino3dm.Circle(5) + point = rhino3dm.Point3d(0,0,0) + id1 = file3dm.Objects.AddCircle(circle) + print("delete object") + print(id1) + id2 = file3dm.Objects.AddPoint(rhino3dm.Point3d(0,0,0)) + + qtyObjects = len(file3dm.Objects) + + file3dm.Objects.Delete(id1) + + qtyObjects2 = len(file3dm.Objects) + + self.assertTrue(qtyObjects == 2 and qtyObjects2 == 1) + + if __name__ == '__main__': print("running tests")