A strange error when use TopoDS::Face()

jianbaoxia

CAD master
Hi I'm JianBaoXia.
TopoDS::Face() is a function I usually used.
Today, I used it as before, however, it raise error.
I have no idea why it occurs. o_O o_O
There is my simply demo:
Code:
//
#include <BRepPrimAPI_MakeCylinder.hxx>
#include <BRepBuilderAPI_MakeFace.hxx>
//
#include <BRepAlgoAPI_Common.hxx>
//
#include <gp_Pln.hxx>
//
#include <TopoDS.hxx>
#include <TopExp_Explorer.hxx>
//
#include <ShapeAnalysis.hxx>
//
#include <STEPControl_Writer.hxx>
#include <Interface_Static.hxx>

int main()
{
    TopoDS_Shape cylinder = BRepPrimAPI_MakeCylinder(gp_Ax2(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1)), 5, 10);
    gp_Pln pln(gp_Pnt(0, 0, 5), gp_Dir(0, 0, 1));
    TopoDS_Face aPlane = BRepBuilderAPI_MakeFace(pln);
    TopoDS_Shape com = BRepAlgoAPI_Common(cylinder, aPlane);
    TopoDS_Face comFace = TopoDS::Face(com); // error occurs cause this code

    return 0;
}
 

Andrey

Active CAD practitioner
Staff member
Dear jianbaoxia,

After Common, com is COMPOUND. You make FACE using COMPOUND, so you have error. Try something like this:

Code:
  TopoDS_Shape com = BRepAlgoAPI_Common(cylinder, aPlane);
  if (com.ShapeType() == TopAbs_COMPOUND)
  {
    if (com.NbChildren() == 1)
    {
      TopoDS_Iterator it(com);
      com = it.Value();
    }
  }
  TopoDS_Face comFace;
  if (com.ShapeType() == TopAbs_FACE)
  {
    comFace = TopoDS::Face(com);
  }


1656926969621.png


The question remains why COMPOUND is returned? Probably for several reasons:

1) The result of the operation can be a real COMPOUND, consisting of "unrelated" elements.

2) If I'm not mistaken, if the operation was not successful, then the algorithm should return something (even "garbage").

3) And so on.

If interested, you can look at the documentation or look at the code of BRepAlgoAPI_Common.
 

Andrey

Active CAD practitioner
Staff member
Dear jianbaoxia,

After Common, com is COMPOUND. You make FACE using COMPOUND, so you have error. Try something like this:

Code:
  TopoDS_Shape com = BRepAlgoAPI_Common(cylinder, aPlane);
  if (com.ShapeType() == TopAbs_COMPOUND)
  {
    if (com.NbChildren() == 1)
    {
      TopoDS_Iterator it(com);
      com = it.Value();
    }
  }
  TopoDS_Face comFace;
  if (com.ShapeType() == TopAbs_FACE)
  {
    comFace = TopoDS::Face(com);
  }


View attachment 317


The question remains why COMPOUND is returned? Probably for several reasons:

1) The result of the operation can be a real COMPOUND, consisting of "unrelated" elements.

2) If I'm not mistaken, if the operation was not successful, then the algorithm should return something (even "garbage").

3) And so on.

If interested, you can look at the documentation or look at the code of BRepAlgoAPI_Common.
Dear @jianbaoxia,

In other words, we have extracted FACE from COMPOUND. This is one of the options. It would be possible to use
Code:
TopExp_Explorer exp(com, TopAbs_FACE)
.
 

jianbaoxia

CAD master
@Andrey It's so happy to have your advise.
Yes, you're right.
I was use TopoDS::Face() in the for loop of TopExp_Explorer lile:
Code:
for(TopExp_Explorer exp(com, TopAbs_FACE);exp.More();exp.Next())
I learn a more useful kill.
Thank you. 😁 😁
 
Top