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