1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: XGUI_Displayer.cpp
4 // Created: 20 Apr 2014
5 // Author: Natalia ERMOLAEVA
7 #include "XGUI_Displayer.h"
8 #include "XGUI_Workshop.h"
9 #include "XGUI_ViewerProxy.h"
11 #include <AppElements_Viewer.h>
13 #include <ModelAPI_Document.h>
14 #include <ModelAPI_Data.h>
15 #include <ModelAPI_Object.h>
16 #include <ModelAPI_Tools.h>
18 #include <ModuleBase_ResultPrs.h>
20 #include <GeomAPI_Shape.h>
21 #include <GeomAPI_IPresentable.h>
22 #include <GeomAPI_ICustomPrs.h>
24 #include <AIS_InteractiveContext.hxx>
25 #include <AIS_LocalContext.hxx>
26 #include <AIS_ListOfInteractive.hxx>
27 #include <AIS_ListIteratorOfListOfInteractive.hxx>
28 #include <AIS_DimensionSelectionMode.hxx>
29 #include <AIS_Shape.hxx>
30 #include <AIS_Dimension.hxx>
31 #include <TColStd_ListIteratorOfListOfInteger.hxx>
32 #include <SelectMgr_ListOfFilter.hxx>
33 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
35 #include <TColStd_MapOfTransient.hxx>
36 #include <TColStd_MapIteratorOfMapOfTransient.hxx>
40 const int MOUSE_SENSITIVITY_IN_PIXEL = 10; ///< defines the local context mouse selection sensitivity
43 // Workaround for bug #25637
44 void displayedObjects(const Handle(AIS_InteractiveContext)& theAIS, AIS_ListOfInteractive& theList)
46 // Get from null point
47 theAIS->DisplayedObjects(theList, true);
48 if (theAIS->HasOpenedContext()) {
49 // get from local context
50 const Handle(AIS_LocalContext)& aLC = theAIS->LocalContext();
51 TColStd_MapOfTransient aMap;
52 int NbDisp = aLC->DisplayedObjects(aMap);
53 TColStd_MapIteratorOfMapOfTransient aIt(aMap);
55 Handle(AIS_InteractiveObject) curIO;
56 Handle(Standard_Transient) Tr;
57 for(; aIt.More(); aIt.Next()){
59 curIO = *((Handle(AIS_InteractiveObject)*) &Tr);
60 theList.Append(curIO);
66 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
67 : myWorkshop(theWorkshop)
71 XGUI_Displayer::~XGUI_Displayer()
75 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
77 return myResult2AISObjectMap.contains(theObject);
80 void XGUI_Displayer::display(ObjectPtr theObject, bool isUpdateViewer)
82 if (isVisible(theObject)) {
83 redisplay(theObject, isUpdateViewer);
87 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
88 bool isShading = false;
89 if (aPrs.get() != NULL) {
90 anAIS = aPrs->getAISObject(AISObjectPtr());
92 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
93 if (aResult.get() != NULL) {
94 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
95 if (aShapePtr.get() != NULL) {
96 anAIS = AISObjectPtr(new GeomAPI_AISObject());
97 anAIS->setImpl(new Handle(AIS_InteractiveObject)(new ModuleBase_ResultPrs(aResult)));
98 //anAIS->createShape(aShapePtr);
104 display(theObject, anAIS, isShading, isUpdateViewer);
108 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS)
110 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
111 if (!aShapePrs.IsNull()) {
112 TopoDS_Shape aShape = aShapePrs->Shape();
113 TopAbs_ShapeEnum aType = aShape.ShapeType();
114 if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
122 void XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
123 bool isShading, bool isUpdateViewer)
125 Handle(AIS_InteractiveContext) aContext = AISContext();
126 if (aContext.IsNull())
129 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
130 if (!anAISIO.IsNull()) {
131 myResult2AISObjectMap[theObject] = theAIS;
132 bool aCanBeShaded = canBeShaded(anAISIO);
133 // In order to avoid extra closing/opening context
135 closeLocalContexts(false);
136 aContext->Display(anAISIO, false);
137 qDebug("### Display %i", (long)anAISIO.Access());
139 aContext->SetDisplayMode(anAISIO, isShading? Shading : Wireframe, false);
140 // Customization of presentation
141 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
142 if (aFeature.get() != NULL) {
143 GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
144 if (aCustPrs.get() != NULL)
145 aCustPrs->customisePresentation(theAIS);
149 activateObjects(myActiveSelectionModes);
156 void XGUI_Displayer::erase(ObjectPtr theObject, const bool isUpdateViewer)
158 if (!isVisible(theObject))
161 Handle(AIS_InteractiveContext) aContext = AISContext();
162 if (aContext.IsNull())
164 AISObjectPtr anObject = myResult2AISObjectMap[theObject];
166 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
167 if (!anAIS.IsNull()) {
168 aContext->Remove(anAIS, isUpdateViewer);
171 myResult2AISObjectMap.remove(theObject);
174 void XGUI_Displayer::redisplay(ObjectPtr theObject, bool isUpdateViewer)
176 if (!isVisible(theObject))
179 AISObjectPtr aAISObj = getAISObject(theObject);
180 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
182 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
184 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
186 erase(theObject, isUpdateViewer);
189 if (aAIS_Obj != aAISObj) {
190 myResult2AISObjectMap[theObject] = aAIS_Obj;
192 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
195 if (!aAISIO.IsNull()) {
196 Handle(AIS_InteractiveContext) aContext = AISContext();
197 if (aContext.IsNull())
199 bool aToSelect = aContext->IsSelected(aAISIO);
200 aContext->Redisplay(aAISIO, false);
201 // Restore selection state after redisplay
203 aContext->SetSelected(aAISIO);
209 void XGUI_Displayer::deactivate(ObjectPtr theObject)
211 if (isVisible(theObject)) {
212 Handle(AIS_InteractiveContext) aContext = AISContext();
213 if (aContext.IsNull())
216 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
217 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
218 aContext->Deactivate(anAIS);
219 qDebug("### Deactivate obj %i", (long)anAIS.Access());
223 void XGUI_Displayer::activate(ObjectPtr theFeature)
225 activate(theFeature, myActiveSelectionModes);
228 void XGUI_Displayer::activate(ObjectPtr theObject, const QIntList& theModes)
230 if (isVisible(theObject)) {
231 Handle(AIS_InteractiveContext) aContext = AISContext();
232 if (aContext.IsNull())
235 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
236 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
237 aContext->Deactivate(anAIS);
238 aContext->Load(anAIS, -1, true);
239 // In order to clear active modes list
240 if (theModes.size() > 0) {
241 foreach(int aMode, theModes) {
242 //aContext->Load(anAIS, aMode, true);
243 aContext->Activate(anAIS, aMode);
244 qDebug("### 1. Activate obj %i, %i", (long)anAIS.Access(), aMode);
247 //aContext->Load(anAIS, 0, true);
248 aContext->Activate(anAIS);
249 qDebug("### 2. Activate obj %i", (long)anAIS.Access());
254 void XGUI_Displayer::activateObjects(const QIntList& theModes)
256 // In order to avoid doblications of selection modes
258 foreach (int aMode, theModes) {
259 if (!aNewModes.contains(aMode))
260 aNewModes.append(aMode);
262 myActiveSelectionModes = aNewModes;
263 Handle(AIS_InteractiveContext) aContext = AISContext();
264 // Open local context if there is no one
265 if (!aContext->HasOpenedContext())
268 //aContext->UseDisplayedObjects();
269 //myUseExternalObjects = true;
271 AIS_ListOfInteractive aPrsList;
272 displayedObjects(aContext, aPrsList);
274 Handle(AIS_Trihedron) aTrihedron;
275 AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
276 Handle(AIS_InteractiveObject) anAISIO;
277 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
278 anAISIO = aLIt.Value();
279 aContext->Load(anAISIO, -1, true);
280 aContext->Deactivate(anAISIO);
281 aTrihedron = Handle(AIS_Trihedron)::DownCast(anAISIO);
282 //Deactivate trihedron which can be activated in local selector
283 if (aTrihedron.IsNull()) {
284 //aContext->Load(anAISIO, -1, true);
285 // In order to clear active modes list
286 if (myActiveSelectionModes.size() == 0) {
287 //aContext->Load(anAISIO, 0, true);
288 aContext->Activate(anAISIO);
289 qDebug("### 2. Activate all %i", (long)anAISIO.Access());
291 foreach(int aMode, myActiveSelectionModes) {
292 //aContext->Load(anAISIO, aMode, true);
293 aContext->Activate(anAISIO, aMode);
294 qDebug("### 1. Activate all %i, %i", (long)anAISIO.Access(), aMode);
302 void XGUI_Displayer::deactivateObjects()
304 myActiveSelectionModes.clear();
305 Handle(AIS_InteractiveContext) aContext = AISContext();
306 // Open local context if there is no one
307 if (!aContext->HasOpenedContext())
310 //aContext->NotUseDisplayedObjects();
311 AIS_ListOfInteractive aPrsList;
312 displayedObjects(aContext, aPrsList);
314 AIS_ListIteratorOfListOfInteractive aLIt;
315 //Handle(AIS_Trihedron) aTrihedron;
316 Handle(AIS_InteractiveObject) anAISIO;
317 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
318 anAISIO = aLIt.Value();
319 aContext->Deactivate(anAISIO);
320 //aTrihedron = Handle(AIS_Trihedron)::DownCast(anAISIO);
321 //if (aTrihedron.IsNull()) {
322 // qDebug("### Deactivate all %i", (long)anAISIO.Access());
323 // //aContext->Activate(anAISIO);
328 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
330 Handle(AIS_InteractiveContext) aContext = AISContext();
331 if (aContext.IsNull())
333 if (!isVisible(theObject))
336 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
337 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
339 TColStd_ListOfInteger aModes;
340 aContext->ActivatedModes(anAIS, aModes);
341 return aModes.Extent() > 0;
344 void XGUI_Displayer::setSelected(const QObjectPtrList& theResults, const bool isUpdateViewer)
346 Handle(AIS_InteractiveContext) aContext = AISContext();
347 if (aContext.IsNull())
349 if (aContext->HasOpenedContext()) {
350 aContext->UnhilightSelected();
351 aContext->ClearSelected();
352 foreach(ObjectPtr aResult, theResults) {
353 if (isVisible(aResult)) {
354 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
355 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
357 aContext->SetSelected(anAIS, false);
361 aContext->UnhilightCurrents();
362 aContext->ClearCurrents();
363 foreach(ObjectPtr aResult, theResults) {
364 if (isVisible(aResult)) {
365 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
366 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
368 aContext->SetCurrentObject(anAIS, false);
377 void XGUI_Displayer::clearSelected()
379 Handle(AIS_InteractiveContext) aContext = AISContext();
381 aContext->UnhilightCurrents(false);
382 aContext->ClearSelected();
386 void XGUI_Displayer::eraseAll(const bool isUpdateViewer)
388 Handle(AIS_InteractiveContext) aContext = AISContext();
389 if (aContext.IsNull())
392 foreach (AISObjectPtr aAISObj, myResult2AISObjectMap) {
394 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
396 aContext->Remove(anIO, false);
398 myResult2AISObjectMap.clear();
403 void XGUI_Displayer::openLocalContext()
405 Handle(AIS_InteractiveContext) aContext = AISContext();
406 if (aContext.IsNull())
408 // Open local context if there is no one
409 if (!aContext->HasOpenedContext()) {
410 // Preserve selected objects
411 //AIS_ListOfInteractive aAisList;
412 //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
413 // aAisList.Append(aContext->Current());
415 // get the filters from the global context and append them to the local context
416 // a list of filters in the global context is not cleared and should be cleared here
417 SelectMgr_ListOfFilter aFilters;
418 aFilters.Assign(aContext->Filters());
419 // it is important to remove the filters in the global context, because there is a code
420 // in the closeLocalContex, which restore the global context filters
421 aContext->RemoveFilters();
423 //aContext->ClearCurrents();
424 aContext->OpenLocalContext();
425 qDebug("### Open context");
426 //aContext->NotUseDisplayedObjects();
428 //myUseExternalObjects = false;
430 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
431 for (;aIt.More(); aIt.Next()) {
432 aContext->AddFilter(aIt.Value());
435 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
436 //for(; aIt2.More(); aIt2.Next()) {
437 // aContext->SetSelected(aIt2.Value(), false);
442 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
444 Handle(AIS_InteractiveContext) aContext = AISContext();
445 if ( (!aContext.IsNull()) && (aContext->HasOpenedContext()) ) {
446 // Preserve selected objects
447 //AIS_ListOfInteractive aAisList;
448 //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
449 // aAisList.Append(aContext->SelectedInteractive());
451 // get the filters from the local context and append them to the global context
452 // a list of filters in the local context is cleared
453 SelectMgr_ListOfFilter aFilters;
454 aFilters.Assign(aContext->Filters());
456 //aContext->ClearSelected();
457 aContext->CloseAllContexts(false);
458 qDebug("### Close context");
460 // Redisplay all object if they were displayed in localContext
461 Handle(AIS_InteractiveObject) aAISIO;
462 foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
463 aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
464 if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
465 aContext->Display(aAISIO, false);
466 aContext->SetDisplayMode(aAISIO, Shading, false);
470 // Append the filters from the local selection in the global selection context
471 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
472 for (;aIt.More(); aIt.Next()) {
473 Handle(SelectMgr_Filter) aFilter = aIt.Value();
474 aContext->AddFilter(aFilter);
479 //myUseExternalObjects = false;
482 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
483 //for(; aIt2.More(); aIt2.Next()) {
484 // if (aContext->IsDisplayed(aIt2.Value()))
485 // aContext->SetCurrentObject(aIt2.Value(), false);
490 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
493 if (myResult2AISObjectMap.contains(theObject))
494 anIO = myResult2AISObjectMap[theObject];
498 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
500 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
501 return getObject(aRefAIS);
504 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
507 foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
508 AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
509 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
516 void XGUI_Displayer::updateViewer()
518 Handle(AIS_InteractiveContext) aContext = AISContext();
519 if (!aContext.IsNull())
520 aContext->UpdateCurrentViewer();
523 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
525 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
526 if ((!aContext.IsNull()) && (!aContext->HasOpenedContext())) {
527 aContext->OpenLocalContext();
528 qDebug("### Open context");
533 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
535 Handle(AIS_InteractiveContext) aContext = AISContext();
536 if (myAndFilter.IsNull() && !aContext.IsNull()) {
537 myAndFilter = new SelectMgr_AndFilter();
538 aContext->AddFilter(myAndFilter);
543 void XGUI_Displayer::displayAIS(AISObjectPtr theAIS, bool isUpdate)
545 Handle(AIS_InteractiveContext) aContext = AISContext();
546 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
547 if (!anAISIO.IsNull()) {
548 aContext->Display(anAISIO, isUpdate);
549 if (aContext->HasOpenedContext()) {
550 //if (myUseExternalObjects) {
551 if (myActiveSelectionModes.size() == 0)
552 aContext->Activate(anAISIO);
554 foreach(int aMode, myActiveSelectionModes) {
555 aContext->Activate(anAISIO, aMode);
563 void XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool isUpdate)
565 Handle(AIS_InteractiveContext) aContext = AISContext();
566 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
567 if (!anAISIO.IsNull()) {
568 aContext->Remove(anAISIO, isUpdate);
573 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool toUpdate)
575 if (theMode == NoMode)
578 Handle(AIS_InteractiveContext) aContext = AISContext();
579 if (aContext.IsNull())
582 AISObjectPtr aAISObj = getAISObject(theObject);
586 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
587 aContext->SetDisplayMode(aAISIO, theMode, toUpdate);
590 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
592 Handle(AIS_InteractiveContext) aContext = AISContext();
593 if (aContext.IsNull())
596 AISObjectPtr aAISObj = getAISObject(theObject);
600 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
601 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
604 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
606 Handle(AIS_InteractiveContext) aContext = AISContext();
607 if (aContext.IsNull())
609 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
610 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
611 for (; aIt.More(); aIt.Next()) {
612 if (theFilter.Access() == aIt.Value().Access())
615 GetFilter()->Add(theFilter);
618 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
620 Handle(AIS_InteractiveContext) aContext = AISContext();
621 if (aContext.IsNull())
623 GetFilter()->Remove(theFilter);
626 void XGUI_Displayer::removeFilters()
628 Handle(AIS_InteractiveContext) aContext = AISContext();
629 if (aContext.IsNull())
631 GetFilter()->Clear();
634 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
636 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
637 foreach(ObjectPtr aObj, aDispList) {
638 if (!theList.contains(aObj))
641 foreach(ObjectPtr aObj, theList) {
642 if (!isVisible(aObj))
643 display(aObj, false);