Salome HOME
Non planar faces 35156
[modules/shaper.git] / src / FiltersPlugin / FiltersPlugin_OnShapeName.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
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 #include "FiltersPlugin_OnShapeName.h"
21
22 #include <ModelAPI_AttributeString.h>
23 // #include <Model_Data.h>
24 // #include <TDF_Label.hxx>
25 // #include <TNaming_Builder.hxx>
26 // #include <TNaming_NamedShape.hxx>
27 // #include <TDataStd_Name.hxx>
28 // #include <TNaming_Tool.hxx>
29 // #include <TopoDS_Shape.hxx>
30
31 #include <Locale_Convert.h>
32 #include <boost/algorithm/string.hpp>
33
34 bool FiltersPlugin_OnShapeName::isSupported(GeomAPI_Shape::ShapeType /*theType*/) const
35 {
36   return true;
37 }
38
39 static bool getShapesName(const GeomShapePtr& theShape,
40                           ResultBodyPtr theResultBody,
41                           std::string& theShapeName)
42 {
43   int nbSubRes = theResultBody->numberOfSubs();
44   if(theResultBody->shape()->isSame(theShape) || (nbSubRes == 0 && theResultBody->shape()->isSubShape(theShape)))
45   {
46     DataPtr data = theResultBody->data();
47     if (data.get()) 
48     {
49       theShapeName = Locale::Convert::toString(data->name());
50       return true;
51     }
52   }
53
54   bool aResult(false);
55   ResultBodyPtr subResult;
56   for (int i = 0; i < nbSubRes; i++)
57   {
58     subResult = theResultBody->subResult(i);
59     if(getShapesName(theShape, subResult, theShapeName))
60     {
61       aResult = true;
62       break;
63     }
64   }
65
66   return aResult;
67 }
68
69 bool FiltersPlugin_OnShapeName::isOk(const GeomShapePtr& theShape, const ResultPtr& theResult,
70                                      const ModelAPI_FiltersArgs& theArgs) const
71 {
72   AttributePtr anAttr = theArgs.argument("pattern");
73   AttributeStringPtr aValue = std::dynamic_pointer_cast<ModelAPI_AttributeString>(anAttr);
74   if (!aValue.get()|| !anAttr->isInitialized())
75     return false;
76   std::string aPattern= aValue->value();
77
78
79   ResultBodyPtr aBodyRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theResult);
80   std::string aShapeName;
81   if(!getShapesName(theShape, aBodyRes, aShapeName))
82   {
83     return false;
84   }
85
86   //remove * from both sides of pattern
87   if(aPattern.back() == '*' && aPattern.front() == '*')
88   {
89     aPattern.pop_back();
90     aPattern.erase(aPattern.begin());
91   }
92
93   bool isFound = false;
94   if(aPattern.front() == '*')
95   {
96     aPattern.erase(aPattern.begin());
97     isFound = boost::algorithm::ends_with(aShapeName, aPattern);
98   }
99   else if(aPattern.back() == '*')
100   {
101     aPattern.pop_back();
102     isFound = boost::algorithm::starts_with(aShapeName, aPattern);
103   }
104   else
105   {
106     isFound = (aShapeName.find(aPattern) != std::string::npos);
107   }
108
109   return isFound;
110 }
111
112 std::string FiltersPlugin_OnShapeName::xmlRepresentation() const
113 {
114   return xmlFromFile("filter-OnShapeName.xml");
115 }
116
117 void FiltersPlugin_OnShapeName::initAttributes(ModelAPI_FiltersArgs& theArguments)
118 {
119   theArguments.initAttribute("pattern", ModelAPI_AttributeString::typeId());
120 }