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>
28 #include <ModuleBase_ViewerPrs.h>
30 #include <GeomAPI_Shape.h>
31 #include <GeomAPI_IPresentable.h>
32 #include <GeomAPI_ICustomPrs.h>
34 #include <AIS_InteractiveContext.hxx>
35 #include <AIS_LocalContext.hxx>
36 #include <AIS_ListOfInteractive.hxx>
37 #include <AIS_ListIteratorOfListOfInteractive.hxx>
38 #include <AIS_DimensionSelectionMode.hxx>
39 #include <AIS_Shape.hxx>
40 #include <AIS_Dimension.hxx>
41 #include <AIS_Trihedron.hxx>
42 #include <AIS_Axis.hxx>
43 #include <AIS_Plane.hxx>
44 #include <AIS_Point.hxx>
45 #include <TColStd_ListIteratorOfListOfInteger.hxx>
46 #include <SelectMgr_ListOfFilter.hxx>
47 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
48 #include <Prs3d_Drawer.hxx>
49 #include <Prs3d_IsoAspect.hxx>
50 #include <SelectMgr_SelectionManager.hxx>
52 #include <StdSelect_ViewerSelector3d.hxx>
54 #include <TColStd_MapOfTransient.hxx>
55 #include <TColStd_MapIteratorOfMapOfTransient.hxx>
57 #include <Events_Loop.h>
58 #include <ModelAPI_Events.h>
62 const int MOUSE_SENSITIVITY_IN_PIXEL = 10; ///< defines the local context mouse selection sensitivity
64 //#define DEBUG_ACTIVATE_OBJECTS
65 //#define DEBUG_DEACTIVATE
66 //#define DEBUG_ACTIVATE_AIS
67 //#define DEBUG_DEACTIVATE_AIS
69 //#define DEBUG_DISPLAY
70 //#define DEBUG_FEATURE_REDISPLAY
71 //#define DEBUG_SELECTION_FILTERS
73 //#define DEBUG_COMPOSILID_DISPLAY
74 // Workaround for bug #25637
75 void displayedObjects(const Handle(AIS_InteractiveContext)& theAIS, AIS_ListOfInteractive& theList)
77 // Get from null point
78 theAIS->DisplayedObjects(theList, true);
79 if (theAIS->HasOpenedContext()) {
80 // get from local context
81 const Handle(AIS_LocalContext)& aLC = theAIS->LocalContext();
82 TColStd_MapOfTransient aMap;
83 int NbDisp = aLC->DisplayedObjects(aMap);
84 TColStd_MapIteratorOfMapOfTransient aIt(aMap);
86 Handle(AIS_InteractiveObject) curIO;
87 Handle(Standard_Transient) Tr;
88 for(; aIt.More(); aIt.Next()){
90 curIO = *((Handle(AIS_InteractiveObject)*) &Tr);
91 theList.Append(curIO);
96 QString qIntListInfo(const QIntList& theValues, const QString& theSeparator = QString(", "))
99 QIntList::const_iterator anIt = theValues.begin(), aLast = theValues.end();
100 for (; anIt != aLast; anIt++) {
101 anInfo.append(QString::number(*anIt));
103 return anInfo.join(theSeparator);
106 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
107 : myWorkshop(theWorkshop), myNeedUpdate(false),
108 myIsTrihedronActive(false), myViewerBlockedRecursiveCount(0)
110 myCustomPrs = std::shared_ptr<GeomAPI_ICustomPrs>(new XGUI_CustomPrs(theWorkshop));
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 Handle(AIS_InteractiveObject) anAISPrs = myWorkshop->module()->createPresentation(aResult);
166 if (anAISPrs.IsNull())
167 anAISPrs = new ModuleBase_ResultPrs(aResult);
168 anAIS->setImpl(new Handle(AIS_InteractiveObject)(anAISPrs));
169 //anAIS->createShape(aShapePtr);
172 #ifdef DEBUG_COMPOSILID_DISPLAY
178 aDisplayed = display(theObject, anAIS, isShading, theUpdateViewer);
183 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS, ModuleBase_IModule* theModule)
185 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
186 if (!aShapePrs.IsNull()) {
187 TopoDS_Shape aShape = aShapePrs->Shape();
190 TopAbs_ShapeEnum aType = aShape.ShapeType();
191 if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
194 // Check that the presentation is not a sketch
195 return theModule->canBeShaded(theAIS);
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, false/*update viewer*/);
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());
275 bool XGUI_Displayer::redisplay(ObjectPtr theObject, bool theUpdateViewer)
277 bool aRedisplayed = false;
278 if (!isVisible(theObject))
281 AISObjectPtr aAISObj = getAISObject(theObject);
282 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
284 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
286 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
288 aRedisplayed = erase(theObject, theUpdateViewer);
291 if (aAIS_Obj != aAISObj) {
292 appendResultObject(theObject, aAIS_Obj);
294 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
297 Handle(AIS_InteractiveContext) aContext = AISContext();
298 if (!aContext.IsNull() && !aAISIO.IsNull()) {
299 // Check that the visualized shape is the same and the redisplay is not necessary
300 // Redisplay of AIS object leads to this object selection compute and the selection
301 // in the browser is lost
302 // this check is not necessary anymore because the selection store/restore is realized
303 // before and after the values modification.
304 // Moreother, this check avoids customize and redisplay presentation if the presentable
305 // parameter is changed.
306 bool isEqualShapes = false;
307 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
308 if (aResult.get() != NULL) {
309 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(aAISIO);
310 if (!aShapePrs.IsNull()) {
311 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
312 if (aShapePtr.get()) {
313 const TopoDS_Shape& aOldShape = aShapePrs->Shape();
314 if (!aOldShape.IsNull())
315 isEqualShapes = aOldShape.IsEqual(aShapePtr->impl<TopoDS_Shape>());
319 // Customization of presentation
320 bool isCustomized = customizeObject(theObject);
321 #ifdef DEBUG_FEATURE_REDISPLAY
322 qDebug(QString("Redisplay: %1, isEqualShapes=%2, isCustomized=%3").
323 arg(!isEqualShapes || isCustomized).arg(isEqualShapes).arg(isCustomized).toStdString().c_str());
325 if (!isEqualShapes || isCustomized) {
326 /// if shapes are equal and presentation are customized, selection should be restored
327 bool aNeedToRestoreSelection = isEqualShapes && isCustomized;
328 if (aNeedToRestoreSelection)
329 myWorkshop->module()->storeSelection();
331 aContext->Redisplay(aAISIO, false);
333 if (aNeedToRestoreSelection)
334 myWorkshop->module()->restoreSelection();
337 #ifdef DEBUG_FEATURE_REDISPLAY
338 qDebug(" Redisplay happens");
347 void XGUI_Displayer::redisplayObjects()
349 // redisplay objects visualized in the viewer
350 static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
351 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
352 QObjectPtrList aDisplayed = myWorkshop->displayer()->displayedObjects();
353 QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
354 for (; anIt != aLast; anIt++) {
355 aECreator->sendUpdated(*anIt, EVENT_DISP);
357 Events_Loop::loop()->flush(EVENT_DISP);
360 void XGUI_Displayer::deactivate(ObjectPtr theObject, const bool theUpdateViewer)
362 #ifdef DEBUG_DEACTIVATE
363 QString anInfoStr = ModuleBase_Tools::objectInfo(theObject);
364 qDebug(QString("deactivate: myActiveSelectionModes[%1]: %2, objects = ").
365 arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
367 toStdString().c_str());
369 Handle(AIS_InteractiveContext) aContext = AISContext();
370 if (!aContext.IsNull() && isVisible(theObject)) {
371 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
372 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
374 deactivateAIS(anAIS);
375 // the selection from the previous activation modes should be cleared manually (#26172)
376 aContext->LocalContext()->ClearOutdatedSelection(anAIS, true);
382 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
383 const bool theUpdateViewer)
385 //Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
386 //if (!aTrihedron.IsNull())
387 // deactivateAIS(aTrihedron);
389 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
390 for (; anIt != aLast; anIt++) {
391 deactivate(*anIt, false);
393 //VSV It seems that there is no necessity to update viewer on deactivation
394 //if (theUpdateViewer)
398 void XGUI_Displayer::getModesOfActivation(ObjectPtr theObject, QIntList& theModes)
400 Handle(AIS_InteractiveContext) aContext = AISContext();
401 if (aContext.IsNull() || !isVisible(theObject))
404 AISObjectPtr aAISObj = getAISObject(theObject);
406 if (aAISObj.get() != NULL) {
407 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
408 TColStd_ListOfInteger aTColModes;
409 aContext->ActivatedModes(anAISIO, aTColModes);
410 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
411 for (; itr.More(); itr.Next() ) {
412 theModes.append(itr.Value());
417 int XGUI_Displayer::getSelectionMode(int theShapeType)
419 return (theShapeType >= TopAbs_SHAPE)? theShapeType :
420 AIS_Shape::SelectionMode((TopAbs_ShapeEnum)theShapeType);
423 bool XGUI_Displayer::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
425 bool aVisible = false;
426 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
427 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
428 if (aPrs.get() || aResult.get()) {
429 aVisible = theDisplayer->isVisible(theObject);
430 // compsolid is not visualized in the viewer, but should have presentation when all sub solids are
431 // visible. It is useful for highlight presentation where compsolid shape is selectable
432 if (!aVisible && aResult.get() && aResult->groupName() == ModelAPI_ResultCompSolid::group()) {
433 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResult);
434 if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
435 bool anAllSubsVisible = aCompsolidResult->numberOfSubs() > 0;
436 for(int i = 0; i < aCompsolidResult->numberOfSubs() && anAllSubsVisible; i++) {
437 anAllSubsVisible = theDisplayer->isVisible(aCompsolidResult->subResult(i));
439 aVisible = anAllSubsVisible;
444 // check if all results of the feature are visible
445 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
446 std::list<ResultPtr> aResults = aFeature->results();
447 std::list<ResultPtr>::const_iterator aIt;
448 aVisible = !aResults.empty();
449 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
450 aVisible = aVisible && theDisplayer->isVisible(*aIt);
457 void XGUI_Displayer::activateObjects(const QIntList& theModes, const QObjectPtrList& theObjList,
458 const bool theUpdateViewer)
460 // Convert shape types to selection types
462 foreach(int aType, theModes) {
463 aModes.append(getSelectionMode(aType));
466 #ifdef DEBUG_ACTIVATE_OBJECTS
468 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
469 for (; anIt != aLast; ++anIt) {
470 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
472 QString anInfoStr = anInfo.join(", ");
474 qDebug(QString("activateObjects: aModes[%1] = %2, myActiveSelectionModes[%3] = %4, objects = %5").
475 arg(aModes.size()).arg(qIntListInfo(aModes)).
476 arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
478 toStdString().c_str());
480 // In order to avoid doblications of selection modes
482 foreach (int aMode, aModes) {
483 if (!aNewModes.contains(aMode))
484 aNewModes.append(aMode);
486 myActiveSelectionModes = aNewModes;
487 Handle(AIS_InteractiveContext) aContext = AISContext();
488 // Open local context if there is no one
489 if (aContext.IsNull() || !aContext->HasOpenedContext())
492 //aContext->UseDisplayedObjects();
493 //myUseExternalObjects = true;
495 Handle(AIS_InteractiveObject) anAISIO;
496 AIS_ListOfInteractive aPrsList;
497 //if (aObjList.isEmpty())
500 foreach(ObjectPtr aObj, theObjList) {
501 if (myResult2AISObjectMap.contains(aObj))
502 aPrsList.Append(myResult2AISObjectMap[aObj]->impl<Handle(AIS_InteractiveObject)>());
506 // Add trihedron because it has to partisipate in selection
507 Handle(AIS_InteractiveObject) aTrihedron;
508 if (isTrihedronActive()) {
509 aTrihedron = getTrihedron();
510 if (!aTrihedron.IsNull() && aContext->IsDisplayed(aTrihedron))
511 aPrsList.Append(aTrihedron);
513 if (aPrsList.Extent() == 0)
516 AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
517 bool isActivationChanged = false;
518 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
519 anAISIO = aLIt.Value();
520 if (activate(anAISIO, myActiveSelectionModes, false))
521 isActivationChanged = true;
523 if (!aTrihedron.IsNull()) {
524 foreach(int aMode, myActiveSelectionModes)
525 aContext->SetSelectionSensitivity(aTrihedron, aMode, 20);
527 // VSV It seems that there is no necessity to update viewer on activation
528 //if (theUpdateViewer && isActivationChanged)
532 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
534 Handle(AIS_InteractiveContext) aContext = AISContext();
535 if (aContext.IsNull() || !isVisible(theObject))
538 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
539 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
541 TColStd_ListOfInteger aModes;
542 aContext->ActivatedModes(anAIS, aModes);
543 return aModes.Extent() > 0;
545 void XGUI_Displayer::setSelected(const QList<ModuleBase_ViewerPrsPtr>& theValues, bool theUpdateViewer)
547 Handle(AIS_InteractiveContext) aContext = AISContext();
548 if (aContext.IsNull())
550 if (aContext->HasOpenedContext()) {
551 aContext->UnhilightSelected(false);
552 aContext->ClearSelected(false);
553 foreach (ModuleBase_ViewerPrsPtr aPrs, theValues) {
554 const GeomShapePtr& aGeomShape = aPrs->shape();
555 if (aGeomShape.get() && !aGeomShape->isNull()) {
556 const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
557 aContext->AddOrRemoveSelected(aShape, false);
559 ObjectPtr anObject = aPrs->object();
560 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
561 if (aResult.get() && isVisible(aResult)) {
562 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
563 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
564 if (!anAIS.IsNull()) {
565 // The methods are replaced in order to provide multi-selection, e.g. restore selection
566 // by activating multi selector widget. It also gives an advantage that the multi
567 // selection in OB gives multi-selection in the viewer
568 //aContext->SetSelected(anAIS, false);
569 // The selection in the context was cleared, so the method sets the objects are selected
570 aContext->AddOrRemoveSelected(anAIS, false);
576 aContext->UnhilightCurrents(false);
577 aContext->ClearCurrents(false);
578 foreach (ModuleBase_ViewerPrsPtr aPrs, theValues) {
579 ObjectPtr anObject = aPrs->object();
580 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
581 if (aResult.get() && isVisible(aResult)) {
582 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
583 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
585 aContext->SetCurrentObject(anAIS, false);
593 void XGUI_Displayer::clearSelected()
595 Handle(AIS_InteractiveContext) aContext = AISContext();
596 if (!aContext.IsNull()) {
597 aContext->UnhilightCurrents(false);
598 aContext->ClearSelected();
602 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
604 bool aErased = false;
605 Handle(AIS_InteractiveContext) aContext = AISContext();
606 if (!aContext.IsNull()) {
607 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
608 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
610 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
611 if (!anIO.IsNull()) {
612 emit beforeObjectErase(aObj, aAISObj);
613 aContext->Remove(anIO, false/*update viewer*/);
620 myResult2AISObjectMap.clear();
623 qDebug(getResult2AISObjectMapInfo().c_str());
628 void deactivateObject(Handle(AIS_InteractiveContext) theContext,
629 Handle(AIS_InteractiveObject) theObject,
630 const bool theClear = true)
632 if (!theObject.IsNull()) {
633 theContext->Deactivate(theObject);
635 //theObject->ClearSelected();
636 // theContext->LocalContext()->ClearOutdatedSelection(theObject, true);
641 void XGUI_Displayer::deactivateTrihedron(const bool theUpdateViewer) const
643 Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
644 Handle(AIS_InteractiveContext) aContext = AISContext();
645 if (!aTrihedron.IsNull() && aContext->IsDisplayed(aTrihedron)) {
646 Handle(AIS_Trihedron) aTrie = Handle(AIS_Trihedron)::DownCast(aTrihedron);
647 deactivateObject(aContext, aTrie);
649 /// #1136 hidden axis are selected in sketch
650 /// workaround for Cascade: there is a crash in AIS_LocalContext::ClearOutdatedSelection
651 /// for Position AIS object in SelectionModes.
652 deactivateObject(aContext, aTrie->XAxis());
653 deactivateObject(aContext, aTrie->YAxis());
654 deactivateObject(aContext, aTrie->Axis());
655 deactivateObject(aContext, aTrie->Position());
657 deactivateObject(aContext, aTrie->XYPlane());
658 deactivateObject(aContext, aTrie->XZPlane());
659 deactivateObject(aContext, aTrie->YZPlane());
666 Handle(AIS_InteractiveObject) XGUI_Displayer::getTrihedron() const
668 return myWorkshop->viewer()->trihedron();
671 void XGUI_Displayer::openLocalContext()
673 Handle(AIS_InteractiveContext) aContext = AISContext();
674 // Open local context if there is no one
675 if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
676 // Preserve selected objects
677 //AIS_ListOfInteractive aAisList;
678 //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
679 // aAisList.Append(aContext->Current());
681 // get the filters from the global context and append them to the local context
682 // a list of filters in the global context is not cleared and should be cleared here
683 SelectMgr_ListOfFilter aFilters;
684 aFilters.Assign(aContext->Filters());
685 // it is important to remove the filters in the global context, because there is a code
686 // in the closeLocalContex, which restore the global context filters
687 aContext->RemoveFilters();
689 //aContext->ClearCurrents();
690 aContext->OpenLocalContext();
691 //deactivateTrihedron();
692 //aContext->NotUseDisplayedObjects();
694 //myUseExternalObjects = false;
696 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
697 for (;aIt.More(); aIt.Next()) {
698 aContext->AddFilter(aIt.Value());
701 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
702 //for(; aIt2.More(); aIt2.Next()) {
703 // aContext->SetSelected(aIt2.Value(), false);
708 void XGUI_Displayer::closeLocalContexts(const bool theUpdateViewer)
710 Handle(AIS_InteractiveContext) aContext = AISContext();
711 if (!aContext.IsNull() && aContext->HasOpenedContext()) {
712 // Preserve selected objects
713 //AIS_ListOfInteractive aAisList;
714 //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
715 // aAisList.Append(aContext->SelectedInteractive());
717 // get the filters from the local context and append them to the global context
718 // a list of filters in the local context is cleared
719 SelectMgr_ListOfFilter aFilters;
720 aFilters.Assign(aContext->Filters());
722 //aContext->ClearSelected();
723 aContext->CloseAllContexts(false);
725 // From the moment when the AIS_DS_Displayed flag is used in the Display of AIS object,
726 // this code is obsolete. It is temporaty commented and should be removed after
727 // the test campaign.
728 // Redisplay all object if they were displayed in localContext
729 /*Handle(AIS_InteractiveObject) aAISIO;
730 foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
731 aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
732 if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
733 aContext->Display(aAISIO, false);
734 aContext->SetDisplayMode(aAISIO, Shading, false);
738 // Append the filters from the local selection in the global selection context
739 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
740 for (;aIt.More(); aIt.Next()) {
741 Handle(SelectMgr_Filter) aFilter = aIt.Value();
742 aContext->AddFilter(aFilter);
747 //myUseExternalObjects = false;
750 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
751 //for(; aIt2.More(); aIt2.Next()) {
752 // if (aContext->IsDisplayed(aIt2.Value()))
753 // aContext->SetCurrentObject(aIt2.Value(), false);
758 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
761 if (myResult2AISObjectMap.contains(theObject))
762 anIO = myResult2AISObjectMap[theObject];
766 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
768 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
769 return getObject(aRefAIS);
772 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
775 foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
776 AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
777 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
783 if (!anObject.get()) {
784 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
785 if (!theIO.IsNull()) {
786 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
788 anObject = myWorkshop->module()->findPresentedObject(anAISObj);
793 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
795 bool aWasEnabled = isUpdateEnabled();
797 myViewerBlockedRecursiveCount--;
799 myViewerBlockedRecursiveCount++;
801 if (myNeedUpdate && isUpdateEnabled()) {
803 myNeedUpdate = false;
808 bool XGUI_Displayer::isUpdateEnabled() const
810 return myViewerBlockedRecursiveCount == 0;
813 void XGUI_Displayer::updateViewer() const
815 Handle(AIS_InteractiveContext) aContext = AISContext();
816 if (!aContext.IsNull() && isUpdateEnabled()) {
817 myWorkshop->viewer()->Zfitall();
818 aContext->UpdateCurrentViewer();
824 void XGUI_Displayer::activateAIS(const Handle(AIS_InteractiveObject)& theIO,
825 const int theMode, const bool theUpdateViewer) const
827 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
828 if (!aContext.IsNull()) {
829 aContext->Activate(theIO, theMode, false);
831 #ifdef DEBUG_ACTIVATE_AIS
832 ObjectPtr anObject = getObject(theIO);
833 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
834 qDebug(QString("activateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
841 void XGUI_Displayer::deactivateAIS(const Handle(AIS_InteractiveObject)& theIO, const int theMode) const
843 Handle(AIS_InteractiveContext) aContext = AISContext();
844 if (!aContext.IsNull()) {
846 aContext->Deactivate(theIO);
848 aContext->Deactivate(theIO, theMode);
850 #ifdef DEBUG_DEACTIVATE_AIS
851 ObjectPtr anObject = getObject(theIO);
852 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
853 qDebug(QString("deactivateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
858 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
860 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
861 if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
862 aContext->OpenLocalContext();
863 if (!isTrihedronActive())
864 deactivateTrihedron(true);
865 aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
866 aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
871 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
873 Handle(AIS_InteractiveContext) aContext = AISContext();
874 if (!aContext.IsNull() && myAndFilter.IsNull()) {
875 myAndFilter = new SelectMgr_AndFilter();
876 aContext->AddFilter(myAndFilter);
881 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
882 bool theUpdateViewer)
884 bool aDisplayed = false;
885 Handle(AIS_InteractiveContext) aContext = AISContext();
886 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
887 if (!aContext.IsNull() && !anAISIO.IsNull()) {
888 aContext->Display(anAISIO, 0/*wireframe*/, 0, false/*update viewer*/, true, AIS_DS_Displayed);
890 aContext->Deactivate(anAISIO);
891 aContext->Load(anAISIO);
892 if (toActivateInSelectionModes) {
893 if (aContext->HasOpenedContext()) {
894 if (myActiveSelectionModes.size() == 0)
895 activateAIS(anAISIO, 0, theUpdateViewer);
897 foreach(int aMode, myActiveSelectionModes) {
898 activateAIS(anAISIO, aMode, theUpdateViewer);
909 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
911 bool aErased = false;
912 Handle(AIS_InteractiveContext) aContext = AISContext();
913 if (!aContext.IsNull()) {
914 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
915 if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
916 aContext->Remove(anAISIO, false/*update viewer*/);
920 if (aErased && theUpdateViewer)
926 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
928 if (theMode == NoMode)
931 Handle(AIS_InteractiveContext) aContext = AISContext();
932 if (aContext.IsNull())
935 AISObjectPtr aAISObj = getAISObject(theObject);
939 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
940 aContext->SetDisplayMode(aAISIO, theMode, false);
941 // Redisplay in order to update new mode because it could be not computed before
946 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
948 Handle(AIS_InteractiveContext) aContext = AISContext();
949 if (aContext.IsNull())
952 AISObjectPtr aAISObj = getAISObject(theObject);
956 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
957 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
960 void XGUI_Displayer::deactivateSelectionFilters()
962 Handle(AIS_InteractiveContext) aContext = AISContext();
963 if (!myAndFilter.IsNull()) {
965 if (!aContext.IsNull()) {
966 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
967 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
968 for (; anIt.More() && !aFound; anIt.Next()) {
969 Handle(SelectMgr_Filter) aFilter = anIt.Value();
970 aFound = aFilter == myAndFilter;
973 aContext->RemoveFilter(myAndFilter);
975 myAndFilter.Nullify();
979 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
981 Handle(AIS_InteractiveContext) aContext = AISContext();
982 if (aContext.IsNull() || hasSelectionFilter(theFilter))
985 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
986 if (!aCompositeFilter.IsNull()) {
987 aCompositeFilter->Add(theFilter);
988 #ifdef DEBUG_SELECTION_FILTERS
989 int aCount = aCompositeFilter->StoredFilters().Extent();
990 qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
995 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
997 Handle(AIS_InteractiveContext) aContext = AISContext();
998 if (aContext.IsNull())
1001 Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
1002 if (!aCompositeFilter.IsNull() && aCompositeFilter->IsIn(theFilter)) {
1003 aCompositeFilter->Remove(theFilter);
1004 #ifdef DEBUG_SELECTION_FILTERS
1005 int aCount = aCompositeFilter->StoredFilters().Extent();
1006 qDebug(QString("removeSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
1011 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
1013 bool aFilterFound = false;
1015 Handle(AIS_InteractiveContext) aContext = AISContext();
1016 if (aContext.IsNull())
1017 return aFilterFound;
1018 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
1019 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
1020 for (; aIt.More() && !aFilterFound; aIt.Next()) {
1021 if (theFilter.Access() == aIt.Value().Access())
1022 aFilterFound = true;
1024 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
1025 if (!aCompositeFilter.IsNull()) {
1026 const SelectMgr_ListOfFilter& aStoredFilters = aCompositeFilter->StoredFilters();
1027 for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
1028 if (theFilter.Access() == aIt.Value().Access())
1029 aFilterFound = true;
1032 return aFilterFound;
1035 void XGUI_Displayer::removeFilters()
1037 Handle(AIS_InteractiveContext) aContext = AISContext();
1038 if (aContext.IsNull())
1041 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
1042 if (!aCompositeFilter.IsNull())
1043 aCompositeFilter->Clear();
1046 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
1048 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
1049 foreach(ObjectPtr aObj, aDispList) {
1050 if (!theList.contains(aObj))
1053 foreach(ObjectPtr aObj, theList) {
1054 if (!isVisible(aObj))
1055 display(aObj, false);
1060 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
1062 if (!isVisible(theObject))
1065 AISObjectPtr aAISObj = getAISObject(theObject);
1066 if (aAISObj.get() == NULL)
1069 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1070 return ::canBeShaded(anAIS, myWorkshop->module());
1073 bool XGUI_Displayer::activate(const Handle(AIS_InteractiveObject)& theIO,
1074 const QIntList& theModes,
1075 const bool theUpdateViewer) const
1077 Handle(AIS_InteractiveContext) aContext = AISContext();
1078 if (aContext.IsNull() || theIO.IsNull())
1081 bool isActivationChanged = false;
1082 // deactivate object in all modes, which are not in the list of activation
1083 // It seems that after the IO deactivation the selected state of the IO's owners
1084 // is modified in OCC(version: 6.8.0) and the selection of the object later is lost.
1085 // By this reason, the number of the IO deactivate is decreased and the object is deactivated
1086 // only if there is a difference in the current modes and the parameters modes.
1087 // If the selection problem happens again, it is possible to write a test scenario and create
1088 // a bug. The bug steps are the following:
1089 // Create two IO, activate them in 5 modes, select the first IO, deactivate 3 modes for both,
1090 // with clicked SHIFT select the second object. The result is the selection of the first IO is lost.
1091 TColStd_ListOfInteger aTColModes;
1092 aContext->ActivatedModes(theIO, aTColModes);
1093 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
1094 QIntList aModesActivatedForIO;
1095 bool isDeactivated = false;
1096 for (; itr.More(); itr.Next() ) {
1097 Standard_Integer aMode = itr.Value();
1098 if (!theModes.contains(aMode)) {
1099 deactivateAIS(theIO, aMode);
1100 isDeactivated = true;
1103 aModesActivatedForIO.append(aMode);
1106 if (isDeactivated) {
1107 // the selection from the previous activation modes should be cleared manually (#26172)
1108 theIO->ClearSelected();
1109 aContext->LocalContext()->ClearOutdatedSelection(theIO, true);
1110 // For performance issues
1111 //if (theUpdateViewer)
1113 isActivationChanged = true;
1116 // loading the interactive object allowing the decomposition
1117 if (aTColModes.IsEmpty()) {
1118 aContext->Load(theIO, -1, true);
1121 // trihedron AIS check should be after the AIS loading.
1122 // If it is not loaded, it is steel selectable in the viewer.
1123 Handle(AIS_Trihedron) aTrihedron;
1124 if (!isTrihedronActive())
1125 aTrihedron = Handle(AIS_Trihedron)::DownCast(theIO);
1126 if (aTrihedron.IsNull()) {
1127 // In order to clear active modes list
1128 if (theModes.size() == 0) {
1129 activateAIS(theIO, 0, theUpdateViewer);
1131 foreach(int aMode, theModes) {
1132 if (!aModesActivatedForIO.contains(aMode)) {
1133 activateAIS(theIO, aMode, theUpdateViewer);
1134 isActivationChanged = true;
1139 return isActivationChanged;
1142 bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
1144 AISObjectPtr anAISObj = getAISObject(theObject);
1145 // correct the result's color it it has the attribute
1146 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1148 // Customization of presentation
1149 GeomCustomPrsPtr aCustomPrs;
1150 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1151 if (aFeature.get() != NULL) {
1152 GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
1153 if (aCustPrs.get() != NULL)
1154 aCustomPrs = aCustPrs;
1156 if (aCustomPrs.get() == NULL) {
1157 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
1158 // we ignore presentable not customized objects
1159 if (aPrs.get() == NULL)
1160 aCustomPrs = myCustomPrs;
1162 bool isCustomized = aCustomPrs.get() &&
1163 aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
1164 return isCustomized;
1168 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject, const QColor& theColor, bool theUpdateViewer)
1170 if (!isVisible(theObject))
1173 AISObjectPtr anAISObj = getAISObject(theObject);
1175 anAISObj->getColor(aR, aG, aB);
1176 anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
1177 if (theUpdateViewer)
1179 return QColor(aR, aG, aB);
1182 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
1184 myResult2AISObjectMap[theObject] = theAIS;
1186 #ifdef DEBUG_DISPLAY
1187 std::ostringstream aPtrStr;
1188 aPtrStr << theObject.get();
1189 qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
1190 qDebug(getResult2AISObjectMapInfo().c_str());
1194 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
1196 QStringList aContent;
1197 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
1198 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
1199 std::ostringstream aPtrStr;
1200 aPtrStr << "aObj = " << aObj.get() << ":";
1201 aPtrStr << "anAIS = " << aAISObj.get() << ":";
1202 aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
1204 aContent.append(aPtrStr.str().c_str());
1206 return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
1207 arg(aContent.join("\n")).toStdString().c_str();
1210 void XGUI_Displayer::activateTrihedron(bool theIsActive)
1212 myIsTrihedronActive = theIsActive;
1213 if (!myIsTrihedronActive) {
1214 deactivateTrihedron(true);
1218 void XGUI_Displayer::displayTrihedron(bool theToDisplay) const
1220 Handle(AIS_InteractiveContext) aContext = AISContext();
1221 if (aContext.IsNull())
1224 Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
1227 if (!aContext->IsDisplayed(aTrihedron))
1228 aContext->Display(aTrihedron,
1230 -1 /* selection mode */,
1231 Standard_True /* update viewer*/,
1232 Standard_False /* allow decomposition */,
1233 AIS_DS_Displayed /* xdisplay status */);
1235 if (!isTrihedronActive())
1236 deactivateTrihedron(false);
1238 activate(aTrihedron, myActiveSelectionModes, false);
1240 deactivateTrihedron(false);
1241 //aContext->LocalContext()->ClearOutdatedSelection(aTrihedron, true);
1242 // the selection from the previous activation modes should be cleared manually (#26172)
1244 aContext->Erase(aTrihedron);
1250 QIntList XGUI_Displayer::activeSelectionModes() const
1253 foreach (int aMode, myActiveSelectionModes) {
1254 // aMode < 9 is a Shape Enum values
1255 aModes << ((aMode < 9)? AIS_Shape::SelectionType(aMode) : aMode);