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),
103 myIsTrihedronActive(false)
105 enableUpdateViewer(true);
106 myCustomPrs = std::shared_ptr<GeomAPI_ICustomPrs>(new XGUI_CustomPrs());
109 XGUI_Displayer::~XGUI_Displayer()
113 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
115 return myResult2AISObjectMap.contains(theObject);
118 bool XGUI_Displayer::display(ObjectPtr theObject, bool theUpdateViewer)
120 bool aDisplayed = false;
121 if (isVisible(theObject)) {
122 #ifdef DEBUG_COMPOSILID_DISPLAY
123 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
124 if (aCompsolidResult.get()) {
125 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
126 ResultPtr aSubResult = aCompsolidResult->subResult(i);
127 if (aSubResult.get())
128 redisplay(aSubResult, false);
135 aDisplayed = redisplay(theObject, theUpdateViewer);
138 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
139 bool isShading = false;
140 if (aPrs.get() != NULL) {
141 anAIS = aPrs->getAISObject(anAIS);
143 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
144 if (aResult.get() != NULL) {
145 #ifdef DEBUG_COMPOSILID_DISPLAY
146 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
147 if (aCompsolidResult.get()) {
148 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
149 ResultPtr aSubResult = aCompsolidResult->subResult(i);
150 if (aSubResult.get())
151 display(aSubResult, false);
158 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
159 if (aShapePtr.get() != NULL) {
160 anAIS = AISObjectPtr(new GeomAPI_AISObject());
161 anAIS->setImpl(new Handle(AIS_InteractiveObject)(new ModuleBase_ResultPrs(aResult)));
162 //anAIS->createShape(aShapePtr);
165 #ifdef DEBUG_COMPOSILID_DISPLAY
171 aDisplayed = display(theObject, anAIS, isShading, theUpdateViewer);
176 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS)
178 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
179 if (!aShapePrs.IsNull()) {
180 TopoDS_Shape aShape = aShapePrs->Shape();
183 TopAbs_ShapeEnum aType = aShape.ShapeType();
184 if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
187 // Check that the presentation is not a sketch
188 Handle(ModuleBase_ResultPrs) aPrs = Handle(ModuleBase_ResultPrs)::DownCast(theAIS);
190 return !aPrs->isSketchMode();
197 bool XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
198 bool isShading, bool theUpdateViewer)
200 bool aDisplayed = false;
202 Handle(AIS_InteractiveContext) aContext = AISContext();
203 if (aContext.IsNull())
206 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
207 if (!anAISIO.IsNull()) {
208 appendResultObject(theObject, theAIS);
210 bool isCustomized = customizeObject(theObject);
212 int aDispMode = isShading? Shading : Wireframe;
214 anAISIO->Attributes()->SetFaceBoundaryDraw( Standard_True );
215 anAISIO->SetDisplayMode(aDispMode);
216 aContext->Display(anAISIO, aDispMode, 0, false, true, AIS_DS_Displayed);
219 emit objectDisplayed(theObject, theAIS);
220 activate(anAISIO, myActiveSelectionModes, theUpdateViewer);
221 // the fix from VPA for more suitable selection of sketcher lines
222 if(anAISIO->Width() > 1) {
223 for(int aModeIdx = 0; aModeIdx < myActiveSelectionModes.length(); ++aModeIdx) {
224 int aMode = myActiveSelectionModes.value(aModeIdx);
225 double aPrecision = (aMode == getSelectionMode(TopAbs_VERTEX))? 20 :
226 (anAISIO->Width() + 2);
227 aContext->SetSelectionSensitivity(anAISIO, aMode, aPrecision);
237 bool XGUI_Displayer::erase(ObjectPtr theObject, const bool theUpdateViewer)
239 bool aErased = false;
240 if (!isVisible(theObject))
243 Handle(AIS_InteractiveContext) aContext = AISContext();
244 if (aContext.IsNull())
247 AISObjectPtr anObject = myResult2AISObjectMap[theObject];
249 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
250 if (!anAIS.IsNull()) {
251 emit beforeObjectErase(theObject, anObject);
252 aContext->Remove(anAIS, theUpdateViewer);
256 myResult2AISObjectMap.remove(theObject);
259 std::ostringstream aPtrStr;
260 aPtrStr << theObject.get();
261 qDebug(QString("erase object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
262 qDebug(getResult2AISObjectMapInfo().c_str());
267 bool XGUI_Displayer::redisplay(ObjectPtr theObject, bool theUpdateViewer)
269 bool aRedisplayed = false;
270 if (!isVisible(theObject))
273 AISObjectPtr aAISObj = getAISObject(theObject);
274 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
276 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
278 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
280 aRedisplayed = erase(theObject, theUpdateViewer);
283 if (aAIS_Obj != aAISObj) {
284 appendResultObject(theObject, aAIS_Obj);
286 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
289 Handle(AIS_InteractiveContext) aContext = AISContext();
290 if (!aContext.IsNull() && !aAISIO.IsNull()) {
291 // Check that the visualized shape is the same and the redisplay is not necessary
292 // Redisplay of AIS object leads to this object selection compute and the selection
293 // in the browser is lost
294 // this check is not necessary anymore because the selection store/restore is realized
295 // before and after the values modification.
296 // Moreother, this check avoids customize and redisplay presentation if the presentable
297 // parameter is changed.
298 bool isEqualShapes = false;
299 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
300 if (aResult.get() != NULL) {
301 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(aAISIO);
302 if (!aShapePrs.IsNull()) {
303 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
304 if (aShapePtr.get()) {
305 const TopoDS_Shape& aOldShape = aShapePrs->Shape();
306 if (!aOldShape.IsNull())
307 isEqualShapes = aOldShape.IsEqual(aShapePtr->impl<TopoDS_Shape>());
311 // Customization of presentation
312 bool isCustomized = customizeObject(theObject);
313 #ifdef DEBUG_FEATURE_REDISPLAY
314 qDebug(QString("Redisplay: %1, isEqualShapes=%2, isCustomized=%3").
315 arg(!isEqualShapes || isCustomized).arg(isEqualShapes).arg(isCustomized).toStdString().c_str());
317 if (!isEqualShapes || isCustomized) {
318 aContext->Redisplay(aAISIO, false);
320 #ifdef DEBUG_FEATURE_REDISPLAY
321 qDebug(" Redisplay happens");
330 void XGUI_Displayer::redisplayObjects()
332 // redisplay objects visualized in the viewer
333 static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
334 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
335 QObjectPtrList aDisplayed = myWorkshop->displayer()->displayedObjects();
336 QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
337 for (; anIt != aLast; anIt++) {
338 aECreator->sendUpdated(*anIt, EVENT_DISP);
340 Events_Loop::loop()->flush(EVENT_DISP);
343 void XGUI_Displayer::deactivate(ObjectPtr theObject, const bool theUpdateViewer)
345 #ifdef DEBUG_DEACTIVATE
346 QString anInfoStr = ModuleBase_Tools::objectInfo(theObject);
347 qDebug(QString("deactivate: myActiveSelectionModes[%1]: %2, objects = ").
348 arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
350 toStdString().c_str());
352 Handle(AIS_InteractiveContext) aContext = AISContext();
353 if (!aContext.IsNull() && isVisible(theObject)) {
354 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
355 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
357 deactivateAIS(anAIS);
358 // the selection from the previous activation modes should be cleared manually (#26172)
359 aContext->LocalContext()->ClearOutdatedSelection(anAIS, true);
365 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
366 const bool theUpdateViewer)
368 //Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
369 //if (!aTrihedron.IsNull())
370 // deactivateAIS(aTrihedron);
372 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
373 for (; anIt != aLast; anIt++) {
374 deactivate(*anIt, false);
376 //VSV It seems that there is no necessity to update viewer on deactivation
377 //if (theUpdateViewer)
381 void XGUI_Displayer::getModesOfActivation(ObjectPtr theObject, QIntList& theModes)
383 Handle(AIS_InteractiveContext) aContext = AISContext();
384 if (aContext.IsNull() || !isVisible(theObject))
387 AISObjectPtr aAISObj = getAISObject(theObject);
389 if (aAISObj.get() != NULL) {
390 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
391 TColStd_ListOfInteger aTColModes;
392 aContext->ActivatedModes(anAISIO, aTColModes);
393 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
394 for (; itr.More(); itr.Next() ) {
395 theModes.append(itr.Value());
400 int XGUI_Displayer::getSelectionMode(int theShapeType)
402 return (theShapeType >= TopAbs_SHAPE)? theShapeType :
403 AIS_Shape::SelectionMode((TopAbs_ShapeEnum)theShapeType);
406 void XGUI_Displayer::activateObjects(const QIntList& theModes, const QObjectPtrList& theObjList,
407 const bool theUpdateViewer)
409 // Convert shape types to selection types
411 foreach(int aType, theModes) {
412 aModes.append(getSelectionMode(aType));
415 #ifdef DEBUG_ACTIVATE_OBJECTS
417 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
418 for (; anIt != aLast; ++anIt) {
419 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
421 QString anInfoStr = anInfo.join(", ");
423 qDebug(QString("activateObjects: aModes[%1] = %2, myActiveSelectionModes[%3] = %4, objects = %5").
424 arg(aModes.size()).arg(qIntListInfo(aModes)).
425 arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
427 toStdString().c_str());
429 // In order to avoid doblications of selection modes
431 foreach (int aMode, aModes) {
432 if (!aNewModes.contains(aMode))
433 aNewModes.append(aMode);
435 myActiveSelectionModes = aNewModes;
436 Handle(AIS_InteractiveContext) aContext = AISContext();
437 // Open local context if there is no one
438 if (aContext.IsNull() || !aContext->HasOpenedContext())
441 //aContext->UseDisplayedObjects();
442 //myUseExternalObjects = true;
444 Handle(AIS_InteractiveObject) anAISIO;
445 AIS_ListOfInteractive aPrsList;
446 //if (aObjList.isEmpty())
449 foreach(ObjectPtr aObj, theObjList) {
450 if (myResult2AISObjectMap.contains(aObj))
451 aPrsList.Append(myResult2AISObjectMap[aObj]->impl<Handle(AIS_InteractiveObject)>());
455 // Add trihedron because it has to partisipate in selection
456 Handle(AIS_InteractiveObject) aTrihedron;
457 if (isTrihedronActive()) {
458 aTrihedron = getTrihedron();
459 if (!aTrihedron.IsNull())
460 aPrsList.Append(aTrihedron);
462 if (aPrsList.Extent() == 0)
465 AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
466 bool isActivationChanged = false;
467 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
468 anAISIO = aLIt.Value();
469 if (activate(anAISIO, myActiveSelectionModes, false))
470 isActivationChanged = true;
472 if (!aTrihedron.IsNull()) {
473 foreach(int aMode, myActiveSelectionModes)
474 aContext->SetSelectionSensitivity(aTrihedron, aMode, 20);
476 // VSV It seems that there is no necessity to update viewer on activation
477 //if (theUpdateViewer && isActivationChanged)
481 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
483 Handle(AIS_InteractiveContext) aContext = AISContext();
484 if (aContext.IsNull() || !isVisible(theObject))
487 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
488 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
490 TColStd_ListOfInteger aModes;
491 aContext->ActivatedModes(anAIS, aModes);
492 return aModes.Extent() > 0;
494 void XGUI_Displayer::setSelected(const QList<ModuleBase_ViewerPrs>& theValues, bool theUpdateViewer)
496 Handle(AIS_InteractiveContext) aContext = AISContext();
497 if (aContext.IsNull())
499 if (aContext->HasOpenedContext()) {
500 aContext->UnhilightSelected();
501 aContext->ClearSelected();
502 foreach (ModuleBase_ViewerPrs aPrs, theValues) {
503 const TopoDS_Shape& aShape = aPrs.shape();
504 if (!aShape.IsNull()) {
505 aContext->AddOrRemoveSelected(aShape, false);
507 ObjectPtr anObject = aPrs.object();
508 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
509 if (aResult.get() && isVisible(aResult)) {
510 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
511 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
512 if (!anAIS.IsNull()) {
513 // The methods are replaced in order to provide multi-selection, e.g. restore selection
514 // by activating multi selector widget. It also gives an advantage that the multi
515 // selection in OB gives multi-selection in the viewer
516 //aContext->SetSelected(anAIS, false);
517 // The selection in the context was cleared, so the method sets the objects are selected
518 aContext->AddOrRemoveSelected(anAIS, false);
524 aContext->UnhilightCurrents();
525 aContext->ClearCurrents();
526 foreach (ModuleBase_ViewerPrs aPrs, theValues) {
527 ObjectPtr anObject = aPrs.object();
528 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
529 if (aResult.get() && isVisible(aResult)) {
530 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
531 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
533 aContext->SetCurrentObject(anAIS, false);
541 void XGUI_Displayer::clearSelected()
543 Handle(AIS_InteractiveContext) aContext = AISContext();
544 if (!aContext.IsNull()) {
545 aContext->UnhilightCurrents(false);
546 aContext->ClearSelected();
550 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
552 bool aErased = false;
553 Handle(AIS_InteractiveContext) aContext = AISContext();
554 if (!aContext.IsNull()) {
555 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
556 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
558 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
559 if (!anIO.IsNull()) {
560 emit beforeObjectErase(aObj, aAISObj);
561 aContext->Remove(anIO, false);
568 myResult2AISObjectMap.clear();
571 qDebug(getResult2AISObjectMapInfo().c_str());
576 #define DEACTVATE_COMP(TheComp) \
577 if (!TheComp.IsNull()) \
578 aContext->Deactivate(TheComp);
580 void XGUI_Displayer::deactivateTrihedron() const
582 Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
583 if (!aTrihedron.IsNull()) {
584 Handle(AIS_Trihedron) aTrie = Handle(AIS_Trihedron)::DownCast(aTrihedron);
585 Handle(AIS_InteractiveContext) aContext = AISContext();
586 aContext->Deactivate(aTrie);
587 DEACTVATE_COMP(aTrie->XAxis());
588 DEACTVATE_COMP(aTrie->YAxis());
589 DEACTVATE_COMP(aTrie->Axis());
590 DEACTVATE_COMP(aTrie->Position());
591 DEACTVATE_COMP(aTrie->XYPlane());
592 DEACTVATE_COMP(aTrie->XZPlane());
593 DEACTVATE_COMP(aTrie->YZPlane());
597 Handle(AIS_InteractiveObject) XGUI_Displayer::getTrihedron() const
599 Handle(AIS_InteractiveContext) aContext = AISContext();
600 if (!aContext.IsNull()) {
601 AIS_ListOfInteractive aList;
602 aContext->DisplayedObjects(aList, true);
603 AIS_ListIteratorOfListOfInteractive aIt;
604 for (aIt.Initialize(aList); aIt.More(); aIt.Next()) {
605 Handle(AIS_Trihedron) aTrihedron = Handle(AIS_Trihedron)::DownCast(aIt.Value());
606 if (!aTrihedron.IsNull()) {
611 return Handle(AIS_InteractiveObject)();
614 void XGUI_Displayer::openLocalContext()
616 Handle(AIS_InteractiveContext) aContext = AISContext();
617 // Open local context if there is no one
618 if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
619 // Preserve selected objects
620 //AIS_ListOfInteractive aAisList;
621 //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
622 // aAisList.Append(aContext->Current());
624 // get the filters from the global context and append them to the local context
625 // a list of filters in the global context is not cleared and should be cleared here
626 SelectMgr_ListOfFilter aFilters;
627 aFilters.Assign(aContext->Filters());
628 // it is important to remove the filters in the global context, because there is a code
629 // in the closeLocalContex, which restore the global context filters
630 aContext->RemoveFilters();
632 //aContext->ClearCurrents();
633 aContext->OpenLocalContext();
634 //deactivateTrihedron();
635 //aContext->NotUseDisplayedObjects();
637 //myUseExternalObjects = false;
639 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
640 for (;aIt.More(); aIt.Next()) {
641 aContext->AddFilter(aIt.Value());
644 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
645 //for(; aIt2.More(); aIt2.Next()) {
646 // aContext->SetSelected(aIt2.Value(), false);
651 void XGUI_Displayer::closeLocalContexts(const bool theUpdateViewer)
653 Handle(AIS_InteractiveContext) aContext = AISContext();
654 if (!aContext.IsNull() && aContext->HasOpenedContext()) {
655 // Preserve selected objects
656 //AIS_ListOfInteractive aAisList;
657 //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
658 // aAisList.Append(aContext->SelectedInteractive());
660 // get the filters from the local context and append them to the global context
661 // a list of filters in the local context is cleared
662 SelectMgr_ListOfFilter aFilters;
663 aFilters.Assign(aContext->Filters());
665 //aContext->ClearSelected();
666 aContext->CloseAllContexts(false);
668 // From the moment when the AIS_DS_Displayed flag is used in the Display of AIS object,
669 // this code is obsolete. It is temporaty commented and should be removed after
670 // the test campaign.
671 // Redisplay all object if they were displayed in localContext
672 /*Handle(AIS_InteractiveObject) aAISIO;
673 foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
674 aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
675 if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
676 aContext->Display(aAISIO, false);
677 aContext->SetDisplayMode(aAISIO, Shading, false);
681 // Append the filters from the local selection in the global selection context
682 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
683 for (;aIt.More(); aIt.Next()) {
684 Handle(SelectMgr_Filter) aFilter = aIt.Value();
685 aContext->AddFilter(aFilter);
690 //myUseExternalObjects = false;
693 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
694 //for(; aIt2.More(); aIt2.Next()) {
695 // if (aContext->IsDisplayed(aIt2.Value()))
696 // aContext->SetCurrentObject(aIt2.Value(), false);
701 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
704 if (myResult2AISObjectMap.contains(theObject))
705 anIO = myResult2AISObjectMap[theObject];
709 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
711 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
712 return getObject(aRefAIS);
715 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
718 foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
719 AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
720 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
726 if (!anObject.get()) {
727 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
728 if (!theIO.IsNull()) {
729 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
731 anObject = myWorkshop->module()->findPresentedObject(anAISObj);
736 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
738 bool aWasEnabled = myEnableUpdateViewer;
740 myEnableUpdateViewer = isEnabled;
741 if (myNeedUpdate && myEnableUpdateViewer) {
743 myNeedUpdate = false;
748 void XGUI_Displayer::updateViewer() const
750 Handle(AIS_InteractiveContext) aContext = AISContext();
751 if (!aContext.IsNull() && myEnableUpdateViewer) {
752 myWorkshop->viewer()->Zfitall();
753 aContext->UpdateCurrentViewer();
759 void XGUI_Displayer::activateAIS(const Handle(AIS_InteractiveObject)& theIO,
760 const int theMode, const bool theUpdateViewer) const
762 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
763 if (!aContext.IsNull()) {
764 aContext->Activate(theIO, theMode, theUpdateViewer);
766 #ifdef DEBUG_ACTIVATE_AIS
767 ObjectPtr anObject = getObject(theIO);
768 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
769 qDebug(QString("activateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
774 void XGUI_Displayer::deactivateAIS(const Handle(AIS_InteractiveObject)& theIO, const int theMode) const
776 Handle(AIS_InteractiveContext) aContext = AISContext();
777 if (!aContext.IsNull()) {
779 aContext->Deactivate(theIO);
781 aContext->Deactivate(theIO, theMode);
783 #ifdef DEBUG_DEACTIVATE_AIS
784 ObjectPtr anObject = getObject(theIO);
785 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
786 qDebug(QString("deactivateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
791 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
793 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
794 if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
795 aContext->OpenLocalContext();
796 if (!isTrihedronActive())
797 deactivateTrihedron();
798 aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
799 aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
804 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
806 Handle(AIS_InteractiveContext) aContext = AISContext();
807 if (!aContext.IsNull() && myAndFilter.IsNull()) {
808 myAndFilter = new SelectMgr_AndFilter();
809 aContext->AddFilter(myAndFilter);
814 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
815 bool theUpdateViewer)
817 bool aDisplayed = false;
818 Handle(AIS_InteractiveContext) aContext = AISContext();
819 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
820 if (!aContext.IsNull() && !anAISIO.IsNull()) {
821 aContext->Display(anAISIO, 0/*wireframe*/, 0, theUpdateViewer, true, AIS_DS_Displayed);
823 aContext->Deactivate(anAISIO);
824 aContext->Load(anAISIO);
825 if (toActivateInSelectionModes) {
826 if (aContext->HasOpenedContext()) {
827 if (myActiveSelectionModes.size() == 0)
828 activateAIS(anAISIO, 0, theUpdateViewer);
830 foreach(int aMode, myActiveSelectionModes) {
831 activateAIS(anAISIO, aMode, theUpdateViewer);
840 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
842 bool aErased = false;
843 Handle(AIS_InteractiveContext) aContext = AISContext();
844 if (!aContext.IsNull()) {
845 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
846 if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
847 aContext->Remove(anAISIO, theUpdateViewer);
855 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
857 if (theMode == NoMode)
860 Handle(AIS_InteractiveContext) aContext = AISContext();
861 if (aContext.IsNull())
864 AISObjectPtr aAISObj = getAISObject(theObject);
868 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
869 aContext->SetDisplayMode(aAISIO, theMode, false);
870 // Redisplay in order to update new mode because it could be not computed before
875 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
877 Handle(AIS_InteractiveContext) aContext = AISContext();
878 if (aContext.IsNull())
881 AISObjectPtr aAISObj = getAISObject(theObject);
885 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
886 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
889 void XGUI_Displayer::deactivateSelectionFilters()
891 Handle(AIS_InteractiveContext) aContext = AISContext();
892 if (!myAndFilter.IsNull()) {
894 if (!aContext.IsNull()) {
895 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
896 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
897 for (; anIt.More() && !aFound; anIt.Next()) {
898 Handle(SelectMgr_Filter) aFilter = anIt.Value();
899 aFound = aFilter == myAndFilter;
902 aContext->RemoveFilter(myAndFilter);
904 myAndFilter.Nullify();
908 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
910 Handle(AIS_InteractiveContext) aContext = AISContext();
911 if (aContext.IsNull() || hasSelectionFilter(theFilter))
914 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
915 if (!aCompositeFilter.IsNull()) {
916 aCompositeFilter->Add(theFilter);
917 #ifdef DEBUG_SELECTION_FILTERS
918 int aCount = aCompositeFilter->StoredFilters().Extent();
919 qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
924 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
926 Handle(AIS_InteractiveContext) aContext = AISContext();
927 if (aContext.IsNull())
930 Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
931 if (!aCompositeFilter.IsNull() && aCompositeFilter->IsIn(theFilter)) {
932 aCompositeFilter->Remove(theFilter);
933 #ifdef DEBUG_SELECTION_FILTERS
934 int aCount = aCompositeFilter->StoredFilters().Extent();
935 qDebug(QString("removeSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
940 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
942 bool aFilterFound = false;
944 Handle(AIS_InteractiveContext) aContext = AISContext();
945 if (aContext.IsNull())
947 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
948 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
949 for (; aIt.More() && !aFilterFound; aIt.Next()) {
950 if (theFilter.Access() == aIt.Value().Access())
953 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
954 if (!aCompositeFilter.IsNull()) {
955 const SelectMgr_ListOfFilter& aStoredFilters = aCompositeFilter->StoredFilters();
956 for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
957 if (theFilter.Access() == aIt.Value().Access())
964 void XGUI_Displayer::removeFilters()
966 Handle(AIS_InteractiveContext) aContext = AISContext();
967 if (aContext.IsNull())
970 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
971 if (!aCompositeFilter.IsNull())
972 aCompositeFilter->Clear();
975 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
977 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
978 foreach(ObjectPtr aObj, aDispList) {
979 if (!theList.contains(aObj))
982 foreach(ObjectPtr aObj, theList) {
983 if (!isVisible(aObj))
984 display(aObj, false);
989 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
991 if (!isVisible(theObject))
994 AISObjectPtr aAISObj = getAISObject(theObject);
995 if (aAISObj.get() == NULL)
998 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
999 return ::canBeShaded(anAIS);
1002 bool XGUI_Displayer::activate(const Handle(AIS_InteractiveObject)& theIO,
1003 const QIntList& theModes,
1004 const bool theUpdateViewer) const
1006 Handle(AIS_InteractiveContext) aContext = AISContext();
1007 if (aContext.IsNull() || theIO.IsNull())
1010 bool isActivationChanged = false;
1011 // deactivate object in all modes, which are not in the list of activation
1012 // It seems that after the IO deactivation the selected state of the IO's owners
1013 // is modified in OCC(version: 6.8.0) and the selection of the object later is lost.
1014 // By this reason, the number of the IO deactivate is decreased and the object is deactivated
1015 // only if there is a difference in the current modes and the parameters modes.
1016 // If the selection problem happens again, it is possible to write a test scenario and create
1017 // a bug. The bug steps are the following:
1018 // Create two IO, activate them in 5 modes, select the first IO, deactivate 3 modes for both,
1019 // with clicked SHIFT select the second object. The result is the selection of the first IO is lost.
1020 TColStd_ListOfInteger aTColModes;
1021 aContext->ActivatedModes(theIO, aTColModes);
1022 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
1023 QIntList aModesActivatedForIO;
1024 bool isDeactivated = false;
1025 for (; itr.More(); itr.Next() ) {
1026 Standard_Integer aMode = itr.Value();
1027 if (!theModes.contains(aMode)) {
1028 deactivateAIS(theIO, aMode);
1029 isDeactivated = true;
1032 aModesActivatedForIO.append(aMode);
1035 if (isDeactivated) {
1036 // the selection from the previous activation modes should be cleared manually (#26172)
1037 theIO->ClearSelected();
1038 aContext->LocalContext()->ClearOutdatedSelection(theIO, true);
1039 // For performance issues
1040 //if (theUpdateViewer)
1042 isActivationChanged = true;
1045 // loading the interactive object allowing the decomposition
1046 if (aTColModes.IsEmpty()) {
1047 aContext->Load(theIO, -1, true);
1050 // trihedron AIS check should be after the AIS loading.
1051 // If it is not loaded, it is steel selectable in the viewer.
1052 Handle(AIS_Trihedron) aTrihedron;
1053 if (!isTrihedronActive())
1054 aTrihedron = Handle(AIS_Trihedron)::DownCast(theIO);
1055 if (aTrihedron.IsNull()) {
1056 // In order to clear active modes list
1057 if (theModes.size() == 0) {
1058 activateAIS(theIO, 0, theUpdateViewer);
1060 foreach(int aMode, theModes) {
1061 if (!aModesActivatedForIO.contains(aMode)) {
1062 activateAIS(theIO, aMode, theUpdateViewer);
1063 isActivationChanged = true;
1068 return isActivationChanged;
1071 bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
1073 AISObjectPtr anAISObj = getAISObject(theObject);
1074 // correct the result's color it it has the attribute
1075 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1077 // Customization of presentation
1078 GeomCustomPrsPtr aCustomPrs;
1079 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1080 if (aFeature.get() != NULL) {
1081 GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
1082 if (aCustPrs.get() != NULL)
1083 aCustomPrs = aCustPrs;
1085 if (aCustomPrs.get() == NULL) {
1086 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
1087 // we ignore presentable not customized objects
1088 if (aPrs.get() == NULL)
1089 aCustomPrs = myCustomPrs;
1091 bool isCustomized = aCustomPrs.get() &&
1092 aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
1093 return isCustomized;
1097 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject, const QColor& theColor, bool theUpdateViewer)
1099 if (!isVisible(theObject))
1102 AISObjectPtr anAISObj = getAISObject(theObject);
1104 anAISObj->getColor(aR, aG, aB);
1105 anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
1106 if (theUpdateViewer)
1108 return QColor(aR, aG, aB);
1111 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
1113 myResult2AISObjectMap[theObject] = theAIS;
1115 #ifdef DEBUG_DISPLAY
1116 std::ostringstream aPtrStr;
1117 aPtrStr << theObject.get();
1118 qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
1119 qDebug(getResult2AISObjectMapInfo().c_str());
1123 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
1125 QStringList aContent;
1126 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
1127 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
1128 std::ostringstream aPtrStr;
1129 aPtrStr << "aObj = " << aObj.get() << ":";
1130 aPtrStr << "anAIS = " << aAISObj.get() << ":";
1131 aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
1133 aContent.append(aPtrStr.str().c_str());
1135 return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
1136 arg(aContent.join("\n")).toStdString().c_str();
1139 void XGUI_Displayer::activateTrihedron(bool theIsActive)
1141 myIsTrihedronActive = theIsActive;
1142 if (!myIsTrihedronActive) {
1143 deactivateTrihedron();