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