Salome HOME
Merge branch 'mbs/32757' of https://codev-tuleap.cea.fr/plugins/git/salome/shaper...
[modules/shaper.git] / src / FiltersPlugin / FiltersPlugin_TopoConnectedFaces.py
1 # Copyright (C) 2014-2022  CEA/DEN, EDF R&D
2 #
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.
7 #
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.
12 #
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
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20
21 from ModelAPI import *
22 from GeomAPI import *
23 from GeomAlgoAPI import GeomAlgoAPI_MapShapesAndAncestors as mapShapesAndAncestors
24
25 FILTER_ID = "TopoConnectedFaces"
26
27 def singleton(cls):
28   instance = cls()
29   instance.__call__ = lambda: instance
30   return instance
31
32 @singleton
33 class FiltersPlugin_TopoConnectedFaces(ModelAPI_Filter):
34   """
35   Filter for faces topologically connected to the selected object.
36   """
37
38   def __init__(self):
39     """x.__init__(...) initializes x; see x.__class__.__doc__ for signature"""
40     ModelAPI_Filter.__init__(self)
41     self.myCached = {}
42
43   def name(self):
44     """ Description of the filter """
45     return "Topologically connected faces"
46
47   def isSupported(self, theType):
48     """ Supported types of filtered shapes """
49     return theType == GeomAPI_Shape.FACE
50
51   def isMultiple(self):
52     """ Returns True if the filter can be used several times within one selection """
53     return True
54
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:
59       return False
60     selectedShape = selectedShapeAttr.value()
61     isPropagated = modelAPI_AttributeBoolean(theArgs.argument("Propagation")).value()
62
63     # cache selected shape and applicable faces
64     if selectedShape not in self.myCached:
65       anOwner = bodyOwner(theResult, True)
66       if anOwner is None:
67         anOwner = modelAPI_ResultBody(theResult)
68         if anOwner is None:
69           return False
70       topLevelShape = anOwner.shape()
71       if not topLevelShape.isSubShape(selectedShape):
72         return False;
73
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()
78
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)
88       else:
89         return False
90       # propagate the connection
91       if isPropagated:
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
96
97     return theShape in self.myCached[selectedShape]
98
99   def xmlRepresentation(self):
100     """ Returns XML string which represents GUI of the filter """
101     return self.xmlFromFile("filter-TopoConnectedFaces.xml")
102
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())
107
108   def adjacentFaces(self, theFace, theMapSA, theShapeType, theApplicableFaces, theRecursive = True):
109     """ Find all faces neighbour to theFace """
110     exp = GeomAPI_ShapeExplorer(theFace, theShapeType)
111     while exp.more():
112       if  exp.current() in theMapSA:
113         faces = theMapSA[exp.current()]
114         for ind in range(faces.size()):
115           f = faces[ind]
116           if f not in theApplicableFaces:
117             theApplicableFaces.insert(f)
118             if theRecursive:
119               self.adjacentFaces(f, theMapSA, theShapeType, theApplicableFaces)
120       exp.next()
121
122
123 # Register the filter object
124 filter = FiltersPlugin_TopoConnectedFaces
125 aSession = ModelAPI_Session.get()
126 aSession.filters().registerFilter(FILTER_ID, filter)