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