1 // Copyright (C) 2014-2024 CEA, EDF
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 email : webmaster.salome@opencascade.com
20 #include "XGUI_Displayer.h"
22 #include "XGUI_FacesPanel.h"
23 #include "XGUI_Selection.h"
24 #include "XGUI_SelectionActivate.h"
25 #include "XGUI_SelectionMgr.h"
26 #include "XGUI_ViewerProxy.h"
27 #include "XGUI_Workshop.h"
30 #include <AppElements_Viewer.h>
33 #include <ModelAPI_Document.h>
34 #include <ModelAPI_Data.h>
35 #include <ModelAPI_Object.h>
36 #include <ModelAPI_Tools.h>
37 #include <ModelAPI_AttributeIntArray.h>
38 #include <ModelAPI_ResultBody.h>
39 #include <ModelAPI_ResultGroup.h>
41 #include <ModuleBase_BRepOwner.h>
42 #include <ModuleBase_IModule.h>
43 #include <ModuleBase_Preferences.h>
44 #include <ModuleBase_Tools.h>
45 #include <ModuleBase_ViewerPrs.h>
46 #include <ModuleBase_IViewer.h>
48 #include <GeomAPI_Shape.h>
49 #include <GeomAPI_IPresentable.h>
50 #include <GeomAPI_ICustomPrs.h>
51 #include <GeomAPI_Pnt.h>
52 #include <GeomAPI_IScreenParams.h>
54 #include <SUIT_Application.h>
55 #include <SUIT_ResourceMgr.h>
56 #include <SUIT_Session.h>
57 #include <SUIT_ViewManager.h>
58 #include <SUIT_ViewModel.h>
60 #include <AIS_InteractiveContext.hxx>
61 #include <AIS_ListOfInteractive.hxx>
62 #include <AIS_ListIteratorOfListOfInteractive.hxx>
63 #include <AIS_Shape.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 <Graphic3d_MapOfStructure.hxx>
72 #include <Graphic3d_MapIteratorOfMapOfStructure.hxx>
73 #include <Prs3d_Drawer.hxx>
74 #include <Prs3d_IsoAspect.hxx>
75 #include <PrsDim_Dimension.hxx>
76 #include <SelectMgr_ListOfFilter.hxx>
77 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
78 #include <SelectMgr_SelectionManager.hxx>
79 #include <TColStd_ListIteratorOfListOfInteger.hxx>
80 #include <BRepMesh_IncrementalMesh.hxx>
82 #include <StdSelect_ViewerSelector3d.hxx>
84 #include <TColStd_MapOfTransient.hxx>
85 #include <TColStd_MapIteratorOfMapOfTransient.hxx>
86 #include <Standard_Version.hxx>
89 #include <inspector/VInspectorAPI_CallBack.hxx>
92 #include <Events_Loop.h>
93 #include <ModelAPI_Events.h>
94 #include <Config_PropManager.h>
99 #pragma warning(disable: 4702)
102 /// defines the local context mouse selection sensitivity
103 const int MOUSE_SENSITIVITY_IN_PIXEL = 10;
105 /// defines the display priority for results brought to front
106 constexpr int FRONT_DISPLAY_PRIORITY = 8; /* = Graphic3d_DisplayPriority_Highlight-1 */
108 /// defines the default (normal) display priority for all results
109 constexpr int DEFAULT_DISPLAY_PRIORITY = 5; /* = Graphic3d_DisplayPriority_Normal */
111 //#define DEBUG_DISPLAY
112 //#define DEBUG_FEATURE_REDISPLAY
113 //#define DEBUG_SELECTION_FILTERS
115 //#define DEBUG_OCCT_SHAPE_SELECTION
117 #define CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
119 //#define DEBUG_VIEWER_BLOCKED_COUNT
121 //**************************************************************
122 void displayedObjects(const Handle(AIS_InteractiveContext)& theAIS, AIS_ListOfInteractive& theList)
124 // Get from null point
125 #if OCC_VERSION_HEX < 0x070400
126 theAIS->DisplayedObjects(theList, true);
128 theAIS->DisplayedObjects(theList);
132 QString qIntListInfo(const QIntList& theValues, const QString& theSeparator = QString(", "))
135 QIntList::const_iterator anIt = theValues.begin(), aLast = theValues.end();
136 for (; anIt != aLast; anIt++) {
137 anInfo.append(QString::number(*anIt));
139 return anInfo.join(theSeparator);
142 /// Apply the clipping planes to all objects in the "OCC3D" viewer.
143 void applyClippingPlanes()
145 SUIT_Application *pApp = SUIT_Session::session()->activeApplication();
146 QList<SUIT_ViewManager*> viewMgrs;
147 pApp->viewManagers(QString("OCCViewer"), viewMgrs);
148 if (!viewMgrs.isEmpty()) {
149 SUIT_ViewManager *pViewMgr = viewMgrs.first();
151 SUIT_ViewModel *pViewMdl = pViewMgr->getViewModel();
153 pViewMdl->applyClippingPlanes(true);
159 //**************************************************************
160 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
161 : myWorkshop(theWorkshop),
162 myViewerBlockedRecursiveCount(0),
166 BRepMesh_IncrementalMesh::SetParallelDefault(Standard_True);
169 //**************************************************************
170 XGUI_Displayer::~XGUI_Displayer()
174 //**************************************************************
175 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
177 return myResult2AISObjectMap.contains(theObject);
180 //**************************************************************
181 bool XGUI_Displayer::display(ObjectPtr theObject, bool theUpdateViewer)
183 bool aDisplayed = false;
184 if (isVisible(theObject)) {
185 aDisplayed = redisplay(theObject, theUpdateViewer);
188 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
189 bool isShading = false;
190 if (aPrs.get() != NULL) {
191 GeomScreenParamsPtr aScreen = std::dynamic_pointer_cast<GeomAPI_IScreenParams>(theObject);
193 aScreen->setScreenPlane(getScreenPlane());
194 aScreen->setViewScale(getViewScale());
196 anAIS = aPrs->getAISObject(anAIS);
198 myWorkshop->module()->customizePresentation(theObject, anAIS);
200 anAIS = myWorkshop->module()->createPresentation(theObject);
204 aDisplayed = display(theObject, anAIS, isShading, theUpdateViewer);
208 applyClippingPlanes();
215 //**************************************************************
216 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS, ModuleBase_IModule* theModule)
218 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
219 if (!aShapePrs.IsNull()) {
220 TopoDS_Shape aShape = aShapePrs->Shape();
223 TopAbs_ShapeEnum aType = aShape.ShapeType();
224 if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
227 // Check that the presentation is not a sketch
228 return theModule->canBeShaded(theAIS);
234 //**************************************************************
235 bool XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
236 bool isShading, bool theUpdateViewer)
238 bool aDisplayed = false;
240 Handle(AIS_InteractiveContext) aContext = AISContext();
241 if (aContext.IsNull())
244 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
245 if (!anAISIO.IsNull()) {
246 appendResultObject(theObject, theAIS);
248 // bos#40617: Apply clipping planes
249 // Retrieve the clipping plane from the OCCT Presentation Manager directly,
250 // as they are stored in the ViewModel of the OCCViewer in GUI, where we
251 // don't have access to.
252 Handle(ViewerData_AISShape) aShape = Handle(ViewerData_AISShape)::DownCast (anAISIO);
253 if (!aShape.IsNull() && aShape->IsClippable()) {
254 Graphic3d_MapOfStructure aSetOfStructures;
255 aContext->MainPrsMgr()->StructureManager()->DisplayedStructures( aSetOfStructures );
256 Graphic3d_MapIteratorOfMapOfStructure aStructureIt( aSetOfStructures );
257 for( ; aStructureIt.More(); aStructureIt.Next() ) {
258 const Handle(Graphic3d_Structure)& aStructure = aStructureIt.Key();
259 if ( aStructure->IsEmpty() || !aStructure->IsVisible() || aStructure->CStructure()->IsForHighlight )
261 const Handle(Graphic3d_SequenceOfHClipPlane) &planes = aStructure->ClipPlanes();
262 if (!planes.IsNull() && !planes->IsEmpty()) {
263 aShape->SetClipPlanes(planes);
269 //bool isCustomized = customizeObject(theObject);
271 int aDispMode = isShading? Shading : Wireframe;
272 anAISIO->SetDisplayMode(aDispMode);
273 aContext->Display(anAISIO, aDispMode, 0, false, AIS_DS_Displayed);
274 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
275 if (ModelAPI_Tools::isBringToFront(aResult)) {
276 // NOTE: do not use a priority higher than 8 as highlight uses priority 9!
277 // Any higher level prevents the highlight from being visible as it would
278 // appear "behind" the Groups.
279 aContext->SetDisplayPriority(anAISIO, FRONT_DISPLAY_PRIORITY);
282 if (getCallBack()) getCallBack()->Display(anAISIO);
286 emit objectDisplayed(theObject, theAIS);
287 selectionActivate()->activate(anAISIO, theUpdateViewer);
289 myWorkshop->updateGroupsText();
296 //**************************************************************
297 bool XGUI_Displayer::erase(ObjectPtr theObject, const bool theUpdateViewer)
299 bool aErased = false;
300 if (!isVisible(theObject))
303 Handle(AIS_InteractiveContext) aContext = AISContext();
304 if (aContext.IsNull())
307 AISObjectPtr anObject = myResult2AISObjectMap.value(theObject);
309 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
310 if (!anAIS.IsNull()) {
311 emit beforeObjectErase(theObject, anObject);
312 aContext->Remove(anAIS, false/*update viewer*/);
314 if (getCallBack()) getCallBack()->Remove(anAIS);
319 myResult2AISObjectMap.remove(theObject);
322 applyClippingPlanes();
326 std::ostringstream aPtrStr;
327 aPtrStr << theObject.get();
328 qDebug(QString("erase object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
329 qDebug(getResult2AISObjectMapInfo().c_str());
331 myWorkshop->updateGroupsText();
338 //**************************************************************
339 bool XGUI_Displayer::redisplay(ObjectPtr theObject, bool theUpdateViewer)
341 bool aRedisplayed = false;
342 Handle(AIS_InteractiveContext) aContext = AISContext();
343 if (aContext.IsNull())
346 if (!isVisible(theObject))
349 AISObjectPtr aAISObj = getAISObject(theObject);
350 Handle(AIS_InteractiveObject) aAISIO;
352 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
354 GeomScreenParamsPtr aScreen = std::dynamic_pointer_cast<GeomAPI_IScreenParams>(theObject);
356 aScreen->setScreenPlane(getScreenPlane());
357 aScreen->setViewScale(getViewScale());
359 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
361 aRedisplayed = erase(theObject, theUpdateViewer);
364 if (aAIS_Obj != aAISObj) {
365 erase(theObject, theUpdateViewer);
366 appendResultObject(theObject, aAIS_Obj);
368 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
371 aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
374 if (!aAISIO.IsNull()) {
375 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
378 std::vector<int> aColor;
379 ModelAPI_Tools::getColor(aResult, aColor);
380 if (aColor.size() > 0) {
382 aCol(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB);
383 aAISIO->SetColor(aCol);
386 double aDeflection = ModelAPI_Tools::getDeflection(aResult);
387 if ((aDeflection >= 0) && (aDeflection != aAISObj->getDeflection()))
388 aAISObj->setDeflection(aDeflection);
391 double aTransparency = ModelAPI_Tools::getTransparency(aResult);
392 if ((aTransparency >= 0) && (aTransparency != aAISObj->getTransparency()))
393 aAISObj->setTransparency(aTransparency);
396 Handle(ModuleBase_ResultPrs) aResPrs = Handle(ModuleBase_ResultPrs)::DownCast(aAISIO);
397 if (!aResPrs.IsNull())
398 aResPrs->updateIsoLines();
400 // Only support the "Bring To Front" command for Groups (for now)
401 ResultGroupPtr aGroup = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(aResult);
403 bool isInFront = ModelAPI_Tools::isBringToFront(aResult);
404 aContext->SetDisplayPriority(aAISIO, (isInFront ? FRONT_DISPLAY_PRIORITY : DEFAULT_DISPLAY_PRIORITY));
407 //myWorkshop->module()->storeSelection();
409 //#ifdef CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
410 //myWorkshop->selector()->deselectPresentation(aAISIO);
413 if (aContext->IsDisplayed(aAISIO))
414 aContext->Redisplay(aAISIO, false);
416 aContext->Display(aAISIO, false);
419 if (getCallBack()) getCallBack()->Redisplay(aAISIO);
422 //if (aNeedToRestoreSelection)
423 //myWorkshop->module()->restoreSelection();
426 #ifdef DEBUG_FEATURE_REDISPLAY
427 qDebug(" Redisplay happens");
429 myWorkshop->updateGroupsText();
436 //**************************************************************
437 void XGUI_Displayer::redisplayObjects()
439 // redisplay objects visualized in the viewer
440 static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
441 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
442 QObjectPtrList aDisplayed = displayedObjects();
443 QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
444 for (; anIt != aLast; anIt++) {
445 aECreator->sendUpdated(*anIt, EVENT_DISP);
447 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
448 if (aViewer->isColorScaleVisible()) {
449 aViewer->setupColorScale();
450 aViewer->setColorScaleShown(true);
452 Events_Loop::loop()->flush(EVENT_DISP);
455 //**************************************************************
456 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
457 const bool theUpdateViewer)
459 //Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
460 //if (!aTrihedron.IsNull())
461 // deactivateAIS(aTrihedron);
463 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
464 for (; anIt != aLast; anIt++) {
465 selectionActivate()->deactivate(*anIt, false);
467 //VSV It seems that there is no necessity to update viewer on deactivation
468 //if (theUpdateViewer)
472 //**************************************************************
473 bool XGUI_Displayer::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
475 bool aVisible = false;
476 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
477 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
478 if (aPrs.get() || aResult.get()) {
479 aVisible = theDisplayer->isVisible(theObject);
480 // compsolid is not visualized in the viewer,
481 // but should have presentation when all sub solids are
482 // visible. It is useful for highlight presentation where compsolid shape is selectable
483 if (!aVisible && aResult.get() && aResult->groupName() == ModelAPI_ResultBody::group()) {
484 ResultBodyPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
485 if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
486 int aNumberOfSubs = aCompsolidResult->numberOfSubs();
487 bool anAllSubsVisible = aNumberOfSubs > 0;
488 for(int i = 0; i < aNumberOfSubs && anAllSubsVisible; i++) {
489 anAllSubsVisible = theDisplayer->isVisible(aCompsolidResult->subResult(i));
491 aVisible = anAllSubsVisible;
495 // it is possible that feature is presentable and has results, so we should check visibility
496 // of results if presentation is not shown (e.g. Sketch Circle/Arc features)
498 // check if all results of the feature are visible
499 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
500 std::list<ResultPtr> aResults;
501 ModelAPI_Tools::allResults(aFeature, aResults);
502 std::list<ResultPtr>::const_iterator aIt;
503 aVisible = !aResults.empty();
504 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
505 aVisible = aVisible && theDisplayer->isVisible(*aIt);
511 //**************************************************************
512 void XGUI_Displayer::setSelected(const QList<ModuleBase_ViewerPrsPtr>& theValues,
513 bool theUpdateViewer)
515 Handle(AIS_InteractiveContext) aContext = AISContext();
516 if (aContext.IsNull())
518 aContext->UnhilightSelected(false);
519 aContext->ClearSelected(false);
521 if (getCallBack()) getCallBack()->ClearSelected();
523 NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)>>
526 foreach (ModuleBase_ViewerPrsPtr aPrs, theValues) {
527 const GeomShapePtr& aGeomShape = aPrs->shape();
528 if (aGeomShape.get() && !aGeomShape->isNull()) {
529 const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
530 #ifdef DEBUG_OCCT_SHAPE_SELECTION
531 // problem 1: performance
532 // problem 2: IO is not specified, so the first found owner is selected, as a result
533 // it might belong to another result
534 aContext->AddOrRemoveSelected(aShape, false);
536 if (getCallBack()) getCallBack()->AddOrRemoveSelected(aShape);
539 NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations;
540 if (aShapesToBeSelected.IsBound(aShape))
541 aPresentations = aShapesToBeSelected.Find(aShape);
542 ObjectPtr anObject = aPrs->object();
543 getPresentations(anObject, aPresentations);
545 aShapesToBeSelected.Bind(aShape, aPresentations);
548 ObjectPtr anObject = aPrs->object();
549 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
550 if (aResult.get() && isVisible(aResult)) {
551 AISObjectPtr anObj = myResult2AISObjectMap.value(aResult);
552 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
553 if (!anAIS.IsNull()) {
554 // The methods are replaced in order to provide multi-selection, e.g. restore selection
555 // by activating multi selector widget. It also gives an advantage that the multi
556 // selection in OB gives multi-selection in the viewer
557 //aContext->SetSelected(anAIS, false);
558 // The selection in the context was cleared, so the method sets the objects are selected
559 aContext->AddOrRemoveSelected(anAIS, false);
561 if (getCallBack()) getCallBack()->AddOrRemoveSelected(anAIS);
567 if (!aShapesToBeSelected.IsEmpty())
568 XGUI_Displayer::AddOrRemoveSelectedShapes(aContext, aShapesToBeSelected);
574 //**************************************************************
575 void XGUI_Displayer::clearSelected(const bool theUpdateViewer)
577 Handle(AIS_InteractiveContext) aContext = AISContext();
578 if (!aContext.IsNull()) {
579 aContext->UnhilightSelected(false);//UnhilightCurrents(false);
580 aContext->ClearSelected(theUpdateViewer);
582 if (getCallBack()) getCallBack()->ClearSelected();
587 //**************************************************************
588 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
590 bool aErased = false;
591 Handle(AIS_InteractiveContext) aContext = AISContext();
592 if (!aContext.IsNull()) {
594 foreach(ObjectPtr aObj, myResult2AISObjectMap.objects()) {
595 AISObjectPtr aAISObj = myResult2AISObjectMap.value(aObj);
597 foreach(ObjectPtr aObj, myResult2AISObjectMap.keys()) {
598 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
601 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
602 if (!anIO.IsNull()) {
603 emit beforeObjectErase(aObj, aAISObj);
604 aContext->Remove(anIO, false/*update viewer*/);
606 if (getCallBack()) getCallBack()->Remove(anIO);
611 myWorkshop->updateGroupsText();
615 myResult2AISObjectMap.clear();
618 qDebug(getResult2AISObjectMapInfo().c_str());
624 //**************************************************************
625 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
628 return myResult2AISObjectMap.value(theObject);
631 if (myResult2AISObjectMap.contains(theObject))
632 anIO = myResult2AISObjectMap[theObject];
637 //**************************************************************
638 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
640 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
641 return getObject(aRefAIS);
644 //**************************************************************
645 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
648 ObjectPtr anObject = myResult2AISObjectMap.value(theIO);
651 ResultToAISMap::const_iterator aMapIter = myResult2AISObjectMap.cbegin();
652 for (; aMapIter != myResult2AISObjectMap.cend(); aMapIter++) {
653 const AISObjectPtr& aAIS = aMapIter.value();
654 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
656 anObject = aMapIter.key();
661 if (!anObject.get()) {
662 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
663 if (!theIO.IsNull()) {
664 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
666 if (myWorkshop->module())
667 anObject = myWorkshop->module()->findPresentedObject(anAISObj);
672 //**************************************************************
673 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
675 bool aWasEnabled = isUpdateEnabled();
677 myViewerBlockedRecursiveCount--;
679 myViewerBlockedRecursiveCount++;
681 #ifdef DEBUG_VIEWER_BLOCKED_COUNT
682 std::cout << "myViewerBlockedRecursiveCount = " << myViewerBlockedRecursiveCount << std::endl;
685 if (myNeedUpdate && isUpdateEnabled()) {
687 myNeedUpdate = false;
692 //**************************************************************
693 void XGUI_Displayer::updateViewer() const
695 Handle(AIS_InteractiveContext) aContext = AISContext();
697 #ifdef DEBUG_VIEWER_BLOCKED_COUNT
698 std::cout << "updateViewer: " << (myViewerBlockedRecursiveCount == 0 ? " done" : " later")
702 if (!aContext.IsNull() && isUpdateEnabled()) {
703 //myWorkshop->viewer()->Zfitall();
704 aContext->UpdateCurrentViewer();
710 //**************************************************************
711 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
713 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
714 if (!aContext.IsNull() && (myContextId != aContext.get())) {
715 myContextId = aContext.get();
716 if (!myWorkshop->selectionActivate()->isTrihedronActive())
717 selectionActivate()->deactivateTrihedron(true);
719 // Commented out according to discussion in bug #2825
720 ModuleBase_IViewer::DefaultHighlightDrawer = aContext->HighlightStyle();
722 Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
723 if (!aTrihedron.IsNull())
724 ModuleBase_Tools::setPointBallHighlighting(aTrihedron.get());
729 //**************************************************************
730 void XGUI_Displayer::setSelectionColor(const std::vector<int>& theColor)
732 Handle(AIS_InteractiveContext) aContext = AISContext();
733 if (!aContext.IsNull()) {
734 Quantity_Color aQColor(theColor[0] / 255.,
736 theColor[2] / 255., Quantity_TOC_RGB);
737 aContext->SelectionStyle()->SetColor(aQColor);
738 aContext->SelectionStyle()->PointAspect()->SetColor(aQColor);
739 aContext->SelectionStyle()->LineAspect()->SetColor(aQColor);
740 aContext->HighlightStyle(Prs3d_TypeOfHighlight_LocalSelected)->SetColor(aQColor);
745 //**************************************************************
746 std::vector<int> XGUI_Displayer::selectionColor() const
748 std::vector<int> aColor;
749 Handle(AIS_InteractiveContext) aContext = AISContext();
750 if (!aContext.IsNull()) {
751 Quantity_Color aQColor = aContext->SelectionStyle()->Color();
752 aColor.push_back((int)(aQColor.Red() * 255));
753 aColor.push_back((int)(aQColor.Green() * 255));
754 aColor.push_back((int)(aQColor.Blue() * 255));
760 //**************************************************************
761 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
763 Handle(AIS_InteractiveContext) aContext = AISContext();
764 if (!aContext.IsNull() && myAndFilter.IsNull()) {
765 myAndFilter = new SelectMgr_AndFilter();
766 aContext->AddFilter(myAndFilter);
771 //**************************************************************
772 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
773 const Standard_Integer theDisplayMode, bool theUpdateViewer)
775 bool aDisplayed = false;
776 Handle(AIS_InteractiveContext) aContext = AISContext();
777 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
778 if (!aContext.IsNull() && !anAISIO.IsNull()) {
779 aContext->Display(anAISIO, theDisplayMode, 0, false/*update viewer*/, AIS_DS_Displayed);
781 if (getCallBack()) getCallBack()->Display(anAISIO);
784 aContext->Deactivate(anAISIO);
786 if (getCallBack()) getCallBack()->Deactivate(anAISIO);
788 aContext->Load(anAISIO);
790 if (getCallBack()) getCallBack()->Load(anAISIO);
792 if (toActivateInSelectionModes)
793 myWorkshop->selectionActivate()->activateOnDisplay(anAISIO, theUpdateViewer);
801 //**************************************************************
802 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
804 bool aErased = false;
805 Handle(AIS_InteractiveContext) aContext = AISContext();
806 if (!aContext.IsNull()) {
807 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
808 if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
809 aContext->Remove(anAISIO, false/*update viewer*/);
811 if (getCallBack()) getCallBack()->Remove(anAISIO);
816 if (aErased && theUpdateViewer)
821 //**************************************************************
822 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
824 if (theMode == NoMode)
827 Handle(AIS_InteractiveContext) aContext = AISContext();
828 if (aContext.IsNull())
831 AISObjectPtr aAISObj = getAISObject(theObject);
835 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
836 aContext->SetDisplayMode(aAISIO, theMode, false);
837 // Redisplay in order to update new mode because it could be not computed before
842 //**************************************************************
843 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
845 Handle(AIS_InteractiveContext) aContext = AISContext();
846 if (aContext.IsNull())
849 AISObjectPtr aAISObj = getAISObject(theObject);
853 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
854 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
857 //**************************************************************
858 void XGUI_Displayer::deactivateSelectionFilters(const bool theAddFilterOnly)
860 Handle(AIS_InteractiveContext) aContext = AISContext();
861 if (!myAndFilter.IsNull()) {
863 if (!aContext.IsNull()) {
864 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
865 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
866 for (; anIt.More() && !aFound; anIt.Next()) {
867 Handle(SelectMgr_Filter) aFilter = anIt.Value();
868 aFound = aFilter == myAndFilter;
871 aContext->RemoveFilter(myAndFilter);
873 myAndFilter.Nullify();
877 //**************************************************************
878 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
880 Handle(AIS_InteractiveContext) aContext = AISContext();
881 if (aContext.IsNull() || hasSelectionFilter(theFilter))
884 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
885 if (!aCompositeFilter.IsNull()) {
886 aCompositeFilter->Add(theFilter);
887 #ifdef DEBUG_SELECTION_FILTERS
888 int aCount = aCompositeFilter->StoredFilters().Extent();
889 qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
894 //**************************************************************
895 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
897 Handle(AIS_InteractiveContext) aContext = AISContext();
898 if (aContext.IsNull())
901 Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
902 if (!aCompositeFilter.IsNull() && aCompositeFilter->IsIn(theFilter)) {
903 aCompositeFilter->Remove(theFilter);
904 #ifdef DEBUG_SELECTION_FILTERS
905 int aCount = aCompositeFilter->StoredFilters().Extent();
906 qDebug(QString("removeSelectionFilter: filters.count() = %1")
907 .arg(aCount).toStdString().c_str());
912 //**************************************************************
913 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
915 bool aFilterFound = false;
917 Handle(AIS_InteractiveContext) aContext = AISContext();
918 if (aContext.IsNull())
920 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
921 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
922 for (; aIt.More() && !aFilterFound; aIt.Next()) {
923 if (theFilter.get() == aIt.Value().get())
926 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
927 if (!aCompositeFilter.IsNull()) {
928 const SelectMgr_ListOfFilter& aStoredFilters = aCompositeFilter->StoredFilters();
929 for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
930 if (theFilter.get() == aIt.Value().get())
937 //**************************************************************
938 void XGUI_Displayer::removeFilters()
940 Handle(AIS_InteractiveContext) aContext = AISContext();
941 if (aContext.IsNull())
944 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
945 if (!aCompositeFilter.IsNull())
946 aCompositeFilter->Clear();
949 //**************************************************************
950 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
953 QObjectPtrList aDispList = myResult2AISObjectMap.objects();
955 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
957 foreach(ObjectPtr aObj, aDispList) {
958 if (!theList.contains(aObj))
961 foreach(ObjectPtr aObj, theList) {
962 if (!isVisible(aObj))
963 display(aObj, false);
968 //**************************************************************
969 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
971 if (!isVisible(theObject))
974 AISObjectPtr aAISObj = getAISObject(theObject);
975 if (aAISObj.get() == NULL)
978 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
979 return ::canBeShaded(anAIS, myWorkshop->module());
982 //**************************************************************
983 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject,
984 const QColor& theColor,
985 bool theUpdateViewer)
987 if (!isVisible(theObject))
990 AISObjectPtr anAISObj = getAISObject(theObject);
992 anAISObj->getColor(aR, aG, aB);
993 anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
994 myWorkshop->updateGroupsText();
997 return QColor(aR, aG, aB);
1000 //**************************************************************
1001 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
1004 myResult2AISObjectMap.add(theObject, theAIS);
1006 myResult2AISObjectMap[theObject] = theAIS;
1009 #ifdef DEBUG_DISPLAY
1010 std::ostringstream aPtrStr;
1011 aPtrStr << theObject.get();
1012 qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
1013 qDebug(getResult2AISObjectMapInfo().c_str());
1018 //**************************************************************
1019 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
1021 QStringList aContent;
1023 foreach(ObjectPtr aObj, myResult2AISObjectMap.objects()) {
1024 AISObjectPtr aAISObj = myResult2AISObjectMap.value(aObj);
1026 foreach(ObjectPtr aObj, myResult2AISObjectMap.keys()) {
1027 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
1029 std::ostringstream aPtrStr;
1030 aPtrStr << "aObj = " << aObj.get() << ":";
1031 aPtrStr << "anAIS = " << aAISObj.get() << ":";
1032 aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
1034 aContent.append(aPtrStr.str().c_str());
1036 return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
1037 arg(aContent.join("\n")).toStdString().c_str();
1041 //**************************************************************
1042 void XGUI_Displayer::getPresentations(const ObjectPtr& theObject,
1043 NCollection_Map<Handle(AIS_InteractiveObject)>& thePresentations)
1045 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1046 if (aResult.get()) {
1047 AISObjectPtr aAISObj = getAISObject(aResult);
1048 if (aAISObj.get() == NULL) {
1049 // if result is a result of a composite feature, it is visualized by visualization of
1050 // composite children, so we should get one of this presentations
1051 ResultBodyPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
1052 if (aCompSolid.get() && aCompSolid->numberOfSubs() > 0) {
1053 aAISObj = getAISObject(aCompSolid->subResult(0));
1056 if (aAISObj.get() != NULL) {
1057 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1058 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1059 thePresentations.Add(anAIS);
1063 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
1064 // find presentation of the feature
1065 AISObjectPtr aAISObj = getAISObject(aFeature);
1066 if (aAISObj.get() != NULL) {
1067 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1068 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1069 thePresentations.Add(anAIS);
1071 // find presentations of the feature results
1072 std::list<ResultPtr> aResults;
1073 ModelAPI_Tools::allResults(aFeature, aResults);
1074 std::list<ResultPtr>::const_iterator anIt = aResults.begin(), aLast = aResults.end();
1075 for (; anIt != aLast; ++anIt) {
1076 AISObjectPtr aCurAISObj = getAISObject(*anIt);
1077 if (aCurAISObj.get() != NULL) {
1078 Handle(AIS_InteractiveObject) anAIS = aCurAISObj->impl<Handle(AIS_InteractiveObject)>();
1079 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1080 thePresentations.Add(anAIS);
1086 //**************************************************************
1087 void XGUI_Displayer::displayTrihedron(bool theToDisplay) const
1089 Handle(AIS_InteractiveContext) aContext = AISContext();
1090 if (aContext.IsNull())
1093 Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
1095 XGUI_SelectionActivate* aSelectionActive = selectionActivate();
1097 if (!aContext->IsDisplayed(aTrihedron))
1098 aContext->Display(aTrihedron,
1100 -1 /* selection mode */,
1101 Standard_True /* update viewer*/,
1102 AIS_DS_Displayed /* xdisplay status */);
1104 if (getCallBack()) getCallBack()->Display(aTrihedron);
1107 if (!aSelectionActive->isTrihedronActive())
1108 aSelectionActive->deactivateTrihedron(false);
1110 aSelectionActive->activate(aTrihedron, false);
1112 aSelectionActive->deactivateTrihedron(false);
1114 aContext->Erase(aTrihedron, Standard_True);
1116 if (getCallBack()) getCallBack()->Remove(aTrihedron);
1121 //**************************************************************
1122 void XGUI_Displayer::AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
1123 const NCollection_DataMap<TopoDS_Shape,
1124 NCollection_Map<Handle(AIS_InteractiveObject)>>& theShapesToBeSelected)
1126 NCollection_Map<Handle(AIS_InteractiveObject)> aCompsolidPresentations;
1127 NCollection_Map<Handle(AIS_InteractiveObject)> aSelectedPresentations;
1129 NCollection_List<Handle(SelectBasics_EntityOwner)> anActiveOwners;
1130 theContext->MainSelector()->ActiveOwners(anActiveOwners);
1131 NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (anActiveOwners);
1132 Handle(SelectMgr_EntityOwner) anOwner;
1134 /// It is very important to check that the owner is processed only once and has a map of
1135 /// processed owners because SetSelected works as a switch.
1136 /// If count of calls setSelectec is even, the object stays in the previous state
1137 /// (selected, deselected)
1138 /// OCCT: to write about the problem that active owners method returns one owner several times
1139 QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
1140 for (; anOwnersIt.More(); anOwnersIt.Next()) {
1141 anOwner = anOwnersIt.Value();
1142 if (aSelectedIds.contains((size_t)anOwner.get()))
1144 aSelectedIds.append((size_t)anOwner.get());
1146 Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
1147 if (!BROwnr.IsNull() && BROwnr->HasShape()) {
1148 const TopoDS_Shape& aShape = BROwnr->Shape();
1150 Handle(ModuleBase_ResultPrs) aResPrs =
1151 Handle(ModuleBase_ResultPrs)::DownCast(BROwnr->Selectable());
1152 TopoDS_Shape aRealShape;
1153 if (!aResPrs.IsNull()) {
1154 aRealShape = aResPrs->originalShape();
1157 if (aShape.IsNull())
1160 Handle(ModuleBase_BRepOwner) aCustomOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
1162 NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)> >
1163 ::Iterator aShapeIt(theShapesToBeSelected);
1164 for (; aShapeIt.More(); aShapeIt.Next()) {
1165 const TopoDS_Shape& aParameterShape = aShapeIt.Key();
1166 // In case of compound we cannot rely on simple comparison method.
1167 // If the compound is generated by Group feature then this compound is alwais new.
1168 // So, we have to compare content of these compounds
1170 // isSame should be used here as it does not check orientation of shapes
1171 // despite on isEqual of shapes or IsBound for shape in QMap. Orientation is
1172 // different for Edges shapes in model shape and owner even if this is the same shape
1173 bool isSame = ModuleBase_Tools::isSameShape(aParameterShape, aShape);
1175 // In case of using HideFaces panel we can have instead of an original shape
1176 // a compaund of faces which represent original shape with hidden faces.
1177 // So, we have to compare the parameter with original shape
1178 if (!aRealShape.IsNull())
1179 isSame = ModuleBase_Tools::isSameShape(aParameterShape, aRealShape);
1182 Handle(AIS_InteractiveObject) anOwnerPresentation =
1183 Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
1184 NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations =
1185 theShapesToBeSelected.Find(aParameterShape);
1186 if (aPresentations.Contains(anOwnerPresentation)) {
1187 theContext->AddOrRemoveSelected(anOwner, Standard_False);
1188 anOwner->SetSelected(Standard_True);
1189 // collect selected presentations to do not select them if compsolid is selected
1190 if (!aSelectedPresentations.Contains(anOwnerPresentation))
1191 aSelectedPresentations.Add(anOwnerPresentation);
1194 else if (!aCustomOwner.IsNull()) { // CompSolid processing #2219
1195 // shape of owner is compound, but shape to be selected is compsolid, so
1196 // we need to compare shape to AIS presentation of owner(rule of the owner creation)
1197 Handle(AIS_Shape) anOwnerPresentation =
1198 Handle(AIS_Shape)::DownCast(anOwner->Selectable());
1199 const TopoDS_Shape& aPresentationShape = anOwnerPresentation->Shape();
1200 if (aParameterShape.IsSame(aPresentationShape) &&
1201 !aCompsolidPresentations.Contains(anOwnerPresentation))
1202 aCompsolidPresentations.Add(anOwnerPresentation);
1207 // select CompSolid presentations if their owners was not selected yet
1208 NCollection_Map<Handle(AIS_InteractiveObject)>::Iterator anIt (aCompsolidPresentations);
1209 for (; anIt.More(); anIt.Next()) {
1210 if (aSelectedPresentations.Contains(anIt.Value()))
1212 theContext->AddOrRemoveSelected(anIt.Value(), Standard_False);
1216 //**************************************************************
1217 XGUI_SelectionActivate* XGUI_Displayer::selectionActivate() const
1219 return myWorkshop->selectionActivate();
1222 //**************************************************************
1223 GeomPlanePtr XGUI_Displayer::getScreenPlane() const
1225 GeomPlanePtr aResult;
1226 Handle(AIS_InteractiveContext) aContext = AISContext();
1227 if (!aContext.IsNull()) {
1228 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1229 Handle(V3d_View) aView = aViewer->ActiveViews().First();
1230 if (!aView.IsNull()) {
1231 double aEyeX, aEyeY, aEyeZ;
1232 aView->Eye(aEyeX, aEyeY, aEyeZ);
1234 double aProjX, aProjY, aProjZ;
1235 aView->Proj(aProjX, aProjY, aProjZ);
1237 GeomPointPtr aPnt = GeomPointPtr(new GeomAPI_Pnt(aEyeX, aEyeY, aEyeZ));
1238 GeomDirPtr aDir = GeomDirPtr(new GeomAPI_Dir(aProjX, aProjY, aProjZ));
1240 aResult = GeomPlanePtr(new GeomAPI_Pln(aPnt, aDir));
1246 double XGUI_Displayer::getViewScale() const
1248 Handle(AIS_InteractiveContext) aContext = AISContext();
1249 if (!aContext.IsNull()) {
1250 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1251 Handle(V3d_View) aView = aViewer->ActiveViews().First();
1252 return aView->Camera()->Scale();