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