]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_Validators.cpp
Salome HOME
86f6cb97ee6c6194fcc5a3798194f9e4e0a0de0a
[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 "PartSet_Tools.h"
10
11 #include <TopoDS.hxx>
12 #include <TopoDS_Edge.hxx>
13 #include <BRep_Tool.hxx>
14 #include <GeomAdaptor_Curve.hxx>
15 #include <GeomAbs_CurveType.hxx>
16 #include <GeomValidators_Tools.h>
17 #include <ModuleBase_ISelection.h>
18 #include <ModuleBase_WidgetShapeSelector.h>
19
20 #include <ModelAPI_AttributeRefAttr.h>
21 #include <ModelAPI_AttributeSelection.h>
22 #include <ModelAPI_AttributeReference.h>
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_AttributeRefList.h>
25 #include <ModelAPI_Object.h>
26 #include <ModelAPI_Session.h>
27
28 #include <SketchPlugin_Sketch.h>
29 #include <SketchPlugin_ConstraintCoincidence.h>
30 #include <SketchPlugin_Arc.h>
31 #include <GeomAPI_Edge.h>
32
33 #include <list>
34 #ifdef _DEBUG
35 #include <iostream>
36 #endif
37
38 int shapesNbPoints(const ModuleBase_ISelection* theSelection)
39 {
40   QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected(ModuleBase_ISelection::Viewer);
41   int aCount = 0;
42   foreach (ModuleBase_ViewerPrs aPrs, aList) {
43     const TopoDS_Shape& aShape = aPrs.shape();
44     if (!aShape.IsNull()) {
45       if (aShape.ShapeType() == TopAbs_VERTEX)
46         aCount++;
47     }
48   }
49   return aCount;
50 }
51
52 int shapesNbLines(const ModuleBase_ISelection* theSelection)
53 {
54   QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected(ModuleBase_ISelection::Viewer);
55   int aCount = 0;
56   foreach(ModuleBase_ViewerPrs aPrs, aList) {
57     const TopoDS_Shape& aShape = aPrs.shape();
58     if (!aShape.IsNull()) {
59       if (aShape.ShapeType() == TopAbs_EDGE) {
60         TopoDS_Edge aEdge = TopoDS::Edge(aShape);
61         Standard_Real aStart, aEnd;
62         Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aStart, aEnd);
63         GeomAdaptor_Curve aAdaptor(aCurve);
64         if (aAdaptor.GetType() == GeomAbs_Line)
65           aCount++;
66       }
67     }
68   }
69   return aCount;
70 }
71
72 bool PartSet_DistanceSelection::isValid(const ModuleBase_ISelection* theSelection) const
73 {
74   int aCount = shapesNbPoints(theSelection) + shapesNbLines(theSelection);
75   return (aCount > 0) && (aCount < 3);
76 }
77
78 bool PartSet_LengthSelection::isValid(const ModuleBase_ISelection* theSelection) const
79 {
80   int aCount = shapesNbLines(theSelection);
81   return (aCount == 1);
82 }
83
84 bool PartSet_PerpendicularSelection::isValid(const ModuleBase_ISelection* theSelection) const
85 {
86   int aCount = shapesNbLines(theSelection);
87   return (aCount > 0) && (aCount < 3);
88 }
89
90 bool PartSet_ParallelSelection::isValid(const ModuleBase_ISelection* theSelection) const
91 {
92   int aCount = shapesNbLines(theSelection);
93   return (aCount > 0) && (aCount < 3);
94 }
95
96 bool PartSet_RadiusSelection::isValid(const ModuleBase_ISelection* theSelection) const
97 {
98   QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected(ModuleBase_ISelection::Viewer);
99   ModuleBase_ViewerPrs aPrs;
100   int aCount = 0;
101   foreach (ModuleBase_ViewerPrs aPrs, aList) {
102     const TopoDS_Shape& aShape = aPrs.shape();
103     if (!aShape.IsNull()) {
104       if (aShape.ShapeType() == TopAbs_EDGE) {
105         TopoDS_Edge aEdge = TopoDS::Edge(aShape);
106         Standard_Real aStart, aEnd;
107         Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aStart, aEnd);
108         GeomAdaptor_Curve aAdaptor(aCurve);
109         if (aAdaptor.GetType() == GeomAbs_Circle)
110           aCount++;
111       }
112     }
113   }
114   return (aCount == 1);
115 }
116
117 bool PartSet_RigidSelection::isValid(const ModuleBase_ISelection* theSelection) const
118 {
119   QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected(ModuleBase_ISelection::Viewer);
120   return (aList.count() == 1);
121 }
122
123
124 bool PartSet_CoincidentSelection::isValid(const ModuleBase_ISelection* theSelection) const
125 {
126   int aCount = shapesNbPoints(theSelection);
127   return (aCount > 0) && (aCount < 3);
128 }
129
130 bool PartSet_HVDirSelection::isValid(const ModuleBase_ISelection* theSelection) const
131 {
132   int aCount = shapesNbLines(theSelection);
133   return (aCount == 1);
134 }
135
136 bool PartSet_FilletSelection::isValid(const ModuleBase_ISelection* theSelection) const
137 {
138   int aCount = shapesNbLines(theSelection);
139   return (aCount > 0) && (aCount < 3);
140 }
141
142 bool PartSet_TangentSelection::isValid(const ModuleBase_ISelection* theSelection) const
143 {
144   QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected(ModuleBase_ISelection::Viewer);
145   if ((aList.size() == 0) || (aList.size() > 2))
146     return false;
147
148   ModuleBase_ViewerPrs aPrs = aList.first();
149   const TopoDS_Shape& aShape = aPrs.shape();
150   if (aShape.IsNull())
151     return false;
152
153   if (aShape.ShapeType() != TopAbs_EDGE)
154     return false;
155
156   std::shared_ptr<GeomAPI_Shape> aShapePtr(new GeomAPI_Shape);
157   aShapePtr->setImpl(new TopoDS_Shape(aShape));
158   GeomAPI_Edge aEdge1(aShapePtr);
159
160   if (aEdge1.isLine() || aEdge1.isArc()) {
161     if (aList.size() == 2) {
162       // Check second selection
163       aPrs = aList.last();
164       const TopoDS_Shape& aShape2 = aPrs.shape();
165       if (aShape2.IsNull())
166         return false;
167
168       if (aShape2.ShapeType() != TopAbs_EDGE)
169         return false;
170
171       std::shared_ptr<GeomAPI_Shape> aShapePtr2(new GeomAPI_Shape);
172       aShapePtr2->setImpl(new TopoDS_Shape(aShape2));
173       GeomAPI_Edge aEdge2(aShapePtr2);
174       if (aEdge1.isLine() && aEdge2.isArc())
175         return true;
176       else if (aEdge1.isArc() && aEdge2.isLine())
177         return true;
178       else
179         return false;
180     } else
181       return true;
182   }
183   return false;
184 }
185
186 bool PartSet_AngleSelection::isValid(const ModuleBase_ISelection* theSelection) const
187 {
188   int aCount = shapesNbLines(theSelection);
189   return (aCount > 0) && (aCount < 3);
190 }
191
192
193 bool PartSet_DifferentObjectsValidator::isValid(const AttributePtr& theAttribute, 
194                                                 const std::list<std::string>& theArguments,
195                                                 std::string& theError) const
196 {
197   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
198
199   // the type of validated attributes should be equal, attributes with different types are not validated
200   // Check RefAttr attributes
201   std::string anAttrType = theAttribute->attributeType();
202   std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs;
203
204   if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
205     AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
206     bool isObject = anAttr->isObject();
207     ObjectPtr anObject = anAttr->object();
208     AttributePtr anAttributeAttr = anAttr->attr();
209
210     anAttrs = aFeature->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
211     if (anAttrs.size() > 0) {
212       std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
213       for(; anAttr != anAttrs.end(); anAttr++) {
214       if ((*anAttr).get() && (*anAttr)->id() != theAttribute->id()) {
215           std::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
216                                       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
217           if (aRef->isObject() != isObject)
218             continue;
219           if (isObject) {
220             if (aRef->object() == anObject)
221               return false;
222           }
223           else { // the attribute reference
224             if (aRef->attr() == theAttribute)
225               return false;
226           }
227         }
228       }
229     }
230   }
231   else if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
232     AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
233     ResultPtr aContext = anAttr->context();
234     GeomShapePtr aShape = anAttr->value();
235
236     // Check selection attributes
237     anAttrs = aFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
238     if (anAttrs.size() > 0) {
239       std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
240       for(; anAttr != anAttrs.end(); anAttr++) {
241         if ((*anAttr).get() && (*anAttr)->id() != theAttribute->id()) {
242           std::shared_ptr<ModelAPI_AttributeSelection> aRef =
243                                         std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*anAttr);
244           // check the object is already presented
245           if (aRef->context() == aContext) {
246             bool aHasShape = aShape.get() != NULL;
247             if (!aHasShape || aRef->value()->isEqual(aShape))
248               return false;
249           }
250         }
251       }
252     }
253   }
254   else if (anAttrType == ModelAPI_AttributeReference::typeId()) {
255     AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
256     ObjectPtr anObject = anAttr->value();
257     // Check selection attributes
258     anAttrs = aFeature->data()->attributes(ModelAPI_AttributeReference::typeId());
259     if (anAttrs.size() > 0) {
260       std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
261       for(; anAttr != anAttrs.end(); anAttr++) {
262         if ((*anAttr).get() && (*anAttr)->id() != theAttribute->id()) {
263           std::shared_ptr<ModelAPI_AttributeReference> aRef =
264             std::dynamic_pointer_cast<ModelAPI_AttributeReference>(*anAttr);
265           // check the object is already presented
266           if (aRef->value() == anObject)
267             return false;
268         }
269         return true;
270       }
271     }
272   }
273   else if(anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
274     std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList = 
275             std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
276     anAttrs = aFeature->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
277     if(anAttrs.size() > 0) {
278       std::list<std::shared_ptr<ModelAPI_Attribute>>::iterator anAttrItr = anAttrs.begin();
279       for(; anAttrItr != anAttrs.end(); anAttrItr++){
280         if ((*anAttrItr).get() && (*anAttrItr)->id() != theAttribute->id()){
281           std::shared_ptr<ModelAPI_AttributeSelectionList> aRefSelList = 
282             std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*anAttrItr);
283           for(int i = 0; i < aCurSelList->size(); i++) {
284             std::shared_ptr<ModelAPI_AttributeSelection> aCurSel = aCurSelList->value(i);
285             for(int j = 0; j < aRefSelList->size(); j++) {
286               std::shared_ptr<ModelAPI_AttributeSelection> aRefSel = aRefSelList->value(j);
287               if(aCurSel->context() == aRefSel->context()) {
288                 if(aCurSel->value().get() == NULL || aRefSel->value().get() == NULL
289                   || aCurSel->value()->isEqual(aRefSel->value())) {
290                     return false;
291                 }
292               }
293             }
294           }
295         }
296       }
297     }
298   }
299   else if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
300     std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
301       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
302     anAttrs = aFeature->data()->attributes(ModelAPI_AttributeRefList::typeId());
303     if (anAttrs.size() > 0) {
304       std::list<std::shared_ptr<ModelAPI_Attribute>>::iterator anAttrItr = anAttrs.begin();
305       for (; anAttrItr != anAttrs.end(); anAttrItr++){
306         if ((*anAttrItr).get() && (*anAttrItr)->id() != theAttribute->id()){
307           std::shared_ptr<ModelAPI_AttributeRefList> aRefSelList =
308             std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anAttrItr);
309           for (int i = 0; i < aCurSelList->size(); i++) {
310             ObjectPtr aCurSelObject = aCurSelList->object(i);
311             for (int j = 0; j < aRefSelList->size(); j++) {
312               if (aCurSelObject == aRefSelList->object(j)) {
313                 return false;
314               }
315             }
316           }
317         }
318       }
319     }
320   }
321   return !featureHasReferences(theAttribute);
322 }
323
324 bool PartSet_DifferentObjectsValidator::featureHasReferences(const AttributePtr& theAttribute) const
325 {
326   std::list<std::pair<std::string, std::list<ObjectPtr> > > allRefs;
327   if (theAttribute->owner().get() && theAttribute->owner()->data()->isValid())
328     theAttribute->owner()->data()->referencesToObjects(allRefs);
329   // collect object referenced by theAttribute
330   std::list<ObjectPtr>* anAttrObjs = 0;
331   std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRefIter = allRefs.begin();
332   for(; aRefIter != allRefs.end(); aRefIter++) {
333     if (theAttribute->id() == aRefIter->first)
334       anAttrObjs = &(aRefIter->second);
335   }
336   if (!anAttrObjs || anAttrObjs->empty())
337     return false; // theAttribute does not references to anything
338   // check with all others
339   for(aRefIter = allRefs.begin(); aRefIter != allRefs.end(); aRefIter++) {
340     if (theAttribute->id() == aRefIter->first)
341       continue; // do not check with myself
342     std::list<ObjectPtr>::iterator aReferenced = aRefIter->second.begin();
343     for(; aReferenced != aRefIter->second.end(); aReferenced++) {
344       std::list<ObjectPtr>::iterator aReferencedByMe = anAttrObjs->begin();
345       for(; aReferencedByMe != anAttrObjs->end(); aReferencedByMe++) {
346         if (*aReferenced == *aReferencedByMe) // found same objects!
347           return true;
348       }
349     }
350   }
351   return false;
352 }
353
354 bool PartSet_SketchEntityValidator::isValid(const AttributePtr& theAttribute,
355                                             const std::list<std::string>& theArguments,
356                                             std::string& theError) const
357 {
358   bool isSketchEntities = true;
359   std::set<std::string> anEntityKinds;
360   std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
361   for (; anIt != aLast; anIt++) {
362     anEntityKinds.insert(*anIt);
363   }
364
365   std::string anAttributeType = theAttribute->attributeType();
366   if (anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
367     AttributeSelectionListPtr aSelectionListAttr = 
368                       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
369     // all context objects should be sketch entities
370     for (int i = 0, aSize = aSelectionListAttr->size(); i < aSize && isSketchEntities; i++) {
371       AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i);
372       ObjectPtr anObject = aSelectAttr->context();
373       // a context of the selection attribute is a feature result. It can be a case when the result
374       // of the feature is null, e.g. the feature is modified and has not been executed yet.
375       // The validator returns an invalid result here. The case is an extrusion built on a sketch
376       // feature. A new sketch element creation leads to an empty result.
377       if (!anObject.get())
378         isSketchEntities = false;
379       else {
380         FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
381         isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
382       }
383     }
384   }
385   if (anAttributeType == ModelAPI_AttributeRefList::typeId()) {
386     AttributeRefListPtr aRefListAttr =
387       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
388     // all context objects should be sketch entities
389     for (int i = 0, aSize = aRefListAttr->size(); i < aSize && isSketchEntities; i++) {
390       ObjectPtr anObject = aRefListAttr->object(i);
391       // a context of the selection attribute is a feature result. It can be a case when the result
392       // of the feature is null, e.g. the feature is modified and has not been executed yet.
393       // The validator returns an invalid result here. The case is an extrusion built on a sketch
394       // feature. A new sketch element creation leads to an empty result.
395       if (!anObject.get())
396         isSketchEntities = false;
397       else {
398         FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
399         isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
400       }
401     }
402   }
403   if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
404     std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = 
405                      std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
406     isSketchEntities = false;
407     if (aRef->isObject()) {
408       ObjectPtr anObject = aRef->object();
409       if (anObject.get() != NULL) {
410         FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
411         if (aFeature.get() != NULL)
412           isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
413       }
414     }
415   }
416
417   return isSketchEntities;
418 }
419
420
421
422 bool PartSet_SameTypeAttrValidator::isValid(const AttributePtr& theAttribute, 
423                                             const std::list<std::string>& theArguments,
424                                             std::string& theError ) const
425 {
426   // there is a check whether the feature contains a point and a linear edge or two point values
427   std::string aParamA = theArguments.front();
428   SessionPtr aMgr = ModelAPI_Session::get();
429   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
430
431   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
432   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
433   if (!aRefAttr)
434     return false;
435
436   bool isObject = aRefAttr->isObject();
437   ObjectPtr anObject = aRefAttr->object();
438   if (isObject && anObject) {
439     FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
440
441     AttributeRefAttrPtr aOtherAttr = aFeature->data()->refattr(aParamA);
442     ObjectPtr aOtherObject = aOtherAttr->object();
443     FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
444     return aRefFea->getKind() == aOtherFea->getKind();
445   }
446   return false;
447 }
448
449 bool PartSet_CoincidentAttr::isValid(const AttributePtr& theAttribute, 
450                                      const std::list<std::string>& theArguments,
451                                      std::string& theError) const
452 {
453   // there is a check whether the feature contains a point and a linear edge or two point values
454   std::string aParamA = theArguments.front();
455   SessionPtr aMgr = ModelAPI_Session::get();
456   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
457
458   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
459   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
460   if (!aRefAttr)
461     return false;
462
463   QList<FeaturePtr> aCoinsideLines;
464
465   bool isObject = aRefAttr->isObject();
466   ObjectPtr anObject = aRefAttr->object();
467   if (isObject && anObject) {
468     FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
469     AttributeRefAttrPtr aOtherAttr = aFeature->data()->refattr(aParamA);
470     ObjectPtr aOtherObject = aOtherAttr->object();
471     // if the other attribute is not filled still, the result is true
472     if (!aOtherObject.get())
473       return true;
474     FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
475
476     // check that both have coincidence
477     FeaturePtr aConstrFeature;
478     std::set<FeaturePtr> aCoinList;
479     const std::set<std::shared_ptr<ModelAPI_Attribute>>& aRefsList = aRefFea->data()->refsToMe();
480     std::set<std::shared_ptr<ModelAPI_Attribute>>::const_iterator aIt;
481     for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
482       std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
483       aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
484       if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
485         AttributeRefAttrPtr aRAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
486         AttributePtr aAR = aRAttr->attr();
487         if (aAR->id() != SketchPlugin_Arc::CENTER_ID()) // ignore constraint to center of arc
488           aCoinList.insert(aConstrFeature);
489           PartSet_Tools::findCoincidences(aConstrFeature, aCoinsideLines,
490                                           SketchPlugin_ConstraintCoincidence::ENTITY_A());
491           PartSet_Tools::findCoincidences(aConstrFeature, aCoinsideLines,
492                                           SketchPlugin_ConstraintCoincidence::ENTITY_B());
493       }
494     }
495     // if there is no coincidence then it is not valid
496     if (aCoinList.size() == 0)
497       return false;
498
499     QList<FeaturePtr>::const_iterator anIt = aCoinsideLines.begin(), aLast = aCoinsideLines.end();
500     bool aValid = false;
501     for (; anIt != aLast && !aValid; anIt++) {
502       aValid = *anIt == aOtherFea;
503     }
504     if (aValid)
505       return true;
506   }
507   return false;
508 }
509