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))
120 // Check that the presentation is not a sketch
121 Handle(ModuleBase_ResultPrs) aPrs = Handle(ModuleBase_ResultPrs)::DownCast(theAIS);
123 return !aPrs->isSketchMode();
130 void XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
131 bool isShading, bool isUpdateViewer)
133 Handle(AIS_InteractiveContext) aContext = AISContext();
134 if (aContext.IsNull())
137 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
138 if (!anAISIO.IsNull()) {
139 myResult2AISObjectMap[theObject] = theAIS;
140 bool aCanBeShaded = ::canBeShaded(anAISIO);
141 // In order to avoid extra closing/opening context
142 SelectMgr_IndexedMapOfOwner aSelectedOwners;
144 myWorkshop->selector()->selection()->selectedOwners(aSelectedOwners);
145 closeLocalContexts(false);
147 aContext->Display(anAISIO, false);
148 qDebug("### Display %i", (long)anAISIO.Access());
150 aContext->SetDisplayMode(anAISIO, isShading? Shading : Wireframe, false);
151 // Customization of presentation
152 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
153 if (aFeature.get() != NULL) {
154 GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
155 if (aCustPrs.get() != NULL)
156 aCustPrs->customisePresentation(theAIS);
160 activateObjects(myActiveSelectionModes);
161 myWorkshop->selector()->setSelectedOwners(aSelectedOwners, false);
168 void XGUI_Displayer::erase(ObjectPtr theObject, const bool isUpdateViewer)
170 if (!isVisible(theObject))
173 Handle(AIS_InteractiveContext) aContext = AISContext();
174 if (aContext.IsNull())
176 AISObjectPtr anObject = myResult2AISObjectMap[theObject];
178 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
179 if (!anAIS.IsNull()) {
180 aContext->Remove(anAIS, isUpdateViewer);
183 myResult2AISObjectMap.remove(theObject);
186 void XGUI_Displayer::redisplay(ObjectPtr theObject, bool isUpdateViewer)
188 if (!isVisible(theObject))
191 AISObjectPtr aAISObj = getAISObject(theObject);
192 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
194 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
196 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
198 erase(theObject, isUpdateViewer);
201 if (aAIS_Obj != aAISObj) {
202 myResult2AISObjectMap[theObject] = aAIS_Obj;
204 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
207 if (!aAISIO.IsNull()) {
208 Handle(AIS_InteractiveContext) aContext = AISContext();
209 if (aContext.IsNull())
211 aContext->Redisplay(aAISIO, false);
217 void XGUI_Displayer::deactivate(ObjectPtr theObject)
219 if (isVisible(theObject)) {
220 Handle(AIS_InteractiveContext) aContext = AISContext();
221 if (aContext.IsNull())
224 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
225 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
226 aContext->Deactivate(anAIS);
227 qDebug("### Deactivate obj %i", (long)anAIS.Access());
231 void XGUI_Displayer::activate(ObjectPtr theFeature)
233 activate(theFeature, myActiveSelectionModes);
236 void XGUI_Displayer::activate(ObjectPtr theObject, const QIntList& theModes)
238 if (isVisible(theObject)) {
239 Handle(AIS_InteractiveContext) aContext = AISContext();
240 if (aContext.IsNull())
243 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
244 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
245 aContext->Deactivate(anAIS);
246 aContext->Load(anAIS, -1, true);
247 // In order to clear active modes list
248 if (theModes.size() > 0) {
249 foreach(int aMode, theModes) {
250 //aContext->Load(anAIS, aMode, true);
251 aContext->Activate(anAIS, aMode);
252 qDebug("### 1. Activate obj %i, %i", (long)anAIS.Access(), aMode);
255 //aContext->Load(anAIS, 0, true);
256 aContext->Activate(anAIS);
257 qDebug("### 2. Activate obj %i", (long)anAIS.Access());
262 void XGUI_Displayer::getModesOfActivation(ObjectPtr theObject, QIntList& theModes)
264 if (!isVisible(theObject))
267 Handle(AIS_InteractiveContext) aContext = AISContext();
268 if (aContext.IsNull())
271 AISObjectPtr aAISObj = getAISObject(theObject);
273 if (aAISObj.get() != NULL) {
274 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
275 TColStd_ListOfInteger aTColModes;
276 aContext->ActivatedModes(anAISIO, aTColModes);
277 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
278 for (; itr.More(); itr.Next() ) {
279 theModes.append(itr.Value());
284 void XGUI_Displayer::activateObjects(const QIntList& theModes)
286 // In order to avoid doblications of selection modes
288 foreach (int aMode, theModes) {
289 if (!aNewModes.contains(aMode))
290 aNewModes.append(aMode);
292 myActiveSelectionModes = aNewModes;
293 Handle(AIS_InteractiveContext) aContext = AISContext();
294 // Open local context if there is no one
295 if (!aContext->HasOpenedContext())
298 //aContext->UseDisplayedObjects();
299 //myUseExternalObjects = true;
301 AIS_ListOfInteractive aPrsList;
302 ::displayedObjects(aContext, aPrsList);
304 Handle(AIS_Trihedron) aTrihedron;
305 AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
306 Handle(AIS_InteractiveObject) anAISIO;
307 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
308 anAISIO = aLIt.Value();
309 aContext->Load(anAISIO, -1, true);
310 aContext->Deactivate(anAISIO);
311 aTrihedron = Handle(AIS_Trihedron)::DownCast(anAISIO);
312 //Deactivate trihedron which can be activated in local selector
313 if (aTrihedron.IsNull()) {
314 //aContext->Load(anAISIO, -1, true);
315 // In order to clear active modes list
316 if (myActiveSelectionModes.size() == 0) {
317 //aContext->Load(anAISIO, 0, true);
318 aContext->Activate(anAISIO);
319 qDebug("### 2. Activate all %i", (long)anAISIO.Access());
321 foreach(int aMode, myActiveSelectionModes) {
322 //aContext->Load(anAISIO, aMode, true);
323 aContext->Activate(anAISIO, aMode);
324 qDebug("### 1. Activate all %i, %i", (long)anAISIO.Access(), aMode);
332 void XGUI_Displayer::deactivateObjects()
334 myActiveSelectionModes.clear();
335 Handle(AIS_InteractiveContext) aContext = AISContext();
336 // Open local context if there is no one
337 if (!aContext->HasOpenedContext())
340 //aContext->NotUseDisplayedObjects();
341 AIS_ListOfInteractive aPrsList;
342 ::displayedObjects(aContext, aPrsList);
344 AIS_ListIteratorOfListOfInteractive aLIt;
345 //Handle(AIS_Trihedron) aTrihedron;
346 Handle(AIS_InteractiveObject) anAISIO;
347 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
348 anAISIO = aLIt.Value();
349 aContext->Deactivate(anAISIO);
350 //aTrihedron = Handle(AIS_Trihedron)::DownCast(anAISIO);
351 //if (aTrihedron.IsNull()) {
352 // qDebug("### Deactivate all %i", (long)anAISIO.Access());
353 // //aContext->Activate(anAISIO);
358 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
360 Handle(AIS_InteractiveContext) aContext = AISContext();
361 if (aContext.IsNull())
363 if (!isVisible(theObject))
366 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
367 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
369 TColStd_ListOfInteger aModes;
370 aContext->ActivatedModes(anAIS, aModes);
371 return aModes.Extent() > 0;
374 void XGUI_Displayer::setSelected(const QObjectPtrList& theResults, const bool isUpdateViewer)
376 Handle(AIS_InteractiveContext) aContext = AISContext();
377 if (aContext.IsNull())
379 if (aContext->HasOpenedContext()) {
380 aContext->UnhilightSelected();
381 aContext->ClearSelected();
382 foreach(ObjectPtr aResult, theResults) {
383 if (isVisible(aResult)) {
384 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
385 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
387 aContext->SetSelected(anAIS, false);
391 aContext->UnhilightCurrents();
392 aContext->ClearCurrents();
393 foreach(ObjectPtr aResult, theResults) {
394 if (isVisible(aResult)) {
395 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
396 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
398 aContext->SetCurrentObject(anAIS, false);
407 void XGUI_Displayer::clearSelected()
409 Handle(AIS_InteractiveContext) aContext = AISContext();
411 aContext->UnhilightCurrents(false);
412 aContext->ClearSelected();
416 void XGUI_Displayer::eraseAll(const bool isUpdateViewer)
418 Handle(AIS_InteractiveContext) aContext = AISContext();
419 if (aContext.IsNull())
422 foreach (AISObjectPtr aAISObj, myResult2AISObjectMap) {
424 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
426 aContext->Remove(anIO, false);
428 myResult2AISObjectMap.clear();
433 void XGUI_Displayer::openLocalContext()
435 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
436 if (aContext.IsNull())
438 // Open local context if there is no one
439 if (!aContext->HasOpenedContext()) {
440 // Preserve selected objects
441 //AIS_ListOfInteractive aAisList;
442 //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
443 // aAisList.Append(aContext->Current());
445 // get the filters from the global context and append them to the local context
446 // a list of filters in the global context is not cleared and should be cleared here
447 SelectMgr_ListOfFilter aFilters;
448 aFilters.Assign(aContext->Filters());
449 // it is important to remove the filters in the global context, because there is a code
450 // in the closeLocalContex, which restore the global context filters
451 aContext->RemoveFilters();
453 //aContext->ClearCurrents();
454 aContext->OpenLocalContext();
455 qDebug("### Open context");
456 //aContext->NotUseDisplayedObjects();
458 //myUseExternalObjects = false;
460 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
461 for (;aIt.More(); aIt.Next()) {
462 aContext->AddFilter(aIt.Value());
465 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
466 //for(; aIt2.More(); aIt2.Next()) {
467 // aContext->SetSelected(aIt2.Value(), false);
472 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
474 Handle(AIS_InteractiveContext) aContext = AISContext();
475 if ( (!aContext.IsNull()) && (aContext->HasOpenedContext()) ) {
476 // Preserve selected objects
477 //AIS_ListOfInteractive aAisList;
478 //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
479 // aAisList.Append(aContext->SelectedInteractive());
481 // get the filters from the local context and append them to the global context
482 // a list of filters in the local context is cleared
483 SelectMgr_ListOfFilter aFilters;
484 aFilters.Assign(aContext->Filters());
486 //aContext->ClearSelected();
487 aContext->CloseAllContexts(false);
488 qDebug("### Close context");
490 // Redisplay all object if they were displayed in localContext
491 Handle(AIS_InteractiveObject) aAISIO;
492 foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
493 aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
494 if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
495 aContext->Display(aAISIO, false);
496 aContext->SetDisplayMode(aAISIO, Shading, false);
500 // Append the filters from the local selection in the global selection context
501 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
502 for (;aIt.More(); aIt.Next()) {
503 Handle(SelectMgr_Filter) aFilter = aIt.Value();
504 aContext->AddFilter(aFilter);
509 //myUseExternalObjects = false;
512 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
513 //for(; aIt2.More(); aIt2.Next()) {
514 // if (aContext->IsDisplayed(aIt2.Value()))
515 // aContext->SetCurrentObject(aIt2.Value(), false);
520 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
523 if (myResult2AISObjectMap.contains(theObject))
524 anIO = myResult2AISObjectMap[theObject];
528 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
530 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
531 return getObject(aRefAIS);
534 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
537 foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
538 AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
539 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
546 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
548 bool aWasEnabled = myEnableUpdateViewer;
550 myEnableUpdateViewer = isEnabled;
555 void XGUI_Displayer::updateViewer()
557 Handle(AIS_InteractiveContext) aContext = AISContext();
558 if (!aContext.IsNull() && myEnableUpdateViewer)
559 aContext->UpdateCurrentViewer();
562 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
564 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
565 if ((!aContext.IsNull()) && (!aContext->HasOpenedContext())) {
566 aContext->OpenLocalContext();
567 qDebug("### Open context");
572 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
574 Handle(AIS_InteractiveContext) aContext = AISContext();
575 if (myAndFilter.IsNull() && !aContext.IsNull()) {
576 myAndFilter = new SelectMgr_AndFilter();
577 aContext->AddFilter(myAndFilter);
582 void XGUI_Displayer::displayAIS(AISObjectPtr theAIS, bool isUpdate)
584 Handle(AIS_InteractiveContext) aContext = AISContext();
585 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
586 if (!anAISIO.IsNull()) {
587 aContext->Display(anAISIO, isUpdate);
588 if (aContext->HasOpenedContext()) {
589 //if (myUseExternalObjects) {
590 if (myActiveSelectionModes.size() == 0)
591 aContext->Activate(anAISIO);
593 foreach(int aMode, myActiveSelectionModes) {
594 aContext->Activate(anAISIO, aMode);
602 void XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool isUpdate)
604 Handle(AIS_InteractiveContext) aContext = AISContext();
605 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
606 if (!anAISIO.IsNull()) {
607 aContext->Remove(anAISIO, isUpdate);
612 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool toUpdate)
614 if (theMode == NoMode)
617 Handle(AIS_InteractiveContext) aContext = AISContext();
618 if (aContext.IsNull())
621 AISObjectPtr aAISObj = getAISObject(theObject);
625 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
626 bool aCanBeShaded = ::canBeShaded(aAISIO);
627 // In order to avoid extra closing/opening context
628 SelectMgr_IndexedMapOfOwner aSelectedOwners;
630 myWorkshop->selector()->selection()->selectedOwners(aSelectedOwners);
631 closeLocalContexts(false);
633 aContext->SetDisplayMode(aAISIO, theMode, false);
636 activateObjects(myActiveSelectionModes);
637 myWorkshop->selector()->setSelectedOwners(aSelectedOwners, false);
643 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
645 Handle(AIS_InteractiveContext) aContext = AISContext();
646 if (aContext.IsNull())
649 AISObjectPtr aAISObj = getAISObject(theObject);
653 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
654 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
657 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
659 Handle(AIS_InteractiveContext) aContext = AISContext();
660 if (aContext.IsNull())
662 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
663 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
664 for (; aIt.More(); aIt.Next()) {
665 if (theFilter.Access() == aIt.Value().Access())
668 GetFilter()->Add(theFilter);
671 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
673 Handle(AIS_InteractiveContext) aContext = AISContext();
674 if (aContext.IsNull())
676 GetFilter()->Remove(theFilter);
679 void XGUI_Displayer::removeFilters()
681 Handle(AIS_InteractiveContext) aContext = AISContext();
682 if (aContext.IsNull())
684 GetFilter()->Clear();
687 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
689 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
690 foreach(ObjectPtr aObj, aDispList) {
691 if (!theList.contains(aObj))
694 foreach(ObjectPtr aObj, theList) {
695 if (!isVisible(aObj))
696 display(aObj, false);
701 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
703 if (!isVisible(theObject))
706 AISObjectPtr aAISObj = getAISObject(theObject);
707 if (aAISObj.get() == NULL)
710 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
711 return ::canBeShaded(anAIS);