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)
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 Handle(ModuleBase_ResultPrs) aPrs = Handle(ModuleBase_ResultPrs)::DownCast(theAIS);
196 return !aPrs->isSketchMode();
203 bool XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
204 bool isShading, bool theUpdateViewer)
206 bool aDisplayed = false;
208 Handle(AIS_InteractiveContext) aContext = AISContext();
209 if (aContext.IsNull())
212 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
213 if (!anAISIO.IsNull()) {
214 appendResultObject(theObject, theAIS);
216 bool isCustomized = customizeObject(theObject);
218 int aDispMode = isShading? Shading : Wireframe;
220 anAISIO->Attributes()->SetFaceBoundaryDraw( Standard_True );
221 anAISIO->SetDisplayMode(aDispMode);
222 aContext->Display(anAISIO, aDispMode, 0, false, true, AIS_DS_Displayed);
225 emit objectDisplayed(theObject, theAIS);
226 activate(anAISIO, myActiveSelectionModes, theUpdateViewer);
227 // the fix from VPA for more suitable selection of sketcher lines
228 if(anAISIO->Width() > 1) {
229 for(int aModeIdx = 0; aModeIdx < myActiveSelectionModes.length(); ++aModeIdx) {
230 int aMode = myActiveSelectionModes.value(aModeIdx);
231 double aPrecision = (aMode == getSelectionMode(TopAbs_VERTEX))? 20 :
232 (anAISIO->Width() + 2);
233 aContext->SetSelectionSensitivity(anAISIO, aMode, aPrecision);
243 bool XGUI_Displayer::erase(ObjectPtr theObject, const bool theUpdateViewer)
245 bool aErased = false;
246 if (!isVisible(theObject))
249 Handle(AIS_InteractiveContext) aContext = AISContext();
250 if (aContext.IsNull())
253 AISObjectPtr anObject = myResult2AISObjectMap[theObject];
255 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
256 if (!anAIS.IsNull()) {
257 emit beforeObjectErase(theObject, anObject);
258 aContext->Remove(anAIS, false/*update viewer*/);
262 myResult2AISObjectMap.remove(theObject);
265 std::ostringstream aPtrStr;
266 aPtrStr << theObject.get();
267 qDebug(QString("erase object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
268 qDebug(getResult2AISObjectMapInfo().c_str());
277 bool XGUI_Displayer::redisplay(ObjectPtr theObject, bool theUpdateViewer)
279 bool aRedisplayed = false;
280 if (!isVisible(theObject))
283 AISObjectPtr aAISObj = getAISObject(theObject);
284 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
286 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
288 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
290 aRedisplayed = erase(theObject, theUpdateViewer);
293 if (aAIS_Obj != aAISObj) {
294 appendResultObject(theObject, aAIS_Obj);
296 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
299 Handle(AIS_InteractiveContext) aContext = AISContext();
300 if (!aContext.IsNull() && !aAISIO.IsNull()) {
301 // Check that the visualized shape is the same and the redisplay is not necessary
302 // Redisplay of AIS object leads to this object selection compute and the selection
303 // in the browser is lost
304 // this check is not necessary anymore because the selection store/restore is realized
305 // before and after the values modification.
306 // Moreother, this check avoids customize and redisplay presentation if the presentable
307 // parameter is changed.
308 bool isEqualShapes = false;
309 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
310 if (aResult.get() != NULL) {
311 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(aAISIO);
312 if (!aShapePrs.IsNull()) {
313 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
314 if (aShapePtr.get()) {
315 const TopoDS_Shape& aOldShape = aShapePrs->Shape();
316 if (!aOldShape.IsNull())
317 isEqualShapes = aOldShape.IsEqual(aShapePtr->impl<TopoDS_Shape>());
321 // Customization of presentation
322 bool isCustomized = customizeObject(theObject);
323 #ifdef DEBUG_FEATURE_REDISPLAY
324 qDebug(QString("Redisplay: %1, isEqualShapes=%2, isCustomized=%3").
325 arg(!isEqualShapes || isCustomized).arg(isEqualShapes).arg(isCustomized).toStdString().c_str());
327 if (!isEqualShapes || isCustomized) {
328 /// if shapes are equal and presentation are customized, selection should be restored
329 bool aNeedToRestoreSelection = isEqualShapes && isCustomized;
330 if (aNeedToRestoreSelection)
331 myWorkshop->module()->storeSelection();
333 aContext->Redisplay(aAISIO, false);
335 if (aNeedToRestoreSelection)
336 myWorkshop->module()->restoreSelection();
339 #ifdef DEBUG_FEATURE_REDISPLAY
340 qDebug(" Redisplay happens");
349 void XGUI_Displayer::redisplayObjects()
351 // redisplay objects visualized in the viewer
352 static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
353 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
354 QObjectPtrList aDisplayed = myWorkshop->displayer()->displayedObjects();
355 QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
356 for (; anIt != aLast; anIt++) {
357 aECreator->sendUpdated(*anIt, EVENT_DISP);
359 Events_Loop::loop()->flush(EVENT_DISP);
362 void XGUI_Displayer::deactivate(ObjectPtr theObject, const bool theUpdateViewer)
364 #ifdef DEBUG_DEACTIVATE
365 QString anInfoStr = ModuleBase_Tools::objectInfo(theObject);
366 qDebug(QString("deactivate: myActiveSelectionModes[%1]: %2, objects = ").
367 arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
369 toStdString().c_str());
371 Handle(AIS_InteractiveContext) aContext = AISContext();
372 if (!aContext.IsNull() && isVisible(theObject)) {
373 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
374 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
376 deactivateAIS(anAIS);
377 // the selection from the previous activation modes should be cleared manually (#26172)
378 aContext->LocalContext()->ClearOutdatedSelection(anAIS, true);
384 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
385 const bool theUpdateViewer)
387 //Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
388 //if (!aTrihedron.IsNull())
389 // deactivateAIS(aTrihedron);
391 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
392 for (; anIt != aLast; anIt++) {
393 deactivate(*anIt, false);
395 //VSV It seems that there is no necessity to update viewer on deactivation
396 //if (theUpdateViewer)
400 void XGUI_Displayer::getModesOfActivation(ObjectPtr theObject, QIntList& theModes)
402 Handle(AIS_InteractiveContext) aContext = AISContext();
403 if (aContext.IsNull() || !isVisible(theObject))
406 AISObjectPtr aAISObj = getAISObject(theObject);
408 if (aAISObj.get() != NULL) {
409 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
410 TColStd_ListOfInteger aTColModes;
411 aContext->ActivatedModes(anAISIO, aTColModes);
412 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
413 for (; itr.More(); itr.Next() ) {
414 theModes.append(itr.Value());
419 int XGUI_Displayer::getSelectionMode(int theShapeType)
421 return (theShapeType >= TopAbs_SHAPE)? theShapeType :
422 AIS_Shape::SelectionMode((TopAbs_ShapeEnum)theShapeType);
425 bool XGUI_Displayer::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
427 bool aVisible = false;
428 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
429 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
430 if (aPrs.get() || aResult.get()) {
431 aVisible = theDisplayer->isVisible(theObject);
432 // compsolid is not visualized in the viewer, but should have presentation when all sub solids are
433 // visible. It is useful for highlight presentation where compsolid shape is selectable
434 if (!aVisible && aResult.get() && aResult->groupName() == ModelAPI_ResultCompSolid::group()) {
435 ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResult);
436 if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
437 bool anAllSubsVisible = aCompsolidResult->numberOfSubs() > 0;
438 for(int i = 0; i < aCompsolidResult->numberOfSubs() && anAllSubsVisible; i++) {
439 anAllSubsVisible = theDisplayer->isVisible(aCompsolidResult->subResult(i));
441 aVisible = anAllSubsVisible;
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_ViewerPrs>& 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_ViewerPrs 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_ViewerPrs 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);
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);