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 WORKAROUND_691
62 //#define DEBUG_ACTIVATE_OBJECTS
63 //#define DEBUG_DEACTIVATE
64 //#define DEBUG_ACTIVATE_AIS
65 //#define DEBUG_DEACTIVATE_AIS
67 //#define DEBUG_DISPLAY
68 //#define DEBUG_FEATURE_REDISPLAY
69 //#define DEBUG_SELECTION_FILTERS
71 //#define DEBUG_COMPOSILID_DISPLAY
72 // Workaround for bug #25637
73 void displayedObjects(const Handle(AIS_InteractiveContext)& theAIS, AIS_ListOfInteractive& theList)
75 // Get from null point
76 theAIS->DisplayedObjects(theList, true);
77 if (theAIS->HasOpenedContext()) {
78 // get from local context
79 const Handle(AIS_LocalContext)& aLC = theAIS->LocalContext();
80 TColStd_MapOfTransient aMap;
81 int NbDisp = aLC->DisplayedObjects(aMap);
82 TColStd_MapIteratorOfMapOfTransient aIt(aMap);
84 Handle(AIS_InteractiveObject) curIO;
85 Handle(Standard_Transient) Tr;
86 for(; aIt.More(); aIt.Next()){
88 curIO = *((Handle(AIS_InteractiveObject)*) &Tr);
89 theList.Append(curIO);
94 QString qIntListInfo(const QIntList& theValues, const QString& theSeparator = QString(", "))
97 QIntList::const_iterator anIt = theValues.begin(), aLast = theValues.end();
98 for (; anIt != aLast; anIt++) {
99 anInfo.append(QString::number(*anIt));
101 return anInfo.join(theSeparator);
104 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
105 : myWorkshop(theWorkshop), myEnableUpdateViewer(true), myNeedUpdate(false),
106 myIsTrihedronActive(false)
108 enableUpdateViewer(true);
109 myCustomPrs = std::shared_ptr<GeomAPI_ICustomPrs>(new XGUI_CustomPrs());
112 XGUI_Displayer::~XGUI_Displayer()
116 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
118 return myResult2AISObjectMap.contains(theObject);
121 bool XGUI_Displayer::display(ObjectPtr theObject, bool theUpdateViewer)
123 bool aDisplayed = false;
124 if (isVisible(theObject)) {
125 #ifdef DEBUG_COMPOSILID_DISPLAY
126 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
127 if (aCompsolidResult.get()) {
128 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
129 ResultPtr aSubResult = aCompsolidResult->subResult(i);
130 if (aSubResult.get())
131 redisplay(aSubResult, false);
138 aDisplayed = redisplay(theObject, theUpdateViewer);
141 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
142 bool isShading = false;
143 if (aPrs.get() != NULL) {
144 anAIS = aPrs->getAISObject(anAIS);
146 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
147 if (aResult.get() != NULL) {
148 #ifdef DEBUG_COMPOSILID_DISPLAY
149 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
150 if (aCompsolidResult.get()) {
151 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
152 ResultPtr aSubResult = aCompsolidResult->subResult(i);
153 if (aSubResult.get())
154 display(aSubResult, false);
161 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
162 if (aShapePtr.get() != NULL) {
163 anAIS = AISObjectPtr(new GeomAPI_AISObject());
164 anAIS->setImpl(new Handle(AIS_InteractiveObject)(new ModuleBase_ResultPrs(aResult)));
165 //anAIS->createShape(aShapePtr);
168 #ifdef DEBUG_COMPOSILID_DISPLAY
174 aDisplayed = display(theObject, anAIS, isShading, theUpdateViewer);
179 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS)
181 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
182 if (!aShapePrs.IsNull()) {
183 TopoDS_Shape aShape = aShapePrs->Shape();
186 TopAbs_ShapeEnum aType = aShape.ShapeType();
187 if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
190 // Check that the presentation is not a sketch
191 Handle(ModuleBase_ResultPrs) aPrs = Handle(ModuleBase_ResultPrs)::DownCast(theAIS);
193 return !aPrs->isSketchMode();
200 bool XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
201 bool isShading, bool theUpdateViewer)
203 bool aDisplayed = false;
205 Handle(AIS_InteractiveContext) aContext = AISContext();
206 if (aContext.IsNull())
209 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
210 if (!anAISIO.IsNull()) {
211 appendResultObject(theObject, theAIS);
213 bool isCustomized = customizeObject(theObject);
215 int aDispMode = isShading? Shading : Wireframe;
217 anAISIO->Attributes()->SetFaceBoundaryDraw( Standard_True );
218 anAISIO->SetDisplayMode(aDispMode);
219 aContext->Display(anAISIO, aDispMode, 0, false, true, AIS_DS_Displayed);
222 emit objectDisplayed(theObject, theAIS);
223 activate(anAISIO, myActiveSelectionModes, theUpdateViewer);
224 // the fix from VPA for more suitable selection of sketcher lines
225 if(anAISIO->Width() > 1) {
226 for(int aModeIdx = 0; aModeIdx < myActiveSelectionModes.length(); ++aModeIdx) {
227 int aMode = myActiveSelectionModes.value(aModeIdx);
228 double aPrecision = (aMode == getSelectionMode(TopAbs_VERTEX))? 20 :
229 (anAISIO->Width() + 2);
230 aContext->SetSelectionSensitivity(anAISIO, aMode, aPrecision);
240 bool XGUI_Displayer::erase(ObjectPtr theObject, const bool theUpdateViewer)
242 bool aErased = false;
243 if (!isVisible(theObject))
246 Handle(AIS_InteractiveContext) aContext = AISContext();
247 if (aContext.IsNull())
250 AISObjectPtr anObject = myResult2AISObjectMap[theObject];
252 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
253 if (!anAIS.IsNull()) {
254 emit beforeObjectErase(theObject, anObject);
255 aContext->Remove(anAIS, theUpdateViewer);
259 myResult2AISObjectMap.remove(theObject);
262 std::ostringstream aPtrStr;
263 aPtrStr << theObject.get();
264 qDebug(QString("erase object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
265 qDebug(getResult2AISObjectMapInfo().c_str());
270 bool XGUI_Displayer::redisplay(ObjectPtr theObject, bool theUpdateViewer)
272 bool aRedisplayed = false;
273 if (!isVisible(theObject))
276 AISObjectPtr aAISObj = getAISObject(theObject);
277 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
279 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
281 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
283 aRedisplayed = erase(theObject, theUpdateViewer);
286 if (aAIS_Obj != aAISObj) {
287 appendResultObject(theObject, aAIS_Obj);
289 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
292 Handle(AIS_InteractiveContext) aContext = AISContext();
293 if (!aContext.IsNull() && !aAISIO.IsNull()) {
294 // Check that the visualized shape is the same and the redisplay is not necessary
295 // Redisplay of AIS object leads to this object selection compute and the selection
296 // in the browser is lost
297 // this check is not necessary anymore because the selection store/restore is realized
298 // before and after the values modification.
299 // Moreother, this check avoids customize and redisplay presentation if the presentable
300 // parameter is changed.
301 bool isEqualShapes = false;
302 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
303 if (aResult.get() != NULL) {
304 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(aAISIO);
305 if (!aShapePrs.IsNull()) {
306 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
307 if (aShapePtr.get()) {
308 const TopoDS_Shape& aOldShape = aShapePrs->Shape();
309 if (!aOldShape.IsNull())
310 isEqualShapes = aOldShape.IsEqual(aShapePtr->impl<TopoDS_Shape>());
314 // Customization of presentation
315 bool isCustomized = customizeObject(theObject);
316 #ifdef DEBUG_FEATURE_REDISPLAY
317 qDebug(QString("Redisplay: %1, isEqualShapes=%2, isCustomized=%3").
318 arg(!isEqualShapes || isCustomized).arg(isEqualShapes).arg(isCustomized).toStdString().c_str());
320 if (!isEqualShapes || isCustomized) {
321 #ifdef WORKAROUND_691
322 // this is a workaround for OCCT 6.9.1, selection area is wrong
323 // on sketch and 3D features
324 aContext->RecomputePrsOnly (aAISIO, false);
325 aContext->SelectionManager()->RecomputeSelection (aAISIO, true);
326 aContext->LocalContext()->ClearOutdatedSelection (aAISIO, false);
328 aContext->Redisplay(aAISIO, false);
331 #ifdef DEBUG_FEATURE_REDISPLAY
332 qDebug(" Redisplay happens");
341 void XGUI_Displayer::redisplayObjects()
343 // redisplay objects visualized in the viewer
344 static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
345 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
346 QObjectPtrList aDisplayed = myWorkshop->displayer()->displayedObjects();
347 QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
348 for (; anIt != aLast; anIt++) {
349 aECreator->sendUpdated(*anIt, EVENT_DISP);
351 Events_Loop::loop()->flush(EVENT_DISP);
354 void XGUI_Displayer::deactivate(ObjectPtr theObject, const bool theUpdateViewer)
356 #ifdef DEBUG_DEACTIVATE
357 QString anInfoStr = ModuleBase_Tools::objectInfo(theObject);
358 qDebug(QString("deactivate: myActiveSelectionModes[%1]: %2, objects = ").
359 arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
361 toStdString().c_str());
363 Handle(AIS_InteractiveContext) aContext = AISContext();
364 if (!aContext.IsNull() && isVisible(theObject)) {
365 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
366 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
368 deactivateAIS(anAIS);
369 // the selection from the previous activation modes should be cleared manually (#26172)
370 aContext->LocalContext()->ClearOutdatedSelection(anAIS, true);
376 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
377 const bool theUpdateViewer)
379 //Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
380 //if (!aTrihedron.IsNull())
381 // deactivateAIS(aTrihedron);
383 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
384 for (; anIt != aLast; anIt++) {
385 deactivate(*anIt, false);
387 //VSV It seems that there is no necessity to update viewer on deactivation
388 //if (theUpdateViewer)
392 void XGUI_Displayer::getModesOfActivation(ObjectPtr theObject, QIntList& theModes)
394 Handle(AIS_InteractiveContext) aContext = AISContext();
395 if (aContext.IsNull() || !isVisible(theObject))
398 AISObjectPtr aAISObj = getAISObject(theObject);
400 if (aAISObj.get() != NULL) {
401 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
402 TColStd_ListOfInteger aTColModes;
403 aContext->ActivatedModes(anAISIO, aTColModes);
404 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
405 for (; itr.More(); itr.Next() ) {
406 theModes.append(itr.Value());
411 int XGUI_Displayer::getSelectionMode(int theShapeType)
413 return (theShapeType >= TopAbs_SHAPE)? theShapeType :
414 AIS_Shape::SelectionMode((TopAbs_ShapeEnum)theShapeType);
417 void XGUI_Displayer::activateObjects(const QIntList& theModes, const QObjectPtrList& theObjList,
418 const bool theUpdateViewer)
420 // Convert shape types to selection types
422 foreach(int aType, theModes) {
423 aModes.append(getSelectionMode(aType));
426 #ifdef DEBUG_ACTIVATE_OBJECTS
428 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
429 for (; anIt != aLast; ++anIt) {
430 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
432 QString anInfoStr = anInfo.join(", ");
434 qDebug(QString("activateObjects: aModes[%1] = %2, myActiveSelectionModes[%3] = %4, objects = %5").
435 arg(aModes.size()).arg(qIntListInfo(aModes)).
436 arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
438 toStdString().c_str());
440 // In order to avoid doblications of selection modes
442 foreach (int aMode, aModes) {
443 if (!aNewModes.contains(aMode))
444 aNewModes.append(aMode);
446 myActiveSelectionModes = aNewModes;
447 Handle(AIS_InteractiveContext) aContext = AISContext();
448 // Open local context if there is no one
449 if (aContext.IsNull() || !aContext->HasOpenedContext())
452 //aContext->UseDisplayedObjects();
453 //myUseExternalObjects = true;
455 Handle(AIS_InteractiveObject) anAISIO;
456 AIS_ListOfInteractive aPrsList;
457 //if (aObjList.isEmpty())
460 foreach(ObjectPtr aObj, theObjList) {
461 if (myResult2AISObjectMap.contains(aObj))
462 aPrsList.Append(myResult2AISObjectMap[aObj]->impl<Handle(AIS_InteractiveObject)>());
466 // Add trihedron because it has to partisipate in selection
467 Handle(AIS_InteractiveObject) aTrihedron;
468 if (isTrihedronActive()) {
469 aTrihedron = getTrihedron();
470 if (!aTrihedron.IsNull())
471 aPrsList.Append(aTrihedron);
473 if (aPrsList.Extent() == 0)
476 AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
477 bool isActivationChanged = false;
478 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
479 anAISIO = aLIt.Value();
480 if (activate(anAISIO, myActiveSelectionModes, false))
481 isActivationChanged = true;
483 if (!aTrihedron.IsNull()) {
484 foreach(int aMode, myActiveSelectionModes)
485 aContext->SetSelectionSensitivity(aTrihedron, aMode, 20);
487 // VSV It seems that there is no necessity to update viewer on activation
488 //if (theUpdateViewer && isActivationChanged)
492 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
494 Handle(AIS_InteractiveContext) aContext = AISContext();
495 if (aContext.IsNull() || !isVisible(theObject))
498 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
499 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
501 TColStd_ListOfInteger aModes;
502 aContext->ActivatedModes(anAIS, aModes);
503 return aModes.Extent() > 0;
505 void XGUI_Displayer::setSelected(const QList<ModuleBase_ViewerPrs>& theValues, bool theUpdateViewer)
507 Handle(AIS_InteractiveContext) aContext = AISContext();
508 if (aContext.IsNull())
510 if (aContext->HasOpenedContext()) {
511 aContext->UnhilightSelected();
512 aContext->ClearSelected();
513 foreach (ModuleBase_ViewerPrs aPrs, theValues) {
514 const TopoDS_Shape& aShape = aPrs.shape();
515 if (!aShape.IsNull()) {
516 aContext->AddOrRemoveSelected(aShape, false);
518 ObjectPtr anObject = aPrs.object();
519 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
520 if (aResult.get() && isVisible(aResult)) {
521 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
522 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
523 if (!anAIS.IsNull()) {
524 // The methods are replaced in order to provide multi-selection, e.g. restore selection
525 // by activating multi selector widget. It also gives an advantage that the multi
526 // selection in OB gives multi-selection in the viewer
527 //aContext->SetSelected(anAIS, false);
528 // The selection in the context was cleared, so the method sets the objects are selected
529 aContext->AddOrRemoveSelected(anAIS, false);
535 aContext->UnhilightCurrents();
536 aContext->ClearCurrents();
537 foreach (ModuleBase_ViewerPrs aPrs, theValues) {
538 ObjectPtr anObject = aPrs.object();
539 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
540 if (aResult.get() && isVisible(aResult)) {
541 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
542 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
544 aContext->SetCurrentObject(anAIS, false);
552 void XGUI_Displayer::clearSelected()
554 Handle(AIS_InteractiveContext) aContext = AISContext();
555 if (!aContext.IsNull()) {
556 aContext->UnhilightCurrents(false);
557 aContext->ClearSelected();
561 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
563 bool aErased = false;
564 Handle(AIS_InteractiveContext) aContext = AISContext();
565 if (!aContext.IsNull()) {
566 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
567 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
569 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
570 if (!anIO.IsNull()) {
571 emit beforeObjectErase(aObj, aAISObj);
572 aContext->Remove(anIO, false);
579 myResult2AISObjectMap.clear();
582 qDebug(getResult2AISObjectMapInfo().c_str());
587 #define DEACTVATE_COMP(TheComp) \
588 if (!TheComp.IsNull()) \
589 aContext->Deactivate(TheComp);
591 void XGUI_Displayer::deactivateTrihedron() const
593 Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
594 if (!aTrihedron.IsNull()) {
595 Handle(AIS_Trihedron) aTrie = Handle(AIS_Trihedron)::DownCast(aTrihedron);
596 Handle(AIS_InteractiveContext) aContext = AISContext();
597 aContext->Deactivate(aTrie);
598 DEACTVATE_COMP(aTrie->XAxis());
599 DEACTVATE_COMP(aTrie->YAxis());
600 DEACTVATE_COMP(aTrie->Axis());
601 DEACTVATE_COMP(aTrie->Position());
602 DEACTVATE_COMP(aTrie->XYPlane());
603 DEACTVATE_COMP(aTrie->XZPlane());
604 DEACTVATE_COMP(aTrie->YZPlane());
608 Handle(AIS_InteractiveObject) XGUI_Displayer::getTrihedron() const
610 Handle(AIS_InteractiveContext) aContext = AISContext();
611 if (!aContext.IsNull()) {
612 AIS_ListOfInteractive aList;
613 aContext->DisplayedObjects(aList, true);
614 AIS_ListIteratorOfListOfInteractive aIt;
615 for (aIt.Initialize(aList); aIt.More(); aIt.Next()) {
616 Handle(AIS_Trihedron) aTrihedron = Handle(AIS_Trihedron)::DownCast(aIt.Value());
617 if (!aTrihedron.IsNull()) {
622 return Handle(AIS_InteractiveObject)();
625 void XGUI_Displayer::openLocalContext()
627 Handle(AIS_InteractiveContext) aContext = AISContext();
628 // Open local context if there is no one
629 if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
630 // Preserve selected objects
631 //AIS_ListOfInteractive aAisList;
632 //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
633 // aAisList.Append(aContext->Current());
635 // get the filters from the global context and append them to the local context
636 // a list of filters in the global context is not cleared and should be cleared here
637 SelectMgr_ListOfFilter aFilters;
638 aFilters.Assign(aContext->Filters());
639 // it is important to remove the filters in the global context, because there is a code
640 // in the closeLocalContex, which restore the global context filters
641 aContext->RemoveFilters();
643 //aContext->ClearCurrents();
644 aContext->OpenLocalContext();
645 //deactivateTrihedron();
646 //aContext->NotUseDisplayedObjects();
648 //myUseExternalObjects = false;
650 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
651 for (;aIt.More(); aIt.Next()) {
652 aContext->AddFilter(aIt.Value());
655 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
656 //for(; aIt2.More(); aIt2.Next()) {
657 // aContext->SetSelected(aIt2.Value(), false);
662 void XGUI_Displayer::closeLocalContexts(const bool theUpdateViewer)
664 Handle(AIS_InteractiveContext) aContext = AISContext();
665 if (!aContext.IsNull() && aContext->HasOpenedContext()) {
666 // Preserve selected objects
667 //AIS_ListOfInteractive aAisList;
668 //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
669 // aAisList.Append(aContext->SelectedInteractive());
671 // get the filters from the local context and append them to the global context
672 // a list of filters in the local context is cleared
673 SelectMgr_ListOfFilter aFilters;
674 aFilters.Assign(aContext->Filters());
676 //aContext->ClearSelected();
677 aContext->CloseAllContexts(false);
679 // From the moment when the AIS_DS_Displayed flag is used in the Display of AIS object,
680 // this code is obsolete. It is temporaty commented and should be removed after
681 // the test campaign.
682 // Redisplay all object if they were displayed in localContext
683 /*Handle(AIS_InteractiveObject) aAISIO;
684 foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
685 aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
686 if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
687 aContext->Display(aAISIO, false);
688 aContext->SetDisplayMode(aAISIO, Shading, false);
692 // Append the filters from the local selection in the global selection context
693 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
694 for (;aIt.More(); aIt.Next()) {
695 Handle(SelectMgr_Filter) aFilter = aIt.Value();
696 aContext->AddFilter(aFilter);
701 //myUseExternalObjects = false;
704 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
705 //for(; aIt2.More(); aIt2.Next()) {
706 // if (aContext->IsDisplayed(aIt2.Value()))
707 // aContext->SetCurrentObject(aIt2.Value(), false);
712 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
715 if (myResult2AISObjectMap.contains(theObject))
716 anIO = myResult2AISObjectMap[theObject];
720 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
722 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
723 return getObject(aRefAIS);
726 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
729 foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
730 AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
731 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
737 if (!anObject.get()) {
738 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
739 if (!theIO.IsNull()) {
740 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
742 anObject = myWorkshop->module()->findPresentedObject(anAISObj);
747 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
749 bool aWasEnabled = myEnableUpdateViewer;
751 myEnableUpdateViewer = isEnabled;
752 if (myNeedUpdate && myEnableUpdateViewer) {
754 myNeedUpdate = false;
759 void XGUI_Displayer::updateViewer() const
761 Handle(AIS_InteractiveContext) aContext = AISContext();
762 if (!aContext.IsNull() && myEnableUpdateViewer) {
763 myWorkshop->viewer()->Zfitall();
764 aContext->UpdateCurrentViewer();
770 void XGUI_Displayer::activateAIS(const Handle(AIS_InteractiveObject)& theIO,
771 const int theMode, const bool theUpdateViewer) const
773 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
774 if (!aContext.IsNull()) {
775 aContext->Activate(theIO, theMode, theUpdateViewer);
777 #ifdef DEBUG_ACTIVATE_AIS
778 ObjectPtr anObject = getObject(theIO);
779 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
780 qDebug(QString("activateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
785 void XGUI_Displayer::deactivateAIS(const Handle(AIS_InteractiveObject)& theIO, const int theMode) const
787 Handle(AIS_InteractiveContext) aContext = AISContext();
788 if (!aContext.IsNull()) {
790 aContext->Deactivate(theIO);
792 aContext->Deactivate(theIO, theMode);
794 #ifdef DEBUG_DEACTIVATE_AIS
795 ObjectPtr anObject = getObject(theIO);
796 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
797 qDebug(QString("deactivateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
802 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
804 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
805 if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
806 aContext->OpenLocalContext();
807 if (!isTrihedronActive())
808 deactivateTrihedron();
809 aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
810 aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
815 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
817 Handle(AIS_InteractiveContext) aContext = AISContext();
818 if (!aContext.IsNull() && myAndFilter.IsNull()) {
819 myAndFilter = new SelectMgr_AndFilter();
820 aContext->AddFilter(myAndFilter);
825 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
826 bool theUpdateViewer)
828 bool aDisplayed = false;
829 Handle(AIS_InteractiveContext) aContext = AISContext();
830 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
831 if (!aContext.IsNull() && !anAISIO.IsNull()) {
832 aContext->Display(anAISIO, 0/*wireframe*/, 0, theUpdateViewer, true, AIS_DS_Displayed);
834 aContext->Deactivate(anAISIO);
835 aContext->Load(anAISIO);
836 if (toActivateInSelectionModes) {
837 if (aContext->HasOpenedContext()) {
838 if (myActiveSelectionModes.size() == 0)
839 activateAIS(anAISIO, 0, theUpdateViewer);
841 foreach(int aMode, myActiveSelectionModes) {
842 activateAIS(anAISIO, aMode, theUpdateViewer);
851 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
853 bool aErased = false;
854 Handle(AIS_InteractiveContext) aContext = AISContext();
855 if (!aContext.IsNull()) {
856 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
857 if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
858 aContext->Remove(anAISIO, theUpdateViewer);
866 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
868 if (theMode == NoMode)
871 Handle(AIS_InteractiveContext) aContext = AISContext();
872 if (aContext.IsNull())
875 AISObjectPtr aAISObj = getAISObject(theObject);
879 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
880 aContext->SetDisplayMode(aAISIO, theMode, false);
881 // Redisplay in order to update new mode because it could be not computed before
886 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
888 Handle(AIS_InteractiveContext) aContext = AISContext();
889 if (aContext.IsNull())
892 AISObjectPtr aAISObj = getAISObject(theObject);
896 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
897 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
900 void XGUI_Displayer::deactivateSelectionFilters()
902 Handle(AIS_InteractiveContext) aContext = AISContext();
903 if (!myAndFilter.IsNull()) {
905 if (!aContext.IsNull()) {
906 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
907 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
908 for (; anIt.More() && !aFound; anIt.Next()) {
909 Handle(SelectMgr_Filter) aFilter = anIt.Value();
910 aFound = aFilter == myAndFilter;
913 aContext->RemoveFilter(myAndFilter);
915 myAndFilter.Nullify();
919 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
921 Handle(AIS_InteractiveContext) aContext = AISContext();
922 if (aContext.IsNull() || hasSelectionFilter(theFilter))
925 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
926 if (!aCompositeFilter.IsNull()) {
927 aCompositeFilter->Add(theFilter);
928 #ifdef DEBUG_SELECTION_FILTERS
929 int aCount = aCompositeFilter->StoredFilters().Extent();
930 qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
935 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
937 Handle(AIS_InteractiveContext) aContext = AISContext();
938 if (aContext.IsNull())
941 Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
942 if (!aCompositeFilter.IsNull() && aCompositeFilter->IsIn(theFilter)) {
943 aCompositeFilter->Remove(theFilter);
944 #ifdef DEBUG_SELECTION_FILTERS
945 int aCount = aCompositeFilter->StoredFilters().Extent();
946 qDebug(QString("removeSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
951 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
953 bool aFilterFound = false;
955 Handle(AIS_InteractiveContext) aContext = AISContext();
956 if (aContext.IsNull())
958 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
959 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
960 for (; aIt.More() && !aFilterFound; aIt.Next()) {
961 if (theFilter.Access() == aIt.Value().Access())
964 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
965 if (!aCompositeFilter.IsNull()) {
966 const SelectMgr_ListOfFilter& aStoredFilters = aCompositeFilter->StoredFilters();
967 for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
968 if (theFilter.Access() == aIt.Value().Access())
975 void XGUI_Displayer::removeFilters()
977 Handle(AIS_InteractiveContext) aContext = AISContext();
978 if (aContext.IsNull())
981 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
982 if (!aCompositeFilter.IsNull())
983 aCompositeFilter->Clear();
986 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
988 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
989 foreach(ObjectPtr aObj, aDispList) {
990 if (!theList.contains(aObj))
993 foreach(ObjectPtr aObj, theList) {
994 if (!isVisible(aObj))
995 display(aObj, false);
1000 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
1002 if (!isVisible(theObject))
1005 AISObjectPtr aAISObj = getAISObject(theObject);
1006 if (aAISObj.get() == NULL)
1009 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1010 return ::canBeShaded(anAIS);
1013 bool XGUI_Displayer::activate(const Handle(AIS_InteractiveObject)& theIO,
1014 const QIntList& theModes,
1015 const bool theUpdateViewer) const
1017 Handle(AIS_InteractiveContext) aContext = AISContext();
1018 if (aContext.IsNull() || theIO.IsNull())
1021 bool isActivationChanged = false;
1022 // deactivate object in all modes, which are not in the list of activation
1023 // It seems that after the IO deactivation the selected state of the IO's owners
1024 // is modified in OCC(version: 6.8.0) and the selection of the object later is lost.
1025 // By this reason, the number of the IO deactivate is decreased and the object is deactivated
1026 // only if there is a difference in the current modes and the parameters modes.
1027 // If the selection problem happens again, it is possible to write a test scenario and create
1028 // a bug. The bug steps are the following:
1029 // Create two IO, activate them in 5 modes, select the first IO, deactivate 3 modes for both,
1030 // with clicked SHIFT select the second object. The result is the selection of the first IO is lost.
1031 TColStd_ListOfInteger aTColModes;
1032 aContext->ActivatedModes(theIO, aTColModes);
1033 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
1034 QIntList aModesActivatedForIO;
1035 bool isDeactivated = false;
1036 for (; itr.More(); itr.Next() ) {
1037 Standard_Integer aMode = itr.Value();
1038 if (!theModes.contains(aMode)) {
1039 deactivateAIS(theIO, aMode);
1040 isDeactivated = true;
1043 aModesActivatedForIO.append(aMode);
1046 if (isDeactivated) {
1047 // the selection from the previous activation modes should be cleared manually (#26172)
1048 theIO->ClearSelected();
1049 aContext->LocalContext()->ClearOutdatedSelection(theIO, true);
1050 // For performance issues
1051 //if (theUpdateViewer)
1053 isActivationChanged = true;
1056 // loading the interactive object allowing the decomposition
1057 if (aTColModes.IsEmpty()) {
1058 aContext->Load(theIO, -1, true);
1061 // trihedron AIS check should be after the AIS loading.
1062 // If it is not loaded, it is steel selectable in the viewer.
1063 Handle(AIS_Trihedron) aTrihedron;
1064 if (!isTrihedronActive())
1065 aTrihedron = Handle(AIS_Trihedron)::DownCast(theIO);
1066 if (aTrihedron.IsNull()) {
1067 // In order to clear active modes list
1068 if (theModes.size() == 0) {
1069 activateAIS(theIO, 0, theUpdateViewer);
1071 foreach(int aMode, theModes) {
1072 if (!aModesActivatedForIO.contains(aMode)) {
1073 activateAIS(theIO, aMode, theUpdateViewer);
1074 isActivationChanged = true;
1079 return isActivationChanged;
1082 bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
1084 AISObjectPtr anAISObj = getAISObject(theObject);
1085 // correct the result's color it it has the attribute
1086 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1088 // Customization of presentation
1089 GeomCustomPrsPtr aCustomPrs;
1090 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1091 if (aFeature.get() != NULL) {
1092 GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
1093 if (aCustPrs.get() != NULL)
1094 aCustomPrs = aCustPrs;
1096 if (aCustomPrs.get() == NULL) {
1097 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
1098 // we ignore presentable not customized objects
1099 if (aPrs.get() == NULL)
1100 aCustomPrs = myCustomPrs;
1102 bool isCustomized = aCustomPrs.get() &&
1103 aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
1104 return isCustomized;
1108 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject, const QColor& theColor, bool theUpdateViewer)
1110 if (!isVisible(theObject))
1113 AISObjectPtr anAISObj = getAISObject(theObject);
1115 anAISObj->getColor(aR, aG, aB);
1116 anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
1117 if (theUpdateViewer)
1119 return QColor(aR, aG, aB);
1122 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
1124 myResult2AISObjectMap[theObject] = theAIS;
1126 #ifdef DEBUG_DISPLAY
1127 std::ostringstream aPtrStr;
1128 aPtrStr << theObject.get();
1129 qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
1130 qDebug(getResult2AISObjectMapInfo().c_str());
1134 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
1136 QStringList aContent;
1137 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
1138 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
1139 std::ostringstream aPtrStr;
1140 aPtrStr << "aObj = " << aObj.get() << ":";
1141 aPtrStr << "anAIS = " << aAISObj.get() << ":";
1142 aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
1144 aContent.append(aPtrStr.str().c_str());
1146 return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
1147 arg(aContent.join("\n")).toStdString().c_str();
1150 void XGUI_Displayer::activateTrihedron(bool theIsActive)
1152 myIsTrihedronActive = theIsActive;
1153 if (!myIsTrihedronActive) {
1154 deactivateTrihedron();