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_ResourceMgr.h>
56 #include <AIS_InteractiveContext.hxx>
57 #include <AIS_ListOfInteractive.hxx>
58 #include <AIS_ListIteratorOfListOfInteractive.hxx>
59 #include <AIS_Shape.hxx>
60 #include <AIS_Trihedron.hxx>
61 #ifdef BEFORE_TRIHEDRON_PATCH
62 #include <AIS_Axis.hxx>
63 #include <AIS_Plane.hxx>
64 #include <AIS_Point.hxx>
66 #include <AIS_Selection.hxx>
67 #include <Prs3d_Drawer.hxx>
68 #include <Prs3d_IsoAspect.hxx>
69 #include <PrsDim_Dimension.hxx>
70 #include <SelectMgr_ListOfFilter.hxx>
71 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
72 #include <SelectMgr_SelectionManager.hxx>
73 #include <TColStd_ListIteratorOfListOfInteger.hxx>
74 #include <BRepMesh_IncrementalMesh.hxx>
76 #include <StdSelect_ViewerSelector3d.hxx>
78 #include <TColStd_MapOfTransient.hxx>
79 #include <TColStd_MapIteratorOfMapOfTransient.hxx>
80 #include <Standard_Version.hxx>
83 #include <inspector/VInspectorAPI_CallBack.hxx>
86 #include <Events_Loop.h>
87 #include <ModelAPI_Events.h>
88 #include <Config_PropManager.h>
93 #pragma warning(disable: 4702)
96 /// defines the local context mouse selection sensitivity
97 const int MOUSE_SENSITIVITY_IN_PIXEL = 10;
99 /// defines the display priority for results brought to front
100 constexpr int FRONT_DISPLAY_PRIORITY = 8; /* = Graphic3d_DisplayPriority_Highlight-1 */
102 /// defines the default (normal) display priority for all results
103 constexpr int DEFAULT_DISPLAY_PRIORITY = 5; /* = Graphic3d_DisplayPriority_Normal */
105 //#define DEBUG_DISPLAY
106 //#define DEBUG_FEATURE_REDISPLAY
107 //#define DEBUG_SELECTION_FILTERS
109 //#define DEBUG_OCCT_SHAPE_SELECTION
111 #define CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
113 //#define DEBUG_VIEWER_BLOCKED_COUNT
115 //**************************************************************
116 void displayedObjects(const Handle(AIS_InteractiveContext)& theAIS, AIS_ListOfInteractive& theList)
118 // Get from null point
119 #if OCC_VERSION_HEX < 0x070400
120 theAIS->DisplayedObjects(theList, true);
122 theAIS->DisplayedObjects(theList);
126 QString qIntListInfo(const QIntList& theValues, const QString& theSeparator = QString(", "))
129 QIntList::const_iterator anIt = theValues.begin(), aLast = theValues.end();
130 for (; anIt != aLast; anIt++) {
131 anInfo.append(QString::number(*anIt));
133 return anInfo.join(theSeparator);
136 //**************************************************************
137 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
138 : myWorkshop(theWorkshop),
139 myViewerBlockedRecursiveCount(0),
143 BRepMesh_IncrementalMesh::SetParallelDefault(Standard_True);
146 //**************************************************************
147 XGUI_Displayer::~XGUI_Displayer()
151 //**************************************************************
152 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
154 return myResult2AISObjectMap.contains(theObject);
157 //**************************************************************
158 bool XGUI_Displayer::display(ObjectPtr theObject, bool theUpdateViewer)
160 bool aDisplayed = false;
161 if (isVisible(theObject)) {
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 myWorkshop->module()->customizePresentation(theObject, anAIS);
177 anAIS = myWorkshop->module()->createPresentation(theObject);
181 aDisplayed = display(theObject, anAIS, isShading, theUpdateViewer);
187 //**************************************************************
188 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS, ModuleBase_IModule* theModule)
190 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
191 if (!aShapePrs.IsNull()) {
192 TopoDS_Shape aShape = aShapePrs->Shape();
195 TopAbs_ShapeEnum aType = aShape.ShapeType();
196 if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
199 // Check that the presentation is not a sketch
200 return theModule->canBeShaded(theAIS);
206 //**************************************************************
207 bool XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
208 bool isShading, bool theUpdateViewer)
210 bool aDisplayed = false;
212 Handle(AIS_InteractiveContext) aContext = AISContext();
213 if (aContext.IsNull())
216 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
217 if (!anAISIO.IsNull()) {
218 appendResultObject(theObject, theAIS);
220 //bool isCustomized = customizeObject(theObject);
222 int aDispMode = isShading? Shading : Wireframe;
223 anAISIO->SetDisplayMode(aDispMode);
224 aContext->Display(anAISIO, aDispMode, 0, false, AIS_DS_Displayed);
225 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
226 if (ModelAPI_Tools::isBringToFront(aResult)) {
227 // NOTE: do not use a priority higher than 8 as highlight uses priority 9!
228 // Any higher level prevents the highlight from being visible as it would
229 // appear "behind" the Groups.
230 aContext->SetDisplayPriority(anAISIO, FRONT_DISPLAY_PRIORITY);
233 if (getCallBack()) getCallBack()->Display(anAISIO);
237 emit objectDisplayed(theObject, theAIS);
238 selectionActivate()->activate(anAISIO, theUpdateViewer);
240 myWorkshop->updateGroupsText();
247 //**************************************************************
248 bool XGUI_Displayer::erase(ObjectPtr theObject, const bool theUpdateViewer)
250 bool aErased = false;
251 if (!isVisible(theObject))
254 Handle(AIS_InteractiveContext) aContext = AISContext();
255 if (aContext.IsNull())
258 AISObjectPtr anObject = myResult2AISObjectMap.value(theObject);
260 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
261 if (!anAIS.IsNull()) {
262 emit beforeObjectErase(theObject, anObject);
263 aContext->Remove(anAIS, false/*update viewer*/);
265 if (getCallBack()) getCallBack()->Remove(anAIS);
270 myResult2AISObjectMap.remove(theObject);
273 std::ostringstream aPtrStr;
274 aPtrStr << theObject.get();
275 qDebug(QString("erase object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
276 qDebug(getResult2AISObjectMapInfo().c_str());
278 myWorkshop->updateGroupsText();
285 //**************************************************************
286 bool XGUI_Displayer::redisplay(ObjectPtr theObject, bool theUpdateViewer)
288 bool aRedisplayed = false;
289 Handle(AIS_InteractiveContext) aContext = AISContext();
290 if (aContext.IsNull())
293 if (!isVisible(theObject))
296 AISObjectPtr aAISObj = getAISObject(theObject);
297 Handle(AIS_InteractiveObject) aAISIO;
299 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
301 GeomScreenParamsPtr aScreen = std::dynamic_pointer_cast<GeomAPI_IScreenParams>(theObject);
303 aScreen->setScreenPlane(getScreenPlane());
304 aScreen->setViewScale(getViewScale());
306 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
308 aRedisplayed = erase(theObject, theUpdateViewer);
311 if (aAIS_Obj != aAISObj) {
312 erase(theObject, theUpdateViewer);
313 appendResultObject(theObject, aAIS_Obj);
315 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
318 aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
321 if (!aAISIO.IsNull()) {
322 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
325 std::vector<int> aColor;
326 ModelAPI_Tools::getColor(aResult, aColor);
327 if (aColor.size() > 0) {
329 aCol(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB);
330 aAISIO->SetColor(aCol);
333 double aDeflection = ModelAPI_Tools::getDeflection(aResult);
334 if ((aDeflection >= 0) && (aDeflection != aAISObj->getDeflection()))
335 aAISObj->setDeflection(aDeflection);
338 double aTransparency = ModelAPI_Tools::getTransparency(aResult);
339 if ((aTransparency >= 0) && (aTransparency != aAISObj->getTransparency()))
340 aAISObj->setTransparency(aTransparency);
343 Handle(ModuleBase_ResultPrs) aResPrs = Handle(ModuleBase_ResultPrs)::DownCast(aAISIO);
344 if (!aResPrs.IsNull())
345 aResPrs->updateIsoLines();
347 // Only support the "Bring To Front" command for Groups (for now)
348 ResultGroupPtr aGroup = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(aResult);
350 bool isInFront = ModelAPI_Tools::isBringToFront(aResult);
351 aContext->SetDisplayPriority(aAISIO, (isInFront ? FRONT_DISPLAY_PRIORITY : DEFAULT_DISPLAY_PRIORITY));
354 //myWorkshop->module()->storeSelection();
356 //#ifdef CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
357 //myWorkshop->selector()->deselectPresentation(aAISIO);
360 if (aContext->IsDisplayed(aAISIO))
361 aContext->Redisplay(aAISIO, false);
363 aContext->Display(aAISIO, false);
366 if (getCallBack()) getCallBack()->Redisplay(aAISIO);
369 //if (aNeedToRestoreSelection)
370 //myWorkshop->module()->restoreSelection();
373 #ifdef DEBUG_FEATURE_REDISPLAY
374 qDebug(" Redisplay happens");
376 myWorkshop->updateGroupsText();
383 //**************************************************************
384 void XGUI_Displayer::redisplayObjects()
386 // redisplay objects visualized in the viewer
387 static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
388 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
389 QObjectPtrList aDisplayed = displayedObjects();
390 QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
391 for (; anIt != aLast; anIt++) {
392 aECreator->sendUpdated(*anIt, EVENT_DISP);
394 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
395 if (aViewer->isColorScaleVisible()) {
396 aViewer->setupColorScale();
397 aViewer->setColorScaleShown(true);
399 Events_Loop::loop()->flush(EVENT_DISP);
402 //**************************************************************
403 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
404 const bool theUpdateViewer)
406 //Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
407 //if (!aTrihedron.IsNull())
408 // deactivateAIS(aTrihedron);
410 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
411 for (; anIt != aLast; anIt++) {
412 selectionActivate()->deactivate(*anIt, false);
414 //VSV It seems that there is no necessity to update viewer on deactivation
415 //if (theUpdateViewer)
419 //**************************************************************
420 bool XGUI_Displayer::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
422 bool aVisible = false;
423 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
424 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
425 if (aPrs.get() || aResult.get()) {
426 aVisible = theDisplayer->isVisible(theObject);
427 // compsolid is not visualized in the viewer,
428 // but should have presentation when all sub solids are
429 // visible. It is useful for highlight presentation where compsolid shape is selectable
430 if (!aVisible && aResult.get() && aResult->groupName() == ModelAPI_ResultBody::group()) {
431 ResultBodyPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
432 if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
433 int aNumberOfSubs = aCompsolidResult->numberOfSubs();
434 bool anAllSubsVisible = aNumberOfSubs > 0;
435 for(int i = 0; i < aNumberOfSubs && anAllSubsVisible; i++) {
436 anAllSubsVisible = theDisplayer->isVisible(aCompsolidResult->subResult(i));
438 aVisible = anAllSubsVisible;
442 // it is possible that feature is presentable and has results, so we should check visibility
443 // of results if presentation is not shown (e.g. Sketch Circle/Arc features)
445 // check if all results of the feature are visible
446 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
447 std::list<ResultPtr> aResults;
448 ModelAPI_Tools::allResults(aFeature, aResults);
449 std::list<ResultPtr>::const_iterator aIt;
450 aVisible = !aResults.empty();
451 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
452 aVisible = aVisible && theDisplayer->isVisible(*aIt);
458 //**************************************************************
459 void XGUI_Displayer::setSelected(const QList<ModuleBase_ViewerPrsPtr>& theValues,
460 bool theUpdateViewer)
462 Handle(AIS_InteractiveContext) aContext = AISContext();
463 if (aContext.IsNull())
465 aContext->UnhilightSelected(false);
466 aContext->ClearSelected(false);
468 if (getCallBack()) getCallBack()->ClearSelected();
470 NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)>>
473 foreach (ModuleBase_ViewerPrsPtr aPrs, theValues) {
474 const GeomShapePtr& aGeomShape = aPrs->shape();
475 if (aGeomShape.get() && !aGeomShape->isNull()) {
476 const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
477 #ifdef DEBUG_OCCT_SHAPE_SELECTION
478 // problem 1: performance
479 // problem 2: IO is not specified, so the first found owner is selected, as a result
480 // it might belong to another result
481 aContext->AddOrRemoveSelected(aShape, false);
483 if (getCallBack()) getCallBack()->AddOrRemoveSelected(aShape);
486 NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations;
487 if (aShapesToBeSelected.IsBound(aShape))
488 aPresentations = aShapesToBeSelected.Find(aShape);
489 ObjectPtr anObject = aPrs->object();
490 getPresentations(anObject, aPresentations);
492 aShapesToBeSelected.Bind(aShape, aPresentations);
495 ObjectPtr anObject = aPrs->object();
496 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
497 if (aResult.get() && isVisible(aResult)) {
498 AISObjectPtr anObj = myResult2AISObjectMap.value(aResult);
499 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
500 if (!anAIS.IsNull()) {
501 // The methods are replaced in order to provide multi-selection, e.g. restore selection
502 // by activating multi selector widget. It also gives an advantage that the multi
503 // selection in OB gives multi-selection in the viewer
504 //aContext->SetSelected(anAIS, false);
505 // The selection in the context was cleared, so the method sets the objects are selected
506 aContext->AddOrRemoveSelected(anAIS, false);
508 if (getCallBack()) getCallBack()->AddOrRemoveSelected(anAIS);
514 if (!aShapesToBeSelected.IsEmpty())
515 XGUI_Displayer::AddOrRemoveSelectedShapes(aContext, aShapesToBeSelected);
521 //**************************************************************
522 void XGUI_Displayer::clearSelected(const bool theUpdateViewer)
524 Handle(AIS_InteractiveContext) aContext = AISContext();
525 if (!aContext.IsNull()) {
526 aContext->UnhilightSelected(false);//UnhilightCurrents(false);
527 aContext->ClearSelected(theUpdateViewer);
529 if (getCallBack()) getCallBack()->ClearSelected();
534 //**************************************************************
535 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
537 bool aErased = false;
538 Handle(AIS_InteractiveContext) aContext = AISContext();
539 if (!aContext.IsNull()) {
541 foreach(ObjectPtr aObj, myResult2AISObjectMap.objects()) {
542 AISObjectPtr aAISObj = myResult2AISObjectMap.value(aObj);
544 foreach(ObjectPtr aObj, myResult2AISObjectMap.keys()) {
545 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);
558 myWorkshop->updateGroupsText();
562 myResult2AISObjectMap.clear();
565 qDebug(getResult2AISObjectMapInfo().c_str());
571 //**************************************************************
572 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
575 return myResult2AISObjectMap.value(theObject);
578 if (myResult2AISObjectMap.contains(theObject))
579 anIO = myResult2AISObjectMap[theObject];
584 //**************************************************************
585 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
587 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
588 return getObject(aRefAIS);
591 //**************************************************************
592 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
595 ObjectPtr anObject = myResult2AISObjectMap.value(theIO);
598 ResultToAISMap::const_iterator aMapIter = myResult2AISObjectMap.cbegin();
599 for (; aMapIter != myResult2AISObjectMap.cend(); aMapIter++) {
600 const AISObjectPtr& aAIS = aMapIter.value();
601 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
603 anObject = aMapIter.key();
608 if (!anObject.get()) {
609 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
610 if (!theIO.IsNull()) {
611 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
613 if (myWorkshop->module())
614 anObject = myWorkshop->module()->findPresentedObject(anAISObj);
619 //**************************************************************
620 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
622 bool aWasEnabled = isUpdateEnabled();
624 myViewerBlockedRecursiveCount--;
626 myViewerBlockedRecursiveCount++;
628 #ifdef DEBUG_VIEWER_BLOCKED_COUNT
629 std::cout << "myViewerBlockedRecursiveCount = " << myViewerBlockedRecursiveCount << std::endl;
632 if (myNeedUpdate && isUpdateEnabled()) {
634 myNeedUpdate = false;
639 //**************************************************************
640 void XGUI_Displayer::updateViewer() const
642 Handle(AIS_InteractiveContext) aContext = AISContext();
644 #ifdef DEBUG_VIEWER_BLOCKED_COUNT
645 std::cout << "updateViewer: " << (myViewerBlockedRecursiveCount == 0 ? " done" : " later")
649 if (!aContext.IsNull() && isUpdateEnabled()) {
650 //myWorkshop->viewer()->Zfitall();
651 aContext->UpdateCurrentViewer();
657 //**************************************************************
658 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
660 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
661 if (!aContext.IsNull() && (myContextId != aContext.get())) {
662 myContextId = aContext.get();
663 if (!myWorkshop->selectionActivate()->isTrihedronActive())
664 selectionActivate()->deactivateTrihedron(true);
666 // Commented out according to discussion in bug #2825
667 ModuleBase_IViewer::DefaultHighlightDrawer = aContext->HighlightStyle();
669 Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
670 if (!aTrihedron.IsNull())
671 ModuleBase_Tools::setPointBallHighlighting(aTrihedron.get());
676 //**************************************************************
677 void XGUI_Displayer::setSelectionColor(const std::vector<int>& theColor)
679 Handle(AIS_InteractiveContext) aContext = AISContext();
680 if (!aContext.IsNull()) {
681 Quantity_Color aQColor(theColor[0] / 255.,
683 theColor[2] / 255., Quantity_TOC_RGB);
684 aContext->SelectionStyle()->SetColor(aQColor);
685 aContext->SelectionStyle()->PointAspect()->SetColor(aQColor);
686 aContext->SelectionStyle()->LineAspect()->SetColor(aQColor);
687 aContext->HighlightStyle(Prs3d_TypeOfHighlight_LocalSelected)->SetColor(aQColor);
692 //**************************************************************
693 std::vector<int> XGUI_Displayer::selectionColor() const
695 std::vector<int> aColor;
696 Handle(AIS_InteractiveContext) aContext = AISContext();
697 if (!aContext.IsNull()) {
698 Quantity_Color aQColor = aContext->SelectionStyle()->Color();
699 aColor.push_back((int)(aQColor.Red() * 255));
700 aColor.push_back((int)(aQColor.Green() * 255));
701 aColor.push_back((int)(aQColor.Blue() * 255));
707 //**************************************************************
708 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
710 Handle(AIS_InteractiveContext) aContext = AISContext();
711 if (!aContext.IsNull() && myAndFilter.IsNull()) {
712 myAndFilter = new SelectMgr_AndFilter();
713 aContext->AddFilter(myAndFilter);
718 //**************************************************************
719 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
720 const Standard_Integer theDisplayMode, bool theUpdateViewer)
722 bool aDisplayed = false;
723 Handle(AIS_InteractiveContext) aContext = AISContext();
724 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
725 if (!aContext.IsNull() && !anAISIO.IsNull()) {
726 aContext->Display(anAISIO, theDisplayMode, 0, false/*update viewer*/, AIS_DS_Displayed);
728 if (getCallBack()) getCallBack()->Display(anAISIO);
731 aContext->Deactivate(anAISIO);
733 if (getCallBack()) getCallBack()->Deactivate(anAISIO);
735 aContext->Load(anAISIO);
737 if (getCallBack()) getCallBack()->Load(anAISIO);
739 if (toActivateInSelectionModes)
740 myWorkshop->selectionActivate()->activateOnDisplay(anAISIO, theUpdateViewer);
748 //**************************************************************
749 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
751 bool aErased = false;
752 Handle(AIS_InteractiveContext) aContext = AISContext();
753 if (!aContext.IsNull()) {
754 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
755 if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
756 aContext->Remove(anAISIO, false/*update viewer*/);
758 if (getCallBack()) getCallBack()->Remove(anAISIO);
763 if (aErased && theUpdateViewer)
768 //**************************************************************
769 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
771 if (theMode == NoMode)
774 Handle(AIS_InteractiveContext) aContext = AISContext();
775 if (aContext.IsNull())
778 AISObjectPtr aAISObj = getAISObject(theObject);
782 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
783 aContext->SetDisplayMode(aAISIO, theMode, false);
784 // Redisplay in order to update new mode because it could be not computed before
789 //**************************************************************
790 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
792 Handle(AIS_InteractiveContext) aContext = AISContext();
793 if (aContext.IsNull())
796 AISObjectPtr aAISObj = getAISObject(theObject);
800 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
801 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
804 //**************************************************************
805 void XGUI_Displayer::deactivateSelectionFilters(const bool theAddFilterOnly)
807 Handle(AIS_InteractiveContext) aContext = AISContext();
808 if (!myAndFilter.IsNull()) {
810 if (!aContext.IsNull()) {
811 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
812 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
813 for (; anIt.More() && !aFound; anIt.Next()) {
814 Handle(SelectMgr_Filter) aFilter = anIt.Value();
815 aFound = aFilter == myAndFilter;
818 aContext->RemoveFilter(myAndFilter);
820 myAndFilter.Nullify();
824 //**************************************************************
825 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
827 Handle(AIS_InteractiveContext) aContext = AISContext();
828 if (aContext.IsNull() || hasSelectionFilter(theFilter))
831 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
832 if (!aCompositeFilter.IsNull()) {
833 aCompositeFilter->Add(theFilter);
834 #ifdef DEBUG_SELECTION_FILTERS
835 int aCount = aCompositeFilter->StoredFilters().Extent();
836 qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
841 //**************************************************************
842 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
844 Handle(AIS_InteractiveContext) aContext = AISContext();
845 if (aContext.IsNull())
848 Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
849 if (!aCompositeFilter.IsNull() && aCompositeFilter->IsIn(theFilter)) {
850 aCompositeFilter->Remove(theFilter);
851 #ifdef DEBUG_SELECTION_FILTERS
852 int aCount = aCompositeFilter->StoredFilters().Extent();
853 qDebug(QString("removeSelectionFilter: filters.count() = %1")
854 .arg(aCount).toStdString().c_str());
859 //**************************************************************
860 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
862 bool aFilterFound = false;
864 Handle(AIS_InteractiveContext) aContext = AISContext();
865 if (aContext.IsNull())
867 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
868 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
869 for (; aIt.More() && !aFilterFound; aIt.Next()) {
870 if (theFilter.get() == aIt.Value().get())
873 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
874 if (!aCompositeFilter.IsNull()) {
875 const SelectMgr_ListOfFilter& aStoredFilters = aCompositeFilter->StoredFilters();
876 for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
877 if (theFilter.get() == aIt.Value().get())
884 //**************************************************************
885 void XGUI_Displayer::removeFilters()
887 Handle(AIS_InteractiveContext) aContext = AISContext();
888 if (aContext.IsNull())
891 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
892 if (!aCompositeFilter.IsNull())
893 aCompositeFilter->Clear();
896 //**************************************************************
897 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
900 QObjectPtrList aDispList = myResult2AISObjectMap.objects();
902 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
904 foreach(ObjectPtr aObj, aDispList) {
905 if (!theList.contains(aObj))
908 foreach(ObjectPtr aObj, theList) {
909 if (!isVisible(aObj))
910 display(aObj, false);
915 //**************************************************************
916 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
918 if (!isVisible(theObject))
921 AISObjectPtr aAISObj = getAISObject(theObject);
922 if (aAISObj.get() == NULL)
925 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
926 return ::canBeShaded(anAIS, myWorkshop->module());
929 //**************************************************************
930 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject,
931 const QColor& theColor,
932 bool theUpdateViewer)
934 if (!isVisible(theObject))
937 AISObjectPtr anAISObj = getAISObject(theObject);
939 anAISObj->getColor(aR, aG, aB);
940 anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
941 myWorkshop->updateGroupsText();
944 return QColor(aR, aG, aB);
947 //**************************************************************
948 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
951 myResult2AISObjectMap.add(theObject, theAIS);
953 myResult2AISObjectMap[theObject] = theAIS;
957 std::ostringstream aPtrStr;
958 aPtrStr << theObject.get();
959 qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
960 qDebug(getResult2AISObjectMapInfo().c_str());
965 //**************************************************************
966 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
968 QStringList aContent;
970 foreach(ObjectPtr aObj, myResult2AISObjectMap.objects()) {
971 AISObjectPtr aAISObj = myResult2AISObjectMap.value(aObj);
973 foreach(ObjectPtr aObj, myResult2AISObjectMap.keys()) {
974 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
976 std::ostringstream aPtrStr;
977 aPtrStr << "aObj = " << aObj.get() << ":";
978 aPtrStr << "anAIS = " << aAISObj.get() << ":";
979 aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
981 aContent.append(aPtrStr.str().c_str());
983 return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
984 arg(aContent.join("\n")).toStdString().c_str();
988 //**************************************************************
989 void XGUI_Displayer::getPresentations(const ObjectPtr& theObject,
990 NCollection_Map<Handle(AIS_InteractiveObject)>& thePresentations)
992 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
994 AISObjectPtr aAISObj = getAISObject(aResult);
995 if (aAISObj.get() == NULL) {
996 // if result is a result of a composite feature, it is visualized by visualization of
997 // composite children, so we should get one of this presentations
998 ResultBodyPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
999 if (aCompSolid.get() && aCompSolid->numberOfSubs() > 0) {
1000 aAISObj = getAISObject(aCompSolid->subResult(0));
1003 if (aAISObj.get() != NULL) {
1004 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1005 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1006 thePresentations.Add(anAIS);
1010 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
1011 // find presentation of the feature
1012 AISObjectPtr aAISObj = getAISObject(aFeature);
1013 if (aAISObj.get() != NULL) {
1014 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1015 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1016 thePresentations.Add(anAIS);
1018 // find presentations of the feature results
1019 std::list<ResultPtr> aResults;
1020 ModelAPI_Tools::allResults(aFeature, aResults);
1021 std::list<ResultPtr>::const_iterator anIt = aResults.begin(), aLast = aResults.end();
1022 for (; anIt != aLast; ++anIt) {
1023 AISObjectPtr aCurAISObj = getAISObject(*anIt);
1024 if (aCurAISObj.get() != NULL) {
1025 Handle(AIS_InteractiveObject) anAIS = aCurAISObj->impl<Handle(AIS_InteractiveObject)>();
1026 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1027 thePresentations.Add(anAIS);
1033 //**************************************************************
1034 void XGUI_Displayer::displayTrihedron(bool theToDisplay) const
1036 Handle(AIS_InteractiveContext) aContext = AISContext();
1037 if (aContext.IsNull())
1040 Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
1042 XGUI_SelectionActivate* aSelectionActive = selectionActivate();
1044 if (!aContext->IsDisplayed(aTrihedron))
1045 aContext->Display(aTrihedron,
1047 -1 /* selection mode */,
1048 Standard_True /* update viewer*/,
1049 AIS_DS_Displayed /* xdisplay status */);
1051 if (getCallBack()) getCallBack()->Display(aTrihedron);
1054 if (!aSelectionActive->isTrihedronActive())
1055 aSelectionActive->deactivateTrihedron(false);
1057 aSelectionActive->activate(aTrihedron, false);
1059 aSelectionActive->deactivateTrihedron(false);
1061 aContext->Erase(aTrihedron, Standard_True);
1063 if (getCallBack()) getCallBack()->Remove(aTrihedron);
1068 //**************************************************************
1069 void XGUI_Displayer::AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
1070 const NCollection_DataMap<TopoDS_Shape,
1071 NCollection_Map<Handle(AIS_InteractiveObject)>>& theShapesToBeSelected)
1073 NCollection_Map<Handle(AIS_InteractiveObject)> aCompsolidPresentations;
1074 NCollection_Map<Handle(AIS_InteractiveObject)> aSelectedPresentations;
1076 NCollection_List<Handle(SelectBasics_EntityOwner)> anActiveOwners;
1077 theContext->MainSelector()->ActiveOwners(anActiveOwners);
1078 NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (anActiveOwners);
1079 Handle(SelectMgr_EntityOwner) anOwner;
1081 /// It is very important to check that the owner is processed only once and has a map of
1082 /// processed owners because SetSelected works as a switch.
1083 /// If count of calls setSelectec is even, the object stays in the previous state
1084 /// (selected, deselected)
1085 /// OCCT: to write about the problem that active owners method returns one owner several times
1086 QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
1087 for (; anOwnersIt.More(); anOwnersIt.Next()) {
1088 anOwner = anOwnersIt.Value();
1089 if (aSelectedIds.contains((size_t)anOwner.get()))
1091 aSelectedIds.append((size_t)anOwner.get());
1093 Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
1094 if (!BROwnr.IsNull() && BROwnr->HasShape()) {
1095 const TopoDS_Shape& aShape = BROwnr->Shape();
1097 Handle(ModuleBase_ResultPrs) aResPrs =
1098 Handle(ModuleBase_ResultPrs)::DownCast(BROwnr->Selectable());
1099 TopoDS_Shape aRealShape;
1100 if (!aResPrs.IsNull()) {
1101 aRealShape = aResPrs->originalShape();
1104 if (aShape.IsNull())
1107 Handle(ModuleBase_BRepOwner) aCustomOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
1109 NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)> >
1110 ::Iterator aShapeIt(theShapesToBeSelected);
1111 for (; aShapeIt.More(); aShapeIt.Next()) {
1112 const TopoDS_Shape& aParameterShape = aShapeIt.Key();
1113 // In case of compound we cannot rely on simple comparison method.
1114 // If the compound is generated by Group feature then this compound is alwais new.
1115 // So, we have to compare content of these compounds
1117 // isSame should be used here as it does not check orientation of shapes
1118 // despite on isEqual of shapes or IsBound for shape in QMap. Orientation is
1119 // different for Edges shapes in model shape and owner even if this is the same shape
1120 bool isSame = ModuleBase_Tools::isSameShape(aParameterShape, aShape);
1122 // In case of using HideFaces panel we can have instead of an original shape
1123 // a compaund of faces which represent original shape with hidden faces.
1124 // So, we have to compare the parameter with original shape
1125 if (!aRealShape.IsNull())
1126 isSame = ModuleBase_Tools::isSameShape(aParameterShape, aRealShape);
1129 Handle(AIS_InteractiveObject) anOwnerPresentation =
1130 Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
1131 NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations =
1132 theShapesToBeSelected.Find(aParameterShape);
1133 if (aPresentations.Contains(anOwnerPresentation)) {
1134 theContext->AddOrRemoveSelected(anOwner, Standard_False);
1135 anOwner->SetSelected(Standard_True);
1136 // collect selected presentations to do not select them if compsolid is selected
1137 if (!aSelectedPresentations.Contains(anOwnerPresentation))
1138 aSelectedPresentations.Add(anOwnerPresentation);
1141 else if (!aCustomOwner.IsNull()) { // CompSolid processing #2219
1142 // shape of owner is compound, but shape to be selected is compsolid, so
1143 // we need to compare shape to AIS presentation of owner(rule of the owner creation)
1144 Handle(AIS_Shape) anOwnerPresentation =
1145 Handle(AIS_Shape)::DownCast(anOwner->Selectable());
1146 const TopoDS_Shape& aPresentationShape = anOwnerPresentation->Shape();
1147 if (aParameterShape.IsSame(aPresentationShape) &&
1148 !aCompsolidPresentations.Contains(anOwnerPresentation))
1149 aCompsolidPresentations.Add(anOwnerPresentation);
1154 // select CompSolid presentations if their owners was not selected yet
1155 NCollection_Map<Handle(AIS_InteractiveObject)>::Iterator anIt (aCompsolidPresentations);
1156 for (; anIt.More(); anIt.Next()) {
1157 if (aSelectedPresentations.Contains(anIt.Value()))
1159 theContext->AddOrRemoveSelected(anIt.Value(), Standard_False);
1163 //**************************************************************
1164 XGUI_SelectionActivate* XGUI_Displayer::selectionActivate() const
1166 return myWorkshop->selectionActivate();
1169 //**************************************************************
1170 GeomPlanePtr XGUI_Displayer::getScreenPlane() const
1172 GeomPlanePtr aResult;
1173 Handle(AIS_InteractiveContext) aContext = AISContext();
1174 if (!aContext.IsNull()) {
1175 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1176 Handle(V3d_View) aView = aViewer->ActiveViews().First();
1177 if (!aView.IsNull()) {
1178 double aEyeX, aEyeY, aEyeZ;
1179 aView->Eye(aEyeX, aEyeY, aEyeZ);
1181 double aProjX, aProjY, aProjZ;
1182 aView->Proj(aProjX, aProjY, aProjZ);
1184 GeomPointPtr aPnt = GeomPointPtr(new GeomAPI_Pnt(aEyeX, aEyeY, aEyeZ));
1185 GeomDirPtr aDir = GeomDirPtr(new GeomAPI_Dir(aProjX, aProjY, aProjZ));
1187 aResult = GeomPlanePtr(new GeomAPI_Pln(aPnt, aDir));
1193 double XGUI_Displayer::getViewScale() const
1195 Handle(AIS_InteractiveContext) aContext = AISContext();
1196 if (!aContext.IsNull()) {
1197 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1198 Handle(V3d_View) aView = aViewer->ActiveViews().First();
1199 return aView->Camera()->Scale();