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::getModesOfActivation(ObjectPtr theObject, QIntList& theModes)
256 if (!isVisible(theObject))
259 Handle(AIS_InteractiveContext) aContext = AISContext();
260 if (aContext.IsNull())
263 AISObjectPtr aAISObj = getAISObject(theObject);
265 if (aAISObj.get() != NULL) {
266 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
267 TColStd_ListOfInteger aTColModes;
268 aContext->ActivatedModes(anAISIO, aTColModes);
269 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
270 for (; itr.More(); itr.Next() ) {
271 theModes.append(itr.Value());
276 void XGUI_Displayer::activateObjects(const QIntList& theModes)
278 // In order to avoid doblications of selection modes
280 foreach (int aMode, theModes) {
281 if (!aNewModes.contains(aMode))
282 aNewModes.append(aMode);
284 myActiveSelectionModes = aNewModes;
285 Handle(AIS_InteractiveContext) aContext = AISContext();
286 // Open local context if there is no one
287 if (!aContext->HasOpenedContext())
290 //aContext->UseDisplayedObjects();
291 //myUseExternalObjects = true;
293 AIS_ListOfInteractive aPrsList;
294 displayedObjects(aContext, aPrsList);
296 Handle(AIS_Trihedron) aTrihedron;
297 AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
298 Handle(AIS_InteractiveObject) anAISIO;
299 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
300 anAISIO = aLIt.Value();
301 aContext->Load(anAISIO, -1, true);
302 aContext->Deactivate(anAISIO);
303 aTrihedron = Handle(AIS_Trihedron)::DownCast(anAISIO);
304 //Deactivate trihedron which can be activated in local selector
305 if (aTrihedron.IsNull()) {
306 //aContext->Load(anAISIO, -1, true);
307 // In order to clear active modes list
308 if (myActiveSelectionModes.size() == 0) {
309 //aContext->Load(anAISIO, 0, true);
310 aContext->Activate(anAISIO);
311 qDebug("### 2. Activate all %i", (long)anAISIO.Access());
313 foreach(int aMode, myActiveSelectionModes) {
314 //aContext->Load(anAISIO, aMode, true);
315 aContext->Activate(anAISIO, aMode);
316 qDebug("### 1. Activate all %i, %i", (long)anAISIO.Access(), aMode);
324 void XGUI_Displayer::deactivateObjects()
326 myActiveSelectionModes.clear();
327 Handle(AIS_InteractiveContext) aContext = AISContext();
328 // Open local context if there is no one
329 if (!aContext->HasOpenedContext())
332 //aContext->NotUseDisplayedObjects();
333 AIS_ListOfInteractive aPrsList;
334 displayedObjects(aContext, aPrsList);
336 AIS_ListIteratorOfListOfInteractive aLIt;
337 //Handle(AIS_Trihedron) aTrihedron;
338 Handle(AIS_InteractiveObject) anAISIO;
339 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
340 anAISIO = aLIt.Value();
341 aContext->Deactivate(anAISIO);
342 //aTrihedron = Handle(AIS_Trihedron)::DownCast(anAISIO);
343 //if (aTrihedron.IsNull()) {
344 // qDebug("### Deactivate all %i", (long)anAISIO.Access());
345 // //aContext->Activate(anAISIO);
350 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
352 Handle(AIS_InteractiveContext) aContext = AISContext();
353 if (aContext.IsNull())
355 if (!isVisible(theObject))
358 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
359 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
361 TColStd_ListOfInteger aModes;
362 aContext->ActivatedModes(anAIS, aModes);
363 return aModes.Extent() > 0;
366 void XGUI_Displayer::setSelected(const QObjectPtrList& theResults, const bool isUpdateViewer)
368 Handle(AIS_InteractiveContext) aContext = AISContext();
369 if (aContext.IsNull())
371 if (aContext->HasOpenedContext()) {
372 aContext->UnhilightSelected();
373 aContext->ClearSelected();
374 foreach(ObjectPtr aResult, theResults) {
375 if (isVisible(aResult)) {
376 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
377 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
379 aContext->SetSelected(anAIS, false);
383 aContext->UnhilightCurrents();
384 aContext->ClearCurrents();
385 foreach(ObjectPtr aResult, theResults) {
386 if (isVisible(aResult)) {
387 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
388 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
390 aContext->SetCurrentObject(anAIS, false);
399 void XGUI_Displayer::clearSelected()
401 Handle(AIS_InteractiveContext) aContext = AISContext();
403 aContext->UnhilightCurrents(false);
404 aContext->ClearSelected();
408 void XGUI_Displayer::eraseAll(const bool isUpdateViewer)
410 Handle(AIS_InteractiveContext) aContext = AISContext();
411 if (aContext.IsNull())
414 foreach (AISObjectPtr aAISObj, myResult2AISObjectMap) {
416 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
418 aContext->Remove(anIO, false);
420 myResult2AISObjectMap.clear();
425 void XGUI_Displayer::openLocalContext()
427 Handle(AIS_InteractiveContext) aContext = AISContext();
428 if (aContext.IsNull())
430 // Open local context if there is no one
431 if (!aContext->HasOpenedContext()) {
432 // Preserve selected objects
433 //AIS_ListOfInteractive aAisList;
434 //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
435 // aAisList.Append(aContext->Current());
437 // get the filters from the global context and append them to the local context
438 // a list of filters in the global context is not cleared and should be cleared here
439 SelectMgr_ListOfFilter aFilters;
440 aFilters.Assign(aContext->Filters());
441 // it is important to remove the filters in the global context, because there is a code
442 // in the closeLocalContex, which restore the global context filters
443 aContext->RemoveFilters();
445 //aContext->ClearCurrents();
446 aContext->OpenLocalContext();
447 qDebug("### Open context");
448 //aContext->NotUseDisplayedObjects();
450 //myUseExternalObjects = false;
452 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
453 for (;aIt.More(); aIt.Next()) {
454 aContext->AddFilter(aIt.Value());
457 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
458 //for(; aIt2.More(); aIt2.Next()) {
459 // aContext->SetSelected(aIt2.Value(), false);
464 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
466 Handle(AIS_InteractiveContext) aContext = AISContext();
467 if ( (!aContext.IsNull()) && (aContext->HasOpenedContext()) ) {
468 // Preserve selected objects
469 //AIS_ListOfInteractive aAisList;
470 //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
471 // aAisList.Append(aContext->SelectedInteractive());
473 // get the filters from the local context and append them to the global context
474 // a list of filters in the local context is cleared
475 SelectMgr_ListOfFilter aFilters;
476 aFilters.Assign(aContext->Filters());
478 //aContext->ClearSelected();
479 aContext->CloseAllContexts(false);
480 qDebug("### Close context");
482 // Redisplay all object if they were displayed in localContext
483 Handle(AIS_InteractiveObject) aAISIO;
484 foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
485 aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
486 if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
487 aContext->Display(aAISIO, false);
488 aContext->SetDisplayMode(aAISIO, Shading, false);
492 // Append the filters from the local selection in the global selection context
493 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
494 for (;aIt.More(); aIt.Next()) {
495 Handle(SelectMgr_Filter) aFilter = aIt.Value();
496 aContext->AddFilter(aFilter);
501 //myUseExternalObjects = false;
504 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
505 //for(; aIt2.More(); aIt2.Next()) {
506 // if (aContext->IsDisplayed(aIt2.Value()))
507 // aContext->SetCurrentObject(aIt2.Value(), false);
512 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
515 if (myResult2AISObjectMap.contains(theObject))
516 anIO = myResult2AISObjectMap[theObject];
520 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
522 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
523 return getObject(aRefAIS);
526 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
529 foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
530 AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
531 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
538 void XGUI_Displayer::updateViewer()
540 Handle(AIS_InteractiveContext) aContext = AISContext();
541 if (!aContext.IsNull())
542 aContext->UpdateCurrentViewer();
545 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
547 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
548 if ((!aContext.IsNull()) && (!aContext->HasOpenedContext())) {
549 aContext->OpenLocalContext();
550 qDebug("### Open context");
555 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
557 Handle(AIS_InteractiveContext) aContext = AISContext();
558 if (myAndFilter.IsNull() && !aContext.IsNull()) {
559 myAndFilter = new SelectMgr_AndFilter();
560 aContext->AddFilter(myAndFilter);
565 void XGUI_Displayer::displayAIS(AISObjectPtr theAIS, bool isUpdate)
567 Handle(AIS_InteractiveContext) aContext = AISContext();
568 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
569 if (!anAISIO.IsNull()) {
570 aContext->Display(anAISIO, isUpdate);
571 if (aContext->HasOpenedContext()) {
572 //if (myUseExternalObjects) {
573 if (myActiveSelectionModes.size() == 0)
574 aContext->Activate(anAISIO);
576 foreach(int aMode, myActiveSelectionModes) {
577 aContext->Activate(anAISIO, aMode);
585 void XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool isUpdate)
587 Handle(AIS_InteractiveContext) aContext = AISContext();
588 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
589 if (!anAISIO.IsNull()) {
590 aContext->Remove(anAISIO, isUpdate);
595 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool toUpdate)
597 if (theMode == NoMode)
600 Handle(AIS_InteractiveContext) aContext = AISContext();
601 if (aContext.IsNull())
604 AISObjectPtr aAISObj = getAISObject(theObject);
608 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
609 bool aCanBeShaded = ::canBeShaded(aAISIO);
610 // In order to avoid extra closing/opening context
612 closeLocalContexts(false);
613 aContext->SetDisplayMode(aAISIO, theMode, toUpdate);
616 activateObjects(myActiveSelectionModes);
620 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
622 Handle(AIS_InteractiveContext) aContext = AISContext();
623 if (aContext.IsNull())
626 AISObjectPtr aAISObj = getAISObject(theObject);
630 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
631 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
634 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
636 Handle(AIS_InteractiveContext) aContext = AISContext();
637 if (aContext.IsNull())
639 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
640 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
641 for (; aIt.More(); aIt.Next()) {
642 if (theFilter.Access() == aIt.Value().Access())
645 GetFilter()->Add(theFilter);
648 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
650 Handle(AIS_InteractiveContext) aContext = AISContext();
651 if (aContext.IsNull())
653 GetFilter()->Remove(theFilter);
656 void XGUI_Displayer::removeFilters()
658 Handle(AIS_InteractiveContext) aContext = AISContext();
659 if (aContext.IsNull())
661 GetFilter()->Clear();
664 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
666 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
667 foreach(ObjectPtr aObj, aDispList) {
668 if (!theList.contains(aObj))
671 foreach(ObjectPtr aObj, theList) {
672 if (!isVisible(aObj))
673 display(aObj, false);
678 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
680 if (!isVisible(theObject))
683 AISObjectPtr aAISObj = getAISObject(theObject);
684 if (aAISObj.get() == NULL)
687 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
688 return ::canBeShaded(anAIS);