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 <AIS_Axis.hxx>
42 #include <AIS_Plane.hxx>
43 #include <AIS_Point.hxx>
44 #include <TColStd_ListIteratorOfListOfInteger.hxx>
45 #include <SelectMgr_ListOfFilter.hxx>
46 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
47 #include <Prs3d_Drawer.hxx>
48 #include <Prs3d_IsoAspect.hxx>
49 #include <SelectMgr_SelectionManager.hxx>
51 #include <StdSelect_ViewerSelector3d.hxx>
53 #include <TColStd_MapOfTransient.hxx>
54 #include <TColStd_MapIteratorOfMapOfTransient.hxx>
56 #include <Events_Loop.h>
57 #include <ModelAPI_Events.h>
61 const int MOUSE_SENSITIVITY_IN_PIXEL = 10; ///< defines the local context mouse selection sensitivity
63 //#define DEBUG_ACTIVATE_OBJECTS
64 //#define DEBUG_DEACTIVATE
65 //#define DEBUG_ACTIVATE_AIS
66 //#define DEBUG_DEACTIVATE_AIS
68 //#define DEBUG_DISPLAY
69 //#define DEBUG_FEATURE_REDISPLAY
70 //#define DEBUG_SELECTION_FILTERS
72 //#define DEBUG_COMPOSILID_DISPLAY
73 // Workaround for bug #25637
74 void displayedObjects(const Handle(AIS_InteractiveContext)& theAIS, AIS_ListOfInteractive& theList)
76 // Get from null point
77 theAIS->DisplayedObjects(theList, true);
78 if (theAIS->HasOpenedContext()) {
79 // get from local context
80 const Handle(AIS_LocalContext)& aLC = theAIS->LocalContext();
81 TColStd_MapOfTransient aMap;
82 int NbDisp = aLC->DisplayedObjects(aMap);
83 TColStd_MapIteratorOfMapOfTransient aIt(aMap);
85 Handle(AIS_InteractiveObject) curIO;
86 Handle(Standard_Transient) Tr;
87 for(; aIt.More(); aIt.Next()){
89 curIO = *((Handle(AIS_InteractiveObject)*) &Tr);
90 theList.Append(curIO);
95 QString qIntListInfo(const QIntList& theValues, const QString& theSeparator = QString(", "))
98 QIntList::const_iterator anIt = theValues.begin(), aLast = theValues.end();
99 for (; anIt != aLast; anIt++) {
100 anInfo.append(QString::number(*anIt));
102 return anInfo.join(theSeparator);
105 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
106 : myWorkshop(theWorkshop), myNeedUpdate(false),
107 myIsTrihedronActive(false), myViewerBlockedRecursiveCount(0)
109 myCustomPrs = std::shared_ptr<GeomAPI_ICustomPrs>(new XGUI_CustomPrs(theWorkshop));
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);
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());
274 bool XGUI_Displayer::redisplay(ObjectPtr theObject, bool theUpdateViewer)
276 bool aRedisplayed = false;
277 if (!isVisible(theObject))
280 AISObjectPtr aAISObj = getAISObject(theObject);
281 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
283 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
285 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
287 aRedisplayed = erase(theObject, theUpdateViewer);
290 if (aAIS_Obj != aAISObj) {
291 appendResultObject(theObject, aAIS_Obj);
293 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
296 Handle(AIS_InteractiveContext) aContext = AISContext();
297 if (!aContext.IsNull() && !aAISIO.IsNull()) {
298 // Check that the visualized shape is the same and the redisplay is not necessary
299 // Redisplay of AIS object leads to this object selection compute and the selection
300 // in the browser is lost
301 // this check is not necessary anymore because the selection store/restore is realized
302 // before and after the values modification.
303 // Moreother, this check avoids customize and redisplay presentation if the presentable
304 // parameter is changed.
305 bool isEqualShapes = false;
306 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
307 if (aResult.get() != NULL) {
308 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(aAISIO);
309 if (!aShapePrs.IsNull()) {
310 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
311 if (aShapePtr.get()) {
312 const TopoDS_Shape& aOldShape = aShapePrs->Shape();
313 if (!aOldShape.IsNull())
314 isEqualShapes = aOldShape.IsEqual(aShapePtr->impl<TopoDS_Shape>());
318 // Customization of presentation
319 bool isCustomized = customizeObject(theObject);
320 #ifdef DEBUG_FEATURE_REDISPLAY
321 qDebug(QString("Redisplay: %1, isEqualShapes=%2, isCustomized=%3").
322 arg(!isEqualShapes || isCustomized).arg(isEqualShapes).arg(isCustomized).toStdString().c_str());
324 if (!isEqualShapes || isCustomized) {
325 aContext->Redisplay(aAISIO, false);
327 #ifdef DEBUG_FEATURE_REDISPLAY
328 qDebug(" Redisplay happens");
337 void XGUI_Displayer::redisplayObjects()
339 // redisplay objects visualized in the viewer
340 static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
341 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
342 QObjectPtrList aDisplayed = myWorkshop->displayer()->displayedObjects();
343 QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
344 for (; anIt != aLast; anIt++) {
345 aECreator->sendUpdated(*anIt, EVENT_DISP);
347 Events_Loop::loop()->flush(EVENT_DISP);
350 void XGUI_Displayer::deactivate(ObjectPtr theObject, const bool theUpdateViewer)
352 #ifdef DEBUG_DEACTIVATE
353 QString anInfoStr = ModuleBase_Tools::objectInfo(theObject);
354 qDebug(QString("deactivate: myActiveSelectionModes[%1]: %2, objects = ").
355 arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
357 toStdString().c_str());
359 Handle(AIS_InteractiveContext) aContext = AISContext();
360 if (!aContext.IsNull() && isVisible(theObject)) {
361 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
362 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
364 deactivateAIS(anAIS);
365 // the selection from the previous activation modes should be cleared manually (#26172)
366 aContext->LocalContext()->ClearOutdatedSelection(anAIS, true);
372 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
373 const bool theUpdateViewer)
375 //Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
376 //if (!aTrihedron.IsNull())
377 // deactivateAIS(aTrihedron);
379 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
380 for (; anIt != aLast; anIt++) {
381 deactivate(*anIt, false);
383 //VSV It seems that there is no necessity to update viewer on deactivation
384 //if (theUpdateViewer)
388 void XGUI_Displayer::getModesOfActivation(ObjectPtr theObject, QIntList& theModes)
390 Handle(AIS_InteractiveContext) aContext = AISContext();
391 if (aContext.IsNull() || !isVisible(theObject))
394 AISObjectPtr aAISObj = getAISObject(theObject);
396 if (aAISObj.get() != NULL) {
397 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
398 TColStd_ListOfInteger aTColModes;
399 aContext->ActivatedModes(anAISIO, aTColModes);
400 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
401 for (; itr.More(); itr.Next() ) {
402 theModes.append(itr.Value());
407 int XGUI_Displayer::getSelectionMode(int theShapeType)
409 return (theShapeType >= TopAbs_SHAPE)? theShapeType :
410 AIS_Shape::SelectionMode((TopAbs_ShapeEnum)theShapeType);
413 void XGUI_Displayer::activateObjects(const QIntList& theModes, const QObjectPtrList& theObjList,
414 const bool theUpdateViewer)
416 // Convert shape types to selection types
418 foreach(int aType, theModes) {
419 aModes.append(getSelectionMode(aType));
422 #ifdef DEBUG_ACTIVATE_OBJECTS
424 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
425 for (; anIt != aLast; ++anIt) {
426 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
428 QString anInfoStr = anInfo.join(", ");
430 qDebug(QString("activateObjects: aModes[%1] = %2, myActiveSelectionModes[%3] = %4, objects = %5").
431 arg(aModes.size()).arg(qIntListInfo(aModes)).
432 arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
434 toStdString().c_str());
436 // In order to avoid doblications of selection modes
438 foreach (int aMode, aModes) {
439 if (!aNewModes.contains(aMode))
440 aNewModes.append(aMode);
442 myActiveSelectionModes = aNewModes;
443 Handle(AIS_InteractiveContext) aContext = AISContext();
444 // Open local context if there is no one
445 if (aContext.IsNull() || !aContext->HasOpenedContext())
448 //aContext->UseDisplayedObjects();
449 //myUseExternalObjects = true;
451 Handle(AIS_InteractiveObject) anAISIO;
452 AIS_ListOfInteractive aPrsList;
453 //if (aObjList.isEmpty())
456 foreach(ObjectPtr aObj, theObjList) {
457 if (myResult2AISObjectMap.contains(aObj))
458 aPrsList.Append(myResult2AISObjectMap[aObj]->impl<Handle(AIS_InteractiveObject)>());
462 // Add trihedron because it has to partisipate in selection
463 Handle(AIS_InteractiveObject) aTrihedron;
464 if (isTrihedronActive()) {
465 aTrihedron = getTrihedron();
466 if (!aTrihedron.IsNull() && aContext->IsDisplayed(aTrihedron))
467 aPrsList.Append(aTrihedron);
469 if (aPrsList.Extent() == 0)
472 AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
473 bool isActivationChanged = false;
474 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
475 anAISIO = aLIt.Value();
476 if (activate(anAISIO, myActiveSelectionModes, false))
477 isActivationChanged = true;
479 if (!aTrihedron.IsNull()) {
480 foreach(int aMode, myActiveSelectionModes)
481 aContext->SetSelectionSensitivity(aTrihedron, aMode, 20);
483 // VSV It seems that there is no necessity to update viewer on activation
484 //if (theUpdateViewer && isActivationChanged)
488 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
490 Handle(AIS_InteractiveContext) aContext = AISContext();
491 if (aContext.IsNull() || !isVisible(theObject))
494 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
495 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
497 TColStd_ListOfInteger aModes;
498 aContext->ActivatedModes(anAIS, aModes);
499 return aModes.Extent() > 0;
501 void XGUI_Displayer::setSelected(const QList<ModuleBase_ViewerPrs>& theValues, bool theUpdateViewer)
503 Handle(AIS_InteractiveContext) aContext = AISContext();
504 if (aContext.IsNull())
506 if (aContext->HasOpenedContext()) {
507 aContext->UnhilightSelected(false);
508 aContext->ClearSelected(false);
509 foreach (ModuleBase_ViewerPrs aPrs, theValues) {
510 const TopoDS_Shape& aShape = aPrs.shape();
511 if (!aShape.IsNull()) {
512 aContext->AddOrRemoveSelected(aShape, false);
514 ObjectPtr anObject = aPrs.object();
515 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
516 if (aResult.get() && isVisible(aResult)) {
517 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
518 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
519 if (!anAIS.IsNull()) {
520 // The methods are replaced in order to provide multi-selection, e.g. restore selection
521 // by activating multi selector widget. It also gives an advantage that the multi
522 // selection in OB gives multi-selection in the viewer
523 //aContext->SetSelected(anAIS, false);
524 // The selection in the context was cleared, so the method sets the objects are selected
525 aContext->AddOrRemoveSelected(anAIS, false);
531 aContext->UnhilightCurrents(false);
532 aContext->ClearCurrents(false);
533 foreach (ModuleBase_ViewerPrs aPrs, theValues) {
534 ObjectPtr anObject = aPrs.object();
535 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
536 if (aResult.get() && isVisible(aResult)) {
537 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
538 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
540 aContext->SetCurrentObject(anAIS, false);
548 void XGUI_Displayer::clearSelected()
550 Handle(AIS_InteractiveContext) aContext = AISContext();
551 if (!aContext.IsNull()) {
552 aContext->UnhilightCurrents(false);
553 aContext->ClearSelected();
557 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
559 bool aErased = false;
560 Handle(AIS_InteractiveContext) aContext = AISContext();
561 if (!aContext.IsNull()) {
562 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
563 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
565 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
566 if (!anIO.IsNull()) {
567 emit beforeObjectErase(aObj, aAISObj);
568 aContext->Remove(anIO, false);
575 myResult2AISObjectMap.clear();
578 qDebug(getResult2AISObjectMapInfo().c_str());
583 void deactivateObject(Handle(AIS_InteractiveContext) theContext,
584 Handle(AIS_InteractiveObject) theObject,
585 const bool theClear = true)
587 if (!theObject.IsNull()) {
588 theContext->Deactivate(theObject);
590 //theObject->ClearSelected();
591 // theContext->LocalContext()->ClearOutdatedSelection(theObject, true);
596 void XGUI_Displayer::deactivateTrihedron(const bool theUpdateViewer) const
598 Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
599 Handle(AIS_InteractiveContext) aContext = AISContext();
600 if (!aTrihedron.IsNull() && aContext->IsDisplayed(aTrihedron)) {
601 Handle(AIS_Trihedron) aTrie = Handle(AIS_Trihedron)::DownCast(aTrihedron);
602 deactivateObject(aContext, aTrie);
604 /// #1136 hidden axis are selected in sketch
605 /// workaround for Cascade: there is a crash in AIS_LocalContext::ClearOutdatedSelection
606 /// for Position AIS object in SelectionModes.
607 deactivateObject(aContext, aTrie->XAxis());
608 deactivateObject(aContext, aTrie->YAxis());
609 deactivateObject(aContext, aTrie->Axis());
610 deactivateObject(aContext, aTrie->Position());
612 deactivateObject(aContext, aTrie->XYPlane());
613 deactivateObject(aContext, aTrie->XZPlane());
614 deactivateObject(aContext, aTrie->YZPlane());
621 Handle(AIS_InteractiveObject) XGUI_Displayer::getTrihedron() const
623 return myWorkshop->viewer()->trihedron();
626 void XGUI_Displayer::openLocalContext()
628 Handle(AIS_InteractiveContext) aContext = AISContext();
629 // Open local context if there is no one
630 if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
631 // Preserve selected objects
632 //AIS_ListOfInteractive aAisList;
633 //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
634 // aAisList.Append(aContext->Current());
636 // get the filters from the global context and append them to the local context
637 // a list of filters in the global context is not cleared and should be cleared here
638 SelectMgr_ListOfFilter aFilters;
639 aFilters.Assign(aContext->Filters());
640 // it is important to remove the filters in the global context, because there is a code
641 // in the closeLocalContex, which restore the global context filters
642 aContext->RemoveFilters();
644 //aContext->ClearCurrents();
645 aContext->OpenLocalContext();
646 //deactivateTrihedron();
647 //aContext->NotUseDisplayedObjects();
649 //myUseExternalObjects = false;
651 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
652 for (;aIt.More(); aIt.Next()) {
653 aContext->AddFilter(aIt.Value());
656 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
657 //for(; aIt2.More(); aIt2.Next()) {
658 // aContext->SetSelected(aIt2.Value(), false);
663 void XGUI_Displayer::closeLocalContexts(const bool theUpdateViewer)
665 Handle(AIS_InteractiveContext) aContext = AISContext();
666 if (!aContext.IsNull() && aContext->HasOpenedContext()) {
667 // Preserve selected objects
668 //AIS_ListOfInteractive aAisList;
669 //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
670 // aAisList.Append(aContext->SelectedInteractive());
672 // get the filters from the local context and append them to the global context
673 // a list of filters in the local context is cleared
674 SelectMgr_ListOfFilter aFilters;
675 aFilters.Assign(aContext->Filters());
677 //aContext->ClearSelected();
678 aContext->CloseAllContexts(false);
680 // From the moment when the AIS_DS_Displayed flag is used in the Display of AIS object,
681 // this code is obsolete. It is temporaty commented and should be removed after
682 // the test campaign.
683 // Redisplay all object if they were displayed in localContext
684 /*Handle(AIS_InteractiveObject) aAISIO;
685 foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
686 aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
687 if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
688 aContext->Display(aAISIO, false);
689 aContext->SetDisplayMode(aAISIO, Shading, false);
693 // Append the filters from the local selection in the global selection context
694 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
695 for (;aIt.More(); aIt.Next()) {
696 Handle(SelectMgr_Filter) aFilter = aIt.Value();
697 aContext->AddFilter(aFilter);
702 //myUseExternalObjects = false;
705 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
706 //for(; aIt2.More(); aIt2.Next()) {
707 // if (aContext->IsDisplayed(aIt2.Value()))
708 // aContext->SetCurrentObject(aIt2.Value(), false);
713 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
716 if (myResult2AISObjectMap.contains(theObject))
717 anIO = myResult2AISObjectMap[theObject];
721 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
723 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
724 return getObject(aRefAIS);
727 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
730 foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
731 AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
732 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
738 if (!anObject.get()) {
739 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
740 if (!theIO.IsNull()) {
741 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
743 anObject = myWorkshop->module()->findPresentedObject(anAISObj);
748 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
750 bool aWasEnabled = isUpdateEnabled();
752 myViewerBlockedRecursiveCount--;
754 myViewerBlockedRecursiveCount++;
756 if (myNeedUpdate && isUpdateEnabled()) {
758 myNeedUpdate = false;
763 bool XGUI_Displayer::isUpdateEnabled() const
765 return myViewerBlockedRecursiveCount == 0;
768 void XGUI_Displayer::updateViewer() const
770 Handle(AIS_InteractiveContext) aContext = AISContext();
771 if (!aContext.IsNull() && isUpdateEnabled()) {
772 myWorkshop->viewer()->Zfitall();
773 aContext->UpdateCurrentViewer();
779 void XGUI_Displayer::activateAIS(const Handle(AIS_InteractiveObject)& theIO,
780 const int theMode, const bool theUpdateViewer) const
782 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
783 if (!aContext.IsNull()) {
784 aContext->Activate(theIO, theMode, false);
786 #ifdef DEBUG_ACTIVATE_AIS
787 ObjectPtr anObject = getObject(theIO);
788 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
789 qDebug(QString("activateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
796 void XGUI_Displayer::deactivateAIS(const Handle(AIS_InteractiveObject)& theIO, const int theMode) const
798 Handle(AIS_InteractiveContext) aContext = AISContext();
799 if (!aContext.IsNull()) {
801 aContext->Deactivate(theIO);
803 aContext->Deactivate(theIO, theMode);
805 #ifdef DEBUG_DEACTIVATE_AIS
806 ObjectPtr anObject = getObject(theIO);
807 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
808 qDebug(QString("deactivateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
813 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
815 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
816 if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
817 aContext->OpenLocalContext();
818 if (!isTrihedronActive())
819 deactivateTrihedron(true);
820 aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
821 aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
826 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
828 Handle(AIS_InteractiveContext) aContext = AISContext();
829 if (!aContext.IsNull() && myAndFilter.IsNull()) {
830 myAndFilter = new SelectMgr_AndFilter();
831 aContext->AddFilter(myAndFilter);
836 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
837 bool theUpdateViewer)
839 bool aDisplayed = false;
840 Handle(AIS_InteractiveContext) aContext = AISContext();
841 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
842 if (!aContext.IsNull() && !anAISIO.IsNull()) {
843 aContext->Display(anAISIO, 0/*wireframe*/, 0, false/*update viewer*/, true, AIS_DS_Displayed);
845 aContext->Deactivate(anAISIO);
846 aContext->Load(anAISIO);
847 if (toActivateInSelectionModes) {
848 if (aContext->HasOpenedContext()) {
849 if (myActiveSelectionModes.size() == 0)
850 activateAIS(anAISIO, 0, theUpdateViewer);
852 foreach(int aMode, myActiveSelectionModes) {
853 activateAIS(anAISIO, aMode, theUpdateViewer);
864 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
866 bool aErased = false;
867 Handle(AIS_InteractiveContext) aContext = AISContext();
868 if (!aContext.IsNull()) {
869 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
870 if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
871 aContext->Remove(anAISIO, false/*update viewer*/);
875 if (aErased && theUpdateViewer)
881 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
883 if (theMode == NoMode)
886 Handle(AIS_InteractiveContext) aContext = AISContext();
887 if (aContext.IsNull())
890 AISObjectPtr aAISObj = getAISObject(theObject);
894 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
895 aContext->SetDisplayMode(aAISIO, theMode, false);
896 // Redisplay in order to update new mode because it could be not computed before
901 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
903 Handle(AIS_InteractiveContext) aContext = AISContext();
904 if (aContext.IsNull())
907 AISObjectPtr aAISObj = getAISObject(theObject);
911 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
912 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
915 void XGUI_Displayer::deactivateSelectionFilters()
917 Handle(AIS_InteractiveContext) aContext = AISContext();
918 if (!myAndFilter.IsNull()) {
920 if (!aContext.IsNull()) {
921 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
922 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
923 for (; anIt.More() && !aFound; anIt.Next()) {
924 Handle(SelectMgr_Filter) aFilter = anIt.Value();
925 aFound = aFilter == myAndFilter;
928 aContext->RemoveFilter(myAndFilter);
930 myAndFilter.Nullify();
934 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
936 Handle(AIS_InteractiveContext) aContext = AISContext();
937 if (aContext.IsNull() || hasSelectionFilter(theFilter))
940 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
941 if (!aCompositeFilter.IsNull()) {
942 aCompositeFilter->Add(theFilter);
943 #ifdef DEBUG_SELECTION_FILTERS
944 int aCount = aCompositeFilter->StoredFilters().Extent();
945 qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
950 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
952 Handle(AIS_InteractiveContext) aContext = AISContext();
953 if (aContext.IsNull())
956 Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
957 if (!aCompositeFilter.IsNull() && aCompositeFilter->IsIn(theFilter)) {
958 aCompositeFilter->Remove(theFilter);
959 #ifdef DEBUG_SELECTION_FILTERS
960 int aCount = aCompositeFilter->StoredFilters().Extent();
961 qDebug(QString("removeSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
966 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
968 bool aFilterFound = false;
970 Handle(AIS_InteractiveContext) aContext = AISContext();
971 if (aContext.IsNull())
973 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
974 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
975 for (; aIt.More() && !aFilterFound; aIt.Next()) {
976 if (theFilter.Access() == aIt.Value().Access())
979 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
980 if (!aCompositeFilter.IsNull()) {
981 const SelectMgr_ListOfFilter& aStoredFilters = aCompositeFilter->StoredFilters();
982 for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
983 if (theFilter.Access() == aIt.Value().Access())
990 void XGUI_Displayer::removeFilters()
992 Handle(AIS_InteractiveContext) aContext = AISContext();
993 if (aContext.IsNull())
996 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
997 if (!aCompositeFilter.IsNull())
998 aCompositeFilter->Clear();
1001 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
1003 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
1004 foreach(ObjectPtr aObj, aDispList) {
1005 if (!theList.contains(aObj))
1008 foreach(ObjectPtr aObj, theList) {
1009 if (!isVisible(aObj))
1010 display(aObj, false);
1015 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
1017 if (!isVisible(theObject))
1020 AISObjectPtr aAISObj = getAISObject(theObject);
1021 if (aAISObj.get() == NULL)
1024 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1025 return ::canBeShaded(anAIS);
1028 bool XGUI_Displayer::activate(const Handle(AIS_InteractiveObject)& theIO,
1029 const QIntList& theModes,
1030 const bool theUpdateViewer) const
1032 Handle(AIS_InteractiveContext) aContext = AISContext();
1033 if (aContext.IsNull() || theIO.IsNull())
1036 bool isActivationChanged = false;
1037 // deactivate object in all modes, which are not in the list of activation
1038 // It seems that after the IO deactivation the selected state of the IO's owners
1039 // is modified in OCC(version: 6.8.0) and the selection of the object later is lost.
1040 // By this reason, the number of the IO deactivate is decreased and the object is deactivated
1041 // only if there is a difference in the current modes and the parameters modes.
1042 // If the selection problem happens again, it is possible to write a test scenario and create
1043 // a bug. The bug steps are the following:
1044 // Create two IO, activate them in 5 modes, select the first IO, deactivate 3 modes for both,
1045 // with clicked SHIFT select the second object. The result is the selection of the first IO is lost.
1046 TColStd_ListOfInteger aTColModes;
1047 aContext->ActivatedModes(theIO, aTColModes);
1048 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
1049 QIntList aModesActivatedForIO;
1050 bool isDeactivated = false;
1051 for (; itr.More(); itr.Next() ) {
1052 Standard_Integer aMode = itr.Value();
1053 if (!theModes.contains(aMode)) {
1054 deactivateAIS(theIO, aMode);
1055 isDeactivated = true;
1058 aModesActivatedForIO.append(aMode);
1061 if (isDeactivated) {
1062 // the selection from the previous activation modes should be cleared manually (#26172)
1063 theIO->ClearSelected();
1064 aContext->LocalContext()->ClearOutdatedSelection(theIO, true);
1065 // For performance issues
1066 //if (theUpdateViewer)
1068 isActivationChanged = true;
1071 // loading the interactive object allowing the decomposition
1072 if (aTColModes.IsEmpty()) {
1073 aContext->Load(theIO, -1, true);
1076 // trihedron AIS check should be after the AIS loading.
1077 // If it is not loaded, it is steel selectable in the viewer.
1078 Handle(AIS_Trihedron) aTrihedron;
1079 if (!isTrihedronActive())
1080 aTrihedron = Handle(AIS_Trihedron)::DownCast(theIO);
1081 if (aTrihedron.IsNull()) {
1082 // In order to clear active modes list
1083 if (theModes.size() == 0) {
1084 activateAIS(theIO, 0, theUpdateViewer);
1086 foreach(int aMode, theModes) {
1087 if (!aModesActivatedForIO.contains(aMode)) {
1088 activateAIS(theIO, aMode, theUpdateViewer);
1089 isActivationChanged = true;
1094 return isActivationChanged;
1097 bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
1099 AISObjectPtr anAISObj = getAISObject(theObject);
1100 // correct the result's color it it has the attribute
1101 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1103 // Customization of presentation
1104 GeomCustomPrsPtr aCustomPrs;
1105 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1106 if (aFeature.get() != NULL) {
1107 GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
1108 if (aCustPrs.get() != NULL)
1109 aCustomPrs = aCustPrs;
1111 if (aCustomPrs.get() == NULL) {
1112 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
1113 // we ignore presentable not customized objects
1114 if (aPrs.get() == NULL)
1115 aCustomPrs = myCustomPrs;
1117 bool isCustomized = aCustomPrs.get() &&
1118 aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
1119 return isCustomized;
1123 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject, const QColor& theColor, bool theUpdateViewer)
1125 if (!isVisible(theObject))
1128 AISObjectPtr anAISObj = getAISObject(theObject);
1130 anAISObj->getColor(aR, aG, aB);
1131 anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
1132 if (theUpdateViewer)
1134 return QColor(aR, aG, aB);
1137 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
1139 myResult2AISObjectMap[theObject] = theAIS;
1141 #ifdef DEBUG_DISPLAY
1142 std::ostringstream aPtrStr;
1143 aPtrStr << theObject.get();
1144 qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
1145 qDebug(getResult2AISObjectMapInfo().c_str());
1149 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
1151 QStringList aContent;
1152 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
1153 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
1154 std::ostringstream aPtrStr;
1155 aPtrStr << "aObj = " << aObj.get() << ":";
1156 aPtrStr << "anAIS = " << aAISObj.get() << ":";
1157 aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
1159 aContent.append(aPtrStr.str().c_str());
1161 return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
1162 arg(aContent.join("\n")).toStdString().c_str();
1165 void XGUI_Displayer::activateTrihedron(bool theIsActive)
1167 myIsTrihedronActive = theIsActive;
1168 if (!myIsTrihedronActive) {
1169 deactivateTrihedron(true);
1173 void XGUI_Displayer::displayTrihedron(bool theToDisplay) const
1175 Handle(AIS_InteractiveContext) aContext = AISContext();
1176 if (aContext.IsNull())
1179 Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
1182 if (!aContext->IsDisplayed(aTrihedron))
1183 aContext->Display(aTrihedron,
1185 -1 /* selection mode */,
1186 Standard_True /* update viewer*/,
1187 Standard_False /* allow decomposition */,
1188 AIS_DS_Displayed /* xdisplay status */);
1190 if (!isTrihedronActive())
1191 deactivateTrihedron(false);
1193 activate(aTrihedron, myActiveSelectionModes, false);
1195 deactivateTrihedron(false);
1196 //aContext->LocalContext()->ClearOutdatedSelection(aTrihedron, true);
1197 // the selection from the previous activation modes should be cleared manually (#26172)
1199 aContext->Erase(aTrihedron);