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