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