Salome HOME
Implement filter "F12: Topologically connected Faces"
[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       mapVFAlgo = mapShapesAndAncestors(topLevelShape, GeomAPI_Shape.VERTEX, GeomAPI_Shape.FACE)
68       mapVF = mapVFAlgo.map()
69       mapEFAlgo = mapShapesAndAncestors(topLevelShape, GeomAPI_Shape.EDGE, GeomAPI_Shape.FACE)
70       mapEF = mapEFAlgo.map()
71
72       # faces adjacent to the selected shape
73       applicableFaces = OriShapeSet()
74       if selectedShape.shapeType() == GeomAPI_Shape.VERTEX:
75         if selectedShape in mapVF: applicableFaces = mapVF[selectedShape]
76       elif selectedShape.shapeType() == GeomAPI_Shape.EDGE:
77         if selectedShape in mapEF: applicableFaces = mapEF[selectedShape]
78       elif selectedShape.shapeType() == GeomAPI_Shape.FACE:
79         applicableFaces.insert(selectedShape)
80         self.adjacentFaces(selectedShape, mapVF, GeomAPI_Shape.VERTEX, applicableFaces, False)
81       else:
82         return False
83       # propagate the connection
84       if isPropagated:
85         appFacesCopy = applicableFaces
86         for ind in range(appFacesCopy.size()):
87           self.adjacentFaces(appFacesCopy[ind], mapEF, GeomAPI_Shape.EDGE, applicableFaces)
88       self.myCached[selectedShape] = applicableFaces
89
90     return theShape in self.myCached[selectedShape]
91
92   def xmlRepresentation(self):
93     """ Returns XML string which represents GUI of the filter """
94     return self.xmlFromFile("filter-TopoConnectedFaces.xml")
95
96   def initAttributes(self, theArgs):
97     """ Initializes arguments of a filter """
98     theArgs.initAttribute("Shape", ModelAPI_AttributeSelection_typeId())
99     theArgs.initAttribute("Propagation", ModelAPI_AttributeBoolean_typeId())
100
101   def adjacentFaces(self, theFace, theMapSA, theShapeType, theApplicableFaces, theRecursive = True):
102     """ Find all faces neighbour to theFace """
103     exp = GeomAPI_ShapeExplorer(theFace, theShapeType)
104     while exp.more():
105       if  exp.current() in theMapSA:
106         faces = theMapSA[exp.current()]
107         for ind in range(faces.size()):
108           f = faces[ind]
109           if f not in theApplicableFaces:
110             theApplicableFaces.insert(f)
111             if theRecursive:
112               self.adjacentFaces(f, theMapSA, theShapeType, theApplicableFaces)
113       exp.next()
114
115
116 # Register the filter object
117 filter = FiltersPlugin_TopoConnectedFaces
118 aSession = ModelAPI_Session.get()
119 aSession.filters().registerFilter(FILTER_ID, filter)