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"
15 #include <AppElements_Viewer.h>
18 #include <ModelAPI_Document.h>
19 #include <ModelAPI_Data.h>
20 #include <ModelAPI_Object.h>
21 #include <ModelAPI_Tools.h>
22 #include <ModelAPI_AttributeIntArray.h>
23 #include <ModelAPI_ResultCompSolid.h>
25 #include <ModuleBase_ResultPrs.h>
26 #include <ModuleBase_Tools.h>
27 #include <ModuleBase_IModule.h>
29 #include <GeomAPI_Shape.h>
30 #include <GeomAPI_IPresentable.h>
31 #include <GeomAPI_ICustomPrs.h>
33 #include <AIS_InteractiveContext.hxx>
34 #include <AIS_LocalContext.hxx>
35 #include <AIS_ListOfInteractive.hxx>
36 #include <AIS_ListIteratorOfListOfInteractive.hxx>
37 #include <AIS_DimensionSelectionMode.hxx>
38 #include <AIS_Shape.hxx>
39 #include <AIS_Dimension.hxx>
40 #include <AIS_Trihedron.hxx>
41 #include <TColStd_ListIteratorOfListOfInteger.hxx>
42 #include <SelectMgr_ListOfFilter.hxx>
43 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
44 #include <Prs3d_Drawer.hxx>
45 #include <Prs3d_IsoAspect.hxx>
47 #include <StdSelect_ViewerSelector3d.hxx>
49 #include <TColStd_MapOfTransient.hxx>
50 #include <TColStd_MapIteratorOfMapOfTransient.hxx>
52 #include <Events_Loop.h>
53 #include <ModelAPI_Events.h>
57 const int MOUSE_SENSITIVITY_IN_PIXEL = 10; ///< defines the local context mouse selection sensitivity
59 //#define DEBUG_ACTIVATE_OBJECTS
60 //#define DEBUG_DEACTIVATE
61 //#define DEBUG_ACTIVATE_AIS
62 //#define DEBUG_DEACTIVATE_AIS
64 //#define DEBUG_DISPLAY
65 //#define DEBUG_FEATURE_REDISPLAY
66 //#define DEBUG_SELECTION_FILTERS
68 //#define DEBUG_COMPOSILID_DISPLAY
69 // Workaround for bug #25637
70 void displayedObjects(const Handle(AIS_InteractiveContext)& theAIS, AIS_ListOfInteractive& theList)
72 // Get from null point
73 theAIS->DisplayedObjects(theList, true);
74 if (theAIS->HasOpenedContext()) {
75 // get from local context
76 const Handle(AIS_LocalContext)& aLC = theAIS->LocalContext();
77 TColStd_MapOfTransient aMap;
78 int NbDisp = aLC->DisplayedObjects(aMap);
79 TColStd_MapIteratorOfMapOfTransient aIt(aMap);
81 Handle(AIS_InteractiveObject) curIO;
82 Handle(Standard_Transient) Tr;
83 for(; aIt.More(); aIt.Next()){
85 curIO = *((Handle(AIS_InteractiveObject)*) &Tr);
86 theList.Append(curIO);
91 QString qIntListInfo(const QIntList& theValues, const QString& theSeparator = QString(", "))
94 QIntList::const_iterator anIt = theValues.begin(), aLast = theValues.end();
95 for (; anIt != aLast; anIt++) {
96 anInfo.append(QString::number(*anIt));
98 return anInfo.join(theSeparator);
101 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
102 : myWorkshop(theWorkshop), myEnableUpdateViewer(true), myNeedUpdate(false)
104 enableUpdateViewer(true);
105 myCustomPrs = std::shared_ptr<GeomAPI_ICustomPrs>(new XGUI_CustomPrs());
108 XGUI_Displayer::~XGUI_Displayer()
112 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
114 return myResult2AISObjectMap.contains(theObject);
117 bool XGUI_Displayer::display(ObjectPtr theObject, bool theUpdateViewer)
119 bool aDisplayed = false;
120 if (isVisible(theObject)) {
121 #ifdef DEBUG_COMPOSILID_DISPLAY
122 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
123 if (aCompsolidResult.get()) {
124 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
125 ResultPtr aSubResult = aCompsolidResult->subResult(i);
126 if (aSubResult.get())
127 redisplay(aSubResult, false);
134 aDisplayed = redisplay(theObject, theUpdateViewer);
137 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
138 bool isShading = false;
139 if (aPrs.get() != NULL) {
140 anAIS = aPrs->getAISObject(anAIS);
142 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
143 if (aResult.get() != NULL) {
144 #ifdef DEBUG_COMPOSILID_DISPLAY
145 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
146 if (aCompsolidResult.get()) {
147 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
148 ResultPtr aSubResult = aCompsolidResult->subResult(i);
149 if (aSubResult.get())
150 display(aSubResult, false);
157 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
158 if (aShapePtr.get() != NULL) {
159 anAIS = AISObjectPtr(new GeomAPI_AISObject());
160 anAIS->setImpl(new Handle(AIS_InteractiveObject)(new ModuleBase_ResultPrs(aResult)));
161 //anAIS->createShape(aShapePtr);
164 #ifdef DEBUG_COMPOSILID_DISPLAY
170 aDisplayed = display(theObject, anAIS, isShading, theUpdateViewer);
175 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS)
177 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
178 if (!aShapePrs.IsNull()) {
179 TopoDS_Shape aShape = aShapePrs->Shape();
182 TopAbs_ShapeEnum aType = aShape.ShapeType();
183 if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
186 // Check that the presentation is not a sketch
187 Handle(ModuleBase_ResultPrs) aPrs = Handle(ModuleBase_ResultPrs)::DownCast(theAIS);
189 return !aPrs->isSketchMode();
196 bool XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
197 bool isShading, bool theUpdateViewer)
199 bool aDisplayed = false;
201 Handle(AIS_InteractiveContext) aContext = AISContext();
202 if (aContext.IsNull())
205 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
206 if (!anAISIO.IsNull()) {
207 appendResultObject(theObject, theAIS);
209 bool isCustomized = customizeObject(theObject);
211 int aDispMode = isShading? Shading : Wireframe;
213 anAISIO->Attributes()->SetFaceBoundaryDraw( Standard_True );
214 anAISIO->SetDisplayMode(aDispMode);
215 aContext->Display(anAISIO, aDispMode, 0, false, true, AIS_DS_Displayed);
218 emit objectDisplayed(theObject, theAIS);
219 activate(anAISIO, myActiveSelectionModes, theUpdateViewer);
220 // the fix from VPA for more suitable selection of sketcher lines
221 if(anAISIO->Width() > 1) {
222 for(int aModeIdx = 0; aModeIdx < myActiveSelectionModes.length(); ++aModeIdx) {
223 aContext->SetSelectionSensitivity(anAISIO,
224 myActiveSelectionModes.value(aModeIdx), anAISIO->Width() + 2);
234 bool XGUI_Displayer::erase(ObjectPtr theObject, const bool theUpdateViewer)
236 bool aErased = false;
237 if (!isVisible(theObject))
240 Handle(AIS_InteractiveContext) aContext = AISContext();
241 if (aContext.IsNull())
244 AISObjectPtr anObject = myResult2AISObjectMap[theObject];
246 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
247 if (!anAIS.IsNull()) {
248 emit beforeObjectErase(theObject, anObject);
249 aContext->Remove(anAIS, theUpdateViewer);
253 myResult2AISObjectMap.remove(theObject);
256 std::ostringstream aPtrStr;
257 aPtrStr << theObject.get();
258 qDebug(QString("erase object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
259 qDebug(getResult2AISObjectMapInfo().c_str());
264 bool XGUI_Displayer::redisplay(ObjectPtr theObject, bool theUpdateViewer)
266 bool aRedisplayed = false;
267 if (!isVisible(theObject))
270 AISObjectPtr aAISObj = getAISObject(theObject);
271 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
273 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
275 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
277 aRedisplayed = erase(theObject, theUpdateViewer);
280 if (aAIS_Obj != aAISObj) {
281 appendResultObject(theObject, aAIS_Obj);
283 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
286 Handle(AIS_InteractiveContext) aContext = AISContext();
287 if (!aContext.IsNull() && !aAISIO.IsNull()) {
288 // Check that the visualized shape is the same and the redisplay is not necessary
289 // Redisplay of AIS object leads to this object selection compute and the selection
290 // in the browser is lost
291 // this check is not necessary anymore because the selection store/restore is realized
292 // before and after the values modification.
293 // Moreother, this check avoids customize and redisplay presentation if the presentable
294 // parameter is changed.
295 bool isEqualShapes = false;
296 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
297 if (aResult.get() != NULL) {
298 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(aAISIO);
299 if (!aShapePrs.IsNull()) {
300 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
301 if (aShapePtr.get()) {
302 const TopoDS_Shape& aOldShape = aShapePrs->Shape();
303 if (!aOldShape.IsNull())
304 isEqualShapes = aOldShape.IsEqual(aShapePtr->impl<TopoDS_Shape>());
308 // Customization of presentation
309 bool isCustomized = customizeObject(theObject);
310 #ifdef DEBUG_FEATURE_REDISPLAY
311 qDebug(QString("Redisplay: %1, isEqualShapes=%2, isCustomized=%3").
312 arg(!isEqualShapes || isCustomized).arg(isEqualShapes).arg(isCustomized).toStdString().c_str());
314 if (!isEqualShapes || isCustomized) {
315 aContext->Redisplay(aAISIO, false);
317 #ifdef DEBUG_FEATURE_REDISPLAY
318 qDebug(" Redisplay happens");
327 void XGUI_Displayer::redisplayObjects()
329 // redisplay objects visualized in the viewer
330 static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
331 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
332 QObjectPtrList aDisplayed = myWorkshop->displayer()->displayedObjects();
333 QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
334 for (; anIt != aLast; anIt++) {
335 aECreator->sendUpdated(*anIt, EVENT_DISP);
337 Events_Loop::loop()->flush(EVENT_DISP);
340 void XGUI_Displayer::deactivate(ObjectPtr theObject, const bool theUpdateViewer)
342 #ifdef DEBUG_DEACTIVATE
343 QString anInfoStr = ModuleBase_Tools::objectInfo(theObject);
344 qDebug(QString("deactivate: myActiveSelectionModes[%1]: %2, objects = ").
345 arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
347 toStdString().c_str());
349 Handle(AIS_InteractiveContext) aContext = AISContext();
350 if (!aContext.IsNull() && isVisible(theObject)) {
351 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
352 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
354 deactivateAIS(anAIS);
355 // the selection from the previous activation modes should be cleared manually (#26172)
356 aContext->LocalContext()->ClearOutdatedSelection(anAIS, true);
362 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
363 const bool theUpdateViewer)
365 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
366 for (; anIt != aLast; anIt++) {
367 deactivate(*anIt, false);
369 //VSV It seems that there is no necessity to update viewer on deactivation
370 //if (theUpdateViewer)
374 void XGUI_Displayer::getModesOfActivation(ObjectPtr theObject, QIntList& theModes)
376 Handle(AIS_InteractiveContext) aContext = AISContext();
377 if (aContext.IsNull() || !isVisible(theObject))
380 AISObjectPtr aAISObj = getAISObject(theObject);
382 if (aAISObj.get() != NULL) {
383 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
384 TColStd_ListOfInteger aTColModes;
385 aContext->ActivatedModes(anAISIO, aTColModes);
386 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
387 for (; itr.More(); itr.Next() ) {
388 theModes.append(itr.Value());
393 void XGUI_Displayer::activateObjects(const QIntList& theModes, const QObjectPtrList& theObjList,
394 const bool theUpdateViewer)
396 // Convert shape types to selection types
398 foreach(int aType, theModes) {
399 if (aType >= TopAbs_SHAPE)
400 aModes.append(aType);
402 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum)aType));
405 #ifdef DEBUG_ACTIVATE_OBJECTS
407 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
408 for (; anIt != aLast; ++anIt) {
409 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
411 QString anInfoStr = anInfo.join(", ");
413 qDebug(QString("activateObjects: aModes[%1] = %2, myActiveSelectionModes[%3] = %4, objects = %5").
414 arg(aModes.size()).arg(qIntListInfo(aModes)).
415 arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
417 toStdString().c_str());
419 // In order to avoid doblications of selection modes
421 foreach (int aMode, aModes) {
422 if (!aNewModes.contains(aMode))
423 aNewModes.append(aMode);
425 myActiveSelectionModes = aNewModes;
426 Handle(AIS_InteractiveContext) aContext = AISContext();
427 // Open local context if there is no one
428 if (aContext.IsNull() || !aContext->HasOpenedContext())
431 //aContext->UseDisplayedObjects();
432 //myUseExternalObjects = true;
434 Handle(AIS_InteractiveObject) anAISIO;
435 AIS_ListOfInteractive aPrsList;
436 if (theObjList.isEmpty())
439 foreach(ObjectPtr aObj, theObjList) {
440 if (myResult2AISObjectMap.contains(aObj))
441 aPrsList.Append(myResult2AISObjectMap[aObj]->impl<Handle(AIS_InteractiveObject)>());
445 AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
446 bool isActivationChanged = false;
447 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
448 anAISIO = aLIt.Value();
449 if (activate(anAISIO, myActiveSelectionModes, false))
450 isActivationChanged = true;
452 // VSV It seems that there is no necessity to update viewer on activation
453 //if (theUpdateViewer && isActivationChanged)
457 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
459 Handle(AIS_InteractiveContext) aContext = AISContext();
460 if (aContext.IsNull() || !isVisible(theObject))
463 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
464 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
466 TColStd_ListOfInteger aModes;
467 aContext->ActivatedModes(anAIS, aModes);
468 return aModes.Extent() > 0;
470 void XGUI_Displayer::setSelected(const QList<ModuleBase_ViewerPrs>& theValues, bool theUpdateViewer)
472 Handle(AIS_InteractiveContext) aContext = AISContext();
473 if (aContext.IsNull())
475 if (aContext->HasOpenedContext()) {
476 aContext->UnhilightSelected();
477 aContext->ClearSelected();
478 foreach (ModuleBase_ViewerPrs aPrs, theValues) {
479 const TopoDS_Shape& aShape = aPrs.shape();
480 if (!aShape.IsNull()) {
481 aContext->AddOrRemoveSelected(aShape, false);
483 ObjectPtr anObject = aPrs.object();
484 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
485 if (aResult.get() && isVisible(aResult)) {
486 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
487 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
488 if (!anAIS.IsNull()) {
489 // The methods are replaced in order to provide multi-selection, e.g. restore selection
490 // by activating multi selector widget. It also gives an advantage that the multi
491 // selection in OB gives multi-selection in the viewer
492 //aContext->SetSelected(anAIS, false);
493 // The selection in the context was cleared, so the method sets the objects are selected
494 aContext->AddOrRemoveSelected(anAIS, false);
500 aContext->UnhilightCurrents();
501 aContext->ClearCurrents();
502 foreach (ModuleBase_ViewerPrs aPrs, theValues) {
503 ObjectPtr anObject = aPrs.object();
504 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
505 if (aResult.get() && isVisible(aResult)) {
506 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
507 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
509 aContext->SetCurrentObject(anAIS, false);
517 void XGUI_Displayer::clearSelected()
519 Handle(AIS_InteractiveContext) aContext = AISContext();
520 if (!aContext.IsNull()) {
521 aContext->UnhilightCurrents(false);
522 aContext->ClearSelected();
526 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
528 bool aErased = false;
529 Handle(AIS_InteractiveContext) aContext = AISContext();
530 if (!aContext.IsNull()) {
531 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
532 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
534 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
535 if (!anIO.IsNull()) {
536 emit beforeObjectErase(aObj, aAISObj);
537 aContext->Remove(anIO, false);
544 myResult2AISObjectMap.clear();
547 qDebug(getResult2AISObjectMapInfo().c_str());
552 void XGUI_Displayer::deactivateTrihedron() const
554 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
555 if (!aContext.IsNull()) {
556 AIS_ListOfInteractive aList;
557 aContext->DisplayedObjects(aList, true);
558 AIS_ListIteratorOfListOfInteractive aIt;
559 for (aIt.Initialize(aList); aIt.More(); aIt.Next()) {
560 Handle(AIS_Trihedron) aTrihedron = Handle(AIS_Trihedron)::DownCast(aIt.Value());
561 if (!aTrihedron.IsNull()) {
562 aContext->Deactivate(aTrihedron);
568 void XGUI_Displayer::openLocalContext()
570 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
571 // Open local context if there is no one
572 if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
573 // Preserve selected objects
574 //AIS_ListOfInteractive aAisList;
575 //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
576 // aAisList.Append(aContext->Current());
578 // get the filters from the global context and append them to the local context
579 // a list of filters in the global context is not cleared and should be cleared here
580 SelectMgr_ListOfFilter aFilters;
581 aFilters.Assign(aContext->Filters());
582 // it is important to remove the filters in the global context, because there is a code
583 // in the closeLocalContex, which restore the global context filters
584 aContext->RemoveFilters();
586 //aContext->ClearCurrents();
587 aContext->OpenLocalContext();
588 deactivateTrihedron();
589 //aContext->NotUseDisplayedObjects();
591 //myUseExternalObjects = false;
593 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
594 for (;aIt.More(); aIt.Next()) {
595 aContext->AddFilter(aIt.Value());
598 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
599 //for(; aIt2.More(); aIt2.Next()) {
600 // aContext->SetSelected(aIt2.Value(), false);
605 void XGUI_Displayer::closeLocalContexts(const bool theUpdateViewer)
607 Handle(AIS_InteractiveContext) aContext = AISContext();
608 if (!aContext.IsNull() && aContext->HasOpenedContext()) {
609 // Preserve selected objects
610 //AIS_ListOfInteractive aAisList;
611 //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
612 // aAisList.Append(aContext->SelectedInteractive());
614 // get the filters from the local context and append them to the global context
615 // a list of filters in the local context is cleared
616 SelectMgr_ListOfFilter aFilters;
617 aFilters.Assign(aContext->Filters());
619 //aContext->ClearSelected();
620 aContext->CloseAllContexts(false);
622 // From the moment when the AIS_DS_Displayed flag is used in the Display of AIS object,
623 // this code is obsolete. It is temporaty commented and should be removed after
624 // the test campaign.
625 // Redisplay all object if they were displayed in localContext
626 /*Handle(AIS_InteractiveObject) aAISIO;
627 foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
628 aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
629 if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
630 aContext->Display(aAISIO, false);
631 aContext->SetDisplayMode(aAISIO, Shading, false);
635 // Append the filters from the local selection in the global selection context
636 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
637 for (;aIt.More(); aIt.Next()) {
638 Handle(SelectMgr_Filter) aFilter = aIt.Value();
639 aContext->AddFilter(aFilter);
644 //myUseExternalObjects = false;
647 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
648 //for(; aIt2.More(); aIt2.Next()) {
649 // if (aContext->IsDisplayed(aIt2.Value()))
650 // aContext->SetCurrentObject(aIt2.Value(), false);
655 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
658 if (myResult2AISObjectMap.contains(theObject))
659 anIO = myResult2AISObjectMap[theObject];
663 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
665 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
666 return getObject(aRefAIS);
669 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
672 foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
673 AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
674 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
680 if (!anObject.get()) {
681 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
682 if (!theIO.IsNull()) {
683 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
685 anObject = myWorkshop->module()->findPresentedObject(anAISObj);
690 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
692 bool aWasEnabled = myEnableUpdateViewer;
694 myEnableUpdateViewer = isEnabled;
695 if (myNeedUpdate && myEnableUpdateViewer) {
697 myNeedUpdate = false;
702 void XGUI_Displayer::updateViewer() const
704 Handle(AIS_InteractiveContext) aContext = AISContext();
705 if (!aContext.IsNull() && myEnableUpdateViewer) {
706 myWorkshop->viewer()->Zfitall();
707 aContext->UpdateCurrentViewer();
713 void XGUI_Displayer::activateAIS(const Handle(AIS_InteractiveObject)& theIO,
714 const int theMode, const bool theUpdateViewer) const
716 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
717 if (!aContext.IsNull()) {
718 aContext->Activate(theIO, theMode, theUpdateViewer);
720 #ifdef DEBUG_ACTIVATE_AIS
721 ObjectPtr anObject = getObject(theIO);
722 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
723 qDebug(QString("activateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
728 void XGUI_Displayer::deactivateAIS(const Handle(AIS_InteractiveObject)& theIO, const int theMode) const
730 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
731 if (!aContext.IsNull()) {
733 aContext->Deactivate(theIO);
735 aContext->Deactivate(theIO, theMode);
737 #ifdef DEBUG_DEACTIVATE_AIS
738 ObjectPtr anObject = getObject(theIO);
739 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
740 qDebug(QString("deactivateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
745 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
747 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
748 if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
749 aContext->OpenLocalContext();
750 deactivateTrihedron();
751 aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
752 aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
757 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
759 Handle(AIS_InteractiveContext) aContext = AISContext();
760 if (!aContext.IsNull() && myAndFilter.IsNull()) {
761 myAndFilter = new SelectMgr_AndFilter();
762 aContext->AddFilter(myAndFilter);
767 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
768 bool theUpdateViewer)
770 bool aDisplayed = false;
771 Handle(AIS_InteractiveContext) aContext = AISContext();
772 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
773 if (!aContext.IsNull() && !anAISIO.IsNull()) {
774 aContext->Display(anAISIO, 0/*wireframe*/, 0, theUpdateViewer, true, AIS_DS_Displayed);
776 aContext->Deactivate(anAISIO);
777 aContext->Load(anAISIO);
778 if (toActivateInSelectionModes) {
779 if (aContext->HasOpenedContext()) {
780 if (myActiveSelectionModes.size() == 0)
781 activateAIS(anAISIO, 0, theUpdateViewer);
783 foreach(int aMode, myActiveSelectionModes) {
784 activateAIS(anAISIO, aMode, theUpdateViewer);
793 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
795 bool aErased = false;
796 Handle(AIS_InteractiveContext) aContext = AISContext();
797 if (!aContext.IsNull()) {
798 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
799 if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
800 aContext->Remove(anAISIO, theUpdateViewer);
808 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
810 if (theMode == NoMode)
813 Handle(AIS_InteractiveContext) aContext = AISContext();
814 if (aContext.IsNull())
817 AISObjectPtr aAISObj = getAISObject(theObject);
821 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
822 aContext->SetDisplayMode(aAISIO, theMode, false);
823 // Redisplay in order to update new mode because it could be not computed before
828 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
830 Handle(AIS_InteractiveContext) aContext = AISContext();
831 if (aContext.IsNull())
834 AISObjectPtr aAISObj = getAISObject(theObject);
838 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
839 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
842 void XGUI_Displayer::deactivateSelectionFilters()
844 Handle(AIS_InteractiveContext) aContext = AISContext();
845 if (!myAndFilter.IsNull()) {
847 if (!aContext.IsNull()) {
848 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
849 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
850 for (; anIt.More() && !aFound; anIt.Next()) {
851 Handle(SelectMgr_Filter) aFilter = anIt.Value();
852 aFound = aFilter == myAndFilter;
855 aContext->RemoveFilter(myAndFilter);
857 myAndFilter.Nullify();
861 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
863 Handle(AIS_InteractiveContext) aContext = AISContext();
864 if (aContext.IsNull() || hasSelectionFilter(theFilter))
867 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
868 if (!aCompositeFilter.IsNull()) {
869 aCompositeFilter->Add(theFilter);
870 #ifdef DEBUG_SELECTION_FILTERS
871 int aCount = aCompositeFilter->StoredFilters().Extent();
872 qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
877 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
879 Handle(AIS_InteractiveContext) aContext = AISContext();
880 if (aContext.IsNull())
883 Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
884 if (!aCompositeFilter.IsNull() && aCompositeFilter->IsIn(theFilter)) {
885 aCompositeFilter->Remove(theFilter);
886 #ifdef DEBUG_SELECTION_FILTERS
887 int aCount = aCompositeFilter->StoredFilters().Extent();
888 qDebug(QString("removeSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
893 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
895 bool aFilterFound = false;
897 Handle(AIS_InteractiveContext) aContext = AISContext();
898 if (aContext.IsNull())
900 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
901 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
902 for (; aIt.More() && !aFilterFound; aIt.Next()) {
903 if (theFilter.Access() == aIt.Value().Access())
906 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
907 if (!aCompositeFilter.IsNull()) {
908 const SelectMgr_ListOfFilter& aStoredFilters = aCompositeFilter->StoredFilters();
909 for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
910 if (theFilter.Access() == aIt.Value().Access())
917 void XGUI_Displayer::removeFilters()
919 Handle(AIS_InteractiveContext) aContext = AISContext();
920 if (aContext.IsNull())
923 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
924 if (!aCompositeFilter.IsNull())
925 aCompositeFilter->Clear();
928 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
930 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
931 foreach(ObjectPtr aObj, aDispList) {
932 if (!theList.contains(aObj))
935 foreach(ObjectPtr aObj, theList) {
936 if (!isVisible(aObj))
937 display(aObj, false);
942 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
944 if (!isVisible(theObject))
947 AISObjectPtr aAISObj = getAISObject(theObject);
948 if (aAISObj.get() == NULL)
951 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
952 return ::canBeShaded(anAIS);
955 bool XGUI_Displayer::activate(const Handle(AIS_InteractiveObject)& theIO,
956 const QIntList& theModes,
957 const bool theUpdateViewer) const
959 Handle(AIS_InteractiveContext) aContext = AISContext();
960 if (aContext.IsNull() || theIO.IsNull())
963 bool isActivationChanged = false;
964 // deactivate object in all modes, which are not in the list of activation
965 // It seems that after the IO deactivation the selected state of the IO's owners
966 // is modified in OCC(version: 6.8.0) and the selection of the object later is lost.
967 // By this reason, the number of the IO deactivate is decreased and the object is deactivated
968 // only if there is a difference in the current modes and the parameters modes.
969 // If the selection problem happens again, it is possible to write a test scenario and create
970 // a bug. The bug steps are the following:
971 // Create two IO, activate them in 5 modes, select the first IO, deactivate 3 modes for both,
972 // with clicked SHIFT select the second object. The result is the selection of the first IO is lost.
973 TColStd_ListOfInteger aTColModes;
974 aContext->ActivatedModes(theIO, aTColModes);
975 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
976 QIntList aModesActivatedForIO;
977 bool isDeactivated = false;
978 for (; itr.More(); itr.Next() ) {
979 Standard_Integer aMode = itr.Value();
980 if (!theModes.contains(aMode)) {
981 deactivateAIS(theIO, aMode);
982 isDeactivated = true;
985 aModesActivatedForIO.append(aMode);
989 // the selection from the previous activation modes should be cleared manually (#26172)
990 theIO->ClearSelected();
991 aContext->LocalContext()->ClearOutdatedSelection(theIO, true);
992 // For performance issues
993 //if (theUpdateViewer)
995 isActivationChanged = true;
998 // loading the interactive object allowing the decomposition
999 if (aTColModes.IsEmpty()) {
1000 aContext->Load(theIO, -1, true);
1003 // trihedron AIS check should be after the AIS loading.
1004 // If it is not loaded, it is steel selectable in the viewer.
1005 Handle(AIS_Trihedron) aTrihedron = Handle(AIS_Trihedron)::DownCast(theIO);
1006 if (aTrihedron.IsNull()) {
1007 //aContext->Load(anAISIO, -1, true);
1008 // In order to clear active modes list
1009 if (theModes.size() == 0) {
1010 //aContext->Load(anAISIO, 0, true);
1011 activateAIS(theIO, 0, theUpdateViewer);
1013 foreach(int aMode, theModes) {
1014 //aContext->Load(anAISIO, aMode, true);
1015 if (!aModesActivatedForIO.contains(aMode)) {
1016 activateAIS(theIO, aMode, theUpdateViewer);
1017 isActivationChanged = true;
1022 return isActivationChanged;
1025 bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
1027 AISObjectPtr anAISObj = getAISObject(theObject);
1028 // correct the result's color it it has the attribute
1029 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1031 // Customization of presentation
1032 GeomCustomPrsPtr aCustomPrs;
1033 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1034 if (aFeature.get() != NULL) {
1035 GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
1036 if (aCustPrs.get() != NULL)
1037 aCustomPrs = aCustPrs;
1039 if (aCustomPrs.get() == NULL) {
1040 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
1041 // we ignore presentable not customized objects
1042 if (aPrs.get() == NULL)
1043 aCustomPrs = myCustomPrs;
1045 bool isCustomized = aCustomPrs.get() &&
1046 aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
1047 return isCustomized;
1051 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject, const QColor& theColor, bool theUpdateViewer)
1053 if (!isVisible(theObject))
1056 AISObjectPtr anAISObj = getAISObject(theObject);
1058 anAISObj->getColor(aR, aG, aB);
1059 anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
1060 if (theUpdateViewer)
1062 return QColor(aR, aG, aB);
1065 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
1067 myResult2AISObjectMap[theObject] = theAIS;
1069 #ifdef DEBUG_DISPLAY
1070 std::ostringstream aPtrStr;
1071 aPtrStr << theObject.get();
1072 qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
1073 qDebug(getResult2AISObjectMapInfo().c_str());
1077 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
1079 QStringList aContent;
1080 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
1081 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
1082 std::ostringstream aPtrStr;
1083 aPtrStr << "aObj = " << aObj.get() << ":";
1084 aPtrStr << "anAIS = " << aAISObj.get() << ":";
1085 aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
1087 aContent.append(aPtrStr.str().c_str());
1089 return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
1090 arg(aContent.join("\n")).toStdString().c_str();