1 // Copyright (C) 2014-2021 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>
39 #include <ModelAPI_ResultConstruction.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 //#define DEBUG_DISPLAY
100 //#define DEBUG_FEATURE_REDISPLAY
101 //#define DEBUG_SELECTION_FILTERS
103 //#define DEBUG_OCCT_SHAPE_SELECTION
105 #define CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
107 //#define DEBUG_VIEWER_BLOCKED_COUNT
109 //**************************************************************
110 void displayedObjects(const Handle(AIS_InteractiveContext)& theAIS, AIS_ListOfInteractive& theList)
112 // Get from null point
113 #if OCC_VERSION_HEX < 0x070400
114 theAIS->DisplayedObjects(theList, true);
116 theAIS->DisplayedObjects(theList);
120 QString qIntListInfo(const QIntList& theValues, const QString& theSeparator = QString(", "))
123 QIntList::const_iterator anIt = theValues.begin(), aLast = theValues.end();
124 for (; anIt != aLast; anIt++) {
125 anInfo.append(QString::number(*anIt));
127 return anInfo.join(theSeparator);
130 //**************************************************************
131 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
132 : myWorkshop(theWorkshop),
133 myViewerBlockedRecursiveCount(0),
137 BRepMesh_IncrementalMesh::SetParallelDefault(Standard_True);
140 //**************************************************************
141 XGUI_Displayer::~XGUI_Displayer()
145 //**************************************************************
146 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
148 return myResult2AISObjectMap.contains(theObject);
151 //**************************************************************
152 bool XGUI_Displayer::display(ObjectPtr theObject, bool theUpdateViewer)
154 bool aDisplayed = false;
155 if (isVisible(theObject)) {
156 aDisplayed = redisplay(theObject, theUpdateViewer);
159 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
160 bool isShading = false;
161 if (aPrs.get() != NULL) {
162 GeomScreenParamsPtr aScreen = std::dynamic_pointer_cast<GeomAPI_IScreenParams>(theObject);
164 aScreen->setScreenPlane(getScreenPlane());
165 aScreen->setViewScale(getViewScale());
167 anAIS = aPrs->getAISObject(anAIS);
169 myWorkshop->module()->customizePresentation(theObject, anAIS);
171 anAIS = myWorkshop->module()->createPresentation(theObject);
175 aDisplayed = display(theObject, anAIS, isShading, theUpdateViewer);
181 //**************************************************************
182 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS, ModuleBase_IModule* theModule)
184 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
185 if (!aShapePrs.IsNull()) {
186 TopoDS_Shape aShape = aShapePrs->Shape();
189 TopAbs_ShapeEnum aType = aShape.ShapeType();
190 if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
193 // Check that the presentation is not a sketch
194 return theModule->canBeShaded(theAIS);
200 //**************************************************************
201 bool XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
202 bool isShading, bool theUpdateViewer)
204 bool aDisplayed = false;
206 Handle(AIS_InteractiveContext) aContext = AISContext();
207 if (aContext.IsNull())
210 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
211 if (!anAISIO.IsNull()) {
212 appendResultObject(theObject, theAIS);
214 //bool isCustomized = customizeObject(theObject);
216 int aDispMode = isShading? Shading : Wireframe;
217 anAISIO->SetDisplayMode(aDispMode);
218 aContext->Display(anAISIO, aDispMode, 0, false, AIS_DS_Displayed);
220 if (getCallBack()) getCallBack()->Display(anAISIO);
224 emit objectDisplayed(theObject, theAIS);
225 selectionActivate()->activate(anAISIO, theUpdateViewer);
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());
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");
361 //**************************************************************
362 void XGUI_Displayer::redisplayObjects()
364 // redisplay objects visualized in the viewer
365 static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
366 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
367 QObjectPtrList aDisplayed = displayedObjects();
368 QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
369 for (; anIt != aLast; anIt++) {
370 aECreator->sendUpdated(*anIt, EVENT_DISP);
372 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
373 if (aViewer->isColorScaleVisible()) {
374 aViewer->setupColorScale();
375 aViewer->setColorScaleShown(true);
377 Events_Loop::loop()->flush(EVENT_DISP);
380 //**************************************************************
381 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
382 const bool theUpdateViewer)
384 //Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
385 //if (!aTrihedron.IsNull())
386 // deactivateAIS(aTrihedron);
388 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
389 for (; anIt != aLast; anIt++) {
390 selectionActivate()->deactivate(*anIt, false);
392 //VSV It seems that there is no necessity to update viewer on deactivation
393 //if (theUpdateViewer)
397 //**************************************************************
398 bool XGUI_Displayer::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
400 bool aVisible = false;
401 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
402 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
403 if (aPrs.get() || aResult.get()) {
404 aVisible = theDisplayer->isVisible(theObject);
405 // compsolid is not visualized in the viewer,
406 // but should have presentation when all sub solids are
407 // visible. It is useful for highlight presentation where compsolid shape is selectable
408 if (!aVisible && aResult.get() && aResult->groupName() == ModelAPI_ResultBody::group()) {
409 ResultBodyPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
410 if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
411 int aNumberOfSubs = aCompsolidResult->numberOfSubs();
412 bool anAllSubsVisible = aNumberOfSubs > 0;
413 for(int i = 0; i < aNumberOfSubs && anAllSubsVisible; i++) {
414 anAllSubsVisible = theDisplayer->isVisible(aCompsolidResult->subResult(i));
416 aVisible = anAllSubsVisible;
420 // it is possible that feature is presentable and has results, so we should check visibility
421 // of results if presentation is not shown (e.g. Sketch Circle/Arc features)
423 // check if all results of the feature are visible
424 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
425 std::list<ResultPtr> aResults;
426 ModelAPI_Tools::allResults(aFeature, aResults);
427 std::list<ResultPtr>::const_iterator aIt;
428 aVisible = !aResults.empty();
429 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
430 aVisible = aVisible && theDisplayer->isVisible(*aIt);
436 //**************************************************************
437 void XGUI_Displayer::setSelected(const QList<ModuleBase_ViewerPrsPtr>& theValues,
438 bool theUpdateViewer)
440 Handle(AIS_InteractiveContext) aContext = AISContext();
441 if (aContext.IsNull())
443 aContext->UnhilightSelected(false);
444 aContext->ClearSelected(false);
446 if (getCallBack()) getCallBack()->ClearSelected();
448 NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)>>
451 foreach (ModuleBase_ViewerPrsPtr aPrs, theValues) {
452 const GeomShapePtr& aGeomShape = aPrs->shape();
453 if (aGeomShape.get() && !aGeomShape->isNull()) {
454 const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
455 #ifdef DEBUG_OCCT_SHAPE_SELECTION
456 // problem 1: performance
457 // problem 2: IO is not specified, so the first found owner is selected, as a result
458 // it might belong to another result
459 aContext->AddOrRemoveSelected(aShape, false);
461 if (getCallBack()) getCallBack()->AddOrRemoveSelected(aShape);
464 NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations;
465 if (aShapesToBeSelected.IsBound(aShape))
466 aPresentations = aShapesToBeSelected.Find(aShape);
467 ObjectPtr anObject = aPrs->object();
468 getPresentations(anObject, aPresentations);
470 aShapesToBeSelected.Bind(aShape, aPresentations);
473 ObjectPtr anObject = aPrs->object();
474 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
475 if (aResult.get() && isVisible(aResult)) {
476 AISObjectPtr anObj = myResult2AISObjectMap.value(aResult);
477 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
478 if (!anAIS.IsNull()) {
479 // The methods are replaced in order to provide multi-selection, e.g. restore selection
480 // by activating multi selector widget. It also gives an advantage that the multi
481 // selection in OB gives multi-selection in the viewer
482 //aContext->SetSelected(anAIS, false);
483 // The selection in the context was cleared, so the method sets the objects are selected
484 aContext->AddOrRemoveSelected(anAIS, false);
486 if (getCallBack()) getCallBack()->AddOrRemoveSelected(anAIS);
492 if (!aShapesToBeSelected.IsEmpty())
493 XGUI_Displayer::AddOrRemoveSelectedShapes(aContext, aShapesToBeSelected);
499 //**************************************************************
500 void XGUI_Displayer::clearSelected(const bool theUpdateViewer)
502 Handle(AIS_InteractiveContext) aContext = AISContext();
503 if (!aContext.IsNull()) {
504 aContext->UnhilightSelected(false);//UnhilightCurrents(false);
505 aContext->ClearSelected(theUpdateViewer);
507 if (getCallBack()) getCallBack()->ClearSelected();
512 //**************************************************************
513 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
515 bool aErased = false;
516 Handle(AIS_InteractiveContext) aContext = AISContext();
517 if (!aContext.IsNull()) {
519 foreach(ObjectPtr aObj, myResult2AISObjectMap.objects()) {
520 AISObjectPtr aAISObj = myResult2AISObjectMap.value(aObj);
522 foreach(ObjectPtr aObj, myResult2AISObjectMap.keys()) {
523 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
526 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
527 if (!anIO.IsNull()) {
528 emit beforeObjectErase(aObj, aAISObj);
529 aContext->Remove(anIO, false/*update viewer*/);
531 if (getCallBack()) getCallBack()->Remove(anIO);
539 myResult2AISObjectMap.clear();
542 qDebug(getResult2AISObjectMapInfo().c_str());
548 //**************************************************************
549 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
552 return myResult2AISObjectMap.value(theObject);
555 if (myResult2AISObjectMap.contains(theObject))
556 anIO = myResult2AISObjectMap[theObject];
561 //**************************************************************
562 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
564 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
565 return getObject(aRefAIS);
568 //**************************************************************
569 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
572 ObjectPtr anObject = myResult2AISObjectMap.value(theIO);
575 ResultToAISMap::const_iterator aMapIter = myResult2AISObjectMap.cbegin();
576 for (; aMapIter != myResult2AISObjectMap.cend(); aMapIter++) {
577 const AISObjectPtr& aAIS = aMapIter.value();
578 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
580 anObject = aMapIter.key();
585 if (!anObject.get()) {
586 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
587 if (!theIO.IsNull()) {
588 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
590 if (myWorkshop->module())
591 anObject = myWorkshop->module()->findPresentedObject(anAISObj);
596 //**************************************************************
597 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
599 bool aWasEnabled = isUpdateEnabled();
601 myViewerBlockedRecursiveCount--;
603 myViewerBlockedRecursiveCount++;
605 #ifdef DEBUG_VIEWER_BLOCKED_COUNT
606 std::cout << "myViewerBlockedRecursiveCount = " << myViewerBlockedRecursiveCount << std::endl;
609 if (myNeedUpdate && isUpdateEnabled()) {
611 myNeedUpdate = false;
616 //**************************************************************
617 void XGUI_Displayer::updateViewer() const
619 Handle(AIS_InteractiveContext) aContext = AISContext();
621 #ifdef DEBUG_VIEWER_BLOCKED_COUNT
622 std::cout << "updateViewer: " << (myViewerBlockedRecursiveCount == 0 ? " done" : " later")
626 if (!aContext.IsNull() && isUpdateEnabled()) {
627 //myWorkshop->viewer()->Zfitall();
628 aContext->UpdateCurrentViewer();
634 //**************************************************************
635 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
637 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
638 if (!aContext.IsNull() && (myContextId != aContext.get())) {
639 myContextId = aContext.get();
640 if (!myWorkshop->selectionActivate()->isTrihedronActive())
641 selectionActivate()->deactivateTrihedron(true);
642 // Do not modify default drawer. The same is done in ModuleBase_ResultPrs
643 //aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
644 //aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
646 // Commented out according to discussion in bug #2825
647 ModuleBase_IViewer::DefaultHighlightDrawer = aContext->HighlightStyle();
648 //Handle(Prs3d_Drawer) aSelStyle = aContext->SelectionStyle();
649 //double aDeflection =
650 // QString(ModelAPI_ResultConstruction::DEFAULT_DEFLECTION().c_str()).toDouble();
652 // aDeflection = Config_PropManager::real("Visualization", "construction_deflection");
655 //ModuleBase_IViewer::DefaultHighlightDrawer->SetDeviationCoefficient(aDeflection);
656 //aSelStyle->SetDeviationCoefficient(aDeflection);
658 Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
659 if (!aTrihedron.IsNull())
660 ModuleBase_Tools::setPointBallHighlighting(aTrihedron.get());
665 //**************************************************************
666 void XGUI_Displayer::setSelectionColor(const std::vector<int>& theColor)
668 Handle(AIS_InteractiveContext) aContext = AISContext();
669 if (!aContext.IsNull()) {
670 Quantity_Color aQColor(theColor[0] / 255.,
672 theColor[2] / 255., Quantity_TOC_RGB);
673 aContext->SelectionStyle()->SetColor(aQColor);
674 aContext->SelectionStyle()->PointAspect()->SetColor(aQColor);
675 aContext->SelectionStyle()->LineAspect()->SetColor(aQColor);
676 aContext->HighlightStyle(Prs3d_TypeOfHighlight_LocalSelected)->SetColor(aQColor);
681 //**************************************************************
682 std::vector<int> XGUI_Displayer::selectionColor() const
684 std::vector<int> aColor;
685 Handle(AIS_InteractiveContext) aContext = AISContext();
686 if (!aContext.IsNull()) {
687 Quantity_Color aQColor = aContext->SelectionStyle()->Color();
688 aColor.push_back((int)(aQColor.Red() * 255));
689 aColor.push_back((int)(aQColor.Green() * 255));
690 aColor.push_back((int)(aQColor.Blue() * 255));
696 //**************************************************************
697 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
699 Handle(AIS_InteractiveContext) aContext = AISContext();
700 if (!aContext.IsNull() && myAndFilter.IsNull()) {
701 myAndFilter = new SelectMgr_AndFilter();
702 aContext->AddFilter(myAndFilter);
707 //**************************************************************
708 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
709 const Standard_Integer theDisplayMode, bool theUpdateViewer)
711 bool aDisplayed = false;
712 Handle(AIS_InteractiveContext) aContext = AISContext();
713 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
714 if (!aContext.IsNull() && !anAISIO.IsNull()) {
715 aContext->Display(anAISIO, theDisplayMode, 0, false/*update viewer*/, AIS_DS_Displayed);
717 if (getCallBack()) getCallBack()->Display(anAISIO);
720 aContext->Deactivate(anAISIO);
722 if (getCallBack()) getCallBack()->Deactivate(anAISIO);
724 aContext->Load(anAISIO);
726 if (getCallBack()) getCallBack()->Load(anAISIO);
728 if (toActivateInSelectionModes)
729 myWorkshop->selectionActivate()->activateOnDisplay(anAISIO, theUpdateViewer);
737 //**************************************************************
738 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
740 bool aErased = false;
741 Handle(AIS_InteractiveContext) aContext = AISContext();
742 if (!aContext.IsNull()) {
743 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
744 if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
745 aContext->Remove(anAISIO, false/*update viewer*/);
747 if (getCallBack()) getCallBack()->Remove(anAISIO);
752 if (aErased && theUpdateViewer)
757 //**************************************************************
758 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
760 if (theMode == NoMode)
763 Handle(AIS_InteractiveContext) aContext = AISContext();
764 if (aContext.IsNull())
767 AISObjectPtr aAISObj = getAISObject(theObject);
771 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
772 aContext->SetDisplayMode(aAISIO, theMode, false);
773 // Redisplay in order to update new mode because it could be not computed before
778 //**************************************************************
779 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
781 Handle(AIS_InteractiveContext) aContext = AISContext();
782 if (aContext.IsNull())
785 AISObjectPtr aAISObj = getAISObject(theObject);
789 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
790 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
793 //**************************************************************
794 void XGUI_Displayer::deactivateSelectionFilters(const bool theAddFilterOnly)
796 Handle(AIS_InteractiveContext) aContext = AISContext();
797 if (!myAndFilter.IsNull()) {
799 if (!aContext.IsNull()) {
800 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
801 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
802 for (; anIt.More() && !aFound; anIt.Next()) {
803 Handle(SelectMgr_Filter) aFilter = anIt.Value();
804 aFound = aFilter == myAndFilter;
807 aContext->RemoveFilter(myAndFilter);
809 myAndFilter.Nullify();
813 //**************************************************************
814 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
816 Handle(AIS_InteractiveContext) aContext = AISContext();
817 if (aContext.IsNull() || hasSelectionFilter(theFilter))
820 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
821 if (!aCompositeFilter.IsNull()) {
822 aCompositeFilter->Add(theFilter);
823 #ifdef DEBUG_SELECTION_FILTERS
824 int aCount = aCompositeFilter->StoredFilters().Extent();
825 qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
830 //**************************************************************
831 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
833 Handle(AIS_InteractiveContext) aContext = AISContext();
834 if (aContext.IsNull())
837 Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
838 if (!aCompositeFilter.IsNull() && aCompositeFilter->IsIn(theFilter)) {
839 aCompositeFilter->Remove(theFilter);
840 #ifdef DEBUG_SELECTION_FILTERS
841 int aCount = aCompositeFilter->StoredFilters().Extent();
842 qDebug(QString("removeSelectionFilter: filters.count() = %1")
843 .arg(aCount).toStdString().c_str());
848 //**************************************************************
849 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
851 bool aFilterFound = false;
853 Handle(AIS_InteractiveContext) aContext = AISContext();
854 if (aContext.IsNull())
856 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
857 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
858 for (; aIt.More() && !aFilterFound; aIt.Next()) {
859 if (theFilter.get() == aIt.Value().get())
862 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
863 if (!aCompositeFilter.IsNull()) {
864 const SelectMgr_ListOfFilter& aStoredFilters = aCompositeFilter->StoredFilters();
865 for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
866 if (theFilter.get() == aIt.Value().get())
873 //**************************************************************
874 void XGUI_Displayer::removeFilters()
876 Handle(AIS_InteractiveContext) aContext = AISContext();
877 if (aContext.IsNull())
880 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
881 if (!aCompositeFilter.IsNull())
882 aCompositeFilter->Clear();
885 //**************************************************************
886 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
889 QObjectPtrList aDispList = myResult2AISObjectMap.objects();
891 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
893 foreach(ObjectPtr aObj, aDispList) {
894 if (!theList.contains(aObj))
897 foreach(ObjectPtr aObj, theList) {
898 if (!isVisible(aObj))
899 display(aObj, false);
904 //**************************************************************
905 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
907 if (!isVisible(theObject))
910 AISObjectPtr aAISObj = getAISObject(theObject);
911 if (aAISObj.get() == NULL)
914 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
915 return ::canBeShaded(anAIS, myWorkshop->module());
918 //**************************************************************
919 //bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
921 // AISObjectPtr anAISObj = getAISObject(theObject);
922 // // correct the result's color it it has the attribute
923 // ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
925 // // Customization of presentation
926 // GeomCustomPrsPtr aCustomPrs;
927 // FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
928 // if (aFeature.get() != NULL) {
929 // GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
930 // if (aCustPrs.get() != NULL)
931 // aCustomPrs = aCustPrs;
933 // if (aCustomPrs.get() == NULL) {
934 // GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
935 // // we ignore presentable not customized objects
936 // if (aPrs.get() == NULL)
937 // aCustomPrs = myCustomPrs;
939 // bool isCustomized = aCustomPrs.get() &&
940 // aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
941 // isCustomized = myWorkshop->module()->afterCustomisePresentation(aResult, anAISObj, myCustomPrs)
944 // // update presentation state if faces panel is active
945 // if (anAISObj.get() && myWorkshop->facesPanel())
946 // isCustomized = myWorkshop->facesPanel()->customizeObject(theObject, anAISObj) || isCustomized;
948 // return isCustomized;
951 //**************************************************************
952 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject,
953 const QColor& theColor,
954 bool theUpdateViewer)
956 if (!isVisible(theObject))
959 AISObjectPtr anAISObj = getAISObject(theObject);
961 anAISObj->getColor(aR, aG, aB);
962 anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
965 return QColor(aR, aG, aB);
968 //**************************************************************
969 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
972 myResult2AISObjectMap.add(theObject, theAIS);
974 myResult2AISObjectMap[theObject] = theAIS;
978 std::ostringstream aPtrStr;
979 aPtrStr << theObject.get();
980 qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
981 qDebug(getResult2AISObjectMapInfo().c_str());
986 //**************************************************************
987 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
989 QStringList aContent;
991 foreach(ObjectPtr aObj, myResult2AISObjectMap.objects()) {
992 AISObjectPtr aAISObj = myResult2AISObjectMap.value(aObj);
994 foreach(ObjectPtr aObj, myResult2AISObjectMap.keys()) {
995 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
997 std::ostringstream aPtrStr;
998 aPtrStr << "aObj = " << aObj.get() << ":";
999 aPtrStr << "anAIS = " << aAISObj.get() << ":";
1000 aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
1002 aContent.append(aPtrStr.str().c_str());
1004 return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
1005 arg(aContent.join("\n")).toStdString().c_str();
1009 //**************************************************************
1010 void XGUI_Displayer::getPresentations(const ObjectPtr& theObject,
1011 NCollection_Map<Handle(AIS_InteractiveObject)>& thePresentations)
1013 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1014 if (aResult.get()) {
1015 AISObjectPtr aAISObj = getAISObject(aResult);
1016 if (aAISObj.get() == NULL) {
1017 // if result is a result of a composite feature, it is visualized by visualization of
1018 // composite children, so we should get one of this presentations
1019 ResultBodyPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
1020 if (aCompSolid.get() && aCompSolid->numberOfSubs() > 0) {
1021 aAISObj = getAISObject(aCompSolid->subResult(0));
1024 if (aAISObj.get() != NULL) {
1025 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1026 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1027 thePresentations.Add(anAIS);
1031 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
1032 // find presentation of the feature
1033 AISObjectPtr aAISObj = getAISObject(aFeature);
1034 if (aAISObj.get() != NULL) {
1035 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1036 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1037 thePresentations.Add(anAIS);
1039 // find presentations of the feature results
1040 std::list<ResultPtr> aResults;
1041 ModelAPI_Tools::allResults(aFeature, aResults);
1042 std::list<ResultPtr>::const_iterator anIt = aResults.begin(), aLast = aResults.end();
1043 for (; anIt != aLast; ++anIt) {
1044 AISObjectPtr aCurAISObj = getAISObject(*anIt);
1045 if (aCurAISObj.get() != NULL) {
1046 Handle(AIS_InteractiveObject) anAIS = aCurAISObj->impl<Handle(AIS_InteractiveObject)>();
1047 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1048 thePresentations.Add(anAIS);
1054 //**************************************************************
1055 void XGUI_Displayer::displayTrihedron(bool theToDisplay) const
1057 Handle(AIS_InteractiveContext) aContext = AISContext();
1058 if (aContext.IsNull())
1061 Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
1063 XGUI_SelectionActivate* aSelectionActive = selectionActivate();
1065 if (!aContext->IsDisplayed(aTrihedron))
1066 aContext->Display(aTrihedron,
1068 -1 /* selection mode */,
1069 Standard_True /* update viewer*/,
1070 AIS_DS_Displayed /* xdisplay status */);
1072 if (getCallBack()) getCallBack()->Display(aTrihedron);
1075 if (!aSelectionActive->isTrihedronActive())
1076 aSelectionActive->deactivateTrihedron(false);
1078 aSelectionActive->activate(aTrihedron, false);
1080 aSelectionActive->deactivateTrihedron(false);
1082 aContext->Erase(aTrihedron, Standard_True);
1084 if (getCallBack()) getCallBack()->Remove(aTrihedron);
1089 //**************************************************************
1090 void XGUI_Displayer::AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
1091 const NCollection_DataMap<TopoDS_Shape,
1092 NCollection_Map<Handle(AIS_InteractiveObject)>>& theShapesToBeSelected)
1094 NCollection_Map<Handle(AIS_InteractiveObject)> aCompsolidPresentations;
1095 NCollection_Map<Handle(AIS_InteractiveObject)> aSelectedPresentations;
1097 NCollection_List<Handle(SelectBasics_EntityOwner)> anActiveOwners;
1098 theContext->MainSelector()->ActiveOwners(anActiveOwners);
1099 NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (anActiveOwners);
1100 Handle(SelectMgr_EntityOwner) anOwner;
1102 /// It is very important to check that the owner is processed only once and has a map of
1103 /// processed owners because SetSelected works as a switch.
1104 /// If count of calls setSelectec is even, the object stays in the previous state
1105 /// (selected, deselected)
1106 /// OCCT: to write about the problem that active owners method returns one owner several times
1107 QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
1108 for (; anOwnersIt.More(); anOwnersIt.Next()) {
1109 anOwner = anOwnersIt.Value();
1110 if (aSelectedIds.contains((size_t)anOwner.get()))
1112 aSelectedIds.append((size_t)anOwner.get());
1114 Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
1115 if (!BROwnr.IsNull() && BROwnr->HasShape()) {
1116 const TopoDS_Shape& aShape = BROwnr->Shape();
1118 Handle(ModuleBase_ResultPrs) aResPrs =
1119 Handle(ModuleBase_ResultPrs)::DownCast(BROwnr->Selectable());
1120 TopoDS_Shape aRealShape;
1121 if (!aResPrs.IsNull()) {
1122 aRealShape = aResPrs->originalShape();
1125 if (aShape.IsNull())
1128 Handle(ModuleBase_BRepOwner) aCustomOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
1130 NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)> >
1131 ::Iterator aShapeIt(theShapesToBeSelected);
1132 for (; aShapeIt.More(); aShapeIt.Next()) {
1133 const TopoDS_Shape& aParameterShape = aShapeIt.Key();
1134 // In case of compound we cannot rely on simple comparison method.
1135 // If the compound is generated by Group feature then this compound is alwais new.
1136 // So, we have to compare content of these compounds
1138 // isSame should be used here as it does not check orientation of shapes
1139 // despite on isEqual of shapes or IsBound for shape in QMap. Orientation is
1140 // different for Edges shapes in model shape and owner even if this is the same shape
1141 bool isSame = ModuleBase_Tools::isSameShape(aParameterShape, aShape);
1143 // In case of using HideFaces panel we can have instead of an original shape
1144 // a compaund of faces which represent original shape with hidden faces.
1145 // So, we have to compare the parameter with original shape
1146 if (!aRealShape.IsNull())
1147 isSame = ModuleBase_Tools::isSameShape(aParameterShape, aRealShape);
1150 Handle(AIS_InteractiveObject) anOwnerPresentation =
1151 Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
1152 NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations =
1153 theShapesToBeSelected.Find(aParameterShape);
1154 if (aPresentations.Contains(anOwnerPresentation)) {
1155 theContext->AddOrRemoveSelected(anOwner, Standard_False);
1156 anOwner->SetSelected(Standard_True);
1157 // collect selected presentations to do not select them if compsolid is selected
1158 if (!aSelectedPresentations.Contains(anOwnerPresentation))
1159 aSelectedPresentations.Add(anOwnerPresentation);
1162 else if (!aCustomOwner.IsNull()) { // CompSolid processing #2219
1163 // shape of owner is compound, but shape to be selected is compsolid, so
1164 // we need to compare shape to AIS presentation of owner(rule of the owner creation)
1165 Handle(AIS_Shape) anOwnerPresentation =
1166 Handle(AIS_Shape)::DownCast(anOwner->Selectable());
1167 const TopoDS_Shape& aPresentationShape = anOwnerPresentation->Shape();
1168 if (aParameterShape.IsSame(aPresentationShape) &&
1169 !aCompsolidPresentations.Contains(anOwnerPresentation))
1170 aCompsolidPresentations.Add(anOwnerPresentation);
1175 // select CompSolid presentations if their owners was not selected yet
1176 NCollection_Map<Handle(AIS_InteractiveObject)>::Iterator anIt (aCompsolidPresentations);
1177 for (; anIt.More(); anIt.Next()) {
1178 if (aSelectedPresentations.Contains(anIt.Value()))
1180 theContext->AddOrRemoveSelected(anIt.Value(), Standard_False);
1184 //**************************************************************
1185 XGUI_SelectionActivate* XGUI_Displayer::selectionActivate() const
1187 return myWorkshop->selectionActivate();
1190 //**************************************************************
1191 GeomPlanePtr XGUI_Displayer::getScreenPlane() const
1193 GeomPlanePtr aResult;
1194 Handle(AIS_InteractiveContext) aContext = AISContext();
1195 if (!aContext.IsNull()) {
1196 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1197 Handle(V3d_View) aView;
1198 for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews()) {
1199 aView = aViewer->ActiveView();
1202 if (!aView.IsNull()) {
1203 double aEyeX, aEyeY, aEyeZ;
1204 aView->Eye(aEyeX, aEyeY, aEyeZ);
1206 double aProjX, aProjY, aProjZ;
1207 aView->Proj(aProjX, aProjY, aProjZ);
1209 GeomPointPtr aPnt = GeomPointPtr(new GeomAPI_Pnt(aEyeX, aEyeY, aEyeZ));
1210 GeomDirPtr aDir = GeomDirPtr(new GeomAPI_Dir(aProjX, aProjY, aProjZ));
1212 aResult = GeomPlanePtr(new GeomAPI_Pln(aPnt, aDir));
1218 double XGUI_Displayer::getViewScale() const
1220 Handle(AIS_InteractiveContext) aContext = AISContext();
1221 if (!aContext.IsNull()) {
1222 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1223 Handle(V3d_View) aView;
1224 for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews()) {
1225 aView = aViewer->ActiveView();
1228 return aView->Camera()->Scale();