Give a TopoDS_Face,find connected face with the given one

Give a TopoDS_Face,How to find connected face with the given one?
How to find all possible edges connected with the given one?

jelle's picture

Yes, use The Top_Explorer class

jelle's picture

Yes, use The Top_Explorer class

jelle's picture

Yes, use The Top_Explorer class

Vincent's picture

Thanks!
Do you mean TopExp_Explorer?
can you give me more hints?

Mark Blome's picture

Hi Vincent,

I'm not sure if I understand your question correctly, but for "walking along" faces
connected by edges or edges connected by vertices I use the following class that I have
implemented based on PythonOCC. Using this class "propagation" algorithms can be
implemented.

class ShapeNeighbors(object):
def __init__(self, aShape, forShapeType, max_nb_ascendants = None):
self._neigborST = forShapeType
self._max_nb_ascendants = max_nb_ascendants
# e.g. for face neighbors it might make sense to specify only to take edges into
# account (while searching for neighbors), that are connected to not more than 2 (or 3)
# faces (to not propagate along non-manifold edges)
if not (forShapeType in [TopAbs_FACE, TopAbs_EDGE]):
raise RuntimeError, "ShapeNeighbors: Only supported for subshapes of type: Face or Edge."
DesShapeType = {TopAbs_FACE: TopAbs_EDGE, TopAbs_EDGE: TopAbs_VERTEX}[forShapeType]
self._SDesMap = TT.MapShapesAndDescendants(aShape, forShapeType, DesShapeType)
self._SAsMap = TT.MapShapesAndAncestors(aShape, DesShapeType, forShapeType)

def GetAllNeighbors(self, subshape):
allneighbors = TT.IndexedListOfShape()
neighbors_treated = TT.ShapeSet()
self.GetAllNeighbors_(subshape, allneighbors, neighbors_treated)

def GetAllNeighbors_(self, subshape, allneighbors, neighbors_treated):
theseneighbors = self.GetNeighbors(subshape)
for neighbor in theseneighbors:
if not (neighbor in neighbors_treated):
neighbors_treated.add(neighbor)
newneighbors = self.GetAllNeighbors(neighbor)
for newneighbor in newneighbors:
if not (newneighbor in neighbors_treated):
neighbors_treated.add(newneighbor)
allneighbors.append(newneighbor)

def GetConnectingShape(self, subshape, neighbor):
# returns - the edge connecting subshape and neighbor faces or
# - the vertex connecting subshape and neighbor edges
# additionally returns the (unique) list of faces/edges connected to the connecting shape
if (subshape is None) or (subshape.IsNull()==1):
raise RuntimeError, "GetConnectingShape: Shape is None or Null."
if (neighbor is None) or (neighbor.IsNull()==1):
raise RuntimeError, "GetConnectingShape: Neighbor shape is None or Null."
if subshape.ShapeType()!=self._neigborST:
raise RuntimeError, "GetConnectingShape: Subshape is of wrong type."
if neighbor.ShapeType()!=self._neigborST:
raise RuntimeError, "GetConnectingShape: Neighbor shape is of wrong type."
# all edges or vertices connected to subshape
if not (subshape in self._SDesMap):
raise RuntimeError, "GetConnectingShape: This shape seems not to be a subshape of the main shape."
if not (neighbor in self._SDesMap):
raise RuntimeError, "GetConnectingShape: Neighbor shape seems not to be a subshape of the main shape."
# one of these shapes must be the connecting shape
desc_shapes = self._SDesMap[subshape] # TT.ListOfShape
for desc_shape in desc_shapes:
# all faces or edges connected to this edge or vertex
if not (desc_shape in self._SAsMap):
raise RuntimeError, "GetConnectingShape: Error finding neighbors (internal error)."
asc_shapes = self._SAsMap[desc_shape]
for asc_shape in asc_shapes:
if asc_shape.IsSame(neighbor):
return desc_shape, TT.IndexedListOfShape(TT.ShapeSet(asc_shapes)) # found !
raise RuntimeError, "GetConnectingShape: Shapes and are not neighbor shapes."

def GetNeighbors(self, subshape):
neighbors = TT.IndexedListOfShape()
neighbors_Set = TT.ShapeSet()
if (subshape is None) or (subshape.IsNull()==1):
raise RuntimeError, "GetNeighbors: Shape is None or Null."
if subshape.ShapeType()!=self._neigborST:
raise RuntimeError, "GetNeighbors: Shape is of wrong type."
# all edges or vertices connected to subshape
if not (subshape in self._SDesMap):
raise RuntimeError, "GetNeighbors: This shape seems not to be a subshape of the main shape."
desc_shapes = self._SDesMap[subshape] # TT.ListOfShape
for desc_shape in desc_shapes:
# all faces or edges connected to this edge or vertex
if not (desc_shape in self._SAsMap):
raise RuntimeError, "GetNeighbors: Error finding neighbors (internal error)."
asc_shapes = self._SAsMap[desc_shape]
if self._max_nb_ascendants is not None:
# e.g. for face neighbors it might make sense to specify only to take edges into
# account (while searching for neighbors), that are connected to not more than 2 or 3
# faces (to not propagate along non-manifold edges)
if len(asc_shapes) > self._max_nb_ascendants:
continue
for asc_shape in asc_shapes:
if (not asc_shape.IsSame(subshape)) and (not (asc_shape in neighbors_Set)):
if not (asc_shape.ShapeType()==subshape.ShapeType()):
raise RuntimeError, "GetNeighbors: Error finding neighbors (internal error)."
neighbors.append(asc_shape); neighbors_Set.add(asc_shape)
return neighbors

All classes TT.* are my convenient wrapper classes/functions around Opencascade data classes, e.g.
MapShapesAndAncestors is defined as follows:

def MapShapesAndAncestors(ShapeorShapelist, ShapeTypeShape, ShapeTypeAncestors):
from ShapeLists import ListOfShape, IndexedListOfShape
# Maps shapes and their ancestors,
SAMap = TopTools_IndexedDataMapOfShapeListOfShape()
TopExp().MapShapesAndAncestors(ShapeorShapelist, ShapeTypeShape, ShapeTypeAncestors, SAMap)
return IndexedMapOfShapeListOfShape(SAMap)

Hope this helps,
Mark

Vincent's picture

Thanks very much! It is very helpful.