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