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