Edges to wire


i have a couple of edges (see attachment red color) and now i connect them to a wire with:

BRepBuilderAPI_MakeWire makeWire;
for (TopExp_Explorer ex(intersectionShape, TopAbs_EDGE); ex.More(); ex.Next())

but i only get the green wire.

Looks like the the makeWire.Add(..) adds a not connected edge, but they are all connected (like the picture shows).

Thx for help

Cauchy Ding's picture

Hi Matthias,

Before calling BRepBuilderAPI_MakeWire, you have to sort edges' order.


shinpei's picture

Thx Ding,

how could I do that?


Cauchy Ding's picture

Hi Matthias,

If input edges don't contain any degenerated edge, you can use similar codes listed in attachment.
Hope it works for you.


shinpei's picture

Hi Ding,

thx for your time. i will take a look at your nice code. ;)

Thx Matthias

Sharad Verma's picture

Thanks Ding.. Thanks a lot.. :)

Sharad Verma's picture

Hi Ding,

This code doesn't work for the wire, which contains an edge twice. Same as a pipe contains twice the connecting edge, the above code doesn't return 4 edges, it returns only 3 edges.

Can you please give me corrected algo for the same?

Mark Blome's picture

Dear Shard Verma,

you can use ShapeAnalysis_WireOrder to sort edges, this is how I do it in Python using PythonOCC
wrappers for Opencascade. The funtion below will return a list of wires, in case you have provided edges
that are not connected:

def _buildWiresFromEdgeset(self, edgelist):
from OCC.TopExp import TopExp
from OCC.BRep import BRep_Tool
from OCC.ShapeAnalysis import ShapeAnalysis_WireOrder
from OCC.Precision import Precision
from OCC.BRepBuilderAPI import BRepBuilderAPI_MakeWire
from OCC.BRepBuilderAPI import BRepBuilderAPI_WireDone, BRepBuilderAPI_EmptyWire, BRepBuilderAPI_DisconnectedWire, BRepBuilderAPI_NonManifoldWire
wb_errdict={BRepBuilderAPI_WireDone:"No error", BRepBuilderAPI_EmptyWire:"Empty wire", BRepBuilderAPI_DisconnectedWire:"disconnected wire",
BRepBuilderAPI_NonManifoldWire:"non-manifold wire"}
sawo_statusdict={0:"all edges are direct and in sequence",
1:"all edges are direct but some are not in sequence",
2:"unresolved gaps remain",
-1:"some edges are reversed, but no gaps remain",
-2:"some edges are reversed and some gaps remain",
-10:"failure on reorder"}
TE = TopExp(); DS=TopoDS()
isclosed = False # in general, wires will not be closed
mode3d = True
SAWO = ShapeAnalysis_WireOrder(mode3d, Precision().PConfusion())
for edge in edgelist:
V1 = TE.FirstVertex(DS.Edge(edge))
V2 = TE.LastVertex(DS.Edge(edge))
pnt1 = BRep_Tool().Pnt(V1)
pnt2 = BRep_Tool().Pnt(V2)
SAWO.Add(pnt1.XYZ(), pnt2.XYZ())
#print "SAWO.Status()", SAWO.Status()
if not SAWO.IsDone():
raise RuntimeError, "build wire: Unable to reorder edges: \n" + sawo_statusdict[SAWO.Status()]
if SAWO.Status() not in [0, -1]:
pass # not critical, wirebuilder will handle this
Wirelist = TT.IndexedListOfShape()
#print "Number of chains: ", SAWO.NbChains()
for i in range(SAWO.NbChains()):
wirebuilder = BRepBuilderAPI_MakeWire()
estart, eend = SAWO.Chain(i+1)
#print "Number of edges in chain", i, ": ", eend - estart + 1
if (eend - estart + 1)==0:
for j in range(estart, eend+1):
idx = abs(SAWO.Ordered(j)) #<0 if edge should be reversed, which wirebuilder will handle for us
wirebuilder = s_addToWireBuilder(wirebuilder, edgelist[idx-1])
if wirebuilder is None:
raise RuntimeError, " build wire: Error adding edge number " + str(j+1) + " to Wire number " + str(i)
err = wirebuilder.Error()
if err != BRepBuilderAPI_WireDone:
raise RuntimeError, "Overlay2D: build wire: Error adding edge number " + str(j+1) + " to Wire number " + str(i) +": \n" + wb_errdict[err]
aWire = wirebuilder.Wire()
except Exception, err:
raise RuntimeError, "Overlay2D: build wire: Creation of Wire number " + str(i) + " from edge(s) failed. \n" + str(err)
return Wirelist

def _addToWireBuilder(self, wirebuilder, aShape):
st = aShape.ShapeType()
if (not st in [TopAbs_WIRE, TopAbs_EDGE]):
raise RuntimeError, "_addToWireBuilder: Shape must be an edge or a wire."
edgelist = allEdges(aShape)
for i, edge in enumerate(edgelist):
except Exception, err:
raise RuntimeError, "build wire:Error adding edge number " + str(i) + "to Wirebuilder: \n" + str(err)
return wirebuilder

Timo Roth's picture

Also ShapeAnalysis_FreeBounds::ConnectEdgesToWires can be used to build wires from unsorted edges. I guess Mark's solution has better performance but I didn't check it. After the call to ConnectEdgesToWires it might be necessary to fix the resulting wires.


Timo Roth's picture