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),
103 myIsTrihedronActive(false)
105 enableUpdateViewer(true);
106 myCustomPrs = std::shared_ptr<GeomAPI_ICustomPrs>(new XGUI_CustomPrs());
109 XGUI_Displayer::~XGUI_Displayer()
113 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
115 return myResult2AISObjectMap.contains(theObject);
118 bool XGUI_Displayer::display(ObjectPtr theObject, bool theUpdateViewer)
120 bool aDisplayed = false;
121 if (isVisible(theObject)) {
122 #ifdef DEBUG_COMPOSILID_DISPLAY
123 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
124 if (aCompsolidResult.get()) {
125 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
126 ResultPtr aSubResult = aCompsolidResult->subResult(i);
127 if (aSubResult.get())
128 redisplay(aSubResult, false);
135 aDisplayed = redisplay(theObject, theUpdateViewer);
138 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
139 bool isShading = false;
140 if (aPrs.get() != NULL) {
141 anAIS = aPrs->getAISObject(anAIS);
143 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
144 if (aResult.get() != NULL) {
145 #ifdef DEBUG_COMPOSILID_DISPLAY
146 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
147 if (aCompsolidResult.get()) {
148 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
149 ResultPtr aSubResult = aCompsolidResult->subResult(i);
150 if (aSubResult.get())
151 display(aSubResult, false);
158 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
159 if (aShapePtr.get() != NULL) {
160 anAIS = AISObjectPtr(new GeomAPI_AISObject());
161 anAIS->setImpl(new Handle(AIS_InteractiveObject)(new ModuleBase_ResultPrs(aResult)));
162 //anAIS->createShape(aShapePtr);
165 #ifdef DEBUG_COMPOSILID_DISPLAY
171 aDisplayed = display(theObject, anAIS, isShading, theUpdateViewer);
176 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS)
178 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
179 if (!aShapePrs.IsNull()) {
180 TopoDS_Shape aShape = aShapePrs->Shape();
183 TopAbs_ShapeEnum aType = aShape.ShapeType();
184 if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
187 // Check that the presentation is not a sketch
188 Handle(ModuleBase_ResultPrs) aPrs = Handle(ModuleBase_ResultPrs)::DownCast(theAIS);
190 return !aPrs->isSketchMode();
197 bool XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
198 bool isShading, bool theUpdateViewer)
200 bool aDisplayed = false;
202 Handle(AIS_InteractiveContext) aContext = AISContext();
203 if (aContext.IsNull())
206 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
207 if (!anAISIO.IsNull()) {
208 appendResultObject(theObject, theAIS);
210 bool isCustomized = customizeObject(theObject);
212 int aDispMode = isShading? Shading : Wireframe;
214 anAISIO->Attributes()->SetFaceBoundaryDraw( Standard_True );
215 anAISIO->SetDisplayMode(aDispMode);
216 aContext->Display(anAISIO, aDispMode, 0, false, true, AIS_DS_Displayed);
219 emit objectDisplayed(theObject, theAIS);
220 activate(anAISIO, myActiveSelectionModes, theUpdateViewer);
221 // the fix from VPA for more suitable selection of sketcher lines
222 if(anAISIO->Width() > 1) {
223 for(int aModeIdx = 0; aModeIdx < myActiveSelectionModes.length(); ++aModeIdx) {
224 int aMode = myActiveSelectionModes.value(aModeIdx);
225 double aPrecision = (aMode == getSelectionMode(TopAbs_VERTEX))? 20 :
226 (anAISIO->Width() + 2);
227 aContext->SetSelectionSensitivity(anAISIO, aMode, aPrecision);
237 bool XGUI_Displayer::erase(ObjectPtr theObject, const bool theUpdateViewer)
239 bool aErased = false;
240 if (!isVisible(theObject))
243 Handle(AIS_InteractiveContext) aContext = AISContext();
244 if (aContext.IsNull())
247 AISObjectPtr anObject = myResult2AISObjectMap[theObject];
249 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
250 if (!anAIS.IsNull()) {
251 emit beforeObjectErase(theObject, anObject);
252 aContext->Remove(anAIS, theUpdateViewer);
256 myResult2AISObjectMap.remove(theObject);
259 std::ostringstream aPtrStr;
260 aPtrStr << theObject.get();
261 qDebug(QString("erase object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
262 qDebug(getResult2AISObjectMapInfo().c_str());
267 bool XGUI_Displayer::redisplay(ObjectPtr theObject, bool theUpdateViewer)
269 bool aRedisplayed = false;
270 if (!isVisible(theObject))
273 AISObjectPtr aAISObj = getAISObject(theObject);
274 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
276 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
278 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
280 aRedisplayed = erase(theObject, theUpdateViewer);
283 if (aAIS_Obj != aAISObj) {
284 appendResultObject(theObject, aAIS_Obj);
286 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
289 Handle(AIS_InteractiveContext) aContext = AISContext();
290 if (!aContext.IsNull() && !aAISIO.IsNull()) {
291 // Check that the visualized shape is the same and the redisplay is not necessary
292 // Redisplay of AIS object leads to this object selection compute and the selection
293 // in the browser is lost
294 // this check is not necessary anymore because the selection store/restore is realized
295 // before and after the values modification.
296 // Moreother, this check avoids customize and redisplay presentation if the presentable
297 // parameter is changed.
298 bool isEqualShapes = false;
299 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
300 if (aResult.get() != NULL) {
301 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(aAISIO);
302 if (!aShapePrs.IsNull()) {
303 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
304 if (aShapePtr.get()) {
305 const TopoDS_Shape& aOldShape = aShapePrs->Shape();
306 if (!aOldShape.IsNull())
307 isEqualShapes = aOldShape.IsEqual(aShapePtr->impl<TopoDS_Shape>());
311 // Customization of presentation
312 bool isCustomized = customizeObject(theObject);
313 #ifdef DEBUG_FEATURE_REDISPLAY
314 qDebug(QString("Redisplay: %1, isEqualShapes=%2, isCustomized=%3").
315 arg(!isEqualShapes || isCustomized).arg(isEqualShapes).arg(isCustomized).toStdString().c_str());
317 if (!isEqualShapes || isCustomized) {
318 aContext->Redisplay(aAISIO, false);
320 #ifdef DEBUG_FEATURE_REDISPLAY
321 qDebug(" Redisplay happens");
330 void XGUI_Displayer::redisplayObjects()
332 // redisplay objects visualized in the viewer
333 static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
334 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
335 QObjectPtrList aDisplayed = myWorkshop->displayer()->displayedObjects();
336 QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
337 for (; anIt != aLast; anIt++) {
338 aECreator->sendUpdated(*anIt, EVENT_DISP);
340 Events_Loop::loop()->flush(EVENT_DISP);
343 void XGUI_Displayer::deactivate(ObjectPtr theObject, const bool theUpdateViewer)
345 #ifdef DEBUG_DEACTIVATE
346 QString anInfoStr = ModuleBase_Tools::objectInfo(theObject);
347 qDebug(QString("deactivate: myActiveSelectionModes[%1]: %2, objects = ").
348 arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
350 toStdString().c_str());
352 Handle(AIS_InteractiveContext) aContext = AISContext();
353 if (!aContext.IsNull() && isVisible(theObject)) {
354 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
355 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
357 deactivateAIS(anAIS);
358 // the selection from the previous activation modes should be cleared manually (#26172)
359 aContext->LocalContext()->ClearOutdatedSelection(anAIS, true);
365 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
366 const bool theUpdateViewer)
368 //Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
369 //if (!aTrihedron.IsNull())
370 // deactivateAIS(aTrihedron);
372 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
373 for (; anIt != aLast; anIt++) {
374 deactivate(*anIt, false);
376 //VSV It seems that there is no necessity to update viewer on deactivation
377 //if (theUpdateViewer)
381 void XGUI_Displayer::getModesOfActivation(ObjectPtr theObject, QIntList& theModes)
383 Handle(AIS_InteractiveContext) aContext = AISContext();
384 if (aContext.IsNull() || !isVisible(theObject))
387 AISObjectPtr aAISObj = getAISObject(theObject);
389 if (aAISObj.get() != NULL) {
390 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
391 TColStd_ListOfInteger aTColModes;
392 aContext->ActivatedModes(anAISIO, aTColModes);
393 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
394 for (; itr.More(); itr.Next() ) {
395 theModes.append(itr.Value());
400 int XGUI_Displayer::getSelectionMode(int theShapeType)
402 return (theShapeType >= TopAbs_SHAPE)? theShapeType :
403 AIS_Shape::SelectionMode((TopAbs_ShapeEnum)theShapeType);
406 void XGUI_Displayer::activateObjects(const QIntList& theModes, const QObjectPtrList& theObjList,
407 const bool theUpdateViewer)
409 // Convert shape types to selection types
411 foreach(int aType, theModes) {
412 aModes.append(getSelectionMode(aType));
415 #ifdef DEBUG_ACTIVATE_OBJECTS
417 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
418 for (; anIt != aLast; ++anIt) {
419 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
421 QString anInfoStr = anInfo.join(", ");
423 qDebug(QString("activateObjects: aModes[%1] = %2, myActiveSelectionModes[%3] = %4, objects = %5").
424 arg(aModes.size()).arg(qIntListInfo(aModes)).
425 arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
427 toStdString().c_str());
429 // In order to avoid doblications of selection modes
431 foreach (int aMode, aModes) {
432 if (!aNewModes.contains(aMode))
433 aNewModes.append(aMode);
435 myActiveSelectionModes = aNewModes;
436 Handle(AIS_InteractiveContext) aContext = AISContext();
437 // Open local context if there is no one
438 if (aContext.IsNull() || !aContext->HasOpenedContext())
441 //aContext->UseDisplayedObjects();
442 //myUseExternalObjects = true;
444 Handle(AIS_InteractiveObject) anAISIO;
445 AIS_ListOfInteractive aPrsList;
446 //if (aObjList.isEmpty())
449 foreach(ObjectPtr aObj, theObjList) {
450 if (myResult2AISObjectMap.contains(aObj))
451 aPrsList.Append(myResult2AISObjectMap[aObj]->impl<Handle(AIS_InteractiveObject)>());
455 // Add trihedron because it has to partisipate in selection
456 Handle(AIS_InteractiveObject) aTrihedron;
457 if (isTrihedronActive()) {
458 aTrihedron = getTrihedron();
459 if (!aTrihedron.IsNull())
460 aPrsList.Append(aTrihedron);
462 if (aPrsList.Extent() == 0)
465 AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
466 bool isActivationChanged = false;
467 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
468 anAISIO = aLIt.Value();
469 if (activate(anAISIO, myActiveSelectionModes, false))
470 isActivationChanged = true;
472 if (!aTrihedron.IsNull()) {
473 foreach(int aMode, myActiveSelectionModes)
474 aContext->SetSelectionSensitivity(aTrihedron, aMode, 15);
476 // VSV It seems that there is no necessity to update viewer on activation
477 //if (theUpdateViewer && isActivationChanged)
481 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
483 Handle(AIS_InteractiveContext) aContext = AISContext();
484 if (aContext.IsNull() || !isVisible(theObject))
487 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
488 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
490 TColStd_ListOfInteger aModes;
491 aContext->ActivatedModes(anAIS, aModes);
492 return aModes.Extent() > 0;
494 void XGUI_Displayer::setSelected(const QList<ModuleBase_ViewerPrs>& theValues, bool theUpdateViewer)
496 Handle(AIS_InteractiveContext) aContext = AISContext();
497 if (aContext.IsNull())
499 if (aContext->HasOpenedContext()) {
500 aContext->UnhilightSelected();
501 aContext->ClearSelected();
502 foreach (ModuleBase_ViewerPrs aPrs, theValues) {
503 const TopoDS_Shape& aShape = aPrs.shape();
504 if (!aShape.IsNull()) {
505 aContext->AddOrRemoveSelected(aShape, false);
507 ObjectPtr anObject = aPrs.object();
508 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
509 if (aResult.get() && isVisible(aResult)) {
510 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
511 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
512 if (!anAIS.IsNull()) {
513 // The methods are replaced in order to provide multi-selection, e.g. restore selection
514 // by activating multi selector widget. It also gives an advantage that the multi
515 // selection in OB gives multi-selection in the viewer
516 //aContext->SetSelected(anAIS, false);
517 // The selection in the context was cleared, so the method sets the objects are selected
518 aContext->AddOrRemoveSelected(anAIS, false);
524 aContext->UnhilightCurrents();
525 aContext->ClearCurrents();
526 foreach (ModuleBase_ViewerPrs aPrs, theValues) {
527 ObjectPtr anObject = aPrs.object();
528 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
529 if (aResult.get() && isVisible(aResult)) {
530 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
531 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
533 aContext->SetCurrentObject(anAIS, false);
541 void XGUI_Displayer::clearSelected()
543 Handle(AIS_InteractiveContext) aContext = AISContext();
544 if (!aContext.IsNull()) {
545 aContext->UnhilightCurrents(false);
546 aContext->ClearSelected();
550 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
552 bool aErased = false;
553 Handle(AIS_InteractiveContext) aContext = AISContext();
554 if (!aContext.IsNull()) {
555 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
556 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
558 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
559 if (!anIO.IsNull()) {
560 emit beforeObjectErase(aObj, aAISObj);
561 aContext->Remove(anIO, false);
568 myResult2AISObjectMap.clear();
571 qDebug(getResult2AISObjectMapInfo().c_str());
576 void XGUI_Displayer::deactivateTrihedron() const
578 Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
579 if (!aTrihedron.IsNull()) {
580 deactivateAIS(aTrihedron);
584 Handle(AIS_InteractiveObject) XGUI_Displayer::getTrihedron() const
586 Handle(AIS_InteractiveContext) aContext = AISContext();
587 if (!aContext.IsNull()) {
588 AIS_ListOfInteractive aList;
589 aContext->DisplayedObjects(aList, true);
590 AIS_ListIteratorOfListOfInteractive aIt;
591 for (aIt.Initialize(aList); aIt.More(); aIt.Next()) {
592 Handle(AIS_Trihedron) aTrihedron = Handle(AIS_Trihedron)::DownCast(aIt.Value());
593 if (!aTrihedron.IsNull()) {
598 return Handle(AIS_InteractiveObject)();
601 void XGUI_Displayer::openLocalContext()
603 Handle(AIS_InteractiveContext) aContext = AISContext();
604 // Open local context if there is no one
605 if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
606 // Preserve selected objects
607 //AIS_ListOfInteractive aAisList;
608 //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
609 // aAisList.Append(aContext->Current());
611 // get the filters from the global context and append them to the local context
612 // a list of filters in the global context is not cleared and should be cleared here
613 SelectMgr_ListOfFilter aFilters;
614 aFilters.Assign(aContext->Filters());
615 // it is important to remove the filters in the global context, because there is a code
616 // in the closeLocalContex, which restore the global context filters
617 aContext->RemoveFilters();
619 //aContext->ClearCurrents();
620 aContext->OpenLocalContext();
621 //deactivateTrihedron();
622 //aContext->NotUseDisplayedObjects();
624 //myUseExternalObjects = false;
626 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
627 for (;aIt.More(); aIt.Next()) {
628 aContext->AddFilter(aIt.Value());
631 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
632 //for(; aIt2.More(); aIt2.Next()) {
633 // aContext->SetSelected(aIt2.Value(), false);
638 void XGUI_Displayer::closeLocalContexts(const bool theUpdateViewer)
640 Handle(AIS_InteractiveContext) aContext = AISContext();
641 if (!aContext.IsNull() && aContext->HasOpenedContext()) {
642 // Preserve selected objects
643 //AIS_ListOfInteractive aAisList;
644 //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
645 // aAisList.Append(aContext->SelectedInteractive());
647 // get the filters from the local context and append them to the global context
648 // a list of filters in the local context is cleared
649 SelectMgr_ListOfFilter aFilters;
650 aFilters.Assign(aContext->Filters());
652 //aContext->ClearSelected();
653 aContext->CloseAllContexts(false);
655 // From the moment when the AIS_DS_Displayed flag is used in the Display of AIS object,
656 // this code is obsolete. It is temporaty commented and should be removed after
657 // the test campaign.
658 // Redisplay all object if they were displayed in localContext
659 /*Handle(AIS_InteractiveObject) aAISIO;
660 foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
661 aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
662 if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
663 aContext->Display(aAISIO, false);
664 aContext->SetDisplayMode(aAISIO, Shading, false);
668 // Append the filters from the local selection in the global selection context
669 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
670 for (;aIt.More(); aIt.Next()) {
671 Handle(SelectMgr_Filter) aFilter = aIt.Value();
672 aContext->AddFilter(aFilter);
677 //myUseExternalObjects = false;
680 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
681 //for(; aIt2.More(); aIt2.Next()) {
682 // if (aContext->IsDisplayed(aIt2.Value()))
683 // aContext->SetCurrentObject(aIt2.Value(), false);
688 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
691 if (myResult2AISObjectMap.contains(theObject))
692 anIO = myResult2AISObjectMap[theObject];
696 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
698 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
699 return getObject(aRefAIS);
702 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
705 foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
706 AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
707 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
713 if (!anObject.get()) {
714 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
715 if (!theIO.IsNull()) {
716 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
718 anObject = myWorkshop->module()->findPresentedObject(anAISObj);
723 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
725 bool aWasEnabled = myEnableUpdateViewer;
727 myEnableUpdateViewer = isEnabled;
728 if (myNeedUpdate && myEnableUpdateViewer) {
730 myNeedUpdate = false;
735 void XGUI_Displayer::updateViewer() const
737 Handle(AIS_InteractiveContext) aContext = AISContext();
738 if (!aContext.IsNull() && myEnableUpdateViewer) {
739 myWorkshop->viewer()->Zfitall();
740 aContext->UpdateCurrentViewer();
746 void XGUI_Displayer::activateAIS(const Handle(AIS_InteractiveObject)& theIO,
747 const int theMode, const bool theUpdateViewer) const
749 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
750 if (!aContext.IsNull()) {
751 aContext->Activate(theIO, theMode, theUpdateViewer);
753 #ifdef DEBUG_ACTIVATE_AIS
754 ObjectPtr anObject = getObject(theIO);
755 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
756 qDebug(QString("activateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
761 void XGUI_Displayer::deactivateAIS(const Handle(AIS_InteractiveObject)& theIO, const int theMode) const
763 Handle(AIS_InteractiveContext) aContext = AISContext();
764 if (!aContext.IsNull()) {
766 aContext->Deactivate(theIO);
768 aContext->Deactivate(theIO, theMode);
770 #ifdef DEBUG_DEACTIVATE_AIS
771 ObjectPtr anObject = getObject(theIO);
772 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
773 qDebug(QString("deactivateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
778 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
780 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
781 if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
782 aContext->OpenLocalContext();
783 if (!isTrihedronActive())
784 deactivateTrihedron();
785 aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
786 aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
791 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
793 Handle(AIS_InteractiveContext) aContext = AISContext();
794 if (!aContext.IsNull() && myAndFilter.IsNull()) {
795 myAndFilter = new SelectMgr_AndFilter();
796 aContext->AddFilter(myAndFilter);
801 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
802 bool theUpdateViewer)
804 bool aDisplayed = false;
805 Handle(AIS_InteractiveContext) aContext = AISContext();
806 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
807 if (!aContext.IsNull() && !anAISIO.IsNull()) {
808 aContext->Display(anAISIO, 0/*wireframe*/, 0, theUpdateViewer, true, AIS_DS_Displayed);
810 aContext->Deactivate(anAISIO);
811 aContext->Load(anAISIO);
812 if (toActivateInSelectionModes) {
813 if (aContext->HasOpenedContext()) {
814 if (myActiveSelectionModes.size() == 0)
815 activateAIS(anAISIO, 0, theUpdateViewer);
817 foreach(int aMode, myActiveSelectionModes) {
818 activateAIS(anAISIO, aMode, theUpdateViewer);
827 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
829 bool aErased = false;
830 Handle(AIS_InteractiveContext) aContext = AISContext();
831 if (!aContext.IsNull()) {
832 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
833 if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
834 aContext->Remove(anAISIO, theUpdateViewer);
842 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
844 if (theMode == NoMode)
847 Handle(AIS_InteractiveContext) aContext = AISContext();
848 if (aContext.IsNull())
851 AISObjectPtr aAISObj = getAISObject(theObject);
855 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
856 aContext->SetDisplayMode(aAISIO, theMode, false);
857 // Redisplay in order to update new mode because it could be not computed before
862 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
864 Handle(AIS_InteractiveContext) aContext = AISContext();
865 if (aContext.IsNull())
868 AISObjectPtr aAISObj = getAISObject(theObject);
872 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
873 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
876 void XGUI_Displayer::deactivateSelectionFilters()
878 Handle(AIS_InteractiveContext) aContext = AISContext();
879 if (!myAndFilter.IsNull()) {
881 if (!aContext.IsNull()) {
882 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
883 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
884 for (; anIt.More() && !aFound; anIt.Next()) {
885 Handle(SelectMgr_Filter) aFilter = anIt.Value();
886 aFound = aFilter == myAndFilter;
889 aContext->RemoveFilter(myAndFilter);
891 myAndFilter.Nullify();
895 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
897 Handle(AIS_InteractiveContext) aContext = AISContext();
898 if (aContext.IsNull() || hasSelectionFilter(theFilter))
901 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
902 if (!aCompositeFilter.IsNull()) {
903 aCompositeFilter->Add(theFilter);
904 #ifdef DEBUG_SELECTION_FILTERS
905 int aCount = aCompositeFilter->StoredFilters().Extent();
906 qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
911 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
913 Handle(AIS_InteractiveContext) aContext = AISContext();
914 if (aContext.IsNull())
917 Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
918 if (!aCompositeFilter.IsNull() && aCompositeFilter->IsIn(theFilter)) {
919 aCompositeFilter->Remove(theFilter);
920 #ifdef DEBUG_SELECTION_FILTERS
921 int aCount = aCompositeFilter->StoredFilters().Extent();
922 qDebug(QString("removeSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
927 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
929 bool aFilterFound = false;
931 Handle(AIS_InteractiveContext) aContext = AISContext();
932 if (aContext.IsNull())
934 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
935 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
936 for (; aIt.More() && !aFilterFound; aIt.Next()) {
937 if (theFilter.Access() == aIt.Value().Access())
940 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
941 if (!aCompositeFilter.IsNull()) {
942 const SelectMgr_ListOfFilter& aStoredFilters = aCompositeFilter->StoredFilters();
943 for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
944 if (theFilter.Access() == aIt.Value().Access())
951 void XGUI_Displayer::removeFilters()
953 Handle(AIS_InteractiveContext) aContext = AISContext();
954 if (aContext.IsNull())
957 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
958 if (!aCompositeFilter.IsNull())
959 aCompositeFilter->Clear();
962 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
964 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
965 foreach(ObjectPtr aObj, aDispList) {
966 if (!theList.contains(aObj))
969 foreach(ObjectPtr aObj, theList) {
970 if (!isVisible(aObj))
971 display(aObj, false);
976 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
978 if (!isVisible(theObject))
981 AISObjectPtr aAISObj = getAISObject(theObject);
982 if (aAISObj.get() == NULL)
985 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
986 return ::canBeShaded(anAIS);
989 bool XGUI_Displayer::activate(const Handle(AIS_InteractiveObject)& theIO,
990 const QIntList& theModes,
991 const bool theUpdateViewer) const
993 Handle(AIS_InteractiveContext) aContext = AISContext();
994 if (aContext.IsNull() || theIO.IsNull())
997 bool isActivationChanged = false;
998 // deactivate object in all modes, which are not in the list of activation
999 // It seems that after the IO deactivation the selected state of the IO's owners
1000 // is modified in OCC(version: 6.8.0) and the selection of the object later is lost.
1001 // By this reason, the number of the IO deactivate is decreased and the object is deactivated
1002 // only if there is a difference in the current modes and the parameters modes.
1003 // If the selection problem happens again, it is possible to write a test scenario and create
1004 // a bug. The bug steps are the following:
1005 // Create two IO, activate them in 5 modes, select the first IO, deactivate 3 modes for both,
1006 // with clicked SHIFT select the second object. The result is the selection of the first IO is lost.
1007 TColStd_ListOfInteger aTColModes;
1008 aContext->ActivatedModes(theIO, aTColModes);
1009 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
1010 QIntList aModesActivatedForIO;
1011 bool isDeactivated = false;
1012 for (; itr.More(); itr.Next() ) {
1013 Standard_Integer aMode = itr.Value();
1014 if (!theModes.contains(aMode)) {
1015 deactivateAIS(theIO, aMode);
1016 isDeactivated = true;
1019 aModesActivatedForIO.append(aMode);
1022 if (isDeactivated) {
1023 // the selection from the previous activation modes should be cleared manually (#26172)
1024 theIO->ClearSelected();
1025 aContext->LocalContext()->ClearOutdatedSelection(theIO, true);
1026 // For performance issues
1027 //if (theUpdateViewer)
1029 isActivationChanged = true;
1032 // loading the interactive object allowing the decomposition
1033 if (aTColModes.IsEmpty()) {
1034 aContext->Load(theIO, -1, true);
1037 // trihedron AIS check should be after the AIS loading.
1038 // If it is not loaded, it is steel selectable in the viewer.
1039 Handle(AIS_Trihedron) aTrihedron;
1040 if (!isTrihedronActive())
1041 aTrihedron = Handle(AIS_Trihedron)::DownCast(theIO);
1042 if (aTrihedron.IsNull()) {
1043 // In order to clear active modes list
1044 if (theModes.size() == 0) {
1045 activateAIS(theIO, 0, theUpdateViewer);
1047 foreach(int aMode, theModes) {
1048 if (!aModesActivatedForIO.contains(aMode)) {
1049 activateAIS(theIO, aMode, theUpdateViewer);
1050 isActivationChanged = true;
1055 return isActivationChanged;
1058 bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
1060 AISObjectPtr anAISObj = getAISObject(theObject);
1061 // correct the result's color it it has the attribute
1062 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1064 // Customization of presentation
1065 GeomCustomPrsPtr aCustomPrs;
1066 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1067 if (aFeature.get() != NULL) {
1068 GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
1069 if (aCustPrs.get() != NULL)
1070 aCustomPrs = aCustPrs;
1072 if (aCustomPrs.get() == NULL) {
1073 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
1074 // we ignore presentable not customized objects
1075 if (aPrs.get() == NULL)
1076 aCustomPrs = myCustomPrs;
1078 bool isCustomized = aCustomPrs.get() &&
1079 aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
1080 return isCustomized;
1084 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject, const QColor& theColor, bool theUpdateViewer)
1086 if (!isVisible(theObject))
1089 AISObjectPtr anAISObj = getAISObject(theObject);
1091 anAISObj->getColor(aR, aG, aB);
1092 anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
1093 if (theUpdateViewer)
1095 return QColor(aR, aG, aB);
1098 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
1100 myResult2AISObjectMap[theObject] = theAIS;
1102 #ifdef DEBUG_DISPLAY
1103 std::ostringstream aPtrStr;
1104 aPtrStr << theObject.get();
1105 qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
1106 qDebug(getResult2AISObjectMapInfo().c_str());
1110 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
1112 QStringList aContent;
1113 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
1114 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
1115 std::ostringstream aPtrStr;
1116 aPtrStr << "aObj = " << aObj.get() << ":";
1117 aPtrStr << "anAIS = " << aAISObj.get() << ":";
1118 aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
1120 aContent.append(aPtrStr.str().c_str());
1122 return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
1123 arg(aContent.join("\n")).toStdString().c_str();
1126 void XGUI_Displayer::activateTrihedron(bool theIsActive)
1128 myIsTrihedronActive = theIsActive;
1129 if (!myIsTrihedronActive)
1130 deactivateTrihedron();