From 69021226ca1969fbcb000e28ef03c6b721f7bcbb Mon Sep 17 00:00:00 2001 From: fraguada Date: Wed, 20 Nov 2024 11:10:22 +0100 Subject: [PATCH] adding delete for DimStyleTable --- src/bindings/bnd_bitmap.cpp | 6 ++-- src/bindings/bnd_bitmap.h | 1 + src/bindings/bnd_dimensionstyle.cpp | 2 ++ src/bindings/bnd_dimensionstyle.h | 2 ++ src/bindings/bnd_extensions.cpp | 33 ++++++++++++++++--- src/bindings/bnd_extensions.h | 3 ++ tests/javascript/file3dm.BitmapTable.test.js | 2 +- .../javascript/file3dm.DimStyleTable.test.js | 33 +++++++++++++++++++ tests/python/test_File3dm_DimStyleTable.py | 33 +++++++++++++++++++ 9 files changed, 108 insertions(+), 7 deletions(-) create mode 100644 tests/javascript/file3dm.DimStyleTable.test.js create mode 100644 tests/python/test_File3dm_DimStyleTable.py diff --git a/src/bindings/bnd_bitmap.cpp b/src/bindings/bnd_bitmap.cpp index 677b7cf4..bdd8fe33 100644 --- a/src/bindings/bnd_bitmap.cpp +++ b/src/bindings/bnd_bitmap.cpp @@ -20,13 +20,14 @@ void BND_Bitmap::SetTrackedPointer(ON_Bitmap* bitmap, const ON_ModelComponentRef void initBitmapBindings(rh3dmpymodule& m) { - py::class_(m, "Bitmap") + py::class_(m, "Bitmap") .def(py::init<>()) .def_property_readonly("Width", &BND_Bitmap::Width) .def_property_readonly("Height", &BND_Bitmap::Height) .def_property_readonly("BitsPerPixel", &BND_Bitmap::BitsPerPixel) .def_property_readonly("SizeOfScan", &BND_Bitmap::SizeOfScan) .def_property_readonly("SizeOfImage", &BND_Bitmap::SizeOfImage) + .def_property_readonly("Id", &BND_Bitmap::GetId) ; } #endif @@ -36,13 +37,14 @@ using namespace emscripten; void initBitmapBindings(void*) { - class_("Bitmap") + class_>("Bitmap") .constructor<>() .property("width", &BND_Bitmap::Width) .property("height", &BND_Bitmap::Height) .property("bitsPerPixel", &BND_Bitmap::BitsPerPixel) .property("sizeOfScan", &BND_Bitmap::SizeOfScan) .property("sizeOfImage", &BND_Bitmap::SizeOfImage) + .property("id", &BND_Bitmap::GetId) ; } #endif diff --git a/src/bindings/bnd_bitmap.h b/src/bindings/bnd_bitmap.h index 53bfecbd..a6ba8f22 100644 --- a/src/bindings/bnd_bitmap.h +++ b/src/bindings/bnd_bitmap.h @@ -25,6 +25,7 @@ class BND_Bitmap : public BND_CommonObject //const unsigned char* Bits(int scan_line_index) const; //const ON_FileReference& FileReference() const; //void SetFileReference(const ON_FileReference& file_reference); + BND_UUID GetId() const { return ON_UUID_to_Binding( m_bitmap->Id()); } void SetFileFullPath(std::wstring path) { m_bitmap->SetFileFullPath(path.c_str(), true); } protected: diff --git a/src/bindings/bnd_dimensionstyle.cpp b/src/bindings/bnd_dimensionstyle.cpp index 8590edd2..b5f3cc4b 100644 --- a/src/bindings/bnd_dimensionstyle.cpp +++ b/src/bindings/bnd_dimensionstyle.cpp @@ -97,6 +97,7 @@ void initDimensionStyleBindings(rh3dmpymodule& m) .def_property_readonly("IsChild", &BND_DimensionStyle::IsChild) .def("IsChildOf", &BND_DimensionStyle::IsChildOf, py::arg("id")) .def_property("ParentId", &BND_DimensionStyle::GetParentId, &BND_DimensionStyle::SetParentId) + .def_property_readonly("Id", &BND_DimensionStyle::GetId) ; py::enum_(pyDimStyle, "Field") @@ -261,6 +262,7 @@ void initDimensionStyleBindings(void*) .property("isChild", &BND_DimensionStyle::IsChild) .function("isChildOf", &BND_DimensionStyle::IsChildOf) .property("parentId", &BND_DimensionStyle::GetParentId, &BND_DimensionStyle::SetParentId) + .property("id", &BND_DimensionStyle::GetId) ; } #endif diff --git a/src/bindings/bnd_dimensionstyle.h b/src/bindings/bnd_dimensionstyle.h index 604e2fe6..a2314e4e 100644 --- a/src/bindings/bnd_dimensionstyle.h +++ b/src/bindings/bnd_dimensionstyle.h @@ -30,6 +30,8 @@ class BND_DimensionStyle : public BND_CommonObject class BND_Font* GetFont() const; void SetFont(const class BND_Font* font); + BND_UUID GetId() const { return ON_UUID_to_Binding( m_dimstyle->Id()); } + void ScaleLengthValues(double scale) { m_dimstyle->Scale(scale); } BND_UUID GetArrowBlockId1() const { return ON_UUID_to_Binding(m_dimstyle->ArrowBlockId1()); } void SetArrowBlockId1(BND_UUID id) { m_dimstyle->SetArrowBlockId1(Binding_to_ON_UUID(id)); } diff --git a/src/bindings/bnd_extensions.cpp b/src/bindings/bnd_extensions.cpp index cb5a2159..ab6caf0c 100644 --- a/src/bindings/bnd_extensions.cpp +++ b/src/bindings/bnd_extensions.cpp @@ -418,6 +418,12 @@ static ON_UUID Internal_ONX_Model_AddModelGeometry( return ON_ModelGeometryComponent::FromModelComponentRef(model_component_reference, &ON_ModelGeometryComponent::Unset)->Id(); } +static bool DeleteModelComponent(BND_UUID id, ON_ModelComponent::Type objectType, std::shared_ptr m_model) +{ + ON_UUID _id = Binding_to_ON_UUID(id); + ON_ModelComponentReference compref = m_model->RemoveModelComponent(objectType, _id); + return !compref.IsEmpty(); +} BND_UUID BND_ONXModel_ObjectTable::AddPoint1(double x, double y, double z) { @@ -763,6 +769,11 @@ int BND_File3dmMaterialTable::Add(const BND_Material& material) return material_index; } +bool BND_File3dmMaterialTable::Delete(BND_UUID id) +{ + return DeleteModelComponent(id, ON_ModelComponent::Type::Material, m_model); +} + BND_Material* BND_File3dmMaterialTable::IterIndex(int index) { return FindIndex(index); @@ -923,9 +934,14 @@ void BND_File3dmBitmapTable::Add(const BND_Bitmap& bitmap) m_model->AddModelComponent(*b); } +bool BND_File3dmBitmapTable::Delete(BND_UUID id) +{ + return DeleteModelComponent(id, ON_ModelComponent::Type::Image, m_model); +} + BND_Bitmap* BND_File3dmBitmapTable::FindIndex(int index) { - ON_ModelComponentReference compref = m_model->MaterialFromIndex(index); + ON_ModelComponentReference compref = m_model->ImageFromIndex(index); const ON_ModelComponent* model_component = compref.ModelComponent(); ON_Bitmap* modelbitmap = const_cast(ON_Bitmap::Cast(model_component)); if (modelbitmap) @@ -972,9 +988,7 @@ int BND_File3dmLayerTable::AddLayer(std::wstring name, BND_Color color) 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(); + return DeleteModelComponent(id, ON_ModelComponent::Type::Layer, m_model); } BND_Layer* BND_File3dmLayerTable::FindName(std::wstring name, BND_UUID parentId) @@ -1175,6 +1189,11 @@ void BND_File3dmDimStyleTable::Add(const BND_DimensionStyle& dimstyle) m_model->AddModelComponent(*ds); } +bool BND_File3dmDimStyleTable::Delete(BND_UUID id) +{ + return DeleteModelComponent(id, ON_ModelComponent::Type::DimStyle, m_model); +} + BND_DimensionStyle* BND_File3dmDimStyleTable::FindIndex(int index) const { ON_ModelComponentReference compref = m_model->DimensionStyleFromIndex(index); @@ -1724,6 +1743,7 @@ void initExtensionsBindings(rh3dmpymodule& m) .def("__iter__", [](py::object s) { return PyBNDIterator(s.cast(), s); }) #endif .def("Add", &BND_File3dmMaterialTable::Add, py::arg("material")) + .def("Delete", &BND_File3dmMaterialTable::Delete, py::arg("id")) .def("FindIndex", &BND_File3dmMaterialTable::FindIndex, py::arg("index")) .def("FindId", &BND_File3dmMaterialTable::FindId, py::arg("id")) .def("FindFromAttributes", &BND_File3dmMaterialTable::FromAttributes) @@ -1765,6 +1785,7 @@ void initExtensionsBindings(rh3dmpymodule& m) .def("__iter__", [](py::object s) { return PyBNDIterator(s.cast(), s); }) #endif .def("Add", &BND_File3dmBitmapTable::Add, py::arg("bitmap")) + .def("Delete", &BND_File3dmBitmapTable::Delete, py::arg("id")) .def("FindIndex", &BND_File3dmBitmapTable::FindIndex, py::arg("index")) .def("FindId", &BND_File3dmBitmapTable::FindId, py::arg("id")) ; @@ -1820,6 +1841,7 @@ void initExtensionsBindings(rh3dmpymodule& m) .def("__iter__", [](py::object s) { return PyBNDIterator(s.cast(), s); }) #endif .def("Add", &BND_File3dmDimStyleTable::Add, py::arg("dimstyle")) + .def("Delete", &BND_File3dmDimStyleTable::Delete, py::arg("id")) .def("FindIndex", &BND_File3dmDimStyleTable::FindIndex, py::arg("index")) .def("FindId", &BND_File3dmDimStyleTable::FindId, py::arg("id")) ; @@ -2066,6 +2088,7 @@ void initExtensionsBindings(void*) .property("count", &BND_File3dmMaterialTable::Count) .function("get", &BND_File3dmMaterialTable::FindIndex, allow_raw_pointers()) .function("add", &BND_File3dmMaterialTable::Add) + .function("delete", &BND_File3dmMaterialTable::Delete) .function("findIndex", &BND_File3dmMaterialTable::FindIndex, allow_raw_pointers()) .function("findId", &BND_File3dmMaterialTable::FindId, allow_raw_pointers()) .function("findFromAttributes", &BND_File3dmMaterialTable::FromAttributes, allow_raw_pointers()) @@ -2087,6 +2110,7 @@ void initExtensionsBindings(void*) .property("count", &BND_File3dmBitmapTable::Count) .function("get", &BND_File3dmBitmapTable::FindIndex, allow_raw_pointers()) .function("add", &BND_File3dmBitmapTable::Add) + .function("delete", &BND_File3dmBitmapTable::Delete) .function("findIndex", &BND_File3dmBitmapTable::FindIndex, allow_raw_pointers()) .function("findId", &BND_File3dmBitmapTable::FindId, allow_raw_pointers()) ; @@ -2117,6 +2141,7 @@ void initExtensionsBindings(void*) .property("count", &BND_File3dmDimStyleTable::Count) .function("get", &BND_File3dmDimStyleTable::FindIndex, allow_raw_pointers()) .function("add", &BND_File3dmDimStyleTable::Add) + .function("delete", &BND_File3dmDimStyleTable::Delete) .function("findIndex", &BND_File3dmDimStyleTable::FindIndex, allow_raw_pointers()) .function("findId", &BND_File3dmDimStyleTable::FindId, allow_raw_pointers()) ; diff --git a/src/bindings/bnd_extensions.h b/src/bindings/bnd_extensions.h index 12ef4a50..e8d0d484 100644 --- a/src/bindings/bnd_extensions.h +++ b/src/bindings/bnd_extensions.h @@ -105,6 +105,7 @@ class BND_File3dmMaterialTable BND_File3dmMaterialTable(std::shared_ptr m) { m_model = m; } int Count() const { return m_model->ActiveComponentCount(ON_ModelComponent::Type::RenderMaterial); } int Add(const class BND_Material& material); + bool Delete(BND_UUID id); class BND_Material* FindIndex(int index); class BND_Material* IterIndex(int index); // helper function for iterator class BND_Material* FindId(BND_UUID id); @@ -140,6 +141,7 @@ class BND_File3dmBitmapTable BND_File3dmBitmapTable(std::shared_ptr m) { m_model = m; } int Count() const { return m_model.get()->ActiveComponentCount(ON_ModelComponent::Type::Image); } void Add(const class BND_Bitmap& bitmap); + bool Delete(BND_UUID id); class BND_Bitmap* FindIndex(int index); class BND_Bitmap* IterIndex(int index); // helper function for iterator class BND_Bitmap* FindId(BND_UUID id); @@ -184,6 +186,7 @@ class BND_File3dmDimStyleTable BND_File3dmDimStyleTable(std::shared_ptr m) { m_model = m; } int Count() const { return m_model.get()->ActiveComponentCount(ON_ModelComponent::Type::DimStyle); } void Add(const class BND_DimensionStyle& dimstyle); + bool Delete(BND_UUID id); class BND_DimensionStyle* FindIndex(int index) const; class BND_DimensionStyle* IterIndex(int index) const; // helper function for iterator class BND_DimensionStyle* FindId(BND_UUID id) const; diff --git a/tests/javascript/file3dm.BitmapTable.test.js b/tests/javascript/file3dm.BitmapTable.test.js index ae1cb4ba..be254cac 100644 --- a/tests/javascript/file3dm.BitmapTable.test.js +++ b/tests/javascript/file3dm.BitmapTable.test.js @@ -28,6 +28,6 @@ test.skip('DeleteBitmap', async () => { const qtyBitmaps2 = model.bitmaps().count - expect(qtyDims1 === 2 && qtyDims2 === 1).toBe(true) + expect(qtyBitmaps1 === 2 && qtyBitmaps2 === 1).toBe(true) }) \ No newline at end of file diff --git a/tests/javascript/file3dm.DimStyleTable.test.js b/tests/javascript/file3dm.DimStyleTable.test.js new file mode 100644 index 00000000..0564fb86 --- /dev/null +++ b/tests/javascript/file3dm.DimStyleTable.test.js @@ -0,0 +1,33 @@ +const rhino3dm = require('rhino3dm') + +let rhino +beforeEach( async() => { + rhino = await rhino3dm() + }) +//TODO +// Skipping for now. +test('DeleteDimStyle', async () => { + + const file3dm = new rhino.File3dm() + file3dm.applicationName = 'rhino3dm.js' + file3dm.applicationDetails = 'rhino3dm-tests-dimStyleTable-deleteDimStyle' + file3dm.applicationUrl = 'https://rhino3d.com' + + const ds1 = new rhino.DimensionStyle() + const ds2 = new rhino.DimensionStyle() + + // .bitmaps().add() is void + model.dimstyles().add(ds1) + model.dimstyles().add(ds2) + + const qtyDimStyles1 = model.dimstyles().count + + const id1 = model.dimstyles().get(0).id + + model.dimstyles().delete(id1) + + const qtyDimStyles2 = model.dimstyles().count + + expect(qtyDimStyles1 === 2 && qtyDimStyles2 === 1).toBe(true) + +}) \ No newline at end of file diff --git a/tests/python/test_File3dm_DimStyleTable.py b/tests/python/test_File3dm_DimStyleTable.py new file mode 100644 index 00000000..f0a08d30 --- /dev/null +++ b/tests/python/test_File3dm_DimStyleTable.py @@ -0,0 +1,33 @@ +import rhino3dm +import unittest + +#objective: to test creating file with bitmaps +class TestFile3dmBitmapTable(unittest.TestCase): + + def test_deleteDimStyle(self): + file3dm = rhino3dm.File3dm() + file3dm.ApplicationName = 'python' + file3dm.ApplicationDetails = 'rhino3dm-tests-deleteDimStyle' + file3dm.ApplicationUrl = 'https://rhino3d.com' + + #create bitmaps + bm1 = rhino3dm.DimensionStyle() + bm2 = rhino3dm.DimensionStyle() + + file3dm.DimStyles.Add(bm1) + file3dm.DimStyles.Add(bm2) + + qtyDimStyles1 = len(file3dm.DimStyles) + + id1 = file3dm.DimStyles[0].Id + + file3dm.DimStyles.Delete(id1) + + qtyDimStyles2 = len(file3dm.DimStyles) + + self.assertTrue(qtyDimStyles1 == 2 and qtyDimStyles2 == 1) + +if __name__ == '__main__': + print("running tests") + unittest.main() + print("tests complete") \ No newline at end of file