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