1 # Copyright (C) 2014-2024 CEA, EDF
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Lesser General Public License for more details.
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 from ModelAPI import *
23 from GeomAlgoAPI import GeomAlgoAPI_MapShapesAndAncestors as mapShapesAndAncestors
25 FILTER_ID = "TopoConnectedFaces"
29 instance.__call__ = lambda: instance
33 class FiltersPlugin_TopoConnectedFaces(ModelAPI_Filter):
35 Filter for faces topologically connected to the selected object.
39 """x.__init__(...) initializes x; see x.__class__.__doc__ for signature"""
40 ModelAPI_Filter.__init__(self)
44 """ Description of the filter """
45 return "Topologically connected faces"
47 def isSupported(self, theType):
48 """ Supported types of filtered shapes """
49 return theType == GeomAPI_Shape.FACE
52 """ Returns True if the filter can be used several times within one selection """
55 def isOk(self, theShape, theResult, theArgs):
56 """ True if theShape is applicable for the filter """
57 selectedShapeAttr = modelAPI_AttributeSelection(theArgs.argument("Shape"))
58 if selectedShapeAttr is None:
60 selectedShape = selectedShapeAttr.value()
61 isPropagated = modelAPI_AttributeBoolean(theArgs.argument("Propagation")).value()
63 # cache selected shape and applicable faces
64 if selectedShape not in self.myCached:
65 anOwner = bodyOwner(theResult, True)
67 anOwner = modelAPI_ResultBody(theResult)
70 topLevelShape = anOwner.shape()
71 if not topLevelShape.isSubShape(selectedShape):
74 mapVFAlgo = mapShapesAndAncestors(topLevelShape, GeomAPI_Shape.VERTEX, GeomAPI_Shape.FACE)
75 mapVF = mapVFAlgo.map()
76 mapEFAlgo = mapShapesAndAncestors(topLevelShape, GeomAPI_Shape.EDGE, GeomAPI_Shape.FACE)
77 mapEF = mapEFAlgo.map()
79 # faces adjacent to the selected shape
80 applicableFaces = OriShapeSet()
81 if selectedShape.shapeType() == GeomAPI_Shape.VERTEX:
82 if selectedShape in mapVF: applicableFaces = mapVF[selectedShape]
83 elif selectedShape.shapeType() == GeomAPI_Shape.EDGE:
84 if selectedShape in mapEF: applicableFaces = mapEF[selectedShape]
85 elif selectedShape.shapeType() == GeomAPI_Shape.FACE:
86 applicableFaces.insert(selectedShape)
87 self.adjacentFaces(selectedShape, mapVF, GeomAPI_Shape.VERTEX, applicableFaces, False)
90 # propagate the connection
92 appFacesCopy = applicableFaces
93 for ind in range(appFacesCopy.size()):
94 self.adjacentFaces(appFacesCopy[ind], mapEF, GeomAPI_Shape.EDGE, applicableFaces)
95 self.myCached[selectedShape] = applicableFaces
97 return theShape in self.myCached[selectedShape]
99 def xmlRepresentation(self):
100 """ Returns XML string which represents GUI of the filter """
101 return self.xmlFromFile("filter-TopoConnectedFaces.xml")
103 def initAttributes(self, theArgs):
104 """ Initializes arguments of a filter """
105 theArgs.initAttribute("Shape", ModelAPI_AttributeSelection.typeId())
106 theArgs.initAttribute("Propagation", ModelAPI_AttributeBoolean.typeId())
108 def adjacentFaces(self, theFace, theMapSA, theShapeType, theApplicableFaces, theRecursive = True):
109 """ Find all faces neighbour to theFace """
110 exp = GeomAPI_ShapeExplorer(theFace, theShapeType)
112 if exp.current() in theMapSA:
113 faces = theMapSA[exp.current()]
114 for ind in range(faces.size()):
116 if f not in theApplicableFaces:
117 theApplicableFaces.insert(f)
119 self.adjacentFaces(f, theMapSA, theShapeType, theApplicableFaces)
123 # Register the filter object
124 filter = FiltersPlugin_TopoConnectedFaces
125 aSession = ModelAPI_Session.get()
126 aSession.filters().registerFilter(FILTER_ID, filter)