Salome HOME
6a5c458d8b65c7a2bad9049e39d1ce5a61c391ff
[modules/shaper.git] / src / FiltersPlugin / FiltersPlugin_OnPlaneSide.cpp
1 // Copyright (C) 2014-2022  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_OnPlaneSide.h"
21
22 #include <ModelAPI_AttributeSelection.h>
23
24 #include <GeomAPI_Face.h>
25 #include <GeomAPI_Pln.h>
26 #include <GeomAPI_Pnt.h>
27 #include <GeomAPI_XYZ.h>
28
29
30 bool FiltersPlugin_OnPlaneSide::isSupported(GeomAPI_Shape::ShapeType /*theType*/) const
31 {
32   return true;
33 }
34
35 bool FiltersPlugin_OnPlaneSide::isOk(const GeomShapePtr& theShape, const ResultPtr&,
36                                      const ModelAPI_FiltersArgs& theArgs) const
37 {
38   static const double THE_TOLERANCE = 1.e-7;
39
40   AttributePtr aAttr = theArgs.argument("OnPlaneSide");
41   AttributeSelectionPtr aList = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aAttr);
42   if (!aList.get())
43     return false;
44   GeomShapePtr aPlanarShape = aList->value();
45   if (!aPlanarShape) {
46     ResultPtr aContext = aList->context();
47     if (!aContext)
48       return false;
49     aPlanarShape = aContext->shape();
50   }
51
52   GeomPlanePtr aPlane = aPlanarShape->face()->getPlane();
53   GeomPointPtr aPlaneLoc = aPlane->location();
54   GeomDirPtr aPlaneNorm = aPlane->direction();
55
56   GeomPointPtr aShapeCenter = theShape->middlePoint();
57   GeomXYZPtr aVec = aShapeCenter->xyz()->decreased(aPlaneLoc->xyz());
58
59   return aVec->dot(aPlaneNorm->xyz()) > THE_TOLERANCE;
60 }
61
62 std::string FiltersPlugin_OnPlaneSide::xmlRepresentation() const
63 {
64   return xmlFromFile("filter-OnPlaneSide.xml");
65 }
66
67 void FiltersPlugin_OnPlaneSide::initAttributes(ModelAPI_FiltersArgs& theArguments)
68 {
69   theArguments.initAttribute("OnPlaneSide", ModelAPI_AttributeSelection::typeId());
70 }