Salome HOME
Implement filter "F9: External Faces"
[modules/shaper.git] / src / FiltersPlugin / FiltersPlugin_OnPlaneSide.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_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     aPlanarShape = aList->context()->shape();
47
48   GeomPlanePtr aPlane = aPlanarShape->face()->getPlane();
49   GeomPointPtr aPlaneLoc = aPlane->location();
50   GeomDirPtr aPlaneNorm = aPlane->direction();
51
52   GeomPointPtr aShapeCenter = theShape->middlePoint();
53   GeomXYZPtr aVec = aShapeCenter->xyz()->decreased(aPlaneLoc->xyz());
54
55   return aVec->dot(aPlaneNorm->xyz()) > THE_TOLERANCE;
56 }
57
58 static std::string XMLRepresentation =
59 "<filter id = \"OnPlaneSide\">"
60 " <shape_selector id=\"OnPlaneSide__OnPlaneSide\""
61 "   label=\"Plane:\""
62 "   tooltip=\"Select plane or planar face.\""
63 "   shape_types=\"faces\">"
64 "   <validator id=\"GeomValidators_ShapeType\" parameters=\"plane\"/>"
65 " </shape_selector>"
66 "</filter>";
67
68
69 std::string FiltersPlugin_OnPlaneSide::xmlRepresentation() const
70 {
71   return XMLRepresentation;
72 }
73
74 void FiltersPlugin_OnPlaneSide::initAttributes(ModelAPI_FiltersArgs& theArguments)
75 {
76   theArguments.initAttribute("OnPlaneSide", ModelAPI_AttributeSelection::typeId());
77 }