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