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, false/*update viewer*/);
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 /// if shapes are equal and presentation are customized, selection should be restored
326 bool aNeedToRestoreSelection = isEqualShapes && isCustomized;
327 if (aNeedToRestoreSelection)
328 myWorkshop->module()->storeSelection();
330 aContext->Redisplay(aAISIO, false);
332 if (aNeedToRestoreSelection)
333 myWorkshop->module()->restoreSelection();
336 #ifdef DEBUG_FEATURE_REDISPLAY
337 qDebug(" Redisplay happens");
346 void XGUI_Displayer::redisplayObjects()
348 // redisplay objects visualized in the viewer
349 static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
350 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
351 QObjectPtrList aDisplayed = myWorkshop->displayer()->displayedObjects();
352 QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
353 for (; anIt != aLast; anIt++) {
354 aECreator->sendUpdated(*anIt, EVENT_DISP);
356 Events_Loop::loop()->flush(EVENT_DISP);
359 void XGUI_Displayer::deactivate(ObjectPtr theObject, const bool theUpdateViewer)
361 #ifdef DEBUG_DEACTIVATE
362 QString anInfoStr = ModuleBase_Tools::objectInfo(theObject);
363 qDebug(QString("deactivate: myActiveSelectionModes[%1]: %2, objects = ").
364 arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
366 toStdString().c_str());
368 Handle(AIS_InteractiveContext) aContext = AISContext();
369 if (!aContext.IsNull() && isVisible(theObject)) {
370 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
371 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
373 deactivateAIS(anAIS);
374 // the selection from the previous activation modes should be cleared manually (#26172)
375 aContext->LocalContext()->ClearOutdatedSelection(anAIS, true);
381 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
382 const bool theUpdateViewer)
384 //Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
385 //if (!aTrihedron.IsNull())
386 // deactivateAIS(aTrihedron);
388 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
389 for (; anIt != aLast; anIt++) {
390 deactivate(*anIt, false);
392 //VSV It seems that there is no necessity to update viewer on deactivation
393 //if (theUpdateViewer)
397 void XGUI_Displayer::getModesOfActivation(ObjectPtr theObject, QIntList& theModes)
399 Handle(AIS_InteractiveContext) aContext = AISContext();
400 if (aContext.IsNull() || !isVisible(theObject))
403 AISObjectPtr aAISObj = getAISObject(theObject);
405 if (aAISObj.get() != NULL) {
406 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
407 TColStd_ListOfInteger aTColModes;
408 aContext->ActivatedModes(anAISIO, aTColModes);
409 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
410 for (; itr.More(); itr.Next() ) {
411 theModes.append(itr.Value());
416 int XGUI_Displayer::getSelectionMode(int theShapeType)
418 return (theShapeType >= TopAbs_SHAPE)? theShapeType :
419 AIS_Shape::SelectionMode((TopAbs_ShapeEnum)theShapeType);
422 bool XGUI_Displayer::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
424 bool aVisible = false;
425 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
426 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
427 if (aPrs.get() || aResult.get()) {
428 aVisible = theDisplayer->isVisible(theObject);
429 // compsolid is not visualized in the viewer, but should have presentation when all sub solids are
430 // visible. It is useful for highlight presentation where compsolid shape is selectable
431 if (!aVisible && aResult.get() && aResult->groupName() == ModelAPI_ResultCompSolid::group()) {
432 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResult);
433 if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
434 bool anAllSubsVisible = aCompsolidResult->numberOfSubs() > 0;
435 for(int i = 0; i < aCompsolidResult->numberOfSubs() && anAllSubsVisible; i++) {
436 anAllSubsVisible = theDisplayer->isVisible(aCompsolidResult->subResult(i));
438 aVisible = anAllSubsVisible;
443 // check if all results of the feature are visible
444 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
445 std::list<ResultPtr> aResults = aFeature->results();
446 std::list<ResultPtr>::const_iterator aIt;
447 aVisible = !aResults.empty();
448 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
449 aVisible = aVisible && theDisplayer->isVisible(*aIt);
456 void XGUI_Displayer::activateObjects(const QIntList& theModes, const QObjectPtrList& theObjList,
457 const bool theUpdateViewer)
459 // Convert shape types to selection types
461 foreach(int aType, theModes) {
462 aModes.append(getSelectionMode(aType));
465 #ifdef DEBUG_ACTIVATE_OBJECTS
467 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
468 for (; anIt != aLast; ++anIt) {
469 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
471 QString anInfoStr = anInfo.join(", ");
473 qDebug(QString("activateObjects: aModes[%1] = %2, myActiveSelectionModes[%3] = %4, objects = %5").
474 arg(aModes.size()).arg(qIntListInfo(aModes)).
475 arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
477 toStdString().c_str());
479 // In order to avoid doblications of selection modes
481 foreach (int aMode, aModes) {
482 if (!aNewModes.contains(aMode))
483 aNewModes.append(aMode);
485 myActiveSelectionModes = aNewModes;
486 Handle(AIS_InteractiveContext) aContext = AISContext();
487 // Open local context if there is no one
488 if (aContext.IsNull() || !aContext->HasOpenedContext())
491 //aContext->UseDisplayedObjects();
492 //myUseExternalObjects = true;
494 Handle(AIS_InteractiveObject) anAISIO;
495 AIS_ListOfInteractive aPrsList;
496 //if (aObjList.isEmpty())
499 foreach(ObjectPtr aObj, theObjList) {
500 if (myResult2AISObjectMap.contains(aObj))
501 aPrsList.Append(myResult2AISObjectMap[aObj]->impl<Handle(AIS_InteractiveObject)>());
505 // Add trihedron because it has to partisipate in selection
506 Handle(AIS_InteractiveObject) aTrihedron;
507 if (isTrihedronActive()) {
508 aTrihedron = getTrihedron();
509 if (!aTrihedron.IsNull() && aContext->IsDisplayed(aTrihedron))
510 aPrsList.Append(aTrihedron);
512 if (aPrsList.Extent() == 0)
515 AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
516 bool isActivationChanged = false;
517 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
518 anAISIO = aLIt.Value();
519 if (activate(anAISIO, myActiveSelectionModes, false))
520 isActivationChanged = true;
522 if (!aTrihedron.IsNull()) {
523 foreach(int aMode, myActiveSelectionModes)
524 aContext->SetSelectionSensitivity(aTrihedron, aMode, 20);
526 // VSV It seems that there is no necessity to update viewer on activation
527 //if (theUpdateViewer && isActivationChanged)
531 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
533 Handle(AIS_InteractiveContext) aContext = AISContext();
534 if (aContext.IsNull() || !isVisible(theObject))
537 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
538 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
540 TColStd_ListOfInteger aModes;
541 aContext->ActivatedModes(anAIS, aModes);
542 return aModes.Extent() > 0;
544 void XGUI_Displayer::setSelected(const QList<ModuleBase_ViewerPrs>& theValues, bool theUpdateViewer)
546 Handle(AIS_InteractiveContext) aContext = AISContext();
547 if (aContext.IsNull())
549 if (aContext->HasOpenedContext()) {
550 aContext->UnhilightSelected(false);
551 aContext->ClearSelected(false);
552 foreach (ModuleBase_ViewerPrs aPrs, theValues) {
553 const TopoDS_Shape& aShape = aPrs.shape();
554 if (!aShape.IsNull()) {
555 aContext->AddOrRemoveSelected(aShape, false);
557 ObjectPtr anObject = aPrs.object();
558 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
559 if (aResult.get() && isVisible(aResult)) {
560 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
561 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
562 if (!anAIS.IsNull()) {
563 // The methods are replaced in order to provide multi-selection, e.g. restore selection
564 // by activating multi selector widget. It also gives an advantage that the multi
565 // selection in OB gives multi-selection in the viewer
566 //aContext->SetSelected(anAIS, false);
567 // The selection in the context was cleared, so the method sets the objects are selected
568 aContext->AddOrRemoveSelected(anAIS, false);
574 aContext->UnhilightCurrents(false);
575 aContext->ClearCurrents(false);
576 foreach (ModuleBase_ViewerPrs aPrs, theValues) {
577 ObjectPtr anObject = aPrs.object();
578 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
579 if (aResult.get() && isVisible(aResult)) {
580 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
581 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
583 aContext->SetCurrentObject(anAIS, false);
591 void XGUI_Displayer::clearSelected()
593 Handle(AIS_InteractiveContext) aContext = AISContext();
594 if (!aContext.IsNull()) {
595 aContext->UnhilightCurrents(false);
596 aContext->ClearSelected();
600 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
602 bool aErased = false;
603 Handle(AIS_InteractiveContext) aContext = AISContext();
604 if (!aContext.IsNull()) {
605 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
606 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
608 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
609 if (!anIO.IsNull()) {
610 emit beforeObjectErase(aObj, aAISObj);
611 aContext->Remove(anIO, false/*update viewer*/);
618 myResult2AISObjectMap.clear();
621 qDebug(getResult2AISObjectMapInfo().c_str());
626 void deactivateObject(Handle(AIS_InteractiveContext) theContext,
627 Handle(AIS_InteractiveObject) theObject,
628 const bool theClear = true)
630 if (!theObject.IsNull()) {
631 theContext->Deactivate(theObject);
633 //theObject->ClearSelected();
634 // theContext->LocalContext()->ClearOutdatedSelection(theObject, true);
639 void XGUI_Displayer::deactivateTrihedron(const bool theUpdateViewer) const
641 Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
642 Handle(AIS_InteractiveContext) aContext = AISContext();
643 if (!aTrihedron.IsNull() && aContext->IsDisplayed(aTrihedron)) {
644 Handle(AIS_Trihedron) aTrie = Handle(AIS_Trihedron)::DownCast(aTrihedron);
645 deactivateObject(aContext, aTrie);
647 /// #1136 hidden axis are selected in sketch
648 /// workaround for Cascade: there is a crash in AIS_LocalContext::ClearOutdatedSelection
649 /// for Position AIS object in SelectionModes.
650 deactivateObject(aContext, aTrie->XAxis());
651 deactivateObject(aContext, aTrie->YAxis());
652 deactivateObject(aContext, aTrie->Axis());
653 deactivateObject(aContext, aTrie->Position());
655 deactivateObject(aContext, aTrie->XYPlane());
656 deactivateObject(aContext, aTrie->XZPlane());
657 deactivateObject(aContext, aTrie->YZPlane());
664 Handle(AIS_InteractiveObject) XGUI_Displayer::getTrihedron() const
666 return myWorkshop->viewer()->trihedron();
669 void XGUI_Displayer::openLocalContext()
671 Handle(AIS_InteractiveContext) aContext = AISContext();
672 // Open local context if there is no one
673 if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
674 // Preserve selected objects
675 //AIS_ListOfInteractive aAisList;
676 //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
677 // aAisList.Append(aContext->Current());
679 // get the filters from the global context and append them to the local context
680 // a list of filters in the global context is not cleared and should be cleared here
681 SelectMgr_ListOfFilter aFilters;
682 aFilters.Assign(aContext->Filters());
683 // it is important to remove the filters in the global context, because there is a code
684 // in the closeLocalContex, which restore the global context filters
685 aContext->RemoveFilters();
687 //aContext->ClearCurrents();
688 aContext->OpenLocalContext();
689 //deactivateTrihedron();
690 //aContext->NotUseDisplayedObjects();
692 //myUseExternalObjects = false;
694 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
695 for (;aIt.More(); aIt.Next()) {
696 aContext->AddFilter(aIt.Value());
699 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
700 //for(; aIt2.More(); aIt2.Next()) {
701 // aContext->SetSelected(aIt2.Value(), false);
706 void XGUI_Displayer::closeLocalContexts(const bool theUpdateViewer)
708 Handle(AIS_InteractiveContext) aContext = AISContext();
709 if (!aContext.IsNull() && aContext->HasOpenedContext()) {
710 // Preserve selected objects
711 //AIS_ListOfInteractive aAisList;
712 //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
713 // aAisList.Append(aContext->SelectedInteractive());
715 // get the filters from the local context and append them to the global context
716 // a list of filters in the local context is cleared
717 SelectMgr_ListOfFilter aFilters;
718 aFilters.Assign(aContext->Filters());
720 //aContext->ClearSelected();
721 aContext->CloseAllContexts(false);
723 // From the moment when the AIS_DS_Displayed flag is used in the Display of AIS object,
724 // this code is obsolete. It is temporaty commented and should be removed after
725 // the test campaign.
726 // Redisplay all object if they were displayed in localContext
727 /*Handle(AIS_InteractiveObject) aAISIO;
728 foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
729 aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
730 if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
731 aContext->Display(aAISIO, false);
732 aContext->SetDisplayMode(aAISIO, Shading, false);
736 // Append the filters from the local selection in the global selection context
737 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
738 for (;aIt.More(); aIt.Next()) {
739 Handle(SelectMgr_Filter) aFilter = aIt.Value();
740 aContext->AddFilter(aFilter);
745 //myUseExternalObjects = false;
748 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
749 //for(; aIt2.More(); aIt2.Next()) {
750 // if (aContext->IsDisplayed(aIt2.Value()))
751 // aContext->SetCurrentObject(aIt2.Value(), false);
756 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
759 if (myResult2AISObjectMap.contains(theObject))
760 anIO = myResult2AISObjectMap[theObject];
764 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
766 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
767 return getObject(aRefAIS);
770 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
773 foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
774 AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
775 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
781 if (!anObject.get()) {
782 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
783 if (!theIO.IsNull()) {
784 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
786 anObject = myWorkshop->module()->findPresentedObject(anAISObj);
791 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
793 bool aWasEnabled = isUpdateEnabled();
795 myViewerBlockedRecursiveCount--;
797 myViewerBlockedRecursiveCount++;
799 if (myNeedUpdate && isUpdateEnabled()) {
801 myNeedUpdate = false;
806 bool XGUI_Displayer::isUpdateEnabled() const
808 return myViewerBlockedRecursiveCount == 0;
811 void XGUI_Displayer::updateViewer() const
813 Handle(AIS_InteractiveContext) aContext = AISContext();
814 if (!aContext.IsNull() && isUpdateEnabled()) {
815 myWorkshop->viewer()->Zfitall();
816 aContext->UpdateCurrentViewer();
822 void XGUI_Displayer::activateAIS(const Handle(AIS_InteractiveObject)& theIO,
823 const int theMode, const bool theUpdateViewer) const
825 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
826 if (!aContext.IsNull()) {
827 aContext->Activate(theIO, theMode, false);
829 #ifdef DEBUG_ACTIVATE_AIS
830 ObjectPtr anObject = getObject(theIO);
831 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
832 qDebug(QString("activateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
839 void XGUI_Displayer::deactivateAIS(const Handle(AIS_InteractiveObject)& theIO, const int theMode) const
841 Handle(AIS_InteractiveContext) aContext = AISContext();
842 if (!aContext.IsNull()) {
844 aContext->Deactivate(theIO);
846 aContext->Deactivate(theIO, theMode);
848 #ifdef DEBUG_DEACTIVATE_AIS
849 ObjectPtr anObject = getObject(theIO);
850 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
851 qDebug(QString("deactivateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
856 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
858 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
859 if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
860 aContext->OpenLocalContext();
861 if (!isTrihedronActive())
862 deactivateTrihedron(true);
863 aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
864 aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
869 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
871 Handle(AIS_InteractiveContext) aContext = AISContext();
872 if (!aContext.IsNull() && myAndFilter.IsNull()) {
873 myAndFilter = new SelectMgr_AndFilter();
874 aContext->AddFilter(myAndFilter);
879 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
880 bool theUpdateViewer)
882 bool aDisplayed = false;
883 Handle(AIS_InteractiveContext) aContext = AISContext();
884 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
885 if (!aContext.IsNull() && !anAISIO.IsNull()) {
886 aContext->Display(anAISIO, 0/*wireframe*/, 0, false/*update viewer*/, true, AIS_DS_Displayed);
888 aContext->Deactivate(anAISIO);
889 aContext->Load(anAISIO);
890 if (toActivateInSelectionModes) {
891 if (aContext->HasOpenedContext()) {
892 if (myActiveSelectionModes.size() == 0)
893 activateAIS(anAISIO, 0, theUpdateViewer);
895 foreach(int aMode, myActiveSelectionModes) {
896 activateAIS(anAISIO, aMode, theUpdateViewer);
907 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
909 bool aErased = false;
910 Handle(AIS_InteractiveContext) aContext = AISContext();
911 if (!aContext.IsNull()) {
912 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
913 if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
914 aContext->Remove(anAISIO, false/*update viewer*/);
918 if (aErased && theUpdateViewer)
924 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
926 if (theMode == NoMode)
929 Handle(AIS_InteractiveContext) aContext = AISContext();
930 if (aContext.IsNull())
933 AISObjectPtr aAISObj = getAISObject(theObject);
937 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
938 aContext->SetDisplayMode(aAISIO, theMode, false);
939 // Redisplay in order to update new mode because it could be not computed before
944 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
946 Handle(AIS_InteractiveContext) aContext = AISContext();
947 if (aContext.IsNull())
950 AISObjectPtr aAISObj = getAISObject(theObject);
954 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
955 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
958 void XGUI_Displayer::deactivateSelectionFilters()
960 Handle(AIS_InteractiveContext) aContext = AISContext();
961 if (!myAndFilter.IsNull()) {
963 if (!aContext.IsNull()) {
964 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
965 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
966 for (; anIt.More() && !aFound; anIt.Next()) {
967 Handle(SelectMgr_Filter) aFilter = anIt.Value();
968 aFound = aFilter == myAndFilter;
971 aContext->RemoveFilter(myAndFilter);
973 myAndFilter.Nullify();
977 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
979 Handle(AIS_InteractiveContext) aContext = AISContext();
980 if (aContext.IsNull() || hasSelectionFilter(theFilter))
983 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
984 if (!aCompositeFilter.IsNull()) {
985 aCompositeFilter->Add(theFilter);
986 #ifdef DEBUG_SELECTION_FILTERS
987 int aCount = aCompositeFilter->StoredFilters().Extent();
988 qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
993 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
995 Handle(AIS_InteractiveContext) aContext = AISContext();
996 if (aContext.IsNull())
999 Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
1000 if (!aCompositeFilter.IsNull() && aCompositeFilter->IsIn(theFilter)) {
1001 aCompositeFilter->Remove(theFilter);
1002 #ifdef DEBUG_SELECTION_FILTERS
1003 int aCount = aCompositeFilter->StoredFilters().Extent();
1004 qDebug(QString("removeSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
1009 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
1011 bool aFilterFound = false;
1013 Handle(AIS_InteractiveContext) aContext = AISContext();
1014 if (aContext.IsNull())
1015 return aFilterFound;
1016 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
1017 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
1018 for (; aIt.More() && !aFilterFound; aIt.Next()) {
1019 if (theFilter.Access() == aIt.Value().Access())
1020 aFilterFound = true;
1022 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
1023 if (!aCompositeFilter.IsNull()) {
1024 const SelectMgr_ListOfFilter& aStoredFilters = aCompositeFilter->StoredFilters();
1025 for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
1026 if (theFilter.Access() == aIt.Value().Access())
1027 aFilterFound = true;
1030 return aFilterFound;
1033 void XGUI_Displayer::removeFilters()
1035 Handle(AIS_InteractiveContext) aContext = AISContext();
1036 if (aContext.IsNull())
1039 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
1040 if (!aCompositeFilter.IsNull())
1041 aCompositeFilter->Clear();
1044 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
1046 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
1047 foreach(ObjectPtr aObj, aDispList) {
1048 if (!theList.contains(aObj))
1051 foreach(ObjectPtr aObj, theList) {
1052 if (!isVisible(aObj))
1053 display(aObj, false);
1058 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
1060 if (!isVisible(theObject))
1063 AISObjectPtr aAISObj = getAISObject(theObject);
1064 if (aAISObj.get() == NULL)
1067 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1068 return ::canBeShaded(anAIS);
1071 bool XGUI_Displayer::activate(const Handle(AIS_InteractiveObject)& theIO,
1072 const QIntList& theModes,
1073 const bool theUpdateViewer) const
1075 Handle(AIS_InteractiveContext) aContext = AISContext();
1076 if (aContext.IsNull() || theIO.IsNull())
1079 bool isActivationChanged = false;
1080 // deactivate object in all modes, which are not in the list of activation
1081 // It seems that after the IO deactivation the selected state of the IO's owners
1082 // is modified in OCC(version: 6.8.0) and the selection of the object later is lost.
1083 // By this reason, the number of the IO deactivate is decreased and the object is deactivated
1084 // only if there is a difference in the current modes and the parameters modes.
1085 // If the selection problem happens again, it is possible to write a test scenario and create
1086 // a bug. The bug steps are the following:
1087 // Create two IO, activate them in 5 modes, select the first IO, deactivate 3 modes for both,
1088 // with clicked SHIFT select the second object. The result is the selection of the first IO is lost.
1089 TColStd_ListOfInteger aTColModes;
1090 aContext->ActivatedModes(theIO, aTColModes);
1091 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
1092 QIntList aModesActivatedForIO;
1093 bool isDeactivated = false;
1094 for (; itr.More(); itr.Next() ) {
1095 Standard_Integer aMode = itr.Value();
1096 if (!theModes.contains(aMode)) {
1097 deactivateAIS(theIO, aMode);
1098 isDeactivated = true;
1101 aModesActivatedForIO.append(aMode);
1104 if (isDeactivated) {
1105 // the selection from the previous activation modes should be cleared manually (#26172)
1106 theIO->ClearSelected();
1107 aContext->LocalContext()->ClearOutdatedSelection(theIO, true);
1108 // For performance issues
1109 //if (theUpdateViewer)
1111 isActivationChanged = true;
1114 // loading the interactive object allowing the decomposition
1115 if (aTColModes.IsEmpty()) {
1116 aContext->Load(theIO, -1, true);
1119 // trihedron AIS check should be after the AIS loading.
1120 // If it is not loaded, it is steel selectable in the viewer.
1121 Handle(AIS_Trihedron) aTrihedron;
1122 if (!isTrihedronActive())
1123 aTrihedron = Handle(AIS_Trihedron)::DownCast(theIO);
1124 if (aTrihedron.IsNull()) {
1125 // In order to clear active modes list
1126 if (theModes.size() == 0) {
1127 activateAIS(theIO, 0, theUpdateViewer);
1129 foreach(int aMode, theModes) {
1130 if (!aModesActivatedForIO.contains(aMode)) {
1131 activateAIS(theIO, aMode, theUpdateViewer);
1132 isActivationChanged = true;
1137 return isActivationChanged;
1140 bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
1142 AISObjectPtr anAISObj = getAISObject(theObject);
1143 // correct the result's color it it has the attribute
1144 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1146 // Customization of presentation
1147 GeomCustomPrsPtr aCustomPrs;
1148 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1149 if (aFeature.get() != NULL) {
1150 GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
1151 if (aCustPrs.get() != NULL)
1152 aCustomPrs = aCustPrs;
1154 if (aCustomPrs.get() == NULL) {
1155 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
1156 // we ignore presentable not customized objects
1157 if (aPrs.get() == NULL)
1158 aCustomPrs = myCustomPrs;
1160 bool isCustomized = aCustomPrs.get() &&
1161 aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
1162 return isCustomized;
1166 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject, const QColor& theColor, bool theUpdateViewer)
1168 if (!isVisible(theObject))
1171 AISObjectPtr anAISObj = getAISObject(theObject);
1173 anAISObj->getColor(aR, aG, aB);
1174 anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
1175 if (theUpdateViewer)
1177 return QColor(aR, aG, aB);
1180 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
1182 myResult2AISObjectMap[theObject] = theAIS;
1184 #ifdef DEBUG_DISPLAY
1185 std::ostringstream aPtrStr;
1186 aPtrStr << theObject.get();
1187 qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
1188 qDebug(getResult2AISObjectMapInfo().c_str());
1192 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
1194 QStringList aContent;
1195 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
1196 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
1197 std::ostringstream aPtrStr;
1198 aPtrStr << "aObj = " << aObj.get() << ":";
1199 aPtrStr << "anAIS = " << aAISObj.get() << ":";
1200 aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
1202 aContent.append(aPtrStr.str().c_str());
1204 return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
1205 arg(aContent.join("\n")).toStdString().c_str();
1208 void XGUI_Displayer::activateTrihedron(bool theIsActive)
1210 myIsTrihedronActive = theIsActive;
1211 if (!myIsTrihedronActive) {
1212 deactivateTrihedron(true);
1216 void XGUI_Displayer::displayTrihedron(bool theToDisplay) const
1218 Handle(AIS_InteractiveContext) aContext = AISContext();
1219 if (aContext.IsNull())
1222 Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
1225 if (!aContext->IsDisplayed(aTrihedron))
1226 aContext->Display(aTrihedron,
1228 -1 /* selection mode */,
1229 Standard_True /* update viewer*/,
1230 Standard_False /* allow decomposition */,
1231 AIS_DS_Displayed /* xdisplay status */);
1233 if (!isTrihedronActive())
1234 deactivateTrihedron(false);
1236 activate(aTrihedron, myActiveSelectionModes, false);
1238 deactivateTrihedron(false);
1239 //aContext->LocalContext()->ClearOutdatedSelection(aTrihedron, true);
1240 // the selection from the previous activation modes should be cleared manually (#26172)
1242 aContext->Erase(aTrihedron);