Splitting edge on cylinder

Hi all,

I'm trying to split an edge on a cylinder. I've seen some posts concerning splitting edges of shapes, and I've been using those to construct my code.

I can successfully split an edge on a box, however, when I try to do the same thing on a cylinder, I get an invalid shape that is unorientable and is missing curves according to BRepCheck_Analyzer. I'm making the cylinder with TopoDS_Solid cyl = BRepPrimAPI_MakeCylinder(gp_Ax2(gp_Pnt(0.0, 0.0, 0.0), gp_Dir(0.0, 0.0, 0.1)), 2.0, 8.0).Solid().

I'm simply using the code posted here https://forum.freecadweb.org/viewtopic.php?t=3264, and instead of creating a box, I'm creating a cylinder.

If anyone has any insight into why I can split an edge on the box primitive, but not the cylinder primitive, that would be super helpful.

Thanks!

Adam

Adam Updegrove's picture

In case someone comes across this and has the same issue, this wasn't working because pcurves needed to be added for both of the new edges. It works fine for the box because the connected faces are planes, but the cylinder requires these to be fully defined. Here is the code in case anyone is interested.

      try
      {
          Standard_Real length = 8.0;
          Standard_Real radius = 2.0;
          TopoDS_Solid cyl = BRepPrimAPI_MakeCylinder(gp_Ax2(gp_Pnt(0.0, 0.0, 0.0), gp_Dir(0.0, 0.0, 0.1)), radius, length).Solid();

          TopoDS_Face face = TopoDS::Face(OCCTUtils_GetFirstType(cyl, TopAbs_FACE));
          TopoDS_Edge edge = TopoDS::Edge(OCCTUtils_GetFirstType(cyl, TopAbs_EDGE));

          BRep_Builder builder;

          Standard_Real pFirst, pLast;
          Handle(Geom_Curve) curve = BRep_Tool::Curve(edge, pFirst, pLast);
          gp_Pnt midPoint = curve->Value(pFirst + (pLast-pFirst)/2);

          TopoDS_Vertex vStart, vEnd, vMiddle;
          vStart = TopExp::FirstVertex(edge);
          vEnd = TopExp::LastVertex(edge);
          builder.MakeVertex(vMiddle, midPoint, Precision::Confusion());

          TopoDS_Edge newEdge1 = BRepBuilderAPI_MakeEdge(curve, vStart, TopoDS::Vertex(vMiddle.Reversed()));
          TopoDS_Edge newEdge2 = BRepBuilderAPI_MakeEdge(curve, vMiddle, TopoDS::Vertex(vEnd.Reversed()));

          TopoDS_Wire wire;
          builder.MakeWire(wire);
          newEdge1.Orientation(TopAbs_REVERSED);
          builder.Add(wire, newEdge1);
          newEdge2.Orientation(TopAbs_REVERSED);
          builder.Add(wire, newEdge2);

          builder.UpdateEdge(newEdge1, new Geom2d_Line(gp_Pnt2d(0.0, length), gp_Dir2d(1,0)), face, Precision::Confusion());
          builder.Range(newEdge1, face, 0, pFirst + (pLast-pFirst)/2);
          builder.UpdateEdge(newEdge2, new Geom2d_Line(gp_Pnt2d(0.0, length), gp_Dir2d(1,0)), face, Precision::Confusion());
          builder.Range(newEdge2, face, pFirst + (pLast-pFirst)/2, pLast);

          Handle(ShapeBuild_ReShape) context = new ShapeBuild_ReShape();
          context->Replace(edge, wire);
          TopoDS_Shape output = context->Apply(cyl);

          std::cout << std::endl << "Program finished normally" << std::endl;
      }
      catch (Standard_Failure)
      {
          Handle_Standard_Failure e = Standard_Failure::Caught();
          std::cout << "OCC Error: " << e->GetMessageString() << std::endl;
      }
      catch (const std::exception &error)
      {
          std::cout << "My Error: " << error.what() << std::endl;
      }