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