]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_Validators.cpp
Salome HOME
38bf03582b9d0a1bd9299078d981e3ef1414b3e9
[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 #include <ModelAPI_Session.h>
24
25 #include <SketchPlugin_Sketch.h>
26
27 #include <list>
28 #ifdef _DEBUG
29 #include <iostream>
30 #endif
31
32 int shapesNbPoints(const ModuleBase_ISelection* theSelection)
33 {
34   QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected();  
35   int aCount = 0;
36   foreach (ModuleBase_ViewerPrs aPrs, aList) {
37     const TopoDS_Shape& aShape = aPrs.shape();
38     if (!aShape.IsNull()) {
39       if (aShape.ShapeType() == TopAbs_VERTEX)
40         aCount++;
41     }
42   }
43   return aCount;
44 }
45
46 int shapesNbLines(const ModuleBase_ISelection* theSelection)
47 {
48   QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected();
49   int aCount = 0;
50   foreach(ModuleBase_ViewerPrs aPrs, aList) {
51     const TopoDS_Shape& aShape = aPrs.shape();
52     if (!aShape.IsNull()) {
53       if (aShape.ShapeType() == TopAbs_EDGE) {
54         TopoDS_Edge aEdge = TopoDS::Edge(aShape);
55         Standard_Real aStart, aEnd;
56         Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aStart, aEnd);
57         GeomAdaptor_Curve aAdaptor(aCurve);
58         if (aAdaptor.GetType() == GeomAbs_Line)
59           aCount++;
60       }
61     }
62   }
63   return aCount;
64 }
65
66 bool PartSet_DistanceValidator::isValid(const ModuleBase_ISelection* theSelection) const
67 {
68   int aCount = shapesNbPoints(theSelection) + shapesNbLines(theSelection);
69   return (aCount > 0) && (aCount < 3);
70 }
71
72 bool PartSet_LengthValidator::isValid(const ModuleBase_ISelection* theSelection) const
73 {
74   int aCount = shapesNbLines(theSelection);
75   return (aCount > 0) && (aCount < 2);
76 }
77
78 bool PartSet_PerpendicularValidator::isValid(const ModuleBase_ISelection* theSelection) const
79 {
80   int aCount = shapesNbLines(theSelection);
81   return (aCount > 0) && (aCount < 3);
82 }
83
84 bool PartSet_ParallelValidator::isValid(const ModuleBase_ISelection* theSelection) const
85 {
86   int aCount = shapesNbLines(theSelection);
87   return (aCount > 0) && (aCount < 3);
88 }
89
90 bool PartSet_RadiusValidator::isValid(const ModuleBase_ISelection* theSelection) const
91 {
92   QList<ModuleBase_ViewerPrs> aList = theSelection->getSelected();
93   ModuleBase_ViewerPrs aPrs;
94   int aCount = 0;
95   foreach (ModuleBase_ViewerPrs aPrs, aList) {
96     const TopoDS_Shape& aShape = aPrs.shape();
97     if (!aShape.IsNull()) {
98       if (aShape.ShapeType() == TopAbs_EDGE) {
99         TopoDS_Edge aEdge = TopoDS::Edge(aShape);
100         Standard_Real aStart, aEnd;
101         Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aEdge, aStart, aEnd);
102         GeomAdaptor_Curve aAdaptor(aCurve);
103         if (aAdaptor.GetType() == GeomAbs_Circle)
104           aCount++;
105       }
106     }
107   }
108   return (aCount > 0) && (aCount < 2);
109 }
110
111 bool PartSet_RigidValidator::isValid(const ModuleBase_ISelection* theSelection) const
112 {
113   int aCount = shapesNbLines(theSelection);
114   return (aCount > 0) && (aCount < 2);
115 }
116
117 bool PartSet_DifferentObjectsValidator::isValid(const AttributePtr& theAttribute, 
118                                                 const std::list<std::string>& theArguments) const
119 {
120   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
121
122   // 1. check whether the object of the attribute is not among the feature attributes
123   // find the attribute's object
124   ObjectPtr anObject =  GeomValidators_Tools::getObject(theAttribute);
125
126   // check whether the object is not among other feature attributes
127   if (anObject.get() != NULL) {
128     // Check RefAttr attributes
129     std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs = aFeature->data()->attributes("");
130     //if (anAttrs.size() > 0) {
131     std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anIt = anAttrs.begin();
132     for(; anIt != anAttrs.end(); anIt++) {
133       AttributePtr anAttr = *anIt;
134       // the function parameter attribute should be skipped
135       if (anAttr.get() == NULL || anAttr->id() == theAttribute->id())
136         continue;
137       ObjectPtr aCurObject =  GeomValidators_Tools::getObject(anAttr);
138       if (aCurObject  && aCurObject == anObject)
139         return false;
140     }
141   }
142   else {
143     // 2. collect object referenced by theAttribute and ...
144     if (featureHasReferences(theAttribute)) {
145       // 3. check whether the attribute value is not among other feature attributes
146       std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs = 
147         aFeature->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
148       std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
149       for(; anAttr != anAttrs.end(); anAttr++) {
150         if (*anAttr) {
151           std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = 
152             std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
153           // check the object is already presented
154           if (!aRef->isObject() && aRef->attr() == theAttribute)
155             return false;
156         }
157 // TODO(nds) v1.0.2 master
158 //bool PartSet_DifferentObjectsValidator::isValid(const FeaturePtr& theFeature,
159 //                                                const std::list<std::string>& theArguments,
160 //                                                const ObjectPtr& theObject,
161 //                                                const GeomShapePtr& theShape) const
162 //{
163 //  // Check RefAttr attributes
164 //  std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs =
165 //    theFeature->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
166 //  if (anAttrs.size() > 0) {
167 //    std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
168 //    for(; anAttr != anAttrs.end(); anAttr++) {
169 //      if (*anAttr) {
170 //        std::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
171 //          std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
172 //        // check the object is already presented
173 //        if (aRef->isObject() && aRef->object() == theObject)
174 //          return false;
175 //      }
176 //    }
177 //  }
178 //  // Check selection attributes
179 //  anAttrs = theFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
180 //  if (anAttrs.size() > 0) {
181 //    std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
182 //    for(; anAttr != anAttrs.end(); anAttr++) {
183 //      if (*anAttr) {
184 //        std::shared_ptr<ModelAPI_AttributeSelection> aRef =
185 //          std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*anAttr);
186 //        // check the object is already presented
187 //        if (aRef->isInitialized() && aRef->context() == theObject) {
188 //          if (theShape.get() != NULL) {
189 //            if (aRef->value()->isEqual(theShape))
190 //              return false;
191 //          } else
192 //            return false;
193 //        }
194 //      }
195 //    }
196 //  }
197 //  // Check selection attributes
198 //  anAttrs = theFeature->data()->attributes(ModelAPI_AttributeReference::typeId());
199 //  if (anAttrs.size() > 0) {
200 //    std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
201 //    for(; anAttr != anAttrs.end(); anAttr++) {
202 //      if (*anAttr) {
203 //        std::shared_ptr<ModelAPI_AttributeReference> aRef =
204 //          std::dynamic_pointer_cast<ModelAPI_AttributeReference>(*anAttr);
205 //        // check the object is already presented
206 //        if (aRef->isInitialized() && aRef->value() == theObject)
207 //          return false;
208 // ======= end of todo
209       }
210       return true;
211     }
212   }
213   return true;
214 }
215
216 bool PartSet_DifferentObjectsValidator::featureHasReferences(const AttributePtr& theAttribute) const
217 // TODO(nds) v1.0.2 master
218 //bool PartSet_DifferentObjectsValidator::isValid(const FeaturePtr& theFeature,
219 //                                                const std::list<std::string>& theArguments,
220 //                                                const AttributePtr& theAttribute) const
221 //{
222 //  if (PartSet_DifferentObjectsValidator::isValid(theAttribute, theArguments)) {
223 //    std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs =
224 //      theFeature->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
225 //    std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
226 //    for(; anAttr != anAttrs.end(); anAttr++) {
227 //      if (*anAttr) {
228 //        std::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
229 //          std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttr);
230 //        // check the object is already presented
231 //        if (!aRef->isObject() && aRef->attr() == theAttribute)
232 //          return false;
233 //      }
234 //    }
235 //    return true;
236 //  }
237 //  return false;
238 //}
239 //
240 //bool PartSet_DifferentObjectsValidator::isValid(const AttributePtr& theAttribute,
241 //                                                const std::list<std::string>& theArguments) const
242 // ======= end of todo
243 {
244   std::list<std::pair<std::string, std::list<ObjectPtr> > > allRefs;
245   if (theAttribute->owner().get() && theAttribute->owner()->data().get())
246     theAttribute->owner()->data()->referencesToObjects(allRefs);
247   // collect object referenced by theAttribute
248   std::list<ObjectPtr>* anAttrObjs = 0;
249   std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRefIter = allRefs.begin();
250   for(; aRefIter != allRefs.end(); aRefIter++) {
251     if (theAttribute->id() == aRefIter->first)
252       anAttrObjs = &(aRefIter->second);
253   }
254   if (!anAttrObjs || anAttrObjs->empty())
255     return true; // theAttribute does not references to anything
256   // check with all others
257   for(aRefIter = allRefs.begin(); aRefIter != allRefs.end(); aRefIter++) {
258     if (theAttribute->id() == aRefIter->first)
259       continue; // do not check with myself
260     std::list<ObjectPtr>::iterator aReferenced = aRefIter->second.begin();
261     for(; aReferenced != aRefIter->second.end(); aReferenced++) {
262       std::list<ObjectPtr>::iterator aReferencedByMe = anAttrObjs->begin();
263       for(; aReferencedByMe != anAttrObjs->end(); aReferencedByMe++) {
264         if (*aReferenced == *aReferencedByMe) // found same objects!
265           return false;
266       }
267     }
268   }
269   return true;
270 }
271
272 bool PartSet_SketchEntityValidator::isValid(const AttributePtr& theAttribute,
273                                             const std::list<std::string>& theArguments) const
274 {
275   bool isSketchEntities = true;
276   std::set<std::string> anEntityKinds;
277   std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
278   for (; anIt != aLast; anIt++) {
279     anEntityKinds.insert(*anIt);
280   }
281
282   std::string anAttributeType = theAttribute->attributeType();
283   if (anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
284     AttributeSelectionListPtr aSelectionListAttr = 
285                       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
286     // it filters only selection list attributes
287     std::string aType = aSelectionListAttr->selectionType().c_str();
288     // all context objects should be sketch entities
289     int aSize = aSelectionListAttr->size();
290     for (int i = 0; i < aSelectionListAttr->size() && isSketchEntities; i++) {
291       AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i);
292       ObjectPtr anObject = aSelectAttr->context();
293       FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
294       isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
295     }
296   }
297   if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
298     std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = 
299                      std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
300     isSketchEntities = false;
301     if (aRef->isObject()) {
302       ObjectPtr anObject = aRef->object();
303       if (anObject.get() != NULL) {
304         FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
305         if (aFeature.get() != NULL)
306           isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
307       }
308     }
309   }
310
311   return isSketchEntities;
312 }
313
314
315
316 bool PartSet_SameTypeAttrValidator::isValid(
317   const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
318 {
319   // there is a check whether the feature contains a point and a linear edge or two point values
320   std::string aParamA = theArguments.front();
321   SessionPtr aMgr = ModelAPI_Session::get();
322   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
323
324   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
325   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
326   if (!aRefAttr)
327     return false;
328
329   bool isObject = aRefAttr->isObject();
330   ObjectPtr anObject = aRefAttr->object();
331   if (isObject && anObject) {
332     FeaturePtr aRefFea = ModelAPI_Feature::feature(anObject);
333
334     AttributeRefAttrPtr aOtherAttr = aFeature->data()->refattr(aParamA);
335     ObjectPtr aOtherObject = aOtherAttr->object();
336     FeaturePtr aOtherFea = ModelAPI_Feature::feature(aOtherObject);
337     return aRefFea->getKind() == aOtherFea->getKind();
338   }
339   return false;
340 }
341