Salome HOME
Finalize implementation of filter "F8: On/In/Out a Solid"
[modules/shaper.git] / src / FiltersPlugin / FiltersPlugin_RelativeToSolid.cpp
1 // Copyright (C) 2014-2019  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_RelativeToSolid.h"
21
22 #include <GeomAPI_Solid.h>
23
24 #include <GeomAlgoAPI_SolidClassifier.h>
25
26 #include <ModelAPI_AttributeSelection.h>
27 #include <ModelAPI_AttributeString.h>
28
29
30 bool FiltersPlugin_RelativeToSolid::isSupported(GeomAPI_Shape::ShapeType theType) const
31 {
32   return true;
33 }
34
35 bool FiltersPlugin_RelativeToSolid::isOk(const GeomShapePtr& theShape,
36                                          const ModelAPI_FiltersArgs& theArgs) const
37 {
38   AttributePtr anAttr = theArgs.argument("Solid");
39   AttributeSelectionPtr aSel = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttr);
40   if (!aSel)
41     return false;
42
43   anAttr = theArgs.argument("Location");
44   AttributeStringPtr aLocAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(anAttr);
45   if (!aLocAttr)
46     return false;
47   std::string aLocString = aLocAttr->value();
48
49   GeomShapePtr aSolidSelected = aSel->value();
50   if (!aSolidSelected && aSel->context())
51     aSolidSelected = aSel->context()->shape();
52   GeomSolidPtr aSolid = aSolidSelected ? aSolidSelected->solid() : GeomSolidPtr();
53   if (!aSolid)
54     return false;
55
56   GeomAlgoAPI_SolidClassifier aClassifier(aSolid, theShape);
57   GeomAlgoAPI_SolidClassifier::State aState = aClassifier.state();
58
59   bool isOK = false;
60   if (aLocString == "in")
61     isOK = aState == GeomAlgoAPI_SolidClassifier::State_IN;
62   else if (aLocString == "out")
63     isOK = aState == GeomAlgoAPI_SolidClassifier::State_OUT;
64   else if (aLocString == "on")
65     isOK = aState == GeomAlgoAPI_SolidClassifier::State_ON;
66   else if (aLocString == "not_on")
67     isOK = !(aState & GeomAlgoAPI_SolidClassifier::State_ON);
68   else if (aLocString == "not_out")
69     isOK = !(aState & GeomAlgoAPI_SolidClassifier::State_OUT);
70   else if (aLocString == "not_in")
71     isOK = !(aState & GeomAlgoAPI_SolidClassifier::State_IN);
72   return isOK;
73 }
74
75 static std::string XMLRepresentation =
76 "<filter id = \"RelativeToSolid\">"
77 " <shape_selector id=\"RelativeToSolid__Solid\""
78 "   label=\"Solid:\""
79 "   tooltip=\"Select a solid.\""
80 "   shape_types=\"solids\">"
81 "   <validator id=\"GeomValidators_ShapeType\" parameters=\"solid\"/>"
82 " </shape_selector>"
83 " <switch id=\"RelativeToSolid__Location\" label=\"Algorithm:\">"
84 "   <case id=\"in\" title=\"In\"/>"
85 "   <case id=\"out\" title=\"Out\"/>"
86 "   <case id=\"on\" title=\"On\"/>"
87 "   <case id=\"not_on\" title=\"Not On\"/>"
88 "   <case id=\"not_out\" title=\"In and On\"/>"
89 "   <case id=\"not_in\" title=\"On and Out\"/>"
90 " </switch>"
91 "</filter>";
92
93
94 std::string FiltersPlugin_RelativeToSolid::xmlRepresentation() const
95 {
96   return XMLRepresentation;
97 }
98
99 void FiltersPlugin_RelativeToSolid::initAttributes(ModelAPI_FiltersArgs& theArguments)
100 {
101   theArguments.initAttribute("Solid", ModelAPI_AttributeSelection::typeId());
102   theArguments.initAttribute("Location", ModelAPI_AttributeString::typeId());
103 }