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