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"
10 #include "XGUI_SelectionMgr.h"
11 #include "XGUI_Selection.h"
13 #include <AppElements_Viewer.h>
15 #include <ModelAPI_Document.h>
16 #include <ModelAPI_Data.h>
17 #include <ModelAPI_Object.h>
18 #include <ModelAPI_Tools.h>
20 #include <ModuleBase_ResultPrs.h>
22 #include <GeomAPI_Shape.h>
23 #include <GeomAPI_IPresentable.h>
24 #include <GeomAPI_ICustomPrs.h>
26 #include <AIS_InteractiveContext.hxx>
27 #include <AIS_LocalContext.hxx>
28 #include <AIS_ListOfInteractive.hxx>
29 #include <AIS_ListIteratorOfListOfInteractive.hxx>
30 #include <AIS_DimensionSelectionMode.hxx>
31 #include <AIS_Shape.hxx>
32 #include <AIS_Dimension.hxx>
33 #include <TColStd_ListIteratorOfListOfInteger.hxx>
34 #include <SelectMgr_ListOfFilter.hxx>
35 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
37 #include <TColStd_MapOfTransient.hxx>
38 #include <TColStd_MapIteratorOfMapOfTransient.hxx>
42 const int MOUSE_SENSITIVITY_IN_PIXEL = 10; ///< defines the local context mouse selection sensitivity
45 // Workaround for bug #25637
46 void displayedObjects(const Handle(AIS_InteractiveContext)& theAIS, AIS_ListOfInteractive& theList)
48 // Get from null point
49 theAIS->DisplayedObjects(theList, true);
50 if (theAIS->HasOpenedContext()) {
51 // get from local context
52 const Handle(AIS_LocalContext)& aLC = theAIS->LocalContext();
53 TColStd_MapOfTransient aMap;
54 int NbDisp = aLC->DisplayedObjects(aMap);
55 TColStd_MapIteratorOfMapOfTransient aIt(aMap);
57 Handle(AIS_InteractiveObject) curIO;
58 Handle(Standard_Transient) Tr;
59 for(; aIt.More(); aIt.Next()){
61 curIO = *((Handle(AIS_InteractiveObject)*) &Tr);
62 theList.Append(curIO);
68 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
69 : myWorkshop(theWorkshop)
71 enableUpdateViewer(true);
74 XGUI_Displayer::~XGUI_Displayer()
78 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
80 return myResult2AISObjectMap.contains(theObject);
83 void XGUI_Displayer::display(ObjectPtr theObject, bool isUpdateViewer)
85 if (isVisible(theObject)) {
86 redisplay(theObject, isUpdateViewer);
90 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
91 bool isShading = false;
92 if (aPrs.get() != NULL) {
93 anAIS = aPrs->getAISObject(AISObjectPtr());
95 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
96 if (aResult.get() != NULL) {
97 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
98 if (aShapePtr.get() != NULL) {
99 anAIS = AISObjectPtr(new GeomAPI_AISObject());
100 anAIS->setImpl(new Handle(AIS_InteractiveObject)(new ModuleBase_ResultPrs(aResult)));
101 //anAIS->createShape(aShapePtr);
107 display(theObject, anAIS, isShading, isUpdateViewer);
111 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS)
113 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
114 if (!aShapePrs.IsNull()) {
115 TopoDS_Shape aShape = aShapePrs->Shape();
116 TopAbs_ShapeEnum aType = aShape.ShapeType();
117 if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
125 void XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
126 bool isShading, bool isUpdateViewer)
128 Handle(AIS_InteractiveContext) aContext = AISContext();
129 if (aContext.IsNull())
132 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
133 if (!anAISIO.IsNull()) {
134 myResult2AISObjectMap[theObject] = theAIS;
135 bool aCanBeShaded = ::canBeShaded(anAISIO);
136 // In order to avoid extra closing/opening context
137 SelectMgr_IndexedMapOfOwner aSelectedOwners;
139 myWorkshop->selector()->selection()->selectedOwners(aSelectedOwners);
140 closeLocalContexts(false);
142 aContext->Display(anAISIO, false);
143 qDebug("### Display %i", (long)anAISIO.Access());
145 aContext->SetDisplayMode(anAISIO, isShading? Shading : Wireframe, false);
146 // Customization of presentation
147 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
148 if (aFeature.get() != NULL) {
149 GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
150 if (aCustPrs.get() != NULL)
151 aCustPrs->customisePresentation(theAIS);
155 activateObjects(myActiveSelectionModes);
156 myWorkshop->selector()->setSelectedOwners(aSelectedOwners, false);
163 void XGUI_Displayer::erase(ObjectPtr theObject, const bool isUpdateViewer)
165 if (!isVisible(theObject))
168 Handle(AIS_InteractiveContext) aContext = AISContext();
169 if (aContext.IsNull())
171 AISObjectPtr anObject = myResult2AISObjectMap[theObject];
173 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
174 if (!anAIS.IsNull()) {
175 aContext->Remove(anAIS, isUpdateViewer);
178 myResult2AISObjectMap.remove(theObject);
181 void XGUI_Displayer::redisplay(ObjectPtr theObject, bool isUpdateViewer)
183 if (!isVisible(theObject))
186 AISObjectPtr aAISObj = getAISObject(theObject);
187 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
189 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
191 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
193 erase(theObject, isUpdateViewer);
196 if (aAIS_Obj != aAISObj) {
197 myResult2AISObjectMap[theObject] = aAIS_Obj;
199 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
202 if (!aAISIO.IsNull()) {
203 Handle(AIS_InteractiveContext) aContext = AISContext();
204 if (aContext.IsNull())
206 aContext->Redisplay(aAISIO, false);
212 void XGUI_Displayer::deactivate(ObjectPtr theObject)
214 if (isVisible(theObject)) {
215 Handle(AIS_InteractiveContext) aContext = AISContext();
216 if (aContext.IsNull())
219 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
220 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
221 aContext->Deactivate(anAIS);
222 qDebug("### Deactivate obj %i", (long)anAIS.Access());
226 void XGUI_Displayer::activate(ObjectPtr theFeature)
228 activate(theFeature, myActiveSelectionModes);
231 void XGUI_Displayer::activate(ObjectPtr theObject, const QIntList& theModes)
233 if (isVisible(theObject)) {
234 Handle(AIS_InteractiveContext) aContext = AISContext();
235 if (aContext.IsNull())
238 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
239 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
240 aContext->Deactivate(anAIS);
241 aContext->Load(anAIS, -1, true);
242 // In order to clear active modes list
243 if (theModes.size() > 0) {
244 foreach(int aMode, theModes) {
245 //aContext->Load(anAIS, aMode, true);
246 aContext->Activate(anAIS, aMode);
247 qDebug("### 1. Activate obj %i, %i", (long)anAIS.Access(), aMode);
250 //aContext->Load(anAIS, 0, true);
251 aContext->Activate(anAIS);
252 qDebug("### 2. Activate obj %i", (long)anAIS.Access());
257 void XGUI_Displayer::getModesOfActivation(ObjectPtr theObject, QIntList& theModes)
259 if (!isVisible(theObject))
262 Handle(AIS_InteractiveContext) aContext = AISContext();
263 if (aContext.IsNull())
266 AISObjectPtr aAISObj = getAISObject(theObject);
268 if (aAISObj.get() != NULL) {
269 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
270 TColStd_ListOfInteger aTColModes;
271 aContext->ActivatedModes(anAISIO, aTColModes);
272 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
273 for (; itr.More(); itr.Next() ) {
274 theModes.append(itr.Value());
279 void XGUI_Displayer::activateObjects(const QIntList& theModes)
281 // In order to avoid doblications of selection modes
283 foreach (int aMode, theModes) {
284 if (!aNewModes.contains(aMode))
285 aNewModes.append(aMode);
287 myActiveSelectionModes = aNewModes;
288 Handle(AIS_InteractiveContext) aContext = AISContext();
289 // Open local context if there is no one
290 if (!aContext->HasOpenedContext())
293 //aContext->UseDisplayedObjects();
294 //myUseExternalObjects = true;
296 AIS_ListOfInteractive aPrsList;
297 displayedObjects(aContext, aPrsList);
299 Handle(AIS_Trihedron) aTrihedron;
300 AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
301 Handle(AIS_InteractiveObject) anAISIO;
302 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
303 anAISIO = aLIt.Value();
304 aContext->Load(anAISIO, -1, true);
305 aContext->Deactivate(anAISIO);
306 aTrihedron = Handle(AIS_Trihedron)::DownCast(anAISIO);
307 //Deactivate trihedron which can be activated in local selector
308 if (aTrihedron.IsNull()) {
309 //aContext->Load(anAISIO, -1, true);
310 // In order to clear active modes list
311 if (myActiveSelectionModes.size() == 0) {
312 //aContext->Load(anAISIO, 0, true);
313 aContext->Activate(anAISIO);
314 qDebug("### 2. Activate all %i", (long)anAISIO.Access());
316 foreach(int aMode, myActiveSelectionModes) {
317 //aContext->Load(anAISIO, aMode, true);
318 aContext->Activate(anAISIO, aMode);
319 qDebug("### 1. Activate all %i, %i", (long)anAISIO.Access(), aMode);
327 void XGUI_Displayer::deactivateObjects()
329 myActiveSelectionModes.clear();
330 Handle(AIS_InteractiveContext) aContext = AISContext();
331 // Open local context if there is no one
332 if (!aContext->HasOpenedContext())
335 //aContext->NotUseDisplayedObjects();
336 AIS_ListOfInteractive aPrsList;
337 displayedObjects(aContext, aPrsList);
339 AIS_ListIteratorOfListOfInteractive aLIt;
340 //Handle(AIS_Trihedron) aTrihedron;
341 Handle(AIS_InteractiveObject) anAISIO;
342 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
343 anAISIO = aLIt.Value();
344 aContext->Deactivate(anAISIO);
345 //aTrihedron = Handle(AIS_Trihedron)::DownCast(anAISIO);
346 //if (aTrihedron.IsNull()) {
347 // qDebug("### Deactivate all %i", (long)anAISIO.Access());
348 // //aContext->Activate(anAISIO);
353 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
355 Handle(AIS_InteractiveContext) aContext = AISContext();
356 if (aContext.IsNull())
358 if (!isVisible(theObject))
361 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
362 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
364 TColStd_ListOfInteger aModes;
365 aContext->ActivatedModes(anAIS, aModes);
366 return aModes.Extent() > 0;
369 void XGUI_Displayer::setSelected(const QObjectPtrList& theResults, const bool isUpdateViewer)
371 Handle(AIS_InteractiveContext) aContext = AISContext();
372 if (aContext.IsNull())
374 if (aContext->HasOpenedContext()) {
375 aContext->UnhilightSelected();
376 aContext->ClearSelected();
377 foreach(ObjectPtr aResult, theResults) {
378 if (isVisible(aResult)) {
379 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
380 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
382 aContext->SetSelected(anAIS, false);
386 aContext->UnhilightCurrents();
387 aContext->ClearCurrents();
388 foreach(ObjectPtr aResult, theResults) {
389 if (isVisible(aResult)) {
390 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
391 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
393 aContext->SetCurrentObject(anAIS, false);
402 void XGUI_Displayer::clearSelected()
404 Handle(AIS_InteractiveContext) aContext = AISContext();
406 aContext->UnhilightCurrents(false);
407 aContext->ClearSelected();
411 void XGUI_Displayer::eraseAll(const bool isUpdateViewer)
413 Handle(AIS_InteractiveContext) aContext = AISContext();
414 if (aContext.IsNull())
417 foreach (AISObjectPtr aAISObj, myResult2AISObjectMap) {
419 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
421 aContext->Remove(anIO, false);
423 myResult2AISObjectMap.clear();
428 void XGUI_Displayer::openLocalContext()
430 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
431 if (aContext.IsNull())
433 // Open local context if there is no one
434 if (!aContext->HasOpenedContext()) {
435 // Preserve selected objects
436 //AIS_ListOfInteractive aAisList;
437 //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
438 // aAisList.Append(aContext->Current());
440 // get the filters from the global context and append them to the local context
441 // a list of filters in the global context is not cleared and should be cleared here
442 SelectMgr_ListOfFilter aFilters;
443 aFilters.Assign(aContext->Filters());
444 // it is important to remove the filters in the global context, because there is a code
445 // in the closeLocalContex, which restore the global context filters
446 aContext->RemoveFilters();
448 //aContext->ClearCurrents();
449 aContext->OpenLocalContext();
450 qDebug("### Open context");
451 //aContext->NotUseDisplayedObjects();
453 //myUseExternalObjects = false;
455 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
456 for (;aIt.More(); aIt.Next()) {
457 aContext->AddFilter(aIt.Value());
460 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
461 //for(; aIt2.More(); aIt2.Next()) {
462 // aContext->SetSelected(aIt2.Value(), false);
467 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
469 Handle(AIS_InteractiveContext) aContext = AISContext();
470 if ( (!aContext.IsNull()) && (aContext->HasOpenedContext()) ) {
471 // Preserve selected objects
472 //AIS_ListOfInteractive aAisList;
473 //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
474 // aAisList.Append(aContext->SelectedInteractive());
476 // get the filters from the local context and append them to the global context
477 // a list of filters in the local context is cleared
478 SelectMgr_ListOfFilter aFilters;
479 aFilters.Assign(aContext->Filters());
481 //aContext->ClearSelected();
482 aContext->CloseAllContexts(false);
483 qDebug("### Close context");
485 // Redisplay all object if they were displayed in localContext
486 Handle(AIS_InteractiveObject) aAISIO;
487 foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
488 aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
489 if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
490 aContext->Display(aAISIO, false);
491 aContext->SetDisplayMode(aAISIO, Shading, false);
495 // Append the filters from the local selection in the global selection context
496 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
497 for (;aIt.More(); aIt.Next()) {
498 Handle(SelectMgr_Filter) aFilter = aIt.Value();
499 aContext->AddFilter(aFilter);
504 //myUseExternalObjects = false;
507 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
508 //for(; aIt2.More(); aIt2.Next()) {
509 // if (aContext->IsDisplayed(aIt2.Value()))
510 // aContext->SetCurrentObject(aIt2.Value(), false);
515 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
518 if (myResult2AISObjectMap.contains(theObject))
519 anIO = myResult2AISObjectMap[theObject];
523 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
525 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
526 return getObject(aRefAIS);
529 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
532 foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
533 AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
534 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
541 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
543 bool aWasEnabled = myEnableUpdateViewer;
545 myEnableUpdateViewer = isEnabled;
550 void XGUI_Displayer::updateViewer()
552 Handle(AIS_InteractiveContext) aContext = AISContext();
553 if (!aContext.IsNull() && myEnableUpdateViewer)
554 aContext->UpdateCurrentViewer();
557 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
559 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
560 if ((!aContext.IsNull()) && (!aContext->HasOpenedContext())) {
561 aContext->OpenLocalContext();
562 qDebug("### Open context");
567 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
569 Handle(AIS_InteractiveContext) aContext = AISContext();
570 if (myAndFilter.IsNull() && !aContext.IsNull()) {
571 myAndFilter = new SelectMgr_AndFilter();
572 aContext->AddFilter(myAndFilter);
577 void XGUI_Displayer::displayAIS(AISObjectPtr theAIS, bool isUpdate)
579 Handle(AIS_InteractiveContext) aContext = AISContext();
580 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
581 if (!anAISIO.IsNull()) {
582 aContext->Display(anAISIO, isUpdate);
583 if (aContext->HasOpenedContext()) {
584 //if (myUseExternalObjects) {
585 if (myActiveSelectionModes.size() == 0)
586 aContext->Activate(anAISIO);
588 foreach(int aMode, myActiveSelectionModes) {
589 aContext->Activate(anAISIO, aMode);
597 void XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool isUpdate)
599 Handle(AIS_InteractiveContext) aContext = AISContext();
600 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
601 if (!anAISIO.IsNull()) {
602 aContext->Remove(anAISIO, isUpdate);
607 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool toUpdate)
609 if (theMode == NoMode)
612 Handle(AIS_InteractiveContext) aContext = AISContext();
613 if (aContext.IsNull())
616 AISObjectPtr aAISObj = getAISObject(theObject);
620 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
621 bool aCanBeShaded = ::canBeShaded(aAISIO);
622 // In order to avoid extra closing/opening context
623 SelectMgr_IndexedMapOfOwner aSelectedOwners;
625 myWorkshop->selector()->selection()->selectedOwners(aSelectedOwners);
626 closeLocalContexts(false);
628 aContext->SetDisplayMode(aAISIO, theMode, false);
631 activateObjects(myActiveSelectionModes);
632 myWorkshop->selector()->setSelectedOwners(aSelectedOwners, false);
638 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
640 Handle(AIS_InteractiveContext) aContext = AISContext();
641 if (aContext.IsNull())
644 AISObjectPtr aAISObj = getAISObject(theObject);
648 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
649 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
652 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
654 Handle(AIS_InteractiveContext) aContext = AISContext();
655 if (aContext.IsNull())
657 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
658 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
659 for (; aIt.More(); aIt.Next()) {
660 if (theFilter.Access() == aIt.Value().Access())
663 GetFilter()->Add(theFilter);
666 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
668 Handle(AIS_InteractiveContext) aContext = AISContext();
669 if (aContext.IsNull())
671 GetFilter()->Remove(theFilter);
674 void XGUI_Displayer::removeFilters()
676 Handle(AIS_InteractiveContext) aContext = AISContext();
677 if (aContext.IsNull())
679 GetFilter()->Clear();
682 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
684 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
685 foreach(ObjectPtr aObj, aDispList) {
686 if (!theList.contains(aObj))
689 foreach(ObjectPtr aObj, theList) {
690 if (!isVisible(aObj))
691 display(aObj, false);
696 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
698 if (!isVisible(theObject))
701 AISObjectPtr aAISObj = getAISObject(theObject);
702 if (aAISObj.get() == NULL)
705 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
706 return ::canBeShaded(anAIS);