Salome HOME
31fd4c048b0f9587e26b76c1e86e0cf63a1bd3d8
[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 <ModelAPI_AttributeRefAttr.h>
15 #include <ModelAPI_AttributeSelection.h>
16 #include <ModelAPI_AttributeReference.h>
17
18 #include <list>
19
20 int shapesNbPoints(const ModuleBase_ISelection* theSelection)
21 {
22   QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected();  
23   int aCount = 0;
24   foreach (ModuleBase_ViewerPrs aPrs, aList) {
25     const TopoDS_Shape& aShape = aPrs.shape();
26     if (!aShape.IsNull()) {
27       if (aShape.ShapeType() == TopAbs_VERTEX)
28         aCount++;
29     }
30   }
31   return aCount;
32 }
33
34 int shapesNbLines(const ModuleBase_ISelection* theSelection)
35 {
36   QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected();
37   int aCount = 0;
38   foreach(ModuleBase_ViewerPrs aPrs, aList) {
39     const TopoDS_Shape& aShape = aPrs.shape();
40     if (!aShape.IsNull()) {
41       if (aShape.ShapeType() == TopAbs_EDGE) {
42         TopoDS_Edge aEdge = TopoDS::Edge(aShape);
43         Standard_Real aStart, aEnd;
44         Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aStart, aEnd);
45         GeomAdaptor_Curve aAdaptor(aCurve);
46         if (aAdaptor.GetType() == GeomAbs_Line)
47           aCount++;
48       }
49     }
50   }
51   return aCount;
52 }
53
54 bool PartSet_DistanceValidator::isValid(const ModuleBase_ISelection* theSelection) const
55 {
56   int aCount = shapesNbPoints(theSelection);
57   return (aCount > 0) && (aCount < 3);
58 }
59
60 bool PartSet_LengthValidator::isValid(const ModuleBase_ISelection* theSelection) const
61 {
62   int aCount = shapesNbLines(theSelection);
63   return (aCount > 0) && (aCount < 2);
64 }
65
66 bool PartSet_PerpendicularValidator::isValid(const ModuleBase_ISelection* theSelection) const
67 {
68   int aCount = shapesNbLines(theSelection);
69   return (aCount > 0) && (aCount < 3);
70 }
71
72 bool PartSet_ParallelValidator::isValid(const ModuleBase_ISelection* theSelection) const
73 {
74   int aCount = shapesNbLines(theSelection);
75   return (aCount > 0) && (aCount < 3);
76 }
77
78 bool PartSet_RadiusValidator::isValid(const ModuleBase_ISelection* theSelection) const
79 {
80   QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected();
81   ModuleBase_ViewerPrs aPrs;
82   int aCount = 0;
83   foreach (ModuleBase_ViewerPrs aPrs, aList) {
84     const TopoDS_Shape& aShape = aPrs.shape();
85     if (!aShape.IsNull()) {
86       if (aShape.ShapeType() == TopAbs_EDGE) {
87         TopoDS_Edge aEdge = TopoDS::Edge(aShape);
88         Standard_Real aStart, aEnd;
89         Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aStart, aEnd);
90         GeomAdaptor_Curve aAdaptor(aCurve);
91         if (aAdaptor.GetType() == GeomAbs_Circle)
92           aCount++;
93       }
94     }
95   }
96   return (aCount > 0) && (aCount < 2);
97 }
98
99
100
101 bool PartSet_DifferentObjectsValidator::isValid(const FeaturePtr& theFeature, 
102                                                 const std::list<std::string>& theArguments,
103                                                 const ObjectPtr& theObject) const
104 {
105   // Check RefAttr attributes
106   std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs = 
107     theFeature->data()->attributes(ModelAPI_AttributeRefAttr::type());
108   if (anAttrs.size() > 0) {
109     std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
110     for(; anAttr != anAttrs.end(); anAttr++) {
111       if (*anAttr) {
112         std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = 
113           std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
114         // check the object is already presented
115         if (aRef->isObject() && aRef->object() == theObject)
116           return false;
117       }
118     }
119   }
120   // Check selection attributes
121   anAttrs = theFeature->data()->attributes(ModelAPI_AttributeSelection::type());
122   if (anAttrs.size() > 0) {
123     std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
124     for(; anAttr != anAttrs.end(); anAttr++) {
125       if (*anAttr) {
126         std::shared_ptr<ModelAPI_AttributeSelection> aRef = 
127           std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*anAttr);
128         // check the object is already presented
129         if (aRef->isInitialized() && aRef->context() == theObject)
130           return false;
131       }
132     }
133   }
134   // Check selection attributes
135   anAttrs = theFeature->data()->attributes(ModelAPI_AttributeReference::type());
136   if (anAttrs.size() > 0) {
137     std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
138     for(; anAttr != anAttrs.end(); anAttr++) {
139       if (*anAttr) {
140         std::shared_ptr<ModelAPI_AttributeReference> aRef = 
141           std::dynamic_pointer_cast<ModelAPI_AttributeReference>(*anAttr);
142         // check the object is already presented
143         if (aRef->isInitialized() && aRef->value() == theObject)
144           return false;
145       }
146     }
147   }
148   return true;
149 }
150
151 bool PartSet_DifferentObjectsValidator::isValid(const FeaturePtr& theFeature, 
152                                                 const std::list<std::string>& theArguments,
153                                                 const AttributePtr& theAttribute) const
154 {
155   // not implemented
156   return true;
157 }
158
159 bool PartSet_DifferentObjectsValidator::isValid(const AttributePtr& theAttribute, 
160                                                 const std::list<std::string>& theArguments) const
161 {
162   // not implemented
163   return true;
164 }