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"
11 #include <AppElements_Viewer.h>
13 #include <ModelAPI_Document.h>
14 #include <ModelAPI_Data.h>
15 #include <ModelAPI_Object.h>
16 #include <ModelAPI_Tools.h>
18 #include <ModuleBase_ResultPrs.h>
20 #include <GeomAPI_Shape.h>
21 #include <GeomAPI_IPresentable.h>
22 #include <GeomAPI_ICustomPrs.h>
24 #include <AIS_InteractiveContext.hxx>
25 #include <AIS_LocalContext.hxx>
26 #include <AIS_ListOfInteractive.hxx>
27 #include <AIS_ListIteratorOfListOfInteractive.hxx>
28 #include <AIS_DimensionSelectionMode.hxx>
29 #include <AIS_Shape.hxx>
30 #include <AIS_Dimension.hxx>
31 #include <TColStd_ListIteratorOfListOfInteger.hxx>
32 #include <SelectMgr_ListOfFilter.hxx>
33 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
35 #include <TColStd_MapOfTransient.hxx>
36 #include <TColStd_MapIteratorOfMapOfTransient.hxx>
40 const int MOUSE_SENSITIVITY_IN_PIXEL = 10; ///< defines the local context mouse selection sensitivity
43 // Workaround for bug #25637
44 void displayedObjects(const Handle(AIS_InteractiveContext)& theAIS, AIS_ListOfInteractive& theList)
46 // Get from null point
47 theAIS->DisplayedObjects(theList, true);
48 if (theAIS->HasOpenedContext()) {
49 // get from local context
50 const Handle(AIS_LocalContext)& aLC = theAIS->LocalContext();
51 TColStd_MapOfTransient aMap;
52 int NbDisp = aLC->DisplayedObjects(aMap);
53 TColStd_MapIteratorOfMapOfTransient aIt(aMap);
55 Handle(AIS_InteractiveObject) curIO;
56 Handle(Standard_Transient) Tr;
57 for(; aIt.More(); aIt.Next()){
59 curIO = *((Handle(AIS_InteractiveObject)*) &Tr);
60 theList.Append(curIO);
66 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
67 : myWorkshop(theWorkshop)
71 XGUI_Displayer::~XGUI_Displayer()
75 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
77 return myResult2AISObjectMap.contains(theObject);
80 void XGUI_Displayer::display(ObjectPtr theObject, bool isUpdateViewer)
82 if (isVisible(theObject)) {
83 redisplay(theObject, isUpdateViewer);
87 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
88 bool isShading = false;
89 if (aPrs.get() != NULL) {
90 anAIS = aPrs->getAISObject(AISObjectPtr());
92 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
93 if (aResult.get() != NULL) {
94 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
95 if (aShapePtr.get() != NULL) {
96 anAIS = AISObjectPtr(new GeomAPI_AISObject());
97 anAIS->setImpl(new Handle(AIS_InteractiveObject)(new ModuleBase_ResultPrs(aResult)));
98 //anAIS->createShape(aShapePtr);
104 display(theObject, anAIS, isShading, isUpdateViewer);
108 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS)
110 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
111 if (!aShapePrs.IsNull()) {
112 TopoDS_Shape aShape = aShapePrs->Shape();
113 TopAbs_ShapeEnum aType = aShape.ShapeType();
114 if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
122 void XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
123 bool isShading, bool isUpdateViewer)
125 Handle(AIS_InteractiveContext) aContext = AISContext();
126 if (aContext.IsNull())
129 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
130 if (!anAISIO.IsNull()) {
131 myResult2AISObjectMap[theObject] = theAIS;
132 bool aCanBeShaded = canBeShaded(anAISIO);
133 // In order to avoid extra closing/opening context
135 closeLocalContexts(false);
136 aContext->Display(anAISIO, false);
137 qDebug("### Display %i", (long)anAISIO.Access());
139 aContext->SetDisplayMode(anAISIO, isShading? Shading : Wireframe, false);
140 // Customization of presentation
141 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
142 if (aFeature.get() != NULL) {
143 GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
144 if (aCustPrs.get() != NULL)
145 aCustPrs->customisePresentation(theAIS);
149 activateObjects(myActiveSelectionModes);
156 void XGUI_Displayer::erase(ObjectPtr theObject, const bool isUpdateViewer)
158 if (!isVisible(theObject))
161 Handle(AIS_InteractiveContext) aContext = AISContext();
162 if (aContext.IsNull())
164 AISObjectPtr anObject = myResult2AISObjectMap[theObject];
166 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
167 if (!anAIS.IsNull()) {
168 aContext->Remove(anAIS, isUpdateViewer);
171 myResult2AISObjectMap.remove(theObject);
174 void XGUI_Displayer::redisplay(ObjectPtr theObject, bool isUpdateViewer)
176 if (!isVisible(theObject))
179 AISObjectPtr aAISObj = getAISObject(theObject);
180 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
182 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
184 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
186 erase(theObject, isUpdateViewer);
189 if (aAIS_Obj != aAISObj) {
190 myResult2AISObjectMap[theObject] = aAIS_Obj;
192 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
195 if (!aAISIO.IsNull()) {
196 Handle(AIS_InteractiveContext) aContext = AISContext();
197 if (aContext.IsNull())
199 aContext->Redisplay(aAISIO, isUpdateViewer);
203 void XGUI_Displayer::deactivate(ObjectPtr theObject)
205 if (isVisible(theObject)) {
206 Handle(AIS_InteractiveContext) aContext = AISContext();
207 if (aContext.IsNull())
210 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
211 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
212 aContext->Deactivate(anAIS);
213 qDebug("### Deactivate obj %i", (long)anAIS.Access());
217 void XGUI_Displayer::activate(ObjectPtr theFeature)
219 activate(theFeature, myActiveSelectionModes);
222 void XGUI_Displayer::activate(ObjectPtr theObject, const QIntList& theModes)
224 if (isVisible(theObject)) {
225 Handle(AIS_InteractiveContext) aContext = AISContext();
226 if (aContext.IsNull())
229 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
230 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
231 aContext->Deactivate(anAIS);
232 aContext->Load(anAIS, -1, true);
233 // In order to clear active modes list
234 if (theModes.size() > 0) {
235 foreach(int aMode, theModes) {
236 //aContext->Load(anAIS, aMode, true);
237 aContext->Activate(anAIS, aMode);
238 qDebug("### 1. Activate obj %i, %i", (long)anAIS.Access(), aMode);
241 //aContext->Load(anAIS, 0, true);
242 aContext->Activate(anAIS);
243 qDebug("### 2. Activate obj %i", (long)anAIS.Access());
248 void XGUI_Displayer::activateObjects(const QIntList& theModes)
250 // In order to avoid doblications of selection modes
252 foreach (int aMode, theModes) {
253 if (!aNewModes.contains(aMode))
254 aNewModes.append(aMode);
256 myActiveSelectionModes = aNewModes;
257 Handle(AIS_InteractiveContext) aContext = AISContext();
258 // Open local context if there is no one
259 if (!aContext->HasOpenedContext())
262 //aContext->UseDisplayedObjects();
263 //myUseExternalObjects = true;
265 AIS_ListOfInteractive aPrsList;
266 displayedObjects(aContext, aPrsList);
268 Handle(AIS_Trihedron) aTrihedron;
269 AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
270 Handle(AIS_InteractiveObject) anAISIO;
271 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
272 anAISIO = aLIt.Value();
273 aContext->Load(anAISIO, -1, true);
274 aContext->Deactivate(anAISIO);
275 aTrihedron = Handle(AIS_Trihedron)::DownCast(anAISIO);
276 //Deactivate trihedron which can be activated in local selector
277 if (aTrihedron.IsNull()) {
278 //aContext->Load(anAISIO, -1, true);
279 // In order to clear active modes list
280 if (myActiveSelectionModes.size() == 0) {
281 //aContext->Load(anAISIO, 0, true);
282 aContext->Activate(anAISIO);
283 qDebug("### 2. Activate all %i", (long)anAISIO.Access());
285 foreach(int aMode, myActiveSelectionModes) {
286 //aContext->Load(anAISIO, aMode, true);
287 aContext->Activate(anAISIO, aMode);
288 qDebug("### 1. Activate all %i, %i", (long)anAISIO.Access(), aMode);
296 void XGUI_Displayer::deactivateObjects()
298 myActiveSelectionModes.clear();
299 Handle(AIS_InteractiveContext) aContext = AISContext();
300 // Open local context if there is no one
301 if (!aContext->HasOpenedContext())
304 //aContext->NotUseDisplayedObjects();
305 AIS_ListOfInteractive aPrsList;
306 displayedObjects(aContext, aPrsList);
308 AIS_ListIteratorOfListOfInteractive aLIt;
309 //Handle(AIS_Trihedron) aTrihedron;
310 Handle(AIS_InteractiveObject) anAISIO;
311 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
312 anAISIO = aLIt.Value();
313 aContext->Deactivate(anAISIO);
314 //aTrihedron = Handle(AIS_Trihedron)::DownCast(anAISIO);
315 //if (aTrihedron.IsNull()) {
316 // qDebug("### Deactivate all %i", (long)anAISIO.Access());
317 // //aContext->Activate(anAISIO);
322 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
324 Handle(AIS_InteractiveContext) aContext = AISContext();
325 if (aContext.IsNull())
327 if (!isVisible(theObject))
330 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
331 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
333 TColStd_ListOfInteger aModes;
334 aContext->ActivatedModes(anAIS, aModes);
335 return aModes.Extent() > 0;
338 void XGUI_Displayer::setSelected(const QObjectPtrList& theResults, const bool isUpdateViewer)
340 Handle(AIS_InteractiveContext) aContext = AISContext();
341 if (aContext.IsNull())
343 if (aContext->HasOpenedContext()) {
344 aContext->UnhilightSelected();
345 aContext->ClearSelected();
346 foreach(ObjectPtr aResult, theResults) {
347 if (isVisible(aResult)) {
348 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
349 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
351 aContext->SetSelected(anAIS, false);
355 aContext->UnhilightCurrents();
356 aContext->ClearCurrents();
357 foreach(ObjectPtr aResult, theResults) {
358 if (isVisible(aResult)) {
359 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
360 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
362 aContext->SetCurrentObject(anAIS, false);
371 void XGUI_Displayer::clearSelected()
373 Handle(AIS_InteractiveContext) aContext = AISContext();
375 aContext->UnhilightCurrents(false);
376 aContext->ClearSelected();
380 void XGUI_Displayer::eraseAll(const bool isUpdateViewer)
382 Handle(AIS_InteractiveContext) aContext = AISContext();
383 if (aContext.IsNull())
386 foreach (AISObjectPtr aAISObj, myResult2AISObjectMap) {
388 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
390 aContext->Remove(anIO, false);
392 myResult2AISObjectMap.clear();
397 void XGUI_Displayer::openLocalContext()
399 Handle(AIS_InteractiveContext) aContext = AISContext();
400 if (aContext.IsNull())
402 // Open local context if there is no one
403 if (!aContext->HasOpenedContext()) {
404 // Preserve selected objects
405 //AIS_ListOfInteractive aAisList;
406 //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
407 // aAisList.Append(aContext->Current());
409 // get the filters from the global context and append them to the local context
410 // a list of filters in the global context is not cleared and should be cleared here
411 SelectMgr_ListOfFilter aFilters;
412 aFilters.Assign(aContext->Filters());
413 // it is important to remove the filters in the global context, because there is a code
414 // in the closeLocalContex, which restore the global context filters
415 aContext->RemoveFilters();
417 //aContext->ClearCurrents();
418 aContext->OpenLocalContext();
419 qDebug("### Open context");
420 //aContext->NotUseDisplayedObjects();
422 //myUseExternalObjects = false;
424 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
425 for (;aIt.More(); aIt.Next()) {
426 aContext->AddFilter(aIt.Value());
429 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
430 //for(; aIt2.More(); aIt2.Next()) {
431 // aContext->SetSelected(aIt2.Value(), false);
436 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
438 Handle(AIS_InteractiveContext) aContext = AISContext();
439 if ( (!aContext.IsNull()) && (aContext->HasOpenedContext()) ) {
440 // Preserve selected objects
441 //AIS_ListOfInteractive aAisList;
442 //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
443 // aAisList.Append(aContext->SelectedInteractive());
445 // get the filters from the local context and append them to the global context
446 // a list of filters in the local context is cleared
447 SelectMgr_ListOfFilter aFilters;
448 aFilters.Assign(aContext->Filters());
450 //aContext->ClearSelected();
451 aContext->CloseAllContexts(false);
452 qDebug("### Close context");
454 // Redisplay all object if they were displayed in localContext
455 Handle(AIS_InteractiveObject) aAISIO;
456 foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
457 aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
458 if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
459 aContext->Display(aAISIO, false);
460 aContext->SetDisplayMode(aAISIO, Shading, false);
464 // Append the filters from the local selection in the global selection context
465 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
466 for (;aIt.More(); aIt.Next()) {
467 Handle(SelectMgr_Filter) aFilter = aIt.Value();
468 aContext->AddFilter(aFilter);
473 //myUseExternalObjects = false;
476 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
477 //for(; aIt2.More(); aIt2.Next()) {
478 // if (aContext->IsDisplayed(aIt2.Value()))
479 // aContext->SetCurrentObject(aIt2.Value(), false);
484 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
487 if (myResult2AISObjectMap.contains(theObject))
488 anIO = myResult2AISObjectMap[theObject];
492 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
494 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
495 return getObject(aRefAIS);
498 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
501 foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
502 AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
503 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
510 void XGUI_Displayer::updateViewer()
512 Handle(AIS_InteractiveContext) aContext = AISContext();
513 if (!aContext.IsNull())
514 aContext->UpdateCurrentViewer();
517 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
519 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
520 if ((!aContext.IsNull()) && (!aContext->HasOpenedContext())) {
521 aContext->OpenLocalContext();
522 qDebug("### Open context");
527 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
529 Handle(AIS_InteractiveContext) aContext = AISContext();
530 if (myAndFilter.IsNull() && !aContext.IsNull()) {
531 myAndFilter = new SelectMgr_AndFilter();
532 aContext->AddFilter(myAndFilter);
537 void XGUI_Displayer::displayAIS(AISObjectPtr theAIS, bool isUpdate)
539 Handle(AIS_InteractiveContext) aContext = AISContext();
540 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
541 if (!anAISIO.IsNull()) {
542 aContext->Display(anAISIO, isUpdate);
543 if (aContext->HasOpenedContext()) {
544 //if (myUseExternalObjects) {
545 if (myActiveSelectionModes.size() == 0)
546 aContext->Activate(anAISIO);
548 foreach(int aMode, myActiveSelectionModes) {
549 aContext->Activate(anAISIO, aMode);
557 void XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool isUpdate)
559 Handle(AIS_InteractiveContext) aContext = AISContext();
560 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
561 if (!anAISIO.IsNull()) {
562 aContext->Remove(anAISIO, isUpdate);
567 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool toUpdate)
569 if (theMode == NoMode)
572 Handle(AIS_InteractiveContext) aContext = AISContext();
573 if (aContext.IsNull())
576 AISObjectPtr aAISObj = getAISObject(theObject);
580 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
581 aContext->SetDisplayMode(aAISIO, theMode, toUpdate);
584 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
586 Handle(AIS_InteractiveContext) aContext = AISContext();
587 if (aContext.IsNull())
590 AISObjectPtr aAISObj = getAISObject(theObject);
594 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
595 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
598 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
600 Handle(AIS_InteractiveContext) aContext = AISContext();
601 if (aContext.IsNull())
603 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
604 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
605 for (; aIt.More(); aIt.Next()) {
606 if (theFilter.Access() == aIt.Value().Access())
609 GetFilter()->Add(theFilter);
612 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
614 Handle(AIS_InteractiveContext) aContext = AISContext();
615 if (aContext.IsNull())
617 GetFilter()->Remove(theFilter);
620 void XGUI_Displayer::removeFilters()
622 Handle(AIS_InteractiveContext) aContext = AISContext();
623 if (aContext.IsNull())
625 GetFilter()->Clear();
628 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
630 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
631 foreach(ObjectPtr aObj, aDispList) {
632 if (!theList.contains(aObj))
635 foreach(ObjectPtr aObj, theList) {
636 if (!isVisible(aObj))
637 display(aObj, false);