Salome HOME
7a3d43fe7f17274dfd84289076a41a4f10802552
[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
16 int shapesNbPoints(const ModuleBase_ISelection* theSelection)
17 {
18   std::list<ModuleBase_ViewerPrs> aList = theSelection->getSelected();
19   std::list<ModuleBase_ViewerPrs>::iterator it;
20   ModuleBase_ViewerPrs aPrs;
21   int aCount = 0;
22   for (it = aList.begin(); it != aList.end(); ++it) {
23     aPrs = *it;
24     const TopoDS_Shape& aShape = aPrs.shape();
25     if (!aShape.IsNull()) {
26       if (aShape.ShapeType() == TopAbs_VERTEX)
27         aCount++;
28     }
29   }
30   return aCount;
31 }
32
33 int shapesNbLines(const ModuleBase_ISelection* theSelection)
34 {
35   std::list<ModuleBase_ViewerPrs> aList = theSelection->getSelected();
36   std::list<ModuleBase_ViewerPrs>::iterator it;
37   ModuleBase_ViewerPrs aPrs;
38   int aCount = 0;
39   for (it = aList.begin(); it != aList.end(); ++it) {
40     aPrs = *it;
41     const TopoDS_Shape& aShape = aPrs.shape();
42     if (!aShape.IsNull()) {
43       if (aShape.ShapeType() == TopAbs_EDGE) {
44         TopoDS_Edge aEdge = TopoDS::Edge(aShape);
45         Standard_Real aStart, aEnd;
46         Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aStart, aEnd);
47         GeomAdaptor_Curve aAdaptor(aCurve);
48         if (aAdaptor.GetType() == GeomAbs_Line)
49           aCount++;
50       }
51     }
52   }
53   return aCount;
54 }
55
56 bool PartSet_DistanceValidator::isValid(const ModuleBase_ISelection* theSelection) const
57 {
58   int aCount = shapesNbPoints(theSelection);
59   return (aCount > 0) && (aCount < 3);
60 }
61
62 bool PartSet_LengthValidator::isValid(const ModuleBase_ISelection* theSelection) const
63 {
64   int aCount = shapesNbLines(theSelection);
65   return (aCount > 0) && (aCount < 2);
66 }
67
68 bool PartSet_PerpendicularValidator::isValid(const ModuleBase_ISelection* theSelection) const
69 {
70   int aCount = shapesNbLines(theSelection);
71   return (aCount > 0) && (aCount < 3);
72 }
73
74 bool PartSet_ParallelValidator::isValid(const ModuleBase_ISelection* theSelection) const
75 {
76   int aCount = shapesNbLines(theSelection);
77   return (aCount > 0) && (aCount < 3);
78 }
79
80 bool PartSet_RadiusValidator::isValid(const ModuleBase_ISelection* theSelection) const
81 {
82   std::list<ModuleBase_ViewerPrs> aList = theSelection->getSelected();
83   std::list<ModuleBase_ViewerPrs>::iterator it;
84   ModuleBase_ViewerPrs aPrs;
85   int aCount = 0;
86   for (it = aList.begin(); it != aList.end(); ++it) {
87     aPrs = *it;
88     const TopoDS_Shape& aShape = aPrs.shape();
89     if (!aShape.IsNull()) {
90       if (aShape.ShapeType() == TopAbs_EDGE) {
91         TopoDS_Edge aEdge = TopoDS::Edge(aShape);
92         Standard_Real aStart, aEnd;
93         Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aStart, aEnd);
94         GeomAdaptor_Curve aAdaptor(aCurve);
95         if (aAdaptor.GetType() == GeomAbs_Circle)
96           aCount++;
97       }
98     }
99   }
100   return (aCount > 0) && (aCount < 2);
101 }
102