Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions examples/core_inherit_topods_shape.py
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

# 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()

32 changes: 30 additions & 2 deletions test/core_wrapper_features_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()
Expand Down