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