1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: XGUI_Displayer.cpp
4 // Created: 20 Apr 2014
5 // Author: Natalia ERMOLAEVA
7 #include "XGUI_Displayer.h"
8 #include "XGUI_Workshop.h"
9 #include "XGUI_ViewerProxy.h"
10 #include "XGUI_SelectionMgr.h"
11 #include "XGUI_Selection.h"
12 #include "XGUI_CustomPrs.h"
15 #include <AppElements_Viewer.h>
18 #include <ModelAPI_Document.h>
19 #include <ModelAPI_Data.h>
20 #include <ModelAPI_Object.h>
21 #include <ModelAPI_Tools.h>
22 #include <ModelAPI_AttributeIntArray.h>
23 #include <ModelAPI_ResultCompSolid.h>
25 #include <ModuleBase_ResultPrs.h>
26 #include <ModuleBase_Tools.h>
27 #include <ModuleBase_IModule.h>
29 #include <GeomAPI_Shape.h>
30 #include <GeomAPI_IPresentable.h>
31 #include <GeomAPI_ICustomPrs.h>
33 #include <AIS_InteractiveContext.hxx>
34 #include <AIS_LocalContext.hxx>
35 #include <AIS_ListOfInteractive.hxx>
36 #include <AIS_ListIteratorOfListOfInteractive.hxx>
37 #include <AIS_DimensionSelectionMode.hxx>
38 #include <AIS_Shape.hxx>
39 #include <AIS_Dimension.hxx>
40 #include <AIS_Trihedron.hxx>
41 #include <AIS_Axis.hxx>
42 #include <AIS_Plane.hxx>
43 #include <AIS_Point.hxx>
44 #include <TColStd_ListIteratorOfListOfInteger.hxx>
45 #include <SelectMgr_ListOfFilter.hxx>
46 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
47 #include <Prs3d_Drawer.hxx>
48 #include <Prs3d_IsoAspect.hxx>
49 #include <SelectMgr_SelectionManager.hxx>
51 #include <StdSelect_ViewerSelector3d.hxx>
53 #include <TColStd_MapOfTransient.hxx>
54 #include <TColStd_MapIteratorOfMapOfTransient.hxx>
56 #include <Events_Loop.h>
57 #include <ModelAPI_Events.h>
61 const int MOUSE_SENSITIVITY_IN_PIXEL = 10; ///< defines the local context mouse selection sensitivity
63 //#define DEBUG_ACTIVATE_OBJECTS
64 //#define DEBUG_DEACTIVATE
65 //#define DEBUG_ACTIVATE_AIS
66 //#define DEBUG_DEACTIVATE_AIS
68 //#define DEBUG_DISPLAY
69 //#define DEBUG_FEATURE_REDISPLAY
70 //#define DEBUG_SELECTION_FILTERS
72 //#define DEBUG_COMPOSILID_DISPLAY
73 // Workaround for bug #25637
74 void displayedObjects(const Handle(AIS_InteractiveContext)& theAIS, AIS_ListOfInteractive& theList)
76 // Get from null point
77 theAIS->DisplayedObjects(theList, true);
78 if (theAIS->HasOpenedContext()) {
79 // get from local context
80 const Handle(AIS_LocalContext)& aLC = theAIS->LocalContext();
81 TColStd_MapOfTransient aMap;
82 int NbDisp = aLC->DisplayedObjects(aMap);
83 TColStd_MapIteratorOfMapOfTransient aIt(aMap);
85 Handle(AIS_InteractiveObject) curIO;
86 Handle(Standard_Transient) Tr;
87 for(; aIt.More(); aIt.Next()){
89 curIO = *((Handle(AIS_InteractiveObject)*) &Tr);
90 theList.Append(curIO);
95 QString qIntListInfo(const QIntList& theValues, const QString& theSeparator = QString(", "))
98 QIntList::const_iterator anIt = theValues.begin(), aLast = theValues.end();
99 for (; anIt != aLast; anIt++) {
100 anInfo.append(QString::number(*anIt));
102 return anInfo.join(theSeparator);
105 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
106 : myWorkshop(theWorkshop), myNeedUpdate(false),
107 myIsTrihedronActive(false), myViewerBlockedRecursiveCount(0)
109 myCustomPrs = std::shared_ptr<GeomAPI_ICustomPrs>(new XGUI_CustomPrs(theWorkshop));
112 XGUI_Displayer::~XGUI_Displayer()
116 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
118 return myResult2AISObjectMap.contains(theObject);
121 bool XGUI_Displayer::display(ObjectPtr theObject, bool theUpdateViewer)
123 bool aDisplayed = false;
124 if (isVisible(theObject)) {
125 #ifdef DEBUG_COMPOSILID_DISPLAY
126 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
127 if (aCompsolidResult.get()) {
128 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
129 ResultPtr aSubResult = aCompsolidResult->subResult(i);
130 if (aSubResult.get())
131 redisplay(aSubResult, false);
138 aDisplayed = redisplay(theObject, theUpdateViewer);
141 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
142 bool isShading = false;
143 if (aPrs.get() != NULL) {
144 anAIS = aPrs->getAISObject(anAIS);
146 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
147 if (aResult.get() != NULL) {
148 #ifdef DEBUG_COMPOSILID_DISPLAY
149 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
150 if (aCompsolidResult.get()) {
151 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
152 ResultPtr aSubResult = aCompsolidResult->subResult(i);
153 if (aSubResult.get())
154 display(aSubResult, false);
161 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
162 if (aShapePtr.get() != NULL) {
163 anAIS = AISObjectPtr(new GeomAPI_AISObject());
164 anAIS->setImpl(new Handle(AIS_InteractiveObject)(new ModuleBase_ResultPrs(aResult)));
165 //anAIS->createShape(aShapePtr);
168 #ifdef DEBUG_COMPOSILID_DISPLAY
174 aDisplayed = display(theObject, anAIS, isShading, theUpdateViewer);
179 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS)
181 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
182 if (!aShapePrs.IsNull()) {
183 TopoDS_Shape aShape = aShapePrs->Shape();
186 TopAbs_ShapeEnum aType = aShape.ShapeType();
187 if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
190 // Check that the presentation is not a sketch
191 Handle(ModuleBase_ResultPrs) aPrs = Handle(ModuleBase_ResultPrs)::DownCast(theAIS);
193 return !aPrs->isSketchMode();
200 bool XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
201 bool isShading, bool theUpdateViewer)
203 bool aDisplayed = false;
205 Handle(AIS_InteractiveContext) aContext = AISContext();
206 if (aContext.IsNull())
209 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
210 if (!anAISIO.IsNull()) {
211 appendResultObject(theObject, theAIS);
213 bool isCustomized = customizeObject(theObject);
215 int aDispMode = isShading? Shading : Wireframe;
217 anAISIO->Attributes()->SetFaceBoundaryDraw( Standard_True );
218 anAISIO->SetDisplayMode(aDispMode);
219 aContext->Display(anAISIO, aDispMode, 0, false, true, AIS_DS_Displayed);
222 emit objectDisplayed(theObject, theAIS);
223 activate(anAISIO, myActiveSelectionModes, theUpdateViewer);
224 // the fix from VPA for more suitable selection of sketcher lines
225 if(anAISIO->Width() > 1) {
226 for(int aModeIdx = 0; aModeIdx < myActiveSelectionModes.length(); ++aModeIdx) {
227 int aMode = myActiveSelectionModes.value(aModeIdx);
228 double aPrecision = (aMode == getSelectionMode(TopAbs_VERTEX))? 20 :
229 (anAISIO->Width() + 2);
230 aContext->SetSelectionSensitivity(anAISIO, aMode, aPrecision);
240 bool XGUI_Displayer::erase(ObjectPtr theObject, const bool theUpdateViewer)
242 bool aErased = false;
243 if (!isVisible(theObject))
246 Handle(AIS_InteractiveContext) aContext = AISContext();
247 if (aContext.IsNull())
250 AISObjectPtr anObject = myResult2AISObjectMap[theObject];
252 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
253 if (!anAIS.IsNull()) {
254 emit beforeObjectErase(theObject, anObject);
255 aContext->Remove(anAIS);
259 myResult2AISObjectMap.remove(theObject);
262 std::ostringstream aPtrStr;
263 aPtrStr << theObject.get();
264 qDebug(QString("erase object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
265 qDebug(getResult2AISObjectMapInfo().c_str());
274 bool XGUI_Displayer::redisplay(ObjectPtr theObject, bool theUpdateViewer)
276 bool aRedisplayed = false;
277 if (!isVisible(theObject))
280 AISObjectPtr aAISObj = getAISObject(theObject);
281 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
283 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
285 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
287 aRedisplayed = erase(theObject, theUpdateViewer);
290 if (aAIS_Obj != aAISObj) {
291 appendResultObject(theObject, aAIS_Obj);
293 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
296 Handle(AIS_InteractiveContext) aContext = AISContext();
297 if (!aContext.IsNull() && !aAISIO.IsNull()) {
298 // Check that the visualized shape is the same and the redisplay is not necessary
299 // Redisplay of AIS object leads to this object selection compute and the selection
300 // in the browser is lost
301 // this check is not necessary anymore because the selection store/restore is realized
302 // before and after the values modification.
303 // Moreother, this check avoids customize and redisplay presentation if the presentable
304 // parameter is changed.
305 bool isEqualShapes = false;
306 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
307 if (aResult.get() != NULL) {
308 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(aAISIO);
309 if (!aShapePrs.IsNull()) {
310 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
311 if (aShapePtr.get()) {
312 const TopoDS_Shape& aOldShape = aShapePrs->Shape();
313 if (!aOldShape.IsNull())
314 isEqualShapes = aOldShape.IsEqual(aShapePtr->impl<TopoDS_Shape>());
318 // Customization of presentation
319 bool isCustomized = customizeObject(theObject);
320 #ifdef DEBUG_FEATURE_REDISPLAY
321 qDebug(QString("Redisplay: %1, isEqualShapes=%2, isCustomized=%3").
322 arg(!isEqualShapes || isCustomized).arg(isEqualShapes).arg(isCustomized).toStdString().c_str());
324 if (!isEqualShapes || isCustomized) {
325 aContext->Redisplay(aAISIO, false);
327 #ifdef DEBUG_FEATURE_REDISPLAY
328 qDebug(" Redisplay happens");
337 void XGUI_Displayer::redisplayObjects()
339 // redisplay objects visualized in the viewer
340 static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
341 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
342 QObjectPtrList aDisplayed = myWorkshop->displayer()->displayedObjects();
343 QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
344 for (; anIt != aLast; anIt++) {
345 aECreator->sendUpdated(*anIt, EVENT_DISP);
347 Events_Loop::loop()->flush(EVENT_DISP);
350 void XGUI_Displayer::deactivate(ObjectPtr theObject, const bool theUpdateViewer)
352 #ifdef DEBUG_DEACTIVATE
353 QString anInfoStr = ModuleBase_Tools::objectInfo(theObject);
354 qDebug(QString("deactivate: myActiveSelectionModes[%1]: %2, objects = ").
355 arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
357 toStdString().c_str());
359 Handle(AIS_InteractiveContext) aContext = AISContext();
360 if (!aContext.IsNull() && isVisible(theObject)) {
361 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
362 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
364 deactivateAIS(anAIS);
365 // the selection from the previous activation modes should be cleared manually (#26172)
366 aContext->LocalContext()->ClearOutdatedSelection(anAIS, true);
372 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
373 const bool theUpdateViewer)
375 //Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
376 //if (!aTrihedron.IsNull())
377 // deactivateAIS(aTrihedron);
379 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
380 for (; anIt != aLast; anIt++) {
381 deactivate(*anIt, false);
383 //VSV It seems that there is no necessity to update viewer on deactivation
384 //if (theUpdateViewer)
388 void XGUI_Displayer::getModesOfActivation(ObjectPtr theObject, QIntList& theModes)
390 Handle(AIS_InteractiveContext) aContext = AISContext();
391 if (aContext.IsNull() || !isVisible(theObject))
394 AISObjectPtr aAISObj = getAISObject(theObject);
396 if (aAISObj.get() != NULL) {
397 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
398 TColStd_ListOfInteger aTColModes;
399 aContext->ActivatedModes(anAISIO, aTColModes);
400 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
401 for (; itr.More(); itr.Next() ) {
402 theModes.append(itr.Value());
407 int XGUI_Displayer::getSelectionMode(int theShapeType)
409 return (theShapeType >= TopAbs_SHAPE)? theShapeType :
410 AIS_Shape::SelectionMode((TopAbs_ShapeEnum)theShapeType);
413 bool XGUI_Displayer::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
415 bool aVisible = false;
416 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
417 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
418 if (aPrs.get() || aResult.get()) {
419 aVisible = theDisplayer->isVisible(theObject);
420 // compsolid is not visualized in the viewer, but should have presentation when all sub solids are
421 // visible. It is useful for highlight presentation where compsolid shape is selectable
422 if (!aVisible && aResult.get() && aResult->groupName() == ModelAPI_ResultCompSolid::group()) {
423 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResult);
424 if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
425 bool anAllSubsVisible = aCompsolidResult->numberOfSubs() > 0;
426 for(int i = 0; i < aCompsolidResult->numberOfSubs() && anAllSubsVisible; i++) {
427 anAllSubsVisible = theDisplayer->isVisible(aCompsolidResult->subResult(i));
429 aVisible = anAllSubsVisible;
434 // check if all results of the feature are visible
435 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
436 std::list<ResultPtr> aResults = aFeature->results();
437 std::list<ResultPtr>::const_iterator aIt;
438 aVisible = !aResults.empty();
439 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
440 aVisible = aVisible && theDisplayer->isVisible(*aIt);
447 void XGUI_Displayer::activateObjects(const QIntList& theModes, const QObjectPtrList& theObjList,
448 const bool theUpdateViewer)
450 // Convert shape types to selection types
452 foreach(int aType, theModes) {
453 aModes.append(getSelectionMode(aType));
456 #ifdef DEBUG_ACTIVATE_OBJECTS
458 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
459 for (; anIt != aLast; ++anIt) {
460 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
462 QString anInfoStr = anInfo.join(", ");
464 qDebug(QString("activateObjects: aModes[%1] = %2, myActiveSelectionModes[%3] = %4, objects = %5").
465 arg(aModes.size()).arg(qIntListInfo(aModes)).
466 arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
468 toStdString().c_str());
470 // In order to avoid doblications of selection modes
472 foreach (int aMode, aModes) {
473 if (!aNewModes.contains(aMode))
474 aNewModes.append(aMode);
476 myActiveSelectionModes = aNewModes;
477 Handle(AIS_InteractiveContext) aContext = AISContext();
478 // Open local context if there is no one
479 if (aContext.IsNull() || !aContext->HasOpenedContext())
482 //aContext->UseDisplayedObjects();
483 //myUseExternalObjects = true;
485 Handle(AIS_InteractiveObject) anAISIO;
486 AIS_ListOfInteractive aPrsList;
487 //if (aObjList.isEmpty())
490 foreach(ObjectPtr aObj, theObjList) {
491 if (myResult2AISObjectMap.contains(aObj))
492 aPrsList.Append(myResult2AISObjectMap[aObj]->impl<Handle(AIS_InteractiveObject)>());
496 // Add trihedron because it has to partisipate in selection
497 Handle(AIS_InteractiveObject) aTrihedron;
498 if (isTrihedronActive()) {
499 aTrihedron = getTrihedron();
500 if (!aTrihedron.IsNull() && aContext->IsDisplayed(aTrihedron))
501 aPrsList.Append(aTrihedron);
503 if (aPrsList.Extent() == 0)
506 AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
507 bool isActivationChanged = false;
508 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
509 anAISIO = aLIt.Value();
510 if (activate(anAISIO, myActiveSelectionModes, false))
511 isActivationChanged = true;
513 if (!aTrihedron.IsNull()) {
514 foreach(int aMode, myActiveSelectionModes)
515 aContext->SetSelectionSensitivity(aTrihedron, aMode, 20);
517 // VSV It seems that there is no necessity to update viewer on activation
518 //if (theUpdateViewer && isActivationChanged)
522 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
524 Handle(AIS_InteractiveContext) aContext = AISContext();
525 if (aContext.IsNull() || !isVisible(theObject))
528 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
529 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
531 TColStd_ListOfInteger aModes;
532 aContext->ActivatedModes(anAIS, aModes);
533 return aModes.Extent() > 0;
535 void XGUI_Displayer::setSelected(const QList<ModuleBase_ViewerPrs>& theValues, bool theUpdateViewer)
537 Handle(AIS_InteractiveContext) aContext = AISContext();
538 if (aContext.IsNull())
540 if (aContext->HasOpenedContext()) {
541 aContext->UnhilightSelected(false);
542 aContext->ClearSelected(false);
543 foreach (ModuleBase_ViewerPrs aPrs, theValues) {
544 const TopoDS_Shape& aShape = aPrs.shape();
545 if (!aShape.IsNull()) {
546 aContext->AddOrRemoveSelected(aShape, false);
548 ObjectPtr anObject = aPrs.object();
549 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
550 if (aResult.get() && isVisible(aResult)) {
551 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
552 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
553 if (!anAIS.IsNull()) {
554 // The methods are replaced in order to provide multi-selection, e.g. restore selection
555 // by activating multi selector widget. It also gives an advantage that the multi
556 // selection in OB gives multi-selection in the viewer
557 //aContext->SetSelected(anAIS, false);
558 // The selection in the context was cleared, so the method sets the objects are selected
559 aContext->AddOrRemoveSelected(anAIS, false);
565 aContext->UnhilightCurrents(false);
566 aContext->ClearCurrents(false);
567 foreach (ModuleBase_ViewerPrs aPrs, theValues) {
568 ObjectPtr anObject = aPrs.object();
569 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
570 if (aResult.get() && isVisible(aResult)) {
571 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
572 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
574 aContext->SetCurrentObject(anAIS, false);
582 void XGUI_Displayer::clearSelected()
584 Handle(AIS_InteractiveContext) aContext = AISContext();
585 if (!aContext.IsNull()) {
586 aContext->UnhilightCurrents(false);
587 aContext->ClearSelected();
591 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
593 bool aErased = false;
594 Handle(AIS_InteractiveContext) aContext = AISContext();
595 if (!aContext.IsNull()) {
596 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
597 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
599 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
600 if (!anIO.IsNull()) {
601 emit beforeObjectErase(aObj, aAISObj);
602 aContext->Remove(anIO, false);
609 myResult2AISObjectMap.clear();
612 qDebug(getResult2AISObjectMapInfo().c_str());
617 void deactivateObject(Handle(AIS_InteractiveContext) theContext,
618 Handle(AIS_InteractiveObject) theObject,
619 const bool theClear = true)
621 if (!theObject.IsNull()) {
622 theContext->Deactivate(theObject);
624 //theObject->ClearSelected();
625 // theContext->LocalContext()->ClearOutdatedSelection(theObject, true);
630 void XGUI_Displayer::deactivateTrihedron(const bool theUpdateViewer) const
632 Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
633 Handle(AIS_InteractiveContext) aContext = AISContext();
634 if (!aTrihedron.IsNull() && aContext->IsDisplayed(aTrihedron)) {
635 Handle(AIS_Trihedron) aTrie = Handle(AIS_Trihedron)::DownCast(aTrihedron);
636 deactivateObject(aContext, aTrie);
638 /// #1136 hidden axis are selected in sketch
639 /// workaround for Cascade: there is a crash in AIS_LocalContext::ClearOutdatedSelection
640 /// for Position AIS object in SelectionModes.
641 deactivateObject(aContext, aTrie->XAxis());
642 deactivateObject(aContext, aTrie->YAxis());
643 deactivateObject(aContext, aTrie->Axis());
644 deactivateObject(aContext, aTrie->Position());
646 deactivateObject(aContext, aTrie->XYPlane());
647 deactivateObject(aContext, aTrie->XZPlane());
648 deactivateObject(aContext, aTrie->YZPlane());
655 Handle(AIS_InteractiveObject) XGUI_Displayer::getTrihedron() const
657 return myWorkshop->viewer()->trihedron();
660 void XGUI_Displayer::openLocalContext()
662 Handle(AIS_InteractiveContext) aContext = AISContext();
663 // Open local context if there is no one
664 if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
665 // Preserve selected objects
666 //AIS_ListOfInteractive aAisList;
667 //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
668 // aAisList.Append(aContext->Current());
670 // get the filters from the global context and append them to the local context
671 // a list of filters in the global context is not cleared and should be cleared here
672 SelectMgr_ListOfFilter aFilters;
673 aFilters.Assign(aContext->Filters());
674 // it is important to remove the filters in the global context, because there is a code
675 // in the closeLocalContex, which restore the global context filters
676 aContext->RemoveFilters();
678 //aContext->ClearCurrents();
679 aContext->OpenLocalContext();
680 //deactivateTrihedron();
681 //aContext->NotUseDisplayedObjects();
683 //myUseExternalObjects = false;
685 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
686 for (;aIt.More(); aIt.Next()) {
687 aContext->AddFilter(aIt.Value());
690 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
691 //for(; aIt2.More(); aIt2.Next()) {
692 // aContext->SetSelected(aIt2.Value(), false);
697 void XGUI_Displayer::closeLocalContexts(const bool theUpdateViewer)
699 Handle(AIS_InteractiveContext) aContext = AISContext();
700 if (!aContext.IsNull() && aContext->HasOpenedContext()) {
701 // Preserve selected objects
702 //AIS_ListOfInteractive aAisList;
703 //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
704 // aAisList.Append(aContext->SelectedInteractive());
706 // get the filters from the local context and append them to the global context
707 // a list of filters in the local context is cleared
708 SelectMgr_ListOfFilter aFilters;
709 aFilters.Assign(aContext->Filters());
711 //aContext->ClearSelected();
712 aContext->CloseAllContexts(false);
714 // From the moment when the AIS_DS_Displayed flag is used in the Display of AIS object,
715 // this code is obsolete. It is temporaty commented and should be removed after
716 // the test campaign.
717 // Redisplay all object if they were displayed in localContext
718 /*Handle(AIS_InteractiveObject) aAISIO;
719 foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
720 aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
721 if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
722 aContext->Display(aAISIO, false);
723 aContext->SetDisplayMode(aAISIO, Shading, false);
727 // Append the filters from the local selection in the global selection context
728 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
729 for (;aIt.More(); aIt.Next()) {
730 Handle(SelectMgr_Filter) aFilter = aIt.Value();
731 aContext->AddFilter(aFilter);
736 //myUseExternalObjects = false;
739 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
740 //for(; aIt2.More(); aIt2.Next()) {
741 // if (aContext->IsDisplayed(aIt2.Value()))
742 // aContext->SetCurrentObject(aIt2.Value(), false);
747 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
750 if (myResult2AISObjectMap.contains(theObject))
751 anIO = myResult2AISObjectMap[theObject];
755 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
757 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
758 return getObject(aRefAIS);
761 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
764 foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
765 AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
766 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
772 if (!anObject.get()) {
773 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
774 if (!theIO.IsNull()) {
775 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
777 anObject = myWorkshop->module()->findPresentedObject(anAISObj);
782 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
784 bool aWasEnabled = isUpdateEnabled();
786 myViewerBlockedRecursiveCount--;
788 myViewerBlockedRecursiveCount++;
790 if (myNeedUpdate && isUpdateEnabled()) {
792 myNeedUpdate = false;
797 bool XGUI_Displayer::isUpdateEnabled() const
799 return myViewerBlockedRecursiveCount == 0;
802 void XGUI_Displayer::updateViewer() const
804 Handle(AIS_InteractiveContext) aContext = AISContext();
805 if (!aContext.IsNull() && isUpdateEnabled()) {
806 myWorkshop->viewer()->Zfitall();
807 aContext->UpdateCurrentViewer();
813 void XGUI_Displayer::activateAIS(const Handle(AIS_InteractiveObject)& theIO,
814 const int theMode, const bool theUpdateViewer) const
816 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
817 if (!aContext.IsNull()) {
818 aContext->Activate(theIO, theMode, false);
820 #ifdef DEBUG_ACTIVATE_AIS
821 ObjectPtr anObject = getObject(theIO);
822 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
823 qDebug(QString("activateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
830 void XGUI_Displayer::deactivateAIS(const Handle(AIS_InteractiveObject)& theIO, const int theMode) const
832 Handle(AIS_InteractiveContext) aContext = AISContext();
833 if (!aContext.IsNull()) {
835 aContext->Deactivate(theIO);
837 aContext->Deactivate(theIO, theMode);
839 #ifdef DEBUG_DEACTIVATE_AIS
840 ObjectPtr anObject = getObject(theIO);
841 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
842 qDebug(QString("deactivateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
847 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
849 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
850 if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
851 aContext->OpenLocalContext();
852 if (!isTrihedronActive())
853 deactivateTrihedron(true);
854 aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
855 aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
860 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
862 Handle(AIS_InteractiveContext) aContext = AISContext();
863 if (!aContext.IsNull() && myAndFilter.IsNull()) {
864 myAndFilter = new SelectMgr_AndFilter();
865 aContext->AddFilter(myAndFilter);
870 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
871 bool theUpdateViewer)
873 bool aDisplayed = false;
874 Handle(AIS_InteractiveContext) aContext = AISContext();
875 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
876 if (!aContext.IsNull() && !anAISIO.IsNull()) {
877 aContext->Display(anAISIO, 0/*wireframe*/, 0, false/*update viewer*/, true, AIS_DS_Displayed);
879 aContext->Deactivate(anAISIO);
880 aContext->Load(anAISIO);
881 if (toActivateInSelectionModes) {
882 if (aContext->HasOpenedContext()) {
883 if (myActiveSelectionModes.size() == 0)
884 activateAIS(anAISIO, 0, theUpdateViewer);
886 foreach(int aMode, myActiveSelectionModes) {
887 activateAIS(anAISIO, aMode, theUpdateViewer);
898 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
900 bool aErased = false;
901 Handle(AIS_InteractiveContext) aContext = AISContext();
902 if (!aContext.IsNull()) {
903 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
904 if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
905 aContext->Remove(anAISIO, false/*update viewer*/);
909 if (aErased && theUpdateViewer)
915 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
917 if (theMode == NoMode)
920 Handle(AIS_InteractiveContext) aContext = AISContext();
921 if (aContext.IsNull())
924 AISObjectPtr aAISObj = getAISObject(theObject);
928 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
929 aContext->SetDisplayMode(aAISIO, theMode, false);
930 // Redisplay in order to update new mode because it could be not computed before
935 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
937 Handle(AIS_InteractiveContext) aContext = AISContext();
938 if (aContext.IsNull())
941 AISObjectPtr aAISObj = getAISObject(theObject);
945 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
946 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
949 void XGUI_Displayer::deactivateSelectionFilters()
951 Handle(AIS_InteractiveContext) aContext = AISContext();
952 if (!myAndFilter.IsNull()) {
954 if (!aContext.IsNull()) {
955 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
956 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
957 for (; anIt.More() && !aFound; anIt.Next()) {
958 Handle(SelectMgr_Filter) aFilter = anIt.Value();
959 aFound = aFilter == myAndFilter;
962 aContext->RemoveFilter(myAndFilter);
964 myAndFilter.Nullify();
968 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
970 Handle(AIS_InteractiveContext) aContext = AISContext();
971 if (aContext.IsNull() || hasSelectionFilter(theFilter))
974 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
975 if (!aCompositeFilter.IsNull()) {
976 aCompositeFilter->Add(theFilter);
977 #ifdef DEBUG_SELECTION_FILTERS
978 int aCount = aCompositeFilter->StoredFilters().Extent();
979 qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
984 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
986 Handle(AIS_InteractiveContext) aContext = AISContext();
987 if (aContext.IsNull())
990 Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
991 if (!aCompositeFilter.IsNull() && aCompositeFilter->IsIn(theFilter)) {
992 aCompositeFilter->Remove(theFilter);
993 #ifdef DEBUG_SELECTION_FILTERS
994 int aCount = aCompositeFilter->StoredFilters().Extent();
995 qDebug(QString("removeSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
1000 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
1002 bool aFilterFound = false;
1004 Handle(AIS_InteractiveContext) aContext = AISContext();
1005 if (aContext.IsNull())
1006 return aFilterFound;
1007 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
1008 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
1009 for (; aIt.More() && !aFilterFound; aIt.Next()) {
1010 if (theFilter.Access() == aIt.Value().Access())
1011 aFilterFound = true;
1013 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
1014 if (!aCompositeFilter.IsNull()) {
1015 const SelectMgr_ListOfFilter& aStoredFilters = aCompositeFilter->StoredFilters();
1016 for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
1017 if (theFilter.Access() == aIt.Value().Access())
1018 aFilterFound = true;
1021 return aFilterFound;
1024 void XGUI_Displayer::removeFilters()
1026 Handle(AIS_InteractiveContext) aContext = AISContext();
1027 if (aContext.IsNull())
1030 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
1031 if (!aCompositeFilter.IsNull())
1032 aCompositeFilter->Clear();
1035 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
1037 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
1038 foreach(ObjectPtr aObj, aDispList) {
1039 if (!theList.contains(aObj))
1042 foreach(ObjectPtr aObj, theList) {
1043 if (!isVisible(aObj))
1044 display(aObj, false);
1049 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
1051 if (!isVisible(theObject))
1054 AISObjectPtr aAISObj = getAISObject(theObject);
1055 if (aAISObj.get() == NULL)
1058 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1059 return ::canBeShaded(anAIS);
1062 bool XGUI_Displayer::activate(const Handle(AIS_InteractiveObject)& theIO,
1063 const QIntList& theModes,
1064 const bool theUpdateViewer) const
1066 Handle(AIS_InteractiveContext) aContext = AISContext();
1067 if (aContext.IsNull() || theIO.IsNull())
1070 bool isActivationChanged = false;
1071 // deactivate object in all modes, which are not in the list of activation
1072 // It seems that after the IO deactivation the selected state of the IO's owners
1073 // is modified in OCC(version: 6.8.0) and the selection of the object later is lost.
1074 // By this reason, the number of the IO deactivate is decreased and the object is deactivated
1075 // only if there is a difference in the current modes and the parameters modes.
1076 // If the selection problem happens again, it is possible to write a test scenario and create
1077 // a bug. The bug steps are the following:
1078 // Create two IO, activate them in 5 modes, select the first IO, deactivate 3 modes for both,
1079 // with clicked SHIFT select the second object. The result is the selection of the first IO is lost.
1080 TColStd_ListOfInteger aTColModes;
1081 aContext->ActivatedModes(theIO, aTColModes);
1082 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
1083 QIntList aModesActivatedForIO;
1084 bool isDeactivated = false;
1085 for (; itr.More(); itr.Next() ) {
1086 Standard_Integer aMode = itr.Value();
1087 if (!theModes.contains(aMode)) {
1088 deactivateAIS(theIO, aMode);
1089 isDeactivated = true;
1092 aModesActivatedForIO.append(aMode);
1095 if (isDeactivated) {
1096 // the selection from the previous activation modes should be cleared manually (#26172)
1097 theIO->ClearSelected();
1098 aContext->LocalContext()->ClearOutdatedSelection(theIO, true);
1099 // For performance issues
1100 //if (theUpdateViewer)
1102 isActivationChanged = true;
1105 // loading the interactive object allowing the decomposition
1106 if (aTColModes.IsEmpty()) {
1107 aContext->Load(theIO, -1, true);
1110 // trihedron AIS check should be after the AIS loading.
1111 // If it is not loaded, it is steel selectable in the viewer.
1112 Handle(AIS_Trihedron) aTrihedron;
1113 if (!isTrihedronActive())
1114 aTrihedron = Handle(AIS_Trihedron)::DownCast(theIO);
1115 if (aTrihedron.IsNull()) {
1116 // In order to clear active modes list
1117 if (theModes.size() == 0) {
1118 activateAIS(theIO, 0, theUpdateViewer);
1120 foreach(int aMode, theModes) {
1121 if (!aModesActivatedForIO.contains(aMode)) {
1122 activateAIS(theIO, aMode, theUpdateViewer);
1123 isActivationChanged = true;
1128 return isActivationChanged;
1131 bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
1133 AISObjectPtr anAISObj = getAISObject(theObject);
1134 // correct the result's color it it has the attribute
1135 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1137 // Customization of presentation
1138 GeomCustomPrsPtr aCustomPrs;
1139 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1140 if (aFeature.get() != NULL) {
1141 GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
1142 if (aCustPrs.get() != NULL)
1143 aCustomPrs = aCustPrs;
1145 if (aCustomPrs.get() == NULL) {
1146 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
1147 // we ignore presentable not customized objects
1148 if (aPrs.get() == NULL)
1149 aCustomPrs = myCustomPrs;
1151 bool isCustomized = aCustomPrs.get() &&
1152 aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
1153 return isCustomized;
1157 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject, const QColor& theColor, bool theUpdateViewer)
1159 if (!isVisible(theObject))
1162 AISObjectPtr anAISObj = getAISObject(theObject);
1164 anAISObj->getColor(aR, aG, aB);
1165 anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
1166 if (theUpdateViewer)
1168 return QColor(aR, aG, aB);
1171 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
1173 myResult2AISObjectMap[theObject] = theAIS;
1175 #ifdef DEBUG_DISPLAY
1176 std::ostringstream aPtrStr;
1177 aPtrStr << theObject.get();
1178 qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
1179 qDebug(getResult2AISObjectMapInfo().c_str());
1183 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
1185 QStringList aContent;
1186 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
1187 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
1188 std::ostringstream aPtrStr;
1189 aPtrStr << "aObj = " << aObj.get() << ":";
1190 aPtrStr << "anAIS = " << aAISObj.get() << ":";
1191 aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
1193 aContent.append(aPtrStr.str().c_str());
1195 return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
1196 arg(aContent.join("\n")).toStdString().c_str();
1199 void XGUI_Displayer::activateTrihedron(bool theIsActive)
1201 myIsTrihedronActive = theIsActive;
1202 if (!myIsTrihedronActive) {
1203 deactivateTrihedron(true);
1207 void XGUI_Displayer::displayTrihedron(bool theToDisplay) const
1209 Handle(AIS_InteractiveContext) aContext = AISContext();
1210 if (aContext.IsNull())
1213 Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
1216 if (!aContext->IsDisplayed(aTrihedron))
1217 aContext->Display(aTrihedron,
1219 -1 /* selection mode */,
1220 Standard_True /* update viewer*/,
1221 Standard_False /* allow decomposition */,
1222 AIS_DS_Displayed /* xdisplay status */);
1224 if (!isTrihedronActive())
1225 deactivateTrihedron(false);
1227 activate(aTrihedron, myActiveSelectionModes, false);
1229 deactivateTrihedron(false);
1230 //aContext->LocalContext()->ClearOutdatedSelection(aTrihedron, true);
1231 // the selection from the previous activation modes should be cleared manually (#26172)
1233 aContext->Erase(aTrihedron);