Salome HOME
Merge remote-tracking branch 'remotes/origin/Filters_Development_2'
[modules/shaper.git] / src / FiltersPlugin / FiltersPlugin_TopoConnectedFaces.py
1 # Copyright (C) 2014-2019  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 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:
55       return False
56     selectedShape = selectedShapeAttr.value()
57     isPropagated = modelAPI_AttributeBoolean(theArgs.argument("Propagation")).value()
58
59     # cache selected shape and applicable faces
60     if selectedShape not in self.myCached:
61       anOwner = bodyOwner(theResult, True)
62       if anOwner is None:
63         anOwner = modelAPI_ResultBody(theResult)
64         if anOwner is None:
65           return False
66       topLevelShape = anOwner.shape()
67       if not topLevelShape.isSubShape(selectedShape):
68         return False;
69
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()
74
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)
84       else:
85         return False
86       # propagate the connection
87       if isPropagated:
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
92
93     return theShape in self.myCached[selectedShape]
94
95   def xmlRepresentation(self):
96     """ Returns XML string which represents GUI of the filter """
97     return self.xmlFromFile("filter-TopoConnectedFaces.xml")
98
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())
103
104   def adjacentFaces(self, theFace, theMapSA, theShapeType, theApplicableFaces, theRecursive = True):
105     """ Find all faces neighbour to theFace """
106     exp = GeomAPI_ShapeExplorer(theFace, theShapeType)
107     while exp.more():
108       if  exp.current() in theMapSA:
109         faces = theMapSA[exp.current()]
110         for ind in range(faces.size()):
111           f = faces[ind]
112           if f not in theApplicableFaces:
113             theApplicableFaces.insert(f)
114             if theRecursive:
115               self.adjacentFaces(f, theMapSA, theShapeType, theApplicableFaces)
116       exp.next()
117
118
119 # Register the filter object
120 filter = FiltersPlugin_TopoConnectedFaces
121 aSession = ModelAPI_Session.get()
122 aSession.filters().registerFilter(FILTER_ID, filter)