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 Handle(AIS_InteractiveObject) anAISPrs = myWorkshop->module()->createPresentation(aResult);
165 if (anAISPrs.IsNull())
166 anAISPrs = new ModuleBase_ResultPrs(aResult);
167 anAIS->setImpl(new Handle(AIS_InteractiveObject)(anAISPrs));
168 //anAIS->createShape(aShapePtr);
171 #ifdef DEBUG_COMPOSILID_DISPLAY
177 aDisplayed = display(theObject, anAIS, isShading, theUpdateViewer);
182 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS, ModuleBase_IModule* theModule)
184 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
185 if (!aShapePrs.IsNull()) {
186 TopoDS_Shape aShape = aShapePrs->Shape();
189 TopAbs_ShapeEnum aType = aShape.ShapeType();
190 if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
193 // Check that the presentation is not a sketch
194 return theModule->canBeShaded(theAIS);
200 bool XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
201 bool isShading, bool theUpdateViewer)
203 bool aDisplayed = false;
205 Handle(AIS_InteractiveContext) aContext = AISContext();
206 if (aContext.IsNull())
209 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
210 if (!anAISIO.IsNull()) {
211 appendResultObject(theObject, theAIS);
213 bool isCustomized = customizeObject(theObject);
215 int aDispMode = isShading? Shading : Wireframe;
217 anAISIO->Attributes()->SetFaceBoundaryDraw( Standard_True );
218 anAISIO->SetDisplayMode(aDispMode);
219 aContext->Display(anAISIO, aDispMode, 0, false, true, AIS_DS_Displayed);
222 emit objectDisplayed(theObject, theAIS);
223 activate(anAISIO, myActiveSelectionModes, theUpdateViewer);
224 // the fix from VPA for more suitable selection of sketcher lines
225 if(anAISIO->Width() > 1) {
226 for(int aModeIdx = 0; aModeIdx < myActiveSelectionModes.length(); ++aModeIdx) {
227 int aMode = myActiveSelectionModes.value(aModeIdx);
228 double aPrecision = (aMode == getSelectionMode(TopAbs_VERTEX))? 20 :
229 (anAISIO->Width() + 2);
230 aContext->SetSelectionSensitivity(anAISIO, aMode, aPrecision);
240 bool XGUI_Displayer::erase(ObjectPtr theObject, const bool theUpdateViewer)
242 bool aErased = false;
243 if (!isVisible(theObject))
246 Handle(AIS_InteractiveContext) aContext = AISContext();
247 if (aContext.IsNull())
250 AISObjectPtr anObject = myResult2AISObjectMap[theObject];
252 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
253 if (!anAIS.IsNull()) {
254 emit beforeObjectErase(theObject, anObject);
255 aContext->Remove(anAIS, false/*update viewer*/);
259 myResult2AISObjectMap.remove(theObject);
262 std::ostringstream aPtrStr;
263 aPtrStr << theObject.get();
264 qDebug(QString("erase object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
265 qDebug(getResult2AISObjectMapInfo().c_str());
274 bool XGUI_Displayer::redisplay(ObjectPtr theObject, bool theUpdateViewer)
276 bool aRedisplayed = false;
277 if (!isVisible(theObject))
280 AISObjectPtr aAISObj = getAISObject(theObject);
281 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
283 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
285 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
287 aRedisplayed = erase(theObject, theUpdateViewer);
290 if (aAIS_Obj != aAISObj) {
291 appendResultObject(theObject, aAIS_Obj);
293 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
296 Handle(AIS_InteractiveContext) aContext = AISContext();
297 if (!aContext.IsNull() && !aAISIO.IsNull()) {
298 // Check that the visualized shape is the same and the redisplay is not necessary
299 // Redisplay of AIS object leads to this object selection compute and the selection
300 // in the browser is lost
301 // this check is not necessary anymore because the selection store/restore is realized
302 // before and after the values modification.
303 // Moreother, this check avoids customize and redisplay presentation if the presentable
304 // parameter is changed.
305 bool isEqualShapes = false;
306 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
307 if (aResult.get() != NULL) {
308 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(aAISIO);
309 if (!aShapePrs.IsNull()) {
310 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
311 if (aShapePtr.get()) {
312 const TopoDS_Shape& aOldShape = aShapePrs->Shape();
313 if (!aOldShape.IsNull())
314 isEqualShapes = aOldShape.IsEqual(aShapePtr->impl<TopoDS_Shape>());
318 // Customization of presentation
319 bool isCustomized = customizeObject(theObject);
320 #ifdef DEBUG_FEATURE_REDISPLAY
321 qDebug(QString("Redisplay: %1, isEqualShapes=%2, isCustomized=%3").
322 arg(!isEqualShapes || isCustomized).arg(isEqualShapes).arg(isCustomized).toStdString().c_str());
324 if (!isEqualShapes || isCustomized) {
325 /// if shapes are equal and presentation are customized, selection should be restored
326 bool aNeedToRestoreSelection = isEqualShapes && isCustomized;
327 if (aNeedToRestoreSelection)
328 myWorkshop->module()->storeSelection();
330 aContext->Redisplay(aAISIO, false);
332 if (aNeedToRestoreSelection)
333 myWorkshop->module()->restoreSelection();
336 #ifdef DEBUG_FEATURE_REDISPLAY
337 qDebug(" Redisplay happens");
346 void XGUI_Displayer::redisplayObjects()
348 // redisplay objects visualized in the viewer
349 static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
350 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
351 QObjectPtrList aDisplayed = myWorkshop->displayer()->displayedObjects();
352 QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
353 for (; anIt != aLast; anIt++) {
354 aECreator->sendUpdated(*anIt, EVENT_DISP);
356 Events_Loop::loop()->flush(EVENT_DISP);
359 void XGUI_Displayer::deactivate(ObjectPtr theObject, const bool theUpdateViewer)
361 #ifdef DEBUG_DEACTIVATE
362 QString anInfoStr = ModuleBase_Tools::objectInfo(theObject);
363 qDebug(QString("deactivate: myActiveSelectionModes[%1]: %2, objects = ").
364 arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
366 toStdString().c_str());
368 Handle(AIS_InteractiveContext) aContext = AISContext();
369 if (!aContext.IsNull() && isVisible(theObject)) {
370 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
371 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
373 deactivateAIS(anAIS);
374 // the selection from the previous activation modes should be cleared manually (#26172)
375 aContext->LocalContext()->ClearOutdatedSelection(anAIS, true);
381 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
382 const bool theUpdateViewer)
384 //Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
385 //if (!aTrihedron.IsNull())
386 // deactivateAIS(aTrihedron);
388 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
389 for (; anIt != aLast; anIt++) {
390 deactivate(*anIt, false);
392 //VSV It seems that there is no necessity to update viewer on deactivation
393 //if (theUpdateViewer)
397 void XGUI_Displayer::getModesOfActivation(ObjectPtr theObject, QIntList& theModes)
399 Handle(AIS_InteractiveContext) aContext = AISContext();
400 if (aContext.IsNull() || !isVisible(theObject))
403 AISObjectPtr aAISObj = getAISObject(theObject);
405 if (aAISObj.get() != NULL) {
406 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
407 TColStd_ListOfInteger aTColModes;
408 aContext->ActivatedModes(anAISIO, aTColModes);
409 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
410 for (; itr.More(); itr.Next() ) {
411 theModes.append(itr.Value());
416 int XGUI_Displayer::getSelectionMode(int theShapeType)
418 return (theShapeType >= TopAbs_SHAPE)? theShapeType :
419 AIS_Shape::SelectionMode((TopAbs_ShapeEnum)theShapeType);
422 bool XGUI_Displayer::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
424 bool aVisible = false;
425 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
426 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
427 if (aPrs.get() || aResult.get()) {
428 aVisible = theDisplayer->isVisible(theObject);
429 // compsolid is not visualized in the viewer, but should have presentation when all sub solids are
430 // visible. It is useful for highlight presentation where compsolid shape is selectable
431 if (!aVisible && aResult.get() && aResult->groupName() == ModelAPI_ResultCompSolid::group()) {
432 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResult);
433 if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
434 bool anAllSubsVisible = aCompsolidResult->numberOfSubs() > 0;
435 for(int i = 0; i < aCompsolidResult->numberOfSubs() && anAllSubsVisible; i++) {
436 anAllSubsVisible = theDisplayer->isVisible(aCompsolidResult->subResult(i));
438 aVisible = anAllSubsVisible;
443 // check if all results of the feature are visible
444 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
445 std::list<ResultPtr> aResults = aFeature->results();
446 std::list<ResultPtr>::const_iterator aIt;
447 aVisible = !aResults.empty();
448 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
449 aVisible = aVisible && theDisplayer->isVisible(*aIt);
456 void XGUI_Displayer::activateObjects(const QIntList& theModes, const QObjectPtrList& theObjList,
457 const bool theUpdateViewer)
459 // Convert shape types to selection types
461 foreach(int aType, theModes) {
462 aModes.append(getSelectionMode(aType));
465 #ifdef DEBUG_ACTIVATE_OBJECTS
467 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
468 for (; anIt != aLast; ++anIt) {
469 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
471 QString anInfoStr = anInfo.join(", ");
473 qDebug(QString("activateObjects: aModes[%1] = %2, myActiveSelectionModes[%3] = %4, objects = %5").
474 arg(aModes.size()).arg(qIntListInfo(aModes)).
475 arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
477 toStdString().c_str());
479 // In order to avoid doblications of selection modes
481 foreach (int aMode, aModes) {
482 if (!aNewModes.contains(aMode))
483 aNewModes.append(aMode);
485 myActiveSelectionModes = aNewModes;
486 Handle(AIS_InteractiveContext) aContext = AISContext();
487 // Open local context if there is no one
488 if (aContext.IsNull() || !aContext->HasOpenedContext())
491 //aContext->UseDisplayedObjects();
492 //myUseExternalObjects = true;
494 Handle(AIS_InteractiveObject) anAISIO;
495 AIS_ListOfInteractive aPrsList;
496 //if (aObjList.isEmpty())
499 foreach(ObjectPtr aObj, theObjList) {
500 if (myResult2AISObjectMap.contains(aObj))
501 aPrsList.Append(myResult2AISObjectMap[aObj]->impl<Handle(AIS_InteractiveObject)>());
505 // Add trihedron because it has to partisipate in selection
506 Handle(AIS_InteractiveObject) aTrihedron;
507 if (isTrihedronActive()) {
508 aTrihedron = getTrihedron();
509 if (!aTrihedron.IsNull() && aContext->IsDisplayed(aTrihedron))
510 aPrsList.Append(aTrihedron);
512 if (aPrsList.Extent() == 0)
515 AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
516 bool isActivationChanged = false;
517 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
518 anAISIO = aLIt.Value();
519 if (activate(anAISIO, myActiveSelectionModes, false))
520 isActivationChanged = true;
522 if (!aTrihedron.IsNull()) {
523 foreach(int aMode, myActiveSelectionModes)
524 aContext->SetSelectionSensitivity(aTrihedron, aMode, 20);
526 // VSV It seems that there is no necessity to update viewer on activation
527 //if (theUpdateViewer && isActivationChanged)
531 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
533 Handle(AIS_InteractiveContext) aContext = AISContext();
534 if (aContext.IsNull() || !isVisible(theObject))
537 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
538 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
540 TColStd_ListOfInteger aModes;
541 aContext->ActivatedModes(anAIS, aModes);
542 return aModes.Extent() > 0;
544 void XGUI_Displayer::setSelected(const QList<ModuleBase_ViewerPrs>& theValues, bool theUpdateViewer)
546 Handle(AIS_InteractiveContext) aContext = AISContext();
547 if (aContext.IsNull())
549 if (aContext->HasOpenedContext()) {
550 aContext->UnhilightSelected(false);
551 aContext->ClearSelected(false);
552 foreach (ModuleBase_ViewerPrs aPrs, theValues) {
553 const GeomShapePtr& aGeomShape = aPrs.shape();
554 if (aGeomShape.get() && !aGeomShape->isNull()) {
555 const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
556 aContext->AddOrRemoveSelected(aShape, false);
558 ObjectPtr anObject = aPrs.object();
559 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
560 if (aResult.get() && isVisible(aResult)) {
561 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
562 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
563 if (!anAIS.IsNull()) {
564 // The methods are replaced in order to provide multi-selection, e.g. restore selection
565 // by activating multi selector widget. It also gives an advantage that the multi
566 // selection in OB gives multi-selection in the viewer
567 //aContext->SetSelected(anAIS, false);
568 // The selection in the context was cleared, so the method sets the objects are selected
569 aContext->AddOrRemoveSelected(anAIS, false);
575 aContext->UnhilightCurrents(false);
576 aContext->ClearCurrents(false);
577 foreach (ModuleBase_ViewerPrs aPrs, theValues) {
578 ObjectPtr anObject = aPrs.object();
579 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
580 if (aResult.get() && isVisible(aResult)) {
581 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
582 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
584 aContext->SetCurrentObject(anAIS, false);
592 void XGUI_Displayer::clearSelected()
594 Handle(AIS_InteractiveContext) aContext = AISContext();
595 if (!aContext.IsNull()) {
596 aContext->UnhilightCurrents(false);
597 aContext->ClearSelected();
601 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
603 bool aErased = false;
604 Handle(AIS_InteractiveContext) aContext = AISContext();
605 if (!aContext.IsNull()) {
606 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
607 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
609 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
610 if (!anIO.IsNull()) {
611 emit beforeObjectErase(aObj, aAISObj);
612 aContext->Remove(anIO, false/*update viewer*/);
619 myResult2AISObjectMap.clear();
622 qDebug(getResult2AISObjectMapInfo().c_str());
627 void deactivateObject(Handle(AIS_InteractiveContext) theContext,
628 Handle(AIS_InteractiveObject) theObject,
629 const bool theClear = true)
631 if (!theObject.IsNull()) {
632 theContext->Deactivate(theObject);
634 //theObject->ClearSelected();
635 // theContext->LocalContext()->ClearOutdatedSelection(theObject, true);
640 void XGUI_Displayer::deactivateTrihedron(const bool theUpdateViewer) const
642 Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
643 Handle(AIS_InteractiveContext) aContext = AISContext();
644 if (!aTrihedron.IsNull() && aContext->IsDisplayed(aTrihedron)) {
645 Handle(AIS_Trihedron) aTrie = Handle(AIS_Trihedron)::DownCast(aTrihedron);
646 deactivateObject(aContext, aTrie);
648 /// #1136 hidden axis are selected in sketch
649 /// workaround for Cascade: there is a crash in AIS_LocalContext::ClearOutdatedSelection
650 /// for Position AIS object in SelectionModes.
651 deactivateObject(aContext, aTrie->XAxis());
652 deactivateObject(aContext, aTrie->YAxis());
653 deactivateObject(aContext, aTrie->Axis());
654 deactivateObject(aContext, aTrie->Position());
656 deactivateObject(aContext, aTrie->XYPlane());
657 deactivateObject(aContext, aTrie->XZPlane());
658 deactivateObject(aContext, aTrie->YZPlane());
665 Handle(AIS_InteractiveObject) XGUI_Displayer::getTrihedron() const
667 return myWorkshop->viewer()->trihedron();
670 void XGUI_Displayer::openLocalContext()
672 Handle(AIS_InteractiveContext) aContext = AISContext();
673 // Open local context if there is no one
674 if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
675 // Preserve selected objects
676 //AIS_ListOfInteractive aAisList;
677 //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
678 // aAisList.Append(aContext->Current());
680 // get the filters from the global context and append them to the local context
681 // a list of filters in the global context is not cleared and should be cleared here
682 SelectMgr_ListOfFilter aFilters;
683 aFilters.Assign(aContext->Filters());
684 // it is important to remove the filters in the global context, because there is a code
685 // in the closeLocalContex, which restore the global context filters
686 aContext->RemoveFilters();
688 //aContext->ClearCurrents();
689 aContext->OpenLocalContext();
690 //deactivateTrihedron();
691 //aContext->NotUseDisplayedObjects();
693 //myUseExternalObjects = false;
695 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
696 for (;aIt.More(); aIt.Next()) {
697 aContext->AddFilter(aIt.Value());
700 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
701 //for(; aIt2.More(); aIt2.Next()) {
702 // aContext->SetSelected(aIt2.Value(), false);
707 void XGUI_Displayer::closeLocalContexts(const bool theUpdateViewer)
709 Handle(AIS_InteractiveContext) aContext = AISContext();
710 if (!aContext.IsNull() && aContext->HasOpenedContext()) {
711 // Preserve selected objects
712 //AIS_ListOfInteractive aAisList;
713 //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
714 // aAisList.Append(aContext->SelectedInteractive());
716 // get the filters from the local context and append them to the global context
717 // a list of filters in the local context is cleared
718 SelectMgr_ListOfFilter aFilters;
719 aFilters.Assign(aContext->Filters());
721 //aContext->ClearSelected();
722 aContext->CloseAllContexts(false);
724 // From the moment when the AIS_DS_Displayed flag is used in the Display of AIS object,
725 // this code is obsolete. It is temporaty commented and should be removed after
726 // the test campaign.
727 // Redisplay all object if they were displayed in localContext
728 /*Handle(AIS_InteractiveObject) aAISIO;
729 foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
730 aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
731 if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
732 aContext->Display(aAISIO, false);
733 aContext->SetDisplayMode(aAISIO, Shading, false);
737 // Append the filters from the local selection in the global selection context
738 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
739 for (;aIt.More(); aIt.Next()) {
740 Handle(SelectMgr_Filter) aFilter = aIt.Value();
741 aContext->AddFilter(aFilter);
746 //myUseExternalObjects = false;
749 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
750 //for(; aIt2.More(); aIt2.Next()) {
751 // if (aContext->IsDisplayed(aIt2.Value()))
752 // aContext->SetCurrentObject(aIt2.Value(), false);
757 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
760 if (myResult2AISObjectMap.contains(theObject))
761 anIO = myResult2AISObjectMap[theObject];
765 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
767 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
768 return getObject(aRefAIS);
771 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
774 foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
775 AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
776 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
782 if (!anObject.get()) {
783 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
784 if (!theIO.IsNull()) {
785 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
787 anObject = myWorkshop->module()->findPresentedObject(anAISObj);
792 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
794 bool aWasEnabled = isUpdateEnabled();
796 myViewerBlockedRecursiveCount--;
798 myViewerBlockedRecursiveCount++;
800 if (myNeedUpdate && isUpdateEnabled()) {
802 myNeedUpdate = false;
807 bool XGUI_Displayer::isUpdateEnabled() const
809 return myViewerBlockedRecursiveCount == 0;
812 void XGUI_Displayer::updateViewer() const
814 Handle(AIS_InteractiveContext) aContext = AISContext();
815 if (!aContext.IsNull() && isUpdateEnabled()) {
816 myWorkshop->viewer()->Zfitall();
817 aContext->UpdateCurrentViewer();
823 void XGUI_Displayer::activateAIS(const Handle(AIS_InteractiveObject)& theIO,
824 const int theMode, const bool theUpdateViewer) const
826 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
827 if (!aContext.IsNull()) {
828 aContext->Activate(theIO, theMode, false);
830 #ifdef DEBUG_ACTIVATE_AIS
831 ObjectPtr anObject = getObject(theIO);
832 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
833 qDebug(QString("activateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
840 void XGUI_Displayer::deactivateAIS(const Handle(AIS_InteractiveObject)& theIO, const int theMode) const
842 Handle(AIS_InteractiveContext) aContext = AISContext();
843 if (!aContext.IsNull()) {
845 aContext->Deactivate(theIO);
847 aContext->Deactivate(theIO, theMode);
849 #ifdef DEBUG_DEACTIVATE_AIS
850 ObjectPtr anObject = getObject(theIO);
851 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
852 qDebug(QString("deactivateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
857 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
859 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
860 if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
861 aContext->OpenLocalContext();
862 if (!isTrihedronActive())
863 deactivateTrihedron(true);
864 aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
865 aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
870 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
872 Handle(AIS_InteractiveContext) aContext = AISContext();
873 if (!aContext.IsNull() && myAndFilter.IsNull()) {
874 myAndFilter = new SelectMgr_AndFilter();
875 aContext->AddFilter(myAndFilter);
880 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
881 bool theUpdateViewer)
883 bool aDisplayed = false;
884 Handle(AIS_InteractiveContext) aContext = AISContext();
885 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
886 if (!aContext.IsNull() && !anAISIO.IsNull()) {
887 aContext->Display(anAISIO, 0/*wireframe*/, 0, false/*update viewer*/, true, AIS_DS_Displayed);
889 aContext->Deactivate(anAISIO);
890 aContext->Load(anAISIO);
891 if (toActivateInSelectionModes) {
892 if (aContext->HasOpenedContext()) {
893 if (myActiveSelectionModes.size() == 0)
894 activateAIS(anAISIO, 0, theUpdateViewer);
896 foreach(int aMode, myActiveSelectionModes) {
897 activateAIS(anAISIO, aMode, theUpdateViewer);
908 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
910 bool aErased = false;
911 Handle(AIS_InteractiveContext) aContext = AISContext();
912 if (!aContext.IsNull()) {
913 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
914 if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
915 aContext->Remove(anAISIO, false/*update viewer*/);
919 if (aErased && theUpdateViewer)
925 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
927 if (theMode == NoMode)
930 Handle(AIS_InteractiveContext) aContext = AISContext();
931 if (aContext.IsNull())
934 AISObjectPtr aAISObj = getAISObject(theObject);
938 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
939 aContext->SetDisplayMode(aAISIO, theMode, false);
940 // Redisplay in order to update new mode because it could be not computed before
945 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
947 Handle(AIS_InteractiveContext) aContext = AISContext();
948 if (aContext.IsNull())
951 AISObjectPtr aAISObj = getAISObject(theObject);
955 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
956 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
959 void XGUI_Displayer::deactivateSelectionFilters()
961 Handle(AIS_InteractiveContext) aContext = AISContext();
962 if (!myAndFilter.IsNull()) {
964 if (!aContext.IsNull()) {
965 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
966 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
967 for (; anIt.More() && !aFound; anIt.Next()) {
968 Handle(SelectMgr_Filter) aFilter = anIt.Value();
969 aFound = aFilter == myAndFilter;
972 aContext->RemoveFilter(myAndFilter);
974 myAndFilter.Nullify();
978 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
980 Handle(AIS_InteractiveContext) aContext = AISContext();
981 if (aContext.IsNull() || hasSelectionFilter(theFilter))
984 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
985 if (!aCompositeFilter.IsNull()) {
986 aCompositeFilter->Add(theFilter);
987 #ifdef DEBUG_SELECTION_FILTERS
988 int aCount = aCompositeFilter->StoredFilters().Extent();
989 qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
994 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
996 Handle(AIS_InteractiveContext) aContext = AISContext();
997 if (aContext.IsNull())
1000 Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
1001 if (!aCompositeFilter.IsNull() && aCompositeFilter->IsIn(theFilter)) {
1002 aCompositeFilter->Remove(theFilter);
1003 #ifdef DEBUG_SELECTION_FILTERS
1004 int aCount = aCompositeFilter->StoredFilters().Extent();
1005 qDebug(QString("removeSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
1010 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
1012 bool aFilterFound = false;
1014 Handle(AIS_InteractiveContext) aContext = AISContext();
1015 if (aContext.IsNull())
1016 return aFilterFound;
1017 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
1018 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
1019 for (; aIt.More() && !aFilterFound; aIt.Next()) {
1020 if (theFilter.Access() == aIt.Value().Access())
1021 aFilterFound = true;
1023 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
1024 if (!aCompositeFilter.IsNull()) {
1025 const SelectMgr_ListOfFilter& aStoredFilters = aCompositeFilter->StoredFilters();
1026 for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
1027 if (theFilter.Access() == aIt.Value().Access())
1028 aFilterFound = true;
1031 return aFilterFound;
1034 void XGUI_Displayer::removeFilters()
1036 Handle(AIS_InteractiveContext) aContext = AISContext();
1037 if (aContext.IsNull())
1040 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
1041 if (!aCompositeFilter.IsNull())
1042 aCompositeFilter->Clear();
1045 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
1047 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
1048 foreach(ObjectPtr aObj, aDispList) {
1049 if (!theList.contains(aObj))
1052 foreach(ObjectPtr aObj, theList) {
1053 if (!isVisible(aObj))
1054 display(aObj, false);
1059 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
1061 if (!isVisible(theObject))
1064 AISObjectPtr aAISObj = getAISObject(theObject);
1065 if (aAISObj.get() == NULL)
1068 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1069 return ::canBeShaded(anAIS, myWorkshop->module());
1072 bool XGUI_Displayer::activate(const Handle(AIS_InteractiveObject)& theIO,
1073 const QIntList& theModes,
1074 const bool theUpdateViewer) const
1076 Handle(AIS_InteractiveContext) aContext = AISContext();
1077 if (aContext.IsNull() || theIO.IsNull())
1080 bool isActivationChanged = false;
1081 // deactivate object in all modes, which are not in the list of activation
1082 // It seems that after the IO deactivation the selected state of the IO's owners
1083 // is modified in OCC(version: 6.8.0) and the selection of the object later is lost.
1084 // By this reason, the number of the IO deactivate is decreased and the object is deactivated
1085 // only if there is a difference in the current modes and the parameters modes.
1086 // If the selection problem happens again, it is possible to write a test scenario and create
1087 // a bug. The bug steps are the following:
1088 // Create two IO, activate them in 5 modes, select the first IO, deactivate 3 modes for both,
1089 // with clicked SHIFT select the second object. The result is the selection of the first IO is lost.
1090 TColStd_ListOfInteger aTColModes;
1091 aContext->ActivatedModes(theIO, aTColModes);
1092 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
1093 QIntList aModesActivatedForIO;
1094 bool isDeactivated = false;
1095 for (; itr.More(); itr.Next() ) {
1096 Standard_Integer aMode = itr.Value();
1097 if (!theModes.contains(aMode)) {
1098 deactivateAIS(theIO, aMode);
1099 isDeactivated = true;
1102 aModesActivatedForIO.append(aMode);
1105 if (isDeactivated) {
1106 // the selection from the previous activation modes should be cleared manually (#26172)
1107 theIO->ClearSelected();
1108 aContext->LocalContext()->ClearOutdatedSelection(theIO, true);
1109 // For performance issues
1110 //if (theUpdateViewer)
1112 isActivationChanged = true;
1115 // loading the interactive object allowing the decomposition
1116 if (aTColModes.IsEmpty()) {
1117 aContext->Load(theIO, -1, true);
1120 // trihedron AIS check should be after the AIS loading.
1121 // If it is not loaded, it is steel selectable in the viewer.
1122 Handle(AIS_Trihedron) aTrihedron;
1123 if (!isTrihedronActive())
1124 aTrihedron = Handle(AIS_Trihedron)::DownCast(theIO);
1125 if (aTrihedron.IsNull()) {
1126 // In order to clear active modes list
1127 if (theModes.size() == 0) {
1128 activateAIS(theIO, 0, theUpdateViewer);
1130 foreach(int aMode, theModes) {
1131 if (!aModesActivatedForIO.contains(aMode)) {
1132 activateAIS(theIO, aMode, theUpdateViewer);
1133 isActivationChanged = true;
1138 return isActivationChanged;
1141 bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
1143 AISObjectPtr anAISObj = getAISObject(theObject);
1144 // correct the result's color it it has the attribute
1145 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1147 // Customization of presentation
1148 GeomCustomPrsPtr aCustomPrs;
1149 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1150 if (aFeature.get() != NULL) {
1151 GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
1152 if (aCustPrs.get() != NULL)
1153 aCustomPrs = aCustPrs;
1155 if (aCustomPrs.get() == NULL) {
1156 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
1157 // we ignore presentable not customized objects
1158 if (aPrs.get() == NULL)
1159 aCustomPrs = myCustomPrs;
1161 bool isCustomized = aCustomPrs.get() &&
1162 aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
1163 return isCustomized;
1167 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject, const QColor& theColor, bool theUpdateViewer)
1169 if (!isVisible(theObject))
1172 AISObjectPtr anAISObj = getAISObject(theObject);
1174 anAISObj->getColor(aR, aG, aB);
1175 anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
1176 if (theUpdateViewer)
1178 return QColor(aR, aG, aB);
1181 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
1183 myResult2AISObjectMap[theObject] = theAIS;
1185 #ifdef DEBUG_DISPLAY
1186 std::ostringstream aPtrStr;
1187 aPtrStr << theObject.get();
1188 qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
1189 qDebug(getResult2AISObjectMapInfo().c_str());
1193 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
1195 QStringList aContent;
1196 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
1197 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
1198 std::ostringstream aPtrStr;
1199 aPtrStr << "aObj = " << aObj.get() << ":";
1200 aPtrStr << "anAIS = " << aAISObj.get() << ":";
1201 aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
1203 aContent.append(aPtrStr.str().c_str());
1205 return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
1206 arg(aContent.join("\n")).toStdString().c_str();
1209 void XGUI_Displayer::activateTrihedron(bool theIsActive)
1211 myIsTrihedronActive = theIsActive;
1212 if (!myIsTrihedronActive) {
1213 deactivateTrihedron(true);
1217 void XGUI_Displayer::displayTrihedron(bool theToDisplay) const
1219 Handle(AIS_InteractiveContext) aContext = AISContext();
1220 if (aContext.IsNull())
1223 Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
1226 if (!aContext->IsDisplayed(aTrihedron))
1227 aContext->Display(aTrihedron,
1229 -1 /* selection mode */,
1230 Standard_True /* update viewer*/,
1231 Standard_False /* allow decomposition */,
1232 AIS_DS_Displayed /* xdisplay status */);
1234 if (!isTrihedronActive())
1235 deactivateTrihedron(false);
1237 activate(aTrihedron, myActiveSelectionModes, false);
1239 deactivateTrihedron(false);
1240 //aContext->LocalContext()->ClearOutdatedSelection(aTrihedron, true);
1241 // the selection from the previous activation modes should be cleared manually (#26172)
1243 aContext->Erase(aTrihedron);