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