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