]> SALOME platform Git repositories - modules/shaper.git/blob - src/FiltersPlugin/FiltersPlugin_OnShapeName.cpp
Salome HOME
[EDF] (2023-T1) Filters on group
[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 #include <ModelAPI_AttributeString.h>
22 #include <ModelAPI_Tools.h>
23
24 #include <Locale_Convert.h>
25 #include <regex>
26
27
28 bool FiltersPlugin_OnShapeName::isSupported(GeomAPI_Shape::ShapeType /*theType*/) const
29 {
30   return true;
31 }
32
33 //=======================================================================
34 bool FiltersPlugin_OnShapeName::isOk(const GeomShapePtr& theShape, const ResultPtr& theResult,
35                                      const ModelAPI_FiltersArgs& theArgs) const
36 {
37   if (!theResult)
38     return false;
39
40   AttributePtr anAttr = theArgs.argument("pattern");
41   AttributeStringPtr aValue = std::dynamic_pointer_cast<ModelAPI_AttributeString>(anAttr);
42   if (!aValue || !anAttr->isInitialized())
43     return false;
44
45   std::regex aPattern(aValue->value());
46
47   ResultBodyPtr aBodyRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theResult);
48
49   for(; aBodyRes.get(); aBodyRes = ModelAPI_Tools::bodyOwner(aBodyRes))
50   {
51     const DataPtr data = aBodyRes->data();
52     if (data.get()) 
53     {
54       const std::string aResName = Locale::Convert::toString(data->name());
55       if(std::regex_search(aResName, aPattern))
56       {
57         return true;
58       }
59     }
60   }
61
62   return false;
63 }
64
65 //=======================================================================
66 std::string FiltersPlugin_OnShapeName::xmlRepresentation() const
67 {
68   return xmlFromFile("filter-OnShapeName.xml");
69 }
70
71 //=======================================================================
72 void FiltersPlugin_OnShapeName::initAttributes(ModelAPI_FiltersArgs& theArguments)
73 {
74   theArguments.initAttribute("pattern", ModelAPI_AttributeString::typeId());
75 }