Salome HOME
a4b8b840159db9f59547b6c528ae7120ea223a94
[modules/shaper.git] / src / FiltersPlugin / FiltersPlugin_RelativeToSolid.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_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 theType >= GeomAPI_Shape::SHELL;
33 }
34
35 bool FiltersPlugin_RelativeToSolid::isOk(const GeomShapePtr& theShape, const ResultPtr&,
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 std::string FiltersPlugin_RelativeToSolid::xmlRepresentation() const
76 {
77   return xmlFromFile("filter-RelativeToSolid.xml");
78 }
79
80 void FiltersPlugin_RelativeToSolid::initAttributes(ModelAPI_FiltersArgs& theArguments)
81 {
82   theArguments.initAttribute("Solid", ModelAPI_AttributeSelection::typeId());
83   theArguments.initAttribute("Location", ModelAPI_AttributeString::typeId());
84 }