Salome HOME
CEA : Lot2 - Add new filters
[modules/shaper.git] / src / FiltersPlugin / FiltersPlugin_EdgeSize.cpp
1 // Copyright (C) 2014-2021  CEA/DEN, EDF R&D
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_EdgeSize.h"
21
22 #include <ModelAPI_AttributeString.h>
23 #include <ModelAPI_AttributeDouble.h>
24 #include <ModelAPI_ResultBody.h>
25 #include <ModelAPI_Tools.h>
26
27 #include <GeomAPI_Edge.h>
28 #include <GeomAPI_Shape.h>
29 #include <GeomAPI_Wire.h>
30
31 #include <GeomAlgoAPI_ShapeTools.h>
32
33 #include <Precision.hxx>
34
35 #include <map>
36 #include <cmath>
37
38 //=================================================================================================
39 bool FiltersPlugin_EdgeSize::isSupported(GeomAPI_Shape::ShapeType theType) const
40 {
41   return theType == GeomAPI_Shape::EDGE;
42 }
43
44 //=================================================================================================
45 bool FiltersPlugin_EdgeSize::isOk(const GeomShapePtr& theShape, const ResultPtr&,
46                                   const ModelAPI_FiltersArgs& theArgs) const
47 {
48   AttributePtr anAttr = theArgs.argument("value");
49   AttributeDoublePtr aValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(anAttr);
50   if (!aValue.get()|| !anAttr->isInitialized() )
51     return false;
52   double aVal = aValue->value();
53
54   anAttr = theArgs.argument("valueMax");
55   aValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(anAttr);
56   if (!aValue.get()|| !anAttr->isInitialized() )
57     return false;
58   double aValMax = aValue->value();
59
60   if (aVal < 0.0)
61     return false;
62
63   GeomEdgePtr anEdge;
64   switch (theShape->shapeType()) {
65   case GeomAPI_Shape::EDGE:
66     anEdge = GeomEdgePtr(new GeomAPI_Edge(theShape));
67     break;
68   case GeomAPI_Shape::WIRE:
69     anEdge = GeomAlgoAPI_ShapeTools::wireToEdge(
70         GeomWirePtr(new GeomAPI_Wire(theShape)));
71     break;
72   default:
73     return false;
74   }
75
76   double aLength = anEdge->length();
77
78   anAttr = theArgs.argument("comparatorType");
79   AttributeStringPtr aCompAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(anAttr);
80   if (!aCompAttr)
81     return false;
82   std::string aCompString = aCompAttr->value();
83
84   bool isOK = false;
85   if (aCompString == "inf")
86     isOK = aLength < aVal - Precision::Confusion();
87   else if (aCompString == "infEq")
88     isOK = aLength < aVal + Precision::Confusion();
89   else if (aCompString == "sup")
90     isOK = aLength > aVal + Precision::Confusion();
91   else if (aCompString == "supEq")
92     isOK = aLength > aVal - Precision::Confusion();
93   else if (aCompString == "isBetween")
94     isOK = aVal <= aValMax
95            && aLength > aVal - Precision::Confusion()
96            && aLength < aValMax + Precision::Confusion();
97   else if (aCompString == "isStrictlyBetween")
98     isOK = aVal <= aValMax
99            && aLength > aVal + Precision::Confusion()
100            && aLength < aValMax - Precision::Confusion();
101   return isOK;
102 }
103
104 //=================================================================================================
105 std::string FiltersPlugin_EdgeSize::xmlRepresentation() const
106 {
107   return xmlFromFile("filter-EdgeSize.xml");
108 }
109
110 //=================================================================================================
111 void FiltersPlugin_EdgeSize::initAttributes(ModelAPI_FiltersArgs& theArguments)
112 {
113   theArguments.initAttribute("comparatorType", ModelAPI_AttributeString::typeId());
114   theArguments.initAttribute("value", ModelAPI_AttributeDouble::typeId());
115   theArguments.initAttribute("valueMax", ModelAPI_AttributeDouble::typeId());
116 }