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);
150 aContext->Load(anAISIO, -1, true);
157 void XGUI_Displayer::erase(ObjectPtr theObject, const bool isUpdateViewer)
159 if (!isVisible(theObject))
162 Handle(AIS_InteractiveContext) aContext = AISContext();
163 if (aContext.IsNull())
165 AISObjectPtr anObject = myResult2AISObjectMap[theObject];
167 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
168 if (!anAIS.IsNull()) {
169 aContext->Remove(anAIS, isUpdateViewer);
172 myResult2AISObjectMap.remove(theObject);
175 void XGUI_Displayer::redisplay(ObjectPtr theObject, bool isUpdateViewer)
177 if (!isVisible(theObject))
180 AISObjectPtr aAISObj = getAISObject(theObject);
181 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
183 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
185 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
187 erase(theObject, isUpdateViewer);
190 if (aAIS_Obj != aAISObj) {
191 myResult2AISObjectMap[theObject] = aAIS_Obj;
193 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
196 if (!aAISIO.IsNull()) {
197 Handle(AIS_InteractiveContext) aContext = AISContext();
198 if (aContext.IsNull())
200 aContext->Redisplay(aAISIO, isUpdateViewer);
204 void XGUI_Displayer::deactivate(ObjectPtr theObject)
206 if (isVisible(theObject)) {
207 Handle(AIS_InteractiveContext) aContext = AISContext();
208 if (aContext.IsNull())
211 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
212 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
213 aContext->Deactivate(anAIS);
214 qDebug("### Deactivate obj %i", (long)anAIS.Access());
218 void XGUI_Displayer::activate(ObjectPtr theFeature)
220 activate(theFeature, myActiveSelectionModes);
223 void XGUI_Displayer::activate(ObjectPtr theObject, const QIntList& theModes)
225 if (isVisible(theObject)) {
226 Handle(AIS_InteractiveContext) aContext = AISContext();
227 if (aContext.IsNull())
230 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
231 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
232 aContext->Deactivate(anAIS);
233 //if (aContext->HasOpenedContext()) {
234 // aContext->Load(anAIS, -1, true);
236 // In order to clear active modes list
237 if (theModes.size() > 0) {
238 foreach(int aMode, theModes) {
239 aContext->Activate(anAIS, aMode);
240 qDebug("### 1. Activate obj %i, %i", (long)anAIS.Access(), aMode);
243 aContext->Activate(anAIS);
244 qDebug("### 2. Activate obj %i", (long)anAIS.Access());
249 void XGUI_Displayer::activateObjects(const QIntList& theModes)
251 // In order to avoid doblications of selection modes
252 myActiveSelectionModes.clear();
253 foreach (int aMode, theModes) {
254 if (!myActiveSelectionModes.contains(aMode))
255 myActiveSelectionModes.append(aMode);
257 Handle(AIS_InteractiveContext) aContext = AISContext();
258 // Open local context if there is no one
259 if (!aContext->HasOpenedContext())
262 //aContext->UseDisplayedObjects();
263 //myUseExternalObjects = true;
265 AIS_ListOfInteractive aPrsList;
266 displayedObjects(aContext, aPrsList);
268 Handle(AIS_Trihedron) aTrihedron;
269 AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
270 Handle(AIS_InteractiveObject) anAISIO;
271 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
272 anAISIO = aLIt.Value();
273 aContext->Deactivate(anAISIO);
274 aTrihedron = Handle(AIS_Trihedron)::DownCast(anAISIO);
275 //Deactivate trihedron which can be activated in local selector
276 if (aTrihedron.IsNull()) {
277 //aContext->Load(anAISIO, -1, true);
278 // In order to clear active modes list
279 if (myActiveSelectionModes.size() == 0) {
280 aContext->Activate(anAISIO);
281 qDebug("### 2. Activate all %i", (long)anAISIO.Access());
283 foreach(int aMode, myActiveSelectionModes) {
284 aContext->Activate(anAISIO, aMode);
285 qDebug("### 1. Activate all %i, %i", (long)anAISIO.Access(), aMode);
293 void XGUI_Displayer::deactivateObjects()
295 myActiveSelectionModes.clear();
296 Handle(AIS_InteractiveContext) aContext = AISContext();
297 // Open local context if there is no one
298 if (!aContext->HasOpenedContext())
301 //aContext->NotUseDisplayedObjects();
302 AIS_ListOfInteractive aPrsList;
303 displayedObjects(aContext, aPrsList);
305 AIS_ListIteratorOfListOfInteractive aLIt;
306 Handle(AIS_Trihedron) aTrihedron;
307 Handle(AIS_InteractiveObject) anAISIO;
308 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
309 anAISIO = aLIt.Value();
310 aContext->Deactivate(anAISIO);
311 aTrihedron = Handle(AIS_Trihedron)::DownCast(anAISIO);
312 if (aTrihedron.IsNull()) {
313 //aContext->Load(anAISIO, -1, true);
314 qDebug("### Deactivate all %i", (long)anAISIO.Access());
315 //aContext->Activate(anAISIO);
320 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
322 Handle(AIS_InteractiveContext) aContext = AISContext();
323 if (aContext.IsNull())
325 if (!isVisible(theObject))
328 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
329 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
331 TColStd_ListOfInteger aModes;
332 aContext->ActivatedModes(anAIS, aModes);
333 return aModes.Extent() > 0;
336 void XGUI_Displayer::stopSelection(const QObjectPtrList& theResults, const bool isStop,
337 const bool isUpdateViewer)
339 Handle(AIS_InteractiveContext) aContext = AISContext();
340 if (aContext.IsNull())
343 Handle(AIS_Shape) anAIS;
344 QObjectPtrList::const_iterator anIt = theResults.begin(), aLast = theResults.end();
346 for (; anIt != aLast; anIt++) {
348 if (isVisible(aFeature))
349 anAIS = Handle(AIS_Shape)::DownCast(
350 myResult2AISObjectMap[aFeature]->impl<Handle(AIS_InteractiveObject)>());
355 QColor aColor(Qt::white);
357 Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
361 QColor aColor(Qt::red);
363 Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
372 void XGUI_Displayer::setSelected(const QObjectPtrList& theResults, const bool isUpdateViewer)
374 Handle(AIS_InteractiveContext) aContext = AISContext();
375 if (aContext.IsNull())
377 if (aContext->HasOpenedContext()) {
378 aContext->UnhilightSelected();
379 aContext->ClearSelected();
380 foreach(ObjectPtr aResult, theResults) {
381 if (isVisible(aResult)) {
382 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
383 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
385 aContext->SetSelected(anAIS, false);
389 aContext->UnhilightCurrents();
390 aContext->ClearCurrents();
391 foreach(ObjectPtr aResult, theResults) {
392 if (isVisible(aResult)) {
393 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
394 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
396 aContext->SetCurrentObject(anAIS, false);
405 void XGUI_Displayer::clearSelected()
407 Handle(AIS_InteractiveContext) aContext = AISContext();
409 aContext->UnhilightCurrents(false);
410 aContext->ClearSelected();
414 void XGUI_Displayer::eraseAll(const bool isUpdateViewer)
416 Handle(AIS_InteractiveContext) aContext = AISContext();
417 if (aContext.IsNull())
420 foreach (AISObjectPtr aAISObj, myResult2AISObjectMap) {
422 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
424 aContext->Remove(anIO, false);
426 myResult2AISObjectMap.clear();
431 //void XGUI_Displayer::eraseDeletedResults(const bool isUpdateViewer)
433 // Handle(AIS_InteractiveContext) aContext = AISContext();
434 // if (aContext.IsNull())
437 // QObjectPtrList aRemoved;
438 // foreach (ObjectPtr aFeature, myResult2AISObjectMap.keys()) {
439 // if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
440 // AISObjectPtr anObj = myResult2AISObjectMap[aFeature];
443 // Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
444 // if (!anAIS.IsNull()) {
445 // aContext->Remove(anAIS, false);
446 // aRemoved.append(aFeature);
450 // foreach(ObjectPtr aObj, aRemoved) {
451 // myResult2AISObjectMap.remove(aObj);
454 // if (isUpdateViewer)
458 void XGUI_Displayer::openLocalContext()
460 Handle(AIS_InteractiveContext) aContext = AISContext();
461 if (aContext.IsNull())
463 // Open local context if there is no one
464 if (!aContext->HasOpenedContext()) {
465 // Preserve selected objects
466 //AIS_ListOfInteractive aAisList;
467 //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
468 // aAisList.Append(aContext->Current());
470 // get the filters from the global context and append them to the local context
471 // a list of filters in the global context is not cleared and should be cleared here
472 SelectMgr_ListOfFilter aFilters;
473 aFilters.Assign(aContext->Filters());
474 // it is important to remove the filters in the global context, because there is a code
475 // in the closeLocalContex, which restore the global context filters
476 aContext->RemoveFilters();
478 //aContext->ClearCurrents();
479 aContext->OpenLocalContext();
480 qDebug("### Open context");
481 //aContext->NotUseDisplayedObjects();
483 //myUseExternalObjects = false;
484 myActiveSelectionModes.clear();
486 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
487 for (;aIt.More(); aIt.Next()) {
488 aContext->AddFilter(aIt.Value());
491 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
492 //for(; aIt2.More(); aIt2.Next()) {
493 // aContext->SetSelected(aIt2.Value(), false);
498 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
500 Handle(AIS_InteractiveContext) aContext = AISContext();
501 if ( (!aContext.IsNull()) && (aContext->HasOpenedContext()) ) {
502 // Preserve selected objects
503 //AIS_ListOfInteractive aAisList;
504 //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
505 // aAisList.Append(aContext->SelectedInteractive());
507 // get the filters from the local context and append them to the global context
508 // a list of filters in the local context is cleared
509 SelectMgr_ListOfFilter aFilters;
510 aFilters.Assign(aContext->Filters());
512 //aContext->ClearSelected();
513 aContext->CloseAllContexts(false);
514 qDebug("### Close context");
516 // Redisplay all object if they were displayed in localContext
517 Handle(AIS_InteractiveObject) aAISIO;
518 foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
519 aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
520 if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
521 aContext->Display(aAISIO, false);
522 aContext->SetDisplayMode(aAISIO, Shading, false);
526 // Append the filters from the local selection in the global selection context
527 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
528 for (;aIt.More(); aIt.Next()) {
529 Handle(SelectMgr_Filter) aFilter = aIt.Value();
530 aContext->AddFilter(aFilter);
535 //myUseExternalObjects = false;
536 myActiveSelectionModes.clear();
539 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
540 //for(; aIt2.More(); aIt2.Next()) {
541 // if (aContext->IsDisplayed(aIt2.Value()))
542 // aContext->SetCurrentObject(aIt2.Value(), false);
547 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
550 if (myResult2AISObjectMap.contains(theObject))
551 anIO = myResult2AISObjectMap[theObject];
555 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
557 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
558 return getObject(aRefAIS);
561 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
564 foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
565 AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
566 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
573 void XGUI_Displayer::updateViewer()
575 Handle(AIS_InteractiveContext) aContext = AISContext();
576 if (!aContext.IsNull())
577 aContext->UpdateCurrentViewer();
580 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
582 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
583 if ((!aContext.IsNull()) && (!aContext->HasOpenedContext())) {
584 aContext->OpenLocalContext();
585 qDebug("### Open context");
590 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
592 Handle(AIS_InteractiveContext) aContext = AISContext();
593 if (myAndFilter.IsNull() && !aContext.IsNull()) {
594 myAndFilter = new SelectMgr_AndFilter();
595 aContext->AddFilter(myAndFilter);
600 void XGUI_Displayer::displayAIS(AISObjectPtr theAIS, bool isUpdate)
602 Handle(AIS_InteractiveContext) aContext = AISContext();
603 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
604 if (!anAISIO.IsNull()) {
605 aContext->Display(anAISIO, isUpdate);
606 if (aContext->HasOpenedContext()) {
607 //if (myUseExternalObjects) {
608 if (myActiveSelectionModes.size() == 0)
609 aContext->Activate(anAISIO);
611 foreach(int aMode, myActiveSelectionModes) {
612 aContext->Activate(anAISIO, aMode);
620 void XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool isUpdate)
622 Handle(AIS_InteractiveContext) aContext = AISContext();
623 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
624 if (!anAISIO.IsNull()) {
625 aContext->Remove(anAISIO, isUpdate);
630 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool toUpdate)
632 if (theMode == NoMode)
635 Handle(AIS_InteractiveContext) aContext = AISContext();
636 if (aContext.IsNull())
639 AISObjectPtr aAISObj = getAISObject(theObject);
643 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
644 aContext->SetDisplayMode(aAISIO, theMode, toUpdate);
647 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
649 Handle(AIS_InteractiveContext) aContext = AISContext();
650 if (aContext.IsNull())
653 AISObjectPtr aAISObj = getAISObject(theObject);
657 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
658 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
661 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
663 Handle(AIS_InteractiveContext) aContext = AISContext();
664 if (aContext.IsNull())
666 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
667 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
668 for (; aIt.More(); aIt.Next()) {
669 if (theFilter.Access() == aIt.Value().Access())
672 GetFilter()->Add(theFilter);
675 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
677 Handle(AIS_InteractiveContext) aContext = AISContext();
678 if (aContext.IsNull())
680 GetFilter()->Remove(theFilter);
683 void XGUI_Displayer::removeFilters()
685 Handle(AIS_InteractiveContext) aContext = AISContext();
686 if (aContext.IsNull())
688 GetFilter()->Clear();
691 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
693 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
694 foreach(ObjectPtr aObj, aDispList) {
695 if (!theList.contains(aObj))
698 foreach(ObjectPtr aObj, theList) {
699 if (!isVisible(aObj))
700 display(aObj, false);