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