Salome HOME
updated copyright message
[modules/shaper.git] / src / FiltersPlugin / FiltersPlugin_OnPlane.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_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   default: // [to avoid compilation warning]
45     break;
46   }
47   return false;
48 }
49
50 GeomPlanePtr getPlane(AttributeSelectionPtr theAttr)
51 {
52   GeomShapePtr aGeom = theAttr->value();
53   if (!aGeom.get()) {  // construction plane, #2942
54     aGeom = theAttr->context()->shape();
55   }
56   GeomFacePtr aFace(new GeomAPI_Face(aGeom));
57   return aFace->getPlane();
58 }
59
60 bool FiltersPlugin_OnPlane::isOk(const GeomShapePtr& theShape, const ResultPtr&,
61                                  const ModelAPI_FiltersArgs& theArgs) const
62 {
63   AttributePtr aAttr = theArgs.argument("OnPlane");
64   AttributeSelectionListPtr aList =
65     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aAttr);
66   if (!aList.get())
67     return false;
68   if (theShape->isVertex()) {
69     GeomVertexPtr aVertex(new GeomAPI_Vertex(theShape));
70     GeomPointPtr aPnt = aVertex->point();
71     for (int i = 0; i < aList->size(); i++) {
72       GeomPlanePtr aPlane = getPlane(aList->value(i));
73       if (aPlane->distance(aPnt) < LIN_TOLERANCE)
74         return true;
75     }
76   }
77   else if (theShape->isEdge()) {
78     GeomEdgePtr aEdge(new GeomAPI_Edge(theShape));
79     if (aEdge->isLine()) {
80       GeomPointPtr aPnt1 = aEdge->firstPoint();
81       GeomPointPtr aPnt2 = aEdge->lastPoint();
82       for (int i = 0; i < aList->size(); i++) {
83         GeomPlanePtr aPlane = getPlane(aList->value(i));
84         if ((aPlane->distance(aPnt1) < LIN_TOLERANCE) && (aPlane->distance(aPnt2) < LIN_TOLERANCE))
85           return true;
86       }
87     }
88     else if (aEdge->isPlanar()) {
89       for (int i = 0; i < aList->size(); i++) {
90         GeomPlanePtr aPlane = getPlane(aList->value(i));
91         if (aEdge->isInPlane(aPlane))
92           return true;
93       }
94     }
95   }
96   else if (theShape->isFace()) {
97     GeomFacePtr aFace(new GeomAPI_Face(theShape));
98     if (aFace->isPlanar()) {
99       GeomPlanePtr aPln = aFace->getPlane();
100       for (int i = 0; i < aList->size(); i++) {
101         AttributeSelectionPtr aCurAttr = aList->value(i);
102         GeomPlanePtr aPlane = getPlane(aCurAttr);
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 }