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