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), myEnableUpdateViewer(true), myNeedUpdate(false),
107 myIsTrihedronActive(false)
109 enableUpdateViewer(true);
110 myCustomPrs = std::shared_ptr<GeomAPI_ICustomPrs>(new XGUI_CustomPrs());
113 XGUI_Displayer::~XGUI_Displayer()
117 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
119 return myResult2AISObjectMap.contains(theObject);
122 bool XGUI_Displayer::display(ObjectPtr theObject, bool theUpdateViewer)
124 bool aDisplayed = false;
125 if (isVisible(theObject)) {
126 #ifdef DEBUG_COMPOSILID_DISPLAY
127 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
128 if (aCompsolidResult.get()) {
129 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
130 ResultPtr aSubResult = aCompsolidResult->subResult(i);
131 if (aSubResult.get())
132 redisplay(aSubResult, false);
139 aDisplayed = redisplay(theObject, theUpdateViewer);
142 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
143 bool isShading = false;
144 if (aPrs.get() != NULL) {
145 anAIS = aPrs->getAISObject(anAIS);
147 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
148 if (aResult.get() != NULL) {
149 #ifdef DEBUG_COMPOSILID_DISPLAY
150 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
151 if (aCompsolidResult.get()) {
152 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
153 ResultPtr aSubResult = aCompsolidResult->subResult(i);
154 if (aSubResult.get())
155 display(aSubResult, false);
162 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
163 if (aShapePtr.get() != NULL) {
164 anAIS = AISObjectPtr(new GeomAPI_AISObject());
165 anAIS->setImpl(new Handle(AIS_InteractiveObject)(new ModuleBase_ResultPrs(aResult)));
166 //anAIS->createShape(aShapePtr);
169 #ifdef DEBUG_COMPOSILID_DISPLAY
175 aDisplayed = display(theObject, anAIS, isShading, theUpdateViewer);
180 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS)
182 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
183 if (!aShapePrs.IsNull()) {
184 TopoDS_Shape aShape = aShapePrs->Shape();
187 TopAbs_ShapeEnum aType = aShape.ShapeType();
188 if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
191 // Check that the presentation is not a sketch
192 Handle(ModuleBase_ResultPrs) aPrs = Handle(ModuleBase_ResultPrs)::DownCast(theAIS);
194 return !aPrs->isSketchMode();
201 bool XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
202 bool isShading, bool theUpdateViewer)
204 bool aDisplayed = false;
206 Handle(AIS_InteractiveContext) aContext = AISContext();
207 if (aContext.IsNull())
210 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
211 if (!anAISIO.IsNull()) {
212 appendResultObject(theObject, theAIS);
214 bool isCustomized = customizeObject(theObject);
216 int aDispMode = isShading? Shading : Wireframe;
218 anAISIO->Attributes()->SetFaceBoundaryDraw( Standard_True );
219 anAISIO->SetDisplayMode(aDispMode);
220 aContext->Display(anAISIO, aDispMode, 0, false, true, AIS_DS_Displayed);
223 emit objectDisplayed(theObject, theAIS);
224 activate(anAISIO, myActiveSelectionModes, theUpdateViewer);
225 // the fix from VPA for more suitable selection of sketcher lines
226 if(anAISIO->Width() > 1) {
227 for(int aModeIdx = 0; aModeIdx < myActiveSelectionModes.length(); ++aModeIdx) {
228 int aMode = myActiveSelectionModes.value(aModeIdx);
229 double aPrecision = (aMode == getSelectionMode(TopAbs_VERTEX))? 20 :
230 (anAISIO->Width() + 2);
231 aContext->SetSelectionSensitivity(anAISIO, aMode, aPrecision);
241 bool XGUI_Displayer::erase(ObjectPtr theObject, const bool theUpdateViewer)
243 bool aErased = false;
244 if (!isVisible(theObject))
247 Handle(AIS_InteractiveContext) aContext = AISContext();
248 if (aContext.IsNull())
251 AISObjectPtr anObject = myResult2AISObjectMap[theObject];
253 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
254 if (!anAIS.IsNull()) {
255 emit beforeObjectErase(theObject, anObject);
256 aContext->Remove(anAIS, theUpdateViewer);
260 myResult2AISObjectMap.remove(theObject);
263 std::ostringstream aPtrStr;
264 aPtrStr << theObject.get();
265 qDebug(QString("erase object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
266 qDebug(getResult2AISObjectMapInfo().c_str());
271 bool XGUI_Displayer::redisplay(ObjectPtr theObject, bool theUpdateViewer)
273 bool aRedisplayed = false;
274 if (!isVisible(theObject))
277 AISObjectPtr aAISObj = getAISObject(theObject);
278 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
280 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
282 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
284 aRedisplayed = erase(theObject, theUpdateViewer);
287 if (aAIS_Obj != aAISObj) {
288 appendResultObject(theObject, aAIS_Obj);
290 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
293 Handle(AIS_InteractiveContext) aContext = AISContext();
294 if (!aContext.IsNull() && !aAISIO.IsNull()) {
295 // Check that the visualized shape is the same and the redisplay is not necessary
296 // Redisplay of AIS object leads to this object selection compute and the selection
297 // in the browser is lost
298 // this check is not necessary anymore because the selection store/restore is realized
299 // before and after the values modification.
300 // Moreother, this check avoids customize and redisplay presentation if the presentable
301 // parameter is changed.
302 bool isEqualShapes = false;
303 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
304 if (aResult.get() != NULL) {
305 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(aAISIO);
306 if (!aShapePrs.IsNull()) {
307 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
308 if (aShapePtr.get()) {
309 const TopoDS_Shape& aOldShape = aShapePrs->Shape();
310 if (!aOldShape.IsNull())
311 isEqualShapes = aOldShape.IsEqual(aShapePtr->impl<TopoDS_Shape>());
315 // Customization of presentation
316 bool isCustomized = customizeObject(theObject);
317 #ifdef DEBUG_FEATURE_REDISPLAY
318 qDebug(QString("Redisplay: %1, isEqualShapes=%2, isCustomized=%3").
319 arg(!isEqualShapes || isCustomized).arg(isEqualShapes).arg(isCustomized).toStdString().c_str());
321 if (!isEqualShapes || isCustomized) {
322 aContext->Redisplay(aAISIO, false);
324 #ifdef DEBUG_FEATURE_REDISPLAY
325 qDebug(" Redisplay happens");
334 void XGUI_Displayer::redisplayObjects()
336 // redisplay objects visualized in the viewer
337 static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
338 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
339 QObjectPtrList aDisplayed = myWorkshop->displayer()->displayedObjects();
340 QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
341 for (; anIt != aLast; anIt++) {
342 aECreator->sendUpdated(*anIt, EVENT_DISP);
344 Events_Loop::loop()->flush(EVENT_DISP);
347 void XGUI_Displayer::deactivate(ObjectPtr theObject, const bool theUpdateViewer)
349 #ifdef DEBUG_DEACTIVATE
350 QString anInfoStr = ModuleBase_Tools::objectInfo(theObject);
351 qDebug(QString("deactivate: myActiveSelectionModes[%1]: %2, objects = ").
352 arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
354 toStdString().c_str());
356 Handle(AIS_InteractiveContext) aContext = AISContext();
357 if (!aContext.IsNull() && isVisible(theObject)) {
358 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
359 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
361 deactivateAIS(anAIS);
362 // the selection from the previous activation modes should be cleared manually (#26172)
363 aContext->LocalContext()->ClearOutdatedSelection(anAIS, true);
369 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
370 const bool theUpdateViewer)
372 //Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
373 //if (!aTrihedron.IsNull())
374 // deactivateAIS(aTrihedron);
376 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
377 for (; anIt != aLast; anIt++) {
378 deactivate(*anIt, false);
380 //VSV It seems that there is no necessity to update viewer on deactivation
381 //if (theUpdateViewer)
385 void XGUI_Displayer::getModesOfActivation(ObjectPtr theObject, QIntList& theModes)
387 Handle(AIS_InteractiveContext) aContext = AISContext();
388 if (aContext.IsNull() || !isVisible(theObject))
391 AISObjectPtr aAISObj = getAISObject(theObject);
393 if (aAISObj.get() != NULL) {
394 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
395 TColStd_ListOfInteger aTColModes;
396 aContext->ActivatedModes(anAISIO, aTColModes);
397 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
398 for (; itr.More(); itr.Next() ) {
399 theModes.append(itr.Value());
404 int XGUI_Displayer::getSelectionMode(int theShapeType)
406 return (theShapeType >= TopAbs_SHAPE)? theShapeType :
407 AIS_Shape::SelectionMode((TopAbs_ShapeEnum)theShapeType);
410 void XGUI_Displayer::activateObjects(const QIntList& theModes, const QObjectPtrList& theObjList,
411 const bool theUpdateViewer)
413 // Convert shape types to selection types
415 foreach(int aType, theModes) {
416 aModes.append(getSelectionMode(aType));
419 #ifdef DEBUG_ACTIVATE_OBJECTS
421 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
422 for (; anIt != aLast; ++anIt) {
423 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
425 QString anInfoStr = anInfo.join(", ");
427 qDebug(QString("activateObjects: aModes[%1] = %2, myActiveSelectionModes[%3] = %4, objects = %5").
428 arg(aModes.size()).arg(qIntListInfo(aModes)).
429 arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
431 toStdString().c_str());
433 // In order to avoid doblications of selection modes
435 foreach (int aMode, aModes) {
436 if (!aNewModes.contains(aMode))
437 aNewModes.append(aMode);
439 myActiveSelectionModes = aNewModes;
440 Handle(AIS_InteractiveContext) aContext = AISContext();
441 // Open local context if there is no one
442 if (aContext.IsNull() || !aContext->HasOpenedContext())
445 //aContext->UseDisplayedObjects();
446 //myUseExternalObjects = true;
448 Handle(AIS_InteractiveObject) anAISIO;
449 AIS_ListOfInteractive aPrsList;
450 //if (aObjList.isEmpty())
453 foreach(ObjectPtr aObj, theObjList) {
454 if (myResult2AISObjectMap.contains(aObj))
455 aPrsList.Append(myResult2AISObjectMap[aObj]->impl<Handle(AIS_InteractiveObject)>());
459 // Add trihedron because it has to partisipate in selection
460 Handle(AIS_InteractiveObject) aTrihedron;
461 if (isTrihedronActive()) {
462 aTrihedron = getTrihedron();
463 if (!aTrihedron.IsNull() && aContext->IsDisplayed(aTrihedron))
464 aPrsList.Append(aTrihedron);
466 if (aPrsList.Extent() == 0)
469 AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
470 bool isActivationChanged = false;
471 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
472 anAISIO = aLIt.Value();
473 if (activate(anAISIO, myActiveSelectionModes, false))
474 isActivationChanged = true;
476 if (!aTrihedron.IsNull()) {
477 foreach(int aMode, myActiveSelectionModes)
478 aContext->SetSelectionSensitivity(aTrihedron, aMode, 20);
480 // VSV It seems that there is no necessity to update viewer on activation
481 //if (theUpdateViewer && isActivationChanged)
485 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
487 Handle(AIS_InteractiveContext) aContext = AISContext();
488 if (aContext.IsNull() || !isVisible(theObject))
491 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
492 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
494 TColStd_ListOfInteger aModes;
495 aContext->ActivatedModes(anAIS, aModes);
496 return aModes.Extent() > 0;
498 void XGUI_Displayer::setSelected(const QList<ModuleBase_ViewerPrs>& theValues, bool theUpdateViewer)
500 Handle(AIS_InteractiveContext) aContext = AISContext();
501 if (aContext.IsNull())
503 if (aContext->HasOpenedContext()) {
504 aContext->UnhilightSelected(false);
505 aContext->ClearSelected(false);
506 foreach (ModuleBase_ViewerPrs aPrs, theValues) {
507 const TopoDS_Shape& aShape = aPrs.shape();
508 if (!aShape.IsNull()) {
509 aContext->AddOrRemoveSelected(aShape, false);
511 ObjectPtr anObject = aPrs.object();
512 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
513 if (aResult.get() && isVisible(aResult)) {
514 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
515 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
516 if (!anAIS.IsNull()) {
517 // The methods are replaced in order to provide multi-selection, e.g. restore selection
518 // by activating multi selector widget. It also gives an advantage that the multi
519 // selection in OB gives multi-selection in the viewer
520 //aContext->SetSelected(anAIS, false);
521 // The selection in the context was cleared, so the method sets the objects are selected
522 aContext->AddOrRemoveSelected(anAIS, false);
528 aContext->UnhilightCurrents(false);
529 aContext->ClearCurrents(false);
530 foreach (ModuleBase_ViewerPrs aPrs, theValues) {
531 ObjectPtr anObject = aPrs.object();
532 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
533 if (aResult.get() && isVisible(aResult)) {
534 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
535 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
537 aContext->SetCurrentObject(anAIS, false);
545 void XGUI_Displayer::clearSelected()
547 Handle(AIS_InteractiveContext) aContext = AISContext();
548 if (!aContext.IsNull()) {
549 aContext->UnhilightCurrents(false);
550 aContext->ClearSelected();
554 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
556 bool aErased = false;
557 Handle(AIS_InteractiveContext) aContext = AISContext();
558 if (!aContext.IsNull()) {
559 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
560 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
562 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
563 if (!anIO.IsNull()) {
564 emit beforeObjectErase(aObj, aAISObj);
565 aContext->Remove(anIO, false);
572 myResult2AISObjectMap.clear();
575 qDebug(getResult2AISObjectMapInfo().c_str());
580 void deactivateObject(Handle(AIS_InteractiveContext) theContext,
581 Handle(AIS_InteractiveObject) theObject,
582 const bool theClear = true)
584 if (!theObject.IsNull()) {
585 theContext->Deactivate(theObject);
587 //theObject->ClearSelected();
588 // theContext->LocalContext()->ClearOutdatedSelection(theObject, true);
593 void XGUI_Displayer::deactivateTrihedron(const bool theUpdateViewer) const
595 Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
596 Handle(AIS_InteractiveContext) aContext = AISContext();
597 if (!aTrihedron.IsNull() && aContext->IsDisplayed(aTrihedron)) {
598 Handle(AIS_Trihedron) aTrie = Handle(AIS_Trihedron)::DownCast(aTrihedron);
599 deactivateObject(aContext, aTrie);
601 /// #1136 hidden axis are selected in sketch
602 /// workaround for Cascade: there is a crash in AIS_LocalContext::ClearOutdatedSelection
603 /// for Position AIS object in SelectionModes.
604 deactivateObject(aContext, aTrie->XAxis());
605 deactivateObject(aContext, aTrie->YAxis());
606 deactivateObject(aContext, aTrie->Axis());
607 deactivateObject(aContext, aTrie->Position());
609 deactivateObject(aContext, aTrie->XYPlane());
610 deactivateObject(aContext, aTrie->XZPlane());
611 deactivateObject(aContext, aTrie->YZPlane());
618 Handle(AIS_InteractiveObject) XGUI_Displayer::getTrihedron() const
620 return myWorkshop->viewer()->trihedron();
623 void XGUI_Displayer::openLocalContext()
625 Handle(AIS_InteractiveContext) aContext = AISContext();
626 // Open local context if there is no one
627 if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
628 // Preserve selected objects
629 //AIS_ListOfInteractive aAisList;
630 //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
631 // aAisList.Append(aContext->Current());
633 // get the filters from the global context and append them to the local context
634 // a list of filters in the global context is not cleared and should be cleared here
635 SelectMgr_ListOfFilter aFilters;
636 aFilters.Assign(aContext->Filters());
637 // it is important to remove the filters in the global context, because there is a code
638 // in the closeLocalContex, which restore the global context filters
639 aContext->RemoveFilters();
641 //aContext->ClearCurrents();
642 aContext->OpenLocalContext();
643 //deactivateTrihedron();
644 //aContext->NotUseDisplayedObjects();
646 //myUseExternalObjects = false;
648 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
649 for (;aIt.More(); aIt.Next()) {
650 aContext->AddFilter(aIt.Value());
653 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
654 //for(; aIt2.More(); aIt2.Next()) {
655 // aContext->SetSelected(aIt2.Value(), false);
660 void XGUI_Displayer::closeLocalContexts(const bool theUpdateViewer)
662 Handle(AIS_InteractiveContext) aContext = AISContext();
663 if (!aContext.IsNull() && aContext->HasOpenedContext()) {
664 // Preserve selected objects
665 //AIS_ListOfInteractive aAisList;
666 //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
667 // aAisList.Append(aContext->SelectedInteractive());
669 // get the filters from the local context and append them to the global context
670 // a list of filters in the local context is cleared
671 SelectMgr_ListOfFilter aFilters;
672 aFilters.Assign(aContext->Filters());
674 //aContext->ClearSelected();
675 aContext->CloseAllContexts(false);
677 // From the moment when the AIS_DS_Displayed flag is used in the Display of AIS object,
678 // this code is obsolete. It is temporaty commented and should be removed after
679 // the test campaign.
680 // Redisplay all object if they were displayed in localContext
681 /*Handle(AIS_InteractiveObject) aAISIO;
682 foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
683 aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
684 if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
685 aContext->Display(aAISIO, false);
686 aContext->SetDisplayMode(aAISIO, Shading, false);
690 // Append the filters from the local selection in the global selection context
691 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
692 for (;aIt.More(); aIt.Next()) {
693 Handle(SelectMgr_Filter) aFilter = aIt.Value();
694 aContext->AddFilter(aFilter);
699 //myUseExternalObjects = false;
702 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
703 //for(; aIt2.More(); aIt2.Next()) {
704 // if (aContext->IsDisplayed(aIt2.Value()))
705 // aContext->SetCurrentObject(aIt2.Value(), false);
710 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
713 if (myResult2AISObjectMap.contains(theObject))
714 anIO = myResult2AISObjectMap[theObject];
718 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
720 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
721 return getObject(aRefAIS);
724 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
727 foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
728 AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
729 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
735 if (!anObject.get()) {
736 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
737 if (!theIO.IsNull()) {
738 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
740 anObject = myWorkshop->module()->findPresentedObject(anAISObj);
745 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
747 bool aWasEnabled = myEnableUpdateViewer;
749 myEnableUpdateViewer = isEnabled;
750 if (myNeedUpdate && myEnableUpdateViewer) {
752 myNeedUpdate = false;
757 void XGUI_Displayer::updateViewer() const
759 Handle(AIS_InteractiveContext) aContext = AISContext();
760 if (!aContext.IsNull() && myEnableUpdateViewer) {
761 myWorkshop->viewer()->Zfitall();
762 aContext->UpdateCurrentViewer();
768 void XGUI_Displayer::activateAIS(const Handle(AIS_InteractiveObject)& theIO,
769 const int theMode, const bool theUpdateViewer) const
771 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
772 if (!aContext.IsNull()) {
773 aContext->Activate(theIO, theMode, theUpdateViewer);
775 #ifdef DEBUG_ACTIVATE_AIS
776 ObjectPtr anObject = getObject(theIO);
777 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
778 qDebug(QString("activateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
783 void XGUI_Displayer::deactivateAIS(const Handle(AIS_InteractiveObject)& theIO, const int theMode) const
785 Handle(AIS_InteractiveContext) aContext = AISContext();
786 if (!aContext.IsNull()) {
788 aContext->Deactivate(theIO);
790 aContext->Deactivate(theIO, theMode);
792 #ifdef DEBUG_DEACTIVATE_AIS
793 ObjectPtr anObject = getObject(theIO);
794 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
795 qDebug(QString("deactivateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
800 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
802 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
803 if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
804 aContext->OpenLocalContext();
805 if (!isTrihedronActive())
806 deactivateTrihedron(true);
807 aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
808 aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
813 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
815 Handle(AIS_InteractiveContext) aContext = AISContext();
816 if (!aContext.IsNull() && myAndFilter.IsNull()) {
817 myAndFilter = new SelectMgr_AndFilter();
818 aContext->AddFilter(myAndFilter);
823 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
824 bool theUpdateViewer)
826 bool aDisplayed = false;
827 Handle(AIS_InteractiveContext) aContext = AISContext();
828 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
829 if (!aContext.IsNull() && !anAISIO.IsNull()) {
830 aContext->Display(anAISIO, 0/*wireframe*/, 0, theUpdateViewer, true, AIS_DS_Displayed);
832 aContext->Deactivate(anAISIO);
833 aContext->Load(anAISIO);
834 if (toActivateInSelectionModes) {
835 if (aContext->HasOpenedContext()) {
836 if (myActiveSelectionModes.size() == 0)
837 activateAIS(anAISIO, 0, theUpdateViewer);
839 foreach(int aMode, myActiveSelectionModes) {
840 activateAIS(anAISIO, aMode, theUpdateViewer);
849 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
851 bool aErased = false;
852 Handle(AIS_InteractiveContext) aContext = AISContext();
853 if (!aContext.IsNull()) {
854 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
855 if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
856 aContext->Remove(anAISIO, theUpdateViewer);
864 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
866 if (theMode == NoMode)
869 Handle(AIS_InteractiveContext) aContext = AISContext();
870 if (aContext.IsNull())
873 AISObjectPtr aAISObj = getAISObject(theObject);
877 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
878 aContext->SetDisplayMode(aAISIO, theMode, false);
879 // Redisplay in order to update new mode because it could be not computed before
884 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
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 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
898 void XGUI_Displayer::deactivateSelectionFilters()
900 Handle(AIS_InteractiveContext) aContext = AISContext();
901 if (!myAndFilter.IsNull()) {
903 if (!aContext.IsNull()) {
904 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
905 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
906 for (; anIt.More() && !aFound; anIt.Next()) {
907 Handle(SelectMgr_Filter) aFilter = anIt.Value();
908 aFound = aFilter == myAndFilter;
911 aContext->RemoveFilter(myAndFilter);
913 myAndFilter.Nullify();
917 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
919 Handle(AIS_InteractiveContext) aContext = AISContext();
920 if (aContext.IsNull() || hasSelectionFilter(theFilter))
923 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
924 if (!aCompositeFilter.IsNull()) {
925 aCompositeFilter->Add(theFilter);
926 #ifdef DEBUG_SELECTION_FILTERS
927 int aCount = aCompositeFilter->StoredFilters().Extent();
928 qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
933 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
935 Handle(AIS_InteractiveContext) aContext = AISContext();
936 if (aContext.IsNull())
939 Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
940 if (!aCompositeFilter.IsNull() && aCompositeFilter->IsIn(theFilter)) {
941 aCompositeFilter->Remove(theFilter);
942 #ifdef DEBUG_SELECTION_FILTERS
943 int aCount = aCompositeFilter->StoredFilters().Extent();
944 qDebug(QString("removeSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
949 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
951 bool aFilterFound = false;
953 Handle(AIS_InteractiveContext) aContext = AISContext();
954 if (aContext.IsNull())
956 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
957 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
958 for (; aIt.More() && !aFilterFound; aIt.Next()) {
959 if (theFilter.Access() == aIt.Value().Access())
962 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
963 if (!aCompositeFilter.IsNull()) {
964 const SelectMgr_ListOfFilter& aStoredFilters = aCompositeFilter->StoredFilters();
965 for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
966 if (theFilter.Access() == aIt.Value().Access())
973 void XGUI_Displayer::removeFilters()
975 Handle(AIS_InteractiveContext) aContext = AISContext();
976 if (aContext.IsNull())
979 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
980 if (!aCompositeFilter.IsNull())
981 aCompositeFilter->Clear();
984 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
986 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
987 foreach(ObjectPtr aObj, aDispList) {
988 if (!theList.contains(aObj))
991 foreach(ObjectPtr aObj, theList) {
992 if (!isVisible(aObj))
993 display(aObj, false);
998 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
1000 if (!isVisible(theObject))
1003 AISObjectPtr aAISObj = getAISObject(theObject);
1004 if (aAISObj.get() == NULL)
1007 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1008 return ::canBeShaded(anAIS);
1011 bool XGUI_Displayer::activate(const Handle(AIS_InteractiveObject)& theIO,
1012 const QIntList& theModes,
1013 const bool theUpdateViewer) const
1015 Handle(AIS_InteractiveContext) aContext = AISContext();
1016 if (aContext.IsNull() || theIO.IsNull())
1019 bool isActivationChanged = false;
1020 // deactivate object in all modes, which are not in the list of activation
1021 // It seems that after the IO deactivation the selected state of the IO's owners
1022 // is modified in OCC(version: 6.8.0) and the selection of the object later is lost.
1023 // By this reason, the number of the IO deactivate is decreased and the object is deactivated
1024 // only if there is a difference in the current modes and the parameters modes.
1025 // If the selection problem happens again, it is possible to write a test scenario and create
1026 // a bug. The bug steps are the following:
1027 // Create two IO, activate them in 5 modes, select the first IO, deactivate 3 modes for both,
1028 // with clicked SHIFT select the second object. The result is the selection of the first IO is lost.
1029 TColStd_ListOfInteger aTColModes;
1030 aContext->ActivatedModes(theIO, aTColModes);
1031 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
1032 QIntList aModesActivatedForIO;
1033 bool isDeactivated = false;
1034 for (; itr.More(); itr.Next() ) {
1035 Standard_Integer aMode = itr.Value();
1036 if (!theModes.contains(aMode)) {
1037 deactivateAIS(theIO, aMode);
1038 isDeactivated = true;
1041 aModesActivatedForIO.append(aMode);
1044 if (isDeactivated) {
1045 // the selection from the previous activation modes should be cleared manually (#26172)
1046 theIO->ClearSelected();
1047 aContext->LocalContext()->ClearOutdatedSelection(theIO, true);
1048 // For performance issues
1049 //if (theUpdateViewer)
1051 isActivationChanged = true;
1054 // loading the interactive object allowing the decomposition
1055 if (aTColModes.IsEmpty()) {
1056 aContext->Load(theIO, -1, true);
1059 // trihedron AIS check should be after the AIS loading.
1060 // If it is not loaded, it is steel selectable in the viewer.
1061 Handle(AIS_Trihedron) aTrihedron;
1062 if (!isTrihedronActive())
1063 aTrihedron = Handle(AIS_Trihedron)::DownCast(theIO);
1064 if (aTrihedron.IsNull()) {
1065 // In order to clear active modes list
1066 if (theModes.size() == 0) {
1067 activateAIS(theIO, 0, theUpdateViewer);
1069 foreach(int aMode, theModes) {
1070 if (!aModesActivatedForIO.contains(aMode)) {
1071 activateAIS(theIO, aMode, theUpdateViewer);
1072 isActivationChanged = true;
1077 return isActivationChanged;
1080 bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
1082 AISObjectPtr anAISObj = getAISObject(theObject);
1083 // correct the result's color it it has the attribute
1084 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1086 // Customization of presentation
1087 GeomCustomPrsPtr aCustomPrs;
1088 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1089 if (aFeature.get() != NULL) {
1090 GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
1091 if (aCustPrs.get() != NULL)
1092 aCustomPrs = aCustPrs;
1094 if (aCustomPrs.get() == NULL) {
1095 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
1096 // we ignore presentable not customized objects
1097 if (aPrs.get() == NULL)
1098 aCustomPrs = myCustomPrs;
1100 bool isCustomized = aCustomPrs.get() &&
1101 aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
1102 return isCustomized;
1106 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject, const QColor& theColor, bool theUpdateViewer)
1108 if (!isVisible(theObject))
1111 AISObjectPtr anAISObj = getAISObject(theObject);
1113 anAISObj->getColor(aR, aG, aB);
1114 anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
1115 if (theUpdateViewer)
1117 return QColor(aR, aG, aB);
1120 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
1122 myResult2AISObjectMap[theObject] = theAIS;
1124 #ifdef DEBUG_DISPLAY
1125 std::ostringstream aPtrStr;
1126 aPtrStr << theObject.get();
1127 qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
1128 qDebug(getResult2AISObjectMapInfo().c_str());
1132 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
1134 QStringList aContent;
1135 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
1136 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
1137 std::ostringstream aPtrStr;
1138 aPtrStr << "aObj = " << aObj.get() << ":";
1139 aPtrStr << "anAIS = " << aAISObj.get() << ":";
1140 aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
1142 aContent.append(aPtrStr.str().c_str());
1144 return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
1145 arg(aContent.join("\n")).toStdString().c_str();
1148 void XGUI_Displayer::activateTrihedron(bool theIsActive)
1150 myIsTrihedronActive = theIsActive;
1151 if (!myIsTrihedronActive) {
1152 deactivateTrihedron(true);
1156 void XGUI_Displayer::displayTrihedron(bool theToDisplay) const
1158 Handle(AIS_InteractiveContext) aContext = AISContext();
1159 if (aContext.IsNull())
1162 Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
1165 if (!aContext->IsDisplayed(aTrihedron))
1166 aContext->Display(aTrihedron,
1168 -1 /* selection mode */,
1169 Standard_True /* update viewer*/,
1170 Standard_False /* allow decomposition */,
1171 AIS_DS_Displayed /* xdisplay status */);
1173 if (!isTrihedronActive())
1174 deactivateTrihedron(false);
1176 activate(aTrihedron, myActiveSelectionModes, false);
1178 deactivateTrihedron(false);
1179 //aContext->LocalContext()->ClearOutdatedSelection(aTrihedron, true);
1180 // the selection from the previous activation modes should be cleared manually (#26172)
1182 aContext->Erase(aTrihedron);