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