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 bool XGUI_Displayer::display(ObjectPtr theObject, bool theUpdateViewer)
116 bool aDisplayed = false;
117 if (isVisible(theObject)) {
118 #ifdef DEBUG_COMPOSILID_DISPLAY
119 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
120 if (aCompsolidResult.get()) {
121 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
122 ResultPtr aSubResult = aCompsolidResult->subResult(i);
123 if (aSubResult.get())
124 redisplay(aSubResult, false);
131 aDisplayed = 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 aDisplayed = display(theObject, anAIS, isShading, theUpdateViewer);
172 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS)
174 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
175 if (!aShapePrs.IsNull()) {
176 TopoDS_Shape aShape = aShapePrs->Shape();
179 TopAbs_ShapeEnum aType = aShape.ShapeType();
180 if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
183 // Check that the presentation is not a sketch
184 Handle(ModuleBase_ResultPrs) aPrs = Handle(ModuleBase_ResultPrs)::DownCast(theAIS);
186 return !aPrs->isSketchMode();
193 bool XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
194 bool isShading, bool theUpdateViewer)
196 bool aDisplayed = false;
198 Handle(AIS_InteractiveContext) aContext = AISContext();
199 if (aContext.IsNull())
202 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
203 if (!anAISIO.IsNull()) {
204 appendResultObject(theObject, theAIS);
206 bool isCustomized = customizeObject(theObject);
208 int aDispMode = isShading? Shading : Wireframe;
210 anAISIO->Attributes()->SetFaceBoundaryDraw( Standard_True );
211 anAISIO->SetDisplayMode(aDispMode);
212 aContext->Display(anAISIO, aDispMode, 0, false, true, AIS_DS_Displayed);
215 emit objectDisplayed(theObject, theAIS);
216 activate(anAISIO, myActiveSelectionModes, theUpdateViewer);
217 // the fix from VPA for more suitable selection of sketcher lines
218 if(anAISIO->Width() > 1) {
219 for(int aModeIdx = 0; aModeIdx < myActiveSelectionModes.length(); ++aModeIdx) {
220 aContext->SetSelectionSensitivity(anAISIO,
221 myActiveSelectionModes.value(aModeIdx), anAISIO->Width() + 2);
231 bool XGUI_Displayer::erase(ObjectPtr theObject, const bool theUpdateViewer)
233 bool aErased = false;
234 if (!isVisible(theObject))
237 Handle(AIS_InteractiveContext) aContext = AISContext();
238 if (aContext.IsNull())
240 AISObjectPtr anObject = myResult2AISObjectMap[theObject];
242 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
243 if (!anAIS.IsNull()) {
244 emit beforeObjectErase(theObject, anObject);
245 aContext->Remove(anAIS, theUpdateViewer);
249 myResult2AISObjectMap.remove(theObject);
252 std::ostringstream aPtrStr;
253 aPtrStr << theObject.get();
254 qDebug(QString("erase object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
255 qDebug(getResult2AISObjectMapInfo().c_str());
260 bool XGUI_Displayer::redisplay(ObjectPtr theObject, bool theUpdateViewer)
262 bool aRedisplayed = false;
263 if (!isVisible(theObject))
266 AISObjectPtr aAISObj = getAISObject(theObject);
267 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
269 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
271 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
273 aRedisplayed = erase(theObject, theUpdateViewer);
276 if (aAIS_Obj != aAISObj) {
277 appendResultObject(theObject, aAIS_Obj);
279 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
282 Handle(AIS_InteractiveContext) aContext = AISContext();
283 if (!aAISIO.IsNull() && !aContext.IsNull()) {
284 // Check that the visualized shape is the same and the redisplay is not necessary
285 // Redisplay of AIS object leads to this object selection compute and the selection
286 // in the browser is lost
287 // this check is not necessary anymore because the selection store/restore is realized
288 // before and after the values modification.
289 // Moreother, this check avoids customize and redisplay presentation if the presentable
290 // parameter is changed.
291 bool isEqualShapes = false;
292 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
293 if (aResult.get() != NULL) {
294 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(aAISIO);
295 if (!aShapePrs.IsNull()) {
296 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
297 if (aShapePtr.get()) {
298 const TopoDS_Shape& aOldShape = aShapePrs->Shape();
299 if (!aOldShape.IsNull())
300 isEqualShapes = aOldShape.IsEqual(aShapePtr->impl<TopoDS_Shape>());
304 // Customization of presentation
305 bool isCustomized = customizeObject(theObject);
306 #ifdef DEBUG_FEATURE_REDISPLAY
307 qDebug(QString("Redisplay: %1, isEqualShapes=%2, isCustomized=%3").
308 arg(!isEqualShapes || isCustomized).arg(isEqualShapes).arg(isCustomized).toStdString().c_str());
310 if (!isEqualShapes || isCustomized) {
311 aContext->Redisplay(aAISIO, false);
313 #ifdef DEBUG_FEATURE_REDISPLAY
314 qDebug(" Redisplay happens");
323 void XGUI_Displayer::redisplayObjects()
325 // redisplay objects visualized in the viewer
326 static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
327 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
328 QObjectPtrList aDisplayed = myWorkshop->displayer()->displayedObjects();
329 QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
330 for (; anIt != aLast; anIt++) {
331 aECreator->sendUpdated(*anIt, EVENT_DISP);
333 Events_Loop::loop()->flush(EVENT_DISP);
336 void XGUI_Displayer::deactivate(ObjectPtr theObject, const bool theUpdateViewer)
338 #ifdef DEBUG_DEACTIVATE
339 QString anInfoStr = ModuleBase_Tools::objectInfo(theObject);
340 qDebug(QString("deactivate: myActiveSelectionModes[%1]: %2, objects = ").
341 arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
343 toStdString().c_str());
345 if (isVisible(theObject)) {
346 Handle(AIS_InteractiveContext) aContext = AISContext();
347 if (aContext.IsNull())
350 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
351 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
353 deactivateAIS(anAIS);
354 // the selection from the previous activation modes should be cleared manually (#26172)
355 aContext->LocalContext()->ClearOutdatedSelection(anAIS, true);
361 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
362 const bool theUpdateViewer)
364 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
365 for (; anIt != aLast; anIt++) {
366 deactivate(*anIt, false);
372 void XGUI_Displayer::getModesOfActivation(ObjectPtr theObject, QIntList& theModes)
374 if (!isVisible(theObject))
377 Handle(AIS_InteractiveContext) aContext = AISContext();
378 if (aContext.IsNull())
381 AISObjectPtr aAISObj = getAISObject(theObject);
383 if (aAISObj.get() != NULL) {
384 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
385 TColStd_ListOfInteger aTColModes;
386 aContext->ActivatedModes(anAISIO, aTColModes);
387 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
388 for (; itr.More(); itr.Next() ) {
389 theModes.append(itr.Value());
394 void XGUI_Displayer::activateObjects(const QIntList& theModes, const QObjectPtrList& theObjList,
395 const bool theUpdateViewer)
397 // Convert shape types to selection types
399 foreach(int aType, theModes) {
400 if (aType >= TopAbs_SHAPE)
401 aModes.append(aType);
403 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum)aType));
406 #ifdef DEBUG_ACTIVATE_OBJECTS
408 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
409 for (; anIt != aLast; ++anIt) {
410 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
412 QString anInfoStr = anInfo.join(", ");
414 qDebug(QString("activateObjects: aModes[%1] = %2, myActiveSelectionModes[%3] = %4, objects = %5").
415 arg(aModes.size()).arg(qIntListInfo(aModes)).
416 arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
418 toStdString().c_str());
420 // In order to avoid doblications of selection modes
422 foreach (int aMode, aModes) {
423 if (!aNewModes.contains(aMode))
424 aNewModes.append(aMode);
426 myActiveSelectionModes = aNewModes;
427 Handle(AIS_InteractiveContext) aContext = AISContext();
428 if (aContext.IsNull())
430 // Open local context if there is no one
431 if (!aContext->HasOpenedContext())
434 //aContext->UseDisplayedObjects();
435 //myUseExternalObjects = true;
437 Handle(AIS_InteractiveObject) anAISIO;
438 AIS_ListOfInteractive aPrsList;
439 if (theObjList.isEmpty())
442 foreach(ObjectPtr aObj, theObjList) {
443 if (myResult2AISObjectMap.contains(aObj))
444 aPrsList.Append(myResult2AISObjectMap[aObj]->impl<Handle(AIS_InteractiveObject)>());
448 AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
449 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
450 anAISIO = aLIt.Value();
451 activate(anAISIO, myActiveSelectionModes, false);
457 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
459 Handle(AIS_InteractiveContext) aContext = AISContext();
460 if (aContext.IsNull())
462 if (!isVisible(theObject))
465 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
466 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
468 TColStd_ListOfInteger aModes;
469 aContext->ActivatedModes(anAIS, aModes);
470 return aModes.Extent() > 0;
472 void XGUI_Displayer::setSelected(const QList<ModuleBase_ViewerPrs>& theValues, bool theUpdateViewer)
474 Handle(AIS_InteractiveContext) aContext = AISContext();
475 if (aContext.IsNull())
477 if (aContext->HasOpenedContext()) {
478 aContext->UnhilightSelected();
479 aContext->ClearSelected();
480 foreach (ModuleBase_ViewerPrs aPrs, theValues) {
481 const TopoDS_Shape& aShape = aPrs.shape();
482 if (!aShape.IsNull()) {
483 aContext->AddOrRemoveSelected(aShape, false);
485 ObjectPtr anObject = aPrs.object();
486 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
487 if (aResult.get() && isVisible(aResult)) {
488 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
489 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
490 if (!anAIS.IsNull()) {
491 // The methods are replaced in order to provide multi-selection, e.g. restore selection
492 // by activating multi selector widget. It also gives an advantage that the multi
493 // selection in OB gives multi-selection in the viewer
494 //aContext->SetSelected(anAIS, false);
495 // The selection in the context was cleared, so the method sets the objects are selected
496 aContext->AddOrRemoveSelected(anAIS, false);
502 aContext->UnhilightCurrents();
503 aContext->ClearCurrents();
504 foreach (ModuleBase_ViewerPrs aPrs, theValues) {
505 ObjectPtr anObject = aPrs.object();
506 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
507 if (aResult.get() && isVisible(aResult)) {
508 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
509 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
511 aContext->SetCurrentObject(anAIS, false);
519 void XGUI_Displayer::clearSelected()
521 Handle(AIS_InteractiveContext) aContext = AISContext();
523 aContext->UnhilightCurrents(false);
524 aContext->ClearSelected();
528 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
530 bool aErased = false;
531 Handle(AIS_InteractiveContext) aContext = AISContext();
532 if (!aContext.IsNull()) {
533 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
534 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
536 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
537 if (!anIO.IsNull()) {
538 emit beforeObjectErase(aObj, aAISObj);
539 aContext->Remove(anIO, false);
546 myResult2AISObjectMap.clear();
549 qDebug(getResult2AISObjectMapInfo().c_str());
554 void XGUI_Displayer::deactivateTrihedron() const
556 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
558 AIS_ListOfInteractive aList;
559 aContext->DisplayedObjects(aList, true);
560 AIS_ListIteratorOfListOfInteractive aIt;
561 for (aIt.Initialize(aList); aIt.More(); aIt.Next()) {
562 Handle(AIS_Trihedron) aTrihedron = Handle(AIS_Trihedron)::DownCast(aIt.Value());
563 if (!aTrihedron.IsNull()) {
564 aContext->Deactivate(aTrihedron);
569 void XGUI_Displayer::openLocalContext()
571 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
572 if (aContext.IsNull())
574 // Open local context if there is no one
575 if (!aContext->HasOpenedContext()) {
576 // Preserve selected objects
577 //AIS_ListOfInteractive aAisList;
578 //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
579 // aAisList.Append(aContext->Current());
581 // get the filters from the global context and append them to the local context
582 // a list of filters in the global context is not cleared and should be cleared here
583 SelectMgr_ListOfFilter aFilters;
584 aFilters.Assign(aContext->Filters());
585 // it is important to remove the filters in the global context, because there is a code
586 // in the closeLocalContex, which restore the global context filters
587 aContext->RemoveFilters();
589 //aContext->ClearCurrents();
590 aContext->OpenLocalContext();
591 deactivateTrihedron();
592 //aContext->NotUseDisplayedObjects();
594 //myUseExternalObjects = false;
596 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
597 for (;aIt.More(); aIt.Next()) {
598 aContext->AddFilter(aIt.Value());
601 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
602 //for(; aIt2.More(); aIt2.Next()) {
603 // aContext->SetSelected(aIt2.Value(), false);
608 void XGUI_Displayer::closeLocalContexts(const bool theUpdateViewer)
610 Handle(AIS_InteractiveContext) aContext = AISContext();
611 if ( (!aContext.IsNull()) && (aContext->HasOpenedContext()) ) {
612 // Preserve selected objects
613 //AIS_ListOfInteractive aAisList;
614 //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
615 // aAisList.Append(aContext->SelectedInteractive());
617 // get the filters from the local context and append them to the global context
618 // a list of filters in the local context is cleared
619 SelectMgr_ListOfFilter aFilters;
620 aFilters.Assign(aContext->Filters());
622 //aContext->ClearSelected();
623 aContext->CloseAllContexts(false);
625 // From the moment when the AIS_DS_Displayed flag is used in the Display of AIS object,
626 // this code is obsolete. It is temporaty commented and should be removed after
627 // the test campaign.
628 // Redisplay all object if they were displayed in localContext
629 /*Handle(AIS_InteractiveObject) aAISIO;
630 foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
631 aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
632 if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
633 aContext->Display(aAISIO, false);
634 aContext->SetDisplayMode(aAISIO, Shading, false);
638 // Append the filters from the local selection in the global selection context
639 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
640 for (;aIt.More(); aIt.Next()) {
641 Handle(SelectMgr_Filter) aFilter = aIt.Value();
642 aContext->AddFilter(aFilter);
647 //myUseExternalObjects = false;
650 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
651 //for(; aIt2.More(); aIt2.Next()) {
652 // if (aContext->IsDisplayed(aIt2.Value()))
653 // aContext->SetCurrentObject(aIt2.Value(), false);
658 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
661 if (myResult2AISObjectMap.contains(theObject))
662 anIO = myResult2AISObjectMap[theObject];
666 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
668 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
669 return getObject(aRefAIS);
672 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
675 foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
676 AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
677 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
683 if (!anObject.get()) {
684 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
685 if (!theIO.IsNull()) {
686 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
688 anObject = myWorkshop->module()->findPresentedObject(anAISObj);
693 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
695 bool aWasEnabled = myEnableUpdateViewer;
697 myEnableUpdateViewer = isEnabled;
702 void XGUI_Displayer::updateViewer() const
704 Handle(AIS_InteractiveContext) aContext = AISContext();
705 if (!aContext.IsNull() && myEnableUpdateViewer) {
706 myWorkshop->viewer()->Zfitall();
707 aContext->UpdateCurrentViewer();
711 void XGUI_Displayer::activateAIS(const Handle(AIS_InteractiveObject)& theIO,
712 const int theMode, const bool theUpdateViewer) const
714 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
715 aContext->Activate(theIO, theMode, theUpdateViewer);
717 #ifdef DEBUG_ACTIVATE_AIS
718 ObjectPtr anObject = getObject(theIO);
719 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
720 qDebug(QString("activateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
724 void XGUI_Displayer::deactivateAIS(const Handle(AIS_InteractiveObject)& theIO, const int theMode) const
726 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
728 aContext->Deactivate(theIO);
730 aContext->Deactivate(theIO, theMode);
732 #ifdef DEBUG_DEACTIVATE_AIS
733 ObjectPtr anObject = getObject(theIO);
734 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
735 qDebug(QString("deactivateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
739 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
741 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
742 if ((!aContext.IsNull()) && (!aContext->HasOpenedContext())) {
743 aContext->OpenLocalContext();
744 deactivateTrihedron();
745 aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
746 aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
751 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
753 Handle(AIS_InteractiveContext) aContext = AISContext();
754 if (myAndFilter.IsNull() && !aContext.IsNull()) {
755 myAndFilter = new SelectMgr_AndFilter();
756 aContext->AddFilter(myAndFilter);
761 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
762 bool theUpdateViewer)
764 bool aDisplayed = false;
765 Handle(AIS_InteractiveContext) aContext = AISContext();
766 if (aContext.IsNull())
768 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
769 if (!anAISIO.IsNull()) {
770 aContext->Display(anAISIO, 0/*wireframe*/, 0, theUpdateViewer, true, AIS_DS_Displayed);
772 aContext->Deactivate(anAISIO);
773 aContext->Load(anAISIO);
774 if (toActivateInSelectionModes) {
775 if (aContext->HasOpenedContext()) {
776 if (myActiveSelectionModes.size() == 0)
777 activateAIS(anAISIO, 0, theUpdateViewer);
779 foreach(int aMode, myActiveSelectionModes) {
780 activateAIS(anAISIO, aMode, theUpdateViewer);
789 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
791 bool aErased = false;
792 Handle(AIS_InteractiveContext) aContext = AISContext();
793 if (aContext.IsNull())
795 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
796 if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
797 aContext->Remove(anAISIO, theUpdateViewer);
804 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
806 if (theMode == NoMode)
809 Handle(AIS_InteractiveContext) aContext = AISContext();
810 if (aContext.IsNull())
813 AISObjectPtr aAISObj = getAISObject(theObject);
817 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
818 aContext->SetDisplayMode(aAISIO, theMode, false);
819 // Redisplay in order to update new mode because it could be not computed before
824 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
826 Handle(AIS_InteractiveContext) aContext = AISContext();
827 if (aContext.IsNull())
830 AISObjectPtr aAISObj = getAISObject(theObject);
834 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
835 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
838 void XGUI_Displayer::deactivateSelectionFilters()
840 Handle(AIS_InteractiveContext) aContext = AISContext();
841 if (!myAndFilter.IsNull() && !aContext.IsNull()) {
843 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
844 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
845 for (; anIt.More() && !aFound; anIt.Next()) {
846 Handle(SelectMgr_Filter) aFilter = anIt.Value();
847 aFound = aFilter == myAndFilter;
850 aContext->RemoveFilter(myAndFilter);
851 myAndFilter.Nullify();
856 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
858 Handle(AIS_InteractiveContext) aContext = AISContext();
859 if (aContext.IsNull() || hasSelectionFilter(theFilter))
862 Handle(SelectMgr_CompositionFilter) aCompFilter = GetFilter();
863 aCompFilter->Add(theFilter);
864 #ifdef DEBUG_SELECTION_FILTERS
865 int aCount = GetFilter()->StoredFilters().Extent();
866 qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
870 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
872 Handle(AIS_InteractiveContext) aContext = AISContext();
873 if (aContext.IsNull())
875 Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
876 if (aCompositeFilter->IsIn(theFilter))
877 aCompositeFilter->Remove(theFilter);
878 #ifdef DEBUG_SELECTION_FILTERS
879 int aCount = GetFilter()->StoredFilters().Extent();
880 qDebug(QString("removeSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
884 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
886 bool aFilterFound = false;
888 Handle(AIS_InteractiveContext) aContext = AISContext();
889 if (aContext.IsNull())
891 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
892 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
893 for (; aIt.More() && !aFilterFound; aIt.Next()) {
894 if (theFilter.Access() == aIt.Value().Access())
897 Handle(SelectMgr_CompositionFilter) aCompFilter = GetFilter();
898 const SelectMgr_ListOfFilter& aStoredFilters = aCompFilter->StoredFilters();
899 for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
900 if (theFilter.Access() == aIt.Value().Access())
906 void XGUI_Displayer::removeFilters()
908 Handle(AIS_InteractiveContext) aContext = AISContext();
909 if (aContext.IsNull())
911 GetFilter()->Clear();
914 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
916 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
917 foreach(ObjectPtr aObj, aDispList) {
918 if (!theList.contains(aObj))
921 foreach(ObjectPtr aObj, theList) {
922 if (!isVisible(aObj))
923 display(aObj, false);
928 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
930 if (!isVisible(theObject))
933 AISObjectPtr aAISObj = getAISObject(theObject);
934 if (aAISObj.get() == NULL)
937 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
938 return ::canBeShaded(anAIS);
941 void XGUI_Displayer::activate(const Handle(AIS_InteractiveObject)& theIO,
942 const QIntList& theModes,
943 const bool theUpdateViewer) const
945 Handle(AIS_InteractiveContext) aContext = AISContext();
946 if (aContext.IsNull() || theIO.IsNull())
949 // deactivate object in all modes, which are not in the list of activation
950 // It seems that after the IO deactivation the selected state of the IO's owners
951 // is modified in OCC(version: 6.8.0) and the selection of the object later is lost.
952 // By this reason, the number of the IO deactivate is decreased and the object is deactivated
953 // only if there is a difference in the current modes and the parameters modes.
954 // If the selection problem happens again, it is possible to write a test scenario and create
955 // a bug. The bug steps are the following:
956 // Create two IO, activate them in 5 modes, select the first IO, deactivate 3 modes for both,
957 // with clicked SHIFT select the second object. The result is the selection of the first IO is lost.
958 TColStd_ListOfInteger aTColModes;
959 aContext->ActivatedModes(theIO, aTColModes);
960 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
961 QIntList aModesActivatedForIO;
962 bool isDeactivated = false;
963 for (; itr.More(); itr.Next() ) {
964 Standard_Integer aMode = itr.Value();
965 if (!theModes.contains(aMode)) {
966 deactivateAIS(theIO, aMode);
967 isDeactivated = true;
970 aModesActivatedForIO.append(aMode);
974 // the selection from the previous activation modes should be cleared manually (#26172)
975 aContext->LocalContext()->ClearOutdatedSelection(theIO, true);
980 // loading the interactive object allowing the decomposition
981 if (aTColModes.IsEmpty()) {
982 aContext->Load(theIO, -1, true);
985 // trihedron AIS check should be after the AIS loading.
986 // If it is not loaded, it is steel selectable in the viewer.
987 Handle(AIS_Trihedron) aTrihedron = Handle(AIS_Trihedron)::DownCast(theIO);
988 if (aTrihedron.IsNull()) {
989 //aContext->Load(anAISIO, -1, true);
990 // In order to clear active modes list
991 if (theModes.size() == 0) {
992 //aContext->Load(anAISIO, 0, true);
993 activateAIS(theIO, 0, theUpdateViewer);
995 foreach(int aMode, theModes) {
996 //aContext->Load(anAISIO, aMode, true);
997 if (!aModesActivatedForIO.contains(aMode)) {
998 activateAIS(theIO, aMode, theUpdateViewer);
1005 bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
1007 AISObjectPtr anAISObj = getAISObject(theObject);
1008 // correct the result's color it it has the attribute
1009 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1011 // Customization of presentation
1012 GeomCustomPrsPtr aCustomPrs;
1013 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1014 if (aFeature.get() != NULL) {
1015 GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
1016 if (aCustPrs.get() != NULL)
1017 aCustomPrs = aCustPrs;
1019 if (aCustomPrs.get() == NULL) {
1020 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
1021 // we ignore presentable not customized objects
1022 if (aPrs.get() == NULL)
1023 aCustomPrs = myCustomPrs;
1025 bool isCustomized = aCustomPrs.get() &&
1026 aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
1027 return isCustomized;
1031 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject, const QColor& theColor, bool theUpdateViewer)
1033 if (!isVisible(theObject))
1036 AISObjectPtr anAISObj = getAISObject(theObject);
1038 anAISObj->getColor(aR, aG, aB);
1039 anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
1040 if (theUpdateViewer)
1042 return QColor(aR, aG, aB);
1045 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
1047 myResult2AISObjectMap[theObject] = theAIS;
1049 #ifdef DEBUG_DISPLAY
1050 std::ostringstream aPtrStr;
1051 aPtrStr << theObject.get();
1052 qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
1053 qDebug(getResult2AISObjectMapInfo().c_str());
1057 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
1059 QStringList aContent;
1060 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
1061 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
1062 std::ostringstream aPtrStr;
1063 aPtrStr << "aObj = " << aObj.get() << ":";
1064 aPtrStr << "anAIS = " << aAISObj.get() << ":";
1065 aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
1067 aContent.append(aPtrStr.str().c_str());
1069 return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
1070 arg(aContent.join("\n")).toStdString().c_str();