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 //Handle(AIS_InteractiveObject) aTriehedron = getTrihedron();
368 //if (!aTriehedron.IsNull())
369 // deactivateAIS(aTriehedron);
371 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
372 for (; anIt != aLast; anIt++) {
373 deactivate(*anIt, false);
375 //VSV It seems that there is no necessity to update viewer on deactivation
376 //if (theUpdateViewer)
380 void XGUI_Displayer::getModesOfActivation(ObjectPtr theObject, QIntList& theModes)
382 Handle(AIS_InteractiveContext) aContext = AISContext();
383 if (aContext.IsNull() || !isVisible(theObject))
386 AISObjectPtr aAISObj = getAISObject(theObject);
388 if (aAISObj.get() != NULL) {
389 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
390 TColStd_ListOfInteger aTColModes;
391 aContext->ActivatedModes(anAISIO, aTColModes);
392 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
393 for (; itr.More(); itr.Next() ) {
394 theModes.append(itr.Value());
399 int XGUI_Displayer::getSelectionMode(int theShapeType)
401 return (theShapeType >= TopAbs_SHAPE)? theShapeType :
402 AIS_Shape::SelectionMode((TopAbs_ShapeEnum)theShapeType);
405 void XGUI_Displayer::activateObjects(const QIntList& theModes, const QObjectPtrList& theObjList,
406 const bool theUpdateViewer)
408 // Convert shape types to selection types
410 foreach(int aType, theModes) {
411 aModes.append(getSelectionMode(aType));
414 #ifdef DEBUG_ACTIVATE_OBJECTS
416 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
417 for (; anIt != aLast; ++anIt) {
418 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
420 QString anInfoStr = anInfo.join(", ");
422 qDebug(QString("activateObjects: aModes[%1] = %2, myActiveSelectionModes[%3] = %4, objects = %5").
423 arg(aModes.size()).arg(qIntListInfo(aModes)).
424 arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
426 toStdString().c_str());
428 // In order to avoid doblications of selection modes
430 foreach (int aMode, aModes) {
431 if (!aNewModes.contains(aMode))
432 aNewModes.append(aMode);
434 myActiveSelectionModes = aNewModes;
435 Handle(AIS_InteractiveContext) aContext = AISContext();
436 // Open local context if there is no one
437 if (aContext.IsNull() || !aContext->HasOpenedContext())
440 //aContext->UseDisplayedObjects();
441 //myUseExternalObjects = true;
443 Handle(AIS_InteractiveObject) anAISIO;
444 AIS_ListOfInteractive aPrsList;
445 //if (aObjList.isEmpty())
448 foreach(ObjectPtr aObj, theObjList) {
449 if (myResult2AISObjectMap.contains(aObj))
450 aPrsList.Append(myResult2AISObjectMap[aObj]->impl<Handle(AIS_InteractiveObject)>());
454 // Add trihedron because it has to partisipate in selection
455 Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
456 if (!aTrihedron.IsNull())
457 aPrsList.Append(aTrihedron);
459 if (aPrsList.Extent() == 0)
462 AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
463 bool isActivationChanged = false;
464 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
465 anAISIO = aLIt.Value();
466 if (activate(anAISIO, myActiveSelectionModes, false))
467 isActivationChanged = true;
469 if (!aTrihedron.IsNull()) {
470 foreach(int aMode, myActiveSelectionModes)
471 aContext->SetSelectionSensitivity(aTrihedron, aMode, 8);
473 // VSV It seems that there is no necessity to update viewer on activation
474 //if (theUpdateViewer && isActivationChanged)
478 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
480 Handle(AIS_InteractiveContext) aContext = AISContext();
481 if (aContext.IsNull() || !isVisible(theObject))
484 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
485 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
487 TColStd_ListOfInteger aModes;
488 aContext->ActivatedModes(anAIS, aModes);
489 return aModes.Extent() > 0;
491 void XGUI_Displayer::setSelected(const QList<ModuleBase_ViewerPrs>& theValues, bool theUpdateViewer)
493 Handle(AIS_InteractiveContext) aContext = AISContext();
494 if (aContext.IsNull())
496 if (aContext->HasOpenedContext()) {
497 aContext->UnhilightSelected();
498 aContext->ClearSelected();
499 foreach (ModuleBase_ViewerPrs aPrs, theValues) {
500 const TopoDS_Shape& aShape = aPrs.shape();
501 if (!aShape.IsNull()) {
502 aContext->AddOrRemoveSelected(aShape, false);
504 ObjectPtr anObject = aPrs.object();
505 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
506 if (aResult.get() && isVisible(aResult)) {
507 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
508 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
509 if (!anAIS.IsNull()) {
510 // The methods are replaced in order to provide multi-selection, e.g. restore selection
511 // by activating multi selector widget. It also gives an advantage that the multi
512 // selection in OB gives multi-selection in the viewer
513 //aContext->SetSelected(anAIS, false);
514 // The selection in the context was cleared, so the method sets the objects are selected
515 aContext->AddOrRemoveSelected(anAIS, false);
521 aContext->UnhilightCurrents();
522 aContext->ClearCurrents();
523 foreach (ModuleBase_ViewerPrs aPrs, theValues) {
524 ObjectPtr anObject = aPrs.object();
525 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
526 if (aResult.get() && isVisible(aResult)) {
527 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
528 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
530 aContext->SetCurrentObject(anAIS, false);
538 void XGUI_Displayer::clearSelected()
540 Handle(AIS_InteractiveContext) aContext = AISContext();
541 if (!aContext.IsNull()) {
542 aContext->UnhilightCurrents(false);
543 aContext->ClearSelected();
547 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
549 bool aErased = false;
550 Handle(AIS_InteractiveContext) aContext = AISContext();
551 if (!aContext.IsNull()) {
552 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
553 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
555 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
556 if (!anIO.IsNull()) {
557 emit beforeObjectErase(aObj, aAISObj);
558 aContext->Remove(anIO, false);
565 myResult2AISObjectMap.clear();
568 qDebug(getResult2AISObjectMapInfo().c_str());
573 void XGUI_Displayer::deactivateTrihedron() const
575 Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
576 if (!aTrihedron.IsNull()) {
577 Handle(AIS_InteractiveContext) aContext = AISContext();
578 aContext->Deactivate(aTrihedron);
582 Handle(AIS_InteractiveObject) XGUI_Displayer::getTrihedron() const
584 Handle(AIS_InteractiveContext) aContext = AISContext();
585 if (!aContext.IsNull()) {
586 AIS_ListOfInteractive aList;
587 aContext->DisplayedObjects(aList, true);
588 AIS_ListIteratorOfListOfInteractive aIt;
589 for (aIt.Initialize(aList); aIt.More(); aIt.Next()) {
590 Handle(AIS_Trihedron) aTrihedron = Handle(AIS_Trihedron)::DownCast(aIt.Value());
591 if (!aTrihedron.IsNull()) {
596 return Handle(AIS_InteractiveObject)();
599 void XGUI_Displayer::openLocalContext()
601 Handle(AIS_InteractiveContext) aContext = AISContext();
602 // Open local context if there is no one
603 if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
604 // Preserve selected objects
605 //AIS_ListOfInteractive aAisList;
606 //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
607 // aAisList.Append(aContext->Current());
609 // get the filters from the global context and append them to the local context
610 // a list of filters in the global context is not cleared and should be cleared here
611 SelectMgr_ListOfFilter aFilters;
612 aFilters.Assign(aContext->Filters());
613 // it is important to remove the filters in the global context, because there is a code
614 // in the closeLocalContex, which restore the global context filters
615 aContext->RemoveFilters();
617 //aContext->ClearCurrents();
618 aContext->OpenLocalContext();
619 //deactivateTrihedron();
620 //aContext->NotUseDisplayedObjects();
622 //myUseExternalObjects = false;
624 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
625 for (;aIt.More(); aIt.Next()) {
626 aContext->AddFilter(aIt.Value());
629 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
630 //for(; aIt2.More(); aIt2.Next()) {
631 // aContext->SetSelected(aIt2.Value(), false);
636 void XGUI_Displayer::closeLocalContexts(const bool theUpdateViewer)
638 Handle(AIS_InteractiveContext) aContext = AISContext();
639 if (!aContext.IsNull() && aContext->HasOpenedContext()) {
640 // Preserve selected objects
641 //AIS_ListOfInteractive aAisList;
642 //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
643 // aAisList.Append(aContext->SelectedInteractive());
645 // get the filters from the local context and append them to the global context
646 // a list of filters in the local context is cleared
647 SelectMgr_ListOfFilter aFilters;
648 aFilters.Assign(aContext->Filters());
650 //aContext->ClearSelected();
651 aContext->CloseAllContexts(false);
653 // From the moment when the AIS_DS_Displayed flag is used in the Display of AIS object,
654 // this code is obsolete. It is temporaty commented and should be removed after
655 // the test campaign.
656 // Redisplay all object if they were displayed in localContext
657 /*Handle(AIS_InteractiveObject) aAISIO;
658 foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
659 aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
660 if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
661 aContext->Display(aAISIO, false);
662 aContext->SetDisplayMode(aAISIO, Shading, false);
666 // Append the filters from the local selection in the global selection context
667 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
668 for (;aIt.More(); aIt.Next()) {
669 Handle(SelectMgr_Filter) aFilter = aIt.Value();
670 aContext->AddFilter(aFilter);
675 //myUseExternalObjects = false;
678 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
679 //for(; aIt2.More(); aIt2.Next()) {
680 // if (aContext->IsDisplayed(aIt2.Value()))
681 // aContext->SetCurrentObject(aIt2.Value(), false);
686 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
689 if (myResult2AISObjectMap.contains(theObject))
690 anIO = myResult2AISObjectMap[theObject];
694 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
696 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
697 return getObject(aRefAIS);
700 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
703 foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
704 AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
705 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
711 if (!anObject.get()) {
712 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
713 if (!theIO.IsNull()) {
714 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
716 anObject = myWorkshop->module()->findPresentedObject(anAISObj);
721 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
723 bool aWasEnabled = myEnableUpdateViewer;
725 myEnableUpdateViewer = isEnabled;
726 if (myNeedUpdate && myEnableUpdateViewer) {
728 myNeedUpdate = false;
733 void XGUI_Displayer::updateViewer() const
735 Handle(AIS_InteractiveContext) aContext = AISContext();
736 if (!aContext.IsNull() && myEnableUpdateViewer) {
737 myWorkshop->viewer()->Zfitall();
738 aContext->UpdateCurrentViewer();
744 void XGUI_Displayer::activateAIS(const Handle(AIS_InteractiveObject)& theIO,
745 const int theMode, const bool theUpdateViewer) const
747 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
748 if (!aContext.IsNull()) {
749 aContext->Activate(theIO, theMode, theUpdateViewer);
751 #ifdef DEBUG_ACTIVATE_AIS
752 ObjectPtr anObject = getObject(theIO);
753 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
754 qDebug(QString("activateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
759 void XGUI_Displayer::deactivateAIS(const Handle(AIS_InteractiveObject)& theIO, const int theMode) const
761 Handle(AIS_InteractiveContext) aContext = AISContext();
762 if (!aContext.IsNull()) {
764 aContext->Deactivate(theIO);
766 aContext->Deactivate(theIO, theMode);
768 #ifdef DEBUG_DEACTIVATE_AIS
769 ObjectPtr anObject = getObject(theIO);
770 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
771 qDebug(QString("deactivateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
776 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
778 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
779 if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
780 aContext->OpenLocalContext();
781 //deactivateTrihedron();
782 aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
783 aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
788 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
790 Handle(AIS_InteractiveContext) aContext = AISContext();
791 if (!aContext.IsNull() && myAndFilter.IsNull()) {
792 myAndFilter = new SelectMgr_AndFilter();
793 aContext->AddFilter(myAndFilter);
798 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
799 bool theUpdateViewer)
801 bool aDisplayed = false;
802 Handle(AIS_InteractiveContext) aContext = AISContext();
803 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
804 if (!aContext.IsNull() && !anAISIO.IsNull()) {
805 aContext->Display(anAISIO, 0/*wireframe*/, 0, theUpdateViewer, true, AIS_DS_Displayed);
807 aContext->Deactivate(anAISIO);
808 aContext->Load(anAISIO);
809 if (toActivateInSelectionModes) {
810 if (aContext->HasOpenedContext()) {
811 if (myActiveSelectionModes.size() == 0)
812 activateAIS(anAISIO, 0, theUpdateViewer);
814 foreach(int aMode, myActiveSelectionModes) {
815 activateAIS(anAISIO, aMode, theUpdateViewer);
824 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
826 bool aErased = false;
827 Handle(AIS_InteractiveContext) aContext = AISContext();
828 if (!aContext.IsNull()) {
829 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
830 if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
831 aContext->Remove(anAISIO, theUpdateViewer);
839 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
841 if (theMode == NoMode)
844 Handle(AIS_InteractiveContext) aContext = AISContext();
845 if (aContext.IsNull())
848 AISObjectPtr aAISObj = getAISObject(theObject);
852 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
853 aContext->SetDisplayMode(aAISIO, theMode, false);
854 // Redisplay in order to update new mode because it could be not computed before
859 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
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 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
873 void XGUI_Displayer::deactivateSelectionFilters()
875 Handle(AIS_InteractiveContext) aContext = AISContext();
876 if (!myAndFilter.IsNull()) {
878 if (!aContext.IsNull()) {
879 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
880 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
881 for (; anIt.More() && !aFound; anIt.Next()) {
882 Handle(SelectMgr_Filter) aFilter = anIt.Value();
883 aFound = aFilter == myAndFilter;
886 aContext->RemoveFilter(myAndFilter);
888 myAndFilter.Nullify();
892 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
894 Handle(AIS_InteractiveContext) aContext = AISContext();
895 if (aContext.IsNull() || hasSelectionFilter(theFilter))
898 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
899 if (!aCompositeFilter.IsNull()) {
900 aCompositeFilter->Add(theFilter);
901 #ifdef DEBUG_SELECTION_FILTERS
902 int aCount = aCompositeFilter->StoredFilters().Extent();
903 qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
908 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
910 Handle(AIS_InteractiveContext) aContext = AISContext();
911 if (aContext.IsNull())
914 Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
915 if (!aCompositeFilter.IsNull() && aCompositeFilter->IsIn(theFilter)) {
916 aCompositeFilter->Remove(theFilter);
917 #ifdef DEBUG_SELECTION_FILTERS
918 int aCount = aCompositeFilter->StoredFilters().Extent();
919 qDebug(QString("removeSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
924 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
926 bool aFilterFound = false;
928 Handle(AIS_InteractiveContext) aContext = AISContext();
929 if (aContext.IsNull())
931 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
932 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
933 for (; aIt.More() && !aFilterFound; aIt.Next()) {
934 if (theFilter.Access() == aIt.Value().Access())
937 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
938 if (!aCompositeFilter.IsNull()) {
939 const SelectMgr_ListOfFilter& aStoredFilters = aCompositeFilter->StoredFilters();
940 for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
941 if (theFilter.Access() == aIt.Value().Access())
948 void XGUI_Displayer::removeFilters()
950 Handle(AIS_InteractiveContext) aContext = AISContext();
951 if (aContext.IsNull())
954 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
955 if (!aCompositeFilter.IsNull())
956 aCompositeFilter->Clear();
959 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
961 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
962 foreach(ObjectPtr aObj, aDispList) {
963 if (!theList.contains(aObj))
966 foreach(ObjectPtr aObj, theList) {
967 if (!isVisible(aObj))
968 display(aObj, false);
973 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
975 if (!isVisible(theObject))
978 AISObjectPtr aAISObj = getAISObject(theObject);
979 if (aAISObj.get() == NULL)
982 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
983 return ::canBeShaded(anAIS);
986 bool XGUI_Displayer::activate(const Handle(AIS_InteractiveObject)& theIO,
987 const QIntList& theModes,
988 const bool theUpdateViewer) const
990 Handle(AIS_InteractiveContext) aContext = AISContext();
991 if (aContext.IsNull() || theIO.IsNull())
994 bool isActivationChanged = false;
995 // deactivate object in all modes, which are not in the list of activation
996 // It seems that after the IO deactivation the selected state of the IO's owners
997 // is modified in OCC(version: 6.8.0) and the selection of the object later is lost.
998 // By this reason, the number of the IO deactivate is decreased and the object is deactivated
999 // only if there is a difference in the current modes and the parameters modes.
1000 // If the selection problem happens again, it is possible to write a test scenario and create
1001 // a bug. The bug steps are the following:
1002 // Create two IO, activate them in 5 modes, select the first IO, deactivate 3 modes for both,
1003 // with clicked SHIFT select the second object. The result is the selection of the first IO is lost.
1004 TColStd_ListOfInteger aTColModes;
1005 aContext->ActivatedModes(theIO, aTColModes);
1006 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
1007 QIntList aModesActivatedForIO;
1008 bool isDeactivated = false;
1009 for (; itr.More(); itr.Next() ) {
1010 Standard_Integer aMode = itr.Value();
1011 if (!theModes.contains(aMode)) {
1012 deactivateAIS(theIO, aMode);
1013 isDeactivated = true;
1016 aModesActivatedForIO.append(aMode);
1019 if (isDeactivated) {
1020 // the selection from the previous activation modes should be cleared manually (#26172)
1021 theIO->ClearSelected();
1022 aContext->LocalContext()->ClearOutdatedSelection(theIO, true);
1023 // For performance issues
1024 //if (theUpdateViewer)
1026 isActivationChanged = true;
1029 // loading the interactive object allowing the decomposition
1030 if (aTColModes.IsEmpty()) {
1031 aContext->Load(theIO, -1, true);
1034 // trihedron AIS check should be after the AIS loading.
1035 // If it is not loaded, it is steel selectable in the viewer.
1036 //Handle(AIS_Trihedron) aTrihedron = Handle(AIS_Trihedron)::DownCast(theIO);
1037 //if (aTrihedron.IsNull()) {
1038 //aContext->Load(anAISIO, -1, true);
1039 // In order to clear active modes list
1040 if (theModes.size() == 0) {
1041 //aContext->Load(anAISIO, 0, true);
1042 activateAIS(theIO, 0, theUpdateViewer);
1044 foreach(int aMode, theModes) {
1045 //aContext->Load(anAISIO, aMode, true);
1046 if (!aModesActivatedForIO.contains(aMode)) {
1047 activateAIS(theIO, aMode, theUpdateViewer);
1048 isActivationChanged = true;
1053 return isActivationChanged;
1056 bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
1058 AISObjectPtr anAISObj = getAISObject(theObject);
1059 // correct the result's color it it has the attribute
1060 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1062 // Customization of presentation
1063 GeomCustomPrsPtr aCustomPrs;
1064 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1065 if (aFeature.get() != NULL) {
1066 GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
1067 if (aCustPrs.get() != NULL)
1068 aCustomPrs = aCustPrs;
1070 if (aCustomPrs.get() == NULL) {
1071 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
1072 // we ignore presentable not customized objects
1073 if (aPrs.get() == NULL)
1074 aCustomPrs = myCustomPrs;
1076 bool isCustomized = aCustomPrs.get() &&
1077 aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
1078 return isCustomized;
1082 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject, const QColor& theColor, bool theUpdateViewer)
1084 if (!isVisible(theObject))
1087 AISObjectPtr anAISObj = getAISObject(theObject);
1089 anAISObj->getColor(aR, aG, aB);
1090 anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
1091 if (theUpdateViewer)
1093 return QColor(aR, aG, aB);
1096 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
1098 myResult2AISObjectMap[theObject] = theAIS;
1100 #ifdef DEBUG_DISPLAY
1101 std::ostringstream aPtrStr;
1102 aPtrStr << theObject.get();
1103 qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
1104 qDebug(getResult2AISObjectMapInfo().c_str());
1108 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
1110 QStringList aContent;
1111 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
1112 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
1113 std::ostringstream aPtrStr;
1114 aPtrStr << "aObj = " << aObj.get() << ":";
1115 aPtrStr << "anAIS = " << aAISObj.get() << ":";
1116 aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
1118 aContent.append(aPtrStr.str().c_str());
1120 return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
1121 arg(aContent.join("\n")).toStdString().c_str();