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;
443 // it is possible that feature is presentable and has results, so we should check visibility
444 // of results if presentation is not shown (e.g. Sketch Circle/Arc features)
446 // check if all results of the feature are visible
447 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
448 std::list<ResultPtr> aResults = aFeature->results();
449 std::list<ResultPtr>::const_iterator aIt;
450 aVisible = !aResults.empty();
451 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
452 aVisible = aVisible && theDisplayer->isVisible(*aIt);
459 void XGUI_Displayer::activateObjects(const QIntList& theModes, const QObjectPtrList& theObjList,
460 const bool theUpdateViewer)
462 // Convert shape types to selection types
464 foreach(int aType, theModes) {
465 aModes.append(getSelectionMode(aType));
468 #ifdef DEBUG_ACTIVATE_OBJECTS
470 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
471 for (; anIt != aLast; ++anIt) {
472 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
474 QString anInfoStr = anInfo.join(", ");
476 qDebug(QString("activateObjects: aModes[%1] = %2, myActiveSelectionModes[%3] = %4, objects = %5").
477 arg(aModes.size()).arg(qIntListInfo(aModes)).
478 arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
480 toStdString().c_str());
482 // In order to avoid doblications of selection modes
484 foreach (int aMode, aModes) {
485 if (!aNewModes.contains(aMode))
486 aNewModes.append(aMode);
488 myActiveSelectionModes = aNewModes;
489 Handle(AIS_InteractiveContext) aContext = AISContext();
490 // Open local context if there is no one
491 if (aContext.IsNull() || !aContext->HasOpenedContext())
494 //aContext->UseDisplayedObjects();
495 //myUseExternalObjects = true;
497 Handle(AIS_InteractiveObject) anAISIO;
498 AIS_ListOfInteractive aPrsList;
499 //if (aObjList.isEmpty())
502 foreach(ObjectPtr aObj, theObjList) {
503 if (myResult2AISObjectMap.contains(aObj))
504 aPrsList.Append(myResult2AISObjectMap[aObj]->impl<Handle(AIS_InteractiveObject)>());
508 // Add trihedron because it has to partisipate in selection
509 Handle(AIS_InteractiveObject) aTrihedron;
510 if (isTrihedronActive()) {
511 aTrihedron = getTrihedron();
512 if (!aTrihedron.IsNull() && aContext->IsDisplayed(aTrihedron))
513 aPrsList.Append(aTrihedron);
515 if (aPrsList.Extent() == 0)
518 AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
519 bool isActivationChanged = false;
520 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
521 anAISIO = aLIt.Value();
522 if (activate(anAISIO, myActiveSelectionModes, false))
523 isActivationChanged = true;
525 if (!aTrihedron.IsNull()) {
526 foreach(int aMode, myActiveSelectionModes)
527 aContext->SetSelectionSensitivity(aTrihedron, aMode, 20);
529 // VSV It seems that there is no necessity to update viewer on activation
530 //if (theUpdateViewer && isActivationChanged)
534 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
536 Handle(AIS_InteractiveContext) aContext = AISContext();
537 if (aContext.IsNull() || !isVisible(theObject))
540 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
541 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
543 TColStd_ListOfInteger aModes;
544 aContext->ActivatedModes(anAIS, aModes);
545 return aModes.Extent() > 0;
547 void XGUI_Displayer::setSelected(const QList<ModuleBase_ViewerPrsPtr>& theValues, bool theUpdateViewer)
549 Handle(AIS_InteractiveContext) aContext = AISContext();
550 if (aContext.IsNull())
552 if (aContext->HasOpenedContext()) {
553 aContext->UnhilightSelected(false);
554 aContext->ClearSelected(false);
555 foreach (ModuleBase_ViewerPrsPtr aPrs, theValues) {
556 const GeomShapePtr& aGeomShape = aPrs->shape();
557 if (aGeomShape.get() && !aGeomShape->isNull()) {
558 const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
559 aContext->AddOrRemoveSelected(aShape, false);
561 ObjectPtr anObject = aPrs->object();
562 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
563 if (aResult.get() && isVisible(aResult)) {
564 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
565 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
566 if (!anAIS.IsNull()) {
567 // The methods are replaced in order to provide multi-selection, e.g. restore selection
568 // by activating multi selector widget. It also gives an advantage that the multi
569 // selection in OB gives multi-selection in the viewer
570 //aContext->SetSelected(anAIS, false);
571 // The selection in the context was cleared, so the method sets the objects are selected
572 aContext->AddOrRemoveSelected(anAIS, false);
578 aContext->UnhilightCurrents(false);
579 aContext->ClearCurrents(false);
580 foreach (ModuleBase_ViewerPrsPtr aPrs, theValues) {
581 ObjectPtr anObject = aPrs->object();
582 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
583 if (aResult.get() && isVisible(aResult)) {
584 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
585 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
587 aContext->SetCurrentObject(anAIS, false);
595 void XGUI_Displayer::clearSelected()
597 Handle(AIS_InteractiveContext) aContext = AISContext();
598 if (!aContext.IsNull()) {
599 aContext->UnhilightCurrents(false);
600 aContext->ClearSelected();
604 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
606 bool aErased = false;
607 Handle(AIS_InteractiveContext) aContext = AISContext();
608 if (!aContext.IsNull()) {
609 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
610 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
612 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
613 if (!anIO.IsNull()) {
614 emit beforeObjectErase(aObj, aAISObj);
615 aContext->Remove(anIO, false/*update viewer*/);
622 myResult2AISObjectMap.clear();
625 qDebug(getResult2AISObjectMapInfo().c_str());
630 void deactivateObject(Handle(AIS_InteractiveContext) theContext,
631 Handle(AIS_InteractiveObject) theObject,
632 const bool theClear = true)
634 if (!theObject.IsNull()) {
635 theContext->Deactivate(theObject);
637 //theObject->ClearSelected();
638 // theContext->LocalContext()->ClearOutdatedSelection(theObject, true);
643 void XGUI_Displayer::deactivateTrihedron(const bool theUpdateViewer) const
645 Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
646 Handle(AIS_InteractiveContext) aContext = AISContext();
647 if (!aTrihedron.IsNull() && aContext->IsDisplayed(aTrihedron)) {
648 Handle(AIS_Trihedron) aTrie = Handle(AIS_Trihedron)::DownCast(aTrihedron);
649 deactivateObject(aContext, aTrie);
651 /// #1136 hidden axis are selected in sketch
652 /// workaround for Cascade: there is a crash in AIS_LocalContext::ClearOutdatedSelection
653 /// for Position AIS object in SelectionModes.
654 deactivateObject(aContext, aTrie->XAxis());
655 deactivateObject(aContext, aTrie->YAxis());
656 deactivateObject(aContext, aTrie->Axis());
657 deactivateObject(aContext, aTrie->Position());
659 deactivateObject(aContext, aTrie->XYPlane());
660 deactivateObject(aContext, aTrie->XZPlane());
661 deactivateObject(aContext, aTrie->YZPlane());
668 Handle(AIS_InteractiveObject) XGUI_Displayer::getTrihedron() const
670 return myWorkshop->viewer()->trihedron();
673 void XGUI_Displayer::openLocalContext()
675 Handle(AIS_InteractiveContext) aContext = AISContext();
676 // Open local context if there is no one
677 if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
678 // Preserve selected objects
679 //AIS_ListOfInteractive aAisList;
680 //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
681 // aAisList.Append(aContext->Current());
683 // get the filters from the global context and append them to the local context
684 // a list of filters in the global context is not cleared and should be cleared here
685 SelectMgr_ListOfFilter aFilters;
686 aFilters.Assign(aContext->Filters());
687 // it is important to remove the filters in the global context, because there is a code
688 // in the closeLocalContex, which restore the global context filters
689 aContext->RemoveFilters();
691 //aContext->ClearCurrents();
692 aContext->OpenLocalContext();
693 //deactivateTrihedron();
694 //aContext->NotUseDisplayedObjects();
696 //myUseExternalObjects = false;
698 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
699 for (;aIt.More(); aIt.Next()) {
700 aContext->AddFilter(aIt.Value());
703 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
704 //for(; aIt2.More(); aIt2.Next()) {
705 // aContext->SetSelected(aIt2.Value(), false);
710 void XGUI_Displayer::closeLocalContexts(const bool theUpdateViewer)
712 Handle(AIS_InteractiveContext) aContext = AISContext();
713 if (!aContext.IsNull() && aContext->HasOpenedContext()) {
714 // Preserve selected objects
715 //AIS_ListOfInteractive aAisList;
716 //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
717 // aAisList.Append(aContext->SelectedInteractive());
719 // get the filters from the local context and append them to the global context
720 // a list of filters in the local context is cleared
721 SelectMgr_ListOfFilter aFilters;
722 aFilters.Assign(aContext->Filters());
724 //aContext->ClearSelected();
725 aContext->CloseAllContexts(false);
727 // From the moment when the AIS_DS_Displayed flag is used in the Display of AIS object,
728 // this code is obsolete. It is temporaty commented and should be removed after
729 // the test campaign.
730 // Redisplay all object if they were displayed in localContext
731 /*Handle(AIS_InteractiveObject) aAISIO;
732 foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
733 aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
734 if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
735 aContext->Display(aAISIO, false);
736 aContext->SetDisplayMode(aAISIO, Shading, false);
740 // Append the filters from the local selection in the global selection context
741 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
742 for (;aIt.More(); aIt.Next()) {
743 Handle(SelectMgr_Filter) aFilter = aIt.Value();
744 aContext->AddFilter(aFilter);
749 //myUseExternalObjects = false;
752 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
753 //for(; aIt2.More(); aIt2.Next()) {
754 // if (aContext->IsDisplayed(aIt2.Value()))
755 // aContext->SetCurrentObject(aIt2.Value(), false);
760 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
763 if (myResult2AISObjectMap.contains(theObject))
764 anIO = myResult2AISObjectMap[theObject];
768 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
770 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
771 return getObject(aRefAIS);
774 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
777 foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
778 AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
779 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
785 if (!anObject.get()) {
786 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
787 if (!theIO.IsNull()) {
788 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
790 anObject = myWorkshop->module()->findPresentedObject(anAISObj);
795 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
797 bool aWasEnabled = isUpdateEnabled();
799 myViewerBlockedRecursiveCount--;
801 myViewerBlockedRecursiveCount++;
803 if (myNeedUpdate && isUpdateEnabled()) {
805 myNeedUpdate = false;
810 bool XGUI_Displayer::isUpdateEnabled() const
812 return myViewerBlockedRecursiveCount == 0;
815 void XGUI_Displayer::updateViewer() const
817 Handle(AIS_InteractiveContext) aContext = AISContext();
818 if (!aContext.IsNull() && isUpdateEnabled()) {
819 myWorkshop->viewer()->Zfitall();
820 aContext->UpdateCurrentViewer();
826 void XGUI_Displayer::activateAIS(const Handle(AIS_InteractiveObject)& theIO,
827 const int theMode, const bool theUpdateViewer) const
829 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
830 if (!aContext.IsNull()) {
831 aContext->Activate(theIO, theMode, false);
833 #ifdef DEBUG_ACTIVATE_AIS
834 ObjectPtr anObject = getObject(theIO);
835 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
836 qDebug(QString("activateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
843 void XGUI_Displayer::deactivateAIS(const Handle(AIS_InteractiveObject)& theIO, const int theMode) const
845 Handle(AIS_InteractiveContext) aContext = AISContext();
846 if (!aContext.IsNull()) {
848 aContext->Deactivate(theIO);
850 aContext->Deactivate(theIO, theMode);
852 #ifdef DEBUG_DEACTIVATE_AIS
853 ObjectPtr anObject = getObject(theIO);
854 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
855 qDebug(QString("deactivateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
860 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
862 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
863 if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
864 aContext->OpenLocalContext();
865 if (!isTrihedronActive())
866 deactivateTrihedron(true);
867 aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
868 aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
873 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
875 Handle(AIS_InteractiveContext) aContext = AISContext();
876 if (!aContext.IsNull() && myAndFilter.IsNull()) {
877 myAndFilter = new SelectMgr_AndFilter();
878 aContext->AddFilter(myAndFilter);
883 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
884 bool theUpdateViewer)
886 bool aDisplayed = false;
887 Handle(AIS_InteractiveContext) aContext = AISContext();
888 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
889 if (!aContext.IsNull() && !anAISIO.IsNull()) {
890 aContext->Display(anAISIO, 0/*wireframe*/, 0, false/*update viewer*/, true, AIS_DS_Displayed);
892 aContext->Deactivate(anAISIO);
893 aContext->Load(anAISIO);
894 if (toActivateInSelectionModes) {
895 if (aContext->HasOpenedContext()) {
896 if (myActiveSelectionModes.size() == 0)
897 activateAIS(anAISIO, 0, theUpdateViewer);
899 foreach(int aMode, myActiveSelectionModes) {
900 activateAIS(anAISIO, aMode, theUpdateViewer);
911 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
913 bool aErased = false;
914 Handle(AIS_InteractiveContext) aContext = AISContext();
915 if (!aContext.IsNull()) {
916 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
917 if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
918 aContext->Remove(anAISIO, false/*update viewer*/);
922 if (aErased && theUpdateViewer)
928 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
930 if (theMode == NoMode)
933 Handle(AIS_InteractiveContext) aContext = AISContext();
934 if (aContext.IsNull())
937 AISObjectPtr aAISObj = getAISObject(theObject);
941 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
942 aContext->SetDisplayMode(aAISIO, theMode, false);
943 // Redisplay in order to update new mode because it could be not computed before
948 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
950 Handle(AIS_InteractiveContext) aContext = AISContext();
951 if (aContext.IsNull())
954 AISObjectPtr aAISObj = getAISObject(theObject);
958 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
959 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
962 void XGUI_Displayer::deactivateSelectionFilters()
964 Handle(AIS_InteractiveContext) aContext = AISContext();
965 if (!myAndFilter.IsNull()) {
967 if (!aContext.IsNull()) {
968 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
969 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
970 for (; anIt.More() && !aFound; anIt.Next()) {
971 Handle(SelectMgr_Filter) aFilter = anIt.Value();
972 aFound = aFilter == myAndFilter;
975 aContext->RemoveFilter(myAndFilter);
977 myAndFilter.Nullify();
981 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
983 Handle(AIS_InteractiveContext) aContext = AISContext();
984 if (aContext.IsNull() || hasSelectionFilter(theFilter))
987 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
988 if (!aCompositeFilter.IsNull()) {
989 aCompositeFilter->Add(theFilter);
990 #ifdef DEBUG_SELECTION_FILTERS
991 int aCount = aCompositeFilter->StoredFilters().Extent();
992 qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
997 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
999 Handle(AIS_InteractiveContext) aContext = AISContext();
1000 if (aContext.IsNull())
1003 Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
1004 if (!aCompositeFilter.IsNull() && aCompositeFilter->IsIn(theFilter)) {
1005 aCompositeFilter->Remove(theFilter);
1006 #ifdef DEBUG_SELECTION_FILTERS
1007 int aCount = aCompositeFilter->StoredFilters().Extent();
1008 qDebug(QString("removeSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
1013 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
1015 bool aFilterFound = false;
1017 Handle(AIS_InteractiveContext) aContext = AISContext();
1018 if (aContext.IsNull())
1019 return aFilterFound;
1020 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
1021 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
1022 for (; aIt.More() && !aFilterFound; aIt.Next()) {
1023 if (theFilter.Access() == aIt.Value().Access())
1024 aFilterFound = true;
1026 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
1027 if (!aCompositeFilter.IsNull()) {
1028 const SelectMgr_ListOfFilter& aStoredFilters = aCompositeFilter->StoredFilters();
1029 for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
1030 if (theFilter.Access() == aIt.Value().Access())
1031 aFilterFound = true;
1034 return aFilterFound;
1037 void XGUI_Displayer::removeFilters()
1039 Handle(AIS_InteractiveContext) aContext = AISContext();
1040 if (aContext.IsNull())
1043 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
1044 if (!aCompositeFilter.IsNull())
1045 aCompositeFilter->Clear();
1048 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
1050 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
1051 foreach(ObjectPtr aObj, aDispList) {
1052 if (!theList.contains(aObj))
1055 foreach(ObjectPtr aObj, theList) {
1056 if (!isVisible(aObj))
1057 display(aObj, false);
1062 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
1064 if (!isVisible(theObject))
1067 AISObjectPtr aAISObj = getAISObject(theObject);
1068 if (aAISObj.get() == NULL)
1071 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1072 return ::canBeShaded(anAIS, myWorkshop->module());
1075 bool XGUI_Displayer::activate(const Handle(AIS_InteractiveObject)& theIO,
1076 const QIntList& theModes,
1077 const bool theUpdateViewer) const
1079 Handle(AIS_InteractiveContext) aContext = AISContext();
1080 if (aContext.IsNull() || theIO.IsNull())
1083 bool isActivationChanged = false;
1084 // deactivate object in all modes, which are not in the list of activation
1085 // It seems that after the IO deactivation the selected state of the IO's owners
1086 // is modified in OCC(version: 6.8.0) and the selection of the object later is lost.
1087 // By this reason, the number of the IO deactivate is decreased and the object is deactivated
1088 // only if there is a difference in the current modes and the parameters modes.
1089 // If the selection problem happens again, it is possible to write a test scenario and create
1090 // a bug. The bug steps are the following:
1091 // Create two IO, activate them in 5 modes, select the first IO, deactivate 3 modes for both,
1092 // with clicked SHIFT select the second object. The result is the selection of the first IO is lost.
1093 TColStd_ListOfInteger aTColModes;
1094 aContext->ActivatedModes(theIO, aTColModes);
1095 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
1096 QIntList aModesActivatedForIO;
1097 bool isDeactivated = false;
1098 for (; itr.More(); itr.Next() ) {
1099 Standard_Integer aMode = itr.Value();
1100 if (!theModes.contains(aMode)) {
1101 deactivateAIS(theIO, aMode);
1102 isDeactivated = true;
1105 aModesActivatedForIO.append(aMode);
1108 if (isDeactivated) {
1109 // the selection from the previous activation modes should be cleared manually (#26172)
1110 theIO->ClearSelected();
1111 aContext->LocalContext()->ClearOutdatedSelection(theIO, true);
1112 // For performance issues
1113 //if (theUpdateViewer)
1115 isActivationChanged = true;
1118 // loading the interactive object allowing the decomposition
1119 if (aTColModes.IsEmpty()) {
1120 aContext->Load(theIO, -1, true);
1123 // trihedron AIS check should be after the AIS loading.
1124 // If it is not loaded, it is steel selectable in the viewer.
1125 Handle(AIS_Trihedron) aTrihedron;
1126 if (!isTrihedronActive())
1127 aTrihedron = Handle(AIS_Trihedron)::DownCast(theIO);
1128 if (aTrihedron.IsNull()) {
1129 // In order to clear active modes list
1130 if (theModes.size() == 0) {
1131 activateAIS(theIO, 0, theUpdateViewer);
1133 foreach(int aMode, theModes) {
1134 if (!aModesActivatedForIO.contains(aMode)) {
1135 activateAIS(theIO, aMode, theUpdateViewer);
1136 isActivationChanged = true;
1141 return isActivationChanged;
1144 bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
1146 AISObjectPtr anAISObj = getAISObject(theObject);
1147 // correct the result's color it it has the attribute
1148 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1150 // Customization of presentation
1151 GeomCustomPrsPtr aCustomPrs;
1152 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1153 if (aFeature.get() != NULL) {
1154 GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
1155 if (aCustPrs.get() != NULL)
1156 aCustomPrs = aCustPrs;
1158 if (aCustomPrs.get() == NULL) {
1159 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
1160 // we ignore presentable not customized objects
1161 if (aPrs.get() == NULL)
1162 aCustomPrs = myCustomPrs;
1164 bool isCustomized = aCustomPrs.get() &&
1165 aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
1166 return isCustomized;
1170 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject, const QColor& theColor, bool theUpdateViewer)
1172 if (!isVisible(theObject))
1175 AISObjectPtr anAISObj = getAISObject(theObject);
1177 anAISObj->getColor(aR, aG, aB);
1178 anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
1179 if (theUpdateViewer)
1181 return QColor(aR, aG, aB);
1184 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
1186 myResult2AISObjectMap[theObject] = theAIS;
1188 #ifdef DEBUG_DISPLAY
1189 std::ostringstream aPtrStr;
1190 aPtrStr << theObject.get();
1191 qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
1192 qDebug(getResult2AISObjectMapInfo().c_str());
1196 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
1198 QStringList aContent;
1199 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
1200 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
1201 std::ostringstream aPtrStr;
1202 aPtrStr << "aObj = " << aObj.get() << ":";
1203 aPtrStr << "anAIS = " << aAISObj.get() << ":";
1204 aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
1206 aContent.append(aPtrStr.str().c_str());
1208 return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
1209 arg(aContent.join("\n")).toStdString().c_str();
1212 void XGUI_Displayer::activateTrihedron(bool theIsActive)
1214 myIsTrihedronActive = theIsActive;
1215 if (!myIsTrihedronActive) {
1216 deactivateTrihedron(true);
1220 void XGUI_Displayer::displayTrihedron(bool theToDisplay) const
1222 Handle(AIS_InteractiveContext) aContext = AISContext();
1223 if (aContext.IsNull())
1226 Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
1229 if (!aContext->IsDisplayed(aTrihedron))
1230 aContext->Display(aTrihedron,
1232 -1 /* selection mode */,
1233 Standard_True /* update viewer*/,
1234 Standard_False /* allow decomposition */,
1235 AIS_DS_Displayed /* xdisplay status */);
1237 if (!isTrihedronActive())
1238 deactivateTrihedron(false);
1240 activate(aTrihedron, myActiveSelectionModes, false);
1242 deactivateTrihedron(false);
1243 //aContext->LocalContext()->ClearOutdatedSelection(aTrihedron, true);
1244 // the selection from the previous activation modes should be cleared manually (#26172)
1246 aContext->Erase(aTrihedron);
1252 QIntList XGUI_Displayer::activeSelectionModes() const
1255 foreach (int aMode, myActiveSelectionModes) {
1256 // aMode < 9 is a Shape Enum values
1257 aModes << ((aMode < 9)? AIS_Shape::SelectionType(aMode) : aMode);