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 bool aToSelect = aContext->IsSelected(aAISIO);
207 aContext->Redisplay(aAISIO, false);
208 // Restore selection state after redisplay
210 aContext->SetSelected(aAISIO, false);
216 void XGUI_Displayer::deactivate(ObjectPtr theObject)
218 if (isVisible(theObject)) {
219 Handle(AIS_InteractiveContext) aContext = AISContext();
220 if (aContext.IsNull())
223 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
224 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
225 aContext->Deactivate(anAIS);
226 qDebug("### Deactivate obj %i", (long)anAIS.Access());
230 void XGUI_Displayer::activate(ObjectPtr theFeature)
232 activate(theFeature, myActiveSelectionModes);
235 void XGUI_Displayer::activate(ObjectPtr theObject, const QIntList& theModes)
237 if (isVisible(theObject)) {
238 Handle(AIS_InteractiveContext) aContext = AISContext();
239 if (aContext.IsNull())
242 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
243 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
244 aContext->Deactivate(anAIS);
245 aContext->Load(anAIS, -1, true);
246 // In order to clear active modes list
247 if (theModes.size() > 0) {
248 foreach(int aMode, theModes) {
249 //aContext->Load(anAIS, aMode, true);
250 aContext->Activate(anAIS, aMode);
251 qDebug("### 1. Activate obj %i, %i", (long)anAIS.Access(), aMode);
254 //aContext->Load(anAIS, 0, true);
255 aContext->Activate(anAIS);
256 qDebug("### 2. Activate obj %i", (long)anAIS.Access());
261 void XGUI_Displayer::getModesOfActivation(ObjectPtr theObject, QIntList& theModes)
263 if (!isVisible(theObject))
266 Handle(AIS_InteractiveContext) aContext = AISContext();
267 if (aContext.IsNull())
270 AISObjectPtr aAISObj = getAISObject(theObject);
272 if (aAISObj.get() != NULL) {
273 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
274 TColStd_ListOfInteger aTColModes;
275 aContext->ActivatedModes(anAISIO, aTColModes);
276 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
277 for (; itr.More(); itr.Next() ) {
278 theModes.append(itr.Value());
283 void XGUI_Displayer::activateObjects(const QIntList& theModes)
285 // In order to avoid doblications of selection modes
287 foreach (int aMode, theModes) {
288 if (!aNewModes.contains(aMode))
289 aNewModes.append(aMode);
291 myActiveSelectionModes = aNewModes;
292 Handle(AIS_InteractiveContext) aContext = AISContext();
293 // Open local context if there is no one
294 if (!aContext->HasOpenedContext())
297 //aContext->UseDisplayedObjects();
298 //myUseExternalObjects = true;
300 AIS_ListOfInteractive aPrsList;
301 displayedObjects(aContext, aPrsList);
303 Handle(AIS_Trihedron) aTrihedron;
304 AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
305 Handle(AIS_InteractiveObject) anAISIO;
306 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
307 anAISIO = aLIt.Value();
308 aContext->Load(anAISIO, -1, true);
309 aContext->Deactivate(anAISIO);
310 aTrihedron = Handle(AIS_Trihedron)::DownCast(anAISIO);
311 //Deactivate trihedron which can be activated in local selector
312 if (aTrihedron.IsNull()) {
313 //aContext->Load(anAISIO, -1, true);
314 // In order to clear active modes list
315 if (myActiveSelectionModes.size() == 0) {
316 //aContext->Load(anAISIO, 0, true);
317 aContext->Activate(anAISIO);
318 qDebug("### 2. Activate all %i", (long)anAISIO.Access());
320 foreach(int aMode, myActiveSelectionModes) {
321 //aContext->Load(anAISIO, aMode, true);
322 aContext->Activate(anAISIO, aMode);
323 qDebug("### 1. Activate all %i, %i", (long)anAISIO.Access(), aMode);
331 void XGUI_Displayer::deactivateObjects()
333 myActiveSelectionModes.clear();
334 Handle(AIS_InteractiveContext) aContext = AISContext();
335 // Open local context if there is no one
336 if (!aContext->HasOpenedContext())
339 //aContext->NotUseDisplayedObjects();
340 AIS_ListOfInteractive aPrsList;
341 displayedObjects(aContext, aPrsList);
343 AIS_ListIteratorOfListOfInteractive aLIt;
344 //Handle(AIS_Trihedron) aTrihedron;
345 Handle(AIS_InteractiveObject) anAISIO;
346 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
347 anAISIO = aLIt.Value();
348 aContext->Deactivate(anAISIO);
349 //aTrihedron = Handle(AIS_Trihedron)::DownCast(anAISIO);
350 //if (aTrihedron.IsNull()) {
351 // qDebug("### Deactivate all %i", (long)anAISIO.Access());
352 // //aContext->Activate(anAISIO);
357 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
359 Handle(AIS_InteractiveContext) aContext = AISContext();
360 if (aContext.IsNull())
362 if (!isVisible(theObject))
365 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
366 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
368 TColStd_ListOfInteger aModes;
369 aContext->ActivatedModes(anAIS, aModes);
370 return aModes.Extent() > 0;
373 void XGUI_Displayer::setSelected(const QObjectPtrList& theResults, const bool isUpdateViewer)
375 Handle(AIS_InteractiveContext) aContext = AISContext();
376 if (aContext.IsNull())
378 if (aContext->HasOpenedContext()) {
379 aContext->UnhilightSelected();
380 aContext->ClearSelected();
381 foreach(ObjectPtr aResult, theResults) {
382 if (isVisible(aResult)) {
383 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
384 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
386 aContext->SetSelected(anAIS, false);
390 aContext->UnhilightCurrents();
391 aContext->ClearCurrents();
392 foreach(ObjectPtr aResult, theResults) {
393 if (isVisible(aResult)) {
394 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
395 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
397 aContext->SetCurrentObject(anAIS, false);
406 void XGUI_Displayer::clearSelected()
408 Handle(AIS_InteractiveContext) aContext = AISContext();
410 aContext->UnhilightCurrents(false);
411 aContext->ClearSelected();
415 void XGUI_Displayer::eraseAll(const bool isUpdateViewer)
417 Handle(AIS_InteractiveContext) aContext = AISContext();
418 if (aContext.IsNull())
421 foreach (AISObjectPtr aAISObj, myResult2AISObjectMap) {
423 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
425 aContext->Remove(anIO, false);
427 myResult2AISObjectMap.clear();
432 void XGUI_Displayer::openLocalContext()
434 Handle(AIS_InteractiveContext) aContext = AISContext();
435 if (aContext.IsNull())
437 // Open local context if there is no one
438 if (!aContext->HasOpenedContext()) {
439 // Preserve selected objects
440 //AIS_ListOfInteractive aAisList;
441 //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
442 // aAisList.Append(aContext->Current());
444 // get the filters from the global context and append them to the local context
445 // a list of filters in the global context is not cleared and should be cleared here
446 SelectMgr_ListOfFilter aFilters;
447 aFilters.Assign(aContext->Filters());
448 // it is important to remove the filters in the global context, because there is a code
449 // in the closeLocalContex, which restore the global context filters
450 aContext->RemoveFilters();
452 //aContext->ClearCurrents();
453 aContext->OpenLocalContext();
454 qDebug("### Open context");
455 //aContext->NotUseDisplayedObjects();
457 //myUseExternalObjects = false;
459 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
460 for (;aIt.More(); aIt.Next()) {
461 aContext->AddFilter(aIt.Value());
464 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
465 //for(; aIt2.More(); aIt2.Next()) {
466 // aContext->SetSelected(aIt2.Value(), false);
471 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
473 Handle(AIS_InteractiveContext) aContext = AISContext();
474 if ( (!aContext.IsNull()) && (aContext->HasOpenedContext()) ) {
475 // Preserve selected objects
476 //AIS_ListOfInteractive aAisList;
477 //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
478 // aAisList.Append(aContext->SelectedInteractive());
480 // get the filters from the local context and append them to the global context
481 // a list of filters in the local context is cleared
482 SelectMgr_ListOfFilter aFilters;
483 aFilters.Assign(aContext->Filters());
485 //aContext->ClearSelected();
486 aContext->CloseAllContexts(false);
487 qDebug("### Close context");
489 // Redisplay all object if they were displayed in localContext
490 Handle(AIS_InteractiveObject) aAISIO;
491 foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
492 aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
493 if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
494 aContext->Display(aAISIO, false);
495 aContext->SetDisplayMode(aAISIO, Shading, false);
499 // Append the filters from the local selection in the global selection context
500 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
501 for (;aIt.More(); aIt.Next()) {
502 Handle(SelectMgr_Filter) aFilter = aIt.Value();
503 aContext->AddFilter(aFilter);
508 //myUseExternalObjects = false;
511 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
512 //for(; aIt2.More(); aIt2.Next()) {
513 // if (aContext->IsDisplayed(aIt2.Value()))
514 // aContext->SetCurrentObject(aIt2.Value(), false);
519 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
522 if (myResult2AISObjectMap.contains(theObject))
523 anIO = myResult2AISObjectMap[theObject];
527 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
529 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
530 return getObject(aRefAIS);
533 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
536 foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
537 AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
538 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
545 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
547 bool aWasEnabled = myEnableUpdateViewer;
549 myEnableUpdateViewer = isEnabled;
554 void XGUI_Displayer::updateViewer()
556 Handle(AIS_InteractiveContext) aContext = AISContext();
557 if (!aContext.IsNull() && myEnableUpdateViewer)
558 aContext->UpdateCurrentViewer();
561 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
563 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
564 if ((!aContext.IsNull()) && (!aContext->HasOpenedContext())) {
565 aContext->OpenLocalContext();
566 qDebug("### Open context");
571 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
573 Handle(AIS_InteractiveContext) aContext = AISContext();
574 if (myAndFilter.IsNull() && !aContext.IsNull()) {
575 myAndFilter = new SelectMgr_AndFilter();
576 aContext->AddFilter(myAndFilter);
581 void XGUI_Displayer::displayAIS(AISObjectPtr theAIS, bool isUpdate)
583 Handle(AIS_InteractiveContext) aContext = AISContext();
584 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
585 if (!anAISIO.IsNull()) {
586 aContext->Display(anAISIO, isUpdate);
587 if (aContext->HasOpenedContext()) {
588 //if (myUseExternalObjects) {
589 if (myActiveSelectionModes.size() == 0)
590 aContext->Activate(anAISIO);
592 foreach(int aMode, myActiveSelectionModes) {
593 aContext->Activate(anAISIO, aMode);
601 void XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool isUpdate)
603 Handle(AIS_InteractiveContext) aContext = AISContext();
604 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
605 if (!anAISIO.IsNull()) {
606 aContext->Remove(anAISIO, isUpdate);
611 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool toUpdate)
613 if (theMode == NoMode)
616 Handle(AIS_InteractiveContext) aContext = AISContext();
617 if (aContext.IsNull())
620 AISObjectPtr aAISObj = getAISObject(theObject);
624 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
625 bool aCanBeShaded = ::canBeShaded(aAISIO);
626 // In order to avoid extra closing/opening context
627 SelectMgr_IndexedMapOfOwner aSelectedOwners;
629 myWorkshop->selector()->selection()->selectedOwners(aSelectedOwners);
630 closeLocalContexts(false);
632 aContext->SetDisplayMode(aAISIO, theMode, false);
635 activateObjects(myActiveSelectionModes);
636 myWorkshop->selector()->setSelectedOwners(aSelectedOwners, false);
642 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
644 Handle(AIS_InteractiveContext) aContext = AISContext();
645 if (aContext.IsNull())
648 AISObjectPtr aAISObj = getAISObject(theObject);
652 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
653 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
656 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
658 Handle(AIS_InteractiveContext) aContext = AISContext();
659 if (aContext.IsNull())
661 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
662 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
663 for (; aIt.More(); aIt.Next()) {
664 if (theFilter.Access() == aIt.Value().Access())
667 GetFilter()->Add(theFilter);
670 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
672 Handle(AIS_InteractiveContext) aContext = AISContext();
673 if (aContext.IsNull())
675 GetFilter()->Remove(theFilter);
678 void XGUI_Displayer::removeFilters()
680 Handle(AIS_InteractiveContext) aContext = AISContext();
681 if (aContext.IsNull())
683 GetFilter()->Clear();
686 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
688 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
689 foreach(ObjectPtr aObj, aDispList) {
690 if (!theList.contains(aObj))
693 foreach(ObjectPtr aObj, theList) {
694 if (!isVisible(aObj))
695 display(aObj, false);
700 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
702 if (!isVisible(theObject))
705 AISObjectPtr aAISObj = getAISObject(theObject);
706 if (aAISObj.get() == NULL)
709 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
710 return ::canBeShaded(anAIS);