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::type());
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       }
157       return true;
158     }
159   }
160   return true;
161 }
162
163 bool PartSet_DifferentObjectsValidator::featureHasReferences(const AttributePtr& theAttribute) const
164 {
165   std::list<std::pair<std::string, std::list<ObjectPtr> > > allRefs;
166   if (theAttribute->owner().get() && theAttribute->owner()->data().get())
167     theAttribute->owner()->data()->referencesToObjects(allRefs);
168   // collect object referenced by theAttribute
169   std::list<ObjectPtr>* anAttrObjs = 0;
170   std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRefIter = allRefs.begin();
171   for(; aRefIter != allRefs.end(); aRefIter++) {
172     if (theAttribute->id() == aRefIter->first)
173       anAttrObjs = &(aRefIter->second);
174   }
175   if (!anAttrObjs || anAttrObjs->empty())
176     return true; // theAttribute does not references to anything
177   // check with all others
178   for(aRefIter = allRefs.begin(); aRefIter != allRefs.end(); aRefIter++) {
179     if (theAttribute->id() == aRefIter->first)
180       continue; // do not check with myself
181     std::list<ObjectPtr>::iterator aReferenced = aRefIter->second.begin();
182     for(; aReferenced != aRefIter->second.end(); aReferenced++) {
183       std::list<ObjectPtr>::iterator aReferencedByMe = anAttrObjs->begin();
184       for(; aReferencedByMe != anAttrObjs->end(); aReferencedByMe++) {
185         if (*aReferenced == *aReferencedByMe) // found same objects!
186           return false;
187       }
188     }
189   }
190   return true;
191 }
192
193 bool PartSet_SketchEntityValidator::isValid(const AttributePtr& theAttribute,
194                                             const std::list<std::string>& theArguments) const
195 {
196   bool isSketchEntities = true;
197   std::set<std::string> anEntityKinds;
198   std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
199   for (; anIt != aLast; anIt++) {
200     anEntityKinds.insert(*anIt);
201   }
202
203   std::string anAttributeType = theAttribute->attributeType();
204   if (anAttributeType == ModelAPI_AttributeSelectionList::type()) {
205     AttributeSelectionListPtr aSelectionListAttr = 
206                       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
207     // it filters only selection list attributes
208     std::string aType = aSelectionListAttr->selectionType().c_str();
209     // all context objects should be sketch entities
210     int aSize = aSelectionListAttr->size();
211     for (int i = 0; i < aSelectionListAttr->size() && isSketchEntities; i++) {
212       AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i);
213       ObjectPtr anObject = aSelectAttr->context();
214       FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
215       isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
216     }
217   }
218   if (anAttributeType == ModelAPI_AttributeRefAttr::type()) {
219     std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = 
220                      std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
221     isSketchEntities = false;
222     if (aRef->isObject()) {
223       ObjectPtr anObject = aRef->object();
224       if (anObject.get() != NULL) {
225         FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
226         if (aFeature.get() != NULL)
227           isSketchEntities = anEntityKinds.find(aFeature->getKind()) != anEntityKinds.end();
228       }
229     }
230   }
231
232   return isSketchEntities;
233 }