1 # Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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
51 def isOk(self, theShape, theResult, theArgs):
52 """ True if theShape is applicable for the filter """
53 selectedShapeAttr = modelAPI_AttributeSelection(theArgs.argument("Shape"))
54 if selectedShapeAttr is None:
56 selectedShape = selectedShapeAttr.value()
57 isPropagated = modelAPI_AttributeBoolean(theArgs.argument("Propagation")).value()
59 # cache selected shape and applicable faces
60 if selectedShape not in self.myCached:
61 anOwner = bodyOwner(theResult, True)
63 anOwner = modelAPI_ResultBody(theResult)
66 topLevelShape = anOwner.shape()
67 if not topLevelShape.isSubShape(selectedShape):
70 mapVFAlgo = mapShapesAndAncestors(topLevelShape, GeomAPI_Shape.VERTEX, GeomAPI_Shape.FACE)
71 mapVF = mapVFAlgo.map()
72 mapEFAlgo = mapShapesAndAncestors(topLevelShape, GeomAPI_Shape.EDGE, GeomAPI_Shape.FACE)
73 mapEF = mapEFAlgo.map()
75 # faces adjacent to the selected shape
76 applicableFaces = OriShapeSet()
77 if selectedShape.shapeType() == GeomAPI_Shape.VERTEX:
78 if selectedShape in mapVF: applicableFaces = mapVF[selectedShape]
79 elif selectedShape.shapeType() == GeomAPI_Shape.EDGE:
80 if selectedShape in mapEF: applicableFaces = mapEF[selectedShape]
81 elif selectedShape.shapeType() == GeomAPI_Shape.FACE:
82 applicableFaces.insert(selectedShape)
83 self.adjacentFaces(selectedShape, mapVF, GeomAPI_Shape.VERTEX, applicableFaces, False)
86 # propagate the connection
88 appFacesCopy = applicableFaces
89 for ind in range(appFacesCopy.size()):
90 self.adjacentFaces(appFacesCopy[ind], mapEF, GeomAPI_Shape.EDGE, applicableFaces)
91 self.myCached[selectedShape] = applicableFaces
93 return theShape in self.myCached[selectedShape]
95 def xmlRepresentation(self):
96 """ Returns XML string which represents GUI of the filter """
97 return self.xmlFromFile("filter-TopoConnectedFaces.xml")
99 def initAttributes(self, theArgs):
100 """ Initializes arguments of a filter """
101 theArgs.initAttribute("Shape", ModelAPI_AttributeSelection_typeId())
102 theArgs.initAttribute("Propagation", ModelAPI_AttributeBoolean_typeId())
104 def adjacentFaces(self, theFace, theMapSA, theShapeType, theApplicableFaces, theRecursive = True):
105 """ Find all faces neighbour to theFace """
106 exp = GeomAPI_ShapeExplorer(theFace, theShapeType)
108 if exp.current() in theMapSA:
109 faces = theMapSA[exp.current()]
110 for ind in range(faces.size()):
112 if f not in theApplicableFaces:
113 theApplicableFaces.insert(f)
115 self.adjacentFaces(f, theMapSA, theShapeType, theApplicableFaces)
119 # Register the filter object
120 filter = FiltersPlugin_TopoConnectedFaces
121 aSession = ModelAPI_Session.get()
122 aSession.filters().registerFilter(FILTER_ID, filter)