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 int aMode = myActiveSelectionModes.value(aModeIdx);
224 double aPrecision = (aMode == getSelectionMode(TopAbs_VERTEX))? 15 :
225 (anAISIO->Width() + 2);
226 aContext->SetSelectionSensitivity(anAISIO, aMode, aPrecision);
236 bool XGUI_Displayer::erase(ObjectPtr theObject, const bool theUpdateViewer)
238 bool aErased = false;
239 if (!isVisible(theObject))
242 Handle(AIS_InteractiveContext) aContext = AISContext();
243 if (aContext.IsNull())
246 AISObjectPtr anObject = myResult2AISObjectMap[theObject];
248 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
249 if (!anAIS.IsNull()) {
250 emit beforeObjectErase(theObject, anObject);
251 aContext->Remove(anAIS, theUpdateViewer);
255 myResult2AISObjectMap.remove(theObject);
258 std::ostringstream aPtrStr;
259 aPtrStr << theObject.get();
260 qDebug(QString("erase object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
261 qDebug(getResult2AISObjectMapInfo().c_str());
266 bool XGUI_Displayer::redisplay(ObjectPtr theObject, bool theUpdateViewer)
268 bool aRedisplayed = false;
269 if (!isVisible(theObject))
272 AISObjectPtr aAISObj = getAISObject(theObject);
273 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
275 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
277 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
279 aRedisplayed = erase(theObject, theUpdateViewer);
282 if (aAIS_Obj != aAISObj) {
283 appendResultObject(theObject, aAIS_Obj);
285 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
288 Handle(AIS_InteractiveContext) aContext = AISContext();
289 if (!aContext.IsNull() && !aAISIO.IsNull()) {
290 // Check that the visualized shape is the same and the redisplay is not necessary
291 // Redisplay of AIS object leads to this object selection compute and the selection
292 // in the browser is lost
293 // this check is not necessary anymore because the selection store/restore is realized
294 // before and after the values modification.
295 // Moreother, this check avoids customize and redisplay presentation if the presentable
296 // parameter is changed.
297 bool isEqualShapes = false;
298 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
299 if (aResult.get() != NULL) {
300 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(aAISIO);
301 if (!aShapePrs.IsNull()) {
302 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
303 if (aShapePtr.get()) {
304 const TopoDS_Shape& aOldShape = aShapePrs->Shape();
305 if (!aOldShape.IsNull())
306 isEqualShapes = aOldShape.IsEqual(aShapePtr->impl<TopoDS_Shape>());
310 // Customization of presentation
311 bool isCustomized = customizeObject(theObject);
312 #ifdef DEBUG_FEATURE_REDISPLAY
313 qDebug(QString("Redisplay: %1, isEqualShapes=%2, isCustomized=%3").
314 arg(!isEqualShapes || isCustomized).arg(isEqualShapes).arg(isCustomized).toStdString().c_str());
316 if (!isEqualShapes || isCustomized) {
317 aContext->Redisplay(aAISIO, false);
319 #ifdef DEBUG_FEATURE_REDISPLAY
320 qDebug(" Redisplay happens");
329 void XGUI_Displayer::redisplayObjects()
331 // redisplay objects visualized in the viewer
332 static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
333 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
334 QObjectPtrList aDisplayed = myWorkshop->displayer()->displayedObjects();
335 QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
336 for (; anIt != aLast; anIt++) {
337 aECreator->sendUpdated(*anIt, EVENT_DISP);
339 Events_Loop::loop()->flush(EVENT_DISP);
342 void XGUI_Displayer::deactivate(ObjectPtr theObject, const bool theUpdateViewer)
344 #ifdef DEBUG_DEACTIVATE
345 QString anInfoStr = ModuleBase_Tools::objectInfo(theObject);
346 qDebug(QString("deactivate: myActiveSelectionModes[%1]: %2, objects = ").
347 arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
349 toStdString().c_str());
351 Handle(AIS_InteractiveContext) aContext = AISContext();
352 if (!aContext.IsNull() && isVisible(theObject)) {
353 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
354 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
356 deactivateAIS(anAIS);
357 // the selection from the previous activation modes should be cleared manually (#26172)
358 aContext->LocalContext()->ClearOutdatedSelection(anAIS, true);
364 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
365 const bool theUpdateViewer)
367 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
368 for (; anIt != aLast; anIt++) {
369 deactivate(*anIt, false);
371 //VSV It seems that there is no necessity to update viewer on deactivation
372 //if (theUpdateViewer)
376 void XGUI_Displayer::getModesOfActivation(ObjectPtr theObject, QIntList& theModes)
378 Handle(AIS_InteractiveContext) aContext = AISContext();
379 if (aContext.IsNull() || !isVisible(theObject))
382 AISObjectPtr aAISObj = getAISObject(theObject);
384 if (aAISObj.get() != NULL) {
385 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
386 TColStd_ListOfInteger aTColModes;
387 aContext->ActivatedModes(anAISIO, aTColModes);
388 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
389 for (; itr.More(); itr.Next() ) {
390 theModes.append(itr.Value());
395 int XGUI_Displayer::getSelectionMode(int theShapeType)
397 return (theShapeType >= TopAbs_SHAPE)? theShapeType :
398 AIS_Shape::SelectionMode((TopAbs_ShapeEnum)theShapeType);
401 void XGUI_Displayer::activateObjects(const QIntList& theModes, const QObjectPtrList& theObjList,
402 const bool theUpdateViewer)
404 // Convert shape types to selection types
406 foreach(int aType, theModes) {
407 aModes.append(getSelectionMode(aType));
410 #ifdef DEBUG_ACTIVATE_OBJECTS
412 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
413 for (; anIt != aLast; ++anIt) {
414 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
416 QString anInfoStr = anInfo.join(", ");
418 qDebug(QString("activateObjects: aModes[%1] = %2, myActiveSelectionModes[%3] = %4, objects = %5").
419 arg(aModes.size()).arg(qIntListInfo(aModes)).
420 arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
422 toStdString().c_str());
424 // In order to avoid doblications of selection modes
426 foreach (int aMode, aModes) {
427 if (!aNewModes.contains(aMode))
428 aNewModes.append(aMode);
430 myActiveSelectionModes = aNewModes;
431 Handle(AIS_InteractiveContext) aContext = AISContext();
432 // Open local context if there is no one
433 if (aContext.IsNull() || !aContext->HasOpenedContext())
436 //aContext->UseDisplayedObjects();
437 //myUseExternalObjects = true;
439 Handle(AIS_InteractiveObject) anAISIO;
440 AIS_ListOfInteractive aPrsList;
441 if (theObjList.isEmpty())
444 foreach(ObjectPtr aObj, theObjList) {
445 if (myResult2AISObjectMap.contains(aObj))
446 aPrsList.Append(myResult2AISObjectMap[aObj]->impl<Handle(AIS_InteractiveObject)>());
450 AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
451 bool isActivationChanged = false;
452 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
453 anAISIO = aLIt.Value();
454 if (activate(anAISIO, myActiveSelectionModes, false))
455 isActivationChanged = true;
457 // VSV It seems that there is no necessity to update viewer on activation
458 //if (theUpdateViewer && isActivationChanged)
462 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
464 Handle(AIS_InteractiveContext) aContext = AISContext();
465 if (aContext.IsNull() || !isVisible(theObject))
468 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
469 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
471 TColStd_ListOfInteger aModes;
472 aContext->ActivatedModes(anAIS, aModes);
473 return aModes.Extent() > 0;
475 void XGUI_Displayer::setSelected(const QList<ModuleBase_ViewerPrs>& theValues, bool theUpdateViewer)
477 Handle(AIS_InteractiveContext) aContext = AISContext();
478 if (aContext.IsNull())
480 if (aContext->HasOpenedContext()) {
481 aContext->UnhilightSelected();
482 aContext->ClearSelected();
483 foreach (ModuleBase_ViewerPrs aPrs, theValues) {
484 const TopoDS_Shape& aShape = aPrs.shape();
485 if (!aShape.IsNull()) {
486 aContext->AddOrRemoveSelected(aShape, false);
488 ObjectPtr anObject = aPrs.object();
489 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
490 if (aResult.get() && isVisible(aResult)) {
491 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
492 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
493 if (!anAIS.IsNull()) {
494 // The methods are replaced in order to provide multi-selection, e.g. restore selection
495 // by activating multi selector widget. It also gives an advantage that the multi
496 // selection in OB gives multi-selection in the viewer
497 //aContext->SetSelected(anAIS, false);
498 // The selection in the context was cleared, so the method sets the objects are selected
499 aContext->AddOrRemoveSelected(anAIS, false);
505 aContext->UnhilightCurrents();
506 aContext->ClearCurrents();
507 foreach (ModuleBase_ViewerPrs aPrs, theValues) {
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)>();
514 aContext->SetCurrentObject(anAIS, false);
522 void XGUI_Displayer::clearSelected()
524 Handle(AIS_InteractiveContext) aContext = AISContext();
525 if (!aContext.IsNull()) {
526 aContext->UnhilightCurrents(false);
527 aContext->ClearSelected();
531 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
533 bool aErased = false;
534 Handle(AIS_InteractiveContext) aContext = AISContext();
535 if (!aContext.IsNull()) {
536 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
537 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
539 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
540 if (!anIO.IsNull()) {
541 emit beforeObjectErase(aObj, aAISObj);
542 aContext->Remove(anIO, false);
549 myResult2AISObjectMap.clear();
552 qDebug(getResult2AISObjectMapInfo().c_str());
557 void XGUI_Displayer::deactivateTrihedron() const
559 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
560 if (!aContext.IsNull()) {
561 AIS_ListOfInteractive aList;
562 aContext->DisplayedObjects(aList, true);
563 AIS_ListIteratorOfListOfInteractive aIt;
564 for (aIt.Initialize(aList); aIt.More(); aIt.Next()) {
565 Handle(AIS_Trihedron) aTrihedron = Handle(AIS_Trihedron)::DownCast(aIt.Value());
566 if (!aTrihedron.IsNull()) {
567 aContext->Deactivate(aTrihedron);
573 void XGUI_Displayer::openLocalContext()
575 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
576 // Open local context if there is no one
577 if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
578 // Preserve selected objects
579 //AIS_ListOfInteractive aAisList;
580 //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
581 // aAisList.Append(aContext->Current());
583 // get the filters from the global context and append them to the local context
584 // a list of filters in the global context is not cleared and should be cleared here
585 SelectMgr_ListOfFilter aFilters;
586 aFilters.Assign(aContext->Filters());
587 // it is important to remove the filters in the global context, because there is a code
588 // in the closeLocalContex, which restore the global context filters
589 aContext->RemoveFilters();
591 //aContext->ClearCurrents();
592 aContext->OpenLocalContext();
593 deactivateTrihedron();
594 //aContext->NotUseDisplayedObjects();
596 //myUseExternalObjects = false;
598 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
599 for (;aIt.More(); aIt.Next()) {
600 aContext->AddFilter(aIt.Value());
603 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
604 //for(; aIt2.More(); aIt2.Next()) {
605 // aContext->SetSelected(aIt2.Value(), false);
610 void XGUI_Displayer::closeLocalContexts(const bool theUpdateViewer)
612 Handle(AIS_InteractiveContext) aContext = AISContext();
613 if (!aContext.IsNull() && aContext->HasOpenedContext()) {
614 // Preserve selected objects
615 //AIS_ListOfInteractive aAisList;
616 //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
617 // aAisList.Append(aContext->SelectedInteractive());
619 // get the filters from the local context and append them to the global context
620 // a list of filters in the local context is cleared
621 SelectMgr_ListOfFilter aFilters;
622 aFilters.Assign(aContext->Filters());
624 //aContext->ClearSelected();
625 aContext->CloseAllContexts(false);
627 // From the moment when the AIS_DS_Displayed flag is used in the Display of AIS object,
628 // this code is obsolete. It is temporaty commented and should be removed after
629 // the test campaign.
630 // Redisplay all object if they were displayed in localContext
631 /*Handle(AIS_InteractiveObject) aAISIO;
632 foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
633 aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
634 if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
635 aContext->Display(aAISIO, false);
636 aContext->SetDisplayMode(aAISIO, Shading, false);
640 // Append the filters from the local selection in the global selection context
641 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
642 for (;aIt.More(); aIt.Next()) {
643 Handle(SelectMgr_Filter) aFilter = aIt.Value();
644 aContext->AddFilter(aFilter);
649 //myUseExternalObjects = false;
652 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
653 //for(; aIt2.More(); aIt2.Next()) {
654 // if (aContext->IsDisplayed(aIt2.Value()))
655 // aContext->SetCurrentObject(aIt2.Value(), false);
660 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
663 if (myResult2AISObjectMap.contains(theObject))
664 anIO = myResult2AISObjectMap[theObject];
668 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
670 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
671 return getObject(aRefAIS);
674 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
677 foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
678 AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
679 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
685 if (!anObject.get()) {
686 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
687 if (!theIO.IsNull()) {
688 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
690 anObject = myWorkshop->module()->findPresentedObject(anAISObj);
695 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
697 bool aWasEnabled = myEnableUpdateViewer;
699 myEnableUpdateViewer = isEnabled;
700 if (myNeedUpdate && myEnableUpdateViewer) {
702 myNeedUpdate = false;
707 void XGUI_Displayer::updateViewer() const
709 Handle(AIS_InteractiveContext) aContext = AISContext();
710 if (!aContext.IsNull() && myEnableUpdateViewer) {
711 myWorkshop->viewer()->Zfitall();
712 aContext->UpdateCurrentViewer();
718 void XGUI_Displayer::activateAIS(const Handle(AIS_InteractiveObject)& theIO,
719 const int theMode, const bool theUpdateViewer) const
721 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
722 if (!aContext.IsNull()) {
723 aContext->Activate(theIO, theMode, theUpdateViewer);
725 #ifdef DEBUG_ACTIVATE_AIS
726 ObjectPtr anObject = getObject(theIO);
727 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
728 qDebug(QString("activateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
733 void XGUI_Displayer::deactivateAIS(const Handle(AIS_InteractiveObject)& theIO, const int theMode) const
735 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
736 if (!aContext.IsNull()) {
738 aContext->Deactivate(theIO);
740 aContext->Deactivate(theIO, theMode);
742 #ifdef DEBUG_DEACTIVATE_AIS
743 ObjectPtr anObject = getObject(theIO);
744 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
745 qDebug(QString("deactivateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
750 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
752 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
753 if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
754 aContext->OpenLocalContext();
755 deactivateTrihedron();
756 aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
757 aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
762 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
764 Handle(AIS_InteractiveContext) aContext = AISContext();
765 if (!aContext.IsNull() && myAndFilter.IsNull()) {
766 myAndFilter = new SelectMgr_AndFilter();
767 aContext->AddFilter(myAndFilter);
772 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
773 bool theUpdateViewer)
775 bool aDisplayed = false;
776 Handle(AIS_InteractiveContext) aContext = AISContext();
777 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
778 if (!aContext.IsNull() && !anAISIO.IsNull()) {
779 aContext->Display(anAISIO, 0/*wireframe*/, 0, theUpdateViewer, true, AIS_DS_Displayed);
781 aContext->Deactivate(anAISIO);
782 aContext->Load(anAISIO);
783 if (toActivateInSelectionModes) {
784 if (aContext->HasOpenedContext()) {
785 if (myActiveSelectionModes.size() == 0)
786 activateAIS(anAISIO, 0, theUpdateViewer);
788 foreach(int aMode, myActiveSelectionModes) {
789 activateAIS(anAISIO, aMode, theUpdateViewer);
798 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
800 bool aErased = false;
801 Handle(AIS_InteractiveContext) aContext = AISContext();
802 if (!aContext.IsNull()) {
803 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
804 if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
805 aContext->Remove(anAISIO, theUpdateViewer);
813 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
815 if (theMode == NoMode)
818 Handle(AIS_InteractiveContext) aContext = AISContext();
819 if (aContext.IsNull())
822 AISObjectPtr aAISObj = getAISObject(theObject);
826 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
827 aContext->SetDisplayMode(aAISIO, theMode, false);
828 // Redisplay in order to update new mode because it could be not computed before
833 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
835 Handle(AIS_InteractiveContext) aContext = AISContext();
836 if (aContext.IsNull())
839 AISObjectPtr aAISObj = getAISObject(theObject);
843 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
844 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
847 void XGUI_Displayer::deactivateSelectionFilters()
849 Handle(AIS_InteractiveContext) aContext = AISContext();
850 if (!myAndFilter.IsNull()) {
852 if (!aContext.IsNull()) {
853 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
854 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
855 for (; anIt.More() && !aFound; anIt.Next()) {
856 Handle(SelectMgr_Filter) aFilter = anIt.Value();
857 aFound = aFilter == myAndFilter;
860 aContext->RemoveFilter(myAndFilter);
862 myAndFilter.Nullify();
866 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
868 Handle(AIS_InteractiveContext) aContext = AISContext();
869 if (aContext.IsNull() || hasSelectionFilter(theFilter))
872 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
873 if (!aCompositeFilter.IsNull()) {
874 aCompositeFilter->Add(theFilter);
875 #ifdef DEBUG_SELECTION_FILTERS
876 int aCount = aCompositeFilter->StoredFilters().Extent();
877 qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
882 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
884 Handle(AIS_InteractiveContext) aContext = AISContext();
885 if (aContext.IsNull())
888 Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
889 if (!aCompositeFilter.IsNull() && aCompositeFilter->IsIn(theFilter)) {
890 aCompositeFilter->Remove(theFilter);
891 #ifdef DEBUG_SELECTION_FILTERS
892 int aCount = aCompositeFilter->StoredFilters().Extent();
893 qDebug(QString("removeSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
898 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
900 bool aFilterFound = false;
902 Handle(AIS_InteractiveContext) aContext = AISContext();
903 if (aContext.IsNull())
905 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
906 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
907 for (; aIt.More() && !aFilterFound; aIt.Next()) {
908 if (theFilter.Access() == aIt.Value().Access())
911 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
912 if (!aCompositeFilter.IsNull()) {
913 const SelectMgr_ListOfFilter& aStoredFilters = aCompositeFilter->StoredFilters();
914 for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
915 if (theFilter.Access() == aIt.Value().Access())
922 void XGUI_Displayer::removeFilters()
924 Handle(AIS_InteractiveContext) aContext = AISContext();
925 if (aContext.IsNull())
928 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
929 if (!aCompositeFilter.IsNull())
930 aCompositeFilter->Clear();
933 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
935 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
936 foreach(ObjectPtr aObj, aDispList) {
937 if (!theList.contains(aObj))
940 foreach(ObjectPtr aObj, theList) {
941 if (!isVisible(aObj))
942 display(aObj, false);
947 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
949 if (!isVisible(theObject))
952 AISObjectPtr aAISObj = getAISObject(theObject);
953 if (aAISObj.get() == NULL)
956 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
957 return ::canBeShaded(anAIS);
960 bool XGUI_Displayer::activate(const Handle(AIS_InteractiveObject)& theIO,
961 const QIntList& theModes,
962 const bool theUpdateViewer) const
964 Handle(AIS_InteractiveContext) aContext = AISContext();
965 if (aContext.IsNull() || theIO.IsNull())
968 bool isActivationChanged = false;
969 // deactivate object in all modes, which are not in the list of activation
970 // It seems that after the IO deactivation the selected state of the IO's owners
971 // is modified in OCC(version: 6.8.0) and the selection of the object later is lost.
972 // By this reason, the number of the IO deactivate is decreased and the object is deactivated
973 // only if there is a difference in the current modes and the parameters modes.
974 // If the selection problem happens again, it is possible to write a test scenario and create
975 // a bug. The bug steps are the following:
976 // Create two IO, activate them in 5 modes, select the first IO, deactivate 3 modes for both,
977 // with clicked SHIFT select the second object. The result is the selection of the first IO is lost.
978 TColStd_ListOfInteger aTColModes;
979 aContext->ActivatedModes(theIO, aTColModes);
980 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
981 QIntList aModesActivatedForIO;
982 bool isDeactivated = false;
983 for (; itr.More(); itr.Next() ) {
984 Standard_Integer aMode = itr.Value();
985 if (!theModes.contains(aMode)) {
986 deactivateAIS(theIO, aMode);
987 isDeactivated = true;
990 aModesActivatedForIO.append(aMode);
994 // the selection from the previous activation modes should be cleared manually (#26172)
995 theIO->ClearSelected();
996 aContext->LocalContext()->ClearOutdatedSelection(theIO, true);
997 // For performance issues
998 //if (theUpdateViewer)
1000 isActivationChanged = true;
1003 // loading the interactive object allowing the decomposition
1004 if (aTColModes.IsEmpty()) {
1005 aContext->Load(theIO, -1, true);
1008 // trihedron AIS check should be after the AIS loading.
1009 // If it is not loaded, it is steel selectable in the viewer.
1010 Handle(AIS_Trihedron) aTrihedron = Handle(AIS_Trihedron)::DownCast(theIO);
1011 if (aTrihedron.IsNull()) {
1012 //aContext->Load(anAISIO, -1, true);
1013 // In order to clear active modes list
1014 if (theModes.size() == 0) {
1015 //aContext->Load(anAISIO, 0, true);
1016 activateAIS(theIO, 0, theUpdateViewer);
1018 foreach(int aMode, theModes) {
1019 //aContext->Load(anAISIO, aMode, true);
1020 if (!aModesActivatedForIO.contains(aMode)) {
1021 activateAIS(theIO, aMode, theUpdateViewer);
1022 isActivationChanged = true;
1027 return isActivationChanged;
1030 bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
1032 AISObjectPtr anAISObj = getAISObject(theObject);
1033 // correct the result's color it it has the attribute
1034 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1036 // Customization of presentation
1037 GeomCustomPrsPtr aCustomPrs;
1038 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1039 if (aFeature.get() != NULL) {
1040 GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
1041 if (aCustPrs.get() != NULL)
1042 aCustomPrs = aCustPrs;
1044 if (aCustomPrs.get() == NULL) {
1045 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
1046 // we ignore presentable not customized objects
1047 if (aPrs.get() == NULL)
1048 aCustomPrs = myCustomPrs;
1050 bool isCustomized = aCustomPrs.get() &&
1051 aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
1052 return isCustomized;
1056 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject, const QColor& theColor, bool theUpdateViewer)
1058 if (!isVisible(theObject))
1061 AISObjectPtr anAISObj = getAISObject(theObject);
1063 anAISObj->getColor(aR, aG, aB);
1064 anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
1065 if (theUpdateViewer)
1067 return QColor(aR, aG, aB);
1070 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
1072 myResult2AISObjectMap[theObject] = theAIS;
1074 #ifdef DEBUG_DISPLAY
1075 std::ostringstream aPtrStr;
1076 aPtrStr << theObject.get();
1077 qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
1078 qDebug(getResult2AISObjectMapInfo().c_str());
1082 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
1084 QStringList aContent;
1085 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
1086 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
1087 std::ostringstream aPtrStr;
1088 aPtrStr << "aObj = " << aObj.get() << ":";
1089 aPtrStr << "anAIS = " << aAISObj.get() << ":";
1090 aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
1092 aContent.append(aPtrStr.str().c_str());
1094 return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
1095 arg(aContent.join("\n")).toStdString().c_str();