Salome HOME
Copyright update 2020
[modules/shaper.git] / src / FiltersPlugin / FiltersPlugin_OnPlane.cpp
1 // Copyright (C) 2014-2020  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   if (!aGeom.get()) {  // construction plane, #2942
52     aGeom = theAttr->context()->shape();
53   }
54   GeomFacePtr aFace(new GeomAPI_Face(aGeom));
55   return aFace->getPlane();
56 }
57
58 bool FiltersPlugin_OnPlane::isOk(const GeomShapePtr& theShape, const ResultPtr&,
59                                  const ModelAPI_FiltersArgs& theArgs) const
60 {
61   AttributePtr aAttr = theArgs.argument("OnPlane");
62   AttributeSelectionListPtr aList =
63     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aAttr);
64   if (!aList.get())
65     return false;
66   if (theShape->isVertex()) {
67     GeomVertexPtr aVertex(new GeomAPI_Vertex(theShape));
68     GeomPointPtr aPnt = aVertex->point();
69     for (int i = 0; i < aList->size(); i++) {
70       GeomPlanePtr aPlane = getPlane(aList->value(i));
71       if (aPlane->distance(aPnt) < LIN_TOLERANCE)
72         return true;
73     }
74   }
75   else if (theShape->isEdge()) {
76     GeomEdgePtr aEdge(new GeomAPI_Edge(theShape));
77     if (aEdge->isLine()) {
78       GeomPointPtr aPnt1 = aEdge->firstPoint();
79       GeomPointPtr aPnt2 = aEdge->lastPoint();
80       for (int i = 0; i < aList->size(); i++) {
81         GeomPlanePtr aPlane = getPlane(aList->value(i));
82         if ((aPlane->distance(aPnt1) < LIN_TOLERANCE) && (aPlane->distance(aPnt2) < LIN_TOLERANCE))
83           return true;
84       }
85     }
86     else if (aEdge->isPlanar()) {
87       for (int i = 0; i < aList->size(); i++) {
88         GeomPlanePtr aPlane = getPlane(aList->value(i));
89         if (aEdge->isInPlane(aPlane))
90           return true;
91       }
92     }
93   }
94   else if (theShape->isFace()) {
95     GeomFacePtr aFace(new GeomAPI_Face(theShape));
96     if (aFace->isPlanar()) {
97       GeomPlanePtr aPln = aFace->getPlane();
98       for (int i = 0; i < aList->size(); i++) {
99         AttributeSelectionPtr aAttr = aList->value(i);
100         GeomShapePtr aGeom = aAttr->value();
101         GeomPlanePtr aPlane = getPlane(aAttr);
102         if (aPlane->isCoincident(aPln))
103           return true;
104       }
105     }
106   }
107   return false;
108 }
109
110 std::string FiltersPlugin_OnPlane::xmlRepresentation() const
111 {
112   return xmlFromFile("filter-OnPlane.xml");
113 }
114
115 void FiltersPlugin_OnPlane::initAttributes(ModelAPI_FiltersArgs& theArguments)
116 {
117   theArguments.initAttribute("OnPlane", ModelAPI_AttributeSelectionList::typeId());
118 }