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>
48 #include <ModuleBase_IViewer.h>
50 #include <GeomAPI_Shape.h>
51 #include <GeomAPI_IPresentable.h>
52 #include <GeomAPI_ICustomPrs.h>
53 #include <GeomAPI_Pnt.h>
54 #include <GeomAPI_IScreenParams.h>
56 #include <SUIT_ResourceMgr.h>
58 #include <AIS_InteractiveContext.hxx>
59 #include <AIS_ListOfInteractive.hxx>
60 #include <AIS_ListIteratorOfListOfInteractive.hxx>
61 #include <AIS_DimensionSelectionMode.hxx>
62 #include <AIS_Shape.hxx>
63 #include <AIS_Dimension.hxx>
64 #include <AIS_Trihedron.hxx>
65 #ifdef BEFORE_TRIHEDRON_PATCH
66 #include <AIS_Axis.hxx>
67 #include <AIS_Plane.hxx>
68 #include <AIS_Point.hxx>
70 #include <AIS_Selection.hxx>
71 #include <Prs3d_Drawer.hxx>
72 #include <Prs3d_IsoAspect.hxx>
73 #include <SelectMgr_ListOfFilter.hxx>
74 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
75 #include <SelectMgr_SelectionManager.hxx>
76 #include <TColStd_ListIteratorOfListOfInteger.hxx>
78 #include <StdSelect_ViewerSelector3d.hxx>
80 #include <TColStd_MapOfTransient.hxx>
81 #include <TColStd_MapIteratorOfMapOfTransient.hxx>
84 #include <inspector/VInspectorAPI_CallBack.hxx>
87 #include <Events_Loop.h>
88 #include <ModelAPI_Events.h>
92 /// defines the local context mouse selection sensitivity
93 const int MOUSE_SENSITIVITY_IN_PIXEL = 10;
95 //#define DEBUG_DISPLAY
96 //#define DEBUG_FEATURE_REDISPLAY
97 //#define DEBUG_SELECTION_FILTERS
99 //#define DEBUG_COMPOSILID_DISPLAY
101 //#define DEBUG_OCCT_SHAPE_SELECTION
103 #define CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
105 //#define DEBUG_VIEWER_BLOCKED_COUNT
107 //**************************************************************
108 void displayedObjects(const Handle(AIS_InteractiveContext)& theAIS, AIS_ListOfInteractive& theList)
110 // Get from null point
111 theAIS->DisplayedObjects(theList, true);
114 QString qIntListInfo(const QIntList& theValues, const QString& theSeparator = QString(", "))
117 QIntList::const_iterator anIt = theValues.begin(), aLast = theValues.end();
118 for (; anIt != aLast; anIt++) {
119 anInfo.append(QString::number(*anIt));
121 return anInfo.join(theSeparator);
124 //**************************************************************
125 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
126 : myWorkshop(theWorkshop), myNeedUpdate(false),
127 myViewerBlockedRecursiveCount(0), myIsFirstAISContextUse(true)
129 myCustomPrs = std::shared_ptr<GeomAPI_ICustomPrs>(new XGUI_CustomPrs(theWorkshop));
132 //**************************************************************
133 XGUI_Displayer::~XGUI_Displayer()
137 //**************************************************************
138 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
140 return myResult2AISObjectMap.contains(theObject);
143 //**************************************************************
144 bool XGUI_Displayer::display(ObjectPtr theObject, bool theUpdateViewer)
146 bool aDisplayed = false;
147 if (isVisible(theObject)) {
148 #ifdef DEBUG_COMPOSILID_DISPLAY
149 ResultCompSolidPtr aCompsolidResult =
150 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
151 if (aCompsolidResult.get()) {
152 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
153 ResultPtr aSubResult = aCompsolidResult->subResult(i);
154 if (aSubResult.get())
155 redisplay(aSubResult, false);
162 aDisplayed = redisplay(theObject, theUpdateViewer);
165 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
166 bool isShading = false;
167 if (aPrs.get() != NULL) {
168 GeomScreenParamsPtr aScreen = std::dynamic_pointer_cast<GeomAPI_IScreenParams>(theObject);
170 aScreen->setScreenPlane(getScreenPlane());
171 aScreen->setViewScale(getViewScale());
173 anAIS = aPrs->getAISObject(anAIS);
175 // correct deviation coefficient for
176 /*Handle(AIS_InteractiveObject) anAISPrs = anAIS->impl<Handle(AIS_InteractiveObject)>();
177 if (!anAISPrs.IsNull()) {
178 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(anAISPrs);
179 if (!aShapePrs.IsNull()) {
180 TopoDS_Shape aShape = aShapePrs->Shape();
181 if (!aShape.IsNull())
182 //ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, anAISPrs->Attributes());
187 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
188 if (aResult.get() != NULL) {
189 #ifdef DEBUG_COMPOSILID_DISPLAY
190 ResultCompSolidPtr aCompsolidResult =
191 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
192 if (aCompsolidResult.get()) {
193 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
194 ResultPtr aSubResult = aCompsolidResult->subResult(i);
195 if (aSubResult.get())
196 display(aSubResult, false);
203 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
204 if (aShapePtr.get() != NULL) {
205 anAIS = AISObjectPtr(new GeomAPI_AISObject());
206 Handle(AIS_InteractiveObject) anAISPrs =
207 myWorkshop->module()->createPresentation(aResult);
208 if (anAISPrs.IsNull())
209 anAISPrs = new ModuleBase_ResultPrs(aResult);
211 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(anAISPrs);
212 if (!aShapePrs.IsNull())
213 ModuleBase_Tools::setPointBallHighlighting((AIS_Shape*) aShapePrs.get());
215 anAIS->setImpl(new Handle(AIS_InteractiveObject)(anAISPrs));
216 //anAIS->createShape(aShapePtr);
219 #ifdef DEBUG_COMPOSILID_DISPLAY
225 aDisplayed = display(theObject, anAIS, isShading, theUpdateViewer);
230 //**************************************************************
231 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS, ModuleBase_IModule* theModule)
233 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
234 if (!aShapePrs.IsNull()) {
235 TopoDS_Shape aShape = aShapePrs->Shape();
238 TopAbs_ShapeEnum aType = aShape.ShapeType();
239 if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
242 // Check that the presentation is not a sketch
243 return theModule->canBeShaded(theAIS);
249 //**************************************************************
250 bool XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
251 bool isShading, bool theUpdateViewer)
253 bool aDisplayed = false;
255 Handle(AIS_InteractiveContext) aContext = AISContext();
256 if (aContext.IsNull())
259 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
260 if (!anAISIO.IsNull()) {
261 appendResultObject(theObject, theAIS);
263 bool isCustomized = customizeObject(theObject);
265 int aDispMode = isShading? Shading : Wireframe;
267 anAISIO->Attributes()->SetFaceBoundaryDraw( Standard_True );
268 anAISIO->SetDisplayMode(aDispMode);
269 aContext->Display(anAISIO, aDispMode, 0, false, true, AIS_DS_Displayed);
271 if (getCallBack()) getCallBack()->Display(anAISIO);
275 emit objectDisplayed(theObject, theAIS);
276 selectionActivate()->activate(anAISIO, theUpdateViewer);
284 //**************************************************************
285 bool XGUI_Displayer::erase(ObjectPtr theObject, const bool theUpdateViewer)
287 bool aErased = false;
288 if (!isVisible(theObject))
291 Handle(AIS_InteractiveContext) aContext = AISContext();
292 if (aContext.IsNull())
295 AISObjectPtr anObject = myResult2AISObjectMap[theObject];
297 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
298 if (!anAIS.IsNull()) {
299 emit beforeObjectErase(theObject, anObject);
300 aContext->Remove(anAIS, false/*update viewer*/);
302 if (getCallBack()) getCallBack()->Remove(anAIS);
307 myResult2AISObjectMap.remove(theObject);
310 std::ostringstream aPtrStr;
311 aPtrStr << theObject.get();
312 qDebug(QString("erase object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
313 qDebug(getResult2AISObjectMapInfo().c_str());
322 //**************************************************************
323 bool XGUI_Displayer::redisplay(ObjectPtr theObject, bool theUpdateViewer)
325 bool aRedisplayed = false;
326 if (!isVisible(theObject))
329 AISObjectPtr aAISObj = getAISObject(theObject);
330 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
332 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
334 GeomScreenParamsPtr aScreen = std::dynamic_pointer_cast<GeomAPI_IScreenParams>(theObject);
336 aScreen->setScreenPlane(getScreenPlane());
337 aScreen->setViewScale(getViewScale());
339 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
341 aRedisplayed = erase(theObject, theUpdateViewer);
344 if (aAIS_Obj != aAISObj) {
345 appendResultObject(theObject, aAIS_Obj);
347 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
350 Handle(AIS_InteractiveContext) aContext = AISContext();
351 if (!aContext.IsNull() && !aAISIO.IsNull()) {
352 // Check that the visualized shape is the same and the redisplay is not necessary
353 // Redisplay of AIS object leads to this object selection compute and the selection
354 // in the browser is lost
355 // this check is not necessary anymore because the selection store/restore is realized
356 // before and after the values modification.
357 // Moreother, this check avoids customize and redisplay presentation if the presentable
358 // parameter is changed.
359 bool isEqualShapes = false;
360 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
361 if (aResult.get() != NULL) {
362 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(aAISIO);
363 if (!aShapePrs.IsNull()) {
364 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
365 if (aShapePtr.get()) {
366 const TopoDS_Shape& aOldShape = aShapePrs->Shape();
367 if (!aOldShape.IsNull())
368 isEqualShapes = aOldShape.IsEqual(aShapePtr->impl<TopoDS_Shape>());
372 // Customization of presentation
373 bool isCustomized = customizeObject(theObject);
374 #ifdef DEBUG_FEATURE_REDISPLAY
375 qDebug(QString("Redisplay: %1, isEqualShapes=%2, isCustomized=%3").
376 arg(!isEqualShapes || isCustomized).arg(isEqualShapes)
377 .arg(isCustomized).toStdString().c_str());
379 if (!isEqualShapes || isCustomized) {
380 /// if shapes are equal and presentation are customized, selection should be restored
381 bool aNeedToRestoreSelection = isEqualShapes && isCustomized;
382 if (aNeedToRestoreSelection)
383 myWorkshop->module()->storeSelection();
385 #ifdef CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
386 myWorkshop->selector()->deselectPresentation(aAISIO);
388 aContext->Redisplay(aAISIO, false);
391 if (getCallBack()) getCallBack()->Redisplay(aAISIO);
394 if (aNeedToRestoreSelection)
395 myWorkshop->module()->restoreSelection();
398 #ifdef DEBUG_FEATURE_REDISPLAY
399 qDebug(" Redisplay happens");
408 //**************************************************************
409 void XGUI_Displayer::redisplayObjects()
411 // redisplay objects visualized in the viewer
412 static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
413 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
414 QObjectPtrList aDisplayed = myWorkshop->displayer()->displayedObjects();
415 QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
416 for (; anIt != aLast; anIt++) {
417 aECreator->sendUpdated(*anIt, EVENT_DISP);
419 Events_Loop::loop()->flush(EVENT_DISP);
422 //**************************************************************
423 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
424 const bool theUpdateViewer)
426 //Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
427 //if (!aTrihedron.IsNull())
428 // deactivateAIS(aTrihedron);
430 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
431 for (; anIt != aLast; anIt++) {
432 selectionActivate()->deactivate(*anIt, false);
434 //VSV It seems that there is no necessity to update viewer on deactivation
435 //if (theUpdateViewer)
439 //**************************************************************
440 bool XGUI_Displayer::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
442 bool aVisible = false;
443 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
444 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
445 if (aPrs.get() || aResult.get()) {
446 aVisible = theDisplayer->isVisible(theObject);
447 // compsolid is not visualized in the viewer,
448 // but should have presentation when all sub solids are
449 // visible. It is useful for highlight presentation where compsolid shape is selectable
450 if (!aVisible && aResult.get() && aResult->groupName() == ModelAPI_ResultCompSolid::group()) {
451 ResultCompSolidPtr aCompsolidResult =
452 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResult);
453 if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
454 bool anAllSubsVisible = aCompsolidResult->numberOfSubs() > 0;
455 for(int i = 0; i < aCompsolidResult->numberOfSubs() && anAllSubsVisible; i++) {
456 anAllSubsVisible = theDisplayer->isVisible(aCompsolidResult->subResult(i));
458 aVisible = anAllSubsVisible;
462 // it is possible that feature is presentable and has results, so we should check visibility
463 // of results if presentation is not shown (e.g. Sketch Circle/Arc features)
465 // check if all results of the feature are visible
466 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
467 std::list<ResultPtr> aResults;
468 ModelAPI_Tools::allResults(aFeature, aResults);
469 std::list<ResultPtr>::const_iterator aIt;
470 aVisible = !aResults.empty();
471 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
472 aVisible = aVisible && theDisplayer->isVisible(*aIt);
478 //**************************************************************
479 void XGUI_Displayer::setSelected(const QList<ModuleBase_ViewerPrsPtr>& theValues,
480 bool theUpdateViewer)
482 Handle(AIS_InteractiveContext) aContext = AISContext();
483 if (aContext.IsNull())
485 aContext->UnhilightSelected(false);
486 aContext->ClearSelected(false);
488 if (getCallBack()) getCallBack()->ClearSelected();
490 NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)>>
493 foreach (ModuleBase_ViewerPrsPtr aPrs, theValues) {
494 const GeomShapePtr& aGeomShape = aPrs->shape();
495 if (aGeomShape.get() && !aGeomShape->isNull()) {
496 const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
497 #ifdef DEBUG_OCCT_SHAPE_SELECTION
498 // problem 1: performance
499 // problem 2: IO is not specified, so the first found owner is selected, as a result
500 // it might belong to another result
501 aContext->AddOrRemoveSelected(aShape, false);
503 if (getCallBack()) getCallBack()->AddOrRemoveSelected(aShape);
506 NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations;
507 if (aShapesToBeSelected.IsBound(aShape))
508 aPresentations = aShapesToBeSelected.Find(aShape);
509 ObjectPtr anObject = aPrs->object();
510 getPresentations(anObject, aPresentations);
512 aShapesToBeSelected.Bind(aShape, aPresentations);
515 ObjectPtr anObject = aPrs->object();
516 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
517 if (aResult.get() && isVisible(aResult)) {
518 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
519 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
520 if (!anAIS.IsNull()) {
521 // The methods are replaced in order to provide multi-selection, e.g. restore selection
522 // by activating multi selector widget. It also gives an advantage that the multi
523 // selection in OB gives multi-selection in the viewer
524 //aContext->SetSelected(anAIS, false);
525 // The selection in the context was cleared, so the method sets the objects are selected
526 aContext->AddOrRemoveSelected(anAIS, false);
528 if (getCallBack()) getCallBack()->AddOrRemoveSelected(anAIS);
534 if (!aShapesToBeSelected.IsEmpty())
535 XGUI_Displayer::AddOrRemoveSelectedShapes(aContext, aShapesToBeSelected);
541 //**************************************************************
542 void XGUI_Displayer::clearSelected(const bool theUpdateViewer)
544 Handle(AIS_InteractiveContext) aContext = AISContext();
545 if (!aContext.IsNull()) {
546 aContext->UnhilightSelected(false);//UnhilightCurrents(false);
547 aContext->ClearSelected(theUpdateViewer);
549 if (getCallBack()) getCallBack()->ClearSelected();
554 //**************************************************************
555 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
557 bool aErased = false;
558 Handle(AIS_InteractiveContext) aContext = AISContext();
559 if (!aContext.IsNull()) {
560 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
561 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
563 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
564 if (!anIO.IsNull()) {
565 emit beforeObjectErase(aObj, aAISObj);
566 aContext->Remove(anIO, false/*update viewer*/);
568 if (getCallBack()) getCallBack()->Remove(anIO);
576 myResult2AISObjectMap.clear();
579 qDebug(getResult2AISObjectMapInfo().c_str());
585 //**************************************************************
586 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
589 if (myResult2AISObjectMap.contains(theObject))
590 anIO = myResult2AISObjectMap[theObject];
594 //**************************************************************
595 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
597 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
598 return getObject(aRefAIS);
601 //**************************************************************
602 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
605 ResultToAISMap::const_iterator aMapIter = myResult2AISObjectMap.cbegin();
606 for (; aMapIter != myResult2AISObjectMap.cend(); aMapIter++) {
607 const AISObjectPtr& aAIS = aMapIter.value();
608 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
610 anObject = aMapIter.key();
614 if (!anObject.get()) {
615 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
616 if (!theIO.IsNull()) {
617 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
619 anObject = myWorkshop->module()->findPresentedObject(anAISObj);
624 //**************************************************************
625 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
627 bool aWasEnabled = isUpdateEnabled();
629 myViewerBlockedRecursiveCount--;
631 myViewerBlockedRecursiveCount++;
633 #ifdef DEBUG_VIEWER_BLOCKED_COUNT
634 std::cout << "myViewerBlockedRecursiveCount = " << myViewerBlockedRecursiveCount << std::endl;
637 if (myNeedUpdate && isUpdateEnabled()) {
639 myNeedUpdate = false;
644 //**************************************************************
645 bool XGUI_Displayer::isUpdateEnabled() const
647 return myViewerBlockedRecursiveCount == 0;
650 //**************************************************************
651 void XGUI_Displayer::updateViewer() const
653 Handle(AIS_InteractiveContext) aContext = AISContext();
655 #ifdef DEBUG_VIEWER_BLOCKED_COUNT
656 std::cout << "updateViewer: " << (myViewerBlockedRecursiveCount == 0 ? " done" : " later")
660 if (!aContext.IsNull() && isUpdateEnabled()) {
661 //myWorkshop->viewer()->Zfitall();
662 aContext->UpdateCurrentViewer();
668 //**************************************************************
669 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
671 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
672 if (!aContext.IsNull() && myIsFirstAISContextUse/*&& !aContext->HasOpenedContext()*/) {
673 XGUI_Displayer* aDisplayer = (XGUI_Displayer*)this;
674 aDisplayer->myIsFirstAISContextUse = false;
675 if (!myWorkshop->selectionActivate()->isTrihedronActive())
676 selectionActivate()->deactivateTrihedron(true);
677 aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
678 aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
679 ModuleBase_IViewer::DefaultHighlightDrawer = aContext->HighlightStyle();
684 //**************************************************************
685 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
687 Handle(AIS_InteractiveContext) aContext = AISContext();
688 if (!aContext.IsNull() && myAndFilter.IsNull()) {
689 myAndFilter = new SelectMgr_AndFilter();
690 aContext->AddFilter(myAndFilter);
695 //**************************************************************
696 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
697 const Standard_Integer theDisplayMode, bool theUpdateViewer)
699 bool aDisplayed = false;
700 Handle(AIS_InteractiveContext) aContext = AISContext();
701 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
702 if (!aContext.IsNull() && !anAISIO.IsNull()) {
703 aContext->Display(anAISIO, theDisplayMode, 0, false/*update viewer*/, true, AIS_DS_Displayed);
705 if (getCallBack()) getCallBack()->Display(anAISIO);
708 aContext->Deactivate(anAISIO);
710 if (getCallBack()) getCallBack()->Deactivate(anAISIO);
712 aContext->Load(anAISIO);
714 if (getCallBack()) getCallBack()->Load(anAISIO);
716 if (toActivateInSelectionModes)
717 myWorkshop->selectionActivate()->activateOnDisplay(anAISIO, theUpdateViewer);
725 //**************************************************************
726 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
728 bool aErased = false;
729 Handle(AIS_InteractiveContext) aContext = AISContext();
730 if (!aContext.IsNull()) {
731 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
732 if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
733 aContext->Remove(anAISIO, false/*update viewer*/);
735 if (getCallBack()) getCallBack()->Remove(anAISIO);
740 if (aErased && theUpdateViewer)
745 //**************************************************************
746 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
748 if (theMode == NoMode)
751 Handle(AIS_InteractiveContext) aContext = AISContext();
752 if (aContext.IsNull())
755 AISObjectPtr aAISObj = getAISObject(theObject);
759 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
760 aContext->SetDisplayMode(aAISIO, theMode, false);
761 // Redisplay in order to update new mode because it could be not computed before
766 //**************************************************************
767 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
769 Handle(AIS_InteractiveContext) aContext = AISContext();
770 if (aContext.IsNull())
773 AISObjectPtr aAISObj = getAISObject(theObject);
777 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
778 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
781 //**************************************************************
782 void XGUI_Displayer::deactivateSelectionFilters(const bool theAddFilterOnly)
784 Handle(AIS_InteractiveContext) aContext = AISContext();
785 if (!myAndFilter.IsNull()) {
787 if (!aContext.IsNull()) {
788 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
789 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
790 for (; anIt.More() && !aFound; anIt.Next()) {
791 Handle(SelectMgr_Filter) aFilter = anIt.Value();
792 aFound = aFilter == myAndFilter;
795 aContext->RemoveFilter(myAndFilter);
797 myAndFilter.Nullify();
801 //**************************************************************
802 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
804 Handle(AIS_InteractiveContext) aContext = AISContext();
805 if (aContext.IsNull() || hasSelectionFilter(theFilter))
808 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
809 if (!aCompositeFilter.IsNull()) {
810 aCompositeFilter->Add(theFilter);
811 #ifdef DEBUG_SELECTION_FILTERS
812 int aCount = aCompositeFilter->StoredFilters().Extent();
813 qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
818 //**************************************************************
819 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
821 Handle(AIS_InteractiveContext) aContext = AISContext();
822 if (aContext.IsNull())
825 Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
826 if (!aCompositeFilter.IsNull() && aCompositeFilter->IsIn(theFilter)) {
827 aCompositeFilter->Remove(theFilter);
828 #ifdef DEBUG_SELECTION_FILTERS
829 int aCount = aCompositeFilter->StoredFilters().Extent();
830 qDebug(QString("removeSelectionFilter: filters.count() = %1")
831 .arg(aCount).toStdString().c_str());
836 //**************************************************************
837 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
839 bool aFilterFound = false;
841 Handle(AIS_InteractiveContext) aContext = AISContext();
842 if (aContext.IsNull())
844 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
845 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
846 for (; aIt.More() && !aFilterFound; aIt.Next()) {
847 if (theFilter.get() == aIt.Value().get())
850 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
851 if (!aCompositeFilter.IsNull()) {
852 const SelectMgr_ListOfFilter& aStoredFilters = aCompositeFilter->StoredFilters();
853 for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
854 if (theFilter.get() == aIt.Value().get())
861 //**************************************************************
862 void XGUI_Displayer::removeFilters()
864 Handle(AIS_InteractiveContext) aContext = AISContext();
865 if (aContext.IsNull())
868 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
869 if (!aCompositeFilter.IsNull())
870 aCompositeFilter->Clear();
873 //**************************************************************
874 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
876 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
877 foreach(ObjectPtr aObj, aDispList) {
878 if (!theList.contains(aObj))
881 foreach(ObjectPtr aObj, theList) {
882 if (!isVisible(aObj))
883 display(aObj, false);
888 //**************************************************************
889 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
891 if (!isVisible(theObject))
894 AISObjectPtr aAISObj = getAISObject(theObject);
895 if (aAISObj.get() == NULL)
898 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
899 return ::canBeShaded(anAIS, myWorkshop->module());
902 //**************************************************************
903 bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
905 AISObjectPtr anAISObj = getAISObject(theObject);
906 // correct the result's color it it has the attribute
907 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
909 // Customization of presentation
910 GeomCustomPrsPtr aCustomPrs;
911 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
912 if (aFeature.get() != NULL) {
913 GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
914 if (aCustPrs.get() != NULL)
915 aCustomPrs = aCustPrs;
917 if (aCustomPrs.get() == NULL) {
918 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
919 // we ignore presentable not customized objects
920 if (aPrs.get() == NULL)
921 aCustomPrs = myCustomPrs;
923 bool isCustomized = aCustomPrs.get() &&
924 aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
925 isCustomized = myWorkshop->module()->afterCustomisePresentation(aResult, anAISObj, myCustomPrs)
928 // update presentation state if faces panel is active
929 if (anAISObj.get() && myWorkshop->facesPanel())
930 isCustomized = myWorkshop->facesPanel()->customizeObject(theObject, anAISObj) || isCustomized;
935 //**************************************************************
936 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject,
937 const QColor& theColor,
938 bool theUpdateViewer)
940 if (!isVisible(theObject))
943 AISObjectPtr anAISObj = getAISObject(theObject);
945 anAISObj->getColor(aR, aG, aB);
946 anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
949 return QColor(aR, aG, aB);
952 //**************************************************************
953 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
955 myResult2AISObjectMap[theObject] = theAIS;
958 std::ostringstream aPtrStr;
959 aPtrStr << theObject.get();
960 qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
961 qDebug(getResult2AISObjectMapInfo().c_str());
966 //**************************************************************
967 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
969 QStringList aContent;
970 foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
971 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
972 std::ostringstream aPtrStr;
973 aPtrStr << "aObj = " << aObj.get() << ":";
974 aPtrStr << "anAIS = " << aAISObj.get() << ":";
975 aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
977 aContent.append(aPtrStr.str().c_str());
979 return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
980 arg(aContent.join("\n")).toStdString().c_str();
984 //**************************************************************
985 void XGUI_Displayer::getPresentations(const ObjectPtr& theObject,
986 NCollection_Map<Handle(AIS_InteractiveObject)>& thePresentations)
988 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
990 AISObjectPtr aAISObj = getAISObject(aResult);
991 if (aAISObj.get() == NULL) {
992 // if result is a result of a composite feature, it is visualized by visualization of
993 // composite children, so we should get one of this presentations
994 ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResult);
995 if (aCompSolid.get() && aCompSolid->numberOfSubs() > 0) {
996 aAISObj = getAISObject(aCompSolid->subResult(0));
999 if (aAISObj.get() != NULL) {
1000 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1001 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1002 thePresentations.Add(anAIS);
1006 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
1007 // find presentation of the feature
1008 AISObjectPtr aAISObj = getAISObject(aFeature);
1009 if (aAISObj.get() != NULL) {
1010 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1011 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1012 thePresentations.Add(anAIS);
1014 // find presentations of the feature results
1015 std::list<ResultPtr> aResults;
1016 ModelAPI_Tools::allResults(aFeature, aResults);
1017 std::list<ResultPtr>::const_iterator anIt = aResults.begin(), aLast = aResults.end();
1018 for (; anIt != aLast; ++anIt) {
1019 AISObjectPtr aAISObj = getAISObject(*anIt);
1020 if (aAISObj.get() != NULL) {
1021 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1022 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1023 thePresentations.Add(anAIS);
1029 //**************************************************************
1030 void XGUI_Displayer::displayTrihedron(bool theToDisplay) const
1032 Handle(AIS_InteractiveContext) aContext = AISContext();
1033 if (aContext.IsNull())
1036 Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
1038 XGUI_SelectionActivate* aSelectionActive = selectionActivate();
1040 if (!aContext->IsDisplayed(aTrihedron))
1041 aContext->Display(aTrihedron,
1043 -1 /* selection mode */,
1044 Standard_True /* update viewer*/,
1045 Standard_False /* allow decomposition */,
1046 AIS_DS_Displayed /* xdisplay status */);
1048 if (getCallBack()) getCallBack()->Display(aTrihedron);
1051 if (!aSelectionActive->isTrihedronActive())
1052 aSelectionActive->deactivateTrihedron(false);
1054 aSelectionActive->activate(aTrihedron, false);
1056 aSelectionActive->deactivateTrihedron(false);
1058 aContext->Erase(aTrihedron, Standard_True);
1060 if (getCallBack()) getCallBack()->Remove(aTrihedron);
1067 //**************************************************************
1068 void XGUI_Displayer::AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
1069 const NCollection_DataMap<TopoDS_Shape,
1070 NCollection_Map<Handle(AIS_InteractiveObject)>>& theShapesToBeSelected)
1072 NCollection_Map<Handle(AIS_InteractiveObject)> aCompsolidPresentations;
1073 NCollection_Map<Handle(AIS_InteractiveObject)> aSelectedPresentations;
1075 NCollection_List<Handle(SelectBasics_EntityOwner)> anActiveOwners;
1076 theContext->MainSelector()->ActiveOwners(anActiveOwners);
1077 NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (anActiveOwners);
1078 Handle(SelectMgr_EntityOwner) anOwner;
1080 /// It is very important to check that the owner is processed only once and has a map of
1081 /// processed owners because SetSelected works as a switch.
1082 /// If count of calls setSelectec is even, the object stays in the previous state
1083 /// (selected, deselected)
1084 /// OCCT: to write about the problem that active owners method returns one owner several times
1085 QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
1086 for (; anOwnersIt.More(); anOwnersIt.Next()) {
1087 anOwner = Handle(SelectMgr_EntityOwner)::DownCast (anOwnersIt.Value());
1088 if (aSelectedIds.contains((long)anOwner.get()))
1090 aSelectedIds.append((long)anOwner.get());
1092 Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
1093 if (!BROwnr.IsNull() && BROwnr->HasShape()) {
1094 const TopoDS_Shape& aShape = BROwnr->Shape();
1095 if (aShape.IsNull())
1098 Handle(ModuleBase_BRepOwner) aCustomOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
1100 NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)> >
1101 ::Iterator aShapeIt(theShapesToBeSelected);
1102 for (; aShapeIt.More(); aShapeIt.Next()) {
1103 const TopoDS_Shape& aParameterShape = aShapeIt.Key();
1104 // isSame should be used here as it does not check orientation of shapes
1105 // despite on isEqual of shapes or IsBound for shape in QMap. Orientation is
1106 // different for Edges shapes in model shape and owner even if this is the same shape
1107 if (aParameterShape.IsSame(aShape)) {
1108 Handle(AIS_InteractiveObject) anOwnerPresentation =
1109 Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
1110 NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations =
1111 theShapesToBeSelected.Find(aParameterShape);
1112 if (aPresentations.Contains(anOwnerPresentation)) {
1113 theContext->AddOrRemoveSelected(anOwner, Standard_False);
1114 anOwner->SetSelected (Standard_True);
1115 // collect selected presentations to do not select them if compsolid is selected
1116 if (!aSelectedPresentations.Contains(anOwnerPresentation))
1117 aSelectedPresentations.Add(anOwnerPresentation);
1120 else if (!aCustomOwner.IsNull()) { // CompSolid processing #2219
1121 // shape of owner is compound, but shape to be selected is compsolid, so
1122 // we need to compare shape to AIS presentation of owner(rule of the owner creation)
1123 Handle(AIS_Shape) anOwnerPresentation =
1124 Handle(AIS_Shape)::DownCast(anOwner->Selectable());
1125 const TopoDS_Shape& aPresentationShape = anOwnerPresentation->Shape();
1126 if (aParameterShape.IsSame(anOwnerPresentation->Shape()) &&
1127 !aCompsolidPresentations.Contains(anOwnerPresentation))
1128 aCompsolidPresentations.Add(anOwnerPresentation);
1133 // select CompSolid presentations if their owners was not selected yet
1134 NCollection_Map<Handle(AIS_InteractiveObject)>::Iterator anIt (aCompsolidPresentations);
1135 for (; anIt.More(); anIt.Next()) {
1136 if (aSelectedPresentations.Contains(anIt.Value()))
1138 theContext->AddOrRemoveSelected(anIt.Value(), Standard_False);
1142 //**************************************************************
1143 XGUI_SelectionActivate* XGUI_Displayer::selectionActivate() const
1145 return myWorkshop->selectionActivate();
1148 //**************************************************************
1149 GeomPlanePtr XGUI_Displayer::getScreenPlane() const
1151 GeomPlanePtr aResult;
1152 Handle(AIS_InteractiveContext) aContext = AISContext();
1153 if (!aContext.IsNull()) {
1154 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1155 Handle(V3d_View) aView;
1156 for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews()) {
1157 aView = aViewer->ActiveView();
1160 if (!aView.IsNull()) {
1161 double aEyeX, aEyeY, aEyeZ;
1162 aView->Eye(aEyeX, aEyeY, aEyeZ);
1164 double aProjX, aProjY, aProjZ;
1165 aView->Proj(aProjX, aProjY, aProjZ);
1167 GeomPointPtr aPnt = GeomPointPtr(new GeomAPI_Pnt(aEyeX, aEyeY, aEyeZ));
1168 GeomDirPtr aDir = GeomDirPtr(new GeomAPI_Dir(aProjX, aProjY, aProjZ));
1170 aResult = GeomPlanePtr(new GeomAPI_Pln(aPnt, aDir));
1176 double XGUI_Displayer::getViewScale() const
1178 Handle(AIS_InteractiveContext) aContext = AISContext();
1179 if (!aContext.IsNull()) {
1180 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1181 Handle(V3d_View) aView;
1182 for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews()) {
1183 aView = aViewer->ActiveView();
1186 return aView->Camera()->Scale();