Salome HOME
33e9a4c2e0905a9325035f48e8bb5f70ab1486d8
[modules/shaper.git] / src / FiltersPlugin / FiltersPlugin_OnLine.cpp
1 // Copyright (C) 2014-2021  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_OnLine.h"
21
22 #include <ModelAPI_AttributeSelectionList.h>
23
24 #include <GeomAPI_Edge.h>
25 #include <GeomAPI_Lin.h>
26 #include <GeomAPI_Pnt.h>
27 #include <GeomAPI_Vertex.h>
28
29
30 static void convertToLines(const AttributeSelectionListPtr& theSelected,
31                            std::list<GeomLinePtr>& theLines)
32 {
33   for (int i = 0; i < theSelected->size(); i++) {
34     AttributeSelectionPtr aSel = theSelected->value(i);
35     GeomShapePtr aCurShape = aSel->value();
36     if (!aCurShape)
37       aCurShape = aSel->context()->shape();
38     if (aCurShape && aCurShape->isEdge()) {
39       GeomLinePtr aLine = aCurShape->edge()->line();
40       if (aLine)
41         theLines.push_back(aLine);
42     }
43   }
44 }
45
46
47 bool FiltersPlugin_OnLine::isSupported(GeomAPI_Shape::ShapeType theType) const
48 {
49   return theType == GeomAPI_Shape::EDGE || theType == GeomAPI_Shape::VERTEX;
50 }
51
52 bool FiltersPlugin_OnLine::isOk(const GeomShapePtr& theShape, const ResultPtr&,
53                                 const ModelAPI_FiltersArgs& theArgs) const
54 {
55   AttributePtr aAttr = theArgs.argument("OnLine");
56   AttributeSelectionListPtr aList =
57     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aAttr);
58   if (!aList.get())
59     return false;
60   // convert selected shapes to lines
61   std::list<GeomLinePtr> aLines;
62   convertToLines(aList, aLines);
63
64   if (theShape->isVertex()) {
65     GeomPointPtr aPnt = theShape->vertex()->point();
66     for (std::list<GeomLinePtr>::iterator anIt = aLines.begin(); anIt != aLines.end(); ++anIt) {
67       if ((*anIt)->contains(aPnt))
68         return true;
69     }
70   }
71   else if (theShape->isEdge()) {
72     GeomEdgePtr aEdge(new GeomAPI_Edge(theShape));
73     if (aEdge->isLine()) {
74       GeomPointPtr aPnt1 = aEdge->firstPoint();
75       GeomPointPtr aPnt2 = aEdge->lastPoint();
76       for (std::list<GeomLinePtr>::iterator anIt = aLines.begin(); anIt != aLines.end(); ++anIt) {
77         if ((*anIt)->contains(aPnt1) && (*anIt)->contains(aPnt2))
78           return true;
79       }
80     }
81   }
82   return false;
83 }
84
85 std::string FiltersPlugin_OnLine::xmlRepresentation() const
86 {
87   return xmlFromFile("filter-OnLine.xml");
88 }
89
90 void FiltersPlugin_OnLine::initAttributes(ModelAPI_FiltersArgs& theArguments)
91 {
92   theArguments.initAttribute("OnLine", ModelAPI_AttributeSelectionList::typeId());
93 }