Debugging error defining the normal vector to the face

Forums: 

When debugging from a previous Python code (2.7) to (3.7) error messages occured. (from pythonocc-core==0.18.1 to pythonocc-core=7.4.1)
First I applied 2to3.py, but code needed some further debugging.
Following code caused error at "plane = OCC.Core.Geom.Geom_Plane(Surface)"
How can I solve this problem?

CODE:

for idx, ElementPolygon in enumerate(Polygons):
# The space boundary must have a corresponding building element.
if self.IfcFile.by_guid(CurrentSpace).BoundedBy[idx].RelatedBuildingElement!=None:
# Generate the pythonOCC face corresponding to the geometric representation of the space boundary
ElementFace = self.__CreateFaceAndRemoveOpenings(ElementPolygon, Polygons, CurrentSpace, idx,RelatedBuildingElement)
# Identify the normal vector to the face
Surface = OCC.Core.BRep.BRep_Tool.Surface(ElementFace)
plane = OCC.Core.Geom.Geom_Plane(Surface) #TODO check OCC documentation for changes (downcast no longer needed, error handling: surface need not be a plane ?)
face_bbox = OCC.Core.Bnd.Bnd_Box()
OCC.Core.BRepBndLib.brepbndlib_Add(ElementFace, face_bbox)
face_center = get_bounding_box_center(face_bbox).XYZ()
vv = face_center - SpaceBoxCenter.XYZ()
face_normal = plane.Axis().Direction().XYZ()
if face_normal.Dot(vv)>=0:
face_normal = face_normal
else:
face_normal = plane.Axis().Direction().XYZ().Reversed()

ERROR MESSAGE:

File "C:\00_PhD\Dev\ifc2modelica\Envelop\CreateNetworkEnvelop.py", line 586, in CreateNetwork
plane = OCC.Core.Geom.Geom_Plane(Surface) #TODO check OCC documentation for changes (downcast no longer needed, error handling: surface need not be a plane ?)
File "C:\Workdir\Programs\Anaconda3\envs\py37_64\lib\site-packages\OCC\Core\Geom.py", line 7941, in __init__
_Geom.Geom_Plane_swiginit(self, _Geom.new_Geom_Plane(*args))
TypeError: Wrong number or type of arguments for overloaded function 'new_Geom_Plane'.
Possible C/C++ prototypes are:
Geom_Plane::Geom_Plane(gp_Ax3 const &)
Geom_Plane::Geom_Plane(gp_Pln const &)
Geom_Plane::Geom_Plane(gp_Pnt const &,gp_Dir const &)
Geom_Plane::Geom_Plane(Standard_Real const,Standard_Real const,Standard_Real const,Standard_Real const)

Kirill Gavrilov's picture

This line contains a misleading TODO comment:

Surface = OCC.Core.BRep.BRep_Tool.Surface(ElementFace)
plane = OCC.Core.Geom.Geom_Plane(Surface) #TODO check OCC documentation for changes (downcast no longer needed, error handling: surface need not be a plane ?)

As long as Python wrapper doesn't do any magic, BRep_Tool::Surface() returns an object of a basic type Geom_Surface, which does need to be DownCast'ed to it's subclass Geom_Plate to further code to work. The error you see later shows that you are trying to pass Geom_Surface to constructor of Geom_Plate, which doesn't know what to do with such argument (hence, suggests trying another constructors).

Tip: use "insert Code Snippet" on the Forum.

ManonBollez's picture

Thank you! How could I downcast the Surface to a Plane? Could you insert a code line in a reply? Did you mean Geom_Plate or Geom_Plane?

Kirill Gavrilov's picture

Sorry, I'm not familiar with a Python wrapper syntax. Maybe other users will help here.