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