From c63959c89bee17ae526532c9745f527a7682a4e5 Mon Sep 17 00:00:00 2001 From: StudioWEngineers Date: Wed, 23 Oct 2024 13:47:40 +0100 Subject: [PATCH 1/4] remove trailing whitespaces --- src/bindings/bnd_extensions.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/bindings/bnd_extensions.cpp b/src/bindings/bnd_extensions.cpp index 4bab7cb7..16fbfe7a 100644 --- a/src/bindings/bnd_extensions.cpp +++ b/src/bindings/bnd_extensions.cpp @@ -14,7 +14,7 @@ static bool SeekPastCompressedBuffer(ON_BinaryArchive& archive) size_t sizeof__outbuffer; if (!archive.ReadCompressedBufferSize(&sizeof__outbuffer)) return false; - + if (0 == sizeof__outbuffer) return true; @@ -464,13 +464,13 @@ BND_UUID BND_ONXModel_ObjectTable::AddPolyline2(const std::vector& p BND_UUID BND_ONXModel_ObjectTable::AddPolyline3(emscripten::val points, const class BND_3dmObjectAttributes* attributes) { bool isArray = points.hasOwnProperty("length"); - if( isArray ) + if( isArray ) { const std::vector array = emscripten::vecFromJSArray(points); - return AddPolyline2( array, attributes ); + return AddPolyline2( array, attributes ); } else - return AddPolyline1( points.as(), attributes ); + return AddPolyline1( points.as(), attributes ); } #endif @@ -1217,7 +1217,7 @@ int BND_File3dmInstanceDefinitionTable::Add(std::wstring name, std::wstring desc const int count_a = attributes["length"].as(); #endif - if(m_model && count_g > 0) + if(m_model && count_g > 0) { // Determine if we need to transform geometry to world origin ON_Xform xf; @@ -1230,17 +1230,17 @@ int BND_File3dmInstanceDefinitionTable::Add(std::wstring name, std::wstring desc ON_SimpleArray object_uuids; - for ( int i = 0; i < count_g; i ++ ) + for ( int i = 0; i < count_g; i ++ ) { #if defined(ON_PYTHON_COMPILE) BND_GeometryBase g = py::cast(geometry[i]); BND_3dmObjectAttributes oa = py::cast(attributes[i]); #else - BND_GeometryBase g = geometry[i].as(); + BND_GeometryBase g = geometry[i].as(); BND_3dmObjectAttributes oa = attributes[i].as(); #endif - + const ON_Geometry* pConstGeom = g.GeometryPointer(); const ON_3dmObjectAttributes* pConstAtts = i < count_a ? oa.m_attributes : &ON_3dmObjectAttributes::DefaultAttributes; @@ -1302,7 +1302,7 @@ int BND_File3dmInstanceDefinitionTable::Add(std::wstring name, std::wstring desc } } } - + return index; } @@ -1614,7 +1614,7 @@ BND_TUPLE BND_FileObject::GetTextureMapping( const class BND_File3dm* file3dm, i if(tm == ON_TextureMapping::Unset) return NullTuple(); - else + else { SetTuple(rc, 0, BND_TextureMapping(tm, nullptr)); SetTuple(rc, 1, BND_Transform(m_object_xform)); From 1562b7b5ca379595d92f29179c92cc2735407970 Mon Sep 17 00:00:00 2001 From: StudioWEngineers Date: Wed, 23 Oct 2024 14:14:44 +0100 Subject: [PATCH 2/4] add support for negative indexing Python #651 --- src/bindings/bnd_extensions.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/bindings/bnd_extensions.cpp b/src/bindings/bnd_extensions.cpp index 16fbfe7a..4eabac4a 100644 --- a/src/bindings/bnd_extensions.cpp +++ b/src/bindings/bnd_extensions.cpp @@ -669,10 +669,7 @@ static BND_FileObject* FileObjectFromCompRef(ON_ModelComponentReference& compref BND_FileObject* BND_ONXModel_ObjectTable::ModelObjectAt(int index) { -#if defined(ON_PYTHON_COMPILE) - if (index < 0) - throw py::index_error(); -#else +#if !defined(ON_PYTHON_COMPILE) if (index < 0) return nullptr; #endif @@ -700,6 +697,12 @@ BND_FileObject* BND_ONXModel_ObjectTable::ModelObjectAt(int index) } } +#if defined(ON_PYTHON_COMPILE) + if (index < 0) + int new_index = m_compref_cache.Count() + index; + index = new_index < 0 ? std::abs(new_index) : new_index; +#endif + if (index < m_compref_cache.Count()) { return FileObjectFromCompRef(m_compref_cache[index]); From 8f266d1284ae2cd0a3b001dcb40322edd3983234 Mon Sep 17 00:00:00 2001 From: StudioWEngineers Date: Wed, 23 Oct 2024 14:15:38 +0100 Subject: [PATCH 3/4] add tests --- tests/python/test_File3dm_ObjectTable.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/tests/python/test_File3dm_ObjectTable.py b/tests/python/test_File3dm_ObjectTable.py index 0fca92b0..80c3a2f1 100644 --- a/tests/python/test_File3dm_ObjectTable.py +++ b/tests/python/test_File3dm_ObjectTable.py @@ -24,7 +24,28 @@ 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`. + """ + file_3dm = rhino3dm.File3dm() + for i in range(2): + file_3dm.Objects.AddPoint(rhino3dm.Point3d(i, i, i)) + + with self.assertRaises(IndexError, msg="Test negative IndexError"): + file_3dm.Objects[-3] + + with self.assertRaises(IndexError, msg="Test positive IndexError"): + file_3dm.Objects[3] + + with self.subTest(msg="Test positive indexing"): + self.assertEqual(file_3dm.Objects[1].Geometry.Location, rhino3dm.Point3d(1, 1, 1)) + + with self.subTest(msg="Test negative indexing"): + self.assertEqual(file_3dm.Objects[-2].Geometry.Location, rhino3dm.Point3d(0, 0, 0)) + + if __name__ == '__main__': print("running tests") unittest.main() - print("tests complete") \ No newline at end of file + print("tests complete") From 9db22e90c423a40dc2ad0c2ea10d31f2963d27a1 Mon Sep 17 00:00:00 2001 From: StudioWEngineers Date: Wed, 23 Oct 2024 19:50:01 +0100 Subject: [PATCH 4/4] fix compilation error --- src/bindings/bnd_extensions.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/bindings/bnd_extensions.cpp b/src/bindings/bnd_extensions.cpp index 4eabac4a..2c71cf0d 100644 --- a/src/bindings/bnd_extensions.cpp +++ b/src/bindings/bnd_extensions.cpp @@ -699,8 +699,7 @@ BND_FileObject* BND_ONXModel_ObjectTable::ModelObjectAt(int index) #if defined(ON_PYTHON_COMPILE) if (index < 0) - int new_index = m_compref_cache.Count() + index; - index = new_index < 0 ? std::abs(new_index) : new_index; + index = m_compref_cache.Count() + index < 0 ? std::abs(index) : m_compref_cache.Count() + index; #endif if (index < m_compref_cache.Count())