Skip to content

Commit

Permalink
Added delete method for layers, fixed uuid conversion for python
Browse files Browse the repository at this point in the history
  • Loading branch information
fraguada committed Nov 19, 2024
1 parent 59dc784 commit 2719712
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/bindings/bnd_extensions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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())
Expand Down
1 change: 1 addition & 0 deletions src/bindings/bnd_extensions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/bindings/bnd_uuid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>(id);
#endif
return ON_UuidFromString(s.c_str());
}

Expand Down
27 changes: 27 additions & 0 deletions tests/python/test_File3dm_LayerTable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
24 changes: 23 additions & 1 deletion tests/python/test_File3dm_ObjectTable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
"""
Expand All @@ -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")
Expand Down

0 comments on commit 2719712

Please sign in to comment.