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