Salome HOME
It provides error messages for validators.
[modules/shaper.git] / src / PartSet / PartSet_OperationPrs.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_OperationPrs.cpp
4 // Created:     01 Jul 2015
5 // Author:      Natalia ERMOLAEVA
6
7 #include "PartSet_OperationPrs.h"
8 #include "PartSet_Tools.h"
9
10 #include "XGUI_Workshop.h"
11 #include "XGUI_ModuleConnector.h"
12 #include "XGUI_Displayer.h"
13
14 #include "ModuleBase_Tools.h"
15
16 #include <ModelAPI_Result.h>
17 #include <ModelAPI_Attribute.h>
18 #include <ModelAPI_AttributeRefAttr.h>
19 #include <ModelAPI_AttributeReference.h>
20 #include <ModelAPI_AttributeSelection.h>
21 #include <ModelAPI_AttributeSelectionList.h>
22 #include <ModelAPI_AttributeRefList.h>
23 #include <ModelAPI_Validator.h>
24 #include <ModelAPI_Session.h>
25
26 #include <GeomValidators_Tools.h>
27
28 #include <GeomAPI_IPresentable.h>
29
30 #include <StdPrs_WFDeflectionShape.hxx>
31
32 #include <QList>
33
34 IMPLEMENT_STANDARD_HANDLE(PartSet_OperationPrs, ViewerData_AISShape);
35 IMPLEMENT_STANDARD_RTTIEXT(PartSet_OperationPrs, ViewerData_AISShape);
36
37 PartSet_OperationPrs::PartSet_OperationPrs(ModuleBase_IWorkshop* theWorkshop)
38   : ViewerData_AISShape(TopoDS_Shape()), myFeature(FeaturePtr()), myWorkshop(theWorkshop)
39 {
40   myShapeColor = ModuleBase_Tools::color("Visualization", "construction_plane_color", "1,1,0");
41   myResultColor = ModuleBase_Tools::color("Visualization", "construction_plane_color", "0,1,0");
42 }
43
44 bool PartSet_OperationPrs::canActivate(const FeaturePtr& theFeature)
45 {
46   bool aHasSelectionAttribute = false;
47
48   std::list<AttributePtr> anAttributes = theFeature->data()->attributes("");
49   std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
50   for (; anIt != aLast && !aHasSelectionAttribute; anIt++)
51     aHasSelectionAttribute = isSelectionAttribute(*anIt);
52
53   return aHasSelectionAttribute;
54 }
55
56 void PartSet_OperationPrs::setFeature(const FeaturePtr& theFeature)
57 {
58   myFeature = theFeature;
59   updateShapes();
60 }
61
62 /*bool PartSet_OperationPrs::dependOn(const ObjectPtr& theResult)
63 {
64   return myFeatureShapes.contains(theResult);
65 }*/
66
67 void PartSet_OperationPrs::updateShapes()
68 {
69   myFeatureShapes.clear();
70   getFeatureShapes(myFeatureShapes);
71
72   myFeatureResults.clear();
73   if (myFeature)
74     myFeatureResults = myFeature->results();
75 }
76
77 void PartSet_OperationPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
78                                    const Handle(Prs3d_Presentation)& thePresentation, 
79                                    const Standard_Integer theMode)
80 {
81   Quantity_Color aColor(1., 1., 0., Quantity_TOC_RGB); // yellow
82   SetColor(aColor);
83
84   thePresentation->Clear();
85   XGUI_Displayer* aDisplayer = workshop()->displayer();
86
87   // create presentations on the base of the shapes
88   Handle(Prs3d_Drawer) aDrawer = Attributes();
89   QMap<ObjectPtr, QList<GeomShapePtr> >::const_iterator anIt = myFeatureShapes.begin(),
90                                                         aLast = myFeatureShapes.end();
91   for (; anIt != aLast; anIt++) {
92     ObjectPtr anObject = anIt.key();
93     if (!isVisible(aDisplayer, anObject))
94       continue;
95     QList<GeomShapePtr> aShapes = anIt.value();
96     QList<GeomShapePtr>::const_iterator aShIt = aShapes.begin(), aShLast = aShapes.end();
97     for (; aShIt != aShLast; aShIt++) {
98       GeomShapePtr aGeomShape = *aShIt;
99       if (!aGeomShape.get())
100         continue;
101       TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
102       // change deviation coefficient to provide more precise circle
103       ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
104       StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
105     }
106   }
107
108   aColor = Quantity_Color(0., 1., 0., Quantity_TOC_RGB); // green
109   SetColor(aColor);
110
111   std::list<ResultPtr>::const_iterator aRIt = myFeatureResults.begin(),
112                                        aRLast = myFeatureResults.end();
113   for (; aRIt != aRLast; aRIt++) {
114     ResultPtr aResult = *aRIt;
115     if (!isVisible(aDisplayer, aResult))
116       continue;
117     GeomShapePtr aGeomShape = aResult->shape();
118     if (!aGeomShape.get())
119       continue;
120     TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
121     // change deviation coefficient to provide more precise circle
122     ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, aDrawer);
123     StdPrs_WFDeflectionShape::Add(thePresentation, aShape, aDrawer);
124   }
125 }
126
127 void PartSet_OperationPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
128                                             const Standard_Integer aMode)
129 {
130   // the presentation is not used in the selection
131 }
132
133 bool PartSet_OperationPrs::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
134 {
135   bool aVisible = false;
136   GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
137   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
138   if (aPrs.get() || aResult.get())
139     aVisible = theDisplayer->isVisible(theObject);
140   else {
141     // check if all results of the feature are visible
142     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
143     std::list<ResultPtr> aResults = aFeature->results();
144     std::list<ResultPtr>::const_iterator aIt;
145     aVisible = !aResults.empty();
146     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
147       aVisible = aVisible && theDisplayer->isVisible(*aIt);
148     }
149   }
150   return aVisible;
151 }
152
153 bool isSubObject(const ObjectPtr& theObject, const FeaturePtr& theFeature)
154 {
155   bool isSub = false;
156   CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
157   if (aComposite.get())
158     isSub = aComposite->isSub(theObject);
159
160   return isSub;
161 }
162
163 void addValue(const ObjectPtr& theObject, const GeomShapePtr& theShape,
164               const FeaturePtr& theFeature,
165               QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
166 {
167   if (theObject.get()) {
168     GeomShapePtr aShape = theShape;
169     if (!aShape.get()) {
170       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
171       if (aResult.get())
172         aShape = aResult->shape();
173     }
174     if (!isSubObject(theObject, theFeature)) {
175       if (theObjectShapes.contains(theObject))
176         theObjectShapes[theObject].append(aShape);
177       else {
178         QList<GeomShapePtr> aShapes;
179         aShapes.append(aShape);
180         theObjectShapes[theObject] = aShapes;
181       }
182     }
183   }
184 }
185
186 void PartSet_OperationPrs::getFeatureShapes(QMap<ObjectPtr, QList<GeomShapePtr> >& theObjectShapes)
187 {
188   if (!myFeature.get())
189     return;
190
191   ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
192
193   QList<GeomShapePtr> aShapes;
194   std::list<AttributePtr> anAttributes = myFeature->data()->attributes("");
195   std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
196   for (; anIt != aLast; anIt++) {
197     AttributePtr anAttribute = *anIt;
198     if (!isSelectionAttribute(anAttribute))
199       continue;
200
201     if (!aValidators->isCase(myFeature, anAttribute->id()))
202       continue; // this attribute is not participated in the current case
203
204     std::string anAttrType = anAttribute->attributeType();
205
206     if (anAttrType == ModelAPI_AttributeSelectionList::typeId()) {
207       std::shared_ptr<ModelAPI_AttributeSelectionList> aCurSelList = 
208               std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
209       for(int i = 0; i < aCurSelList->size(); i++) {
210         std::shared_ptr<ModelAPI_AttributeSelection> aSelAttribute = aCurSelList->value(i);
211         ResultPtr aResult = aSelAttribute->context();
212         GeomShapePtr aShape = aSelAttribute->value();
213         addValue(aResult, aShape, myFeature, theObjectShapes);
214       }
215     }
216     if (anAttrType == ModelAPI_AttributeRefList::typeId()) {
217       std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
218         std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttribute);
219       for (int i = 0; i < aCurSelList->size(); i++) {
220         GeomShapePtr aShape;
221         addValue(aCurSelList->object(i), aShape, myFeature, theObjectShapes);
222       }
223     }
224     else {
225       ObjectPtr anObject;
226       GeomShapePtr aShape;
227       if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
228         AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
229         if (anAttr->isObject()) {
230           anObject = anAttr->object();
231         }
232         else {
233           aShape = PartSet_Tools::findShapeBy2DPoint(anAttr, myWorkshop);
234           // the distance point is not found if the point is selected in the 2nd time
235           // TODO: after debug, this check can be removed
236           if (!aShape.get())
237             continue;
238           anObject = anAttr->attr()->owner();
239         }
240       }
241       if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
242         AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
243         anObject = anAttr->context();
244         aShape = anAttr->value();
245       }
246       if (anAttrType == ModelAPI_AttributeReference::typeId()) {
247         AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
248         anObject = anAttr->value();
249       }
250       addValue(anObject, aShape, myFeature, theObjectShapes);
251     }
252   }
253 }
254
255 bool PartSet_OperationPrs::isSelectionAttribute(const AttributePtr& theAttribute)
256 {
257   std::string anAttrType = theAttribute->attributeType();
258
259   return anAttrType == ModelAPI_AttributeSelectionList::typeId() ||
260          anAttrType == ModelAPI_AttributeRefList::typeId() ||
261          anAttrType == ModelAPI_AttributeRefAttr::typeId() ||
262          anAttrType == ModelAPI_AttributeSelection::typeId() ||
263          anAttrType == ModelAPI_AttributeReference::typeId();
264 }
265
266 XGUI_Workshop* PartSet_OperationPrs::workshop() const
267 {
268   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
269   return aConnector->workshop();
270 }