1 // Copyright (C) 2014-2020 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
20 #include "FiltersPlugin_OppositeToEdge.h"
22 #include <ModelAPI_AttributeSelection.h>
23 #include <ModelAPI_ResultBody.h>
24 #include <ModelAPI_Tools.h>
26 #include <GeomAPI_Shape.h>
27 #include <GeomAPI_ShapeExplorer.h>
28 #include <GeomAPI_Wire.h>
29 #include <GeomAPI_WireExplorer.h>
33 typedef std::map<GeomShapePtr, SetOfShapes, GeomAPI_Shape::Comparator> MapShapeAndAncestors;
35 static void mapEdgesAndFaces(const GeomShapePtr theShape, MapShapeAndAncestors& theMap)
37 GeomAPI_ShapeExplorer aFExp(theShape, GeomAPI_Shape::FACE);
38 for (; aFExp.more(); aFExp.next()) {
39 GeomShapePtr aFace = aFExp.current();
40 GeomAPI_ShapeExplorer aEExp(aFace, GeomAPI_Shape::EDGE);
41 for (; aEExp.more(); aEExp.next())
42 theMap[aEExp.current()].insert(aFace);
46 // Return edge in the quadratic face opposite to the given one.
47 // If the face is not quadratic, returns empty shape.
48 static GeomShapePtr oppositeEdgeInQuadFace(const GeomShapePtr theEdge,
49 const GeomShapePtr theFace)
51 static int THE_QUAD = 4;
54 int anOriginalEdgeIndex = -THE_QUAD;
55 GeomShapePtr anOppositeEdge;
56 GeomAPI_ShapeExplorer aWExp(theFace, GeomAPI_Shape::WIRE);
57 GeomWirePtr aWire = aWExp.current()->wire();
60 // face with a hole is not a quadrangle
61 return anOppositeEdge;
64 GeomAPI_WireExplorer anExp(aWire);
65 while (anExp.more()) {
66 if (anExp.current()->isSame(theEdge))
67 anOriginalEdgeIndex = aNbEdges;
68 else if (aNbEdges == anOriginalEdgeIndex + THE_QUAD / 2) {
69 if (anOriginalEdgeIndex < THE_QUAD)
70 anOppositeEdge = anExp.current();
71 if (aNbEdges >= THE_QUAD)
78 if (aNbEdges != THE_QUAD) {
80 anOppositeEdge = GeomShapePtr();
87 return anOppositeEdge;
90 // Find all opposite edges for the given.
91 static void cacheOppositeEdge(const GeomShapePtr theEdge,
92 const MapShapeAndAncestors& theEdgeToFaces,
93 SetOfShapes& theCache)
95 MapShapeAndAncestors::const_iterator aFound = theEdgeToFaces.find(theEdge);
96 if (aFound == theEdgeToFaces.end())
99 for (SetOfShapes::const_iterator aFIt = aFound->second.begin();
100 aFIt != aFound->second.end(); ++aFIt) {
101 GeomShapePtr anOpposite = oppositeEdgeInQuadFace(theEdge, *aFIt);
102 if (anOpposite && theCache.find(anOpposite) == theCache.end()) {
103 theCache.insert(anOpposite);
104 cacheOppositeEdge(anOpposite, theEdgeToFaces, theCache);
109 static void cacheOppositeEdges(const GeomShapePtr theTopLevelShape,
110 const GeomShapePtr theEdge,
111 SetOfShapes& theCache)
113 if (!theTopLevelShape || !theEdge)
116 MapShapeAndAncestors anEdgesToFaces;
117 mapEdgesAndFaces(theTopLevelShape, anEdgesToFaces);
119 // keep the original edge
120 theCache.insert(theEdge);
121 // cache opposite edges
122 cacheOppositeEdge(theEdge, anEdgesToFaces, theCache);
126 bool FiltersPlugin_OppositeToEdge::isSupported(GeomAPI_Shape::ShapeType theType) const
128 return theType == GeomAPI_Shape::EDGE;
131 bool FiltersPlugin_OppositeToEdge::isOk(const GeomShapePtr& theShape, const ResultPtr&,
132 const ModelAPI_FiltersArgs& theArgs) const
134 AttributePtr aAttr = theArgs.argument("OppositeToEdge");
135 AttributeSelectionPtr aList = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aAttr);
138 GeomShapePtr anEdge = aList->value();
139 if (!myOriginalEdge || !myOriginalEdge->isSame(anEdge)) {
140 // new edge is selected, need to update the cache
141 const_cast<FiltersPlugin_OppositeToEdge*>(this)->myOriginalEdge = anEdge;
142 const_cast<FiltersPlugin_OppositeToEdge*>(this)->myCachedShapes.clear();
145 if (myCachedShapes.empty()) {
146 ResultBodyPtr aBaseResult = ModelAPI_Tools::bodyOwner(aList->context(), true);
147 if (!aBaseResult.get()) {
148 aBaseResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aList->context());
149 if (!aBaseResult.get())
153 cacheOppositeEdges(aBaseResult->shape(), anEdge,
154 const_cast<FiltersPlugin_OppositeToEdge*>(this)->myCachedShapes);
157 return myCachedShapes.find(theShape) != myCachedShapes.end();
160 std::string FiltersPlugin_OppositeToEdge::xmlRepresentation() const
162 return xmlFromFile("filter-OppositeToEdge.xml");
165 void FiltersPlugin_OppositeToEdge::initAttributes(ModelAPI_FiltersArgs& theArguments)
167 theArguments.initAttribute("OppositeToEdge", ModelAPI_AttributeSelection::typeId());