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