Salome HOME
Implement filter "F3: On a Line"
[modules/shaper.git] / src / FiltersPlugin / FiltersPlugin_OnLine.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_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     GeomShapePtr aCurShape = theSelected->value(i)->value();
35     if (aCurShape && aCurShape->isEdge()) {
36       GeomLinePtr aLine = aCurShape->edge()->line();
37       if (aLine)
38         theLines.push_back(aLine);
39     }
40   }
41 }
42
43
44 bool FiltersPlugin_OnLine::isSupported(GeomAPI_Shape::ShapeType theType) const
45 {
46   return theType == GeomAPI_Shape::EDGE || theType == GeomAPI_Shape::VERTEX;
47 }
48
49 bool FiltersPlugin_OnLine::isOk(const GeomShapePtr& theShape,
50                                 const ModelAPI_FiltersArgs& theArgs) const
51 {
52   AttributePtr aAttr = theArgs.argument("OnLine");
53   AttributeSelectionListPtr aList =
54     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aAttr);
55   if (!aList.get())
56     return false;
57   // convert selected shapes to lines
58   std::list<GeomLinePtr> aLines;
59   convertToLines(aList, aLines);
60
61   if (theShape->isVertex()) {
62     GeomPointPtr aPnt = theShape->vertex()->point();
63     for (std::list<GeomLinePtr>::iterator anIt = aLines.begin(); anIt != aLines.end(); ++anIt) {
64       if ((*anIt)->contains(aPnt))
65         return true;
66     }
67   }
68   else if (theShape->isEdge()) {
69     GeomEdgePtr aEdge(new GeomAPI_Edge(theShape));
70     if (aEdge->isLine()) {
71       GeomPointPtr aPnt1 = aEdge->firstPoint();
72       GeomPointPtr aPnt2 = aEdge->lastPoint();
73       for (std::list<GeomLinePtr>::iterator anIt = aLines.begin(); anIt != aLines.end(); ++anIt) {
74         if ((*anIt)->contains(aPnt1) && (*anIt)->contains(aPnt2))
75           return true;
76       }
77     }
78   }
79   return false;
80 }
81
82 static std::string XMLRepresentation =
83 "<filter id = \"OnLine\">"
84 " <multi_selector id=\"OnLine__OnLine\""
85 "   label=\"Lines:\""
86 "   tooltip=\"Select vertices or segments.\""
87 "   type_choice=\"edges\">"
88 "   <validator id=\"GeomValidators_ShapeType\" parameters=\"line\"/>"
89 " </multi_selector>"
90 "</filter>";
91
92
93 std::string FiltersPlugin_OnLine::xmlRepresentation() const
94 {
95   return XMLRepresentation;
96 }
97
98 void FiltersPlugin_OnLine::initAttributes(ModelAPI_FiltersArgs& theArguments)
99 {
100   theArguments.initAttribute("OnLine", ModelAPI_AttributeSelectionList::typeId());
101 }