1 // Copyright (C) 2014-2023 CEA, EDF
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_ContinuousFaces.h"
22 #include <ModelAPI_AttributeSelectionList.h>
23 #include <ModelAPI_AttributeSelection.h>
24 #include <ModelAPI_AttributeDouble.h>
25 #include <ModelAPI_Tools.h>
27 #include <GeomAPI_Edge.h>
28 #include <GeomAPI_Shape.h>
29 #include <GeomAPI_ShapeExplorer.h>
30 #include <GeomAPI_Wire.h>
32 #include <GeomAlgoAPI_ShapeTools.h>
38 typedef std::map<GeomShapePtr, SetOfShapes, GeomAPI_Shape::Comparator> MapShapeAndAncestors;
40 //=================================================================================================
41 static void mapEdgesAndFaces(const GeomShapePtr theShape, MapShapeAndAncestors& theMap)
43 GeomAPI_ShapeExplorer aFExp(theShape, GeomAPI_Shape::FACE);
44 for (; aFExp.more(); aFExp.next()) {
45 GeomShapePtr aFace = aFExp.current();
46 GeomAPI_ShapeExplorer aEExp(aFace, GeomAPI_Shape::EDGE);
47 for (; aEExp.more(); aEExp.next())
48 theMap[aEExp.current()].insert(aFace);
52 //=================================================================================================
53 // Find all continuous faces for the given.
54 static void cacheContinuousFace(const GeomShapePtr theFace,
55 const MapShapeAndAncestors& theEdgeToFaces,
56 SetOfShapes& theCache,
57 const double & theAngle)
60 MapShapeAndAncestors::const_iterator aFound;
61 GeomAPI_ShapeExplorer aEExp(theFace, GeomAPI_Shape::EDGE);
62 for (; aEExp.more(); aEExp.next()){
63 aFound = theEdgeToFaces.find(aEExp.current());
64 if (aFound == theEdgeToFaces.end())
68 anEdge = GeomEdgePtr(new GeomAPI_Edge(aEExp.current()));
70 for (SetOfShapes::const_iterator aFIt = aFound->second.begin();
71 aFIt != aFound->second.end(); ++aFIt) {
72 std::string anError = "";
73 if (theCache.find(*aFIt) == theCache.end()) {
74 GeomPointPtr aPoint = anEdge->middlePoint();
75 if (GeomAlgoAPI_ShapeTools::isContinuousFaces(theFace,
80 theCache.insert(*aFIt);
81 cacheContinuousFace(*aFIt, theEdgeToFaces, theCache, theAngle);
88 //=================================================================================================
89 static void cacheContinuousFaces(const GeomShapePtr theTopLevelShape,
90 const SetOfShapes& theFaces,
91 SetOfShapes& theCache,
92 const double & theAngle)
94 if (!theTopLevelShape || theFaces.empty())
97 MapShapeAndAncestors anEdgesToFaces;
98 mapEdgesAndFaces(theTopLevelShape, anEdgesToFaces);
100 for (SetOfShapes::const_iterator aFIt = theFaces.begin();
101 aFIt != theFaces.end(); ++aFIt) {
102 // keep the original face
103 theCache.insert(*aFIt);
104 // cache continuous face
105 cacheContinuousFace(*aFIt, anEdgesToFaces, theCache,theAngle);
109 //=================================================================================================
110 static bool updateFaces(const AttributeSelectionListPtr& theList,
111 SetOfShapes& theFaces)
113 bool aNewCache = false;
114 if ((int)theFaces.size() != theList->size()) {
117 for (int i = 0; i < theList->size(); i++) {
118 AttributeSelectionPtr aCurAttr = theList->value(i);
119 GeomShapePtr aFace = aCurAttr->value();
120 if (theFaces.empty() || theFaces.find(aFace) == theFaces.end()) {
128 for (int i = 0; i < theList->size(); i++) {
129 AttributeSelectionPtr aCurAttr = theList->value(i);
130 GeomShapePtr aFace = aCurAttr->value();
131 theFaces.insert(aFace);
137 //=================================================================================================
138 bool FiltersPlugin_ContinuousFaces::isSupported(GeomAPI_Shape::ShapeType theType) const
140 return theType == GeomAPI_Shape::FACE;
143 //=================================================================================================
144 bool FiltersPlugin_ContinuousFaces::isOk(const GeomShapePtr& theShape, const ResultPtr&,
145 const ModelAPI_FiltersArgs& theArgs) const
147 AttributePtr aAttr = theArgs.argument("faces");
148 AttributeSelectionListPtr aList =
149 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aAttr);
153 AttributePtr anAttr = theArgs.argument("value");
154 AttributeDoublePtr aValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(anAttr);
155 if (!aValue.get()|| !anAttr->isInitialized())
157 double anAngle= aValue->value();
159 bool aNewCache = updateFaces(aList,
160 const_cast<FiltersPlugin_ContinuousFaces*>(this)->myFaces);
162 if (aNewCache || fabs(myAngle - anAngle) > 1e-10 ) {
163 const_cast<FiltersPlugin_ContinuousFaces*>(this)->myAngle = anAngle;
164 const_cast<FiltersPlugin_ContinuousFaces*>(this)->myCachedShapes.clear();
167 if (myCachedShapes.empty()) {
168 for (int i = 0; i < aList->size(); i++)
170 ResultBodyPtr aBaseResult = ModelAPI_Tools::bodyOwner(aList->value(i)->context(), true);
171 if (!aBaseResult.get()) {
172 aBaseResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aList->value(i)->context());
173 if (!aBaseResult.get())
176 cacheContinuousFaces(aBaseResult->shape(),
177 const_cast<FiltersPlugin_ContinuousFaces*>(this)->myFaces,
178 const_cast<FiltersPlugin_ContinuousFaces*>(this)->myCachedShapes,anAngle);
181 return myCachedShapes.find(theShape) != myCachedShapes.end();
184 //=================================================================================================
185 std::string FiltersPlugin_ContinuousFaces::xmlRepresentation() const
187 return xmlFromFile("filter-ContinuousFaces.xml");
190 //=================================================================================================
191 void FiltersPlugin_ContinuousFaces::initAttributes(ModelAPI_FiltersArgs& theArguments)
193 theArguments.initAttribute("value", ModelAPI_AttributeDouble::typeId());
194 theArguments.initAttribute("faces", ModelAPI_AttributeSelectionList::typeId());