How to create TopoDS_Edge from to gp_Pnt in OpenCASCADE

OCCUtils provides a conveniece function for creating a TopoDS_Edge between two points (i.e. a straight line):

edge_from_points_occ.cpp
#include <occutils/Edge.hxx>

using namespace OCCUtils;

gp_Pnt p1 = /* ... */;
gp_Pnt p2 = /* ... */;

TopoDS_Edge edge = Edge::FromPoints(p1, p2);
```cpp {filename="edge_from_points_occ.cpp"}

In case you want to do it manually (i.e. without *OCCUtils*), use this snippet

```cpp {filename="makeedge_manual.cpp"}
TopoDS_Edge edge = BRepBuilderAPI_MakeEdge(p1, p2).Edge();
```cpp {filename="makeedge_manual.cpp"}

but be aware that you also have to handle the case of `p1 == p2` i.e.

```cpp {filename="check_points_equal.cpp"}
if (p1.Distance(p2) <= Precision::Confusion()) {
    // Don't try to create an Edge in this case
}
```cpp {filename="check_points_equal.cpp"}

OCCUtil's `Edge::FromPoints` handles this case by returning `TopoDS_Edge()` i.e. a `TopoDS_Edge` where `edge.IsNull() == true`.

Check out similar posts by category: C/C++, OpenCASCADE