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"
14 #include <AppElements_Viewer.h>
16 #include <ModelAPI_Document.h>
17 #include <ModelAPI_Data.h>
18 #include <ModelAPI_Object.h>
19 #include <ModelAPI_Tools.h>
20 #include <ModelAPI_AttributeIntArray.h>
22 #include <ModuleBase_ResultPrs.h>
23 #include <ModuleBase_Tools.h>
25 #include <GeomAPI_Shape.h>
26 #include <GeomAPI_IPresentable.h>
27 #include <GeomAPI_ICustomPrs.h>
29 #include <AIS_InteractiveContext.hxx>
30 #include <AIS_LocalContext.hxx>
31 #include <AIS_ListOfInteractive.hxx>
32 #include <AIS_ListIteratorOfListOfInteractive.hxx>
33 #include <AIS_DimensionSelectionMode.hxx>
34 #include <AIS_Shape.hxx>
35 #include <AIS_Dimension.hxx>
36 #include <TColStd_ListIteratorOfListOfInteger.hxx>
37 #include <SelectMgr_ListOfFilter.hxx>
38 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
40 #include <StdSelect_ViewerSelector3d.hxx>
42 #include <TColStd_MapOfTransient.hxx>
43 #include <TColStd_MapIteratorOfMapOfTransient.hxx>
47 const int MOUSE_SENSITIVITY_IN_PIXEL = 10; ///< defines the local context mouse selection sensitivity
49 //#define DEBUG_DISPLAY
50 #define DEBUG_ACTIVATE
51 //#define DEBUG_FEATURE_REDISPLAY
52 //#define DEBUG_SELECTION_FILTERS
53 //#define DEBUG_USE_CLEAR_OUTDATED_SELECTION
55 // Workaround for bug #25637
56 void displayedObjects(const Handle(AIS_InteractiveContext)& theAIS, AIS_ListOfInteractive& theList)
58 // Get from null point
59 theAIS->DisplayedObjects(theList, true);
60 if (theAIS->HasOpenedContext()) {
61 // get from local context
62 const Handle(AIS_LocalContext)& aLC = theAIS->LocalContext();
63 TColStd_MapOfTransient aMap;
64 int NbDisp = aLC->DisplayedObjects(aMap);
65 TColStd_MapIteratorOfMapOfTransient aIt(aMap);
67 Handle(AIS_InteractiveObject) curIO;
68 Handle(Standard_Transient) Tr;
69 for(; aIt.More(); aIt.Next()){
71 curIO = *((Handle(AIS_InteractiveObject)*) &Tr);
72 theList.Append(curIO);
78 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
79 : myWorkshop(theWorkshop)
81 enableUpdateViewer(true);
82 myCustomPrs = std::shared_ptr<GeomAPI_ICustomPrs>(new XGUI_CustomPrs());
85 XGUI_Displayer::~XGUI_Displayer()
89 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
91 return myResult2AISObjectMap.contains(theObject);
94 void XGUI_Displayer::display(ObjectPtr theObject, bool isUpdateViewer)
96 if (isVisible(theObject)) {
97 redisplay(theObject, isUpdateViewer);
101 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
102 bool isShading = false;
103 if (aPrs.get() != NULL) {
104 anAIS = aPrs->getAISObject(anAIS);
106 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
107 if (aResult.get() != NULL) {
108 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
109 if (aShapePtr.get() != NULL) {
110 anAIS = AISObjectPtr(new GeomAPI_AISObject());
111 anAIS->setImpl(new Handle(AIS_InteractiveObject)(new ModuleBase_ResultPrs(aResult)));
112 //anAIS->createShape(aShapePtr);
118 display(theObject, anAIS, isShading, isUpdateViewer);
122 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS)
124 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
125 if (!aShapePrs.IsNull()) {
126 TopoDS_Shape aShape = aShapePrs->Shape();
127 TopAbs_ShapeEnum aType = aShape.ShapeType();
128 if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
131 // Check that the presentation is not a sketch
132 Handle(ModuleBase_ResultPrs) aPrs = Handle(ModuleBase_ResultPrs)::DownCast(theAIS);
134 return !aPrs->isSketchMode();
141 void XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
142 bool isShading, bool isUpdateViewer)
144 Handle(AIS_InteractiveContext) aContext = AISContext();
145 if (aContext.IsNull())
148 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
149 if (!anAISIO.IsNull()) {
150 appendResultObject(theObject, theAIS);
152 bool aCanBeShaded = ::canBeShaded(anAISIO);
153 // In order to avoid extra closing/opening context
154 SelectMgr_IndexedMapOfOwner aSelectedOwners;
156 myWorkshop->selector()->selection()->selectedOwners(aSelectedOwners);
157 closeLocalContexts(false);
159 aContext->Display(anAISIO, false);
160 aContext->SetDisplayMode(anAISIO, isShading? Shading : Wireframe, false);
162 anAISIO->Attributes()->SetFaceBoundaryDraw( Standard_True );
163 emit objectDisplayed(theObject, theAIS);
165 bool isCustomized = customizeObject(theObject);
167 aContext->Redisplay(anAISIO, false);
171 activateObjects(myActiveSelectionModes);
172 myWorkshop->selector()->setSelectedOwners(aSelectedOwners, false);
175 activate(anAISIO, myActiveSelectionModes);
181 void XGUI_Displayer::erase(ObjectPtr theObject, const bool isUpdateViewer)
183 if (!isVisible(theObject))
186 Handle(AIS_InteractiveContext) aContext = AISContext();
187 if (aContext.IsNull())
189 AISObjectPtr anObject = myResult2AISObjectMap[theObject];
191 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
192 if (!anAIS.IsNull()) {
193 emit beforeObjectErase(theObject, anObject);
194 aContext->Remove(anAIS, isUpdateViewer);
197 myResult2AISObjectMap.remove(theObject);
200 void XGUI_Displayer::redisplay(ObjectPtr theObject, bool isUpdateViewer)
202 if (!isVisible(theObject))
205 AISObjectPtr aAISObj = getAISObject(theObject);
206 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
208 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
210 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
212 erase(theObject, isUpdateViewer);
215 if (aAIS_Obj != aAISObj) {
216 appendResultObject(theObject, aAIS_Obj);
218 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
221 if (!aAISIO.IsNull()) {
222 Handle(AIS_InteractiveContext) aContext = AISContext();
223 if (aContext.IsNull())
225 // Check that the visualized shape is the same and the redisplay is not necessary
226 // Redisplay of AIS object leads to this object selection compute and the selection
227 // in the browser is lost
229 // this check is not necessary anymore because the selection store/restore is realized
230 // before and after the values modification.
231 // Moreother, this check avoids customize and redisplay presentation if the presentable
232 // parameter is changed.
233 bool isEqualShapes = false;
234 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
235 if (aResult.get() != NULL) {
236 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(aAISIO);
237 if (!aShapePrs.IsNull()) {
238 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
239 if (aShapePtr.get()) {
240 const TopoDS_Shape& aOldShape = aShapePrs->Shape();
241 isEqualShapes = aOldShape.IsEqual(aShapePtr->impl<TopoDS_Shape>());
245 // Customization of presentation
246 bool isCustomized = customizeObject(theObject);
247 #ifdef DEBUG_FEATURE_REDISPLAY
248 //qDebug(QString("Redisplay: %1, isEqualShapes=%2, isCustomized=%3").
249 // arg(!isEqualShapes || isCustomized).arg(isEqualShapes).arg(isCustomized).toStdString().c_str());
251 if (!isEqualShapes || isCustomized) {
252 aContext->Redisplay(aAISIO, false);
253 #ifdef DEBUG_FEATURE_REDISPLAY
254 //qDebug(" Redisplay happens");
262 void XGUI_Displayer::deactivate(ObjectPtr theObject)
264 if (isVisible(theObject)) {
265 Handle(AIS_InteractiveContext) aContext = AISContext();
266 if (aContext.IsNull())
269 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
270 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
271 aContext->Deactivate(anAIS);
272 #ifdef DEBUG_USE_CLEAR_OUTDATED_SELECTION
273 aContext->LocalContext()->ClearOutdatedSelection(anAIS, true);
279 /*void XGUI_Displayer::activate(ObjectPtr theFeature)
281 activate(theFeature, myActiveSelectionModes);
284 void XGUI_Displayer::activate(ObjectPtr theObject, const QIntList& theModes)
286 #ifdef DEBUG_ACTIVATE
287 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
289 if (aFeature.get() != NULL) {
291 getModesOfActivation(theObject, aModes);
294 qDebug(QString("activate feature: %1, theModes: %2, myActiveSelectionModes: %3, getModesOf: %4").
295 arg(aFeature->data()->name().c_str()).
296 arg(theModes.size()).
297 arg(myActiveSelectionModes.size()).
298 arg(aModes.size()).toStdString().c_str());
302 if (isVisible(theObject)) {
303 Handle(AIS_InteractiveContext) aContext = AISContext();
304 if (aContext.IsNull())
307 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
308 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
310 activate(anAIS, theModes);
314 void XGUI_Displayer::getModesOfActivation(ObjectPtr theObject, QIntList& theModes)
316 if (!isVisible(theObject))
319 Handle(AIS_InteractiveContext) aContext = AISContext();
320 if (aContext.IsNull())
323 AISObjectPtr aAISObj = getAISObject(theObject);
325 if (aAISObj.get() != NULL) {
326 Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
327 TColStd_ListOfInteger aTColModes;
328 aContext->ActivatedModes(anAISIO, aTColModes);
329 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
330 for (; itr.More(); itr.Next() ) {
331 theModes.append(itr.Value());
336 void XGUI_Displayer::activateObjects(const QIntList& theModes, const QObjectPtrList& theObjList)
338 #ifdef DEBUG_ACTIVATE
339 qDebug(QString("activate all features: theModes: %2, myActiveSelectionModes: %3").
340 arg(theModes.size()).
341 arg(myActiveSelectionModes.size()).
342 toStdString().c_str());
344 // In order to avoid doblications of selection modes
346 foreach (int aMode, theModes) {
347 if (!aNewModes.contains(aMode))
348 aNewModes.append(aMode);
350 myActiveSelectionModes = aNewModes;
351 Handle(AIS_InteractiveContext) aContext = AISContext();
352 if (aContext.IsNull())
354 // Open local context if there is no one
355 if (!aContext->HasOpenedContext())
358 // we need to block the sort of the viewer selector during deactivate/activate because
359 // it takes a lot of time if there are a many objects are processed. It can be performed
360 // manualy after the activation is peformed
361 //Handle(StdSelect_ViewerSelector3d) aSelector = aContext->LocalContext()->MainSelector();
362 //bool isUpdateSortPossible = !aSelector.IsNull() && aSelector->IsUpdateSortPossible();
363 //if (!aSelector.IsNull())
364 // aSelector->SetUpdateSortPossible(false);
366 //aContext->UseDisplayedObjects();
367 //myUseExternalObjects = true;
369 Handle(AIS_InteractiveObject) anAISIO;
370 AIS_ListOfInteractive aPrsList;
371 if (theObjList.isEmpty())
372 ::displayedObjects(aContext, aPrsList);
374 foreach(ObjectPtr aObj, theObjList) {
375 if (myResult2AISObjectMap.contains(aObj))
376 aPrsList.Append(myResult2AISObjectMap[aObj]->impl<Handle(AIS_InteractiveObject)>());
380 AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
381 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
382 anAISIO = aLIt.Value();
383 activate(anAISIO, myActiveSelectionModes);
385 // restore the sorting flag and perform the sort of selection
386 //if (!aSelector.IsNull()) {
387 // aSelector->SetUpdateSortPossible(isUpdateSortPossible);
388 // aSelector->UpdateSort();
393 void XGUI_Displayer::deactivateObjects()
395 myActiveSelectionModes.clear();
396 Handle(AIS_InteractiveContext) aContext = AISContext();
397 // Open local context if there is no one
398 if (!aContext->HasOpenedContext())
401 //aContext->NotUseDisplayedObjects();
402 AIS_ListOfInteractive aPrsList;
403 ::displayedObjects(aContext, aPrsList);
405 AIS_ListIteratorOfListOfInteractive aLIt;
406 //Handle(AIS_Trihedron) aTrihedron;
407 Handle(AIS_InteractiveObject) anAISIO;
408 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
409 anAISIO = aLIt.Value();
410 aContext->Deactivate(anAISIO);
411 #ifdef DEBUG_USE_CLEAR_OUTDATED_SELECTION
412 aContext->LocalContext()->ClearOutdatedSelection(anAISIO, true);
418 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
420 Handle(AIS_InteractiveContext) aContext = AISContext();
421 if (aContext.IsNull())
423 if (!isVisible(theObject))
426 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
427 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
429 TColStd_ListOfInteger aModes;
430 aContext->ActivatedModes(anAIS, aModes);
431 return aModes.Extent() > 0;
434 void XGUI_Displayer::setSelected(const QList<ModuleBase_ViewerPrs>& theValues, bool isUpdateViewer)
436 Handle(AIS_InteractiveContext) aContext = AISContext();
437 if (aContext.IsNull())
439 if (aContext->HasOpenedContext()) {
440 aContext->UnhilightSelected();
441 aContext->ClearSelected();
442 //if (aSelected.size() > 0) {
443 foreach (ModuleBase_ViewerPrs aPrs, theValues) {
444 const TopoDS_Shape& aShape = aPrs.shape();
445 if (!aShape.IsNull()) {
446 aContext->AddOrRemoveSelected(aShape, false);
448 ObjectPtr anObject = aPrs.object();
449 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
450 if (aResult.get() && isVisible(aResult)) {
451 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
452 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
453 if (!anAIS.IsNull()) {
454 // The methods are replaced in order to provide multi-selection, e.g. restore selection
455 // by activating multi selector widget. It also gives an advantage that the multi
456 // selection in OB gives multi-selection in the viewer
457 //aContext->SetSelected(anAIS, false);
458 // The selection in the context was cleared, so the method sets the objects are selected
459 aContext->AddOrRemoveSelected(anAIS, false);
465 aContext->UnhilightCurrents();
466 aContext->ClearCurrents();
467 foreach (ModuleBase_ViewerPrs aPrs, theValues) {
468 ObjectPtr anObject = aPrs.object();
469 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
470 if (aResult.get() && isVisible(aResult)) {
471 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
472 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
474 aContext->SetCurrentObject(anAIS, false);
482 void XGUI_Displayer::clearSelected()
484 Handle(AIS_InteractiveContext) aContext = AISContext();
486 aContext->UnhilightCurrents(false);
487 aContext->ClearSelected();
491 void XGUI_Displayer::eraseAll(const bool isUpdateViewer)
493 Handle(AIS_InteractiveContext) aContext = AISContext();
494 if (!aContext.IsNull()) {
495 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
496 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
498 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
499 if (!anIO.IsNull()) {
500 emit beforeObjectErase(aObj, aAISObj);
501 aContext->Remove(anIO, false);
507 myResult2AISObjectMap.clear();
510 void XGUI_Displayer::openLocalContext()
512 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
513 if (aContext.IsNull())
515 // Open local context if there is no one
516 if (!aContext->HasOpenedContext()) {
517 // Preserve selected objects
518 //AIS_ListOfInteractive aAisList;
519 //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
520 // aAisList.Append(aContext->Current());
522 // get the filters from the global context and append them to the local context
523 // a list of filters in the global context is not cleared and should be cleared here
524 SelectMgr_ListOfFilter aFilters;
525 aFilters.Assign(aContext->Filters());
526 // it is important to remove the filters in the global context, because there is a code
527 // in the closeLocalContex, which restore the global context filters
528 aContext->RemoveFilters();
530 //aContext->ClearCurrents();
531 aContext->OpenLocalContext();
532 //aContext->NotUseDisplayedObjects();
534 //myUseExternalObjects = false;
536 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
537 for (;aIt.More(); aIt.Next()) {
538 aContext->AddFilter(aIt.Value());
541 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
542 //for(; aIt2.More(); aIt2.Next()) {
543 // aContext->SetSelected(aIt2.Value(), false);
548 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
550 Handle(AIS_InteractiveContext) aContext = AISContext();
551 if ( (!aContext.IsNull()) && (aContext->HasOpenedContext()) ) {
552 // Preserve selected objects
553 //AIS_ListOfInteractive aAisList;
554 //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
555 // aAisList.Append(aContext->SelectedInteractive());
557 // get the filters from the local context and append them to the global context
558 // a list of filters in the local context is cleared
559 SelectMgr_ListOfFilter aFilters;
560 aFilters.Assign(aContext->Filters());
562 //aContext->ClearSelected();
563 aContext->CloseAllContexts(false);
565 // Redisplay all object if they were displayed in localContext
566 Handle(AIS_InteractiveObject) aAISIO;
567 foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
568 aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
569 if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
570 aContext->Display(aAISIO, false);
571 aContext->SetDisplayMode(aAISIO, Shading, false);
575 // Append the filters from the local selection in the global selection context
576 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
577 for (;aIt.More(); aIt.Next()) {
578 Handle(SelectMgr_Filter) aFilter = aIt.Value();
579 aContext->AddFilter(aFilter);
584 //myUseExternalObjects = false;
587 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
588 //for(; aIt2.More(); aIt2.Next()) {
589 // if (aContext->IsDisplayed(aIt2.Value()))
590 // aContext->SetCurrentObject(aIt2.Value(), false);
595 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
598 if (myResult2AISObjectMap.contains(theObject))
599 anIO = myResult2AISObjectMap[theObject];
603 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
605 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
606 return getObject(aRefAIS);
609 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
612 foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
613 AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
614 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
621 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
623 bool aWasEnabled = myEnableUpdateViewer;
625 myEnableUpdateViewer = isEnabled;
630 void XGUI_Displayer::updateViewer() const
632 Handle(AIS_InteractiveContext) aContext = AISContext();
633 if (!aContext.IsNull() && myEnableUpdateViewer)
634 aContext->UpdateCurrentViewer();
637 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
639 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
640 if ((!aContext.IsNull()) && (!aContext->HasOpenedContext())) {
641 aContext->OpenLocalContext();
646 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
648 Handle(AIS_InteractiveContext) aContext = AISContext();
649 if (myAndFilter.IsNull() && !aContext.IsNull()) {
650 myAndFilter = new SelectMgr_AndFilter();
651 aContext->AddFilter(myAndFilter);
656 void XGUI_Displayer::displayAIS(AISObjectPtr theAIS, bool isUpdate)
658 Handle(AIS_InteractiveContext) aContext = AISContext();
659 if (aContext.IsNull())
661 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
662 if (!anAISIO.IsNull()) {
663 aContext->Display(anAISIO, isUpdate);
664 if (aContext->HasOpenedContext()) {
665 //if (myUseExternalObjects) {
666 if (myActiveSelectionModes.size() == 0)
667 aContext->Activate(anAISIO);
669 foreach(int aMode, myActiveSelectionModes) {
670 aContext->Activate(anAISIO, aMode);
678 void XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool isUpdate)
680 Handle(AIS_InteractiveContext) aContext = AISContext();
681 if (aContext.IsNull())
683 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
684 if (!anAISIO.IsNull()) {
685 aContext->Remove(anAISIO, isUpdate);
690 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool toUpdate)
692 if (theMode == NoMode)
695 Handle(AIS_InteractiveContext) aContext = AISContext();
696 if (aContext.IsNull())
699 AISObjectPtr aAISObj = getAISObject(theObject);
703 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
704 bool aCanBeShaded = ::canBeShaded(aAISIO);
705 // In order to avoid extra closing/opening context
706 SelectMgr_IndexedMapOfOwner aSelectedOwners;
708 myWorkshop->selector()->selection()->selectedOwners(aSelectedOwners);
709 closeLocalContexts(false);
711 aContext->SetDisplayMode(aAISIO, theMode, false);
712 // Redisplay in order to update new mode because it could be not computed before
713 aContext->Redisplay(aAISIO, false);
716 activateObjects(myActiveSelectionModes);
717 myWorkshop->selector()->setSelectedOwners(aSelectedOwners, false);
723 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
725 Handle(AIS_InteractiveContext) aContext = AISContext();
726 if (aContext.IsNull())
729 AISObjectPtr aAISObj = getAISObject(theObject);
733 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
734 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
737 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
739 Handle(AIS_InteractiveContext) aContext = AISContext();
740 if (aContext.IsNull())
742 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
743 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
744 for (; aIt.More(); aIt.Next()) {
745 if (theFilter.Access() == aIt.Value().Access())
748 Handle(SelectMgr_CompositionFilter) aCompFilter = GetFilter();
749 const SelectMgr_ListOfFilter& aStoredFilters = aCompFilter->StoredFilters();
750 for (aIt.Initialize(aStoredFilters); aIt.More(); aIt.Next()) {
751 if (theFilter.Access() == aIt.Value().Access())
754 aCompFilter->Add(theFilter);
755 #ifdef DEBUG_SELECTION_FILTERS
756 int aCount = GetFilter()->StoredFilters().Extent();
757 qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
761 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
763 Handle(AIS_InteractiveContext) aContext = AISContext();
764 if (aContext.IsNull())
766 Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
767 if (aCompositeFilter->IsIn(theFilter))
768 aCompositeFilter->Remove(theFilter);
769 #ifdef DEBUG_SELECTION_FILTERS
770 int aCount = GetFilter()->StoredFilters().Extent();
771 qDebug(QString("removeSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
775 void XGUI_Displayer::removeFilters()
777 Handle(AIS_InteractiveContext) aContext = AISContext();
778 if (aContext.IsNull())
780 GetFilter()->Clear();
783 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
785 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
786 foreach(ObjectPtr aObj, aDispList) {
787 if (!theList.contains(aObj))
790 foreach(ObjectPtr aObj, theList) {
791 if (!isVisible(aObj))
792 display(aObj, false);
797 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
799 if (!isVisible(theObject))
802 AISObjectPtr aAISObj = getAISObject(theObject);
803 if (aAISObj.get() == NULL)
806 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
807 return ::canBeShaded(anAIS);
810 void XGUI_Displayer::activate(const Handle(AIS_InteractiveObject)& theIO,
811 const QIntList& theModes) const
813 Handle(AIS_InteractiveContext) aContext = AISContext();
814 if (aContext.IsNull() || theIO.IsNull())
817 // deactivate object in all modes, which are not in the list of activation
818 // It seems that after the IO deactivation the selected state of the IO's owners
819 // is modified in OCC(version: 6.8.0) and the selection of the object later is lost.
820 // By this reason, the number of the IO deactivate is decreased and the object is deactivated
821 // only if there is a difference in the current modes and the parameters modes.
822 // If the selection problem happens again, it is possible to write a test scenario and create
823 // a bug. The bug steps are the following:
824 // Create two IO, activate them in 5 modes, select the first IO, deactivate 3 modes for both,
825 // with clicked SHIFT select the second object. The result is the selection of the first IO is lost.
826 TColStd_ListOfInteger aTColModes;
827 aContext->ActivatedModes(theIO, aTColModes);
828 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
829 QIntList aModesActivatedForIO;
830 //bool isDeactivated = false;
831 for (; itr.More(); itr.Next() ) {
832 Standard_Integer aMode = itr.Value();
833 if (!theModes.contains(aMode)) {
834 #ifdef DEBUG_ACTIVATE
835 qDebug(QString("deactivate: %1").arg(aMode).toStdString().c_str());
837 aContext->Deactivate(theIO, aMode);
838 //isDeactivated = true;
841 aModesActivatedForIO.append(aMode);
842 #ifdef DEBUG_ACTIVATE
843 qDebug(QString(" active: %1").arg(aMode).toStdString().c_str());
847 #ifdef DEBUG_USE_CLEAR_OUTDATED_SELECTION
849 aContext->LocalContext()->ClearOutdatedSelection(theIO, true);
853 // loading the interactive object allowing the decomposition
854 if (aTColModes.IsEmpty())
855 aContext->Load(theIO, -1, true);
857 // trihedron AIS check should be after the AIS loading.
858 // If it is not loaded, it is steel selectable in the viewer.
859 Handle(AIS_Trihedron) aTrihedron = Handle(AIS_Trihedron)::DownCast(theIO);
860 if (aTrihedron.IsNull()) {
861 //aContext->Load(anAISIO, -1, true);
862 // In order to clear active modes list
863 if (theModes.size() == 0) {
864 //aContext->Load(anAISIO, 0, true);
865 aContext->Activate(theIO);
866 #ifdef DEBUG_ACTIVATE
867 qDebug("activate in all modes");
870 foreach(int aMode, theModes) {
871 //aContext->Load(anAISIO, aMode, true);
872 if (!aModesActivatedForIO.contains(aMode)) {
873 aContext->Activate(theIO, aMode);
874 #ifdef DEBUG_ACTIVATE
875 qDebug(QString("activate: %1").arg(aMode).toStdString().c_str());
883 bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
885 AISObjectPtr anAISObj = getAISObject(theObject);
886 // correct the result's color it it has the attribute
887 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
889 // Customization of presentation
890 GeomCustomPrsPtr aCustomPrs;
891 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
892 if (aFeature.get() != NULL) {
893 GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
894 if (aCustPrs.get() != NULL)
895 aCustomPrs = aCustPrs;
897 if (aCustomPrs.get() == NULL) {
898 // we ignore presentable not customized objects
899 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
900 if (aPrs.get() != NULL)
902 aCustomPrs = myCustomPrs;
904 return aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
908 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject, const QColor& theColor, bool toUpdate)
910 if (!isVisible(theObject))
913 AISObjectPtr anAISObj = getAISObject(theObject);
915 anAISObj->getColor(aR, aG, aB);
916 anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
919 return QColor(aR, aG, aB);
922 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
924 myResult2AISObjectMap[theObject] = theAIS;
927 std::ostringstream aPtrStr;
928 aPtrStr << theObject.get();
929 qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
930 qDebug(getResult2AISObjectMapInfo().c_str());
934 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
936 QStringList aContent;
937 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
938 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
939 std::ostringstream aPtrStr;
940 aPtrStr << "aObj = " << aObj.get() << ":";
941 aPtrStr << "anAIS = " << aAISObj.get() << ":";
942 aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
944 aContent.append(aPtrStr.str().c_str());
946 return QString("myResult2AISObjectMap: size = %1\n%2").arg(myResult2AISObjectMap.size()).
947 arg(aContent.join("\n")).toStdString().c_str();