retreiving underlying circle object form an edge

As you see I am trying to retrieve the underlying curves of the fused face object between two similar circles.
The trouble occurs when I am trying to get the gp*_circ object of the curve witgh the line of code "gp_Circ circ = circle->Circ();"
so it seems there is something wrong with the casting, but th eexcact same code works perfectly fine for other circles.
anyone got any clue?

TopoDS_Shape Circle() {
gp_Pnt centerPt(0, 0, 0);
gp_Dir dir(0, 0, -1);
gp_Ax2 ax2(centerPt, dir);
gp_Circ circ(ax2, 5);

Geom_Curve* curve = new Geom_Circle(circ);
BRepBuilderAPI_MakeEdge maker(curve);
BRepBuilderAPI_MakeWire wire(maker.Edge());
BRepBuilderAPI_MakeFace face(wire.Wire());
return face.Face();
}

TopoDS_Shape Circle2() {
gp_Pnt centerPt(5, 0, 0);
gp_Dir dir(0, 0, -1);
gp_Ax2 ax2(centerPt, dir);
gp_Circ circ(ax2, 5);

Geom_Curve* curve = new Geom_Circle(circ);
BRepBuilderAPI_MakeEdge maker(curve);
BRepBuilderAPI_MakeWire wire(maker.Edge());
BRepBuilderAPI_MakeFace face(wire.Wire());
return face.Face();
}

TopoDS_Shape Fuse(TopoDS_Shape face1, TopoDS_Shape face2) {

BRepAlgoAPI_Fuse fuse(face1, face2);
fuse.SimplifyResult();
TopExp_Explorer faceExplorer(fuse.Shape(), TopAbs_FACE);

return fuse.Shape();
}

TopoDS_Shape Explorer(TopoDS_Shape shape) {
TopExp_Explorer faceExplorer = TopExp_Explorer(shape, TopAbs_FACE);
TopoDS_Face face = TopoDS::Face(faceExplorer.Current());
TopExp_Explorer edgeExplorer(face, TopAbs_EDGE);
TopoDS_Edge edge = TopoDS::Edge(edgeExplorer.Current());
edgeExplorer.Next();
Standard_Real first, last;
Handle(Geom_Curve) curve = BRep_Tool::Curve(edge, first, last);
Handle(Geom_TrimmedCurve) trimmedCurve = new Geom_TrimmedCurve(curve, first, last);
Handle(Geom_Circle) circle = Handle(Geom_Circle)::DownCast(curve);
gp_Circ circ = circle->Circ();
gp_Ax1 axis = circ.Axis();
gp_Dir dir = axis.Direction();
gp_Pnt center = circle->Location();
double radius = circle->Circ().Radius();
return edgeExplorer.Current();
}

TopoDS_Shape fuse = Fuse(circle, circle2);
TopoDS_Shape explorer = Explorer(fuse);

Kristofer Sejersted's picture

Comment:
The code works when I do not call fuse.simplifyResult() on the fuse result shape. Howver when I do no use this I get wrong shape, see attahced file.
I do not completety understand this simplifyresult function, if anyone could explain this as well, I would be very greatful!

Attachments: 
Mikhail Sazonov's picture

SimplifyResult removes unnecessary inner edges by merging same-surface faces together. And it can replace curves in some edges with TrimmedCurve. If it is so you need to check the kind of curve, and get the Basis curve from the trimmed one, and then it will be circle.

Kristofer Sejersted's picture

Thank you!