Salome HOME
978d6154936421e9f0332bbd4520b7a4e65fb3b4
[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           anAIS->setImpl(new Handle(AIS_InteractiveObject)(new ModuleBase_ResultPrs(aResult)));
165           //anAIS->createShape(aShapePtr);
166           isShading = true;
167         }
168 #ifdef DEBUG_COMPOSILID_DISPLAY
169         } // close else
170 #endif
171       }
172     }
173     if (anAIS)
174       aDisplayed = display(theObject, anAIS, isShading, theUpdateViewer);
175   }
176   return aDisplayed;
177 }
178
179 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS)
180 {
181   Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
182   if (!aShapePrs.IsNull()) {
183     TopoDS_Shape aShape = aShapePrs->Shape();
184     if (aShape.IsNull())
185       return false;
186     TopAbs_ShapeEnum aType = aShape.ShapeType();
187     if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
188       return false;
189     else {
190       // Check that the presentation is not a sketch
191       Handle(ModuleBase_ResultPrs) aPrs = Handle(ModuleBase_ResultPrs)::DownCast(theAIS);
192       if (!aPrs.IsNull()) 
193         return !aPrs->isSketchMode();
194       return true;
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 TopoDS_Shape& aShape = aPrs.shape();
554       if (!aShape.IsNull()) {
555         aContext->AddOrRemoveSelected(aShape, false);
556       } else {
557         ObjectPtr anObject = aPrs.object();
558         ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
559         if (aResult.get() && isVisible(aResult)) {
560           AISObjectPtr anObj = myResult2AISObjectMap[aResult];
561           Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
562           if (!anAIS.IsNull()) {
563             // The methods are replaced in order to provide multi-selection, e.g. restore selection
564             // by activating multi selector widget. It also gives an advantage that the multi
565             // selection in OB gives multi-selection in the viewer
566             //aContext->SetSelected(anAIS, false);
567             // The selection in the context was cleared, so the method sets the objects are selected
568             aContext->AddOrRemoveSelected(anAIS, false);
569           }
570         }
571       }
572     }
573   } else {
574     aContext->UnhilightCurrents(false);
575     aContext->ClearCurrents(false);
576     foreach (ModuleBase_ViewerPrs aPrs, theValues) {
577       ObjectPtr anObject = aPrs.object();
578       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
579       if (aResult.get() && isVisible(aResult)) {
580         AISObjectPtr anObj = myResult2AISObjectMap[aResult];
581         Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
582         if (!anAIS.IsNull())
583           aContext->SetCurrentObject(anAIS, false);
584       }
585     }
586   }
587   if (theUpdateViewer)
588     updateViewer();
589 }
590
591 void XGUI_Displayer::clearSelected()
592 {
593   Handle(AIS_InteractiveContext) aContext = AISContext();
594   if (!aContext.IsNull()) {
595     aContext->UnhilightCurrents(false);
596     aContext->ClearSelected();
597   }
598 }
599
600 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
601 {
602   bool aErased = false;
603   Handle(AIS_InteractiveContext) aContext = AISContext();
604   if (!aContext.IsNull()) {
605     foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
606       AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
607       // erase an object
608       Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
609       if (!anIO.IsNull()) {
610         emit beforeObjectErase(aObj, aAISObj);
611         aContext->Remove(anIO, false/*update viewer*/);
612         aErased = true;
613       }
614     }
615     if (theUpdateViewer)
616       updateViewer();
617   }
618   myResult2AISObjectMap.clear();
619 #ifdef DEBUG_DISPLAY
620   qDebug("eraseAll");
621   qDebug(getResult2AISObjectMapInfo().c_str());
622 #endif
623   return aErased;
624 }
625
626 void deactivateObject(Handle(AIS_InteractiveContext) theContext,
627                       Handle(AIS_InteractiveObject) theObject,
628                       const bool theClear = true)
629 {
630   if (!theObject.IsNull()) {
631     theContext->Deactivate(theObject);
632     //if (theClear) {
633       //theObject->ClearSelected();
634       //  theContext->LocalContext()->ClearOutdatedSelection(theObject, true);
635     //}
636   }
637 }
638
639 void XGUI_Displayer::deactivateTrihedron(const bool theUpdateViewer) const
640 {
641   Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
642   Handle(AIS_InteractiveContext) aContext = AISContext();
643   if (!aTrihedron.IsNull() && aContext->IsDisplayed(aTrihedron)) {
644     Handle(AIS_Trihedron) aTrie = Handle(AIS_Trihedron)::DownCast(aTrihedron);
645     deactivateObject(aContext, aTrie);
646
647     /// #1136 hidden axis are selected in sketch
648     /// workaround for Cascade: there is a crash in AIS_LocalContext::ClearOutdatedSelection
649     /// for Position AIS object in SelectionModes.
650     deactivateObject(aContext, aTrie->XAxis());
651     deactivateObject(aContext, aTrie->YAxis());
652     deactivateObject(aContext, aTrie->Axis());
653     deactivateObject(aContext, aTrie->Position());
654
655     deactivateObject(aContext, aTrie->XYPlane());
656     deactivateObject(aContext, aTrie->XZPlane());
657     deactivateObject(aContext, aTrie->YZPlane());
658
659     if (theUpdateViewer)
660       updateViewer();
661   }
662 }
663
664 Handle(AIS_InteractiveObject) XGUI_Displayer::getTrihedron() const
665 {
666   return myWorkshop->viewer()->trihedron();
667 }
668
669 void XGUI_Displayer::openLocalContext()
670 {
671   Handle(AIS_InteractiveContext) aContext = AISContext();
672   // Open local context if there is no one
673   if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
674     // Preserve selected objects
675     //AIS_ListOfInteractive aAisList;
676     //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
677     //  aAisList.Append(aContext->Current());
678
679     // get the filters from the global context and append them to the local context
680     // a list of filters in the global context is not cleared and should be cleared here
681     SelectMgr_ListOfFilter aFilters;
682     aFilters.Assign(aContext->Filters());
683     // it is important to remove the filters in the global context, because there is a code
684     // in the closeLocalContex, which restore the global context filters
685     aContext->RemoveFilters();
686
687     //aContext->ClearCurrents();
688     aContext->OpenLocalContext();
689     //deactivateTrihedron();
690     //aContext->NotUseDisplayedObjects();
691
692     //myUseExternalObjects = false;
693
694     SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
695     for (;aIt.More(); aIt.Next()) {
696       aContext->AddFilter(aIt.Value());
697     }
698     // Restore selection
699     //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
700     //for(; aIt2.More(); aIt2.Next()) {
701     //  aContext->SetSelected(aIt2.Value(), false);
702     //}
703   }
704 }
705
706 void XGUI_Displayer::closeLocalContexts(const bool theUpdateViewer)
707 {
708   Handle(AIS_InteractiveContext) aContext = AISContext();
709   if (!aContext.IsNull() && aContext->HasOpenedContext()) {
710     // Preserve selected objects
711     //AIS_ListOfInteractive aAisList;
712     //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
713     //  aAisList.Append(aContext->SelectedInteractive());
714
715     // get the filters from the local context and append them to the global context
716     // a list of filters in the local context is cleared
717     SelectMgr_ListOfFilter aFilters;
718     aFilters.Assign(aContext->Filters());
719
720     //aContext->ClearSelected();
721     aContext->CloseAllContexts(false);
722
723     // From the moment when the AIS_DS_Displayed flag is used in the Display of AIS object,
724     // this code is obsolete. It is temporaty commented and should be removed after
725     // the test campaign.
726     // Redisplay all object if they were displayed in localContext
727     /*Handle(AIS_InteractiveObject) aAISIO;
728     foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
729       aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
730       if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
731         aContext->Display(aAISIO, false);
732         aContext->SetDisplayMode(aAISIO, Shading, false);
733       }
734     }*/
735
736     // Append the filters from the local selection in the global selection context
737     SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
738     for (;aIt.More(); aIt.Next()) {
739       Handle(SelectMgr_Filter) aFilter = aIt.Value();
740       aContext->AddFilter(aFilter);
741     }
742
743     if (theUpdateViewer)
744       updateViewer();
745     //myUseExternalObjects = false;
746
747     // Restore selection
748     //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
749     //for(; aIt2.More(); aIt2.Next()) {
750     //  if (aContext->IsDisplayed(aIt2.Value()))
751     //    aContext->SetCurrentObject(aIt2.Value(), false);
752     //}
753   }
754 }
755
756 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
757 {
758   AISObjectPtr anIO;
759   if (myResult2AISObjectMap.contains(theObject))
760     anIO = myResult2AISObjectMap[theObject];
761   return anIO;
762 }
763
764 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
765 {
766   Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
767   return getObject(aRefAIS);
768 }
769
770 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
771 {
772   ObjectPtr anObject;
773   foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
774     AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
775     Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
776     if (anAIS == theIO)
777       anObject = anObj;
778     if (anObject.get())
779       break;
780   }
781   if (!anObject.get()) {
782     std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
783     if (!theIO.IsNull()) {
784       anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
785     }
786     anObject = myWorkshop->module()->findPresentedObject(anAISObj);
787   }
788   return anObject;
789 }
790
791 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
792 {
793   bool aWasEnabled = isUpdateEnabled();
794   if (isEnabled)
795     myViewerBlockedRecursiveCount--;
796   else
797     myViewerBlockedRecursiveCount++;
798
799   if (myNeedUpdate && isUpdateEnabled()) {
800     updateViewer();
801     myNeedUpdate = false;
802   }
803   return aWasEnabled;
804 }
805
806 bool XGUI_Displayer::isUpdateEnabled() const
807 {
808   return myViewerBlockedRecursiveCount == 0;
809 }
810
811 void XGUI_Displayer::updateViewer() const
812 {
813   Handle(AIS_InteractiveContext) aContext = AISContext();
814   if (!aContext.IsNull() && isUpdateEnabled()) {
815     myWorkshop->viewer()->Zfitall();
816     aContext->UpdateCurrentViewer();
817   } else {
818     myNeedUpdate = true;
819   }
820 }
821
822 void XGUI_Displayer::activateAIS(const Handle(AIS_InteractiveObject)& theIO,
823                                  const int theMode, const bool theUpdateViewer) const
824 {
825   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
826   if (!aContext.IsNull()) {
827     aContext->Activate(theIO, theMode, false);
828
829 #ifdef DEBUG_ACTIVATE_AIS
830     ObjectPtr anObject = getObject(theIO);
831     anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
832     qDebug(QString("activateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
833 #endif
834     if (theUpdateViewer)
835       updateViewer();
836   }
837 }
838
839 void XGUI_Displayer::deactivateAIS(const Handle(AIS_InteractiveObject)& theIO, const int theMode) const
840 {
841   Handle(AIS_InteractiveContext) aContext = AISContext();
842   if (!aContext.IsNull()) {
843     if (theMode == -1)
844       aContext->Deactivate(theIO);
845     else
846       aContext->Deactivate(theIO, theMode);
847
848 #ifdef DEBUG_DEACTIVATE_AIS
849     ObjectPtr anObject = getObject(theIO);
850     anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
851     qDebug(QString("deactivateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
852 #endif
853   }
854 }
855
856 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
857 {
858   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
859   if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
860     aContext->OpenLocalContext();
861     if (!isTrihedronActive())
862       deactivateTrihedron(true);
863     aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
864     aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
865   }
866   return aContext;
867 }
868
869 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
870 {
871   Handle(AIS_InteractiveContext) aContext = AISContext();
872   if (!aContext.IsNull() && myAndFilter.IsNull()) {
873     myAndFilter = new SelectMgr_AndFilter();
874     aContext->AddFilter(myAndFilter);
875   }
876   return myAndFilter;
877 }
878
879 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
880                                 bool theUpdateViewer)
881 {
882   bool aDisplayed = false;
883   Handle(AIS_InteractiveContext) aContext = AISContext();
884   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
885   if (!aContext.IsNull() && !anAISIO.IsNull()) {
886     aContext->Display(anAISIO, 0/*wireframe*/, 0, false/*update viewer*/, true, AIS_DS_Displayed);
887     aDisplayed = true;
888     aContext->Deactivate(anAISIO);
889     aContext->Load(anAISIO);
890     if (toActivateInSelectionModes) {
891       if (aContext->HasOpenedContext()) {
892         if (myActiveSelectionModes.size() == 0)
893           activateAIS(anAISIO, 0, theUpdateViewer);
894         else {
895           foreach(int aMode, myActiveSelectionModes) {
896             activateAIS(anAISIO, aMode, theUpdateViewer);
897           }
898         }
899       }
900     }
901     if (theUpdateViewer)
902       updateViewer();
903   }
904   return aDisplayed;
905 }
906
907 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
908 {
909   bool aErased = false;
910   Handle(AIS_InteractiveContext) aContext = AISContext();
911   if (!aContext.IsNull()) {
912     Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
913     if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
914       aContext->Remove(anAISIO, false/*update viewer*/);
915       aErased = true;
916     }
917   }
918   if (aErased && theUpdateViewer)
919     updateViewer();
920   return aErased;
921 }
922
923
924 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
925 {
926   if (theMode == NoMode)
927     return;
928
929   Handle(AIS_InteractiveContext) aContext = AISContext();
930   if (aContext.IsNull())
931     return;
932
933   AISObjectPtr aAISObj = getAISObject(theObject);
934   if (!aAISObj)
935     return;
936
937   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
938   aContext->SetDisplayMode(aAISIO, theMode, false);
939   // Redisplay in order to update new mode because it could be not computed before
940   if (theUpdateViewer)
941     updateViewer();
942 }
943
944 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
945 {
946   Handle(AIS_InteractiveContext) aContext = AISContext();
947   if (aContext.IsNull())
948     return NoMode;
949
950   AISObjectPtr aAISObj = getAISObject(theObject);
951   if (!aAISObj)
952     return NoMode;
953
954   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
955   return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
956 }
957
958 void XGUI_Displayer::deactivateSelectionFilters()
959 {
960   Handle(AIS_InteractiveContext) aContext = AISContext();
961   if (!myAndFilter.IsNull()) {
962     bool aFound = false;
963     if (!aContext.IsNull()) {
964       const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
965       SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
966       for (; anIt.More() && !aFound; anIt.Next()) {
967         Handle(SelectMgr_Filter) aFilter = anIt.Value();
968         aFound = aFilter == myAndFilter;
969       }
970       if (aFound)
971         aContext->RemoveFilter(myAndFilter);
972     }
973     myAndFilter.Nullify();
974   }
975 }
976
977 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
978 {
979   Handle(AIS_InteractiveContext) aContext = AISContext();
980   if (aContext.IsNull() || hasSelectionFilter(theFilter))
981     return;
982
983   Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
984   if (!aCompositeFilter.IsNull()) {
985     aCompositeFilter->Add(theFilter);
986 #ifdef DEBUG_SELECTION_FILTERS
987     int aCount = aCompositeFilter->StoredFilters().Extent();
988     qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
989 #endif
990   }
991 }
992
993 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
994 {
995   Handle(AIS_InteractiveContext) aContext = AISContext();
996   if (aContext.IsNull())
997     return;
998
999   Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
1000   if (!aCompositeFilter.IsNull() && aCompositeFilter->IsIn(theFilter)) {
1001     aCompositeFilter->Remove(theFilter);
1002 #ifdef DEBUG_SELECTION_FILTERS
1003     int aCount = aCompositeFilter->StoredFilters().Extent();
1004     qDebug(QString("removeSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
1005 #endif
1006   }
1007 }
1008
1009 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
1010 {
1011   bool aFilterFound = false;
1012
1013   Handle(AIS_InteractiveContext) aContext = AISContext();
1014   if (aContext.IsNull())
1015     return aFilterFound;
1016   const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
1017   SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
1018   for (; aIt.More() && !aFilterFound; aIt.Next()) {
1019     if (theFilter.Access() == aIt.Value().Access())
1020       aFilterFound = true;
1021   }
1022   Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
1023   if (!aCompositeFilter.IsNull()) {
1024     const SelectMgr_ListOfFilter& aStoredFilters = aCompositeFilter->StoredFilters();
1025     for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
1026       if (theFilter.Access() == aIt.Value().Access())
1027         aFilterFound = true;
1028     }
1029   }
1030   return aFilterFound;
1031 }
1032
1033 void XGUI_Displayer::removeFilters()
1034 {
1035   Handle(AIS_InteractiveContext) aContext = AISContext();
1036   if (aContext.IsNull())
1037     return;
1038
1039   Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
1040   if (!aCompositeFilter.IsNull())
1041     aCompositeFilter->Clear();
1042 }
1043
1044 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
1045 {
1046   QObjectPtrList aDispList = myResult2AISObjectMap.keys();
1047   foreach(ObjectPtr aObj, aDispList) {
1048     if (!theList.contains(aObj))
1049       erase(aObj, false);
1050   }
1051   foreach(ObjectPtr aObj, theList) {
1052     if (!isVisible(aObj))
1053       display(aObj, false);
1054   }
1055   updateViewer();
1056 }
1057
1058 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
1059
1060   if (!isVisible(theObject))
1061     return false;
1062
1063   AISObjectPtr aAISObj = getAISObject(theObject);
1064   if (aAISObj.get() == NULL)
1065     return false;
1066
1067   Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1068   return ::canBeShaded(anAIS);
1069 }
1070
1071 bool XGUI_Displayer::activate(const Handle(AIS_InteractiveObject)& theIO,
1072                               const QIntList& theModes,
1073                               const bool theUpdateViewer) const
1074 {
1075   Handle(AIS_InteractiveContext) aContext = AISContext();
1076   if (aContext.IsNull() || theIO.IsNull())
1077     return false;
1078   
1079   bool isActivationChanged = false;
1080   // deactivate object in all modes, which are not in the list of activation
1081   // It seems that after the IO deactivation the selected state of the IO's owners
1082   // is modified in OCC(version: 6.8.0) and the selection of the object later is lost.
1083   // By this reason, the number of the IO deactivate is decreased and the object is deactivated
1084   // only if there is a difference in the current modes and the parameters modes.
1085   // If the selection problem happens again, it is possible to write a test scenario and create
1086   // a bug. The bug steps are the following:
1087   // Create two IO, activate them in 5 modes, select the first IO, deactivate 3 modes for both,
1088   // with clicked SHIFT select the second object. The result is the selection of the first IO is lost.
1089   TColStd_ListOfInteger aTColModes;
1090   aContext->ActivatedModes(theIO, aTColModes);
1091   TColStd_ListIteratorOfListOfInteger itr( aTColModes );
1092   QIntList aModesActivatedForIO;
1093   bool isDeactivated = false;
1094   for (; itr.More(); itr.Next() ) {
1095     Standard_Integer aMode = itr.Value();
1096     if (!theModes.contains(aMode)) {
1097       deactivateAIS(theIO, aMode);
1098       isDeactivated = true;
1099     }
1100     else {
1101       aModesActivatedForIO.append(aMode);
1102     }
1103   }
1104   if (isDeactivated) {
1105     // the selection from the previous activation modes should be cleared manually (#26172)
1106     theIO->ClearSelected();
1107     aContext->LocalContext()->ClearOutdatedSelection(theIO, true);
1108     // For performance issues
1109     //if (theUpdateViewer)
1110     //  updateViewer();
1111     isActivationChanged = true;
1112   }
1113
1114   // loading the interactive object allowing the decomposition
1115   if (aTColModes.IsEmpty()) {
1116     aContext->Load(theIO, -1, true);
1117   }
1118
1119   // trihedron AIS check should be after the AIS loading.
1120   // If it is not loaded, it is steel selectable in the viewer.
1121   Handle(AIS_Trihedron) aTrihedron;
1122   if (!isTrihedronActive())
1123     aTrihedron = Handle(AIS_Trihedron)::DownCast(theIO);
1124   if (aTrihedron.IsNull()) {
1125       // In order to clear active modes list
1126     if (theModes.size() == 0) {
1127       activateAIS(theIO, 0, theUpdateViewer);
1128     } else {
1129       foreach(int aMode, theModes) {
1130         if (!aModesActivatedForIO.contains(aMode)) {
1131           activateAIS(theIO, aMode, theUpdateViewer);
1132           isActivationChanged = true;
1133         }
1134       }
1135     }
1136   }
1137   return isActivationChanged;
1138 }
1139
1140 bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
1141 {
1142   AISObjectPtr anAISObj = getAISObject(theObject);
1143   // correct the result's color it it has the attribute
1144   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1145
1146   // Customization of presentation
1147   GeomCustomPrsPtr aCustomPrs;
1148   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1149   if (aFeature.get() != NULL) {
1150     GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
1151     if (aCustPrs.get() != NULL)
1152       aCustomPrs = aCustPrs;
1153   }
1154   if (aCustomPrs.get() == NULL) {
1155     GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
1156     // we ignore presentable not customized objects
1157     if (aPrs.get() == NULL)
1158       aCustomPrs = myCustomPrs;
1159   }
1160   bool isCustomized = aCustomPrs.get() &&
1161                       aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
1162   return isCustomized;
1163 }
1164
1165
1166 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject, const QColor& theColor, bool theUpdateViewer)
1167 {
1168   if (!isVisible(theObject))
1169     return Qt::black;
1170
1171   AISObjectPtr anAISObj = getAISObject(theObject);
1172   int aR, aG, aB;
1173   anAISObj->getColor(aR, aG, aB);
1174   anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
1175   if (theUpdateViewer)
1176     updateViewer();
1177   return QColor(aR, aG, aB);
1178 }
1179
1180 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
1181 {
1182   myResult2AISObjectMap[theObject] = theAIS;
1183
1184 #ifdef DEBUG_DISPLAY
1185   std::ostringstream aPtrStr;
1186   aPtrStr << theObject.get();
1187   qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
1188   qDebug(getResult2AISObjectMapInfo().c_str());
1189 #endif
1190 }
1191
1192 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
1193 {
1194   QStringList aContent;
1195   foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
1196     AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
1197     std::ostringstream aPtrStr;
1198     aPtrStr << "aObj = " << aObj.get() << ":";
1199     aPtrStr << "anAIS = " << aAISObj.get() << ":";
1200     aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
1201     
1202     aContent.append(aPtrStr.str().c_str());
1203   }
1204   return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
1205                                             arg(aContent.join("\n")).toStdString().c_str();
1206 }
1207
1208 void XGUI_Displayer::activateTrihedron(bool theIsActive) 
1209 {  
1210   myIsTrihedronActive = theIsActive; 
1211   if (!myIsTrihedronActive) {
1212     deactivateTrihedron(true);
1213   }
1214 }
1215
1216 void XGUI_Displayer::displayTrihedron(bool theToDisplay) const
1217 {
1218   Handle(AIS_InteractiveContext) aContext = AISContext();
1219   if (aContext.IsNull())
1220     return;
1221
1222   Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
1223
1224   if (theToDisplay) {
1225     if (!aContext->IsDisplayed(aTrihedron))
1226       aContext->Display(aTrihedron,
1227                         0 /*wireframe*/,
1228                         -1 /* selection mode */,
1229                         Standard_True /* update viewer*/,
1230                         Standard_False /* allow decomposition */,
1231                         AIS_DS_Displayed /* xdisplay status */);
1232
1233     if (!isTrihedronActive())
1234       deactivateTrihedron(false);
1235     else
1236       activate(aTrihedron, myActiveSelectionModes, false);
1237   } else {
1238     deactivateTrihedron(false);
1239     //aContext->LocalContext()->ClearOutdatedSelection(aTrihedron, true);
1240     // the selection from the previous activation modes should be cleared manually (#26172)
1241
1242     aContext->Erase(aTrihedron);
1243   }
1244
1245   updateViewer();
1246 }