Salome HOME
Build SHAPER with dev version of OCCT (IR-2020-07-10)
[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_Shape.hxx>
60 #include <AIS_Dimension.hxx>
61 #include <AIS_Trihedron.hxx>
62 #ifdef BEFORE_TRIHEDRON_PATCH
63 #include <AIS_Axis.hxx>
64 #include <AIS_Plane.hxx>
65 #include <AIS_Point.hxx>
66 #endif
67 #include <AIS_Selection.hxx>
68 #include <Prs3d_Drawer.hxx>
69 #include <Prs3d_IsoAspect.hxx>
70 #include <SelectMgr_ListOfFilter.hxx>
71 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
72 #include <SelectMgr_SelectionManager.hxx>
73 #include <TColStd_ListIteratorOfListOfInteger.hxx>
74 #include <BRepMesh_IncrementalMesh.hxx>
75
76 #include <StdSelect_ViewerSelector3d.hxx>
77
78 #include <TColStd_MapOfTransient.hxx>
79 #include <TColStd_MapIteratorOfMapOfTransient.hxx>
80 #include <Standard_Version.hxx>
81
82 #ifdef TINSPECTOR
83 #include <inspector/VInspectorAPI_CallBack.hxx>
84 #endif
85
86 #include <Events_Loop.h>
87 #include <ModelAPI_Events.h>
88 #include <Config_PropManager.h>
89
90 #include <set>
91
92 #ifdef _MSC_VER
93 #pragma warning(disable: 4702)
94 #endif
95
96 /// defines the local context mouse selection sensitivity
97 const int MOUSE_SENSITIVITY_IN_PIXEL = 10;
98
99 //#define DEBUG_DISPLAY
100 //#define DEBUG_FEATURE_REDISPLAY
101 //#define DEBUG_SELECTION_FILTERS
102
103 //#define DEBUG_OCCT_SHAPE_SELECTION
104
105 #define CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
106
107 //#define DEBUG_VIEWER_BLOCKED_COUNT
108
109 //**************************************************************
110 void displayedObjects(const Handle(AIS_InteractiveContext)& theAIS, AIS_ListOfInteractive& theList)
111 {
112   // Get from null point
113 #if OCC_VERSION_HEX < 0x070400
114         theAIS->DisplayedObjects(theList, true);
115 #else
116         theAIS->DisplayedObjects(theList);
117 #endif
118 }
119
120 QString qIntListInfo(const QIntList& theValues, const QString& theSeparator = QString(", "))
121 {
122   QStringList anInfo;
123   QIntList::const_iterator anIt = theValues.begin(), aLast = theValues.end();
124   for (; anIt != aLast; anIt++) {
125     anInfo.append(QString::number(*anIt));
126   }
127   return anInfo.join(theSeparator);
128 }
129
130 //**************************************************************
131 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
132 : myWorkshop(theWorkshop),
133   myViewerBlockedRecursiveCount(0),
134   myContextId(0),
135   myNeedUpdate(false)
136 {
137   BRepMesh_IncrementalMesh::SetParallelDefault(Standard_True);
138 }
139
140 //**************************************************************
141 XGUI_Displayer::~XGUI_Displayer()
142 {
143 }
144
145 //**************************************************************
146 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
147 {
148   return myResult2AISObjectMap.contains(theObject);
149 }
150
151 //**************************************************************
152 bool XGUI_Displayer::display(ObjectPtr theObject, bool theUpdateViewer)
153 {
154   bool aDisplayed = false;
155   if (isVisible(theObject)) {
156     aDisplayed = redisplay(theObject, theUpdateViewer);
157   } else {
158     AISObjectPtr anAIS;
159     GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
160     bool isShading = false;
161     if (aPrs.get() != NULL) {
162       GeomScreenParamsPtr aScreen = std::dynamic_pointer_cast<GeomAPI_IScreenParams>(theObject);
163       if (aScreen.get()) {
164         aScreen->setScreenPlane(getScreenPlane());
165         aScreen->setViewScale(getViewScale());
166       }
167       anAIS = aPrs->getAISObject(anAIS);
168       if (anAIS.get())
169         myWorkshop->module()->customizePresentation(theObject, anAIS);
170     } else {
171       anAIS = myWorkshop->module()->createPresentation(theObject);
172       isShading = true;
173     }
174     if (anAIS.get())
175       aDisplayed = display(theObject, anAIS, isShading, theUpdateViewer);
176   }
177   return aDisplayed;
178 }
179
180
181 //**************************************************************
182 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS, ModuleBase_IModule* theModule)
183 {
184   Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
185   if (!aShapePrs.IsNull()) {
186     TopoDS_Shape aShape = aShapePrs->Shape();
187     if (aShape.IsNull())
188       return false;
189     TopAbs_ShapeEnum aType = aShape.ShapeType();
190     if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
191       return false;
192     else {
193       // Check that the presentation is not a sketch
194       return theModule->canBeShaded(theAIS);
195     }
196   }
197   return false;
198 }
199
200 //**************************************************************
201 bool XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
202                              bool isShading, bool theUpdateViewer)
203 {
204   bool aDisplayed = false;
205
206   Handle(AIS_InteractiveContext) aContext = AISContext();
207   if (aContext.IsNull())
208     return aDisplayed;
209
210   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
211   if (!anAISIO.IsNull()) {
212     appendResultObject(theObject, theAIS);
213
214     //bool isCustomized = customizeObject(theObject);
215
216     int aDispMode = isShading? Shading : Wireframe;
217     anAISIO->SetDisplayMode(aDispMode);
218     aContext->Display(anAISIO, aDispMode, 0, false, AIS_DS_Displayed);
219     #ifdef TINSPECTOR
220     if (getCallBack()) getCallBack()->Display(anAISIO);
221     #endif
222     aDisplayed = true;
223
224     emit objectDisplayed(theObject, theAIS);
225     selectionActivate()->activate(anAISIO, theUpdateViewer);
226   }
227   if (theUpdateViewer)
228     updateViewer();
229
230   return aDisplayed;
231 }
232
233 //**************************************************************
234 bool XGUI_Displayer::erase(ObjectPtr theObject, const bool theUpdateViewer)
235 {
236   bool aErased = false;
237   if (!isVisible(theObject))
238     return aErased;
239
240   Handle(AIS_InteractiveContext) aContext = AISContext();
241   if (aContext.IsNull())
242     return aErased;
243
244   AISObjectPtr anObject = myResult2AISObjectMap.value(theObject);
245   if (anObject) {
246     Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
247     if (!anAIS.IsNull()) {
248       emit beforeObjectErase(theObject, anObject);
249       aContext->Remove(anAIS, false/*update viewer*/);
250       #ifdef TINSPECTOR
251       if (getCallBack()) getCallBack()->Remove(anAIS);
252       #endif
253       aErased = true;
254     }
255   }
256   myResult2AISObjectMap.remove(theObject);
257
258 #ifdef DEBUG_DISPLAY
259   std::ostringstream aPtrStr;
260   aPtrStr << theObject.get();
261   qDebug(QString("erase object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
262   qDebug(getResult2AISObjectMapInfo().c_str());
263 #endif
264
265   if (theUpdateViewer)
266     updateViewer();
267
268   return aErased;
269 }
270
271 //**************************************************************
272 bool XGUI_Displayer::redisplay(ObjectPtr theObject, bool theUpdateViewer)
273 {
274   bool aRedisplayed = false;
275   Handle(AIS_InteractiveContext) aContext = AISContext();
276   if (aContext.IsNull())
277     return aRedisplayed;
278
279   if (!isVisible(theObject))
280     return aRedisplayed;
281
282   AISObjectPtr aAISObj = getAISObject(theObject);
283   Handle(AIS_InteractiveObject) aAISIO;
284
285   GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
286   if (aPrs) {
287     GeomScreenParamsPtr aScreen = std::dynamic_pointer_cast<GeomAPI_IScreenParams>(theObject);
288     if (aScreen.get()) {
289       aScreen->setScreenPlane(getScreenPlane());
290       aScreen->setViewScale(getViewScale());
291     }
292     AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
293     if (!aAIS_Obj) {
294       aRedisplayed = erase(theObject, theUpdateViewer);
295       return aRedisplayed;
296     }
297     if (aAIS_Obj != aAISObj) {
298       erase(theObject, theUpdateViewer);
299       appendResultObject(theObject, aAIS_Obj);
300     }
301     aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
302   }
303   else {
304     aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
305   }
306
307   if (!aAISIO.IsNull()) {
308     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
309     if (aResult.get()) {
310       // Set color
311       std::vector<int> aColor;
312       ModelAPI_Tools::getColor(aResult, aColor);
313       if (aColor.size() > 0) {
314         Quantity_Color
315           aCol(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB);
316         aAISIO->SetColor(aCol);
317       }
318       // Set deflection
319       double aDeflection = ModelAPI_Tools::getDeflection(aResult);
320       if ((aDeflection >= 0) && (aDeflection != aAISObj->getDeflection()))
321         aAISObj->setDeflection(aDeflection);
322
323       // Set transparency
324       double aTransparency = ModelAPI_Tools::getTransparency(aResult);
325       if ((aTransparency >= 0) && (aTransparency != aAISObj->getTransparency()))
326         aAISObj->setTransparency(aTransparency);
327
328       // Set Iso-Lines
329       Handle(ModuleBase_ResultPrs) aResPrs = Handle(ModuleBase_ResultPrs)::DownCast(aAISIO);
330       if (!aResPrs.IsNull())
331         aResPrs->updateIsoLines();
332     }
333     //myWorkshop->module()->storeSelection();
334
335 //#ifdef CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
336     //myWorkshop->selector()->deselectPresentation(aAISIO);
337 //#endif
338
339     if (aContext->IsDisplayed(aAISIO))
340       aContext->Redisplay(aAISIO, false);
341     else {
342       aContext->Display(aAISIO, false);
343     }
344       #ifdef TINSPECTOR
345       if (getCallBack()) getCallBack()->Redisplay(aAISIO);
346       #endif
347
348       //if (aNeedToRestoreSelection)
349     //myWorkshop->module()->restoreSelection();
350
351     aRedisplayed = true;
352     #ifdef DEBUG_FEATURE_REDISPLAY
353       qDebug("  Redisplay happens");
354     #endif
355     if (theUpdateViewer)
356       updateViewer();
357   }
358   return aRedisplayed;
359 }
360
361 //**************************************************************
362 void XGUI_Displayer::redisplayObjects()
363 {
364   // redisplay objects visualized in the viewer
365   static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
366   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
367   QObjectPtrList aDisplayed = displayedObjects();
368   QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
369   for (; anIt != aLast; anIt++) {
370     aECreator->sendUpdated(*anIt, EVENT_DISP);
371   }
372   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
373   if (aViewer->isColorScaleVisible()) {
374     aViewer->setupColorScale();
375     aViewer->setColorScaleShown(true);
376   }
377   Events_Loop::loop()->flush(EVENT_DISP);
378 }
379
380 //**************************************************************
381 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
382                                        const bool theUpdateViewer)
383 {
384   //Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
385   //if (!aTrihedron.IsNull())
386   //  deactivateAIS(aTrihedron);
387
388   QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
389   for (; anIt != aLast; anIt++) {
390     selectionActivate()->deactivate(*anIt, false);
391   }
392   //VSV It seems that there is no necessity to update viewer on deactivation
393   //if (theUpdateViewer)
394   //  updateViewer();
395 }
396
397 //**************************************************************
398 bool XGUI_Displayer::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
399 {
400   bool aVisible = false;
401   GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
402   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
403   if (aPrs.get() || aResult.get()) {
404     aVisible = theDisplayer->isVisible(theObject);
405     // compsolid is not visualized in the viewer,
406     // but should have presentation when all sub solids are
407     // visible. It is useful for highlight presentation where compsolid shape is selectable
408     if (!aVisible && aResult.get() && aResult->groupName() == ModelAPI_ResultBody::group()) {
409       ResultBodyPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
410       if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
411         int aNumberOfSubs = aCompsolidResult->numberOfSubs();
412         bool anAllSubsVisible = aNumberOfSubs > 0;
413         for(int i = 0; i < aNumberOfSubs && anAllSubsVisible; i++) {
414           anAllSubsVisible = theDisplayer->isVisible(aCompsolidResult->subResult(i));
415         }
416         aVisible = anAllSubsVisible;
417       }
418     }
419   }
420   // it is possible that feature is presentable and has results, so we should check visibility
421   // of results if presentation is not shown (e.g. Sketch Circle/Arc features)
422   if (!aVisible) {
423     // check if all results of the feature are visible
424     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
425     std::list<ResultPtr> aResults;
426     ModelAPI_Tools::allResults(aFeature, aResults);
427     std::list<ResultPtr>::const_iterator aIt;
428     aVisible = !aResults.empty();
429     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
430       aVisible = aVisible && theDisplayer->isVisible(*aIt);
431     }
432   }
433   return aVisible;
434 }
435
436 //**************************************************************
437 void XGUI_Displayer::setSelected(const  QList<ModuleBase_ViewerPrsPtr>& theValues,
438                                  bool theUpdateViewer)
439 {
440   Handle(AIS_InteractiveContext) aContext = AISContext();
441   if (aContext.IsNull())
442     return;
443   aContext->UnhilightSelected(false);
444   aContext->ClearSelected(false);
445   #ifdef TINSPECTOR
446   if (getCallBack()) getCallBack()->ClearSelected();
447   #endif
448   NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)>>
449     aShapesToBeSelected;
450
451   foreach (ModuleBase_ViewerPrsPtr aPrs, theValues) {
452     const GeomShapePtr& aGeomShape = aPrs->shape();
453     if (aGeomShape.get() && !aGeomShape->isNull()) {
454       const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
455 #ifdef DEBUG_OCCT_SHAPE_SELECTION
456       // problem 1: performance
457       // problem 2: IO is not specified, so the first found owner is selected, as a result
458       // it might belong to another result
459       aContext->AddOrRemoveSelected(aShape, false);
460       #ifdef TINSPECTOR
461       if (getCallBack()) getCallBack()->AddOrRemoveSelected(aShape);
462       #endif
463 #else
464       NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations;
465       if (aShapesToBeSelected.IsBound(aShape))
466         aPresentations = aShapesToBeSelected.Find(aShape);
467       ObjectPtr anObject = aPrs->object();
468       getPresentations(anObject, aPresentations);
469
470       aShapesToBeSelected.Bind(aShape, aPresentations);
471 #endif
472     } else {
473       ObjectPtr anObject = aPrs->object();
474       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
475       if (aResult.get() && isVisible(aResult)) {
476         AISObjectPtr anObj = myResult2AISObjectMap.value(aResult);
477         Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
478         if (!anAIS.IsNull()) {
479           // The methods are replaced in order to provide multi-selection, e.g. restore selection
480           // by activating multi selector widget. It also gives an advantage that the multi
481           // selection in OB gives multi-selection in the viewer
482           //aContext->SetSelected(anAIS, false);
483           // The selection in the context was cleared, so the method sets the objects are selected
484           aContext->AddOrRemoveSelected(anAIS, false);
485           #ifdef TINSPECTOR
486           if (getCallBack()) getCallBack()->AddOrRemoveSelected(anAIS);
487           #endif
488         }
489       }
490     }
491   }
492   if (!aShapesToBeSelected.IsEmpty())
493     XGUI_Displayer::AddOrRemoveSelectedShapes(aContext, aShapesToBeSelected);
494
495   if (theUpdateViewer)
496     updateViewer();
497 }
498
499 //**************************************************************
500 void XGUI_Displayer::clearSelected(const bool theUpdateViewer)
501 {
502   Handle(AIS_InteractiveContext) aContext = AISContext();
503   if (!aContext.IsNull()) {
504     aContext->UnhilightSelected(false);//UnhilightCurrents(false);
505     aContext->ClearSelected(theUpdateViewer);
506     #ifdef TINSPECTOR
507     if (getCallBack()) getCallBack()->ClearSelected();
508     #endif
509   }
510 }
511
512 //**************************************************************
513 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
514 {
515   bool aErased = false;
516   Handle(AIS_InteractiveContext) aContext = AISContext();
517   if (!aContext.IsNull()) {
518 #ifdef OPTIMIZE_PRS
519     foreach(ObjectPtr aObj, myResult2AISObjectMap.objects()) {
520       AISObjectPtr aAISObj = myResult2AISObjectMap.value(aObj);
521 #else
522     foreach(ObjectPtr aObj, myResult2AISObjectMap.keys()) {
523       AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
524 #endif
525       // erase an object
526       Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
527       if (!anIO.IsNull()) {
528         emit beforeObjectErase(aObj, aAISObj);
529         aContext->Remove(anIO, false/*update viewer*/);
530         #ifdef TINSPECTOR
531         if (getCallBack()) getCallBack()->Remove(anIO);
532         #endif
533         aErased = true;
534       }
535     }
536     if (theUpdateViewer)
537       updateViewer();
538   }
539   myResult2AISObjectMap.clear();
540 #ifdef DEBUG_DISPLAY
541   qDebug("eraseAll");
542   qDebug(getResult2AISObjectMapInfo().c_str());
543 #endif
544   return aErased;
545 }
546
547
548 //**************************************************************
549 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
550 {
551 #ifdef OPTIMIZE_PRS
552   return myResult2AISObjectMap.value(theObject);
553 #else
554   AISObjectPtr anIO;
555   if (myResult2AISObjectMap.contains(theObject))
556     anIO = myResult2AISObjectMap[theObject];
557   return anIO;
558 #endif
559 }
560
561 //**************************************************************
562 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
563 {
564   Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
565   return getObject(aRefAIS);
566 }
567
568 //**************************************************************
569 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
570 {
571 #ifdef OPTIMIZE_PRS
572   ObjectPtr anObject = myResult2AISObjectMap.value(theIO);
573 #else
574   ObjectPtr anObject;
575   ResultToAISMap::const_iterator aMapIter = myResult2AISObjectMap.cbegin();
576   for (; aMapIter != myResult2AISObjectMap.cend(); aMapIter++) {
577     const AISObjectPtr& aAIS = aMapIter.value();
578     Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
579     if (anAIS == theIO)
580       anObject = aMapIter.key();
581     if (anObject.get())
582       break;
583   }
584 #endif
585   if (!anObject.get()) {
586     std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
587     if (!theIO.IsNull()) {
588       anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
589     }
590     if (myWorkshop->module())
591       anObject = myWorkshop->module()->findPresentedObject(anAISObj);
592   }
593   return anObject;
594 }
595
596 //**************************************************************
597 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
598 {
599   bool aWasEnabled = isUpdateEnabled();
600   if (isEnabled)
601     myViewerBlockedRecursiveCount--;
602   else
603     myViewerBlockedRecursiveCount++;
604
605 #ifdef DEBUG_VIEWER_BLOCKED_COUNT
606   std::cout << "myViewerBlockedRecursiveCount = " << myViewerBlockedRecursiveCount << std::endl;
607 #endif
608
609   if (myNeedUpdate && isUpdateEnabled()) {
610     updateViewer();
611     myNeedUpdate = false;
612   }
613   return aWasEnabled;
614 }
615
616 //**************************************************************
617 void XGUI_Displayer::updateViewer() const
618 {
619   Handle(AIS_InteractiveContext) aContext = AISContext();
620
621 #ifdef DEBUG_VIEWER_BLOCKED_COUNT
622   std::cout << "updateViewer: " << (myViewerBlockedRecursiveCount == 0 ? " done" : " later")
623             << std::endl;
624 #endif
625
626   if (!aContext.IsNull() && isUpdateEnabled()) {
627     //myWorkshop->viewer()->Zfitall();
628     aContext->UpdateCurrentViewer();
629   } else {
630     myNeedUpdate = true;
631   }
632 }
633
634 //**************************************************************
635 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
636 {
637   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
638   if (!aContext.IsNull() && (myContextId != aContext.get())) {
639     myContextId = aContext.get();
640     if (!myWorkshop->selectionActivate()->isTrihedronActive())
641       selectionActivate()->deactivateTrihedron(true);
642     // Do not modify default drawer. The same is done in ModuleBase_ResultPrs
643     //aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
644     //aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
645
646     // Commented out according to discussion in bug #2825
647     ModuleBase_IViewer::DefaultHighlightDrawer = aContext->HighlightStyle();
648     //Handle(Prs3d_Drawer) aSelStyle = aContext->SelectionStyle();
649     //double aDeflection =
650     //  QString(ModelAPI_ResultConstruction::DEFAULT_DEFLECTION().c_str()).toDouble();
651     //try {
652     //  aDeflection = Config_PropManager::real("Visualization", "construction_deflection");
653     //} catch (...) {}
654
655     //ModuleBase_IViewer::DefaultHighlightDrawer->SetDeviationCoefficient(aDeflection);
656     //aSelStyle->SetDeviationCoefficient(aDeflection);
657
658     Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
659     if (!aTrihedron.IsNull())
660       ModuleBase_Tools::setPointBallHighlighting(aTrihedron.get());
661   }
662   return aContext;
663 }
664
665 //**************************************************************
666 void XGUI_Displayer::setSelectionColor(const std::vector<int>& theColor)
667 {
668   Handle(AIS_InteractiveContext) aContext = AISContext();
669   if (!aContext.IsNull()) {
670     Quantity_Color aQColor(theColor[0] / 255.,
671                            theColor[1] / 255.,
672                            theColor[2] / 255., Quantity_TOC_RGB);
673     aContext->SelectionStyle()->SetColor(aQColor);
674     aContext->SelectionStyle()->PointAspect()->SetColor(aQColor);
675     aContext->SelectionStyle()->LineAspect()->SetColor(aQColor);
676     aContext->HighlightStyle(Prs3d_TypeOfHighlight_LocalSelected)->SetColor(aQColor);
677   }
678 }
679
680
681 //**************************************************************
682 std::vector<int> XGUI_Displayer::selectionColor() const
683 {
684   std::vector<int> aColor;
685   Handle(AIS_InteractiveContext) aContext = AISContext();
686   if (!aContext.IsNull()) {
687     Quantity_Color aQColor = aContext->SelectionStyle()->Color();
688     aColor.push_back((int)(aQColor.Red() * 255));
689     aColor.push_back((int)(aQColor.Green() * 255));
690     aColor.push_back((int)(aQColor.Blue() * 255));
691   }
692   return aColor;
693 }
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*/, 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 aCurAISObj = getAISObject(*anIt);
1045       if (aCurAISObj.get() != NULL) {
1046         Handle(AIS_InteractiveObject) anAIS = aCurAISObj->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                         AIS_DS_Displayed /* xdisplay status */);
1071     #ifdef TINSPECTOR
1072     if (getCallBack()) getCallBack()->Display(aTrihedron);
1073     #endif
1074
1075     if (!aSelectionActive->isTrihedronActive())
1076       aSelectionActive->deactivateTrihedron(false);
1077     else
1078       aSelectionActive->activate(aTrihedron, false);
1079   } else {
1080     aSelectionActive->deactivateTrihedron(false);
1081
1082     aContext->Erase(aTrihedron, Standard_True);
1083     #ifdef TINSPECTOR
1084     if (getCallBack()) getCallBack()->Remove(aTrihedron);
1085     #endif
1086   }
1087 }
1088
1089 //**************************************************************
1090 void XGUI_Displayer::AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
1091                            const NCollection_DataMap<TopoDS_Shape,
1092                            NCollection_Map<Handle(AIS_InteractiveObject)>>& theShapesToBeSelected)
1093 {
1094   NCollection_Map<Handle(AIS_InteractiveObject)> aCompsolidPresentations;
1095   NCollection_Map<Handle(AIS_InteractiveObject)> aSelectedPresentations;
1096
1097   NCollection_List<Handle(SelectBasics_EntityOwner)> anActiveOwners;
1098   theContext->MainSelector()->ActiveOwners(anActiveOwners);
1099   NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (anActiveOwners);
1100   Handle(SelectMgr_EntityOwner) anOwner;
1101
1102   /// It is very important to check that the owner is processed only once and has a map of
1103   /// processed owners because SetSelected works as a switch.
1104   /// If count of calls setSelectec is even, the object stays in the previous state
1105   /// (selected, deselected)
1106   /// OCCT: to write about the problem that active owners method returns one owner several times
1107   QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
1108   for (; anOwnersIt.More(); anOwnersIt.Next()) {
1109     anOwner = anOwnersIt.Value();
1110     if (aSelectedIds.contains((size_t)anOwner.get()))
1111       continue;
1112     aSelectedIds.append((size_t)anOwner.get());
1113
1114     Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
1115     if (!BROwnr.IsNull() && BROwnr->HasShape()) {
1116       const TopoDS_Shape& aShape = BROwnr->Shape();
1117
1118       Handle(ModuleBase_ResultPrs) aResPrs =
1119           Handle(ModuleBase_ResultPrs)::DownCast(BROwnr->Selectable());
1120       TopoDS_Shape aRealShape;
1121       if (!aResPrs.IsNull()) {
1122         aRealShape = aResPrs->originalShape();
1123       }
1124
1125       if (aShape.IsNull())
1126         continue;
1127
1128       Handle(ModuleBase_BRepOwner) aCustomOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
1129
1130       NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)> >
1131                                              ::Iterator aShapeIt(theShapesToBeSelected);
1132       for (; aShapeIt.More(); aShapeIt.Next()) {
1133         const TopoDS_Shape& aParameterShape = aShapeIt.Key();
1134         // In case of compound we cannot rely on simple comparison method.
1135         // If the compound is generated by Group feature then this compound is alwais new.
1136         // So, we have to compare content of these compounds
1137
1138           // isSame should be used here as it does not check orientation of shapes
1139           // despite on isEqual of shapes or IsBound for shape in QMap. Orientation is
1140           // different for Edges shapes in model shape and owner even if this is the same shape
1141         bool isSame = ModuleBase_Tools::isSameShape(aParameterShape, aShape);
1142         if (!isSame) {
1143           // In case of using HideFaces panel we can have instead of an original shape
1144           // a compaund of faces which represent original shape with hidden faces.
1145           // So, we have to compare the parameter with original shape
1146           if (!aRealShape.IsNull())
1147             isSame = ModuleBase_Tools::isSameShape(aParameterShape, aRealShape);
1148         }
1149         if (isSame) {
1150           Handle(AIS_InteractiveObject) anOwnerPresentation =
1151             Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
1152           NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations =
1153             theShapesToBeSelected.Find(aParameterShape);
1154           if (aPresentations.Contains(anOwnerPresentation)) {
1155             theContext->AddOrRemoveSelected(anOwner, Standard_False);
1156             anOwner->SetSelected(Standard_True);
1157             // collect selected presentations to do not select them if compsolid is selected
1158             if (!aSelectedPresentations.Contains(anOwnerPresentation))
1159               aSelectedPresentations.Add(anOwnerPresentation);
1160           }
1161         }
1162         else if (!aCustomOwner.IsNull()) { // CompSolid processing #2219
1163           // shape of owner is compound, but shape to be selected is compsolid, so
1164           // we need to compare shape to AIS presentation of owner(rule of the owner creation)
1165           Handle(AIS_Shape) anOwnerPresentation =
1166                             Handle(AIS_Shape)::DownCast(anOwner->Selectable());
1167           const TopoDS_Shape& aPresentationShape = anOwnerPresentation->Shape();
1168           if (aParameterShape.IsSame(aPresentationShape) &&
1169               !aCompsolidPresentations.Contains(anOwnerPresentation))
1170             aCompsolidPresentations.Add(anOwnerPresentation);
1171         }
1172       }
1173     }
1174   }
1175   // select CompSolid presentations if their owners was not selected yet
1176   NCollection_Map<Handle(AIS_InteractiveObject)>::Iterator anIt (aCompsolidPresentations);
1177   for (; anIt.More(); anIt.Next()) {
1178     if (aSelectedPresentations.Contains(anIt.Value()))
1179       continue;
1180     theContext->AddOrRemoveSelected(anIt.Value(), Standard_False);
1181   }
1182 }
1183
1184 //**************************************************************
1185 XGUI_SelectionActivate* XGUI_Displayer::selectionActivate() const
1186 {
1187   return myWorkshop->selectionActivate();
1188 }
1189
1190 //**************************************************************
1191 GeomPlanePtr XGUI_Displayer::getScreenPlane() const
1192 {
1193   GeomPlanePtr aResult;
1194   Handle(AIS_InteractiveContext) aContext = AISContext();
1195   if (!aContext.IsNull()) {
1196     Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1197     Handle(V3d_View) aView;
1198     for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews()) {
1199       aView = aViewer->ActiveView();
1200       break;
1201     }
1202     if (!aView.IsNull()) {
1203       double aEyeX, aEyeY, aEyeZ;
1204       aView->Eye(aEyeX, aEyeY, aEyeZ);
1205
1206       double aProjX, aProjY, aProjZ;
1207       aView->Proj(aProjX, aProjY, aProjZ);
1208
1209       GeomPointPtr aPnt = GeomPointPtr(new GeomAPI_Pnt(aEyeX, aEyeY, aEyeZ));
1210       GeomDirPtr aDir = GeomDirPtr(new GeomAPI_Dir(aProjX, aProjY, aProjZ));
1211
1212       aResult = GeomPlanePtr(new GeomAPI_Pln(aPnt, aDir));
1213     }
1214   }
1215   return aResult;
1216 }
1217
1218 double XGUI_Displayer::getViewScale() const
1219 {
1220   Handle(AIS_InteractiveContext) aContext = AISContext();
1221   if (!aContext.IsNull()) {
1222     Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1223     Handle(V3d_View) aView;
1224     for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews()) {
1225       aView = aViewer->ActiveView();
1226       break;
1227     }
1228     return aView->Camera()->Scale();
1229   }
1230   return 1;
1231 }