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