From b220ed2ba4d5bebba98361a03bb4750dc8b9c7e6 Mon Sep 17 00:00:00 2001 From: Johannes Verherstraeten Date: Mon, 21 Oct 2024 18:14:42 +0200 Subject: [PATCH] Add hash eq and neq operations to TShape --- src/SWIG_files/wrapper/TopoDS.i | 33 ++++++++++++++++++++++++++++++ test/test_core_wrapper_features.py | 32 +++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/src/SWIG_files/wrapper/TopoDS.i b/src/SWIG_files/wrapper/TopoDS.i index 5c4f57046..e264c4a05 100644 --- a/src/SWIG_files/wrapper/TopoDS.i +++ b/src/SWIG_files/wrapper/TopoDS.i @@ -1980,11 +1980,44 @@ Returns the type as a term of the shapeenum enum: vertex, edge, wire, face, .... ") ShapeType; virtual TopAbs_ShapeEnum ShapeType(); + +%extend{ + bool __ne_wrapper__(const opencascade::handle & other) { + if (self!=other) return true; + else return false; + } +} +%pythoncode { +def __ne__(self, right): + try: + return self.__ne_wrapper__(right) + except: + return True +} + +%extend{ + bool __eq_wrapper__(const opencascade::handle & other) { + if (self==other) return true; + else return false; + } +} +%pythoncode { +def __eq__(self, right): + try: + return self.__eq_wrapper__(right) + except: + return False +} }; %make_alias(TopoDS_TShape) +%extend TopoDS_TShape { + size_t __hash__() { + return opencascade::hash(self);} +}; + %extend TopoDS_TShape { %pythoncode { __repr__ = _dumps_object diff --git a/test/test_core_wrapper_features.py b/test/test_core_wrapper_features.py index 4b47f9283..741f6c95e 100644 --- a/test/test_core_wrapper_features.py +++ b/test/test_core_wrapper_features.py @@ -539,6 +539,38 @@ def test_neq_operator() -> None: assert shape_1 != "some_string" +def test_tshape_hash_operator() -> None: + shape_1 = BRepPrimAPI_MakeBox(10, 20, 30).Shape() + shape_2 = BRepPrimAPI_MakeBox(10, 20, 30).Shape() + # Create a shape that differs from shape_2, but has the same TShape + shape_3 = shape_2.Reversed() + assert hash(shape_1.TShape()) == hash(shape_1.TShape()) + assert not hash(shape_1.TShape()) == hash(shape_2.TShape()) + assert hash(shape_2.TShape()) == hash(shape_3.TShape()) + + +def test_tshape_eq_operator() -> None: + shape_1 = BRepPrimAPI_MakeBox(10, 20, 30).Shape() + shape_2 = BRepPrimAPI_MakeBox(10, 20, 30).Shape() + # Create a shape that differs from shape_2, but has the same TShape + shape_3 = shape_2.Reversed() + assert shape_1.TShape() == shape_1.TShape() + assert not shape_1.TShape() == shape_2.TShape() + assert shape_2.TShape() == shape_3.TShape() + assert not shape_1.TShape() == "some_string" + + +def test_tshape_neq_operator() -> None: + shape_1 = BRepPrimAPI_MakeBox(10, 20, 30).Shape() + shape_2 = BRepPrimAPI_MakeBox(10, 20, 30).Shape() + # Create a shape that differs from shape_2, but has the same TShape + shape_3 = shape_2.Reversed() + assert not shape_1.TShape() != shape_1.TShape() + assert shape_1.TShape() != shape_2.TShape() + assert not shape_2.TShape() != shape_3.TShape() + assert shape_1.TShape() != "some_string" + + def test_inherit_topods_shape() -> None: class InheritEdge(TopoDS_Edge): def __init__(self, edge: TopoDS_Edge) -> None: