1 // Copyright (C) 2014-2022 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 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>
40 #include <ModuleBase_BRepOwner.h>
41 #include <ModuleBase_IModule.h>
42 #include <ModuleBase_Preferences.h>
43 #include <ModuleBase_Tools.h>
44 #include <ModuleBase_ViewerPrs.h>
45 #include <ModuleBase_IViewer.h>
47 #include <GeomAPI_Shape.h>
48 #include <GeomAPI_IPresentable.h>
49 #include <GeomAPI_ICustomPrs.h>
50 #include <GeomAPI_Pnt.h>
51 #include <GeomAPI_IScreenParams.h>
53 #include <SUIT_ResourceMgr.h>
55 #include <AIS_InteractiveContext.hxx>
56 #include <AIS_ListOfInteractive.hxx>
57 #include <AIS_ListIteratorOfListOfInteractive.hxx>
58 #include <AIS_Shape.hxx>
59 #include <AIS_Trihedron.hxx>
60 #ifdef BEFORE_TRIHEDRON_PATCH
61 #include <AIS_Axis.hxx>
62 #include <AIS_Plane.hxx>
63 #include <AIS_Point.hxx>
65 #include <AIS_Selection.hxx>
66 #include <Prs3d_Drawer.hxx>
67 #include <Prs3d_IsoAspect.hxx>
68 #include <PrsDim_Dimension.hxx>
69 #include <SelectMgr_ListOfFilter.hxx>
70 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
71 #include <SelectMgr_SelectionManager.hxx>
72 #include <TColStd_ListIteratorOfListOfInteger.hxx>
73 #include <BRepMesh_IncrementalMesh.hxx>
75 #include <StdSelect_ViewerSelector3d.hxx>
77 #include <TColStd_MapOfTransient.hxx>
78 #include <TColStd_MapIteratorOfMapOfTransient.hxx>
79 #include <Standard_Version.hxx>
82 #include <inspector/VInspectorAPI_CallBack.hxx>
85 #include <Events_Loop.h>
86 #include <ModelAPI_Events.h>
87 #include <Config_PropManager.h>
92 #pragma warning(disable: 4702)
95 /// defines the local context mouse selection sensitivity
96 const int MOUSE_SENSITIVITY_IN_PIXEL = 10;
98 //#define DEBUG_DISPLAY
99 //#define DEBUG_FEATURE_REDISPLAY
100 //#define DEBUG_SELECTION_FILTERS
102 //#define DEBUG_OCCT_SHAPE_SELECTION
104 #define CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
106 //#define DEBUG_VIEWER_BLOCKED_COUNT
108 //**************************************************************
109 void displayedObjects(const Handle(AIS_InteractiveContext)& theAIS, AIS_ListOfInteractive& theList)
111 // Get from null point
112 #if OCC_VERSION_HEX < 0x070400
113 theAIS->DisplayedObjects(theList, true);
115 theAIS->DisplayedObjects(theList);
119 QString qIntListInfo(const QIntList& theValues, const QString& theSeparator = QString(", "))
122 QIntList::const_iterator anIt = theValues.begin(), aLast = theValues.end();
123 for (; anIt != aLast; anIt++) {
124 anInfo.append(QString::number(*anIt));
126 return anInfo.join(theSeparator);
129 //**************************************************************
130 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
131 : myWorkshop(theWorkshop),
132 myViewerBlockedRecursiveCount(0),
136 BRepMesh_IncrementalMesh::SetParallelDefault(Standard_True);
139 //**************************************************************
140 XGUI_Displayer::~XGUI_Displayer()
144 //**************************************************************
145 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
147 return myResult2AISObjectMap.contains(theObject);
150 //**************************************************************
151 bool XGUI_Displayer::display(ObjectPtr theObject, bool theUpdateViewer)
153 bool aDisplayed = false;
154 if (isVisible(theObject)) {
155 aDisplayed = redisplay(theObject, theUpdateViewer);
158 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
159 bool isShading = false;
160 if (aPrs.get() != NULL) {
161 GeomScreenParamsPtr aScreen = std::dynamic_pointer_cast<GeomAPI_IScreenParams>(theObject);
163 aScreen->setScreenPlane(getScreenPlane());
164 aScreen->setViewScale(getViewScale());
166 anAIS = aPrs->getAISObject(anAIS);
168 myWorkshop->module()->customizePresentation(theObject, anAIS);
170 anAIS = myWorkshop->module()->createPresentation(theObject);
174 aDisplayed = display(theObject, anAIS, isShading, theUpdateViewer);
180 //**************************************************************
181 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS, ModuleBase_IModule* theModule)
183 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
184 if (!aShapePrs.IsNull()) {
185 TopoDS_Shape aShape = aShapePrs->Shape();
188 TopAbs_ShapeEnum aType = aShape.ShapeType();
189 if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
192 // Check that the presentation is not a sketch
193 return theModule->canBeShaded(theAIS);
199 //**************************************************************
200 bool XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
201 bool isShading, bool theUpdateViewer)
203 bool aDisplayed = false;
205 Handle(AIS_InteractiveContext) aContext = AISContext();
206 if (aContext.IsNull())
209 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
210 if (!anAISIO.IsNull()) {
211 appendResultObject(theObject, theAIS);
213 //bool isCustomized = customizeObject(theObject);
215 int aDispMode = isShading? Shading : Wireframe;
216 anAISIO->SetDisplayMode(aDispMode);
217 aContext->Display(anAISIO, aDispMode, 0, false, AIS_DS_Displayed);
219 if (getCallBack()) getCallBack()->Display(anAISIO);
223 emit objectDisplayed(theObject, theAIS);
224 selectionActivate()->activate(anAISIO, theUpdateViewer);
226 myWorkshop->updateGroupsText();
233 //**************************************************************
234 bool XGUI_Displayer::erase(ObjectPtr theObject, const bool theUpdateViewer)
236 bool aErased = false;
237 if (!isVisible(theObject))
240 Handle(AIS_InteractiveContext) aContext = AISContext();
241 if (aContext.IsNull())
244 AISObjectPtr anObject = myResult2AISObjectMap.value(theObject);
246 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
247 if (!anAIS.IsNull()) {
248 emit beforeObjectErase(theObject, anObject);
249 aContext->Remove(anAIS, false/*update viewer*/);
251 if (getCallBack()) getCallBack()->Remove(anAIS);
256 myResult2AISObjectMap.remove(theObject);
259 std::ostringstream aPtrStr;
260 aPtrStr << theObject.get();
261 qDebug(QString("erase object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
262 qDebug(getResult2AISObjectMapInfo().c_str());
264 myWorkshop->updateGroupsText();
271 //**************************************************************
272 bool XGUI_Displayer::redisplay(ObjectPtr theObject, bool theUpdateViewer)
274 bool aRedisplayed = false;
275 Handle(AIS_InteractiveContext) aContext = AISContext();
276 if (aContext.IsNull())
279 if (!isVisible(theObject))
282 AISObjectPtr aAISObj = getAISObject(theObject);
283 Handle(AIS_InteractiveObject) aAISIO;
285 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
287 GeomScreenParamsPtr aScreen = std::dynamic_pointer_cast<GeomAPI_IScreenParams>(theObject);
289 aScreen->setScreenPlane(getScreenPlane());
290 aScreen->setViewScale(getViewScale());
292 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
294 aRedisplayed = erase(theObject, theUpdateViewer);
297 if (aAIS_Obj != aAISObj) {
298 erase(theObject, theUpdateViewer);
299 appendResultObject(theObject, aAIS_Obj);
301 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
304 aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
307 if (!aAISIO.IsNull()) {
308 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
311 std::vector<int> aColor;
312 ModelAPI_Tools::getColor(aResult, aColor);
313 if (aColor.size() > 0) {
315 aCol(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB);
316 aAISIO->SetColor(aCol);
319 double aDeflection = ModelAPI_Tools::getDeflection(aResult);
320 if ((aDeflection >= 0) && (aDeflection != aAISObj->getDeflection()))
321 aAISObj->setDeflection(aDeflection);
324 double aTransparency = ModelAPI_Tools::getTransparency(aResult);
325 if ((aTransparency >= 0) && (aTransparency != aAISObj->getTransparency()))
326 aAISObj->setTransparency(aTransparency);
329 Handle(ModuleBase_ResultPrs) aResPrs = Handle(ModuleBase_ResultPrs)::DownCast(aAISIO);
330 if (!aResPrs.IsNull())
331 aResPrs->updateIsoLines();
333 //myWorkshop->module()->storeSelection();
335 //#ifdef CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
336 //myWorkshop->selector()->deselectPresentation(aAISIO);
339 if (aContext->IsDisplayed(aAISIO))
340 aContext->Redisplay(aAISIO, false);
342 aContext->Display(aAISIO, false);
345 if (getCallBack()) getCallBack()->Redisplay(aAISIO);
348 //if (aNeedToRestoreSelection)
349 //myWorkshop->module()->restoreSelection();
352 #ifdef DEBUG_FEATURE_REDISPLAY
353 qDebug(" Redisplay happens");
355 myWorkshop->updateGroupsText();
362 //**************************************************************
363 void XGUI_Displayer::redisplayObjects()
365 // redisplay objects visualized in the viewer
366 static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
367 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
368 QObjectPtrList aDisplayed = displayedObjects();
369 QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
370 for (; anIt != aLast; anIt++) {
371 aECreator->sendUpdated(*anIt, EVENT_DISP);
373 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
374 if (aViewer->isColorScaleVisible()) {
375 aViewer->setupColorScale();
376 aViewer->setColorScaleShown(true);
378 Events_Loop::loop()->flush(EVENT_DISP);
381 //**************************************************************
382 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
383 const bool theUpdateViewer)
385 //Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
386 //if (!aTrihedron.IsNull())
387 // deactivateAIS(aTrihedron);
389 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
390 for (; anIt != aLast; anIt++) {
391 selectionActivate()->deactivate(*anIt, false);
393 //VSV It seems that there is no necessity to update viewer on deactivation
394 //if (theUpdateViewer)
398 //**************************************************************
399 bool XGUI_Displayer::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
401 bool aVisible = false;
402 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
403 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
404 if (aPrs.get() || aResult.get()) {
405 aVisible = theDisplayer->isVisible(theObject);
406 // compsolid is not visualized in the viewer,
407 // but should have presentation when all sub solids are
408 // visible. It is useful for highlight presentation where compsolid shape is selectable
409 if (!aVisible && aResult.get() && aResult->groupName() == ModelAPI_ResultBody::group()) {
410 ResultBodyPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
411 if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
412 int aNumberOfSubs = aCompsolidResult->numberOfSubs();
413 bool anAllSubsVisible = aNumberOfSubs > 0;
414 for(int i = 0; i < aNumberOfSubs && anAllSubsVisible; i++) {
415 anAllSubsVisible = theDisplayer->isVisible(aCompsolidResult->subResult(i));
417 aVisible = anAllSubsVisible;
421 // it is possible that feature is presentable and has results, so we should check visibility
422 // of results if presentation is not shown (e.g. Sketch Circle/Arc features)
424 // check if all results of the feature are visible
425 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
426 std::list<ResultPtr> aResults;
427 ModelAPI_Tools::allResults(aFeature, aResults);
428 std::list<ResultPtr>::const_iterator aIt;
429 aVisible = !aResults.empty();
430 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
431 aVisible = aVisible && theDisplayer->isVisible(*aIt);
437 //**************************************************************
438 void XGUI_Displayer::setSelected(const QList<ModuleBase_ViewerPrsPtr>& theValues,
439 bool theUpdateViewer)
441 Handle(AIS_InteractiveContext) aContext = AISContext();
442 if (aContext.IsNull())
444 aContext->UnhilightSelected(false);
445 aContext->ClearSelected(false);
447 if (getCallBack()) getCallBack()->ClearSelected();
449 NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)>>
452 foreach (ModuleBase_ViewerPrsPtr aPrs, theValues) {
453 const GeomShapePtr& aGeomShape = aPrs->shape();
454 if (aGeomShape.get() && !aGeomShape->isNull()) {
455 const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
456 #ifdef DEBUG_OCCT_SHAPE_SELECTION
457 // problem 1: performance
458 // problem 2: IO is not specified, so the first found owner is selected, as a result
459 // it might belong to another result
460 aContext->AddOrRemoveSelected(aShape, false);
462 if (getCallBack()) getCallBack()->AddOrRemoveSelected(aShape);
465 NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations;
466 if (aShapesToBeSelected.IsBound(aShape))
467 aPresentations = aShapesToBeSelected.Find(aShape);
468 ObjectPtr anObject = aPrs->object();
469 getPresentations(anObject, aPresentations);
471 aShapesToBeSelected.Bind(aShape, aPresentations);
474 ObjectPtr anObject = aPrs->object();
475 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
476 if (aResult.get() && isVisible(aResult)) {
477 AISObjectPtr anObj = myResult2AISObjectMap.value(aResult);
478 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
479 if (!anAIS.IsNull()) {
480 // The methods are replaced in order to provide multi-selection, e.g. restore selection
481 // by activating multi selector widget. It also gives an advantage that the multi
482 // selection in OB gives multi-selection in the viewer
483 //aContext->SetSelected(anAIS, false);
484 // The selection in the context was cleared, so the method sets the objects are selected
485 aContext->AddOrRemoveSelected(anAIS, false);
487 if (getCallBack()) getCallBack()->AddOrRemoveSelected(anAIS);
493 if (!aShapesToBeSelected.IsEmpty())
494 XGUI_Displayer::AddOrRemoveSelectedShapes(aContext, aShapesToBeSelected);
500 //**************************************************************
501 void XGUI_Displayer::clearSelected(const bool theUpdateViewer)
503 Handle(AIS_InteractiveContext) aContext = AISContext();
504 if (!aContext.IsNull()) {
505 aContext->UnhilightSelected(false);//UnhilightCurrents(false);
506 aContext->ClearSelected(theUpdateViewer);
508 if (getCallBack()) getCallBack()->ClearSelected();
513 //**************************************************************
514 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
516 bool aErased = false;
517 Handle(AIS_InteractiveContext) aContext = AISContext();
518 if (!aContext.IsNull()) {
520 foreach(ObjectPtr aObj, myResult2AISObjectMap.objects()) {
521 AISObjectPtr aAISObj = myResult2AISObjectMap.value(aObj);
523 foreach(ObjectPtr aObj, myResult2AISObjectMap.keys()) {
524 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
527 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
528 if (!anIO.IsNull()) {
529 emit beforeObjectErase(aObj, aAISObj);
530 aContext->Remove(anIO, false/*update viewer*/);
532 if (getCallBack()) getCallBack()->Remove(anIO);
537 myWorkshop->updateGroupsText();
541 myResult2AISObjectMap.clear();
544 qDebug(getResult2AISObjectMapInfo().c_str());
550 //**************************************************************
551 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
554 return myResult2AISObjectMap.value(theObject);
557 if (myResult2AISObjectMap.contains(theObject))
558 anIO = myResult2AISObjectMap[theObject];
563 //**************************************************************
564 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
566 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
567 return getObject(aRefAIS);
570 //**************************************************************
571 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
574 ObjectPtr anObject = myResult2AISObjectMap.value(theIO);
577 ResultToAISMap::const_iterator aMapIter = myResult2AISObjectMap.cbegin();
578 for (; aMapIter != myResult2AISObjectMap.cend(); aMapIter++) {
579 const AISObjectPtr& aAIS = aMapIter.value();
580 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
582 anObject = aMapIter.key();
587 if (!anObject.get()) {
588 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
589 if (!theIO.IsNull()) {
590 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
592 if (myWorkshop->module())
593 anObject = myWorkshop->module()->findPresentedObject(anAISObj);
598 //**************************************************************
599 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
601 bool aWasEnabled = isUpdateEnabled();
603 myViewerBlockedRecursiveCount--;
605 myViewerBlockedRecursiveCount++;
607 #ifdef DEBUG_VIEWER_BLOCKED_COUNT
608 std::cout << "myViewerBlockedRecursiveCount = " << myViewerBlockedRecursiveCount << std::endl;
611 if (myNeedUpdate && isUpdateEnabled()) {
613 myNeedUpdate = false;
618 //**************************************************************
619 void XGUI_Displayer::updateViewer() const
621 Handle(AIS_InteractiveContext) aContext = AISContext();
623 #ifdef DEBUG_VIEWER_BLOCKED_COUNT
624 std::cout << "updateViewer: " << (myViewerBlockedRecursiveCount == 0 ? " done" : " later")
628 if (!aContext.IsNull() && isUpdateEnabled()) {
629 //myWorkshop->viewer()->Zfitall();
630 aContext->UpdateCurrentViewer();
636 //**************************************************************
637 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
639 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
640 if (!aContext.IsNull() && (myContextId != aContext.get())) {
641 myContextId = aContext.get();
642 if (!myWorkshop->selectionActivate()->isTrihedronActive())
643 selectionActivate()->deactivateTrihedron(true);
645 // Commented out according to discussion in bug #2825
646 ModuleBase_IViewer::DefaultHighlightDrawer = aContext->HighlightStyle();
648 Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
649 if (!aTrihedron.IsNull())
650 ModuleBase_Tools::setPointBallHighlighting(aTrihedron.get());
655 //**************************************************************
656 void XGUI_Displayer::setSelectionColor(const std::vector<int>& theColor)
658 Handle(AIS_InteractiveContext) aContext = AISContext();
659 if (!aContext.IsNull()) {
660 Quantity_Color aQColor(theColor[0] / 255.,
662 theColor[2] / 255., Quantity_TOC_RGB);
663 aContext->SelectionStyle()->SetColor(aQColor);
664 aContext->SelectionStyle()->PointAspect()->SetColor(aQColor);
665 aContext->SelectionStyle()->LineAspect()->SetColor(aQColor);
666 aContext->HighlightStyle(Prs3d_TypeOfHighlight_LocalSelected)->SetColor(aQColor);
671 //**************************************************************
672 std::vector<int> XGUI_Displayer::selectionColor() const
674 std::vector<int> aColor;
675 Handle(AIS_InteractiveContext) aContext = AISContext();
676 if (!aContext.IsNull()) {
677 Quantity_Color aQColor = aContext->SelectionStyle()->Color();
678 aColor.push_back((int)(aQColor.Red() * 255));
679 aColor.push_back((int)(aQColor.Green() * 255));
680 aColor.push_back((int)(aQColor.Blue() * 255));
686 //**************************************************************
687 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
689 Handle(AIS_InteractiveContext) aContext = AISContext();
690 if (!aContext.IsNull() && myAndFilter.IsNull()) {
691 myAndFilter = new SelectMgr_AndFilter();
692 aContext->AddFilter(myAndFilter);
697 //**************************************************************
698 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
699 const Standard_Integer theDisplayMode, bool theUpdateViewer)
701 bool aDisplayed = false;
702 Handle(AIS_InteractiveContext) aContext = AISContext();
703 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
704 if (!aContext.IsNull() && !anAISIO.IsNull()) {
705 aContext->Display(anAISIO, theDisplayMode, 0, false/*update viewer*/, AIS_DS_Displayed);
707 if (getCallBack()) getCallBack()->Display(anAISIO);
710 aContext->Deactivate(anAISIO);
712 if (getCallBack()) getCallBack()->Deactivate(anAISIO);
714 aContext->Load(anAISIO);
716 if (getCallBack()) getCallBack()->Load(anAISIO);
718 if (toActivateInSelectionModes)
719 myWorkshop->selectionActivate()->activateOnDisplay(anAISIO, theUpdateViewer);
727 //**************************************************************
728 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
730 bool aErased = false;
731 Handle(AIS_InteractiveContext) aContext = AISContext();
732 if (!aContext.IsNull()) {
733 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
734 if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
735 aContext->Remove(anAISIO, false/*update viewer*/);
737 if (getCallBack()) getCallBack()->Remove(anAISIO);
742 if (aErased && theUpdateViewer)
747 //**************************************************************
748 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
750 if (theMode == NoMode)
753 Handle(AIS_InteractiveContext) aContext = AISContext();
754 if (aContext.IsNull())
757 AISObjectPtr aAISObj = getAISObject(theObject);
761 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
762 aContext->SetDisplayMode(aAISIO, theMode, false);
763 // Redisplay in order to update new mode because it could be not computed before
768 //**************************************************************
769 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
771 Handle(AIS_InteractiveContext) aContext = AISContext();
772 if (aContext.IsNull())
775 AISObjectPtr aAISObj = getAISObject(theObject);
779 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
780 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
783 //**************************************************************
784 void XGUI_Displayer::deactivateSelectionFilters(const bool theAddFilterOnly)
786 Handle(AIS_InteractiveContext) aContext = AISContext();
787 if (!myAndFilter.IsNull()) {
789 if (!aContext.IsNull()) {
790 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
791 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
792 for (; anIt.More() && !aFound; anIt.Next()) {
793 Handle(SelectMgr_Filter) aFilter = anIt.Value();
794 aFound = aFilter == myAndFilter;
797 aContext->RemoveFilter(myAndFilter);
799 myAndFilter.Nullify();
803 //**************************************************************
804 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
806 Handle(AIS_InteractiveContext) aContext = AISContext();
807 if (aContext.IsNull() || hasSelectionFilter(theFilter))
810 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
811 if (!aCompositeFilter.IsNull()) {
812 aCompositeFilter->Add(theFilter);
813 #ifdef DEBUG_SELECTION_FILTERS
814 int aCount = aCompositeFilter->StoredFilters().Extent();
815 qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
820 //**************************************************************
821 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
823 Handle(AIS_InteractiveContext) aContext = AISContext();
824 if (aContext.IsNull())
827 Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
828 if (!aCompositeFilter.IsNull() && aCompositeFilter->IsIn(theFilter)) {
829 aCompositeFilter->Remove(theFilter);
830 #ifdef DEBUG_SELECTION_FILTERS
831 int aCount = aCompositeFilter->StoredFilters().Extent();
832 qDebug(QString("removeSelectionFilter: filters.count() = %1")
833 .arg(aCount).toStdString().c_str());
838 //**************************************************************
839 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
841 bool aFilterFound = false;
843 Handle(AIS_InteractiveContext) aContext = AISContext();
844 if (aContext.IsNull())
846 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
847 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
848 for (; aIt.More() && !aFilterFound; aIt.Next()) {
849 if (theFilter.get() == aIt.Value().get())
852 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
853 if (!aCompositeFilter.IsNull()) {
854 const SelectMgr_ListOfFilter& aStoredFilters = aCompositeFilter->StoredFilters();
855 for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
856 if (theFilter.get() == aIt.Value().get())
863 //**************************************************************
864 void XGUI_Displayer::removeFilters()
866 Handle(AIS_InteractiveContext) aContext = AISContext();
867 if (aContext.IsNull())
870 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
871 if (!aCompositeFilter.IsNull())
872 aCompositeFilter->Clear();
875 //**************************************************************
876 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
879 QObjectPtrList aDispList = myResult2AISObjectMap.objects();
881 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
883 foreach(ObjectPtr aObj, aDispList) {
884 if (!theList.contains(aObj))
887 foreach(ObjectPtr aObj, theList) {
888 if (!isVisible(aObj))
889 display(aObj, false);
894 //**************************************************************
895 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
897 if (!isVisible(theObject))
900 AISObjectPtr aAISObj = getAISObject(theObject);
901 if (aAISObj.get() == NULL)
904 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
905 return ::canBeShaded(anAIS, myWorkshop->module());
908 //**************************************************************
909 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject,
910 const QColor& theColor,
911 bool theUpdateViewer)
913 if (!isVisible(theObject))
916 AISObjectPtr anAISObj = getAISObject(theObject);
918 anAISObj->getColor(aR, aG, aB);
919 anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
920 myWorkshop->updateGroupsText();
923 return QColor(aR, aG, aB);
926 //**************************************************************
927 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
930 myResult2AISObjectMap.add(theObject, theAIS);
932 myResult2AISObjectMap[theObject] = theAIS;
936 std::ostringstream aPtrStr;
937 aPtrStr << theObject.get();
938 qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
939 qDebug(getResult2AISObjectMapInfo().c_str());
944 //**************************************************************
945 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
947 QStringList aContent;
949 foreach(ObjectPtr aObj, myResult2AISObjectMap.objects()) {
950 AISObjectPtr aAISObj = myResult2AISObjectMap.value(aObj);
952 foreach(ObjectPtr aObj, myResult2AISObjectMap.keys()) {
953 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
955 std::ostringstream aPtrStr;
956 aPtrStr << "aObj = " << aObj.get() << ":";
957 aPtrStr << "anAIS = " << aAISObj.get() << ":";
958 aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
960 aContent.append(aPtrStr.str().c_str());
962 return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
963 arg(aContent.join("\n")).toStdString().c_str();
967 //**************************************************************
968 void XGUI_Displayer::getPresentations(const ObjectPtr& theObject,
969 NCollection_Map<Handle(AIS_InteractiveObject)>& thePresentations)
971 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
973 AISObjectPtr aAISObj = getAISObject(aResult);
974 if (aAISObj.get() == NULL) {
975 // if result is a result of a composite feature, it is visualized by visualization of
976 // composite children, so we should get one of this presentations
977 ResultBodyPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
978 if (aCompSolid.get() && aCompSolid->numberOfSubs() > 0) {
979 aAISObj = getAISObject(aCompSolid->subResult(0));
982 if (aAISObj.get() != NULL) {
983 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
984 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
985 thePresentations.Add(anAIS);
989 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
990 // find presentation of the feature
991 AISObjectPtr aAISObj = getAISObject(aFeature);
992 if (aAISObj.get() != NULL) {
993 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
994 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
995 thePresentations.Add(anAIS);
997 // find presentations of the feature results
998 std::list<ResultPtr> aResults;
999 ModelAPI_Tools::allResults(aFeature, aResults);
1000 std::list<ResultPtr>::const_iterator anIt = aResults.begin(), aLast = aResults.end();
1001 for (; anIt != aLast; ++anIt) {
1002 AISObjectPtr aCurAISObj = getAISObject(*anIt);
1003 if (aCurAISObj.get() != NULL) {
1004 Handle(AIS_InteractiveObject) anAIS = aCurAISObj->impl<Handle(AIS_InteractiveObject)>();
1005 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1006 thePresentations.Add(anAIS);
1012 //**************************************************************
1013 void XGUI_Displayer::displayTrihedron(bool theToDisplay) const
1015 Handle(AIS_InteractiveContext) aContext = AISContext();
1016 if (aContext.IsNull())
1019 Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
1021 XGUI_SelectionActivate* aSelectionActive = selectionActivate();
1023 if (!aContext->IsDisplayed(aTrihedron))
1024 aContext->Display(aTrihedron,
1026 -1 /* selection mode */,
1027 Standard_True /* update viewer*/,
1028 AIS_DS_Displayed /* xdisplay status */);
1030 if (getCallBack()) getCallBack()->Display(aTrihedron);
1033 if (!aSelectionActive->isTrihedronActive())
1034 aSelectionActive->deactivateTrihedron(false);
1036 aSelectionActive->activate(aTrihedron, false);
1038 aSelectionActive->deactivateTrihedron(false);
1040 aContext->Erase(aTrihedron, Standard_True);
1042 if (getCallBack()) getCallBack()->Remove(aTrihedron);
1047 //**************************************************************
1048 void XGUI_Displayer::AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
1049 const NCollection_DataMap<TopoDS_Shape,
1050 NCollection_Map<Handle(AIS_InteractiveObject)>>& theShapesToBeSelected)
1052 NCollection_Map<Handle(AIS_InteractiveObject)> aCompsolidPresentations;
1053 NCollection_Map<Handle(AIS_InteractiveObject)> aSelectedPresentations;
1055 NCollection_List<Handle(SelectBasics_EntityOwner)> anActiveOwners;
1056 theContext->MainSelector()->ActiveOwners(anActiveOwners);
1057 NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (anActiveOwners);
1058 Handle(SelectMgr_EntityOwner) anOwner;
1060 /// It is very important to check that the owner is processed only once and has a map of
1061 /// processed owners because SetSelected works as a switch.
1062 /// If count of calls setSelectec is even, the object stays in the previous state
1063 /// (selected, deselected)
1064 /// OCCT: to write about the problem that active owners method returns one owner several times
1065 QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
1066 for (; anOwnersIt.More(); anOwnersIt.Next()) {
1067 anOwner = anOwnersIt.Value();
1068 if (aSelectedIds.contains((size_t)anOwner.get()))
1070 aSelectedIds.append((size_t)anOwner.get());
1072 Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
1073 if (!BROwnr.IsNull() && BROwnr->HasShape()) {
1074 const TopoDS_Shape& aShape = BROwnr->Shape();
1076 Handle(ModuleBase_ResultPrs) aResPrs =
1077 Handle(ModuleBase_ResultPrs)::DownCast(BROwnr->Selectable());
1078 TopoDS_Shape aRealShape;
1079 if (!aResPrs.IsNull()) {
1080 aRealShape = aResPrs->originalShape();
1083 if (aShape.IsNull())
1086 Handle(ModuleBase_BRepOwner) aCustomOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
1088 NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)> >
1089 ::Iterator aShapeIt(theShapesToBeSelected);
1090 for (; aShapeIt.More(); aShapeIt.Next()) {
1091 const TopoDS_Shape& aParameterShape = aShapeIt.Key();
1092 // In case of compound we cannot rely on simple comparison method.
1093 // If the compound is generated by Group feature then this compound is alwais new.
1094 // So, we have to compare content of these compounds
1096 // isSame should be used here as it does not check orientation of shapes
1097 // despite on isEqual of shapes or IsBound for shape in QMap. Orientation is
1098 // different for Edges shapes in model shape and owner even if this is the same shape
1099 bool isSame = ModuleBase_Tools::isSameShape(aParameterShape, aShape);
1101 // In case of using HideFaces panel we can have instead of an original shape
1102 // a compaund of faces which represent original shape with hidden faces.
1103 // So, we have to compare the parameter with original shape
1104 if (!aRealShape.IsNull())
1105 isSame = ModuleBase_Tools::isSameShape(aParameterShape, aRealShape);
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(aPresentationShape) &&
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 = aViewer->ActiveViews().First();
1156 if (!aView.IsNull()) {
1157 double aEyeX, aEyeY, aEyeZ;
1158 aView->Eye(aEyeX, aEyeY, aEyeZ);
1160 double aProjX, aProjY, aProjZ;
1161 aView->Proj(aProjX, aProjY, aProjZ);
1163 GeomPointPtr aPnt = GeomPointPtr(new GeomAPI_Pnt(aEyeX, aEyeY, aEyeZ));
1164 GeomDirPtr aDir = GeomDirPtr(new GeomAPI_Dir(aProjX, aProjY, aProjZ));
1166 aResult = GeomPlanePtr(new GeomAPI_Pln(aPnt, aDir));
1172 double XGUI_Displayer::getViewScale() const
1174 Handle(AIS_InteractiveContext) aContext = AISContext();
1175 if (!aContext.IsNull()) {
1176 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1177 Handle(V3d_View) aView = aViewer->ActiveViews().First();
1178 return aView->Camera()->Scale();