From f50a2d98ebada63da1a312e3262d7bff0a2275ca Mon Sep 17 00:00:00 2001 From: tpaviot Date: Thu, 8 Jan 2015 06:38:49 +0100 Subject: [PATCH] Added inherit topods example and unittest --- examples/core_inherit_topods_shape.py | 50 ++++++++++++++++++++++++++ test/core_wrapper_features_unittest.py | 32 +++++++++++++++-- 2 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 examples/core_inherit_topods_shape.py diff --git a/examples/core_inherit_topods_shape.py b/examples/core_inherit_topods_shape.py new file mode 100644 index 000000000..0de575644 --- /dev/null +++ b/examples/core_inherit_topods_shape.py @@ -0,0 +1,50 @@ +##This file is part of pythonOCC. +## +##pythonOCC is free software: you can redistribute it and/or modify +##it under the terms of the GNU Lesser General Public License as published by +##the Free Software Foundation, either version 3 of the License, or +##(at your option) any later version. +## +##pythonOCC is distributed in the hope that it will be useful, +##but WITHOUT ANY WARRANTY; without even the implied warranty of +##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +##GNU Lesser General Public License for more details. +## +##You should have received a copy of the GNU Lesser General Public License +##along with pythonOCC. If not, see . + +# This example demonstrates how to inherit from a TopoDS_Shape class + +from __future__ import print_function + +from OCC.BRepBuilderAPI import BRepBuilderAPI_MakeEdge +from OCC.TopoDS import TopoDS_Edge +from OCC.gp import gp_Pnt +from OCC.BRep import BRep_Tool + +from OCC.Display.SimpleGui import init_display + + +class InheritEdge(TopoDS_Edge): + def __init__(self, edge): + # following constructor creates an empy TopoDS_Edge + super(InheritEdge, self).__init__() + # we need to copy the base shape using the following three lines + print(self.IsNull()) + self.TShape(edge.TShape()) + self.Location(edge.Location()) + self.Orientation(edge.Orientation()) + print(self.IsNull()) + # then it becomes possible to extend the base class + + def get_curve(self): + return BRep_Tool.Curve(self) + +if __name__ == "__main__": + base_edge = BRepBuilderAPI_MakeEdge(gp_Pnt(), gp_Pnt(100., 0., 0.)).Edge() + inherited_edge = InheritEdge(base_edge) + print(inherited_edge.get_curve()) + display, start_display, add_menu, add_function_to_menu = init_display() + display.DisplayShape(inherited_edge, update=True) + start_display() + diff --git a/test/core_wrapper_features_unittest.py b/test/core_wrapper_features_unittest.py index 24ff6e85f..ca4c9f79e 100644 --- a/test/core_wrapper_features_unittest.py +++ b/test/core_wrapper_features_unittest.py @@ -22,17 +22,20 @@ from OCC.Standard import Standard_Transient, Handle_Standard_Transient from OCC.BRepPrimAPI import BRepPrimAPI_MakeBox -from OCC.BRepBuilderAPI import BRepBuilderAPI_MakeVertex +from OCC.BRepBuilderAPI import (BRepBuilderAPI_MakeVertex, + BRepBuilderAPI_MakeEdge) from OCC.gp import gp_Pnt, gp_Vec, gp_Pnt2d, gp_Lin, gp_Dir from OCC.STEPControl import STEPControl_Writer from OCC.Interface import Interface_Static_SetCVal, Interface_Static_CVal from OCC.GCE2d import GCE2d_MakeSegment from OCC.ShapeFix import ShapeFix_Solid, ShapeFix_Wire -from OCC.TopoDS import TopoDS_Compound, TopoDS_Builder +from OCC.TopoDS import TopoDS_Compound, TopoDS_Builder, TopoDS_Edge from OCC.BRepPrimAPI import BRepPrimAPI_MakeCylinder from OCC.TColStd import TColStd_Array1OfReal, TColStd_Array1OfInteger from OCC.TopExp import TopExp_Explorer from OCC.TopAbs import TopAbs_FACE +from OCC.GProp import GProp_GProps +from OCC.BRepGProp import brepgprop_LinearProperties class TestWrapperFeatures(unittest.TestCase): @@ -332,6 +335,31 @@ def test_neq_operator(self): self.assertFalse(shape_1 != shape_1) self.assertTrue(shape_1 != "some_string") + def test_inherit_topods_shape(self): + at = self.assertTrue + af = self.assertFalse + + class InheritEdge(TopoDS_Edge): + def __init__(self, edge): + # following constructor creates an empy TopoDS_Edge + super(InheritEdge, self).__init__() + # we need to copy the base shape using the following three + # lines + at(self.IsNull()) + self.TShape(edge.TShape()) + self.Location(edge.Location()) + self.Orientation(edge.Orientation()) + af(self.IsNull()) + # then it becomes possible to extend the base class + # create a line, compute its length + base_edge = BRepBuilderAPI_MakeEdge(gp_Pnt(100., 0., 0.), + gp_Pnt(150., 0., 0.)).Edge() + inherited_edge = InheritEdge(base_edge) + g1 = GProp_GProps() + brepgprop_LinearProperties(inherited_edge, g1) + length = g1.Mass() + self.assertEqual(length, 50.) + def suite(): test_suite = unittest.TestSuite()