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