Salome HOME
Validators correction for merge branch 'Dev_1.1.0' of newgeom:newgeom.git into Dev_1.1.0
[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 <GeomValidators_Tools.h>
15 #include <ModuleBase_ISelection.h>
16 #include <ModuleBase_WidgetShapeSelector.h>
17
18 #include <ModelAPI_AttributeRefAttr.h>
19 #include <ModelAPI_AttributeSelection.h>
20 #include <ModelAPI_AttributeReference.h>
21 #include <ModelAPI_AttributeSelectionList.h>
22 #include <ModelAPI_Object.h>
23
24 #include <SketchPlugin_Sketch.h>
25
26 #include <list>
27 #ifdef _DEBUG
28 #include <iostream>
29 #endif
30
31 int shapesNbPoints(const ModuleBase_ISelection* theSelection)
32 {
33   QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected();  
34   int aCount = 0;
35   foreach (ModuleBase_ViewerPrs aPrs, aList) {
36     const TopoDS_Shape& aShape = aPrs.shape();
37     if (!aShape.IsNull()) {
38       if (aShape.ShapeType() == TopAbs_VERTEX)
39         aCount++;
40     }
41   }
42   return aCount;
43 }
44
45 int shapesNbLines(const ModuleBase_ISelection* theSelection)
46 {
47   QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected();
48   int aCount = 0;
49   foreach(ModuleBase_ViewerPrs aPrs, aList) {
50     const TopoDS_Shape& aShape = aPrs.shape();
51     if (!aShape.IsNull()) {
52       if (aShape.ShapeType() == TopAbs_EDGE) {
53         TopoDS_Edge aEdge = TopoDS::Edge(aShape);
54         Standard_Real aStart, aEnd;
55         Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aStart, aEnd);
56         GeomAdaptor_Curve aAdaptor(aCurve);
57         if (aAdaptor.GetType() == GeomAbs_Line)
58           aCount++;
59       }
60     }
61   }
62   return aCount;
63 }
64
65 bool PartSet_DistanceValidator::isValid(const ModuleBase_ISelection* theSelection) const
66 {
67   int aCount = shapesNbPoints(theSelection) + shapesNbLines(theSelection);
68   return (aCount > 0) && (aCount < 3);
69 }
70
71 bool PartSet_LengthValidator::isValid(const ModuleBase_ISelection* theSelection) const
72 {
73   int aCount = shapesNbLines(theSelection);
74   return (aCount > 0) && (aCount < 2);
75 }
76
77 bool PartSet_PerpendicularValidator::isValid(const ModuleBase_ISelection* theSelection) const
78 {
79   int aCount = shapesNbLines(theSelection);
80   return (aCount > 0) && (aCount < 3);
81 }
82
83 bool PartSet_ParallelValidator::isValid(const ModuleBase_ISelection* theSelection) const
84 {
85   int aCount = shapesNbLines(theSelection);
86   return (aCount > 0) && (aCount < 3);
87 }
88
89 bool PartSet_RadiusValidator::isValid(const ModuleBase_ISelection* theSelection) const
90 {
91   QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected();
92   ModuleBase_ViewerPrs aPrs;
93   int aCount = 0;
94   foreach (ModuleBase_ViewerPrs aPrs, aList) {
95     const TopoDS_Shape& aShape = aPrs.shape();
96     if (!aShape.IsNull()) {
97       if (aShape.ShapeType() == TopAbs_EDGE) {
98         TopoDS_Edge aEdge = TopoDS::Edge(aShape);
99         Standard_Real aStart, aEnd;
100         Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aStart, aEnd);
101         GeomAdaptor_Curve aAdaptor(aCurve);
102         if (aAdaptor.GetType() == GeomAbs_Circle)
103           aCount++;
104       }
105     }
106   }
107   return (aCount > 0) && (aCount < 2);
108 }
109
110 bool PartSet_RigidValidator::isValid(const ModuleBase_ISelection* theSelection) const
111 {
112   int aCount = shapesNbLines(theSelection);
113   return (aCount > 0) && (aCount < 2);
114 }
115
116 bool PartSet_DifferentObjectsValidator::isValid(const AttributePtr& theAttribute, 
117                                                 const std::list<std::string>& theArguments) const
118 {
119   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
120
121   // the type of validated attributes should be equal, attributes with different types are not validated
122   // Check RefAttr attributes
123   std::string anAttrType = theAttribute->attributeType();
124   std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs;
125
126   if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
127     AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
128     bool isObject = anAttr->isObject();
129     ObjectPtr anObject = anAttr->object();
130     AttributePtr anAttributeAttr = anAttr->attr();
131
132     anAttrs = aFeature->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
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).get() && (*anAttr)->id() != theAttribute->id()) {
137           std::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
138                                       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
139           if (aRef->isObject() != isObject)
140             continue;
141           if (isObject) {
142             if (aRef->object() == anObject)
143               return false;
144           }
145           else { // the attribute reference
146             if (aRef->attr() == theAttribute)
147               return false;
148           }
149         }
150       }
151     }
152   }
153   else if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
154     AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
155     ResultPtr aContext = anAttr->context();
156     GeomShapePtr aShape = anAttr->value();
157
158     // Check selection attributes
159     anAttrs = aFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
160     if (anAttrs.size() > 0) {
161       std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
162       for(; anAttr != anAttrs.end(); anAttr++) {
163         if ((*anAttr).get() && (*anAttr)->id() != theAttribute->id()) {
164           std::shared_ptr<ModelAPI_AttributeSelection> aRef =
165                                         std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*anAttr);
166           // check the object is already presented
167           if (aRef->context() == aContext) {
168             bool aHasShape = aShape.get() != NULL;
169             if (!aHasShape || aRef->value()->isEqual(aShape))
170               return false;
171           }
172         }
173       }
174     }
175   }
176   else if (anAttrType == ModelAPI_AttributeReference::typeId()) {
177     AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
178     ObjectPtr anObject = anAttr->value();
179     // Check selection attributes
180     anAttrs = aFeature->data()->attributes(ModelAPI_AttributeReference::typeId());
181     if (anAttrs.size() > 0) {
182       std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
183       for(; anAttr != anAttrs.end(); anAttr++) {
184         if ((*anAttr).get() && (*anAttr)->id() != theAttribute->id()) {
185           std::shared_ptr<ModelAPI_AttributeReference> aRef =
186             std::dynamic_pointer_cast<ModelAPI_AttributeReference>(*anAttr);
187           // check the object is already presented
188           if (aRef->value() == anObject)
189             return false;
190         }
191         return true;
192       }
193     }
194   }
195   return !featureHasReferences(theAttribute);
196 }
197
198 bool PartSet_DifferentObjectsValidator::featureHasReferences(const AttributePtr& theAttribute) const
199 {
200   std::list<std::pair<std::string, std::list<ObjectPtr> > > allRefs;
201   if (theAttribute->owner().get() && theAttribute->owner()->data().get())
202     theAttribute->owner()->data()->referencesToObjects(allRefs);
203   // collect object referenced by theAttribute
204   std::list<ObjectPtr>* anAttrObjs = 0;
205   std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRefIter = allRefs.begin();
206   for(; aRefIter != allRefs.end(); aRefIter++) {
207     if (theAttribute->id() == aRefIter->first)
208       anAttrObjs = &(aRefIter->second);
209   }
210   if (!anAttrObjs || anAttrObjs->empty())
211     return false; // theAttribute does not references to anything
212   // check with all others
213   for(aRefIter = allRefs.begin(); aRefIter != allRefs.end(); aRefIter++) {
214     if (theAttribute->id() == aRefIter->first)
215       continue; // do not check with myself
216     std::list<ObjectPtr>::iterator aReferenced = aRefIter->second.begin();
217     for(; aReferenced != aRefIter->second.end(); aReferenced++) {
218       std::list<ObjectPtr>::iterator aReferencedByMe = anAttrObjs->begin();
219       for(; aReferencedByMe != anAttrObjs->end(); aReferencedByMe++) {
220         if (*aReferenced == *aReferencedByMe) // found same objects!
221           return true;
222       }
223     }
224   }
225   return false;
226 }
227
228 bool PartSet_SketchEntityValidator::isValid(const AttributePtr& theAttribute,
229                                             const std::list<std::string>& theArguments) const
230 {
231   bool isSketchEntities = true;
232   std::set<std::string> anEntityKinds;
233   std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
234   for (; anIt != aLast; anIt++) {
235     anEntityKinds.insert(*anIt);
236   }
237
238   std::string anAttributeType = theAttribute->attributeType();
239   if (anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
240     AttributeSelectionListPtr aSelectionListAttr = 
241                       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
242     // it filters only selection list attributes
243     std::string aType = aSelectionListAttr->selectionType().c_str();
244     // all context objects should be sketch entities
245     int aSize = aSelectionListAttr->size();
246     for (int i = 0; i < aSelectionListAttr->size() && isSketchEntities; i++) {
247       AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i);
248       ObjectPtr anObject = aSelectAttr->context();
249       FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
250       isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
251     }
252   }
253   if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
254     std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = 
255                      std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
256     isSketchEntities = false;
257     if (aRef->isObject()) {
258       ObjectPtr anObject = aRef->object();
259       if (anObject.get() != NULL) {
260         FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
261         if (aFeature.get() != NULL)
262           isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
263       }
264     }
265   }
266
267   return isSketchEntities;
268 }