Salome HOME
#1404 Random crash with Shaper: AIS presentations: operation prs, result sketch prs...
[modules/shaper.git] / src / XGUI / XGUI_Displayer.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        XGUI_Displayer.cpp
4 // Created:     20 Apr 2014
5 // Author:      Natalia ERMOLAEVA
6
7 #include "XGUI_Displayer.h"
8 #include "XGUI_Workshop.h"
9 #include "XGUI_ViewerProxy.h"
10 #include "XGUI_SelectionMgr.h"
11 #include "XGUI_Selection.h"
12 #include "XGUI_CustomPrs.h"
13
14 #ifndef HAVE_SALOME
15 #include <AppElements_Viewer.h>
16 #endif
17
18 #include <ModelAPI_Document.h>
19 #include <ModelAPI_Data.h>
20 #include <ModelAPI_Object.h>
21 #include <ModelAPI_Tools.h>
22 #include <ModelAPI_AttributeIntArray.h>
23 #include <ModelAPI_ResultCompSolid.h>
24
25 #include <ModuleBase_ResultPrs.h>
26 #include <ModuleBase_Tools.h>
27 #include <ModuleBase_IModule.h>
28 #include <ModuleBase_ViewerPrs.h>
29
30 #include <GeomAPI_Shape.h>
31 #include <GeomAPI_IPresentable.h>
32 #include <GeomAPI_ICustomPrs.h>
33
34 #include <AIS_InteractiveContext.hxx>
35 #include <AIS_LocalContext.hxx>
36 #include <AIS_ListOfInteractive.hxx>
37 #include <AIS_ListIteratorOfListOfInteractive.hxx>
38 #include <AIS_DimensionSelectionMode.hxx>
39 #include <AIS_Shape.hxx>
40 #include <AIS_Dimension.hxx>
41 #include <AIS_Trihedron.hxx>
42 #include <AIS_Axis.hxx>
43 #include <AIS_Plane.hxx>
44 #include <AIS_Point.hxx>
45 #include <TColStd_ListIteratorOfListOfInteger.hxx>
46 #include <SelectMgr_ListOfFilter.hxx>
47 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
48 #include <Prs3d_Drawer.hxx>
49 #include <Prs3d_IsoAspect.hxx>
50 #include <SelectMgr_SelectionManager.hxx>
51
52 #include <StdSelect_ViewerSelector3d.hxx>
53
54 #include <TColStd_MapOfTransient.hxx>
55 #include <TColStd_MapIteratorOfMapOfTransient.hxx>
56
57 #include <Events_Loop.h>
58 #include <ModelAPI_Events.h>
59
60 #include <set>
61
62 const int MOUSE_SENSITIVITY_IN_PIXEL = 10;  ///< defines the local context mouse selection sensitivity
63
64 //#define DEBUG_ACTIVATE_OBJECTS
65 //#define DEBUG_DEACTIVATE
66 //#define DEBUG_ACTIVATE_AIS
67 //#define DEBUG_DEACTIVATE_AIS
68
69 //#define DEBUG_DISPLAY
70 //#define DEBUG_FEATURE_REDISPLAY
71 //#define DEBUG_SELECTION_FILTERS
72
73 //#define DEBUG_COMPOSILID_DISPLAY
74 // Workaround for bug #25637
75 void displayedObjects(const Handle(AIS_InteractiveContext)& theAIS, AIS_ListOfInteractive& theList)
76 {
77   // Get from null point
78   theAIS->DisplayedObjects(theList, true);
79   if (theAIS->HasOpenedContext()) {
80     // get from local context
81     const Handle(AIS_LocalContext)& aLC = theAIS->LocalContext();
82     TColStd_MapOfTransient aMap;
83     int NbDisp = aLC->DisplayedObjects(aMap);
84     TColStd_MapIteratorOfMapOfTransient aIt(aMap);
85
86     Handle(AIS_InteractiveObject) curIO;
87     Handle(Standard_Transient) Tr;
88     for(; aIt.More(); aIt.Next()){
89       Tr = aIt.Key();
90       curIO = *((Handle(AIS_InteractiveObject)*) &Tr);
91       theList.Append(curIO);
92     }
93   }
94 }
95
96 QString qIntListInfo(const QIntList& theValues, const QString& theSeparator = QString(", "))
97 {
98   QStringList anInfo;
99   QIntList::const_iterator anIt = theValues.begin(), aLast = theValues.end();
100   for (; anIt != aLast; anIt++) {
101     anInfo.append(QString::number(*anIt));
102   }
103   return anInfo.join(theSeparator);
104 }
105
106 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
107   : myWorkshop(theWorkshop), myNeedUpdate(false),
108   myIsTrihedronActive(false), myViewerBlockedRecursiveCount(0)
109 {
110   myCustomPrs = std::shared_ptr<GeomAPI_ICustomPrs>(new XGUI_CustomPrs(theWorkshop));
111 }
112
113 XGUI_Displayer::~XGUI_Displayer()
114 {
115 }
116
117 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
118 {
119   return myResult2AISObjectMap.contains(theObject);
120 }
121
122 bool XGUI_Displayer::display(ObjectPtr theObject, bool theUpdateViewer)
123 {
124   bool aDisplayed = false;
125   if (isVisible(theObject)) {
126 #ifdef DEBUG_COMPOSILID_DISPLAY
127     ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
128     if (aCompsolidResult.get()) {
129       for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
130         ResultPtr aSubResult = aCompsolidResult->subResult(i);
131         if (aSubResult.get())
132           redisplay(aSubResult, false);
133       }
134       if (theUpdateViewer)
135         updateViewer();
136     }
137     else
138 #endif
139     aDisplayed = redisplay(theObject, theUpdateViewer);
140   } else {
141     AISObjectPtr anAIS;
142     GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
143     bool isShading = false;
144     if (aPrs.get() != NULL) {
145       anAIS = aPrs->getAISObject(anAIS);
146     } else {
147       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
148       if (aResult.get() != NULL) {
149 #ifdef DEBUG_COMPOSILID_DISPLAY
150         ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
151         if (aCompsolidResult.get()) {
152           for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
153             ResultPtr aSubResult = aCompsolidResult->subResult(i);
154             if (aSubResult.get())
155               display(aSubResult, false);
156           }
157           if (theUpdateViewer)
158             updateViewer();
159         }
160         else {
161 #endif
162         std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
163         if (aShapePtr.get() != NULL) {
164           anAIS = AISObjectPtr(new GeomAPI_AISObject());
165           Handle(AIS_InteractiveObject) anAISPrs = myWorkshop->module()->createPresentation(aResult);
166           if (anAISPrs.IsNull())
167             anAISPrs = new ModuleBase_ResultPrs(aResult);
168           anAIS->setImpl(new Handle(AIS_InteractiveObject)(anAISPrs));
169           //anAIS->createShape(aShapePtr);
170           isShading = true;
171         }
172 #ifdef DEBUG_COMPOSILID_DISPLAY
173         } // close else
174 #endif
175       }
176     }
177     if (anAIS)
178       aDisplayed = display(theObject, anAIS, isShading, theUpdateViewer);
179   }
180   return aDisplayed;
181 }
182
183 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS, ModuleBase_IModule* theModule)
184 {
185   Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
186   if (!aShapePrs.IsNull()) {
187     TopoDS_Shape aShape = aShapePrs->Shape();
188     if (aShape.IsNull())
189       return false;
190     TopAbs_ShapeEnum aType = aShape.ShapeType();
191     if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
192       return false;
193     else {
194       // Check that the presentation is not a sketch
195       return theModule->canBeShaded(theAIS);
196     }
197   }
198   return false;
199 }
200
201 bool XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS, 
202                              bool isShading, bool theUpdateViewer)
203 {
204   bool aDisplayed = false;
205
206   Handle(AIS_InteractiveContext) aContext = AISContext();
207   if (aContext.IsNull())
208     return aDisplayed;
209
210   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
211   if (!anAISIO.IsNull()) {
212     appendResultObject(theObject, theAIS);
213
214     bool isCustomized = customizeObject(theObject);
215
216     int aDispMode = isShading? Shading : Wireframe;
217     if (isShading)
218       anAISIO->Attributes()->SetFaceBoundaryDraw( Standard_True );
219     anAISIO->SetDisplayMode(aDispMode);
220     aContext->Display(anAISIO, aDispMode, 0, false, true, AIS_DS_Displayed); 
221     aDisplayed = true;
222
223     emit objectDisplayed(theObject, theAIS);
224     activate(anAISIO, myActiveSelectionModes, theUpdateViewer);
225     // the fix from VPA for more suitable selection of sketcher lines
226     if(anAISIO->Width() > 1) {
227       for(int aModeIdx = 0; aModeIdx < myActiveSelectionModes.length(); ++aModeIdx) {
228         int aMode = myActiveSelectionModes.value(aModeIdx);
229         double aPrecision = (aMode == getSelectionMode(TopAbs_VERTEX))? 20 : 
230                                                     (anAISIO->Width() + 2);
231         aContext->SetSelectionSensitivity(anAISIO, aMode, aPrecision);
232       }
233     }
234   } 
235   if (theUpdateViewer)
236     updateViewer();
237
238   return aDisplayed;
239 }
240
241 bool XGUI_Displayer::erase(ObjectPtr theObject, const bool theUpdateViewer)
242 {
243   bool aErased = false;
244   if (!isVisible(theObject))
245     return aErased;
246
247   Handle(AIS_InteractiveContext) aContext = AISContext();
248   if (aContext.IsNull())
249     return aErased;
250
251   AISObjectPtr anObject = myResult2AISObjectMap[theObject];
252   if (anObject) {
253     Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
254     if (!anAIS.IsNull()) {
255       emit beforeObjectErase(theObject, anObject);
256       aContext->Remove(anAIS, false/*update viewer*/);
257       aErased = true;
258     }
259   }
260   myResult2AISObjectMap.remove(theObject);
261
262 #ifdef DEBUG_DISPLAY
263   std::ostringstream aPtrStr;
264   aPtrStr << theObject.get();
265   qDebug(QString("erase object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
266   qDebug(getResult2AISObjectMapInfo().c_str());
267 #endif
268
269   if (theUpdateViewer)
270     updateViewer();
271
272   return aErased;
273 }
274
275 bool XGUI_Displayer::redisplay(ObjectPtr theObject, bool theUpdateViewer)
276 {
277   bool aRedisplayed = false;
278   if (!isVisible(theObject))
279     return aRedisplayed;
280
281   AISObjectPtr aAISObj = getAISObject(theObject);
282   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
283
284   GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
285   if (aPrs) {
286     AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
287     if (!aAIS_Obj) {
288       aRedisplayed = erase(theObject, theUpdateViewer);
289       return aRedisplayed;
290     }
291     if (aAIS_Obj != aAISObj) {
292       appendResultObject(theObject, aAIS_Obj);
293     }
294     aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
295   }
296
297   Handle(AIS_InteractiveContext) aContext = AISContext();
298   if (!aContext.IsNull() && !aAISIO.IsNull()) {
299     // Check that the visualized shape is the same and the redisplay is not necessary
300     // Redisplay of AIS object leads to this object selection compute and the selection 
301     // in the browser is lost
302     // this check is not necessary anymore because the selection store/restore is realized
303     // before and after the values modification.
304     // Moreother, this check avoids customize and redisplay presentation if the presentable
305     // parameter is changed.
306     bool isEqualShapes = false;
307     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
308     if (aResult.get() != NULL) {
309       Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(aAISIO);
310       if (!aShapePrs.IsNull()) {
311         std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
312         if (aShapePtr.get()) {
313           const TopoDS_Shape& aOldShape = aShapePrs->Shape();
314           if (!aOldShape.IsNull())
315             isEqualShapes = aOldShape.IsEqual(aShapePtr->impl<TopoDS_Shape>());
316         }
317       }
318     }
319     // Customization of presentation
320     bool isCustomized = customizeObject(theObject);
321     #ifdef DEBUG_FEATURE_REDISPLAY
322       qDebug(QString("Redisplay: %1, isEqualShapes=%2, isCustomized=%3").
323         arg(!isEqualShapes || isCustomized).arg(isEqualShapes).arg(isCustomized).toStdString().c_str());
324     #endif
325     if (!isEqualShapes || isCustomized) {
326       /// if shapes are equal and presentation are customized, selection should be restored
327       bool aNeedToRestoreSelection = isEqualShapes && isCustomized;
328       if (aNeedToRestoreSelection)
329         myWorkshop->module()->storeSelection();
330
331       aContext->Redisplay(aAISIO, false);
332
333       if (aNeedToRestoreSelection)
334         myWorkshop->module()->restoreSelection();
335
336       aRedisplayed = true;
337       #ifdef DEBUG_FEATURE_REDISPLAY
338         qDebug("  Redisplay happens");
339       #endif
340       if (theUpdateViewer)
341         updateViewer();
342     }
343   }
344   return aRedisplayed;
345 }
346
347 void XGUI_Displayer::redisplayObjects()
348 {
349   // redisplay objects visualized in the viewer
350   static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
351   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
352   QObjectPtrList aDisplayed = myWorkshop->displayer()->displayedObjects();
353   QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
354   for (; anIt != aLast; anIt++) {
355     aECreator->sendUpdated(*anIt, EVENT_DISP);
356   }
357   Events_Loop::loop()->flush(EVENT_DISP);
358 }
359
360 void XGUI_Displayer::deactivate(ObjectPtr theObject, const bool theUpdateViewer)
361 {
362 #ifdef DEBUG_DEACTIVATE
363   QString anInfoStr = ModuleBase_Tools::objectInfo(theObject);
364   qDebug(QString("deactivate: myActiveSelectionModes[%1]: %2, objects = ").
365     arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
366     arg(anInfoStr).
367     toStdString().c_str());
368 #endif
369   Handle(AIS_InteractiveContext) aContext = AISContext();
370   if (!aContext.IsNull() && isVisible(theObject)) {
371     AISObjectPtr anObj = myResult2AISObjectMap[theObject];
372     Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
373
374     deactivateAIS(anAIS);
375     // the selection from the previous activation modes should be cleared manually (#26172)
376     aContext->LocalContext()->ClearOutdatedSelection(anAIS, true);
377     if (theUpdateViewer)
378       updateViewer();
379   }
380 }
381
382 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
383                                        const bool theUpdateViewer)
384 {
385   //Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
386   //if (!aTrihedron.IsNull())
387   //  deactivateAIS(aTrihedron);
388
389   QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
390   for (; anIt != aLast; anIt++) {
391     deactivate(*anIt, false);
392   }
393   //VSV It seems that there is no necessity to update viewer on deactivation
394   //if (theUpdateViewer)
395   //  updateViewer();
396 }
397
398 void XGUI_Displayer::getModesOfActivation(ObjectPtr theObject, QIntList& theModes)
399 {
400   Handle(AIS_InteractiveContext) aContext = AISContext();
401   if (aContext.IsNull() || !isVisible(theObject))
402     return;
403
404   AISObjectPtr aAISObj = getAISObject(theObject);
405
406   if (aAISObj.get() != NULL) {
407     Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
408     TColStd_ListOfInteger aTColModes;
409     aContext->ActivatedModes(anAISIO, aTColModes);
410     TColStd_ListIteratorOfListOfInteger itr( aTColModes );
411     for (; itr.More(); itr.Next() ) {
412       theModes.append(itr.Value());
413     }
414   }
415 }
416
417 int XGUI_Displayer::getSelectionMode(int theShapeType)
418 {
419   return (theShapeType >= TopAbs_SHAPE)? theShapeType : 
420     AIS_Shape::SelectionMode((TopAbs_ShapeEnum)theShapeType);
421 }
422
423 bool XGUI_Displayer::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
424 {
425   bool aVisible = false;
426   GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
427   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
428   if (aPrs.get() || aResult.get()) {
429     aVisible = theDisplayer->isVisible(theObject);
430     // compsolid is not visualized in the viewer, but should have presentation when all sub solids are
431     // visible. It is useful for highlight presentation where compsolid shape is selectable
432     if (!aVisible && aResult.get() && aResult->groupName() == ModelAPI_ResultCompSolid::group()) {
433       ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResult);
434       if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
435         bool anAllSubsVisible = aCompsolidResult->numberOfSubs() > 0;
436         for(int i = 0; i < aCompsolidResult->numberOfSubs() && anAllSubsVisible; i++) {
437           anAllSubsVisible = theDisplayer->isVisible(aCompsolidResult->subResult(i));
438         }
439         aVisible = anAllSubsVisible;
440       }
441     }
442   }
443   // it is possible that feature is presentable and has results, so we should check visibility
444   // of results if presentation is not shown (e.g. Sketch Circle/Arc features)
445   if (!aVisible) {
446     // check if all results of the feature are visible
447     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
448     std::list<ResultPtr> aResults = aFeature->results();
449     std::list<ResultPtr>::const_iterator aIt;
450     aVisible = !aResults.empty();
451     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
452       aVisible = aVisible && theDisplayer->isVisible(*aIt);
453     }
454   }
455   return aVisible;
456 }
457
458
459 void XGUI_Displayer::activateObjects(const QIntList& theModes, const QObjectPtrList& theObjList,
460                                      const bool theUpdateViewer)
461 {
462   // Convert shape types to selection types
463   QIntList aModes;
464   foreach(int aType, theModes) {
465     aModes.append(getSelectionMode(aType));
466   }
467
468 #ifdef DEBUG_ACTIVATE_OBJECTS
469   QStringList anInfo;
470   QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
471   for (; anIt != aLast; ++anIt) {
472     anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
473   }
474   QString anInfoStr = anInfo.join(", ");
475
476   qDebug(QString("activateObjects: aModes[%1] = %2, myActiveSelectionModes[%3] = %4, objects = %5").
477     arg(aModes.size()).arg(qIntListInfo(aModes)).
478     arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
479     arg(anInfoStr).
480     toStdString().c_str());
481 #endif
482   // In order to avoid doblications of selection modes
483   QIntList aNewModes;
484   foreach (int aMode, aModes) {
485     if (!aNewModes.contains(aMode))
486       aNewModes.append(aMode);
487   }
488   myActiveSelectionModes = aNewModes;
489   Handle(AIS_InteractiveContext) aContext = AISContext();
490   // Open local context if there is no one
491   if (aContext.IsNull() || !aContext->HasOpenedContext()) 
492     return;
493
494   //aContext->UseDisplayedObjects();
495   //myUseExternalObjects = true;
496
497   Handle(AIS_InteractiveObject) anAISIO;
498   AIS_ListOfInteractive aPrsList;
499   //if (aObjList.isEmpty())
500   //  return;
501   //else {
502   foreach(ObjectPtr aObj, theObjList) {
503     if (myResult2AISObjectMap.contains(aObj))
504       aPrsList.Append(myResult2AISObjectMap[aObj]->impl<Handle(AIS_InteractiveObject)>());
505   }
506   //}
507
508   // Add trihedron because it has to partisipate in selection
509   Handle(AIS_InteractiveObject) aTrihedron;
510   if (isTrihedronActive()) {
511     aTrihedron = getTrihedron();
512     if (!aTrihedron.IsNull() && aContext->IsDisplayed(aTrihedron))
513       aPrsList.Append(aTrihedron);
514   }
515   if (aPrsList.Extent() == 0)
516     return;
517
518   AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
519   bool isActivationChanged = false;
520   for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
521     anAISIO = aLIt.Value();
522     if (activate(anAISIO, myActiveSelectionModes, false))
523       isActivationChanged = true;
524   }
525   if (!aTrihedron.IsNull()) {
526     foreach(int aMode, myActiveSelectionModes)
527       aContext->SetSelectionSensitivity(aTrihedron, aMode, 20);
528   }
529   // VSV It seems that there is no necessity to update viewer on activation
530   //if (theUpdateViewer && isActivationChanged)
531   //  updateViewer();
532 }
533
534 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
535 {
536   Handle(AIS_InteractiveContext) aContext = AISContext();
537   if (aContext.IsNull() || !isVisible(theObject))
538     return false;
539     
540   AISObjectPtr anObj = myResult2AISObjectMap[theObject];
541   Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
542
543   TColStd_ListOfInteger aModes;
544   aContext->ActivatedModes(anAIS, aModes);
545   return aModes.Extent() > 0;
546 }
547 void XGUI_Displayer::setSelected(const  QList<ModuleBase_ViewerPrsPtr>& theValues, bool theUpdateViewer)
548 {
549   Handle(AIS_InteractiveContext) aContext = AISContext();
550   if (aContext.IsNull())
551     return;
552   if (aContext->HasOpenedContext()) {
553     aContext->UnhilightSelected(false);
554     aContext->ClearSelected(false);
555     foreach (ModuleBase_ViewerPrsPtr aPrs, theValues) {
556       const GeomShapePtr& aGeomShape = aPrs->shape();
557       if (aGeomShape.get() && !aGeomShape->isNull()) {
558         const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
559         aContext->AddOrRemoveSelected(aShape, false);
560       } else {
561         ObjectPtr anObject = aPrs->object();
562         ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
563         if (aResult.get() && isVisible(aResult)) {
564           AISObjectPtr anObj = myResult2AISObjectMap[aResult];
565           Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
566           if (!anAIS.IsNull()) {
567             // The methods are replaced in order to provide multi-selection, e.g. restore selection
568             // by activating multi selector widget. It also gives an advantage that the multi
569             // selection in OB gives multi-selection in the viewer
570             //aContext->SetSelected(anAIS, false);
571             // The selection in the context was cleared, so the method sets the objects are selected
572             aContext->AddOrRemoveSelected(anAIS, false);
573           }
574         }
575       }
576     }
577   } else {
578     aContext->UnhilightCurrents(false);
579     aContext->ClearCurrents(false);
580     foreach (ModuleBase_ViewerPrsPtr aPrs, theValues) {
581       ObjectPtr anObject = aPrs->object();
582       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
583       if (aResult.get() && isVisible(aResult)) {
584         AISObjectPtr anObj = myResult2AISObjectMap[aResult];
585         Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
586         if (!anAIS.IsNull())
587           aContext->SetCurrentObject(anAIS, false);
588       }
589     }
590   }
591   if (theUpdateViewer)
592     updateViewer();
593 }
594
595 void XGUI_Displayer::clearSelected()
596 {
597   Handle(AIS_InteractiveContext) aContext = AISContext();
598   if (!aContext.IsNull()) {
599     aContext->UnhilightCurrents(false);
600     aContext->ClearSelected();
601   }
602 }
603
604 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
605 {
606   bool aErased = false;
607   Handle(AIS_InteractiveContext) aContext = AISContext();
608   if (!aContext.IsNull()) {
609     foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
610       AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
611       // erase an object
612       Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
613       if (!anIO.IsNull()) {
614         emit beforeObjectErase(aObj, aAISObj);
615         aContext->Remove(anIO, false/*update viewer*/);
616         aErased = true;
617       }
618     }
619     if (theUpdateViewer)
620       updateViewer();
621   }
622   myResult2AISObjectMap.clear();
623 #ifdef DEBUG_DISPLAY
624   qDebug("eraseAll");
625   qDebug(getResult2AISObjectMapInfo().c_str());
626 #endif
627   return aErased;
628 }
629
630 void deactivateObject(Handle(AIS_InteractiveContext) theContext,
631                       Handle(AIS_InteractiveObject) theObject,
632                       const bool theClear = true)
633 {
634   if (!theObject.IsNull()) {
635     theContext->Deactivate(theObject);
636     //if (theClear) {
637       //theObject->ClearSelected();
638       //  theContext->LocalContext()->ClearOutdatedSelection(theObject, true);
639     //}
640   }
641 }
642
643 void XGUI_Displayer::deactivateTrihedron(const bool theUpdateViewer) const
644 {
645   Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
646   Handle(AIS_InteractiveContext) aContext = AISContext();
647   if (!aTrihedron.IsNull() && aContext->IsDisplayed(aTrihedron)) {
648     Handle(AIS_Trihedron) aTrie = Handle(AIS_Trihedron)::DownCast(aTrihedron);
649     deactivateObject(aContext, aTrie);
650
651     /// #1136 hidden axis are selected in sketch
652     /// workaround for Cascade: there is a crash in AIS_LocalContext::ClearOutdatedSelection
653     /// for Position AIS object in SelectionModes.
654     deactivateObject(aContext, aTrie->XAxis());
655     deactivateObject(aContext, aTrie->YAxis());
656     deactivateObject(aContext, aTrie->Axis());
657     deactivateObject(aContext, aTrie->Position());
658
659     deactivateObject(aContext, aTrie->XYPlane());
660     deactivateObject(aContext, aTrie->XZPlane());
661     deactivateObject(aContext, aTrie->YZPlane());
662
663     if (theUpdateViewer)
664       updateViewer();
665   }
666 }
667
668 Handle(AIS_InteractiveObject) XGUI_Displayer::getTrihedron() const
669 {
670   return myWorkshop->viewer()->trihedron();
671 }
672
673 void XGUI_Displayer::openLocalContext()
674 {
675   Handle(AIS_InteractiveContext) aContext = AISContext();
676   // Open local context if there is no one
677   if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
678     // Preserve selected objects
679     //AIS_ListOfInteractive aAisList;
680     //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
681     //  aAisList.Append(aContext->Current());
682
683     // get the filters from the global context and append them to the local context
684     // a list of filters in the global context is not cleared and should be cleared here
685     SelectMgr_ListOfFilter aFilters;
686     aFilters.Assign(aContext->Filters());
687     // it is important to remove the filters in the global context, because there is a code
688     // in the closeLocalContex, which restore the global context filters
689     aContext->RemoveFilters();
690
691     //aContext->ClearCurrents();
692     aContext->OpenLocalContext();
693     //deactivateTrihedron();
694     //aContext->NotUseDisplayedObjects();
695
696     //myUseExternalObjects = false;
697
698     SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
699     for (;aIt.More(); aIt.Next()) {
700       aContext->AddFilter(aIt.Value());
701     }
702     // Restore selection
703     //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
704     //for(; aIt2.More(); aIt2.Next()) {
705     //  aContext->SetSelected(aIt2.Value(), false);
706     //}
707   }
708 }
709
710 void XGUI_Displayer::closeLocalContexts(const bool theUpdateViewer)
711 {
712   Handle(AIS_InteractiveContext) aContext = AISContext();
713   if (!aContext.IsNull() && aContext->HasOpenedContext()) {
714     // Preserve selected objects
715     //AIS_ListOfInteractive aAisList;
716     //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
717     //  aAisList.Append(aContext->SelectedInteractive());
718
719     // get the filters from the local context and append them to the global context
720     // a list of filters in the local context is cleared
721     SelectMgr_ListOfFilter aFilters;
722     aFilters.Assign(aContext->Filters());
723
724     //aContext->ClearSelected();
725     aContext->CloseAllContexts(false);
726
727     // From the moment when the AIS_DS_Displayed flag is used in the Display of AIS object,
728     // this code is obsolete. It is temporaty commented and should be removed after
729     // the test campaign.
730     // Redisplay all object if they were displayed in localContext
731     /*Handle(AIS_InteractiveObject) aAISIO;
732     foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
733       aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
734       if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
735         aContext->Display(aAISIO, false);
736         aContext->SetDisplayMode(aAISIO, Shading, false);
737       }
738     }*/
739
740     // Append the filters from the local selection in the global selection context
741     SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
742     for (;aIt.More(); aIt.Next()) {
743       Handle(SelectMgr_Filter) aFilter = aIt.Value();
744       aContext->AddFilter(aFilter);
745     }
746
747     if (theUpdateViewer)
748       updateViewer();
749     //myUseExternalObjects = false;
750
751     // Restore selection
752     //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
753     //for(; aIt2.More(); aIt2.Next()) {
754     //  if (aContext->IsDisplayed(aIt2.Value()))
755     //    aContext->SetCurrentObject(aIt2.Value(), false);
756     //}
757   }
758 }
759
760 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
761 {
762   AISObjectPtr anIO;
763   if (myResult2AISObjectMap.contains(theObject))
764     anIO = myResult2AISObjectMap[theObject];
765   return anIO;
766 }
767
768 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
769 {
770   Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
771   return getObject(aRefAIS);
772 }
773
774 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
775 {
776   ObjectPtr anObject;
777   foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
778     AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
779     Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
780     if (anAIS == theIO)
781       anObject = anObj;
782     if (anObject.get())
783       break;
784   }
785   if (!anObject.get()) {
786     std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
787     if (!theIO.IsNull()) {
788       anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
789     }
790     anObject = myWorkshop->module()->findPresentedObject(anAISObj);
791   }
792   return anObject;
793 }
794
795 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
796 {
797   bool aWasEnabled = isUpdateEnabled();
798   if (isEnabled)
799     myViewerBlockedRecursiveCount--;
800   else
801     myViewerBlockedRecursiveCount++;
802
803   if (myNeedUpdate && isUpdateEnabled()) {
804     updateViewer();
805     myNeedUpdate = false;
806   }
807   return aWasEnabled;
808 }
809
810 bool XGUI_Displayer::isUpdateEnabled() const
811 {
812   return myViewerBlockedRecursiveCount == 0;
813 }
814
815 void XGUI_Displayer::updateViewer() const
816 {
817   Handle(AIS_InteractiveContext) aContext = AISContext();
818   if (!aContext.IsNull() && isUpdateEnabled()) {
819     myWorkshop->viewer()->Zfitall();
820     aContext->UpdateCurrentViewer();
821   } else {
822     myNeedUpdate = true;
823   }
824 }
825
826 void XGUI_Displayer::activateAIS(const Handle(AIS_InteractiveObject)& theIO,
827                                  const int theMode, const bool theUpdateViewer) const
828 {
829   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
830   if (!aContext.IsNull()) {
831     aContext->Activate(theIO, theMode, false);
832
833 #ifdef DEBUG_ACTIVATE_AIS
834     ObjectPtr anObject = getObject(theIO);
835     anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
836     qDebug(QString("activateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
837 #endif
838     if (theUpdateViewer)
839       updateViewer();
840   }
841 }
842
843 void XGUI_Displayer::deactivateAIS(const Handle(AIS_InteractiveObject)& theIO, const int theMode) const
844 {
845   Handle(AIS_InteractiveContext) aContext = AISContext();
846   if (!aContext.IsNull()) {
847     if (theMode == -1)
848       aContext->Deactivate(theIO);
849     else
850       aContext->Deactivate(theIO, theMode);
851
852 #ifdef DEBUG_DEACTIVATE_AIS
853     ObjectPtr anObject = getObject(theIO);
854     anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
855     qDebug(QString("deactivateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
856 #endif
857   }
858 }
859
860 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
861 {
862   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
863   if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
864     aContext->OpenLocalContext();
865     if (!isTrihedronActive())
866       deactivateTrihedron(true);
867     aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
868     aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
869   }
870   return aContext;
871 }
872
873 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
874 {
875   Handle(AIS_InteractiveContext) aContext = AISContext();
876   if (!aContext.IsNull() && myAndFilter.IsNull()) {
877     myAndFilter = new SelectMgr_AndFilter();
878     aContext->AddFilter(myAndFilter);
879   }
880   return myAndFilter;
881 }
882
883 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
884                                 bool theUpdateViewer)
885 {
886   bool aDisplayed = false;
887   Handle(AIS_InteractiveContext) aContext = AISContext();
888   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
889   if (!aContext.IsNull() && !anAISIO.IsNull()) {
890     aContext->Display(anAISIO, 0/*wireframe*/, 0, false/*update viewer*/, true, AIS_DS_Displayed);
891     aDisplayed = true;
892     aContext->Deactivate(anAISIO);
893     aContext->Load(anAISIO);
894     if (toActivateInSelectionModes) {
895       if (aContext->HasOpenedContext()) {
896         if (myActiveSelectionModes.size() == 0)
897           activateAIS(anAISIO, 0, theUpdateViewer);
898         else {
899           foreach(int aMode, myActiveSelectionModes) {
900             activateAIS(anAISIO, aMode, theUpdateViewer);
901           }
902         }
903       }
904     }
905     if (theUpdateViewer)
906       updateViewer();
907   }
908   return aDisplayed;
909 }
910
911 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
912 {
913   bool aErased = false;
914   Handle(AIS_InteractiveContext) aContext = AISContext();
915   if (!aContext.IsNull()) {
916     Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
917     if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
918       aContext->Remove(anAISIO, false/*update viewer*/);
919       aErased = true;
920     }
921   }
922   if (aErased && theUpdateViewer)
923     updateViewer();
924   return aErased;
925 }
926
927
928 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
929 {
930   if (theMode == NoMode)
931     return;
932
933   Handle(AIS_InteractiveContext) aContext = AISContext();
934   if (aContext.IsNull())
935     return;
936
937   AISObjectPtr aAISObj = getAISObject(theObject);
938   if (!aAISObj)
939     return;
940
941   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
942   aContext->SetDisplayMode(aAISIO, theMode, false);
943   // Redisplay in order to update new mode because it could be not computed before
944   if (theUpdateViewer)
945     updateViewer();
946 }
947
948 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
949 {
950   Handle(AIS_InteractiveContext) aContext = AISContext();
951   if (aContext.IsNull())
952     return NoMode;
953
954   AISObjectPtr aAISObj = getAISObject(theObject);
955   if (!aAISObj)
956     return NoMode;
957
958   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
959   return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
960 }
961
962 void XGUI_Displayer::deactivateSelectionFilters()
963 {
964   Handle(AIS_InteractiveContext) aContext = AISContext();
965   if (!myAndFilter.IsNull()) {
966     bool aFound = false;
967     if (!aContext.IsNull()) {
968       const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
969       SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
970       for (; anIt.More() && !aFound; anIt.Next()) {
971         Handle(SelectMgr_Filter) aFilter = anIt.Value();
972         aFound = aFilter == myAndFilter;
973       }
974       if (aFound)
975         aContext->RemoveFilter(myAndFilter);
976     }
977     myAndFilter.Nullify();
978   }
979 }
980
981 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
982 {
983   Handle(AIS_InteractiveContext) aContext = AISContext();
984   if (aContext.IsNull() || hasSelectionFilter(theFilter))
985     return;
986
987   Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
988   if (!aCompositeFilter.IsNull()) {
989     aCompositeFilter->Add(theFilter);
990 #ifdef DEBUG_SELECTION_FILTERS
991     int aCount = aCompositeFilter->StoredFilters().Extent();
992     qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
993 #endif
994   }
995 }
996
997 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
998 {
999   Handle(AIS_InteractiveContext) aContext = AISContext();
1000   if (aContext.IsNull())
1001     return;
1002
1003   Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
1004   if (!aCompositeFilter.IsNull() && aCompositeFilter->IsIn(theFilter)) {
1005     aCompositeFilter->Remove(theFilter);
1006 #ifdef DEBUG_SELECTION_FILTERS
1007     int aCount = aCompositeFilter->StoredFilters().Extent();
1008     qDebug(QString("removeSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
1009 #endif
1010   }
1011 }
1012
1013 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
1014 {
1015   bool aFilterFound = false;
1016
1017   Handle(AIS_InteractiveContext) aContext = AISContext();
1018   if (aContext.IsNull())
1019     return aFilterFound;
1020   const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
1021   SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
1022   for (; aIt.More() && !aFilterFound; aIt.Next()) {
1023     if (theFilter.Access() == aIt.Value().Access())
1024       aFilterFound = true;
1025   }
1026   Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
1027   if (!aCompositeFilter.IsNull()) {
1028     const SelectMgr_ListOfFilter& aStoredFilters = aCompositeFilter->StoredFilters();
1029     for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
1030       if (theFilter.Access() == aIt.Value().Access())
1031         aFilterFound = true;
1032     }
1033   }
1034   return aFilterFound;
1035 }
1036
1037 void XGUI_Displayer::removeFilters()
1038 {
1039   Handle(AIS_InteractiveContext) aContext = AISContext();
1040   if (aContext.IsNull())
1041     return;
1042
1043   Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
1044   if (!aCompositeFilter.IsNull())
1045     aCompositeFilter->Clear();
1046 }
1047
1048 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
1049 {
1050   QObjectPtrList aDispList = myResult2AISObjectMap.keys();
1051   foreach(ObjectPtr aObj, aDispList) {
1052     if (!theList.contains(aObj))
1053       erase(aObj, false);
1054   }
1055   foreach(ObjectPtr aObj, theList) {
1056     if (!isVisible(aObj))
1057       display(aObj, false);
1058   }
1059   updateViewer();
1060 }
1061
1062 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
1063
1064   if (!isVisible(theObject))
1065     return false;
1066
1067   AISObjectPtr aAISObj = getAISObject(theObject);
1068   if (aAISObj.get() == NULL)
1069     return false;
1070
1071   Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1072   return ::canBeShaded(anAIS, myWorkshop->module());
1073 }
1074
1075 bool XGUI_Displayer::activate(const Handle(AIS_InteractiveObject)& theIO,
1076                               const QIntList& theModes,
1077                               const bool theUpdateViewer) const
1078 {
1079   Handle(AIS_InteractiveContext) aContext = AISContext();
1080   if (aContext.IsNull() || theIO.IsNull())
1081     return false;
1082   
1083   bool isActivationChanged = false;
1084   // deactivate object in all modes, which are not in the list of activation
1085   // It seems that after the IO deactivation the selected state of the IO's owners
1086   // is modified in OCC(version: 6.8.0) and the selection of the object later is lost.
1087   // By this reason, the number of the IO deactivate is decreased and the object is deactivated
1088   // only if there is a difference in the current modes and the parameters modes.
1089   // If the selection problem happens again, it is possible to write a test scenario and create
1090   // a bug. The bug steps are the following:
1091   // Create two IO, activate them in 5 modes, select the first IO, deactivate 3 modes for both,
1092   // with clicked SHIFT select the second object. The result is the selection of the first IO is lost.
1093   TColStd_ListOfInteger aTColModes;
1094   aContext->ActivatedModes(theIO, aTColModes);
1095   TColStd_ListIteratorOfListOfInteger itr( aTColModes );
1096   QIntList aModesActivatedForIO;
1097   bool isDeactivated = false;
1098   for (; itr.More(); itr.Next() ) {
1099     Standard_Integer aMode = itr.Value();
1100     if (!theModes.contains(aMode)) {
1101       deactivateAIS(theIO, aMode);
1102       isDeactivated = true;
1103     }
1104     else {
1105       aModesActivatedForIO.append(aMode);
1106     }
1107   }
1108   if (isDeactivated) {
1109     // the selection from the previous activation modes should be cleared manually (#26172)
1110     theIO->ClearSelected();
1111     aContext->LocalContext()->ClearOutdatedSelection(theIO, true);
1112     // For performance issues
1113     //if (theUpdateViewer)
1114     //  updateViewer();
1115     isActivationChanged = true;
1116   }
1117
1118   // loading the interactive object allowing the decomposition
1119   if (aTColModes.IsEmpty()) {
1120     aContext->Load(theIO, -1, true);
1121   }
1122
1123   // trihedron AIS check should be after the AIS loading.
1124   // If it is not loaded, it is steel selectable in the viewer.
1125   Handle(AIS_Trihedron) aTrihedron;
1126   if (!isTrihedronActive())
1127     aTrihedron = Handle(AIS_Trihedron)::DownCast(theIO);
1128   if (aTrihedron.IsNull()) {
1129       // In order to clear active modes list
1130     if (theModes.size() == 0) {
1131       activateAIS(theIO, 0, theUpdateViewer);
1132     } else {
1133       foreach(int aMode, theModes) {
1134         if (!aModesActivatedForIO.contains(aMode)) {
1135           activateAIS(theIO, aMode, theUpdateViewer);
1136           isActivationChanged = true;
1137         }
1138       }
1139     }
1140   }
1141   return isActivationChanged;
1142 }
1143
1144 bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
1145 {
1146   AISObjectPtr anAISObj = getAISObject(theObject);
1147   // correct the result's color it it has the attribute
1148   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1149
1150   // Customization of presentation
1151   GeomCustomPrsPtr aCustomPrs;
1152   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1153   if (aFeature.get() != NULL) {
1154     GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
1155     if (aCustPrs.get() != NULL)
1156       aCustomPrs = aCustPrs;
1157   }
1158   if (aCustomPrs.get() == NULL) {
1159     GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
1160     // we ignore presentable not customized objects
1161     if (aPrs.get() == NULL)
1162       aCustomPrs = myCustomPrs;
1163   }
1164   bool isCustomized = aCustomPrs.get() &&
1165                       aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
1166   return isCustomized;
1167 }
1168
1169
1170 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject, const QColor& theColor, bool theUpdateViewer)
1171 {
1172   if (!isVisible(theObject))
1173     return Qt::black;
1174
1175   AISObjectPtr anAISObj = getAISObject(theObject);
1176   int aR, aG, aB;
1177   anAISObj->getColor(aR, aG, aB);
1178   anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
1179   if (theUpdateViewer)
1180     updateViewer();
1181   return QColor(aR, aG, aB);
1182 }
1183
1184 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
1185 {
1186   myResult2AISObjectMap[theObject] = theAIS;
1187
1188 #ifdef DEBUG_DISPLAY
1189   std::ostringstream aPtrStr;
1190   aPtrStr << theObject.get();
1191   qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
1192   qDebug(getResult2AISObjectMapInfo().c_str());
1193 #endif
1194 }
1195
1196 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
1197 {
1198   QStringList aContent;
1199   foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
1200     AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
1201     std::ostringstream aPtrStr;
1202     aPtrStr << "aObj = " << aObj.get() << ":";
1203     aPtrStr << "anAIS = " << aAISObj.get() << ":";
1204     aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
1205     
1206     aContent.append(aPtrStr.str().c_str());
1207   }
1208   return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
1209                                             arg(aContent.join("\n")).toStdString().c_str();
1210 }
1211
1212 void XGUI_Displayer::activateTrihedron(bool theIsActive) 
1213 {  
1214   myIsTrihedronActive = theIsActive; 
1215   if (!myIsTrihedronActive) {
1216     deactivateTrihedron(true);
1217   }
1218 }
1219
1220 void XGUI_Displayer::displayTrihedron(bool theToDisplay) const
1221 {
1222   Handle(AIS_InteractiveContext) aContext = AISContext();
1223   if (aContext.IsNull())
1224     return;
1225
1226   Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
1227
1228   if (theToDisplay) {
1229     if (!aContext->IsDisplayed(aTrihedron))
1230       aContext->Display(aTrihedron,
1231                         0 /*wireframe*/,
1232                         -1 /* selection mode */,
1233                         Standard_True /* update viewer*/,
1234                         Standard_False /* allow decomposition */,
1235                         AIS_DS_Displayed /* xdisplay status */);
1236
1237     if (!isTrihedronActive())
1238       deactivateTrihedron(false);
1239     else
1240       activate(aTrihedron, myActiveSelectionModes, false);
1241   } else {
1242     deactivateTrihedron(false);
1243     //aContext->LocalContext()->ClearOutdatedSelection(aTrihedron, true);
1244     // the selection from the previous activation modes should be cleared manually (#26172)
1245
1246     aContext->Erase(aTrihedron);
1247   }
1248
1249   updateViewer();
1250 }
1251
1252 QIntList XGUI_Displayer::activeSelectionModes() const 
1253
1254   QIntList aModes;
1255   foreach (int aMode, myActiveSelectionModes) {
1256     // aMode < 9 is a Shape Enum values
1257     aModes << ((aMode < 9)? AIS_Shape::SelectionType(aMode) : aMode);
1258   }
1259   return aModes; 
1260 }