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