Salome HOME
Sources formated according to the codeing standards
[modules/shaper.git] / src / PartSet / PartSet_Validators.cpp
1 // File:        PartSet_Validators.cpp
2 // Created:     09 July 2014
3 // Author:      Vitaly SMETANNIKOV
4
5 #include "PartSet_Validators.h"
6
7 #include <TopoDS.hxx>
8 #include <TopoDS_Edge.hxx>
9 #include <BRep_Tool.hxx>
10 #include <GeomAdaptor_Curve.hxx>
11 #include <GeomAbs_CurveType.hxx>
12
13 #include <list>
14
15 int shapesNbPoints(const ModuleBase_ISelection* theSelection)
16 {
17   std::list<ModuleBase_ViewerPrs> aList = theSelection->getSelected();
18   std::list<ModuleBase_ViewerPrs>::iterator it;
19   ModuleBase_ViewerPrs aPrs;
20   int aCount = 0;
21   for (it = aList.begin(); it != aList.end(); ++it) {
22     aPrs = *it;
23     const TopoDS_Shape& aShape = aPrs.shape();
24     if (!aShape.IsNull()) {
25       if (aShape.ShapeType() == TopAbs_VERTEX)
26         aCount++;
27     }
28   }
29   return aCount;
30 }
31
32 int shapesNbLines(const ModuleBase_ISelection* theSelection)
33 {
34   std::list<ModuleBase_ViewerPrs> aList = theSelection->getSelected();
35   std::list<ModuleBase_ViewerPrs>::iterator it;
36   ModuleBase_ViewerPrs aPrs;
37   int aCount = 0;
38   for (it = aList.begin(); it != aList.end(); ++it) {
39     aPrs = *it;
40     const TopoDS_Shape& aShape = aPrs.shape();
41     if (!aShape.IsNull()) {
42       if (aShape.ShapeType() == TopAbs_EDGE) {
43         TopoDS_Edge aEdge = TopoDS::Edge(aShape);
44         Standard_Real aStart, aEnd;
45         Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aStart, aEnd);
46         GeomAdaptor_Curve aAdaptor(aCurve);
47         if (aAdaptor.GetType() == GeomAbs_Line)
48           aCount++;
49       }
50     }
51   }
52   return aCount;
53 }
54
55 bool PartSet_DistanceValidator::isValid(const ModuleBase_ISelection* theSelection) const
56 {
57   int aCount = shapesNbPoints(theSelection);
58   return (aCount > 0) && (aCount < 3);
59 }
60
61 bool PartSet_LengthValidator::isValid(const ModuleBase_ISelection* theSelection) const
62 {
63   int aCount = shapesNbLines(theSelection);
64   return (aCount > 0) && (aCount < 2);
65 }
66
67 bool PartSet_PerpendicularValidator::isValid(const ModuleBase_ISelection* theSelection) const
68 {
69   int aCount = shapesNbLines(theSelection);
70   return (aCount > 0) && (aCount < 3);
71 }
72
73 bool PartSet_ParallelValidator::isValid(const ModuleBase_ISelection* theSelection) const
74 {
75   int aCount = shapesNbLines(theSelection);
76   return (aCount > 0) && (aCount < 3);
77 }
78
79 bool PartSet_RadiusValidator::isValid(const ModuleBase_ISelection* theSelection) const
80 {
81   std::list<ModuleBase_ViewerPrs> aList = theSelection->getSelected();
82   std::list<ModuleBase_ViewerPrs>::iterator it;
83   ModuleBase_ViewerPrs aPrs;
84   int aCount = 0;
85   for (it = aList.begin(); it != aList.end(); ++it) {
86     aPrs = *it;
87     const TopoDS_Shape& aShape = aPrs.shape();
88     if (!aShape.IsNull()) {
89       if (aShape.ShapeType() == TopAbs_EDGE) {
90         TopoDS_Edge aEdge = TopoDS::Edge(aShape);
91         Standard_Real aStart, aEnd;
92         Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aStart, aEnd);
93         GeomAdaptor_Curve aAdaptor(aCurve);
94         if (aAdaptor.GetType() == GeomAbs_Circle)
95           aCount++;
96       }
97     }
98   }
99   return (aCount > 0) && (aCount < 2);
100 }
101