]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_Validators.cpp
Salome HOME
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   // 1. check whether the object of the attribute is not among the feature attributes
122   // find the attribute's object
123   ObjectPtr anObject =  GeomValidators_Tools::getObject(theAttribute);
124
125   // check whether the object is not among other feature attributes
126   if (anObject.get() != NULL) {
127     // Check RefAttr attributes
128     std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs = aFeature->data()->attributes("");
129     //if (anAttrs.size() > 0) {
130     std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anIt = anAttrs.begin();
131     for(; anIt != anAttrs.end(); anIt++) {
132       AttributePtr anAttr = *anIt;
133       // the function parameter attribute should be skipped
134       if (anAttr.get() == NULL || anAttr->id() == theAttribute->id())
135         continue;
136       ObjectPtr aCurObject =  GeomValidators_Tools::getObject(anAttr);
137       if (aCurObject  && aCurObject == anObject)
138         return false;
139     }
140   }
141   else {
142     // 2. collect object referenced by theAttribute and ...
143     if (featureHasReferences(theAttribute)) {
144       // 3. check whether the attribute value is not among other feature attributes
145       std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs = 
146         aFeature->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
147       std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
148       for(; anAttr != anAttrs.end(); anAttr++) {
149         if (*anAttr) {
150           std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = 
151             std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
152           // check the object is already presented
153           if (!aRef->isObject() && aRef->attr() == theAttribute)
154             return false;
155         }
156 // TODO(nds) v1.0.2 master
157 //bool PartSet_DifferentObjectsValidator::isValid(const FeaturePtr& theFeature,
158 //                                                const std::list<std::string>& theArguments,
159 //                                                const ObjectPtr& theObject,
160 //                                                const GeomShapePtr& theShape) const
161 //{
162 //  // Check RefAttr attributes
163 //  std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs =
164 //    theFeature->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
165 //  if (anAttrs.size() > 0) {
166 //    std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
167 //    for(; anAttr != anAttrs.end(); anAttr++) {
168 //      if (*anAttr) {
169 //        std::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
170 //          std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
171 //        // check the object is already presented
172 //        if (aRef->isObject() && aRef->object() == theObject)
173 //          return false;
174 //      }
175 //    }
176 //  }
177 //  // Check selection attributes
178 //  anAttrs = theFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
179 //  if (anAttrs.size() > 0) {
180 //    std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
181 //    for(; anAttr != anAttrs.end(); anAttr++) {
182 //      if (*anAttr) {
183 //        std::shared_ptr<ModelAPI_AttributeSelection> aRef =
184 //          std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*anAttr);
185 //        // check the object is already presented
186 //        if (aRef->isInitialized() && aRef->context() == theObject) {
187 //          if (theShape.get() != NULL) {
188 //            if (aRef->value()->isEqual(theShape))
189 //              return false;
190 //          } else
191 //            return false;
192 //        }
193 //      }
194 //    }
195 //  }
196 //  // Check selection attributes
197 //  anAttrs = theFeature->data()->attributes(ModelAPI_AttributeReference::typeId());
198 //  if (anAttrs.size() > 0) {
199 //    std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
200 //    for(; anAttr != anAttrs.end(); anAttr++) {
201 //      if (*anAttr) {
202 //        std::shared_ptr<ModelAPI_AttributeReference> aRef =
203 //          std::dynamic_pointer_cast<ModelAPI_AttributeReference>(*anAttr);
204 //        // check the object is already presented
205 //        if (aRef->isInitialized() && aRef->value() == theObject)
206 //          return false;
207 // ======= end of todo
208       }
209       return true;
210     }
211   }
212   return true;
213 }
214
215 bool PartSet_DifferentObjectsValidator::featureHasReferences(const AttributePtr& theAttribute) const
216 // TODO(nds) v1.0.2 master
217 //bool PartSet_DifferentObjectsValidator::isValid(const FeaturePtr& theFeature,
218 //                                                const std::list<std::string>& theArguments,
219 //                                                const AttributePtr& theAttribute) const
220 //{
221 //  if (PartSet_DifferentObjectsValidator::isValid(theAttribute, theArguments)) {
222 //    std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs =
223 //      theFeature->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
224 //    std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
225 //    for(; anAttr != anAttrs.end(); anAttr++) {
226 //      if (*anAttr) {
227 //        std::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
228 //          std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
229 //        // check the object is already presented
230 //        if (!aRef->isObject() && aRef->attr() == theAttribute)
231 //          return false;
232 //      }
233 //    }
234 //    return true;
235 //  }
236 //  return false;
237 //}
238 //
239 //bool PartSet_DifferentObjectsValidator::isValid(const AttributePtr& theAttribute,
240 //                                                const std::list<std::string>& theArguments) const
241 // ======= end of todo
242 {
243   std::list<std::pair<std::string, std::list<ObjectPtr> > > allRefs;
244   if (theAttribute->owner().get() && theAttribute->owner()->data().get())
245     theAttribute->owner()->data()->referencesToObjects(allRefs);
246   // collect object referenced by theAttribute
247   std::list<ObjectPtr>* anAttrObjs = 0;
248   std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRefIter = allRefs.begin();
249   for(; aRefIter != allRefs.end(); aRefIter++) {
250     if (theAttribute->id() == aRefIter->first)
251       anAttrObjs = &(aRefIter->second);
252   }
253   if (!anAttrObjs || anAttrObjs->empty())
254     return true; // theAttribute does not references to anything
255   // check with all others
256   for(aRefIter = allRefs.begin(); aRefIter != allRefs.end(); aRefIter++) {
257     if (theAttribute->id() == aRefIter->first)
258       continue; // do not check with myself
259     std::list<ObjectPtr>::iterator aReferenced = aRefIter->second.begin();
260     for(; aReferenced != aRefIter->second.end(); aReferenced++) {
261       std::list<ObjectPtr>::iterator aReferencedByMe = anAttrObjs->begin();
262       for(; aReferencedByMe != anAttrObjs->end(); aReferencedByMe++) {
263         if (*aReferenced == *aReferencedByMe) // found same objects!
264           return false;
265       }
266     }
267   }
268   return true;
269 }
270
271 bool PartSet_SketchEntityValidator::isValid(const AttributePtr& theAttribute,
272                                             const std::list<std::string>& theArguments) const
273 {
274   bool isSketchEntities = true;
275   std::set<std::string> anEntityKinds;
276   std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
277   for (; anIt != aLast; anIt++) {
278     anEntityKinds.insert(*anIt);
279   }
280
281   std::string anAttributeType = theAttribute->attributeType();
282   if (anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
283     AttributeSelectionListPtr aSelectionListAttr = 
284                       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
285     // it filters only selection list attributes
286     std::string aType = aSelectionListAttr->selectionType().c_str();
287     // all context objects should be sketch entities
288     int aSize = aSelectionListAttr->size();
289     for (int i = 0; i < aSelectionListAttr->size() && isSketchEntities; i++) {
290       AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i);
291       ObjectPtr anObject = aSelectAttr->context();
292       FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
293       isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
294     }
295   }
296   if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
297     std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = 
298                      std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
299     isSketchEntities = false;
300     if (aRef->isObject()) {
301       ObjectPtr anObject = aRef->object();
302       if (anObject.get() != NULL) {
303         FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
304         if (aFeature.get() != NULL)
305           isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
306       }
307     }
308   }
309
310   return isSketchEntities;
311 }