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"
12 #include "XGUI_CustomPrs.h"
14 #include <AppElements_Viewer.h>
16 #include <ModelAPI_Document.h>
17 #include <ModelAPI_Data.h>
18 #include <ModelAPI_Object.h>
19 #include <ModelAPI_Tools.h>
20 #include <ModelAPI_AttributeIntArray.h>
21 #include <ModelAPI_ResultCompSolid.h>
23 #include <ModuleBase_ResultPrs.h>
24 #include <ModuleBase_Tools.h>
25 #include <ModuleBase_IModule.h>
27 #include <GeomAPI_Shape.h>
28 #include <GeomAPI_IPresentable.h>
29 #include <GeomAPI_ICustomPrs.h>
31 #include <AIS_InteractiveContext.hxx>
32 #include <AIS_LocalContext.hxx>
33 #include <AIS_ListOfInteractive.hxx>
34 #include <AIS_ListIteratorOfListOfInteractive.hxx>
35 #include <AIS_DimensionSelectionMode.hxx>
36 #include <AIS_Shape.hxx>
37 #include <AIS_Dimension.hxx>
38 #include <TColStd_ListIteratorOfListOfInteger.hxx>
39 #include <SelectMgr_ListOfFilter.hxx>
40 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
41 #include <Prs3d_Drawer.hxx>
42 #include <Prs3d_IsoAspect.hxx>
44 #include <StdSelect_ViewerSelector3d.hxx>
46 #include <TColStd_MapOfTransient.hxx>
47 #include <TColStd_MapIteratorOfMapOfTransient.hxx>
49 #include <Events_Loop.h>
50 #include <ModelAPI_Events.h>
54 const int MOUSE_SENSITIVITY_IN_PIXEL = 10; ///< defines the local context mouse selection sensitivity
56 //#define DEBUG_ACTIVATE_OBJECTS
57 //#define DEBUG_DEACTIVATE
58 //#define DEBUG_ACTIVATE_AIS
59 //#define DEBUG_DEACTIVATE_AIS
61 //#define DEBUG_DISPLAY
62 //#define DEBUG_FEATURE_REDISPLAY
63 //#define DEBUG_SELECTION_FILTERS
65 //#define DEBUG_COMPOSILID_DISPLAY
66 // Workaround for bug #25637
67 void displayedObjects(const Handle(AIS_InteractiveContext)& theAIS, AIS_ListOfInteractive& theList)
69 // Get from null point
70 theAIS->DisplayedObjects(theList, true);
71 if (theAIS->HasOpenedContext()) {
72 // get from local context
73 const Handle(AIS_LocalContext)& aLC = theAIS->LocalContext();
74 TColStd_MapOfTransient aMap;
75 int NbDisp = aLC->DisplayedObjects(aMap);
76 TColStd_MapIteratorOfMapOfTransient aIt(aMap);
78 Handle(AIS_InteractiveObject) curIO;
79 Handle(Standard_Transient) Tr;
80 for(; aIt.More(); aIt.Next()){
82 curIO = *((Handle(AIS_InteractiveObject)*) &Tr);
83 theList.Append(curIO);
88 QString qIntListInfo(const QIntList& theValues, const QString& theSeparator = QString(", "))
91 QIntList::const_iterator anIt = theValues.begin(), aLast = theValues.end();
92 for (; anIt != aLast; anIt++) {
93 anInfo.append(QString::number(*anIt));
95 return anInfo.join(theSeparator);
98 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
99 : myWorkshop(theWorkshop)
101 enableUpdateViewer(true);
102 myCustomPrs = std::shared_ptr<GeomAPI_ICustomPrs>(new XGUI_CustomPrs());
105 XGUI_Displayer::~XGUI_Displayer()
109 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
111 return myResult2AISObjectMap.contains(theObject);
114 void XGUI_Displayer::display(ObjectPtr theObject, bool theUpdateViewer)
116 if (isVisible(theObject)) {
117 #ifdef DEBUG_COMPOSILID_DISPLAY
118 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
119 if (aCompsolidResult.get()) {
120 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
121 ResultPtr aSubResult = aCompsolidResult->subResult(i);
122 if (aSubResult.get())
123 redisplay(aSubResult, false);
130 redisplay(theObject, theUpdateViewer);
134 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
135 bool isShading = false;
136 if (aPrs.get() != NULL) {
137 anAIS = aPrs->getAISObject(anAIS);
139 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
140 if (aResult.get() != NULL) {
141 #ifdef DEBUG_COMPOSILID_DISPLAY
142 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
143 if (aCompsolidResult.get()) {
144 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
145 ResultPtr aSubResult = aCompsolidResult->subResult(i);
146 if (aSubResult.get())
147 display(aSubResult, false);
154 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
155 if (aShapePtr.get() != NULL) {
156 anAIS = AISObjectPtr(new GeomAPI_AISObject());
157 anAIS->setImpl(new Handle(AIS_InteractiveObject)(new ModuleBase_ResultPrs(aResult)));
158 //anAIS->createShape(aShapePtr);
161 #ifdef DEBUG_COMPOSILID_DISPLAY
167 display(theObject, anAIS, isShading, theUpdateViewer);
171 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS)
173 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
174 if (!aShapePrs.IsNull()) {
175 TopoDS_Shape aShape = aShapePrs->Shape();
178 TopAbs_ShapeEnum aType = aShape.ShapeType();
179 if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
182 // Check that the presentation is not a sketch
183 Handle(ModuleBase_ResultPrs) aPrs = Handle(ModuleBase_ResultPrs)::DownCast(theAIS);
185 return !aPrs->isSketchMode();
192 void XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
193 bool isShading, bool theUpdateViewer)
195 Handle(AIS_InteractiveContext) aContext = AISContext();
196 if (aContext.IsNull())
199 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
200 if (!anAISIO.IsNull()) {
201 appendResultObject(theObject, theAIS);
203 bool isCustomized = customizeObject(theObject);
205 int aDispMode = isShading? Shading : Wireframe;
207 anAISIO->Attributes()->SetFaceBoundaryDraw( Standard_True );
208 anAISIO->SetDisplayMode(aDispMode);
209 aContext->Display(anAISIO, aDispMode, 0, false, true, AIS_DS_Displayed);
211 emit objectDisplayed(theObject, theAIS);
212 activate(anAISIO, myActiveSelectionModes, theUpdateViewer);
213 // the fix from VPA for more suitable selection of sketcher lines
214 if(anAISIO->Width() > 1) {
215 for(int aModeIdx = 0; aModeIdx < myActiveSelectionModes.length(); ++aModeIdx) {
216 aContext->SetSelectionSensitivity(anAISIO,
217 myActiveSelectionModes.value(aModeIdx), anAISIO->Width() + 2);
225 void XGUI_Displayer::erase(ObjectPtr theObject, const bool theUpdateViewer)
227 if (!isVisible(theObject))
230 Handle(AIS_InteractiveContext) aContext = AISContext();
231 if (aContext.IsNull())
233 AISObjectPtr anObject = myResult2AISObjectMap[theObject];
235 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
236 if (!anAIS.IsNull()) {
237 emit beforeObjectErase(theObject, anObject);
238 aContext->Remove(anAIS, theUpdateViewer);
241 myResult2AISObjectMap.remove(theObject);
244 std::ostringstream aPtrStr;
245 aPtrStr << theObject.get();
246 qDebug(QString("erase object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
247 qDebug(getResult2AISObjectMapInfo().c_str());
251 void XGUI_Displayer::redisplay(ObjectPtr theObject, bool theUpdateViewer)
253 if (!isVisible(theObject))
256 AISObjectPtr aAISObj = getAISObject(theObject);
257 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
259 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
261 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
263 erase(theObject, theUpdateViewer);
266 if (aAIS_Obj != aAISObj) {
267 appendResultObject(theObject, aAIS_Obj);
269 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
272 if (!aAISIO.IsNull()) {
273 Handle(AIS_InteractiveContext) aContext = AISContext();
274 if (aContext.IsNull())
276 // Check that the visualized shape is the same and the redisplay is not necessary
277 // Redisplay of AIS object leads to this object selection compute and the selection
278 // in the browser is lost
280 // this check is not necessary anymore because the selection store/restore is realized
281 // before and after the values modification.
282 // Moreother, this check avoids customize and redisplay presentation if the presentable
283 // parameter is changed.
284 bool isEqualShapes = false;
285 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
286 if (aResult.get() != NULL) {
287 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(aAISIO);
288 if (!aShapePrs.IsNull()) {
289 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
290 if (aShapePtr.get()) {
291 const TopoDS_Shape& aOldShape = aShapePrs->Shape();
292 if (!aOldShape.IsNull())
293 isEqualShapes = aOldShape.IsEqual(aShapePtr->impl<TopoDS_Shape>());
297 // Customization of presentation
298 bool isCustomized = customizeObject(theObject);
299 #ifdef DEBUG_FEATURE_REDISPLAY
300 qDebug(QString("Redisplay: %1, isEqualShapes=%2, isCustomized=%3").
301 arg(!isEqualShapes || isCustomized).arg(isEqualShapes).arg(isCustomized).toStdString().c_str());
303 if (!isEqualShapes || isCustomized) {
304 aContext->Redisplay(aAISIO, false);
305 #ifdef DEBUG_FEATURE_REDISPLAY
306 qDebug(" Redisplay happens");
314 void XGUI_Displayer::redisplayObjects()
316 // redisplay objects visualized in the viewer
317 static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
318 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
319 QObjectPtrList aDisplayed = myWorkshop->displayer()->displayedObjects();
320 QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
321 for (; anIt != aLast; anIt++) {
322 aECreator->sendUpdated(*anIt, EVENT_DISP);
324 Events_Loop::loop()->flush(EVENT_DISP);
327 void XGUI_Displayer::deactivate(ObjectPtr theObject, const bool theUpdateViewer)
329 #ifdef DEBUG_DEACTIVATE
330 QString anInfoStr = ModuleBase_Tools::objectInfo(theObject);
331 qDebug(QString("deactivate: myActiveSelectionModes[%1]: %2, objects = ").
332 arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
334 toStdString().c_str());
336 if (isVisible(theObject)) {
337 Handle(AIS_InteractiveContext) aContext = AISContext();
338 if (aContext.IsNull())
341 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
342 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
344 deactivateAIS(anAIS);
345 // the selection from the previous activation modes should be cleared manually (#26172)
346 aContext->LocalContext()->ClearOutdatedSelection(anAIS, true);
352 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
353 const bool theUpdateViewer)
355 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
356 for (; anIt != aLast; anIt++) {
357 deactivate(*anIt, false);
363 void XGUI_Displayer::getModesOfActivation(ObjectPtr theObject, QIntList& theModes)
365 if (!isVisible(theObject))
368 Handle(AIS_InteractiveContext) aContext = AISContext();
369 if (aContext.IsNull())
372 AISObjectPtr aAISObj = getAISObject(theObject);
374 if (aAISObj.get() != NULL) {
375 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
376 TColStd_ListOfInteger aTColModes;
377 aContext->ActivatedModes(anAISIO, aTColModes);
378 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
379 for (; itr.More(); itr.Next() ) {
380 theModes.append(itr.Value());
385 void XGUI_Displayer::activateObjects(const QIntList& theModes, const QObjectPtrList& theObjList,
386 const bool theUpdateViewer)
388 // Convert shape types to selection types
390 foreach(int aType, theModes) {
391 if (aType >= TopAbs_SHAPE)
392 aModes.append(aType);
394 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum)aType));
397 #ifdef DEBUG_ACTIVATE_OBJECTS
399 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
400 for (; anIt != aLast; ++anIt) {
401 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
403 QString anInfoStr = anInfo.join(", ");
405 qDebug(QString("activateObjects: aModes[%1] = %2, myActiveSelectionModes[%3] = %4, objects = %5").
406 arg(aModes.size()).arg(qIntListInfo(aModes)).
407 arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
409 toStdString().c_str());
411 // In order to avoid doblications of selection modes
413 foreach (int aMode, aModes) {
414 if (!aNewModes.contains(aMode))
415 aNewModes.append(aMode);
417 myActiveSelectionModes = aNewModes;
418 Handle(AIS_InteractiveContext) aContext = AISContext();
419 if (aContext.IsNull())
421 // Open local context if there is no one
422 if (!aContext->HasOpenedContext())
425 //aContext->UseDisplayedObjects();
426 //myUseExternalObjects = true;
428 Handle(AIS_InteractiveObject) anAISIO;
429 AIS_ListOfInteractive aPrsList;
430 if (theObjList.isEmpty())
433 foreach(ObjectPtr aObj, theObjList) {
434 if (myResult2AISObjectMap.contains(aObj))
435 aPrsList.Append(myResult2AISObjectMap[aObj]->impl<Handle(AIS_InteractiveObject)>());
439 AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
440 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
441 anAISIO = aLIt.Value();
442 activate(anAISIO, myActiveSelectionModes, false);
448 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
450 Handle(AIS_InteractiveContext) aContext = AISContext();
451 if (aContext.IsNull())
453 if (!isVisible(theObject))
456 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
457 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
459 TColStd_ListOfInteger aModes;
460 aContext->ActivatedModes(anAIS, aModes);
461 return aModes.Extent() > 0;
463 void XGUI_Displayer::setSelected(const QList<ModuleBase_ViewerPrs>& theValues, bool theUpdateViewer)
465 Handle(AIS_InteractiveContext) aContext = AISContext();
466 if (aContext.IsNull())
468 if (aContext->HasOpenedContext()) {
469 aContext->UnhilightSelected();
470 aContext->ClearSelected();
471 foreach (ModuleBase_ViewerPrs aPrs, theValues) {
472 const TopoDS_Shape& aShape = aPrs.shape();
473 if (!aShape.IsNull()) {
474 aContext->AddOrRemoveSelected(aShape, false);
476 ObjectPtr anObject = aPrs.object();
477 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
478 if (aResult.get() && isVisible(aResult)) {
479 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
480 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
481 if (!anAIS.IsNull()) {
482 // The methods are replaced in order to provide multi-selection, e.g. restore selection
483 // by activating multi selector widget. It also gives an advantage that the multi
484 // selection in OB gives multi-selection in the viewer
485 //aContext->SetSelected(anAIS, false);
486 // The selection in the context was cleared, so the method sets the objects are selected
487 aContext->AddOrRemoveSelected(anAIS, false);
493 aContext->UnhilightCurrents();
494 aContext->ClearCurrents();
495 foreach (ModuleBase_ViewerPrs aPrs, theValues) {
496 ObjectPtr anObject = aPrs.object();
497 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
498 if (aResult.get() && isVisible(aResult)) {
499 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
500 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
502 aContext->SetCurrentObject(anAIS, false);
510 void XGUI_Displayer::clearSelected()
512 Handle(AIS_InteractiveContext) aContext = AISContext();
514 aContext->UnhilightCurrents(false);
515 aContext->ClearSelected();
519 void XGUI_Displayer::eraseAll(const bool theUpdateViewer)
521 Handle(AIS_InteractiveContext) aContext = AISContext();
522 if (!aContext.IsNull()) {
523 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
524 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
526 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
527 if (!anIO.IsNull()) {
528 emit beforeObjectErase(aObj, aAISObj);
529 aContext->Remove(anIO, false);
535 myResult2AISObjectMap.clear();
538 qDebug(getResult2AISObjectMapInfo().c_str());
542 void XGUI_Displayer::deactivateTrihedron() const
544 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
546 AIS_ListOfInteractive aList;
547 aContext->DisplayedObjects(aList, true);
548 AIS_ListIteratorOfListOfInteractive aIt;
549 for (aIt.Initialize(aList); aIt.More(); aIt.Next()) {
550 Handle(AIS_Trihedron) aTrihedron = Handle(AIS_Trihedron)::DownCast(aIt.Value());
551 if (!aTrihedron.IsNull()) {
552 aContext->Deactivate(aTrihedron);
557 void XGUI_Displayer::openLocalContext()
559 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
560 if (aContext.IsNull())
562 // Open local context if there is no one
563 if (!aContext->HasOpenedContext()) {
564 // Preserve selected objects
565 //AIS_ListOfInteractive aAisList;
566 //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
567 // aAisList.Append(aContext->Current());
569 // get the filters from the global context and append them to the local context
570 // a list of filters in the global context is not cleared and should be cleared here
571 SelectMgr_ListOfFilter aFilters;
572 aFilters.Assign(aContext->Filters());
573 // it is important to remove the filters in the global context, because there is a code
574 // in the closeLocalContex, which restore the global context filters
575 aContext->RemoveFilters();
577 //aContext->ClearCurrents();
578 aContext->OpenLocalContext();
579 deactivateTrihedron();
580 //aContext->NotUseDisplayedObjects();
582 //myUseExternalObjects = false;
584 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
585 for (;aIt.More(); aIt.Next()) {
586 aContext->AddFilter(aIt.Value());
589 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
590 //for(; aIt2.More(); aIt2.Next()) {
591 // aContext->SetSelected(aIt2.Value(), false);
596 void XGUI_Displayer::closeLocalContexts(const bool theUpdateViewer)
598 Handle(AIS_InteractiveContext) aContext = AISContext();
599 if ( (!aContext.IsNull()) && (aContext->HasOpenedContext()) ) {
600 // Preserve selected objects
601 //AIS_ListOfInteractive aAisList;
602 //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
603 // aAisList.Append(aContext->SelectedInteractive());
605 // get the filters from the local context and append them to the global context
606 // a list of filters in the local context is cleared
607 SelectMgr_ListOfFilter aFilters;
608 aFilters.Assign(aContext->Filters());
610 //aContext->ClearSelected();
611 aContext->CloseAllContexts(false);
613 // From the moment when the AIS_DS_Displayed flag is used in the Display of AIS object,
614 // this code is obsolete. It is temporaty commented and should be removed after
615 // the test campaign.
616 // Redisplay all object if they were displayed in localContext
617 /*Handle(AIS_InteractiveObject) aAISIO;
618 foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
619 aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
620 if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
621 aContext->Display(aAISIO, false);
622 aContext->SetDisplayMode(aAISIO, Shading, false);
626 // Append the filters from the local selection in the global selection context
627 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
628 for (;aIt.More(); aIt.Next()) {
629 Handle(SelectMgr_Filter) aFilter = aIt.Value();
630 aContext->AddFilter(aFilter);
635 //myUseExternalObjects = false;
638 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
639 //for(; aIt2.More(); aIt2.Next()) {
640 // if (aContext->IsDisplayed(aIt2.Value()))
641 // aContext->SetCurrentObject(aIt2.Value(), false);
646 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
649 if (myResult2AISObjectMap.contains(theObject))
650 anIO = myResult2AISObjectMap[theObject];
654 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
656 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
657 return getObject(aRefAIS);
660 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
663 foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
664 AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
665 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
671 if (!anObject.get()) {
672 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
673 if (!theIO.IsNull()) {
674 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
676 anObject = myWorkshop->module()->findPresentedObject(anAISObj);
681 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
683 bool aWasEnabled = myEnableUpdateViewer;
685 myEnableUpdateViewer = isEnabled;
690 void XGUI_Displayer::updateViewer() const
692 Handle(AIS_InteractiveContext) aContext = AISContext();
693 if (!aContext.IsNull() && myEnableUpdateViewer) {
694 myWorkshop->viewer()->Zfitall();
695 aContext->UpdateCurrentViewer();
699 void XGUI_Displayer::activateAIS(const Handle(AIS_InteractiveObject)& theIO,
700 const int theMode, const bool theUpdateViewer) const
702 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
703 aContext->Activate(theIO, theMode, theUpdateViewer);
705 #ifdef DEBUG_ACTIVATE_AIS
706 ObjectPtr anObject = getObject(theIO);
707 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
708 qDebug(QString("activateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
712 void XGUI_Displayer::deactivateAIS(const Handle(AIS_InteractiveObject)& theIO, const int theMode) const
714 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
716 aContext->Deactivate(theIO);
718 aContext->Deactivate(theIO, theMode);
720 #ifdef DEBUG_DEACTIVATE_AIS
721 ObjectPtr anObject = getObject(theIO);
722 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
723 qDebug(QString("deactivateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
727 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
729 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
730 if ((!aContext.IsNull()) && (!aContext->HasOpenedContext())) {
731 aContext->OpenLocalContext();
732 deactivateTrihedron();
733 aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
734 aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
739 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
741 Handle(AIS_InteractiveContext) aContext = AISContext();
742 if (myAndFilter.IsNull() && !aContext.IsNull()) {
743 myAndFilter = new SelectMgr_AndFilter();
744 aContext->AddFilter(myAndFilter);
749 void XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
750 bool theUpdateViewer)
752 Handle(AIS_InteractiveContext) aContext = AISContext();
753 if (aContext.IsNull())
755 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
756 if (!anAISIO.IsNull()) {
757 aContext->Display(anAISIO, 0/*wireframe*/, 0, theUpdateViewer, true, AIS_DS_Displayed);
758 aContext->Deactivate(anAISIO);
759 aContext->Load(anAISIO);
760 if (toActivateInSelectionModes) {
761 if (aContext->HasOpenedContext()) {
762 if (myActiveSelectionModes.size() == 0)
763 activateAIS(anAISIO, 0, theUpdateViewer);
765 foreach(int aMode, myActiveSelectionModes) {
766 activateAIS(anAISIO, aMode, theUpdateViewer);
774 void XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
776 Handle(AIS_InteractiveContext) aContext = AISContext();
777 if (aContext.IsNull())
779 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
780 if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
781 aContext->Remove(anAISIO, theUpdateViewer);
786 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
788 if (theMode == NoMode)
791 Handle(AIS_InteractiveContext) aContext = AISContext();
792 if (aContext.IsNull())
795 AISObjectPtr aAISObj = getAISObject(theObject);
799 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
800 aContext->SetDisplayMode(aAISIO, theMode, false);
801 // Redisplay in order to update new mode because it could be not computed before
806 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
808 Handle(AIS_InteractiveContext) aContext = AISContext();
809 if (aContext.IsNull())
812 AISObjectPtr aAISObj = getAISObject(theObject);
816 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
817 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
820 void XGUI_Displayer::deactivateSelectionFilters()
822 Handle(AIS_InteractiveContext) aContext = AISContext();
823 if (!myAndFilter.IsNull() && !aContext.IsNull()) {
825 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
826 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
827 for (; anIt.More() && !aFound; anIt.Next()) {
828 Handle(SelectMgr_Filter) aFilter = anIt.Value();
829 aFound = aFilter == myAndFilter;
832 aContext->RemoveFilter(myAndFilter);
833 myAndFilter.Nullify();
838 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
840 Handle(AIS_InteractiveContext) aContext = AISContext();
841 if (aContext.IsNull() || hasSelectionFilter(theFilter))
844 Handle(SelectMgr_CompositionFilter) aCompFilter = GetFilter();
845 aCompFilter->Add(theFilter);
846 #ifdef DEBUG_SELECTION_FILTERS
847 int aCount = GetFilter()->StoredFilters().Extent();
848 qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
852 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
854 Handle(AIS_InteractiveContext) aContext = AISContext();
855 if (aContext.IsNull())
857 Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
858 if (aCompositeFilter->IsIn(theFilter))
859 aCompositeFilter->Remove(theFilter);
860 #ifdef DEBUG_SELECTION_FILTERS
861 int aCount = GetFilter()->StoredFilters().Extent();
862 qDebug(QString("removeSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
866 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
868 bool aFilterFound = false;
870 Handle(AIS_InteractiveContext) aContext = AISContext();
871 if (aContext.IsNull())
873 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
874 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
875 for (; aIt.More() && !aFilterFound; aIt.Next()) {
876 if (theFilter.Access() == aIt.Value().Access())
879 Handle(SelectMgr_CompositionFilter) aCompFilter = GetFilter();
880 const SelectMgr_ListOfFilter& aStoredFilters = aCompFilter->StoredFilters();
881 for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
882 if (theFilter.Access() == aIt.Value().Access())
888 void XGUI_Displayer::removeFilters()
890 Handle(AIS_InteractiveContext) aContext = AISContext();
891 if (aContext.IsNull())
893 GetFilter()->Clear();
896 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
898 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
899 foreach(ObjectPtr aObj, aDispList) {
900 if (!theList.contains(aObj))
903 foreach(ObjectPtr aObj, theList) {
904 if (!isVisible(aObj))
905 display(aObj, false);
910 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
912 if (!isVisible(theObject))
915 AISObjectPtr aAISObj = getAISObject(theObject);
916 if (aAISObj.get() == NULL)
919 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
920 return ::canBeShaded(anAIS);
923 void XGUI_Displayer::activate(const Handle(AIS_InteractiveObject)& theIO,
924 const QIntList& theModes,
925 const bool theUpdateViewer) const
927 Handle(AIS_InteractiveContext) aContext = AISContext();
928 if (aContext.IsNull() || theIO.IsNull())
931 // deactivate object in all modes, which are not in the list of activation
932 // It seems that after the IO deactivation the selected state of the IO's owners
933 // is modified in OCC(version: 6.8.0) and the selection of the object later is lost.
934 // By this reason, the number of the IO deactivate is decreased and the object is deactivated
935 // only if there is a difference in the current modes and the parameters modes.
936 // If the selection problem happens again, it is possible to write a test scenario and create
937 // a bug. The bug steps are the following:
938 // Create two IO, activate them in 5 modes, select the first IO, deactivate 3 modes for both,
939 // with clicked SHIFT select the second object. The result is the selection of the first IO is lost.
940 TColStd_ListOfInteger aTColModes;
941 aContext->ActivatedModes(theIO, aTColModes);
942 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
943 QIntList aModesActivatedForIO;
944 bool isDeactivated = false;
945 for (; itr.More(); itr.Next() ) {
946 Standard_Integer aMode = itr.Value();
947 if (!theModes.contains(aMode)) {
948 deactivateAIS(theIO, aMode);
949 isDeactivated = true;
952 aModesActivatedForIO.append(aMode);
956 // the selection from the previous activation modes should be cleared manually (#26172)
957 aContext->LocalContext()->ClearOutdatedSelection(theIO, true);
962 // loading the interactive object allowing the decomposition
963 if (aTColModes.IsEmpty()) {
964 aContext->Load(theIO, -1, true);
967 // trihedron AIS check should be after the AIS loading.
968 // If it is not loaded, it is steel selectable in the viewer.
969 Handle(AIS_Trihedron) aTrihedron = Handle(AIS_Trihedron)::DownCast(theIO);
970 if (aTrihedron.IsNull()) {
971 //aContext->Load(anAISIO, -1, true);
972 // In order to clear active modes list
973 if (theModes.size() == 0) {
974 //aContext->Load(anAISIO, 0, true);
975 activateAIS(theIO, 0, theUpdateViewer);
977 foreach(int aMode, theModes) {
978 //aContext->Load(anAISIO, aMode, true);
979 if (!aModesActivatedForIO.contains(aMode)) {
980 activateAIS(theIO, aMode, theUpdateViewer);
987 bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
989 AISObjectPtr anAISObj = getAISObject(theObject);
990 // correct the result's color it it has the attribute
991 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
993 // Customization of presentation
994 GeomCustomPrsPtr aCustomPrs;
995 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
996 if (aFeature.get() != NULL) {
997 GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
998 if (aCustPrs.get() != NULL)
999 aCustomPrs = aCustPrs;
1001 if (aCustomPrs.get() == NULL) {
1002 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
1003 // we ignore presentable not customized objects
1004 if (aPrs.get() == NULL)
1005 aCustomPrs = myCustomPrs;
1007 bool isCustomized = aCustomPrs.get() &&
1008 aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
1009 return isCustomized;
1013 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject, const QColor& theColor, bool theUpdateViewer)
1015 if (!isVisible(theObject))
1018 AISObjectPtr anAISObj = getAISObject(theObject);
1020 anAISObj->getColor(aR, aG, aB);
1021 anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
1022 if (theUpdateViewer)
1024 return QColor(aR, aG, aB);
1027 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
1029 myResult2AISObjectMap[theObject] = theAIS;
1031 #ifdef DEBUG_DISPLAY
1032 std::ostringstream aPtrStr;
1033 aPtrStr << theObject.get();
1034 qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
1035 qDebug(getResult2AISObjectMapInfo().c_str());
1039 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
1041 QStringList aContent;
1042 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
1043 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
1044 std::ostringstream aPtrStr;
1045 aPtrStr << "aObj = " << aObj.get() << ":";
1046 aPtrStr << "anAIS = " << aAISObj.get() << ":";
1047 aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
1049 aContent.append(aPtrStr.str().c_str());
1051 return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
1052 arg(aContent.join("\n")).toStdString().c_str();