1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "XGUI_Displayer.h"
23 #include "XGUI_CustomPrs.h"
24 #include "XGUI_FacesPanel.h"
25 #include "XGUI_Selection.h"
26 #include "XGUI_SelectionActivate.h"
27 #include "XGUI_SelectionMgr.h"
28 #include "XGUI_ViewerProxy.h"
29 #include "XGUI_Workshop.h"
32 #include <AppElements_Viewer.h>
35 #include <ModelAPI_Document.h>
36 #include <ModelAPI_Data.h>
37 #include <ModelAPI_Object.h>
38 #include <ModelAPI_Tools.h>
39 #include <ModelAPI_AttributeIntArray.h>
40 #include <ModelAPI_ResultCompSolid.h>
42 #include <ModuleBase_BRepOwner.h>
43 #include <ModuleBase_IModule.h>
44 #include <ModuleBase_Preferences.h>
45 #include <ModuleBase_ResultPrs.h>
46 #include <ModuleBase_Tools.h>
47 #include <ModuleBase_ViewerPrs.h>
49 #include <GeomAPI_Shape.h>
50 #include <GeomAPI_IPresentable.h>
51 #include <GeomAPI_ICustomPrs.h>
53 #include <SUIT_ResourceMgr.h>
55 #include <AIS_InteractiveContext.hxx>
56 #include <AIS_ListOfInteractive.hxx>
57 #include <AIS_ListIteratorOfListOfInteractive.hxx>
58 #include <AIS_DimensionSelectionMode.hxx>
59 #include <AIS_Shape.hxx>
60 #include <AIS_Dimension.hxx>
61 #include <AIS_Trihedron.hxx>
62 #ifdef BEFORE_TRIHEDRON_PATCH
63 #include <AIS_Axis.hxx>
64 #include <AIS_Plane.hxx>
65 #include <AIS_Point.hxx>
67 #include <AIS_Selection.hxx>
68 #include <Prs3d_Drawer.hxx>
69 #include <Prs3d_IsoAspect.hxx>
70 #include <SelectMgr_ListOfFilter.hxx>
71 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
72 #include <SelectMgr_SelectionManager.hxx>
73 #include <TColStd_ListIteratorOfListOfInteger.hxx>
75 #include <StdSelect_ViewerSelector3d.hxx>
77 #include <TColStd_MapOfTransient.hxx>
78 #include <TColStd_MapIteratorOfMapOfTransient.hxx>
81 #include <inspector/VInspectorAPI_CallBack.hxx>
84 #include <Events_Loop.h>
85 #include <ModelAPI_Events.h>
89 /// defines the local context mouse selection sensitivity
90 const int MOUSE_SENSITIVITY_IN_PIXEL = 10;
92 //#define DEBUG_DISPLAY
93 //#define DEBUG_FEATURE_REDISPLAY
94 //#define DEBUG_SELECTION_FILTERS
96 //#define DEBUG_COMPOSILID_DISPLAY
98 //#define DEBUG_OCCT_SHAPE_SELECTION
100 #define CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
102 //**************************************************************
103 void displayedObjects(const Handle(AIS_InteractiveContext)& theAIS, AIS_ListOfInteractive& theList)
105 // Get from null point
106 theAIS->DisplayedObjects(theList, true);
109 QString qIntListInfo(const QIntList& theValues, const QString& theSeparator = QString(", "))
112 QIntList::const_iterator anIt = theValues.begin(), aLast = theValues.end();
113 for (; anIt != aLast; anIt++) {
114 anInfo.append(QString::number(*anIt));
116 return anInfo.join(theSeparator);
119 //**************************************************************
120 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
121 : myWorkshop(theWorkshop), myNeedUpdate(false),
122 myViewerBlockedRecursiveCount(0), myIsFirstAISContextUse(true)
124 myCustomPrs = std::shared_ptr<GeomAPI_ICustomPrs>(new XGUI_CustomPrs(theWorkshop));
127 //**************************************************************
128 XGUI_Displayer::~XGUI_Displayer()
132 //**************************************************************
133 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
135 return myResult2AISObjectMap.contains(theObject);
138 //**************************************************************
139 bool XGUI_Displayer::display(ObjectPtr theObject, bool theUpdateViewer)
141 bool aDisplayed = false;
142 if (isVisible(theObject)) {
143 #ifdef DEBUG_COMPOSILID_DISPLAY
144 ResultCompSolidPtr aCompsolidResult =
145 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
146 if (aCompsolidResult.get()) {
147 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
148 ResultPtr aSubResult = aCompsolidResult->subResult(i);
149 if (aSubResult.get())
150 redisplay(aSubResult, false);
157 aDisplayed = redisplay(theObject, theUpdateViewer);
160 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
161 bool isShading = false;
162 if (aPrs.get() != NULL) {
163 anAIS = aPrs->getAISObject(anAIS);
165 // correct deviation coefficient for
166 /*Handle(AIS_InteractiveObject) anAISPrs = anAIS->impl<Handle(AIS_InteractiveObject)>();
167 if (!anAISPrs.IsNull()) {
168 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(anAISPrs);
169 if (!aShapePrs.IsNull()) {
170 TopoDS_Shape aShape = aShapePrs->Shape();
171 if (!aShape.IsNull())
172 //ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, anAISPrs->Attributes());
177 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
178 if (aResult.get() != NULL) {
179 #ifdef DEBUG_COMPOSILID_DISPLAY
180 ResultCompSolidPtr aCompsolidResult =
181 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
182 if (aCompsolidResult.get()) {
183 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
184 ResultPtr aSubResult = aCompsolidResult->subResult(i);
185 if (aSubResult.get())
186 display(aSubResult, false);
193 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
194 if (aShapePtr.get() != NULL) {
195 anAIS = AISObjectPtr(new GeomAPI_AISObject());
196 Handle(AIS_InteractiveObject) anAISPrs =
197 myWorkshop->module()->createPresentation(aResult);
198 if (anAISPrs.IsNull())
199 anAISPrs = new ModuleBase_ResultPrs(aResult);
201 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(anAISPrs);
202 if (!aShapePrs.IsNull())
203 ModuleBase_Tools::setPointBallHighlighting((AIS_Shape*) aShapePrs.get());
205 anAIS->setImpl(new Handle(AIS_InteractiveObject)(anAISPrs));
206 //anAIS->createShape(aShapePtr);
209 #ifdef DEBUG_COMPOSILID_DISPLAY
215 aDisplayed = display(theObject, anAIS, isShading, theUpdateViewer);
220 //**************************************************************
221 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS, ModuleBase_IModule* theModule)
223 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
224 if (!aShapePrs.IsNull()) {
225 TopoDS_Shape aShape = aShapePrs->Shape();
228 TopAbs_ShapeEnum aType = aShape.ShapeType();
229 if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
232 // Check that the presentation is not a sketch
233 return theModule->canBeShaded(theAIS);
239 //**************************************************************
240 bool XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
241 bool isShading, bool theUpdateViewer)
243 bool aDisplayed = false;
245 Handle(AIS_InteractiveContext) aContext = AISContext();
246 if (aContext.IsNull())
249 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
250 if (!anAISIO.IsNull()) {
251 appendResultObject(theObject, theAIS);
253 bool isCustomized = customizeObject(theObject);
255 int aDispMode = isShading? Shading : Wireframe;
257 anAISIO->Attributes()->SetFaceBoundaryDraw( Standard_True );
258 anAISIO->SetDisplayMode(aDispMode);
259 aContext->Display(anAISIO, aDispMode, 0, false, true, AIS_DS_Displayed);
261 if (getCallBack()) getCallBack()->Display(anAISIO);
265 emit objectDisplayed(theObject, theAIS);
266 selectionActivate()->activate(anAISIO, theUpdateViewer);
274 //**************************************************************
275 bool XGUI_Displayer::erase(ObjectPtr theObject, const bool theUpdateViewer)
277 bool aErased = false;
278 if (!isVisible(theObject))
281 Handle(AIS_InteractiveContext) aContext = AISContext();
282 if (aContext.IsNull())
285 AISObjectPtr anObject = myResult2AISObjectMap[theObject];
287 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
288 if (!anAIS.IsNull()) {
289 emit beforeObjectErase(theObject, anObject);
290 aContext->Remove(anAIS, false/*update viewer*/);
292 if (getCallBack()) getCallBack()->Remove(anAIS);
297 myResult2AISObjectMap.remove(theObject);
300 std::ostringstream aPtrStr;
301 aPtrStr << theObject.get();
302 qDebug(QString("erase object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
303 qDebug(getResult2AISObjectMapInfo().c_str());
312 //**************************************************************
313 bool XGUI_Displayer::redisplay(ObjectPtr theObject, bool theUpdateViewer)
315 bool aRedisplayed = false;
316 if (!isVisible(theObject))
319 AISObjectPtr aAISObj = getAISObject(theObject);
320 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
322 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
324 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
326 aRedisplayed = erase(theObject, theUpdateViewer);
329 if (aAIS_Obj != aAISObj) {
330 appendResultObject(theObject, aAIS_Obj);
332 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
335 Handle(AIS_InteractiveContext) aContext = AISContext();
336 if (!aContext.IsNull() && !aAISIO.IsNull()) {
337 // Check that the visualized shape is the same and the redisplay is not necessary
338 // Redisplay of AIS object leads to this object selection compute and the selection
339 // in the browser is lost
340 // this check is not necessary anymore because the selection store/restore is realized
341 // before and after the values modification.
342 // Moreother, this check avoids customize and redisplay presentation if the presentable
343 // parameter is changed.
344 bool isEqualShapes = false;
345 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
346 if (aResult.get() != NULL) {
347 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(aAISIO);
348 if (!aShapePrs.IsNull()) {
349 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
350 if (aShapePtr.get()) {
351 const TopoDS_Shape& aOldShape = aShapePrs->Shape();
352 if (!aOldShape.IsNull())
353 isEqualShapes = aOldShape.IsEqual(aShapePtr->impl<TopoDS_Shape>());
357 // Customization of presentation
358 bool isCustomized = customizeObject(theObject);
359 #ifdef DEBUG_FEATURE_REDISPLAY
360 qDebug(QString("Redisplay: %1, isEqualShapes=%2, isCustomized=%3").
361 arg(!isEqualShapes || isCustomized).arg(isEqualShapes)
362 .arg(isCustomized).toStdString().c_str());
364 if (!isEqualShapes || isCustomized) {
365 /// if shapes are equal and presentation are customized, selection should be restored
366 bool aNeedToRestoreSelection = isEqualShapes && isCustomized;
367 if (aNeedToRestoreSelection)
368 myWorkshop->module()->storeSelection();
370 #ifdef CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
371 myWorkshop->selector()->deselectPresentation(aAISIO);
373 aContext->Redisplay(aAISIO, false);
376 if (getCallBack()) getCallBack()->Redisplay(aAISIO);
379 if (aNeedToRestoreSelection)
380 myWorkshop->module()->restoreSelection();
383 #ifdef DEBUG_FEATURE_REDISPLAY
384 qDebug(" Redisplay happens");
393 //**************************************************************
394 void XGUI_Displayer::redisplayObjects()
396 // redisplay objects visualized in the viewer
397 static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
398 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
399 QObjectPtrList aDisplayed = myWorkshop->displayer()->displayedObjects();
400 QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
401 for (; anIt != aLast; anIt++) {
402 aECreator->sendUpdated(*anIt, EVENT_DISP);
404 Events_Loop::loop()->flush(EVENT_DISP);
407 //**************************************************************
408 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
409 const bool theUpdateViewer)
411 //Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
412 //if (!aTrihedron.IsNull())
413 // deactivateAIS(aTrihedron);
415 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
416 for (; anIt != aLast; anIt++) {
417 selectionActivate()->deactivate(*anIt, false);
419 //VSV It seems that there is no necessity to update viewer on deactivation
420 //if (theUpdateViewer)
424 //**************************************************************
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,
433 // but should have presentation when all sub solids are
434 // visible. It is useful for highlight presentation where compsolid shape is selectable
435 if (!aVisible && aResult.get() && aResult->groupName() == ModelAPI_ResultCompSolid::group()) {
436 ResultCompSolidPtr aCompsolidResult =
437 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResult);
438 if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
439 bool anAllSubsVisible = aCompsolidResult->numberOfSubs() > 0;
440 for(int i = 0; i < aCompsolidResult->numberOfSubs() && anAllSubsVisible; i++) {
441 anAllSubsVisible = theDisplayer->isVisible(aCompsolidResult->subResult(i));
443 aVisible = anAllSubsVisible;
447 // it is possible that feature is presentable and has results, so we should check visibility
448 // of results if presentation is not shown (e.g. Sketch Circle/Arc features)
450 // check if all results of the feature are visible
451 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
452 std::list<ResultPtr> aResults;
453 ModelAPI_Tools::allResults(aFeature, aResults);
454 std::list<ResultPtr>::const_iterator aIt;
455 aVisible = !aResults.empty();
456 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
457 aVisible = aVisible && theDisplayer->isVisible(*aIt);
463 //**************************************************************
464 void XGUI_Displayer::setSelected(const QList<ModuleBase_ViewerPrsPtr>& theValues,
465 bool theUpdateViewer)
467 Handle(AIS_InteractiveContext) aContext = AISContext();
468 if (aContext.IsNull())
470 aContext->UnhilightSelected(false);
471 aContext->ClearSelected(false);
473 if (getCallBack()) getCallBack()->ClearSelected();
475 NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)>>
478 foreach (ModuleBase_ViewerPrsPtr aPrs, theValues) {
479 const GeomShapePtr& aGeomShape = aPrs->shape();
480 if (aGeomShape.get() && !aGeomShape->isNull()) {
481 const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
482 #ifdef DEBUG_OCCT_SHAPE_SELECTION
483 // problem 1: performance
484 // problem 2: IO is not specified, so the first found owner is selected, as a result
485 // it might belong to another result
486 aContext->AddOrRemoveSelected(aShape, false);
488 if (getCallBack()) getCallBack()->AddOrRemoveSelected(aShape);
491 NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations;
492 if (aShapesToBeSelected.IsBound(aShape))
493 aPresentations = aShapesToBeSelected.Find(aShape);
494 ObjectPtr anObject = aPrs->object();
495 getPresentations(anObject, aPresentations);
497 aShapesToBeSelected.Bind(aShape, aPresentations);
500 ObjectPtr anObject = aPrs->object();
501 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
502 if (aResult.get() && isVisible(aResult)) {
503 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
504 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
505 if (!anAIS.IsNull()) {
506 // The methods are replaced in order to provide multi-selection, e.g. restore selection
507 // by activating multi selector widget. It also gives an advantage that the multi
508 // selection in OB gives multi-selection in the viewer
509 //aContext->SetSelected(anAIS, false);
510 // The selection in the context was cleared, so the method sets the objects are selected
511 aContext->AddOrRemoveSelected(anAIS, false);
513 if (getCallBack()) getCallBack()->AddOrRemoveSelected(anAIS);
519 if (!aShapesToBeSelected.IsEmpty())
520 XGUI_Displayer::AddOrRemoveSelectedShapes(aContext, aShapesToBeSelected);
526 //**************************************************************
527 void XGUI_Displayer::clearSelected(const bool theUpdateViewer)
529 Handle(AIS_InteractiveContext) aContext = AISContext();
530 if (!aContext.IsNull()) {
531 aContext->UnhilightSelected(false);//UnhilightCurrents(false);
532 aContext->ClearSelected(theUpdateViewer);
534 if (getCallBack()) getCallBack()->ClearSelected();
539 //**************************************************************
540 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
542 bool aErased = false;
543 Handle(AIS_InteractiveContext) aContext = AISContext();
544 if (!aContext.IsNull()) {
545 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
546 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
548 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
549 if (!anIO.IsNull()) {
550 emit beforeObjectErase(aObj, aAISObj);
551 aContext->Remove(anIO, false/*update viewer*/);
553 if (getCallBack()) getCallBack()->Remove(anIO);
561 myResult2AISObjectMap.clear();
564 qDebug(getResult2AISObjectMapInfo().c_str());
570 //**************************************************************
571 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
574 if (myResult2AISObjectMap.contains(theObject))
575 anIO = myResult2AISObjectMap[theObject];
579 //**************************************************************
580 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
582 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
583 return getObject(aRefAIS);
586 //**************************************************************
587 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
590 foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
591 AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
592 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
598 if (!anObject.get()) {
599 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
600 if (!theIO.IsNull()) {
601 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
603 anObject = myWorkshop->module()->findPresentedObject(anAISObj);
608 //**************************************************************
609 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
611 bool aWasEnabled = isUpdateEnabled();
613 myViewerBlockedRecursiveCount--;
615 myViewerBlockedRecursiveCount++;
617 if (myNeedUpdate && isUpdateEnabled()) {
619 myNeedUpdate = false;
624 //**************************************************************
625 bool XGUI_Displayer::isUpdateEnabled() const
627 return myViewerBlockedRecursiveCount == 0;
630 //**************************************************************
631 void XGUI_Displayer::updateViewer() const
633 Handle(AIS_InteractiveContext) aContext = AISContext();
634 if (!aContext.IsNull() && isUpdateEnabled()) {
635 //myWorkshop->viewer()->Zfitall();
636 aContext->UpdateCurrentViewer();
642 //**************************************************************
643 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
645 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
646 if (!aContext.IsNull() && myIsFirstAISContextUse/*&& !aContext->HasOpenedContext()*/) {
647 XGUI_Displayer* aDisplayer = (XGUI_Displayer*)this;
648 aDisplayer->myIsFirstAISContextUse = false;
649 if (!myWorkshop->selectionActivate()->isTrihedronActive())
650 selectionActivate()->deactivateTrihedron(true);
651 aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
652 aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
657 //**************************************************************
658 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
660 Handle(AIS_InteractiveContext) aContext = AISContext();
661 if (!aContext.IsNull() && myAndFilter.IsNull()) {
662 myAndFilter = new SelectMgr_AndFilter();
663 aContext->AddFilter(myAndFilter);
668 //**************************************************************
669 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
670 const Standard_Integer theDisplayMode, bool theUpdateViewer)
672 bool aDisplayed = false;
673 Handle(AIS_InteractiveContext) aContext = AISContext();
674 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
675 if (!aContext.IsNull() && !anAISIO.IsNull()) {
676 aContext->Display(anAISIO, theDisplayMode, 0, false/*update viewer*/, true, AIS_DS_Displayed);
678 if (getCallBack()) getCallBack()->Display(anAISIO);
681 aContext->Deactivate(anAISIO);
683 if (getCallBack()) getCallBack()->Deactivate(anAISIO);
685 aContext->Load(anAISIO);
687 if (getCallBack()) getCallBack()->Load(anAISIO);
689 if (toActivateInSelectionModes)
690 myWorkshop->selectionActivate()->activateOnDisplay(anAISIO, theUpdateViewer);
698 //**************************************************************
699 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
701 bool aErased = false;
702 Handle(AIS_InteractiveContext) aContext = AISContext();
703 if (!aContext.IsNull()) {
704 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
705 if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
706 aContext->Remove(anAISIO, false/*update viewer*/);
708 if (getCallBack()) getCallBack()->Remove(anAISIO);
713 if (aErased && theUpdateViewer)
718 //**************************************************************
719 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
721 if (theMode == NoMode)
724 Handle(AIS_InteractiveContext) aContext = AISContext();
725 if (aContext.IsNull())
728 AISObjectPtr aAISObj = getAISObject(theObject);
732 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
733 aContext->SetDisplayMode(aAISIO, theMode, false);
734 // Redisplay in order to update new mode because it could be not computed before
739 //**************************************************************
740 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
742 Handle(AIS_InteractiveContext) aContext = AISContext();
743 if (aContext.IsNull())
746 AISObjectPtr aAISObj = getAISObject(theObject);
750 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
751 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
754 //**************************************************************
755 void XGUI_Displayer::deactivateSelectionFilters(const bool theAddFilterOnly)
757 Handle(AIS_InteractiveContext) aContext = AISContext();
758 if (!myAndFilter.IsNull()) {
760 if (!aContext.IsNull()) {
761 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
762 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
763 for (; anIt.More() && !aFound; anIt.Next()) {
764 Handle(SelectMgr_Filter) aFilter = anIt.Value();
765 aFound = aFilter == myAndFilter;
768 aContext->RemoveFilter(myAndFilter);
770 myAndFilter.Nullify();
774 //**************************************************************
775 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
777 Handle(AIS_InteractiveContext) aContext = AISContext();
778 if (aContext.IsNull() || hasSelectionFilter(theFilter))
781 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
782 if (!aCompositeFilter.IsNull()) {
783 aCompositeFilter->Add(theFilter);
784 #ifdef DEBUG_SELECTION_FILTERS
785 int aCount = aCompositeFilter->StoredFilters().Extent();
786 qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
791 //**************************************************************
792 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
794 Handle(AIS_InteractiveContext) aContext = AISContext();
795 if (aContext.IsNull())
798 Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
799 if (!aCompositeFilter.IsNull() && aCompositeFilter->IsIn(theFilter)) {
800 aCompositeFilter->Remove(theFilter);
801 #ifdef DEBUG_SELECTION_FILTERS
802 int aCount = aCompositeFilter->StoredFilters().Extent();
803 qDebug(QString("removeSelectionFilter: filters.count() = %1")
804 .arg(aCount).toStdString().c_str());
809 //**************************************************************
810 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
812 bool aFilterFound = false;
814 Handle(AIS_InteractiveContext) aContext = AISContext();
815 if (aContext.IsNull())
817 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
818 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
819 for (; aIt.More() && !aFilterFound; aIt.Next()) {
820 if (theFilter.get() == aIt.Value().get())
823 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
824 if (!aCompositeFilter.IsNull()) {
825 const SelectMgr_ListOfFilter& aStoredFilters = aCompositeFilter->StoredFilters();
826 for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
827 if (theFilter.get() == aIt.Value().get())
834 //**************************************************************
835 void XGUI_Displayer::removeFilters()
837 Handle(AIS_InteractiveContext) aContext = AISContext();
838 if (aContext.IsNull())
841 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
842 if (!aCompositeFilter.IsNull())
843 aCompositeFilter->Clear();
846 //**************************************************************
847 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
849 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
850 foreach(ObjectPtr aObj, aDispList) {
851 if (!theList.contains(aObj))
854 foreach(ObjectPtr aObj, theList) {
855 if (!isVisible(aObj))
856 display(aObj, false);
861 //**************************************************************
862 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
864 if (!isVisible(theObject))
867 AISObjectPtr aAISObj = getAISObject(theObject);
868 if (aAISObj.get() == NULL)
871 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
872 return ::canBeShaded(anAIS, myWorkshop->module());
875 //**************************************************************
876 bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
878 AISObjectPtr anAISObj = getAISObject(theObject);
879 // correct the result's color it it has the attribute
880 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
882 // Customization of presentation
883 GeomCustomPrsPtr aCustomPrs;
884 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
885 if (aFeature.get() != NULL) {
886 GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
887 if (aCustPrs.get() != NULL)
888 aCustomPrs = aCustPrs;
890 if (aCustomPrs.get() == NULL) {
891 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
892 // we ignore presentable not customized objects
893 if (aPrs.get() == NULL)
894 aCustomPrs = myCustomPrs;
896 bool isCustomized = aCustomPrs.get() &&
897 aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
898 isCustomized = myWorkshop->module()->afterCustomisePresentation(aResult, anAISObj, myCustomPrs)
901 // update presentation state if faces panel is active
902 if (anAISObj.get() && myWorkshop->facesPanel())
903 isCustomized = myWorkshop->facesPanel()->customizeObject(theObject, anAISObj) || isCustomized;
908 //**************************************************************
909 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject,
910 const QColor& theColor,
911 bool theUpdateViewer)
913 if (!isVisible(theObject))
916 AISObjectPtr anAISObj = getAISObject(theObject);
918 anAISObj->getColor(aR, aG, aB);
919 anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
922 return QColor(aR, aG, aB);
925 //**************************************************************
926 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
928 myResult2AISObjectMap[theObject] = theAIS;
931 std::ostringstream aPtrStr;
932 aPtrStr << theObject.get();
933 qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
934 qDebug(getResult2AISObjectMapInfo().c_str());
939 //**************************************************************
940 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
942 QStringList aContent;
943 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
944 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
945 std::ostringstream aPtrStr;
946 aPtrStr << "aObj = " << aObj.get() << ":";
947 aPtrStr << "anAIS = " << aAISObj.get() << ":";
948 aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
950 aContent.append(aPtrStr.str().c_str());
952 return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
953 arg(aContent.join("\n")).toStdString().c_str();
957 //**************************************************************
958 void XGUI_Displayer::getPresentations(const ObjectPtr& theObject,
959 NCollection_Map<Handle(AIS_InteractiveObject)>& thePresentations)
961 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
963 AISObjectPtr aAISObj = getAISObject(aResult);
964 if (aAISObj.get() == NULL) {
965 // if result is a result of a composite feature, it is visualized by visualization of
966 // composite children, so we should get one of this presentations
967 ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResult);
968 if (aCompSolid.get() && aCompSolid->numberOfSubs() > 0) {
969 aAISObj = getAISObject(aCompSolid->subResult(0));
972 if (aAISObj.get() != NULL) {
973 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
974 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
975 thePresentations.Add(anAIS);
979 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
980 // find presentation of the feature
981 AISObjectPtr aAISObj = getAISObject(aFeature);
982 if (aAISObj.get() != NULL) {
983 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
984 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
985 thePresentations.Add(anAIS);
987 // find presentations of the feature results
988 std::list<ResultPtr> aResults;
989 ModelAPI_Tools::allResults(aFeature, aResults);
990 std::list<ResultPtr>::const_iterator anIt = aResults.begin(), aLast = aResults.end();
991 for (; anIt != aLast; ++anIt) {
992 AISObjectPtr aAISObj = getAISObject(*anIt);
993 if (aAISObj.get() != NULL) {
994 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
995 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
996 thePresentations.Add(anAIS);
1002 //**************************************************************
1003 void XGUI_Displayer::displayTrihedron(bool theToDisplay) const
1005 Handle(AIS_InteractiveContext) aContext = AISContext();
1006 if (aContext.IsNull())
1009 Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
1011 XGUI_SelectionActivate* aSelectionActive = selectionActivate();
1013 if (!aContext->IsDisplayed(aTrihedron))
1014 aContext->Display(aTrihedron,
1016 -1 /* selection mode */,
1017 Standard_True /* update viewer*/,
1018 Standard_False /* allow decomposition */,
1019 AIS_DS_Displayed /* xdisplay status */);
1021 if (getCallBack()) getCallBack()->Display(aTrihedron);
1024 if (!aSelectionActive->isTrihedronActive())
1025 aSelectionActive->deactivateTrihedron(false);
1027 aSelectionActive->activate(aTrihedron, false);
1029 aSelectionActive->deactivateTrihedron(false);
1031 aContext->Erase(aTrihedron, Standard_True);
1033 if (getCallBack()) getCallBack()->Remove(aTrihedron);
1040 //**************************************************************
1041 void XGUI_Displayer::AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
1042 const NCollection_DataMap<TopoDS_Shape,
1043 NCollection_Map<Handle(AIS_InteractiveObject)>>& theShapesToBeSelected)
1045 NCollection_Map<Handle(AIS_InteractiveObject)> aCompsolidPresentations;
1046 NCollection_Map<Handle(AIS_InteractiveObject)> aSelectedPresentations;
1048 NCollection_List<Handle(SelectBasics_EntityOwner)> anActiveOwners;
1049 theContext->MainSelector()->ActiveOwners(anActiveOwners);
1050 NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (anActiveOwners);
1051 Handle(SelectMgr_EntityOwner) anOwner;
1053 /// It is very important to check that the owner is processed only once and has a map of
1054 /// processed owners because SetSelected works as a switch.
1055 /// If count of calls setSelectec is even, the object stays in the previous state
1056 /// (selected, deselected)
1057 /// OCCT: to write about the problem that active owners method returns one owner several times
1058 QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
1059 for (; anOwnersIt.More(); anOwnersIt.Next()) {
1060 anOwner = Handle(SelectMgr_EntityOwner)::DownCast (anOwnersIt.Value());
1061 if (aSelectedIds.contains((long)anOwner.get()))
1063 aSelectedIds.append((long)anOwner.get());
1065 Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
1066 if (!BROwnr.IsNull() && BROwnr->HasShape()) {
1067 const TopoDS_Shape& aShape = BROwnr->Shape();
1068 if (aShape.IsNull())
1071 Handle(ModuleBase_BRepOwner) aCustomOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
1073 NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)> >
1074 ::Iterator aShapeIt(theShapesToBeSelected);
1075 for (; aShapeIt.More(); aShapeIt.Next()) {
1076 const TopoDS_Shape& aParameterShape = aShapeIt.Key();
1077 // isSame should be used here as it does not check orientation of shapes
1078 // despite on isEqual of shapes or IsBound for shape in QMap. Orientation is
1079 // different for Edges shapes in model shape and owner even if this is the same shape
1080 if (aParameterShape.IsSame(aShape)) {
1081 Handle(AIS_InteractiveObject) anOwnerPresentation =
1082 Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
1083 NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations =
1084 theShapesToBeSelected.Find(aParameterShape);
1085 if (aPresentations.Contains(anOwnerPresentation)) {
1086 theContext->AddOrRemoveSelected(anOwner, Standard_False);
1087 anOwner->SetSelected (Standard_True);
1088 // collect selected presentations to do not select them if compsolid is selected
1089 if (!aSelectedPresentations.Contains(anOwnerPresentation))
1090 aSelectedPresentations.Add(anOwnerPresentation);
1093 else if (!aCustomOwner.IsNull()) { // CompSolid processing #2219
1094 // shape of owner is compound, but shape to be selected is compsolid, so
1095 // we need to compare shape to AIS presentation of owner(rule of the owner creation)
1096 Handle(AIS_Shape) anOwnerPresentation =
1097 Handle(AIS_Shape)::DownCast(anOwner->Selectable());
1098 const TopoDS_Shape& aPresentationShape = anOwnerPresentation->Shape();
1099 if (aParameterShape.IsSame(anOwnerPresentation->Shape()) &&
1100 !aCompsolidPresentations.Contains(anOwnerPresentation))
1101 aCompsolidPresentations.Add(anOwnerPresentation);
1106 // select CompSolid presentations if their owners was not selected yet
1107 NCollection_Map<Handle(AIS_InteractiveObject)>::Iterator anIt (aCompsolidPresentations);
1108 for (; anIt.More(); anIt.Next()) {
1109 if (aSelectedPresentations.Contains(anIt.Value()))
1111 theContext->AddOrRemoveSelected(anIt.Value(), Standard_False);
1115 //**************************************************************
1116 XGUI_SelectionActivate* XGUI_Displayer::selectionActivate() const
1118 return myWorkshop->selectionActivate();