Salome HOME
Move representations of filters to separate XML files.
[modules/shaper.git] / src / FiltersPlugin / FiltersPlugin_OnPlane.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_OnPlane.h"
21
22 #include <ModelAPI_AttributeSelectionList.h>
23 #include <GeomAPI_Vertex.h>
24 #include <GeomAPI_Pnt.h>
25 #include <GeomAPI_Face.h>
26 #include <GeomAPI_Pln.h>
27 #include <GeomAPI_Edge.h>
28 #include <GeomAPI_Curve.h>
29 #include <GeomAPI_Circ.h>
30 #include <GeomAPI_Dir.h>
31
32
33 #define LIN_TOLERANCE 1.e-7
34
35 bool FiltersPlugin_OnPlane::isSupported(GeomAPI_Shape::ShapeType theType) const
36 {
37   switch (theType) {
38   case GeomAPI_Shape::SHELL:
39   case GeomAPI_Shape::FACE:
40   case GeomAPI_Shape::WIRE:
41   case GeomAPI_Shape::EDGE:
42   case GeomAPI_Shape::VERTEX:
43     return true;
44   }
45   return false;
46 }
47
48 GeomPlanePtr getPlane(AttributeSelectionPtr theAttr)
49 {
50   GeomShapePtr aGeom = theAttr->value();
51   GeomFacePtr aFace(new GeomAPI_Face(aGeom));
52   return aFace->getPlane();
53 }
54
55 bool FiltersPlugin_OnPlane::isOk(const GeomShapePtr& theShape, const ResultPtr&,
56                                  const ModelAPI_FiltersArgs& theArgs) const
57 {
58   AttributePtr aAttr = theArgs.argument("OnPlane");
59   AttributeSelectionListPtr aList =
60     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aAttr);
61   if (!aList.get())
62     return false;
63   if (theShape->isVertex()) {
64     GeomVertexPtr aVertex(new GeomAPI_Vertex(theShape));
65     GeomPointPtr aPnt = aVertex->point();
66     for (int i = 0; i < aList->size(); i++) {
67       GeomPlanePtr aPlane = getPlane(aList->value(i));
68       if (aPlane->distance(aPnt) < LIN_TOLERANCE)
69         return true;
70     }
71   }
72   else if (theShape->isEdge()) {
73     GeomEdgePtr aEdge(new GeomAPI_Edge(theShape));
74     if (aEdge->isLine()) {
75       GeomPointPtr aPnt1 = aEdge->firstPoint();
76       GeomPointPtr aPnt2 = aEdge->lastPoint();
77       for (int i = 0; i < aList->size(); i++) {
78         GeomPlanePtr aPlane = getPlane(aList->value(i));
79         if ((aPlane->distance(aPnt1) < LIN_TOLERANCE) && (aPlane->distance(aPnt2) < LIN_TOLERANCE))
80           return true;
81       }
82     }
83     else if (aEdge->isCircle()) {
84       GeomCurvePtr aCurve(new GeomAPI_Curve(aEdge));
85       GeomCirclePtr aCircle(new GeomAPI_Circ(aCurve));
86       GeomDirPtr aDir = aCircle->normal();
87       GeomPointPtr aPnt = aCircle->center();
88       for (int i = 0; i < aList->size(); i++) {
89         GeomPlanePtr aPlane = getPlane(aList->value(i));
90         if ((aPlane->direction()->isParallel(aDir)) && (aPlane->distance(aPnt) < LIN_TOLERANCE))
91           return true;
92       }
93     }
94   }
95   else if (theShape->isFace()) {
96     GeomFacePtr aFace(new GeomAPI_Face(theShape));
97     if (aFace->isPlanar()) {
98       GeomPlanePtr aPln = aFace->getPlane();
99       for (int i = 0; i < aList->size(); i++) {
100         AttributeSelectionPtr aAttr = aList->value(i);
101         GeomShapePtr aGeom = aAttr->value();
102         GeomPlanePtr aPlane = getPlane(aAttr);
103         if (aPlane->isCoincident(aPln))
104           return true;
105       }
106     }
107   }
108   return false;
109 }
110
111 std::string FiltersPlugin_OnPlane::xmlRepresentation() const
112 {
113   return xmlFromFile("filter-OnPlane.xml");
114 }
115
116 void FiltersPlugin_OnPlane::initAttributes(ModelAPI_FiltersArgs& theArguments)
117 {
118   theArguments.initAttribute("OnPlane", ModelAPI_AttributeSelectionList::typeId());
119 }