1 // Copyright (C) 2014-2022 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 "Model_FiltersFactory.h"
22 #include "ModelAPI_AttributeBoolean.h"
23 #include "ModelAPI_AttributeSelectionList.h"
24 #include <Events_InfoMessage.h>
26 #include "GeomAPI_Edge.h"
28 void Model_FiltersFactory::registerFilter(const std::string& theID, ModelAPI_Filter* theFilter)
30 if (myFilters.find(theID) != myFilters.end()) {
31 Events_InfoMessage("Model_FiltersFactory", "Filter %1 is already registered").arg(theID).send();
34 myFilters[theID] = FilterPtr(theFilter);
41 std::string myFilterID;
44 /// Returns the filter ID without the filter index
45 static std::string pureFilterID(const std::string& theID)
47 // remove from aPure "_" + number + "_" starting part
48 if (theID.size() > 3 && theID[0] == '_') {
50 while(theID[aNumDigits + 1] < '9' && theID[aNumDigits + 1] > '0')
52 if (aNumDigits && theID[aNumDigits + 1] == '_') {
53 return theID.substr(aNumDigits + 2);
59 bool Model_FiltersFactory::isValid(FeaturePtr theFiltersFeature,
61 GeomShapePtr theShape)
63 // check that the shape type corresponds to the attribute list type
65 std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(theFiltersFeature)->baseAttribute();
67 std::shared_ptr<ModelAPI_AttributeSelectionList> aList =
68 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aBase);
69 std::string aStrType = aList->selectionType();
70 GeomAPI_Shape::ShapeType aType = GeomAPI_Shape::shapeTypeByStr(aStrType);
71 if (theShape->shapeType() != aType)
74 // prepare all filters args
75 ModelAPI_FiltersArgs anArgs;
76 std::list<FilterArgs> aFilters; /// all filters and the reverse values
78 std::list<std::string> aGroups;
79 theFiltersFeature->data()->allGroups(aGroups);
80 for(std::list<std::string>::iterator aGIter = aGroups.begin(); aGIter != aGroups.end(); aGIter++)
82 std::string aPureID = pureFilterID(*aGIter);
83 if (myFilters.find(aPureID) == myFilters.end())
85 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs;
86 theFiltersFeature->data()->attributesOfGroup(*aGIter, anAttrs);
87 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrIter = anAttrs.begin();
88 for(; anAttrIter != anAttrs.end(); anAttrIter++) {
89 std::string anArgID = (*anAttrIter)->id().substr((*aGIter).length() + 2);
90 if (anArgID.empty()) { // reverse flag
91 std::shared_ptr<ModelAPI_AttributeBoolean> aReverse =
92 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(*anAttrIter);
93 FilterArgs aFArgs = { myFilters[aPureID] , aReverse->value() , *aGIter };
94 aFilters.push_back(aFArgs);
97 anArgs.add(*anAttrIter);
102 // iterate filters and check shape for validity for all of them
103 std::list<FilterArgs>::iterator aFilter = aFilters.begin();
104 for(; aFilter != aFilters.end(); aFilter++) {
105 anArgs.setFilter(aFilter->myFilterID);
106 bool aResult = aFilter->myFilter->isOk(theShape, theResult, anArgs);
107 if (aFilter->myReverse)
109 if (!aResult) // one filter is failed => exit immediately
112 // all filters are passed
116 std::list< std::pair<ResultPtr, GeomShapePtr> > Model_FiltersFactory::select
117 (const FiltersFeaturePtr& theFilterFeature,
118 const GeomAPI_Shape::ShapeType theShapeType)
120 std::list< std::pair<ResultPtr, GeomShapePtr> > aResList;
122 DocumentPtr aDoc = theFilterFeature->document();
123 int aNb = aDoc->size(ModelAPI_ResultBody::group());
126 for (int i = 0; i < aNb; i++) {
127 aObj = aDoc->object(ModelAPI_ResultBody::group(), i);
128 aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObj);
129 GeomShapePtr aShape = aBody->shape();
130 std::list<GeomShapePtr> aSubShapes = aShape->subShapes(theShapeType, true);
131 std::list<GeomShapePtr>::const_iterator aShapesIt;
132 for (aShapesIt = aSubShapes.cbegin(); aShapesIt != aSubShapes.cend(); aShapesIt++) {
133 GeomShapePtr aSubShape = (*aShapesIt);
135 // degenerated edge is not valid selection
136 if (theShapeType == GeomAPI_Shape::EDGE)
137 if (aSubShape->edge()->isDegenerated())
140 bool isValid = this->isValid(theFilterFeature, aBody, aSubShape);
143 // bos #24043: Naming on a compsolid works wrong.
144 // Find a simple sub-result for the ViewerPrs context:
145 ResultBodyPtr aContext = aBody;
146 bool isComposite = aContext->numberOfSubs() > 0;
147 while (isComposite) {
149 int nbSubs = aContext->numberOfSubs();
150 for (int aSubIndex = 0; aSubIndex < nbSubs; aSubIndex++) {
151 ResultBodyPtr aSubResult = aContext->subResult(aSubIndex);
152 GeomShapePtr aSubResultShape = aSubResult->shape();
153 if (aSubResultShape->isSubShape(aSubShape)) {
154 aContext = aSubResult;
155 isComposite = aContext->numberOfSubs() > 0;
160 std::pair<ResultPtr, GeomShapePtr> aPair (aContext, aSubShape);
161 aResList.push_back(aPair);
169 /// Returns list of filters for the given shape type
170 /// \param theType a shape type
171 std::list<FilterPtr> Model_FiltersFactory::filters(GeomAPI_Shape::ShapeType theType)
173 std::list<FilterPtr> aResult;
174 std::map<std::string, FilterPtr>::const_iterator anIt;
175 std::list<int> aTypes;
176 std::list<int>::const_iterator aTIt;
177 for (anIt = myFilters.cbegin(); anIt != myFilters.cend(); anIt++) {
178 if (anIt->second->isSupported(theType))
179 aResult.push_back(anIt->second);
184 FilterPtr Model_FiltersFactory::filter(std::string theID)
186 std::string aPureID = pureFilterID(theID);
187 std::map<std::string, FilterPtr>::iterator aFound = myFilters.find(aPureID);
188 return aFound == myFilters.end() ? FilterPtr() : aFound->second;
191 std::string Model_FiltersFactory::id(FilterPtr theFilter)
193 std::map<std::string, FilterPtr>::iterator anIter = myFilters.begin();
194 for(; anIter != myFilters.end(); anIter++) {
195 if (anIter->second == theFilter)
196 return anIter->first;
198 return ""; // unknown case