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>
46 #include <SelectMgr_SelectionManager.hxx>
48 #include <StdSelect_ViewerSelector3d.hxx>
50 #include <TColStd_MapOfTransient.hxx>
51 #include <TColStd_MapIteratorOfMapOfTransient.hxx>
53 #include <Events_Loop.h>
54 #include <ModelAPI_Events.h>
58 const int MOUSE_SENSITIVITY_IN_PIXEL = 10; ///< defines the local context mouse selection sensitivity
60 //#define DEBUG_ACTIVATE_OBJECTS
61 //#define DEBUG_DEACTIVATE
62 //#define DEBUG_ACTIVATE_AIS
63 //#define DEBUG_DEACTIVATE_AIS
65 //#define DEBUG_DISPLAY
66 //#define DEBUG_FEATURE_REDISPLAY
67 //#define DEBUG_SELECTION_FILTERS
69 //#define DEBUG_COMPOSILID_DISPLAY
70 // Workaround for bug #25637
71 void displayedObjects(const Handle(AIS_InteractiveContext)& theAIS, AIS_ListOfInteractive& theList)
73 // Get from null point
74 theAIS->DisplayedObjects(theList, true);
75 if (theAIS->HasOpenedContext()) {
76 // get from local context
77 const Handle(AIS_LocalContext)& aLC = theAIS->LocalContext();
78 TColStd_MapOfTransient aMap;
79 int NbDisp = aLC->DisplayedObjects(aMap);
80 TColStd_MapIteratorOfMapOfTransient aIt(aMap);
82 Handle(AIS_InteractiveObject) curIO;
83 Handle(Standard_Transient) Tr;
84 for(; aIt.More(); aIt.Next()){
86 curIO = *((Handle(AIS_InteractiveObject)*) &Tr);
87 theList.Append(curIO);
92 QString qIntListInfo(const QIntList& theValues, const QString& theSeparator = QString(", "))
95 QIntList::const_iterator anIt = theValues.begin(), aLast = theValues.end();
96 for (; anIt != aLast; anIt++) {
97 anInfo.append(QString::number(*anIt));
99 return anInfo.join(theSeparator);
102 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
103 : myWorkshop(theWorkshop), myEnableUpdateViewer(true), myNeedUpdate(false),
104 myIsTrihedronActive(false)
106 enableUpdateViewer(true);
107 myCustomPrs = std::shared_ptr<GeomAPI_ICustomPrs>(new XGUI_CustomPrs());
110 XGUI_Displayer::~XGUI_Displayer()
114 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
116 return myResult2AISObjectMap.contains(theObject);
119 bool XGUI_Displayer::display(ObjectPtr theObject, bool theUpdateViewer)
121 bool aDisplayed = false;
122 if (isVisible(theObject)) {
123 #ifdef DEBUG_COMPOSILID_DISPLAY
124 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
125 if (aCompsolidResult.get()) {
126 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
127 ResultPtr aSubResult = aCompsolidResult->subResult(i);
128 if (aSubResult.get())
129 redisplay(aSubResult, false);
136 aDisplayed = redisplay(theObject, theUpdateViewer);
139 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
140 bool isShading = false;
141 if (aPrs.get() != NULL) {
142 anAIS = aPrs->getAISObject(anAIS);
144 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
145 if (aResult.get() != NULL) {
146 #ifdef DEBUG_COMPOSILID_DISPLAY
147 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
148 if (aCompsolidResult.get()) {
149 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
150 ResultPtr aSubResult = aCompsolidResult->subResult(i);
151 if (aSubResult.get())
152 display(aSubResult, false);
159 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
160 if (aShapePtr.get() != NULL) {
161 anAIS = AISObjectPtr(new GeomAPI_AISObject());
162 anAIS->setImpl(new Handle(AIS_InteractiveObject)(new ModuleBase_ResultPrs(aResult)));
163 //anAIS->createShape(aShapePtr);
166 #ifdef DEBUG_COMPOSILID_DISPLAY
172 aDisplayed = display(theObject, anAIS, isShading, theUpdateViewer);
177 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS)
179 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
180 if (!aShapePrs.IsNull()) {
181 TopoDS_Shape aShape = aShapePrs->Shape();
184 TopAbs_ShapeEnum aType = aShape.ShapeType();
185 if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
188 // Check that the presentation is not a sketch
189 Handle(ModuleBase_ResultPrs) aPrs = Handle(ModuleBase_ResultPrs)::DownCast(theAIS);
191 return !aPrs->isSketchMode();
198 bool XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
199 bool isShading, bool theUpdateViewer)
201 bool aDisplayed = false;
203 Handle(AIS_InteractiveContext) aContext = AISContext();
204 if (aContext.IsNull())
207 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
208 if (!anAISIO.IsNull()) {
209 appendResultObject(theObject, theAIS);
211 bool isCustomized = customizeObject(theObject);
213 int aDispMode = isShading? Shading : Wireframe;
215 anAISIO->Attributes()->SetFaceBoundaryDraw( Standard_True );
216 anAISIO->SetDisplayMode(aDispMode);
217 aContext->Display(anAISIO, aDispMode, 0, false, true, AIS_DS_Displayed);
220 emit objectDisplayed(theObject, theAIS);
221 activate(anAISIO, myActiveSelectionModes, theUpdateViewer);
222 // the fix from VPA for more suitable selection of sketcher lines
223 if(anAISIO->Width() > 1) {
224 for(int aModeIdx = 0; aModeIdx < myActiveSelectionModes.length(); ++aModeIdx) {
225 int aMode = myActiveSelectionModes.value(aModeIdx);
226 double aPrecision = (aMode == getSelectionMode(TopAbs_VERTEX))? 20 :
227 (anAISIO->Width() + 2);
228 aContext->SetSelectionSensitivity(anAISIO, aMode, aPrecision);
238 bool XGUI_Displayer::erase(ObjectPtr theObject, const bool theUpdateViewer)
240 bool aErased = false;
241 if (!isVisible(theObject))
244 Handle(AIS_InteractiveContext) aContext = AISContext();
245 if (aContext.IsNull())
248 AISObjectPtr anObject = myResult2AISObjectMap[theObject];
250 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
251 if (!anAIS.IsNull()) {
252 emit beforeObjectErase(theObject, anObject);
253 aContext->Remove(anAIS, theUpdateViewer);
257 myResult2AISObjectMap.remove(theObject);
260 std::ostringstream aPtrStr;
261 aPtrStr << theObject.get();
262 qDebug(QString("erase object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
263 qDebug(getResult2AISObjectMapInfo().c_str());
268 bool XGUI_Displayer::redisplay(ObjectPtr theObject, bool theUpdateViewer)
270 bool aRedisplayed = false;
271 if (!isVisible(theObject))
274 AISObjectPtr aAISObj = getAISObject(theObject);
275 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
277 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
279 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
281 aRedisplayed = erase(theObject, theUpdateViewer);
284 if (aAIS_Obj != aAISObj) {
285 appendResultObject(theObject, aAIS_Obj);
287 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
290 Handle(AIS_InteractiveContext) aContext = AISContext();
291 if (!aContext.IsNull() && !aAISIO.IsNull()) {
292 // Check that the visualized shape is the same and the redisplay is not necessary
293 // Redisplay of AIS object leads to this object selection compute and the selection
294 // in the browser is lost
295 // this check is not necessary anymore because the selection store/restore is realized
296 // before and after the values modification.
297 // Moreother, this check avoids customize and redisplay presentation if the presentable
298 // parameter is changed.
299 bool isEqualShapes = false;
300 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
301 if (aResult.get() != NULL) {
302 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(aAISIO);
303 if (!aShapePrs.IsNull()) {
304 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
305 if (aShapePtr.get()) {
306 const TopoDS_Shape& aOldShape = aShapePrs->Shape();
307 if (!aOldShape.IsNull())
308 isEqualShapes = aOldShape.IsEqual(aShapePtr->impl<TopoDS_Shape>());
312 // Customization of presentation
313 bool isCustomized = customizeObject(theObject);
314 #ifdef DEBUG_FEATURE_REDISPLAY
315 qDebug(QString("Redisplay: %1, isEqualShapes=%2, isCustomized=%3").
316 arg(!isEqualShapes || isCustomized).arg(isEqualShapes).arg(isCustomized).toStdString().c_str());
318 if (!isEqualShapes || isCustomized) {
319 aContext->Redisplay(aAISIO, false);
321 #ifdef DEBUG_FEATURE_REDISPLAY
322 qDebug(" Redisplay happens");
331 void XGUI_Displayer::redisplayObjects()
333 // redisplay objects visualized in the viewer
334 static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
335 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
336 QObjectPtrList aDisplayed = myWorkshop->displayer()->displayedObjects();
337 QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
338 for (; anIt != aLast; anIt++) {
339 aECreator->sendUpdated(*anIt, EVENT_DISP);
341 Events_Loop::loop()->flush(EVENT_DISP);
344 void XGUI_Displayer::deactivate(ObjectPtr theObject, const bool theUpdateViewer)
346 #ifdef DEBUG_DEACTIVATE
347 QString anInfoStr = ModuleBase_Tools::objectInfo(theObject);
348 qDebug(QString("deactivate: myActiveSelectionModes[%1]: %2, objects = ").
349 arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
351 toStdString().c_str());
353 Handle(AIS_InteractiveContext) aContext = AISContext();
354 if (!aContext.IsNull() && isVisible(theObject)) {
355 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
356 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
358 deactivateAIS(anAIS);
359 // the selection from the previous activation modes should be cleared manually (#26172)
360 aContext->LocalContext()->ClearOutdatedSelection(anAIS, true);
366 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
367 const bool theUpdateViewer)
369 //Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
370 //if (!aTrihedron.IsNull())
371 // deactivateAIS(aTrihedron);
373 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
374 for (; anIt != aLast; anIt++) {
375 deactivate(*anIt, false);
377 //VSV It seems that there is no necessity to update viewer on deactivation
378 //if (theUpdateViewer)
382 void XGUI_Displayer::getModesOfActivation(ObjectPtr theObject, QIntList& theModes)
384 Handle(AIS_InteractiveContext) aContext = AISContext();
385 if (aContext.IsNull() || !isVisible(theObject))
388 AISObjectPtr aAISObj = getAISObject(theObject);
390 if (aAISObj.get() != NULL) {
391 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
392 TColStd_ListOfInteger aTColModes;
393 aContext->ActivatedModes(anAISIO, aTColModes);
394 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
395 for (; itr.More(); itr.Next() ) {
396 theModes.append(itr.Value());
401 int XGUI_Displayer::getSelectionMode(int theShapeType)
403 return (theShapeType >= TopAbs_SHAPE)? theShapeType :
404 AIS_Shape::SelectionMode((TopAbs_ShapeEnum)theShapeType);
407 void XGUI_Displayer::activateObjects(const QIntList& theModes, const QObjectPtrList& theObjList,
408 const bool theUpdateViewer)
410 // Convert shape types to selection types
412 foreach(int aType, theModes) {
413 aModes.append(getSelectionMode(aType));
416 #ifdef DEBUG_ACTIVATE_OBJECTS
418 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
419 for (; anIt != aLast; ++anIt) {
420 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
422 QString anInfoStr = anInfo.join(", ");
424 qDebug(QString("activateObjects: aModes[%1] = %2, myActiveSelectionModes[%3] = %4, objects = %5").
425 arg(aModes.size()).arg(qIntListInfo(aModes)).
426 arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
428 toStdString().c_str());
430 // In order to avoid doblications of selection modes
432 foreach (int aMode, aModes) {
433 if (!aNewModes.contains(aMode))
434 aNewModes.append(aMode);
436 myActiveSelectionModes = aNewModes;
437 Handle(AIS_InteractiveContext) aContext = AISContext();
438 // Open local context if there is no one
439 if (aContext.IsNull() || !aContext->HasOpenedContext())
442 //aContext->UseDisplayedObjects();
443 //myUseExternalObjects = true;
445 Handle(AIS_InteractiveObject) anAISIO;
446 AIS_ListOfInteractive aPrsList;
447 //if (aObjList.isEmpty())
450 foreach(ObjectPtr aObj, theObjList) {
451 if (myResult2AISObjectMap.contains(aObj))
452 aPrsList.Append(myResult2AISObjectMap[aObj]->impl<Handle(AIS_InteractiveObject)>());
456 // Add trihedron because it has to partisipate in selection
457 Handle(AIS_InteractiveObject) aTrihedron;
458 if (isTrihedronActive()) {
459 aTrihedron = getTrihedron();
460 if (!aTrihedron.IsNull())
461 aPrsList.Append(aTrihedron);
463 if (aPrsList.Extent() == 0)
466 AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
467 bool isActivationChanged = false;
468 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
469 anAISIO = aLIt.Value();
470 if (activate(anAISIO, myActiveSelectionModes, false))
471 isActivationChanged = true;
473 if (!aTrihedron.IsNull()) {
474 foreach(int aMode, myActiveSelectionModes)
475 aContext->SetSelectionSensitivity(aTrihedron, aMode, 20);
477 // VSV It seems that there is no necessity to update viewer on activation
478 //if (theUpdateViewer && isActivationChanged)
482 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
484 Handle(AIS_InteractiveContext) aContext = AISContext();
485 if (aContext.IsNull() || !isVisible(theObject))
488 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
489 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
491 TColStd_ListOfInteger aModes;
492 aContext->ActivatedModes(anAIS, aModes);
493 return aModes.Extent() > 0;
495 void XGUI_Displayer::setSelected(const QList<ModuleBase_ViewerPrs>& theValues, bool theUpdateViewer)
497 Handle(AIS_InteractiveContext) aContext = AISContext();
498 if (aContext.IsNull())
500 if (aContext->HasOpenedContext()) {
501 aContext->UnhilightSelected();
502 aContext->ClearSelected();
503 foreach (ModuleBase_ViewerPrs aPrs, theValues) {
504 const TopoDS_Shape& aShape = aPrs.shape();
505 if (!aShape.IsNull()) {
506 aContext->AddOrRemoveSelected(aShape, false);
508 ObjectPtr anObject = aPrs.object();
509 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
510 if (aResult.get() && isVisible(aResult)) {
511 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
512 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
513 if (!anAIS.IsNull()) {
514 // The methods are replaced in order to provide multi-selection, e.g. restore selection
515 // by activating multi selector widget. It also gives an advantage that the multi
516 // selection in OB gives multi-selection in the viewer
517 //aContext->SetSelected(anAIS, false);
518 // The selection in the context was cleared, so the method sets the objects are selected
519 aContext->AddOrRemoveSelected(anAIS, false);
525 aContext->UnhilightCurrents();
526 aContext->ClearCurrents();
527 foreach (ModuleBase_ViewerPrs aPrs, theValues) {
528 ObjectPtr anObject = aPrs.object();
529 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
530 if (aResult.get() && isVisible(aResult)) {
531 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
532 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
534 aContext->SetCurrentObject(anAIS, false);
542 void XGUI_Displayer::clearSelected()
544 Handle(AIS_InteractiveContext) aContext = AISContext();
545 if (!aContext.IsNull()) {
546 aContext->UnhilightCurrents(false);
547 aContext->ClearSelected();
551 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
553 bool aErased = false;
554 Handle(AIS_InteractiveContext) aContext = AISContext();
555 if (!aContext.IsNull()) {
556 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
557 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
559 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
560 if (!anIO.IsNull()) {
561 emit beforeObjectErase(aObj, aAISObj);
562 aContext->Remove(anIO, false);
569 myResult2AISObjectMap.clear();
572 qDebug(getResult2AISObjectMapInfo().c_str());
577 #define DEACTVATE_COMP(TheComp) \
578 if (!TheComp.IsNull()) \
579 aContext->Deactivate(TheComp);
581 void XGUI_Displayer::deactivateTrihedron() const
583 Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
584 if (!aTrihedron.IsNull()) {
585 Handle(AIS_Trihedron) aTrie = Handle(AIS_Trihedron)::DownCast(aTrihedron);
586 Handle(AIS_InteractiveContext) aContext = AISContext();
587 aContext->Deactivate(aTrie);
588 DEACTVATE_COMP(aTrie->XAxis());
589 DEACTVATE_COMP(aTrie->YAxis());
590 DEACTVATE_COMP(aTrie->Axis());
591 DEACTVATE_COMP(aTrie->Position());
592 DEACTVATE_COMP(aTrie->XYPlane());
593 DEACTVATE_COMP(aTrie->XZPlane());
594 DEACTVATE_COMP(aTrie->YZPlane());
598 Handle(AIS_InteractiveObject) XGUI_Displayer::getTrihedron() const
600 Handle(AIS_InteractiveContext) aContext = AISContext();
601 if (!aContext.IsNull()) {
602 AIS_ListOfInteractive aList;
603 aContext->DisplayedObjects(aList, true);
604 AIS_ListIteratorOfListOfInteractive aIt;
605 for (aIt.Initialize(aList); aIt.More(); aIt.Next()) {
606 Handle(AIS_Trihedron) aTrihedron = Handle(AIS_Trihedron)::DownCast(aIt.Value());
607 if (!aTrihedron.IsNull()) {
612 return Handle(AIS_InteractiveObject)();
615 void XGUI_Displayer::openLocalContext()
617 Handle(AIS_InteractiveContext) aContext = AISContext();
618 // Open local context if there is no one
619 if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
620 // Preserve selected objects
621 //AIS_ListOfInteractive aAisList;
622 //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
623 // aAisList.Append(aContext->Current());
625 // get the filters from the global context and append them to the local context
626 // a list of filters in the global context is not cleared and should be cleared here
627 SelectMgr_ListOfFilter aFilters;
628 aFilters.Assign(aContext->Filters());
629 // it is important to remove the filters in the global context, because there is a code
630 // in the closeLocalContex, which restore the global context filters
631 aContext->RemoveFilters();
633 //aContext->ClearCurrents();
634 aContext->OpenLocalContext();
635 //deactivateTrihedron();
636 //aContext->NotUseDisplayedObjects();
638 //myUseExternalObjects = false;
640 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
641 for (;aIt.More(); aIt.Next()) {
642 aContext->AddFilter(aIt.Value());
645 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
646 //for(; aIt2.More(); aIt2.Next()) {
647 // aContext->SetSelected(aIt2.Value(), false);
652 void XGUI_Displayer::closeLocalContexts(const bool theUpdateViewer)
654 Handle(AIS_InteractiveContext) aContext = AISContext();
655 if (!aContext.IsNull() && aContext->HasOpenedContext()) {
656 // Preserve selected objects
657 //AIS_ListOfInteractive aAisList;
658 //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
659 // aAisList.Append(aContext->SelectedInteractive());
661 // get the filters from the local context and append them to the global context
662 // a list of filters in the local context is cleared
663 SelectMgr_ListOfFilter aFilters;
664 aFilters.Assign(aContext->Filters());
666 //aContext->ClearSelected();
667 aContext->CloseAllContexts(false);
669 // From the moment when the AIS_DS_Displayed flag is used in the Display of AIS object,
670 // this code is obsolete. It is temporaty commented and should be removed after
671 // the test campaign.
672 // Redisplay all object if they were displayed in localContext
673 /*Handle(AIS_InteractiveObject) aAISIO;
674 foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
675 aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
676 if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
677 aContext->Display(aAISIO, false);
678 aContext->SetDisplayMode(aAISIO, Shading, false);
682 // Append the filters from the local selection in the global selection context
683 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
684 for (;aIt.More(); aIt.Next()) {
685 Handle(SelectMgr_Filter) aFilter = aIt.Value();
686 aContext->AddFilter(aFilter);
691 //myUseExternalObjects = false;
694 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
695 //for(; aIt2.More(); aIt2.Next()) {
696 // if (aContext->IsDisplayed(aIt2.Value()))
697 // aContext->SetCurrentObject(aIt2.Value(), false);
702 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
705 if (myResult2AISObjectMap.contains(theObject))
706 anIO = myResult2AISObjectMap[theObject];
710 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
712 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
713 return getObject(aRefAIS);
716 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
719 foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
720 AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
721 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
727 if (!anObject.get()) {
728 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
729 if (!theIO.IsNull()) {
730 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
732 anObject = myWorkshop->module()->findPresentedObject(anAISObj);
737 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
739 bool aWasEnabled = myEnableUpdateViewer;
741 myEnableUpdateViewer = isEnabled;
742 if (myNeedUpdate && myEnableUpdateViewer) {
744 myNeedUpdate = false;
749 void XGUI_Displayer::updateViewer() const
751 Handle(AIS_InteractiveContext) aContext = AISContext();
752 if (!aContext.IsNull() && myEnableUpdateViewer) {
753 myWorkshop->viewer()->Zfitall();
754 aContext->UpdateCurrentViewer();
760 void XGUI_Displayer::activateAIS(const Handle(AIS_InteractiveObject)& theIO,
761 const int theMode, const bool theUpdateViewer) const
763 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
764 if (!aContext.IsNull()) {
765 aContext->Activate(theIO, theMode, theUpdateViewer);
767 #ifdef DEBUG_ACTIVATE_AIS
768 ObjectPtr anObject = getObject(theIO);
769 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
770 qDebug(QString("activateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
775 void XGUI_Displayer::deactivateAIS(const Handle(AIS_InteractiveObject)& theIO, const int theMode) const
777 Handle(AIS_InteractiveContext) aContext = AISContext();
778 if (!aContext.IsNull()) {
780 aContext->Deactivate(theIO);
782 aContext->Deactivate(theIO, theMode);
784 #ifdef DEBUG_DEACTIVATE_AIS
785 ObjectPtr anObject = getObject(theIO);
786 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
787 qDebug(QString("deactivateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
792 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
794 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
795 if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
796 aContext->OpenLocalContext();
797 if (!isTrihedronActive())
798 deactivateTrihedron();
799 aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
800 aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
805 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
807 Handle(AIS_InteractiveContext) aContext = AISContext();
808 if (!aContext.IsNull() && myAndFilter.IsNull()) {
809 myAndFilter = new SelectMgr_AndFilter();
810 aContext->AddFilter(myAndFilter);
815 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
816 bool theUpdateViewer)
818 bool aDisplayed = false;
819 Handle(AIS_InteractiveContext) aContext = AISContext();
820 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
821 if (!aContext.IsNull() && !anAISIO.IsNull()) {
822 aContext->Display(anAISIO, 0/*wireframe*/, 0, theUpdateViewer, true, AIS_DS_Displayed);
824 aContext->Deactivate(anAISIO);
825 aContext->Load(anAISIO);
826 if (toActivateInSelectionModes) {
827 if (aContext->HasOpenedContext()) {
828 if (myActiveSelectionModes.size() == 0)
829 activateAIS(anAISIO, 0, theUpdateViewer);
831 foreach(int aMode, myActiveSelectionModes) {
832 activateAIS(anAISIO, aMode, theUpdateViewer);
841 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
843 bool aErased = false;
844 Handle(AIS_InteractiveContext) aContext = AISContext();
845 if (!aContext.IsNull()) {
846 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
847 if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
848 aContext->Remove(anAISIO, theUpdateViewer);
856 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
858 if (theMode == NoMode)
861 Handle(AIS_InteractiveContext) aContext = AISContext();
862 if (aContext.IsNull())
865 AISObjectPtr aAISObj = getAISObject(theObject);
869 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
870 aContext->SetDisplayMode(aAISIO, theMode, false);
871 // Redisplay in order to update new mode because it could be not computed before
876 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
878 Handle(AIS_InteractiveContext) aContext = AISContext();
879 if (aContext.IsNull())
882 AISObjectPtr aAISObj = getAISObject(theObject);
886 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
887 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
890 void XGUI_Displayer::deactivateSelectionFilters()
892 Handle(AIS_InteractiveContext) aContext = AISContext();
893 if (!myAndFilter.IsNull()) {
895 if (!aContext.IsNull()) {
896 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
897 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
898 for (; anIt.More() && !aFound; anIt.Next()) {
899 Handle(SelectMgr_Filter) aFilter = anIt.Value();
900 aFound = aFilter == myAndFilter;
903 aContext->RemoveFilter(myAndFilter);
905 myAndFilter.Nullify();
909 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
911 Handle(AIS_InteractiveContext) aContext = AISContext();
912 if (aContext.IsNull() || hasSelectionFilter(theFilter))
915 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
916 if (!aCompositeFilter.IsNull()) {
917 aCompositeFilter->Add(theFilter);
918 #ifdef DEBUG_SELECTION_FILTERS
919 int aCount = aCompositeFilter->StoredFilters().Extent();
920 qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
925 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
927 Handle(AIS_InteractiveContext) aContext = AISContext();
928 if (aContext.IsNull())
931 Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
932 if (!aCompositeFilter.IsNull() && aCompositeFilter->IsIn(theFilter)) {
933 aCompositeFilter->Remove(theFilter);
934 #ifdef DEBUG_SELECTION_FILTERS
935 int aCount = aCompositeFilter->StoredFilters().Extent();
936 qDebug(QString("removeSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
941 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
943 bool aFilterFound = false;
945 Handle(AIS_InteractiveContext) aContext = AISContext();
946 if (aContext.IsNull())
948 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
949 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
950 for (; aIt.More() && !aFilterFound; aIt.Next()) {
951 if (theFilter.Access() == aIt.Value().Access())
954 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
955 if (!aCompositeFilter.IsNull()) {
956 const SelectMgr_ListOfFilter& aStoredFilters = aCompositeFilter->StoredFilters();
957 for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
958 if (theFilter.Access() == aIt.Value().Access())
965 void XGUI_Displayer::removeFilters()
967 Handle(AIS_InteractiveContext) aContext = AISContext();
968 if (aContext.IsNull())
971 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
972 if (!aCompositeFilter.IsNull())
973 aCompositeFilter->Clear();
976 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
978 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
979 foreach(ObjectPtr aObj, aDispList) {
980 if (!theList.contains(aObj))
983 foreach(ObjectPtr aObj, theList) {
984 if (!isVisible(aObj))
985 display(aObj, false);
990 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
992 if (!isVisible(theObject))
995 AISObjectPtr aAISObj = getAISObject(theObject);
996 if (aAISObj.get() == NULL)
999 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1000 return ::canBeShaded(anAIS);
1003 bool XGUI_Displayer::activate(const Handle(AIS_InteractiveObject)& theIO,
1004 const QIntList& theModes,
1005 const bool theUpdateViewer) const
1007 Handle(AIS_InteractiveContext) aContext = AISContext();
1008 if (aContext.IsNull() || theIO.IsNull())
1011 bool isActivationChanged = false;
1012 // deactivate object in all modes, which are not in the list of activation
1013 // It seems that after the IO deactivation the selected state of the IO's owners
1014 // is modified in OCC(version: 6.8.0) and the selection of the object later is lost.
1015 // By this reason, the number of the IO deactivate is decreased and the object is deactivated
1016 // only if there is a difference in the current modes and the parameters modes.
1017 // If the selection problem happens again, it is possible to write a test scenario and create
1018 // a bug. The bug steps are the following:
1019 // Create two IO, activate them in 5 modes, select the first IO, deactivate 3 modes for both,
1020 // with clicked SHIFT select the second object. The result is the selection of the first IO is lost.
1021 TColStd_ListOfInteger aTColModes;
1022 aContext->ActivatedModes(theIO, aTColModes);
1023 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
1024 QIntList aModesActivatedForIO;
1025 bool isDeactivated = false;
1026 for (; itr.More(); itr.Next() ) {
1027 Standard_Integer aMode = itr.Value();
1028 if (!theModes.contains(aMode)) {
1029 deactivateAIS(theIO, aMode);
1030 isDeactivated = true;
1033 aModesActivatedForIO.append(aMode);
1036 if (isDeactivated) {
1037 // the selection from the previous activation modes should be cleared manually (#26172)
1038 theIO->ClearSelected();
1039 aContext->LocalContext()->ClearOutdatedSelection(theIO, true);
1040 // For performance issues
1041 //if (theUpdateViewer)
1043 isActivationChanged = true;
1046 // loading the interactive object allowing the decomposition
1047 if (aTColModes.IsEmpty()) {
1048 aContext->Load(theIO, -1, true);
1051 // trihedron AIS check should be after the AIS loading.
1052 // If it is not loaded, it is steel selectable in the viewer.
1053 Handle(AIS_Trihedron) aTrihedron;
1054 if (!isTrihedronActive())
1055 aTrihedron = Handle(AIS_Trihedron)::DownCast(theIO);
1056 if (aTrihedron.IsNull()) {
1057 // In order to clear active modes list
1058 if (theModes.size() == 0) {
1059 activateAIS(theIO, 0, theUpdateViewer);
1061 foreach(int aMode, theModes) {
1062 if (!aModesActivatedForIO.contains(aMode)) {
1063 activateAIS(theIO, aMode, theUpdateViewer);
1064 isActivationChanged = true;
1069 return isActivationChanged;
1072 bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
1074 AISObjectPtr anAISObj = getAISObject(theObject);
1075 // correct the result's color it it has the attribute
1076 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1078 // Customization of presentation
1079 GeomCustomPrsPtr aCustomPrs;
1080 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1081 if (aFeature.get() != NULL) {
1082 GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
1083 if (aCustPrs.get() != NULL)
1084 aCustomPrs = aCustPrs;
1086 if (aCustomPrs.get() == NULL) {
1087 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
1088 // we ignore presentable not customized objects
1089 if (aPrs.get() == NULL)
1090 aCustomPrs = myCustomPrs;
1092 bool isCustomized = aCustomPrs.get() &&
1093 aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
1094 return isCustomized;
1098 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject, const QColor& theColor, bool theUpdateViewer)
1100 if (!isVisible(theObject))
1103 AISObjectPtr anAISObj = getAISObject(theObject);
1105 anAISObj->getColor(aR, aG, aB);
1106 anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
1107 if (theUpdateViewer)
1109 return QColor(aR, aG, aB);
1112 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
1114 myResult2AISObjectMap[theObject] = theAIS;
1116 #ifdef DEBUG_DISPLAY
1117 std::ostringstream aPtrStr;
1118 aPtrStr << theObject.get();
1119 qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
1120 qDebug(getResult2AISObjectMapInfo().c_str());
1124 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
1126 QStringList aContent;
1127 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
1128 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
1129 std::ostringstream aPtrStr;
1130 aPtrStr << "aObj = " << aObj.get() << ":";
1131 aPtrStr << "anAIS = " << aAISObj.get() << ":";
1132 aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
1134 aContent.append(aPtrStr.str().c_str());
1136 return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
1137 arg(aContent.join("\n")).toStdString().c_str();
1140 void XGUI_Displayer::activateTrihedron(bool theIsActive)
1142 myIsTrihedronActive = theIsActive;
1143 if (!myIsTrihedronActive) {
1144 deactivateTrihedron();