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
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>
31 typedef std::map<GeomShapePtr, SetOfShapes, GeomAPI_Shape::Comparator> MapShapeAndAncestors;
33 static void mapEdgesAndFaces(const GeomShapePtr theShape, MapShapeAndAncestors& theMap)
35 GeomAPI_ShapeExplorer aFExp(theShape, GeomAPI_Shape::FACE);
36 for (; aFExp.more(); aFExp.next()) {
37 GeomShapePtr aFace = aFExp.current();
38 GeomAPI_ShapeExplorer aEExp(aFace, GeomAPI_Shape::EDGE);
39 for (; aEExp.more(); aEExp.next())
40 theMap[aEExp.current()].insert(aFace);
44 // Return edge in the quadratic face opposite to the given one.
45 // If the face is not quadratic, returns empty shape.
46 static GeomShapePtr oppositeEdgeInQuadFace(const GeomShapePtr theEdge,
47 const GeomShapePtr theFace)
49 static int THE_QUAD = 4;
52 int anOriginalEdgeIndex = -THE_QUAD;
53 GeomShapePtr anOppositeEdge;
54 GeomAPI_ShapeExplorer anExp(theFace, GeomAPI_Shape::EDGE);
55 while (anExp.more()) {
56 if (anExp.current()->isSame(theEdge))
57 anOriginalEdgeIndex = aNbEdges;
58 else if (aNbEdges == anOriginalEdgeIndex + THE_QUAD / 2) {
59 anOppositeEdge = anExp.current();
60 if (aNbEdges >= THE_QUAD)
67 if (aNbEdges != THE_QUAD) {
69 anOppositeEdge = GeomShapePtr();
76 return anOppositeEdge;
79 // Find all opposite edges for the given.
80 static void cacheOppositeEdge(const GeomShapePtr theEdge,
81 const MapShapeAndAncestors& theEdgeToFaces,
82 SetOfShapes& theCache)
84 MapShapeAndAncestors::const_iterator aFound = theEdgeToFaces.find(theEdge);
85 if (aFound == theEdgeToFaces.end())
88 for (SetOfShapes::const_iterator aFIt = aFound->second.begin();
89 aFIt != aFound->second.end(); ++aFIt) {
90 GeomShapePtr anOpposite = oppositeEdgeInQuadFace(theEdge, *aFIt);
91 if (anOpposite && theCache.find(anOpposite) == theCache.end()) {
92 theCache.insert(anOpposite);
93 cacheOppositeEdge(anOpposite, theEdgeToFaces, theCache);
98 static void cacheOppositeEdges(const GeomShapePtr theTopLevelShape,
99 const GeomShapePtr theEdge,
100 SetOfShapes& theCache)
102 if (!theTopLevelShape || !theEdge)
105 MapShapeAndAncestors anEdgesToFaces;
106 mapEdgesAndFaces(theTopLevelShape, anEdgesToFaces);
108 // keep the original edge
109 theCache.insert(theEdge);
110 // cache opposite edges
111 cacheOppositeEdge(theEdge, anEdgesToFaces, theCache);
115 bool FiltersPlugin_OppositeToEdge::isSupported(GeomAPI_Shape::ShapeType theType) const
117 return theType == GeomAPI_Shape::EDGE;
120 bool FiltersPlugin_OppositeToEdge::isOk(const GeomShapePtr& theShape, const ResultPtr&,
121 const ModelAPI_FiltersArgs& theArgs) const
123 AttributePtr aAttr = theArgs.argument("OppositeToEdge");
124 AttributeSelectionPtr aList = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aAttr);
127 GeomShapePtr anEdge = aList->value();
128 if (!myOriginalEdge || !myOriginalEdge->isSame(anEdge)) {
129 // new edge is selected, need to update the cache
130 const_cast<FiltersPlugin_OppositeToEdge*>(this)->myOriginalEdge = anEdge;
131 const_cast<FiltersPlugin_OppositeToEdge*>(this)->myCachedShapes.clear();
134 if (myCachedShapes.empty()) {
135 ResultBodyPtr aBaseResult = ModelAPI_Tools::bodyOwner(aList->context(), true);
136 if (!aBaseResult.get())
139 cacheOppositeEdges(aBaseResult->shape(), anEdge,
140 const_cast<FiltersPlugin_OppositeToEdge*>(this)->myCachedShapes);
143 return myCachedShapes.find(theShape) != myCachedShapes.end();
146 static std::string XMLRepresentation =
147 "<filter id = \"OppositeToEdge\">"
148 " <shape_selector id=\"OppositeToEdge__OppositeToEdge\""
150 " tooltip=\"Select edge.\""
151 " shape_types=\"edges\">"
152 " <validator id=\"GeomValidators_ShapeType\" parameters=\"line\"/>"
157 std::string FiltersPlugin_OppositeToEdge::xmlRepresentation() const
159 return XMLRepresentation;
162 void FiltersPlugin_OppositeToEdge::initAttributes(ModelAPI_FiltersArgs& theArguments)
164 theArguments.initAttribute("OppositeToEdge", ModelAPI_AttributeSelection::typeId());