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