Salome HOME
bos #19086: Do not clear pre-highlighting on selection of a sketcher object
[modules/shaper.git] / src / XGUI / XGUI_Displayer.cpp
1 // Copyright (C) 2014-2020  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "XGUI_Displayer.h"
21
22 #include "XGUI_FacesPanel.h"
23 #include "XGUI_Selection.h"
24 #include "XGUI_SelectionActivate.h"
25 #include "XGUI_SelectionMgr.h"
26 #include "XGUI_ViewerProxy.h"
27 #include "XGUI_Workshop.h"
28
29 #ifndef HAVE_SALOME
30 #include <AppElements_Viewer.h>
31 #endif
32
33 #include <ModelAPI_Document.h>
34 #include <ModelAPI_Data.h>
35 #include <ModelAPI_Object.h>
36 #include <ModelAPI_Tools.h>
37 #include <ModelAPI_AttributeIntArray.h>
38 #include <ModelAPI_ResultBody.h>
39 #include <ModelAPI_ResultConstruction.h>
40
41 #include <ModuleBase_BRepOwner.h>
42 #include <ModuleBase_IModule.h>
43 #include <ModuleBase_Preferences.h>
44 #include <ModuleBase_Tools.h>
45 #include <ModuleBase_ViewerPrs.h>
46 #include <ModuleBase_IViewer.h>
47
48 #include <GeomAPI_Shape.h>
49 #include <GeomAPI_IPresentable.h>
50 #include <GeomAPI_ICustomPrs.h>
51 #include <GeomAPI_Pnt.h>
52 #include <GeomAPI_IScreenParams.h>
53
54 #include <SUIT_ResourceMgr.h>
55
56 #include <AIS_InteractiveContext.hxx>
57 #include <AIS_ListOfInteractive.hxx>
58 #include <AIS_ListIteratorOfListOfInteractive.hxx>
59 #include <AIS_DimensionSelectionMode.hxx>
60 #include <AIS_Shape.hxx>
61 #include <AIS_Dimension.hxx>
62 #include <AIS_Trihedron.hxx>
63 #ifdef BEFORE_TRIHEDRON_PATCH
64 #include <AIS_Axis.hxx>
65 #include <AIS_Plane.hxx>
66 #include <AIS_Point.hxx>
67 #endif
68 #include <AIS_Selection.hxx>
69 #include <Prs3d_Drawer.hxx>
70 #include <Prs3d_IsoAspect.hxx>
71 #include <SelectMgr_ListOfFilter.hxx>
72 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
73 #include <SelectMgr_SelectionManager.hxx>
74 #include <TColStd_ListIteratorOfListOfInteger.hxx>
75 #include <BRepMesh_IncrementalMesh.hxx>
76
77 #include <StdSelect_ViewerSelector3d.hxx>
78
79 #include <TColStd_MapOfTransient.hxx>
80 #include <TColStd_MapIteratorOfMapOfTransient.hxx>
81 #include <Standard_Version.hxx>
82
83 #ifdef TINSPECTOR
84 #include <inspector/VInspectorAPI_CallBack.hxx>
85 #endif
86
87 #include <Events_Loop.h>
88 #include <ModelAPI_Events.h>
89 #include <Config_PropManager.h>
90
91 #include <set>
92
93 /// defines the local context mouse selection sensitivity
94 const int MOUSE_SENSITIVITY_IN_PIXEL = 10;
95
96 //#define DEBUG_DISPLAY
97 //#define DEBUG_FEATURE_REDISPLAY
98 //#define DEBUG_SELECTION_FILTERS
99
100 //#define DEBUG_OCCT_SHAPE_SELECTION
101
102 #define CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
103
104 //#define DEBUG_VIEWER_BLOCKED_COUNT
105
106 //**************************************************************
107 void displayedObjects(const Handle(AIS_InteractiveContext)& theAIS, AIS_ListOfInteractive& theList)
108 {
109   // Get from null point
110 #if OCC_VERSION_HEX < 0x070400
111         theAIS->DisplayedObjects(theList, true);
112 #else
113         theAIS->DisplayedObjects(theList);
114 #endif
115 }
116
117 QString qIntListInfo(const QIntList& theValues, const QString& theSeparator = QString(", "))
118 {
119   QStringList anInfo;
120   QIntList::const_iterator anIt = theValues.begin(), aLast = theValues.end();
121   for (; anIt != aLast; anIt++) {
122     anInfo.append(QString::number(*anIt));
123   }
124   return anInfo.join(theSeparator);
125 }
126
127 //**************************************************************
128 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
129 : myWorkshop(theWorkshop), myNeedUpdate(false),
130   myViewerBlockedRecursiveCount(0), myContextId(0)
131 {
132   BRepMesh_IncrementalMesh::SetParallelDefault(Standard_True);
133 }
134
135 //**************************************************************
136 XGUI_Displayer::~XGUI_Displayer()
137 {
138 }
139
140 //**************************************************************
141 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
142 {
143   return myResult2AISObjectMap.contains(theObject);
144 }
145
146 //**************************************************************
147 bool XGUI_Displayer::display(ObjectPtr theObject, bool theUpdateViewer)
148 {
149   bool aDisplayed = false;
150   if (isVisible(theObject)) {
151     aDisplayed = redisplay(theObject, theUpdateViewer);
152   } else {
153     AISObjectPtr anAIS;
154     GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
155     bool isShading = false;
156     if (aPrs.get() != NULL) {
157       GeomScreenParamsPtr aScreen = std::dynamic_pointer_cast<GeomAPI_IScreenParams>(theObject);
158       if (aScreen.get()) {
159         aScreen->setScreenPlane(getScreenPlane());
160         aScreen->setViewScale(getViewScale());
161       }
162       anAIS = aPrs->getAISObject(anAIS);
163     } else {
164       anAIS = myWorkshop->module()->createPresentation(theObject);
165       isShading = true;
166     }
167     if (anAIS.get())
168       aDisplayed = display(theObject, anAIS, isShading, theUpdateViewer);
169   }
170   return aDisplayed;
171 }
172
173
174 //**************************************************************
175 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS, ModuleBase_IModule* theModule)
176 {
177   Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
178   if (!aShapePrs.IsNull()) {
179     TopoDS_Shape aShape = aShapePrs->Shape();
180     if (aShape.IsNull())
181       return false;
182     TopAbs_ShapeEnum aType = aShape.ShapeType();
183     if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
184       return false;
185     else {
186       // Check that the presentation is not a sketch
187       return theModule->canBeShaded(theAIS);
188     }
189   }
190   return false;
191 }
192
193 //**************************************************************
194 bool XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
195                              bool isShading, bool theUpdateViewer)
196 {
197   bool aDisplayed = false;
198
199   Handle(AIS_InteractiveContext) aContext = AISContext();
200   if (aContext.IsNull())
201     return aDisplayed;
202
203   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
204   if (!anAISIO.IsNull()) {
205     appendResultObject(theObject, theAIS);
206
207     //bool isCustomized = customizeObject(theObject);
208
209     int aDispMode = isShading? Shading : Wireframe;
210     anAISIO->SetDisplayMode(aDispMode);
211     aContext->Display(anAISIO, aDispMode, 0, false, AIS_DS_Displayed);
212     #ifdef TINSPECTOR
213     if (getCallBack()) getCallBack()->Display(anAISIO);
214     #endif
215     aDisplayed = true;
216
217     emit objectDisplayed(theObject, theAIS);
218     selectionActivate()->activate(anAISIO, theUpdateViewer);
219   }
220   if (theUpdateViewer)
221     updateViewer();
222
223   return aDisplayed;
224 }
225
226 //**************************************************************
227 bool XGUI_Displayer::erase(ObjectPtr theObject, const bool theUpdateViewer)
228 {
229   bool aErased = false;
230   if (!isVisible(theObject))
231     return aErased;
232
233   Handle(AIS_InteractiveContext) aContext = AISContext();
234   if (aContext.IsNull())
235     return aErased;
236
237   AISObjectPtr anObject = myResult2AISObjectMap.value(theObject);
238   if (anObject) {
239     Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
240     if (!anAIS.IsNull()) {
241       emit beforeObjectErase(theObject, anObject);
242       aContext->Remove(anAIS, false/*update viewer*/);
243       #ifdef TINSPECTOR
244       if (getCallBack()) getCallBack()->Remove(anAIS);
245       #endif
246       aErased = true;
247     }
248   }
249   myResult2AISObjectMap.remove(theObject);
250
251 #ifdef DEBUG_DISPLAY
252   std::ostringstream aPtrStr;
253   aPtrStr << theObject.get();
254   qDebug(QString("erase object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
255   qDebug(getResult2AISObjectMapInfo().c_str());
256 #endif
257
258   if (theUpdateViewer)
259     updateViewer();
260
261   return aErased;
262 }
263
264 //**************************************************************
265 bool XGUI_Displayer::redisplay(ObjectPtr theObject, bool theUpdateViewer)
266 {
267   bool aRedisplayed = false;
268   Handle(AIS_InteractiveContext) aContext = AISContext();
269   if (aContext.IsNull())
270     return aRedisplayed;
271
272   if (!isVisible(theObject))
273     return aRedisplayed;
274
275   AISObjectPtr aAISObj = getAISObject(theObject);
276   Handle(AIS_InteractiveObject) aAISIO;
277
278   GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
279   if (aPrs) {
280     GeomScreenParamsPtr aScreen = std::dynamic_pointer_cast<GeomAPI_IScreenParams>(theObject);
281     if (aScreen.get()) {
282       aScreen->setScreenPlane(getScreenPlane());
283       aScreen->setViewScale(getViewScale());
284     }
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       erase(theObject, theUpdateViewer);
292       appendResultObject(theObject, aAIS_Obj);
293     }
294     aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
295   }
296   else {
297     aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
298   }
299
300   if (!aAISIO.IsNull()) {
301     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
302     if (aResult.get()) {
303       // Set color
304       std::vector<int> aColor;
305       ModelAPI_Tools::getColor(aResult, aColor);
306       if (aColor.size() > 0) {
307         Quantity_Color
308           aCol(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB);
309         aAISIO->SetColor(aCol);
310       }
311       // Set deflection
312       double aDeflection = ModelAPI_Tools::getDeflection(aResult);
313       if ((aDeflection >= 0) && (aDeflection != aAISObj->getDeflection()))
314         aAISObj->setDeflection(aDeflection);
315
316       // Set transparency
317       double aTransparency = ModelAPI_Tools::getTransparency(aResult);
318       if ((aTransparency >= 0) && (aTransparency != aAISObj->getTransparency()))
319         aAISObj->setTransparency(aTransparency);
320
321       // Set Iso-Lines
322       Handle(ModuleBase_ResultPrs) aResPrs = Handle(ModuleBase_ResultPrs)::DownCast(aAISIO);
323       if (!aResPrs.IsNull())
324         aResPrs->updateIsoLines();
325     }
326     //myWorkshop->module()->storeSelection();
327
328 //#ifdef CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
329     //myWorkshop->selector()->deselectPresentation(aAISIO);
330 //#endif
331
332     if (aContext->IsDisplayed(aAISIO))
333       aContext->Redisplay(aAISIO, false);
334     else {
335       aContext->Display(aAISIO, false);
336     }
337       #ifdef TINSPECTOR
338       if (getCallBack()) getCallBack()->Redisplay(aAISIO);
339       #endif
340
341       //if (aNeedToRestoreSelection)
342     //myWorkshop->module()->restoreSelection();
343
344     aRedisplayed = true;
345     #ifdef DEBUG_FEATURE_REDISPLAY
346       qDebug("  Redisplay happens");
347     #endif
348     if (theUpdateViewer)
349       updateViewer();
350   }
351   return aRedisplayed;
352 }
353
354 //**************************************************************
355 void XGUI_Displayer::redisplayObjects()
356 {
357   // redisplay objects visualized in the viewer
358   static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
359   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
360   QObjectPtrList aDisplayed = displayedObjects();
361   QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
362   for (; anIt != aLast; anIt++) {
363     aECreator->sendUpdated(*anIt, EVENT_DISP);
364   }
365   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
366   if (aViewer->isColorScaleVisible()) {
367     aViewer->setupColorScale();
368     aViewer->setColorScaleShown(true);
369   }
370   Events_Loop::loop()->flush(EVENT_DISP);
371 }
372
373 //**************************************************************
374 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
375                                        const bool theUpdateViewer)
376 {
377   //Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
378   //if (!aTrihedron.IsNull())
379   //  deactivateAIS(aTrihedron);
380
381   QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
382   for (; anIt != aLast; anIt++) {
383     selectionActivate()->deactivate(*anIt, false);
384   }
385   //VSV It seems that there is no necessity to update viewer on deactivation
386   //if (theUpdateViewer)
387   //  updateViewer();
388 }
389
390 //**************************************************************
391 bool XGUI_Displayer::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
392 {
393   bool aVisible = false;
394   GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
395   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
396   if (aPrs.get() || aResult.get()) {
397     aVisible = theDisplayer->isVisible(theObject);
398     // compsolid is not visualized in the viewer,
399     // but should have presentation when all sub solids are
400     // visible. It is useful for highlight presentation where compsolid shape is selectable
401     if (!aVisible && aResult.get() && aResult->groupName() == ModelAPI_ResultBody::group()) {
402       ResultBodyPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
403       if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
404         int aNumberOfSubs = aCompsolidResult->numberOfSubs();
405         bool anAllSubsVisible = aNumberOfSubs > 0;
406         for(int i = 0; i < aNumberOfSubs && anAllSubsVisible; i++) {
407           anAllSubsVisible = theDisplayer->isVisible(aCompsolidResult->subResult(i));
408         }
409         aVisible = anAllSubsVisible;
410       }
411     }
412   }
413   // it is possible that feature is presentable and has results, so we should check visibility
414   // of results if presentation is not shown (e.g. Sketch Circle/Arc features)
415   if (!aVisible) {
416     // check if all results of the feature are visible
417     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
418     std::list<ResultPtr> aResults;
419     ModelAPI_Tools::allResults(aFeature, aResults);
420     std::list<ResultPtr>::const_iterator aIt;
421     aVisible = !aResults.empty();
422     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
423       aVisible = aVisible && theDisplayer->isVisible(*aIt);
424     }
425   }
426   return aVisible;
427 }
428
429 //**************************************************************
430 void XGUI_Displayer::setSelected(const  QList<ModuleBase_ViewerPrsPtr>& theValues,
431                                  bool theUpdateViewer)
432 {
433   Handle(AIS_InteractiveContext) aContext = AISContext();
434   if (aContext.IsNull())
435     return;
436   aContext->UnhilightSelected(false);
437   aContext->ClearSelected(false);
438   #ifdef TINSPECTOR
439   if (getCallBack()) getCallBack()->ClearSelected();
440   #endif
441   NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)>>
442     aShapesToBeSelected;
443
444   foreach (ModuleBase_ViewerPrsPtr aPrs, theValues) {
445     const GeomShapePtr& aGeomShape = aPrs->shape();
446     if (aGeomShape.get() && !aGeomShape->isNull()) {
447       const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
448 #ifdef DEBUG_OCCT_SHAPE_SELECTION
449       // problem 1: performance
450       // problem 2: IO is not specified, so the first found owner is selected, as a result
451       // it might belong to another result
452       aContext->AddOrRemoveSelected(aShape, false);
453       #ifdef TINSPECTOR
454       if (getCallBack()) getCallBack()->AddOrRemoveSelected(aShape);
455       #endif
456 #else
457       NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations;
458       if (aShapesToBeSelected.IsBound(aShape))
459         aPresentations = aShapesToBeSelected.Find(aShape);
460       ObjectPtr anObject = aPrs->object();
461       getPresentations(anObject, aPresentations);
462
463       aShapesToBeSelected.Bind(aShape, aPresentations);
464 #endif
465     } else {
466       ObjectPtr anObject = aPrs->object();
467       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
468       if (aResult.get() && isVisible(aResult)) {
469         AISObjectPtr anObj = myResult2AISObjectMap.value(aResult);
470         Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
471         if (!anAIS.IsNull()) {
472           // The methods are replaced in order to provide multi-selection, e.g. restore selection
473           // by activating multi selector widget. It also gives an advantage that the multi
474           // selection in OB gives multi-selection in the viewer
475           //aContext->SetSelected(anAIS, false);
476           // The selection in the context was cleared, so the method sets the objects are selected
477           aContext->AddOrRemoveSelected(anAIS, false);
478           #ifdef TINSPECTOR
479           if (getCallBack()) getCallBack()->AddOrRemoveSelected(anAIS);
480           #endif
481         }
482       }
483     }
484   }
485   if (!aShapesToBeSelected.IsEmpty())
486     XGUI_Displayer::AddOrRemoveSelectedShapes(aContext, aShapesToBeSelected);
487
488   if (theUpdateViewer)
489     updateViewer();
490 }
491
492 //**************************************************************
493 void XGUI_Displayer::clearSelected(const bool theUpdateViewer)
494 {
495   Handle(AIS_InteractiveContext) aContext = AISContext();
496   if (!aContext.IsNull()) {
497     aContext->UnhilightSelected(false);//UnhilightCurrents(false);
498     aContext->ClearSelected(theUpdateViewer);
499     #ifdef TINSPECTOR
500     if (getCallBack()) getCallBack()->ClearSelected();
501     #endif
502   }
503 }
504
505 //**************************************************************
506 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
507 {
508   bool aErased = false;
509   Handle(AIS_InteractiveContext) aContext = AISContext();
510   if (!aContext.IsNull()) {
511 #ifdef OPTIMIZE_PRS
512     foreach(ObjectPtr aObj, myResult2AISObjectMap.objects()) {
513       AISObjectPtr aAISObj = myResult2AISObjectMap.value(aObj);
514 #else
515     foreach(ObjectPtr aObj, myResult2AISObjectMap.keys()) {
516       AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
517 #endif
518       // erase an object
519       Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
520       if (!anIO.IsNull()) {
521         emit beforeObjectErase(aObj, aAISObj);
522         aContext->Remove(anIO, false/*update viewer*/);
523         #ifdef TINSPECTOR
524         if (getCallBack()) getCallBack()->Remove(anIO);
525         #endif
526         aErased = true;
527       }
528     }
529     if (theUpdateViewer)
530       updateViewer();
531   }
532   myResult2AISObjectMap.clear();
533 #ifdef DEBUG_DISPLAY
534   qDebug("eraseAll");
535   qDebug(getResult2AISObjectMapInfo().c_str());
536 #endif
537   return aErased;
538 }
539
540
541 //**************************************************************
542 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
543 {
544 #ifdef OPTIMIZE_PRS
545   return myResult2AISObjectMap.value(theObject);
546 #else
547   AISObjectPtr anIO;
548   if (myResult2AISObjectMap.contains(theObject))
549     anIO = myResult2AISObjectMap[theObject];
550   return anIO;
551 #endif
552 }
553
554 //**************************************************************
555 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
556 {
557   Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
558   return getObject(aRefAIS);
559 }
560
561 //**************************************************************
562 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
563 {
564 #ifdef OPTIMIZE_PRS
565   ObjectPtr anObject = myResult2AISObjectMap.value(theIO);
566 #else
567   ObjectPtr anObject;
568   ResultToAISMap::const_iterator aMapIter = myResult2AISObjectMap.cbegin();
569   for (; aMapIter != myResult2AISObjectMap.cend(); aMapIter++) {
570     const AISObjectPtr& aAIS = aMapIter.value();
571     Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
572     if (anAIS == theIO)
573       anObject = aMapIter.key();
574     if (anObject.get())
575       break;
576   }
577 #endif
578   if (!anObject.get()) {
579     std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
580     if (!theIO.IsNull()) {
581       anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
582     }
583     if (myWorkshop->module())
584       anObject = myWorkshop->module()->findPresentedObject(anAISObj);
585   }
586   return anObject;
587 }
588
589 //**************************************************************
590 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
591 {
592   bool aWasEnabled = isUpdateEnabled();
593   if (isEnabled)
594     myViewerBlockedRecursiveCount--;
595   else
596     myViewerBlockedRecursiveCount++;
597
598 #ifdef DEBUG_VIEWER_BLOCKED_COUNT
599   std::cout << "myViewerBlockedRecursiveCount = " << myViewerBlockedRecursiveCount << std::endl;
600 #endif
601
602   if (myNeedUpdate && isUpdateEnabled()) {
603     updateViewer();
604     myNeedUpdate = false;
605   }
606   return aWasEnabled;
607 }
608
609 //**************************************************************
610 void XGUI_Displayer::updateViewer() const
611 {
612   Handle(AIS_InteractiveContext) aContext = AISContext();
613
614 #ifdef DEBUG_VIEWER_BLOCKED_COUNT
615   std::cout << "updateViewer: " << (myViewerBlockedRecursiveCount == 0 ? " done" : " later")
616             << std::endl;
617 #endif
618
619   if (!aContext.IsNull() && isUpdateEnabled()) {
620     //myWorkshop->viewer()->Zfitall();
621     aContext->UpdateCurrentViewer();
622   } else {
623     myNeedUpdate = true;
624   }
625 }
626
627 //**************************************************************
628 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
629 {
630   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
631   if (!aContext.IsNull() && (myContextId != aContext.get())) {
632     myContextId = aContext.get();
633     if (!myWorkshop->selectionActivate()->isTrihedronActive())
634       selectionActivate()->deactivateTrihedron(true);
635     // Do not modify default drawer. The same is done in ModuleBase_ResultPrs
636     //aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
637     //aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
638
639     //Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
640     //aTrihedron->getHighlightPointAspect()->SetScale(2.0);
641     //aTrihedron->getHighlightPointAspect()->SetTypeOfMarker(Aspect_TOM_O_STAR);
642
643     // Commented out according to discussion in bug #2825
644     ModuleBase_IViewer::DefaultHighlightDrawer = aContext->HighlightStyle();
645     //Handle(Prs3d_Drawer) aSelStyle = aContext->SelectionStyle();
646     //double aDeflection =
647     //  QString(ModelAPI_ResultConstruction::DEFAULT_DEFLECTION().c_str()).toDouble();
648     //try {
649     //  aDeflection = Config_PropManager::real("Visualization", "construction_deflection");
650     //} catch (...) {}
651
652     //ModuleBase_IViewer::DefaultHighlightDrawer->SetDeviationCoefficient(aDeflection);
653     //aSelStyle->SetDeviationCoefficient(aDeflection);
654   }
655   return aContext;
656 }
657
658 //**************************************************************
659 void XGUI_Displayer::setSelectionColor(const std::vector<int>& theColor)
660 {
661   Handle(AIS_InteractiveContext) aContext = AISContext();
662   if (!aContext.IsNull()) {
663     Quantity_Color aQColor(theColor[0] / 255.,
664                            theColor[1] / 255.,
665                            theColor[2] / 255., Quantity_TOC_RGB);
666     aContext->SelectionStyle()->SetColor(aQColor);
667     aContext->SelectionStyle()->PointAspect()->SetColor(aQColor);
668     aContext->SelectionStyle()->LineAspect()->SetColor(aQColor);
669     aContext->HighlightStyle(Prs3d_TypeOfHighlight_LocalSelected)->SetColor(aQColor);
670   }
671 }
672
673
674 //**************************************************************
675 std::vector<int> XGUI_Displayer::selectionColor() const
676 {
677   std::vector<int> aColor;
678   Handle(AIS_InteractiveContext) aContext = AISContext();
679   if (!aContext.IsNull()) {
680     Quantity_Color aQColor = aContext->SelectionStyle()->Color();
681     aColor.push_back((int)(aQColor.Red() * 255));
682     aColor.push_back((int)(aQColor.Green() * 255));
683     aColor.push_back((int)(aQColor.Blue() * 255));
684   }
685   return aColor;
686 }
687
688
689 //**************************************************************
690 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
691 {
692   Handle(AIS_InteractiveContext) aContext = AISContext();
693   if (!aContext.IsNull() && myAndFilter.IsNull()) {
694     myAndFilter = new SelectMgr_AndFilter();
695     aContext->AddFilter(myAndFilter);
696   }
697   return myAndFilter;
698 }
699
700 //**************************************************************
701 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
702                                 const Standard_Integer theDisplayMode, bool theUpdateViewer)
703 {
704   bool aDisplayed = false;
705   Handle(AIS_InteractiveContext) aContext = AISContext();
706   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
707   if (!aContext.IsNull() && !anAISIO.IsNull()) {
708     aContext->Display(anAISIO, theDisplayMode, 0, false/*update viewer*/, AIS_DS_Displayed);
709     #ifdef TINSPECTOR
710     if (getCallBack()) getCallBack()->Display(anAISIO);
711     #endif
712     aDisplayed = true;
713     aContext->Deactivate(anAISIO);
714     #ifdef TINSPECTOR
715     if (getCallBack()) getCallBack()->Deactivate(anAISIO);
716     #endif
717     aContext->Load(anAISIO);
718     #ifdef TINSPECTOR
719     if (getCallBack()) getCallBack()->Load(anAISIO);
720     #endif
721     if (toActivateInSelectionModes)
722       myWorkshop->selectionActivate()->activateOnDisplay(anAISIO, theUpdateViewer);
723
724     if (theUpdateViewer)
725       updateViewer();
726   }
727   return aDisplayed;
728 }
729
730 //**************************************************************
731 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
732 {
733   bool aErased = false;
734   Handle(AIS_InteractiveContext) aContext = AISContext();
735   if (!aContext.IsNull()) {
736     Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
737     if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
738       aContext->Remove(anAISIO, false/*update viewer*/);
739       #ifdef TINSPECTOR
740       if (getCallBack()) getCallBack()->Remove(anAISIO);
741       #endif
742       aErased = true;
743     }
744   }
745   if (aErased && theUpdateViewer)
746     updateViewer();
747   return aErased;
748 }
749
750 //**************************************************************
751 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
752 {
753   if (theMode == NoMode)
754     return;
755
756   Handle(AIS_InteractiveContext) aContext = AISContext();
757   if (aContext.IsNull())
758     return;
759
760   AISObjectPtr aAISObj = getAISObject(theObject);
761   if (!aAISObj)
762     return;
763
764   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
765   aContext->SetDisplayMode(aAISIO, theMode, false);
766   // Redisplay in order to update new mode because it could be not computed before
767   if (theUpdateViewer)
768     updateViewer();
769 }
770
771 //**************************************************************
772 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
773 {
774   Handle(AIS_InteractiveContext) aContext = AISContext();
775   if (aContext.IsNull())
776     return NoMode;
777
778   AISObjectPtr aAISObj = getAISObject(theObject);
779   if (!aAISObj)
780     return NoMode;
781
782   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
783   return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
784 }
785
786 //**************************************************************
787 void XGUI_Displayer::deactivateSelectionFilters(const bool theAddFilterOnly)
788 {
789   Handle(AIS_InteractiveContext) aContext = AISContext();
790   if (!myAndFilter.IsNull()) {
791     bool aFound = false;
792     if (!aContext.IsNull()) {
793       const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
794       SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
795       for (; anIt.More() && !aFound; anIt.Next()) {
796         Handle(SelectMgr_Filter) aFilter = anIt.Value();
797         aFound = aFilter == myAndFilter;
798       }
799       if (aFound)
800         aContext->RemoveFilter(myAndFilter);
801     }
802     myAndFilter.Nullify();
803   }
804 }
805
806 //**************************************************************
807 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
808 {
809   Handle(AIS_InteractiveContext) aContext = AISContext();
810   if (aContext.IsNull() || hasSelectionFilter(theFilter))
811     return;
812
813   Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
814   if (!aCompositeFilter.IsNull()) {
815     aCompositeFilter->Add(theFilter);
816 #ifdef DEBUG_SELECTION_FILTERS
817     int aCount = aCompositeFilter->StoredFilters().Extent();
818     qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
819 #endif
820   }
821 }
822
823 //**************************************************************
824 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
825 {
826   Handle(AIS_InteractiveContext) aContext = AISContext();
827   if (aContext.IsNull())
828     return;
829
830   Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
831   if (!aCompositeFilter.IsNull() && aCompositeFilter->IsIn(theFilter)) {
832     aCompositeFilter->Remove(theFilter);
833 #ifdef DEBUG_SELECTION_FILTERS
834     int aCount = aCompositeFilter->StoredFilters().Extent();
835     qDebug(QString("removeSelectionFilter: filters.count() = %1")
836       .arg(aCount).toStdString().c_str());
837 #endif
838   }
839 }
840
841 //**************************************************************
842 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
843 {
844   bool aFilterFound = false;
845
846   Handle(AIS_InteractiveContext) aContext = AISContext();
847   if (aContext.IsNull())
848     return aFilterFound;
849   const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
850   SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
851   for (; aIt.More() && !aFilterFound; aIt.Next()) {
852     if (theFilter.get() == aIt.Value().get())
853       aFilterFound = true;
854   }
855   Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
856   if (!aCompositeFilter.IsNull()) {
857     const SelectMgr_ListOfFilter& aStoredFilters = aCompositeFilter->StoredFilters();
858     for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
859       if (theFilter.get() == aIt.Value().get())
860         aFilterFound = true;
861     }
862   }
863   return aFilterFound;
864 }
865
866 //**************************************************************
867 void XGUI_Displayer::removeFilters()
868 {
869   Handle(AIS_InteractiveContext) aContext = AISContext();
870   if (aContext.IsNull())
871     return;
872
873   Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
874   if (!aCompositeFilter.IsNull())
875     aCompositeFilter->Clear();
876 }
877
878 //**************************************************************
879 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
880 {
881 #ifdef OPTIMIZE_PRS
882   QObjectPtrList aDispList = myResult2AISObjectMap.objects();
883 #else
884   QObjectPtrList aDispList = myResult2AISObjectMap.keys();
885 #endif
886   foreach(ObjectPtr aObj, aDispList) {
887     if (!theList.contains(aObj))
888       erase(aObj, false);
889   }
890   foreach(ObjectPtr aObj, theList) {
891     if (!isVisible(aObj))
892       display(aObj, false);
893   }
894   updateViewer();
895 }
896
897 //**************************************************************
898 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
899 {
900   if (!isVisible(theObject))
901     return false;
902
903   AISObjectPtr aAISObj = getAISObject(theObject);
904   if (aAISObj.get() == NULL)
905     return false;
906
907   Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
908   return ::canBeShaded(anAIS, myWorkshop->module());
909 }
910
911 //**************************************************************
912 //bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
913 //{
914 //  AISObjectPtr anAISObj = getAISObject(theObject);
915 //  // correct the result's color it it has the attribute
916 //  ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
917 //
918 //  // Customization of presentation
919 //  GeomCustomPrsPtr aCustomPrs;
920 //  FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
921 //  if (aFeature.get() != NULL) {
922 //    GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
923 //    if (aCustPrs.get() != NULL)
924 //      aCustomPrs = aCustPrs;
925 //  }
926 //  if (aCustomPrs.get() == NULL) {
927 //    GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
928 //    // we ignore presentable not customized objects
929 //    if (aPrs.get() == NULL)
930 //      aCustomPrs = myCustomPrs;
931 //  }
932 //  bool isCustomized = aCustomPrs.get() &&
933 //                      aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
934 //  isCustomized = myWorkshop->module()->afterCustomisePresentation(aResult, anAISObj, myCustomPrs)
935 //                 || isCustomized;
936 //
937 //  // update presentation state if faces panel is active
938 //  if (anAISObj.get() && myWorkshop->facesPanel())
939 //    isCustomized = myWorkshop->facesPanel()->customizeObject(theObject, anAISObj) || isCustomized;
940 //
941 //  return isCustomized;
942 //}
943
944 //**************************************************************
945 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject,
946                                       const QColor& theColor,
947                                       bool theUpdateViewer)
948 {
949   if (!isVisible(theObject))
950     return Qt::black;
951
952   AISObjectPtr anAISObj = getAISObject(theObject);
953   int aR, aG, aB;
954   anAISObj->getColor(aR, aG, aB);
955   anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
956   if (theUpdateViewer)
957     updateViewer();
958   return QColor(aR, aG, aB);
959 }
960
961 //**************************************************************
962 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
963 {
964 #ifdef OPTIMIZE_PRS
965   myResult2AISObjectMap.add(theObject, theAIS);
966 #else
967   myResult2AISObjectMap[theObject] = theAIS;
968 #endif
969
970 #ifdef DEBUG_DISPLAY
971   std::ostringstream aPtrStr;
972   aPtrStr << theObject.get();
973   qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
974   qDebug(getResult2AISObjectMapInfo().c_str());
975 #endif
976 }
977
978 #ifdef _DEBUG
979 //**************************************************************
980 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
981 {
982   QStringList aContent;
983 #ifdef OPTIMIZE_PRS
984   foreach(ObjectPtr aObj, myResult2AISObjectMap.objects()) {
985     AISObjectPtr aAISObj = myResult2AISObjectMap.value(aObj);
986 #else
987   foreach(ObjectPtr aObj, myResult2AISObjectMap.keys()) {
988     AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
989 #endif
990     std::ostringstream aPtrStr;
991     aPtrStr << "aObj = " << aObj.get() << ":";
992     aPtrStr << "anAIS = " << aAISObj.get() << ":";
993     aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
994
995     aContent.append(aPtrStr.str().c_str());
996   }
997   return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
998                                             arg(aContent.join("\n")).toStdString().c_str();
999 }
1000 #endif
1001
1002 //**************************************************************
1003 void XGUI_Displayer::getPresentations(const ObjectPtr& theObject,
1004                                   NCollection_Map<Handle(AIS_InteractiveObject)>& thePresentations)
1005 {
1006   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1007   if (aResult.get()) {
1008     AISObjectPtr aAISObj = getAISObject(aResult);
1009     if (aAISObj.get() == NULL) {
1010       // if result is a result of a composite feature, it is visualized by visualization of
1011       // composite children, so we should get one of this presentations
1012       ResultBodyPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
1013       if (aCompSolid.get() && aCompSolid->numberOfSubs() > 0) {
1014         aAISObj = getAISObject(aCompSolid->subResult(0));
1015       }
1016     }
1017     if (aAISObj.get() != NULL) {
1018       Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1019       if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1020         thePresentations.Add(anAIS);
1021     }
1022   }
1023   else {
1024     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
1025     // find presentation of the feature
1026     AISObjectPtr aAISObj = getAISObject(aFeature);
1027     if (aAISObj.get() != NULL) {
1028       Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1029       if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1030         thePresentations.Add(anAIS);
1031     }
1032     // find presentations of the feature results
1033     std::list<ResultPtr> aResults;
1034     ModelAPI_Tools::allResults(aFeature, aResults);
1035     std::list<ResultPtr>::const_iterator anIt = aResults.begin(), aLast = aResults.end();
1036     for (; anIt != aLast; ++anIt) {
1037       AISObjectPtr aAISObj = getAISObject(*anIt);
1038       if (aAISObj.get() != NULL) {
1039         Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1040         if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1041           thePresentations.Add(anAIS);
1042       }
1043     }
1044   }
1045 }
1046
1047 //**************************************************************
1048 void XGUI_Displayer::displayTrihedron(bool theToDisplay) const
1049 {
1050   Handle(AIS_InteractiveContext) aContext = AISContext();
1051   if (aContext.IsNull())
1052     return;
1053
1054   Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
1055
1056   XGUI_SelectionActivate* aSelectionActive = selectionActivate();
1057   if (theToDisplay) {
1058     if (!aContext->IsDisplayed(aTrihedron))
1059       aContext->Display(aTrihedron,
1060                         0 /*wireframe*/,
1061                         -1 /* selection mode */,
1062                         Standard_True /* update viewer*/,
1063                         AIS_DS_Displayed /* xdisplay status */);
1064     #ifdef TINSPECTOR
1065     if (getCallBack()) getCallBack()->Display(aTrihedron);
1066     #endif
1067
1068     if (!aSelectionActive->isTrihedronActive())
1069       aSelectionActive->deactivateTrihedron(false);
1070     else
1071       aSelectionActive->activate(aTrihedron, false);
1072   } else {
1073     aSelectionActive->deactivateTrihedron(false);
1074
1075     aContext->Erase(aTrihedron, Standard_True);
1076     #ifdef TINSPECTOR
1077     if (getCallBack()) getCallBack()->Remove(aTrihedron);
1078     #endif
1079   }
1080 }
1081
1082 //**************************************************************
1083 void XGUI_Displayer::AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
1084                            const NCollection_DataMap<TopoDS_Shape,
1085                            NCollection_Map<Handle(AIS_InteractiveObject)>>& theShapesToBeSelected)
1086 {
1087   NCollection_Map<Handle(AIS_InteractiveObject)> aCompsolidPresentations;
1088   NCollection_Map<Handle(AIS_InteractiveObject)> aSelectedPresentations;
1089
1090   NCollection_List<Handle(SelectBasics_EntityOwner)> anActiveOwners;
1091   theContext->MainSelector()->ActiveOwners(anActiveOwners);
1092   NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (anActiveOwners);
1093   Handle(SelectMgr_EntityOwner) anOwner;
1094
1095   /// It is very important to check that the owner is processed only once and has a map of
1096   /// processed owners because SetSelected works as a switch.
1097   /// If count of calls setSelectec is even, the object stays in the previous state
1098   /// (selected, deselected)
1099   /// OCCT: to write about the problem that active owners method returns one owner several times
1100   QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
1101   for (; anOwnersIt.More(); anOwnersIt.Next()) {
1102     anOwner = Handle(SelectMgr_EntityOwner)::DownCast (anOwnersIt.Value());
1103     if (aSelectedIds.contains((size_t)anOwner.get()))
1104       continue;
1105     aSelectedIds.append((size_t)anOwner.get());
1106
1107     Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
1108     if (!BROwnr.IsNull() && BROwnr->HasShape()) {
1109       const TopoDS_Shape& aShape = BROwnr->Shape();
1110       if (aShape.IsNull())
1111         continue;
1112
1113       Handle(ModuleBase_BRepOwner) aCustomOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
1114
1115       NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)> >
1116                                              ::Iterator aShapeIt(theShapesToBeSelected);
1117       for (; aShapeIt.More(); aShapeIt.Next()) {
1118         const TopoDS_Shape& aParameterShape = aShapeIt.Key();
1119         // In case of compound we cannot rely on simple comparison method.
1120         // If the compound is generated by Group feature then this compound is alwais new.
1121         // So, we have to compare content of these compounds
1122
1123           // isSame should be used here as it does not check orientation of shapes
1124           // despite on isEqual of shapes or IsBound for shape in QMap. Orientation is
1125           // different for Edges shapes in model shape and owner even if this is the same shape
1126         if (ModuleBase_Tools::isSameShape(aParameterShape, aShape)) {
1127           Handle(AIS_InteractiveObject) anOwnerPresentation =
1128             Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
1129           NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations =
1130             theShapesToBeSelected.Find(aParameterShape);
1131           if (aPresentations.Contains(anOwnerPresentation)) {
1132             theContext->AddOrRemoveSelected(anOwner, Standard_False);
1133             anOwner->SetSelected(Standard_True);
1134             // collect selected presentations to do not select them if compsolid is selected
1135             if (!aSelectedPresentations.Contains(anOwnerPresentation))
1136               aSelectedPresentations.Add(anOwnerPresentation);
1137           }
1138         }
1139         else if (!aCustomOwner.IsNull()) { // CompSolid processing #2219
1140           // shape of owner is compound, but shape to be selected is compsolid, so
1141           // we need to compare shape to AIS presentation of owner(rule of the owner creation)
1142           Handle(AIS_Shape) anOwnerPresentation =
1143                             Handle(AIS_Shape)::DownCast(anOwner->Selectable());
1144           const TopoDS_Shape& aPresentationShape = anOwnerPresentation->Shape();
1145           if (aParameterShape.IsSame(anOwnerPresentation->Shape()) &&
1146               !aCompsolidPresentations.Contains(anOwnerPresentation))
1147             aCompsolidPresentations.Add(anOwnerPresentation);
1148         }
1149       }
1150     }
1151   }
1152   // select CompSolid presentations if their owners was not selected yet
1153   NCollection_Map<Handle(AIS_InteractiveObject)>::Iterator anIt (aCompsolidPresentations);
1154   for (; anIt.More(); anIt.Next()) {
1155     if (aSelectedPresentations.Contains(anIt.Value()))
1156       continue;
1157     theContext->AddOrRemoveSelected(anIt.Value(), Standard_False);
1158   }
1159 }
1160
1161 //**************************************************************
1162 XGUI_SelectionActivate* XGUI_Displayer::selectionActivate() const
1163 {
1164   return myWorkshop->selectionActivate();
1165 }
1166
1167 //**************************************************************
1168 GeomPlanePtr XGUI_Displayer::getScreenPlane() const
1169 {
1170   GeomPlanePtr aResult;
1171   Handle(AIS_InteractiveContext) aContext = AISContext();
1172   if (!aContext.IsNull()) {
1173     Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1174     Handle(V3d_View) aView;
1175     for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews()) {
1176       aView = aViewer->ActiveView();
1177       break;
1178     }
1179     if (!aView.IsNull()) {
1180       double aEyeX, aEyeY, aEyeZ;
1181       aView->Eye(aEyeX, aEyeY, aEyeZ);
1182
1183       double aProjX, aProjY, aProjZ;
1184       aView->Proj(aProjX, aProjY, aProjZ);
1185
1186       GeomPointPtr aPnt = GeomPointPtr(new GeomAPI_Pnt(aEyeX, aEyeY, aEyeZ));
1187       GeomDirPtr aDir = GeomDirPtr(new GeomAPI_Dir(aProjX, aProjY, aProjZ));
1188
1189       aResult = GeomPlanePtr(new GeomAPI_Pln(aPnt, aDir));
1190     }
1191   }
1192   return aResult;
1193 }
1194
1195 double XGUI_Displayer::getViewScale() const
1196 {
1197   Handle(AIS_InteractiveContext) aContext = AISContext();
1198   if (!aContext.IsNull()) {
1199     Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1200     Handle(V3d_View) aView;
1201     for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews()) {
1202       aView = aViewer->ActiveView();
1203       break;
1204     }
1205     return aView->Camera()->Scale();
1206   }
1207   return 1;
1208 }