1 // Copyright (C) 2014-2019 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_DimensionSelectionMode.hxx>
60 #include <AIS_Shape.hxx>
61 #include <AIS_Dimension.hxx>
62 #include <AIS_Trihedron.hxx>
63 #ifdef BEFORE_TRIHEDRON_PATCH
64 #include <AIS_Axis.hxx>
65 #include <AIS_Plane.hxx>
66 #include <AIS_Point.hxx>
68 #include <AIS_Selection.hxx>
69 #include <Prs3d_Drawer.hxx>
70 #include <Prs3d_IsoAspect.hxx>
71 #include <SelectMgr_ListOfFilter.hxx>
72 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
73 #include <SelectMgr_SelectionManager.hxx>
74 #include <TColStd_ListIteratorOfListOfInteger.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>
92 /// defines the local context mouse selection sensitivity
93 const int MOUSE_SENSITIVITY_IN_PIXEL = 10;
95 //#define DEBUG_DISPLAY
96 //#define DEBUG_FEATURE_REDISPLAY
97 //#define DEBUG_SELECTION_FILTERS
99 //#define DEBUG_OCCT_SHAPE_SELECTION
101 #define CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
103 //#define DEBUG_VIEWER_BLOCKED_COUNT
105 //**************************************************************
106 void displayedObjects(const Handle(AIS_InteractiveContext)& theAIS, AIS_ListOfInteractive& theList)
108 // Get from null point
109 #if OCC_VERSION_HEX < 0x070400
110 theAIS->DisplayedObjects(theList, true);
112 theAIS->DisplayedObjects(theList);
116 QString qIntListInfo(const QIntList& theValues, const QString& theSeparator = QString(", "))
119 QIntList::const_iterator anIt = theValues.begin(), aLast = theValues.end();
120 for (; anIt != aLast; anIt++) {
121 anInfo.append(QString::number(*anIt));
123 return anInfo.join(theSeparator);
126 //**************************************************************
127 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
128 : myWorkshop(theWorkshop), myNeedUpdate(false),
129 myViewerBlockedRecursiveCount(0), myContextId(0)
133 //**************************************************************
134 XGUI_Displayer::~XGUI_Displayer()
138 //**************************************************************
139 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
141 return myResult2AISObjectMap.contains(theObject);
144 //**************************************************************
145 bool XGUI_Displayer::display(ObjectPtr theObject, bool theUpdateViewer)
147 bool aDisplayed = false;
148 if (isVisible(theObject)) {
149 aDisplayed = redisplay(theObject, theUpdateViewer);
152 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
153 bool isShading = false;
154 if (aPrs.get() != NULL) {
155 GeomScreenParamsPtr aScreen = std::dynamic_pointer_cast<GeomAPI_IScreenParams>(theObject);
157 aScreen->setScreenPlane(getScreenPlane());
158 aScreen->setViewScale(getViewScale());
160 anAIS = aPrs->getAISObject(anAIS);
162 anAIS = myWorkshop->module()->createPresentation(theObject);
166 aDisplayed = display(theObject, anAIS, isShading, theUpdateViewer);
172 //**************************************************************
173 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS, ModuleBase_IModule* theModule)
175 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
176 if (!aShapePrs.IsNull()) {
177 TopoDS_Shape aShape = aShapePrs->Shape();
180 TopAbs_ShapeEnum aType = aShape.ShapeType();
181 if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
184 // Check that the presentation is not a sketch
185 return theModule->canBeShaded(theAIS);
191 //**************************************************************
192 bool XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
193 bool isShading, bool theUpdateViewer)
195 bool aDisplayed = false;
197 Handle(AIS_InteractiveContext) aContext = AISContext();
198 if (aContext.IsNull())
201 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
202 if (!anAISIO.IsNull()) {
203 appendResultObject(theObject, theAIS);
205 //bool isCustomized = customizeObject(theObject);
207 int aDispMode = isShading? Shading : Wireframe;
208 anAISIO->SetDisplayMode(aDispMode);
209 aContext->Display(anAISIO, aDispMode, 0, false, true, AIS_DS_Displayed);
211 if (getCallBack()) getCallBack()->Display(anAISIO);
215 emit objectDisplayed(theObject, theAIS);
216 selectionActivate()->activate(anAISIO, theUpdateViewer);
224 //**************************************************************
225 bool XGUI_Displayer::erase(ObjectPtr theObject, const bool theUpdateViewer)
227 bool aErased = false;
228 if (!isVisible(theObject))
231 Handle(AIS_InteractiveContext) aContext = AISContext();
232 if (aContext.IsNull())
235 AISObjectPtr anObject = myResult2AISObjectMap.value(theObject);
237 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
238 if (!anAIS.IsNull()) {
239 emit beforeObjectErase(theObject, anObject);
240 aContext->Remove(anAIS, false/*update viewer*/);
242 if (getCallBack()) getCallBack()->Remove(anAIS);
247 myResult2AISObjectMap.remove(theObject);
250 std::ostringstream aPtrStr;
251 aPtrStr << theObject.get();
252 qDebug(QString("erase object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
253 qDebug(getResult2AISObjectMapInfo().c_str());
262 //**************************************************************
263 bool XGUI_Displayer::redisplay(ObjectPtr theObject, bool theUpdateViewer)
265 bool aRedisplayed = false;
266 Handle(AIS_InteractiveContext) aContext = AISContext();
267 if (aContext.IsNull())
270 if (!isVisible(theObject))
273 AISObjectPtr aAISObj = getAISObject(theObject);
274 Handle(AIS_InteractiveObject) aAISIO;
276 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
278 GeomScreenParamsPtr aScreen = std::dynamic_pointer_cast<GeomAPI_IScreenParams>(theObject);
280 aScreen->setScreenPlane(getScreenPlane());
281 aScreen->setViewScale(getViewScale());
283 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
285 aRedisplayed = erase(theObject, theUpdateViewer);
288 if (aAIS_Obj != aAISObj) {
289 erase(theObject, theUpdateViewer);
290 appendResultObject(theObject, aAIS_Obj);
292 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
295 aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
298 if (!aAISIO.IsNull()) {
299 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
302 std::vector<int> aColor;
303 ModelAPI_Tools::getColor(aResult, aColor);
304 if (aColor.size() > 0) {
306 aCol(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB);
307 aAISIO->SetColor(aCol);
310 double aDeflection = ModelAPI_Tools::getDeflection(aResult);
311 if ((aDeflection >= 0) && (aDeflection != aAISObj->getDeflection()))
312 aAISObj->setDeflection(aDeflection);
315 double aTransparency = ModelAPI_Tools::getTransparency(aResult);
316 if ((aTransparency >= 0) && (aTransparency != aAISObj->getTransparency()))
317 aAISObj->setTransparency(aTransparency);
320 Handle(ModuleBase_ResultPrs) aResPrs = Handle(ModuleBase_ResultPrs)::DownCast(aAISIO);
321 if (!aResPrs.IsNull())
322 aResPrs->updateIsoLines();
324 myWorkshop->module()->storeSelection();
326 #ifdef CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
327 myWorkshop->selector()->deselectPresentation(aAISIO);
330 if (aContext->IsDisplayed(aAISIO))
331 aContext->Redisplay(aAISIO, false);
333 aContext->Display(aAISIO, false);
336 if (getCallBack()) getCallBack()->Redisplay(aAISIO);
339 //if (aNeedToRestoreSelection)
340 myWorkshop->module()->restoreSelection();
343 #ifdef DEBUG_FEATURE_REDISPLAY
344 qDebug(" Redisplay happens");
352 //**************************************************************
353 void XGUI_Displayer::redisplayObjects()
355 // redisplay objects visualized in the viewer
356 static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
357 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
358 QObjectPtrList aDisplayed = displayedObjects();
359 QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
360 for (; anIt != aLast; anIt++) {
361 aECreator->sendUpdated(*anIt, EVENT_DISP);
363 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
364 if (aViewer->isColorScaleVisible()) {
365 aViewer->setupColorScale();
366 aViewer->setColorScaleShown(true);
368 Events_Loop::loop()->flush(EVENT_DISP);
371 //**************************************************************
372 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
373 const bool theUpdateViewer)
375 //Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
376 //if (!aTrihedron.IsNull())
377 // deactivateAIS(aTrihedron);
379 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
380 for (; anIt != aLast; anIt++) {
381 selectionActivate()->deactivate(*anIt, false);
383 //VSV It seems that there is no necessity to update viewer on deactivation
384 //if (theUpdateViewer)
388 //**************************************************************
389 bool XGUI_Displayer::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
391 bool aVisible = false;
392 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
393 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
394 if (aPrs.get() || aResult.get()) {
395 aVisible = theDisplayer->isVisible(theObject);
396 // compsolid is not visualized in the viewer,
397 // but should have presentation when all sub solids are
398 // visible. It is useful for highlight presentation where compsolid shape is selectable
399 if (!aVisible && aResult.get() && aResult->groupName() == ModelAPI_ResultBody::group()) {
400 ResultBodyPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
401 if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
402 int aNumberOfSubs = aCompsolidResult->numberOfSubs();
403 bool anAllSubsVisible = aNumberOfSubs > 0;
404 for(int i = 0; i < aNumberOfSubs && anAllSubsVisible; i++) {
405 anAllSubsVisible = theDisplayer->isVisible(aCompsolidResult->subResult(i));
407 aVisible = anAllSubsVisible;
411 // it is possible that feature is presentable and has results, so we should check visibility
412 // of results if presentation is not shown (e.g. Sketch Circle/Arc features)
414 // check if all results of the feature are visible
415 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
416 std::list<ResultPtr> aResults;
417 ModelAPI_Tools::allResults(aFeature, aResults);
418 std::list<ResultPtr>::const_iterator aIt;
419 aVisible = !aResults.empty();
420 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
421 aVisible = aVisible && theDisplayer->isVisible(*aIt);
427 //**************************************************************
428 void XGUI_Displayer::setSelected(const QList<ModuleBase_ViewerPrsPtr>& theValues,
429 bool theUpdateViewer)
431 Handle(AIS_InteractiveContext) aContext = AISContext();
432 if (aContext.IsNull())
434 aContext->UnhilightSelected(false);
435 aContext->ClearSelected(false);
437 if (getCallBack()) getCallBack()->ClearSelected();
439 NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)>>
442 foreach (ModuleBase_ViewerPrsPtr aPrs, theValues) {
443 const GeomShapePtr& aGeomShape = aPrs->shape();
444 if (aGeomShape.get() && !aGeomShape->isNull()) {
445 const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
446 #ifdef DEBUG_OCCT_SHAPE_SELECTION
447 // problem 1: performance
448 // problem 2: IO is not specified, so the first found owner is selected, as a result
449 // it might belong to another result
450 aContext->AddOrRemoveSelected(aShape, false);
452 if (getCallBack()) getCallBack()->AddOrRemoveSelected(aShape);
455 NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations;
456 if (aShapesToBeSelected.IsBound(aShape))
457 aPresentations = aShapesToBeSelected.Find(aShape);
458 ObjectPtr anObject = aPrs->object();
459 getPresentations(anObject, aPresentations);
461 aShapesToBeSelected.Bind(aShape, aPresentations);
464 ObjectPtr anObject = aPrs->object();
465 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
466 if (aResult.get() && isVisible(aResult)) {
467 AISObjectPtr anObj = myResult2AISObjectMap.value(aResult);
468 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
469 if (!anAIS.IsNull()) {
470 // The methods are replaced in order to provide multi-selection, e.g. restore selection
471 // by activating multi selector widget. It also gives an advantage that the multi
472 // selection in OB gives multi-selection in the viewer
473 //aContext->SetSelected(anAIS, false);
474 // The selection in the context was cleared, so the method sets the objects are selected
475 aContext->AddOrRemoveSelected(anAIS, false);
477 if (getCallBack()) getCallBack()->AddOrRemoveSelected(anAIS);
483 if (!aShapesToBeSelected.IsEmpty())
484 XGUI_Displayer::AddOrRemoveSelectedShapes(aContext, aShapesToBeSelected);
490 //**************************************************************
491 void XGUI_Displayer::clearSelected(const bool theUpdateViewer)
493 Handle(AIS_InteractiveContext) aContext = AISContext();
494 if (!aContext.IsNull()) {
495 aContext->UnhilightSelected(false);//UnhilightCurrents(false);
496 aContext->ClearSelected(theUpdateViewer);
498 if (getCallBack()) getCallBack()->ClearSelected();
503 //**************************************************************
504 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
506 bool aErased = false;
507 Handle(AIS_InteractiveContext) aContext = AISContext();
508 if (!aContext.IsNull()) {
510 foreach(ObjectPtr aObj, myResult2AISObjectMap.objects()) {
511 AISObjectPtr aAISObj = myResult2AISObjectMap.value(aObj);
513 foreach(ObjectPtr aObj, myResult2AISObjectMap.keys()) {
514 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
517 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
518 if (!anIO.IsNull()) {
519 emit beforeObjectErase(aObj, aAISObj);
520 aContext->Remove(anIO, false/*update viewer*/);
522 if (getCallBack()) getCallBack()->Remove(anIO);
530 myResult2AISObjectMap.clear();
533 qDebug(getResult2AISObjectMapInfo().c_str());
539 //**************************************************************
540 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
543 return myResult2AISObjectMap.value(theObject);
546 if (myResult2AISObjectMap.contains(theObject))
547 anIO = myResult2AISObjectMap[theObject];
552 //**************************************************************
553 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
555 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
556 return getObject(aRefAIS);
559 //**************************************************************
560 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
563 ObjectPtr anObject = myResult2AISObjectMap.value(theIO);
566 ResultToAISMap::const_iterator aMapIter = myResult2AISObjectMap.cbegin();
567 for (; aMapIter != myResult2AISObjectMap.cend(); aMapIter++) {
568 const AISObjectPtr& aAIS = aMapIter.value();
569 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
571 anObject = aMapIter.key();
576 if (!anObject.get()) {
577 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
578 if (!theIO.IsNull()) {
579 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
581 if (myWorkshop->module())
582 anObject = myWorkshop->module()->findPresentedObject(anAISObj);
587 //**************************************************************
588 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
590 bool aWasEnabled = isUpdateEnabled();
592 myViewerBlockedRecursiveCount--;
594 myViewerBlockedRecursiveCount++;
596 #ifdef DEBUG_VIEWER_BLOCKED_COUNT
597 std::cout << "myViewerBlockedRecursiveCount = " << myViewerBlockedRecursiveCount << std::endl;
600 if (myNeedUpdate && isUpdateEnabled()) {
602 myNeedUpdate = false;
607 //**************************************************************
608 void XGUI_Displayer::updateViewer() const
610 Handle(AIS_InteractiveContext) aContext = AISContext();
612 #ifdef DEBUG_VIEWER_BLOCKED_COUNT
613 std::cout << "updateViewer: " << (myViewerBlockedRecursiveCount == 0 ? " done" : " later")
617 if (!aContext.IsNull() && isUpdateEnabled()) {
618 //myWorkshop->viewer()->Zfitall();
619 aContext->UpdateCurrentViewer();
625 //**************************************************************
626 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
628 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
629 if (!aContext.IsNull() && (myContextId != aContext.get())) {
630 myContextId = aContext.get();
631 if (!myWorkshop->selectionActivate()->isTrihedronActive())
632 selectionActivate()->deactivateTrihedron(true);
633 // Do not modify default drawer. The same is done in ModuleBase_ResultPrs
634 //aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
635 //aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
637 //Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
638 //aTrihedron->getHighlightPointAspect()->SetScale(2.0);
639 //aTrihedron->getHighlightPointAspect()->SetTypeOfMarker(Aspect_TOM_O_STAR);
641 // Commented out according to discussion in bug #2825
642 ModuleBase_IViewer::DefaultHighlightDrawer = aContext->HighlightStyle();
643 //Handle(Prs3d_Drawer) aSelStyle = aContext->SelectionStyle();
644 //double aDeflection =
645 // QString(ModelAPI_ResultConstruction::DEFAULT_DEFLECTION().c_str()).toDouble();
647 // aDeflection = Config_PropManager::real("Visualization", "construction_deflection");
650 //ModuleBase_IViewer::DefaultHighlightDrawer->SetDeviationCoefficient(aDeflection);
651 //aSelStyle->SetDeviationCoefficient(aDeflection);
656 //**************************************************************
657 void XGUI_Displayer::setSelectionColor(const std::vector<int>& theColor)
659 Handle(AIS_InteractiveContext) aContext = AISContext();
660 if (!aContext.IsNull()) {
661 Quantity_Color aQColor(theColor[0] / 255.,
663 theColor[2] / 255., Quantity_TOC_RGB);
664 aContext->SelectionStyle()->SetColor(aQColor);
665 aContext->SelectionStyle()->PointAspect()->SetColor(aQColor);
666 aContext->SelectionStyle()->LineAspect()->SetColor(aQColor);
667 aContext->HighlightStyle(Prs3d_TypeOfHighlight_LocalSelected)->SetColor(aQColor);
672 //**************************************************************
673 std::vector<int> XGUI_Displayer::selectionColor() const
675 std::vector<int> aColor;
676 Handle(AIS_InteractiveContext) aContext = AISContext();
677 if (!aContext.IsNull()) {
678 Quantity_Color aQColor = aContext->SelectionStyle()->Color();
679 aColor.push_back((int)(aQColor.Red() * 255));
680 aColor.push_back((int)(aQColor.Green() * 255));
681 aColor.push_back((int)(aQColor.Blue() * 255));
687 //**************************************************************
688 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
690 Handle(AIS_InteractiveContext) aContext = AISContext();
691 if (!aContext.IsNull() && myAndFilter.IsNull()) {
692 myAndFilter = new SelectMgr_AndFilter();
693 aContext->AddFilter(myAndFilter);
698 //**************************************************************
699 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
700 const Standard_Integer theDisplayMode, bool theUpdateViewer)
702 bool aDisplayed = false;
703 Handle(AIS_InteractiveContext) aContext = AISContext();
704 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
705 if (!aContext.IsNull() && !anAISIO.IsNull()) {
706 aContext->Display(anAISIO, theDisplayMode, 0, false/*update viewer*/, true, AIS_DS_Displayed);
708 if (getCallBack()) getCallBack()->Display(anAISIO);
711 aContext->Deactivate(anAISIO);
713 if (getCallBack()) getCallBack()->Deactivate(anAISIO);
715 aContext->Load(anAISIO);
717 if (getCallBack()) getCallBack()->Load(anAISIO);
719 if (toActivateInSelectionModes)
720 myWorkshop->selectionActivate()->activateOnDisplay(anAISIO, theUpdateViewer);
728 //**************************************************************
729 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
731 bool aErased = false;
732 Handle(AIS_InteractiveContext) aContext = AISContext();
733 if (!aContext.IsNull()) {
734 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
735 if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
736 aContext->Remove(anAISIO, false/*update viewer*/);
738 if (getCallBack()) getCallBack()->Remove(anAISIO);
743 if (aErased && theUpdateViewer)
748 //**************************************************************
749 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
751 if (theMode == NoMode)
754 Handle(AIS_InteractiveContext) aContext = AISContext();
755 if (aContext.IsNull())
758 AISObjectPtr aAISObj = getAISObject(theObject);
762 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
763 aContext->SetDisplayMode(aAISIO, theMode, false);
764 // Redisplay in order to update new mode because it could be not computed before
769 //**************************************************************
770 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
772 Handle(AIS_InteractiveContext) aContext = AISContext();
773 if (aContext.IsNull())
776 AISObjectPtr aAISObj = getAISObject(theObject);
780 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
781 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
784 //**************************************************************
785 void XGUI_Displayer::deactivateSelectionFilters(const bool theAddFilterOnly)
787 Handle(AIS_InteractiveContext) aContext = AISContext();
788 if (!myAndFilter.IsNull()) {
790 if (!aContext.IsNull()) {
791 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
792 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
793 for (; anIt.More() && !aFound; anIt.Next()) {
794 Handle(SelectMgr_Filter) aFilter = anIt.Value();
795 aFound = aFilter == myAndFilter;
798 aContext->RemoveFilter(myAndFilter);
800 myAndFilter.Nullify();
804 //**************************************************************
805 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
807 Handle(AIS_InteractiveContext) aContext = AISContext();
808 if (aContext.IsNull() || hasSelectionFilter(theFilter))
811 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
812 if (!aCompositeFilter.IsNull()) {
813 aCompositeFilter->Add(theFilter);
814 #ifdef DEBUG_SELECTION_FILTERS
815 int aCount = aCompositeFilter->StoredFilters().Extent();
816 qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
821 //**************************************************************
822 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
824 Handle(AIS_InteractiveContext) aContext = AISContext();
825 if (aContext.IsNull())
828 Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
829 if (!aCompositeFilter.IsNull() && aCompositeFilter->IsIn(theFilter)) {
830 aCompositeFilter->Remove(theFilter);
831 #ifdef DEBUG_SELECTION_FILTERS
832 int aCount = aCompositeFilter->StoredFilters().Extent();
833 qDebug(QString("removeSelectionFilter: filters.count() = %1")
834 .arg(aCount).toStdString().c_str());
839 //**************************************************************
840 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
842 bool aFilterFound = false;
844 Handle(AIS_InteractiveContext) aContext = AISContext();
845 if (aContext.IsNull())
847 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
848 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
849 for (; aIt.More() && !aFilterFound; aIt.Next()) {
850 if (theFilter.get() == aIt.Value().get())
853 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
854 if (!aCompositeFilter.IsNull()) {
855 const SelectMgr_ListOfFilter& aStoredFilters = aCompositeFilter->StoredFilters();
856 for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
857 if (theFilter.get() == aIt.Value().get())
864 //**************************************************************
865 void XGUI_Displayer::removeFilters()
867 Handle(AIS_InteractiveContext) aContext = AISContext();
868 if (aContext.IsNull())
871 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
872 if (!aCompositeFilter.IsNull())
873 aCompositeFilter->Clear();
876 //**************************************************************
877 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
880 QObjectPtrList aDispList = myResult2AISObjectMap.objects();
882 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
884 foreach(ObjectPtr aObj, aDispList) {
885 if (!theList.contains(aObj))
888 foreach(ObjectPtr aObj, theList) {
889 if (!isVisible(aObj))
890 display(aObj, false);
895 //**************************************************************
896 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
898 if (!isVisible(theObject))
901 AISObjectPtr aAISObj = getAISObject(theObject);
902 if (aAISObj.get() == NULL)
905 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
906 return ::canBeShaded(anAIS, myWorkshop->module());
909 //**************************************************************
910 //bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
912 // AISObjectPtr anAISObj = getAISObject(theObject);
913 // // correct the result's color it it has the attribute
914 // ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
916 // // Customization of presentation
917 // GeomCustomPrsPtr aCustomPrs;
918 // FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
919 // if (aFeature.get() != NULL) {
920 // GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
921 // if (aCustPrs.get() != NULL)
922 // aCustomPrs = aCustPrs;
924 // if (aCustomPrs.get() == NULL) {
925 // GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
926 // // we ignore presentable not customized objects
927 // if (aPrs.get() == NULL)
928 // aCustomPrs = myCustomPrs;
930 // bool isCustomized = aCustomPrs.get() &&
931 // aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
932 // isCustomized = myWorkshop->module()->afterCustomisePresentation(aResult, anAISObj, myCustomPrs)
935 // // update presentation state if faces panel is active
936 // if (anAISObj.get() && myWorkshop->facesPanel())
937 // isCustomized = myWorkshop->facesPanel()->customizeObject(theObject, anAISObj) || isCustomized;
939 // return isCustomized;
942 //**************************************************************
943 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject,
944 const QColor& theColor,
945 bool theUpdateViewer)
947 if (!isVisible(theObject))
950 AISObjectPtr anAISObj = getAISObject(theObject);
952 anAISObj->getColor(aR, aG, aB);
953 anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
956 return QColor(aR, aG, aB);
959 //**************************************************************
960 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
963 myResult2AISObjectMap.add(theObject, theAIS);
965 myResult2AISObjectMap[theObject] = theAIS;
969 std::ostringstream aPtrStr;
970 aPtrStr << theObject.get();
971 qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
972 qDebug(getResult2AISObjectMapInfo().c_str());
977 //**************************************************************
978 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
980 QStringList aContent;
982 foreach(ObjectPtr aObj, myResult2AISObjectMap.objects()) {
983 AISObjectPtr aAISObj = myResult2AISObjectMap.value(aObj);
985 foreach(ObjectPtr aObj, myResult2AISObjectMap.keys()) {
986 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
988 std::ostringstream aPtrStr;
989 aPtrStr << "aObj = " << aObj.get() << ":";
990 aPtrStr << "anAIS = " << aAISObj.get() << ":";
991 aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
993 aContent.append(aPtrStr.str().c_str());
995 return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
996 arg(aContent.join("\n")).toStdString().c_str();
1000 //**************************************************************
1001 void XGUI_Displayer::getPresentations(const ObjectPtr& theObject,
1002 NCollection_Map<Handle(AIS_InteractiveObject)>& thePresentations)
1004 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1005 if (aResult.get()) {
1006 AISObjectPtr aAISObj = getAISObject(aResult);
1007 if (aAISObj.get() == NULL) {
1008 // if result is a result of a composite feature, it is visualized by visualization of
1009 // composite children, so we should get one of this presentations
1010 ResultBodyPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
1011 if (aCompSolid.get() && aCompSolid->numberOfSubs() > 0) {
1012 aAISObj = getAISObject(aCompSolid->subResult(0));
1015 if (aAISObj.get() != NULL) {
1016 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1017 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1018 thePresentations.Add(anAIS);
1022 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
1023 // find presentation of the feature
1024 AISObjectPtr aAISObj = getAISObject(aFeature);
1025 if (aAISObj.get() != NULL) {
1026 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1027 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1028 thePresentations.Add(anAIS);
1030 // find presentations of the feature results
1031 std::list<ResultPtr> aResults;
1032 ModelAPI_Tools::allResults(aFeature, aResults);
1033 std::list<ResultPtr>::const_iterator anIt = aResults.begin(), aLast = aResults.end();
1034 for (; anIt != aLast; ++anIt) {
1035 AISObjectPtr aAISObj = getAISObject(*anIt);
1036 if (aAISObj.get() != NULL) {
1037 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1038 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1039 thePresentations.Add(anAIS);
1045 //**************************************************************
1046 void XGUI_Displayer::displayTrihedron(bool theToDisplay) const
1048 Handle(AIS_InteractiveContext) aContext = AISContext();
1049 if (aContext.IsNull())
1052 Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
1054 XGUI_SelectionActivate* aSelectionActive = selectionActivate();
1056 if (!aContext->IsDisplayed(aTrihedron))
1057 aContext->Display(aTrihedron,
1059 -1 /* selection mode */,
1060 Standard_True /* update viewer*/,
1061 Standard_False /* allow decomposition */,
1062 AIS_DS_Displayed /* xdisplay status */);
1064 if (getCallBack()) getCallBack()->Display(aTrihedron);
1067 if (!aSelectionActive->isTrihedronActive())
1068 aSelectionActive->deactivateTrihedron(false);
1070 aSelectionActive->activate(aTrihedron, false);
1072 aSelectionActive->deactivateTrihedron(false);
1074 aContext->Erase(aTrihedron, Standard_True);
1076 if (getCallBack()) getCallBack()->Remove(aTrihedron);
1081 //**************************************************************
1082 void XGUI_Displayer::AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
1083 const NCollection_DataMap<TopoDS_Shape,
1084 NCollection_Map<Handle(AIS_InteractiveObject)>>& theShapesToBeSelected)
1086 NCollection_Map<Handle(AIS_InteractiveObject)> aCompsolidPresentations;
1087 NCollection_Map<Handle(AIS_InteractiveObject)> aSelectedPresentations;
1089 NCollection_List<Handle(SelectBasics_EntityOwner)> anActiveOwners;
1090 theContext->MainSelector()->ActiveOwners(anActiveOwners);
1091 NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (anActiveOwners);
1092 Handle(SelectMgr_EntityOwner) anOwner;
1094 /// It is very important to check that the owner is processed only once and has a map of
1095 /// processed owners because SetSelected works as a switch.
1096 /// If count of calls setSelectec is even, the object stays in the previous state
1097 /// (selected, deselected)
1098 /// OCCT: to write about the problem that active owners method returns one owner several times
1099 QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
1100 for (; anOwnersIt.More(); anOwnersIt.Next()) {
1101 anOwner = Handle(SelectMgr_EntityOwner)::DownCast (anOwnersIt.Value());
1102 if (aSelectedIds.contains((size_t)anOwner.get()))
1104 aSelectedIds.append((size_t)anOwner.get());
1106 Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
1107 if (!BROwnr.IsNull() && BROwnr->HasShape()) {
1108 const TopoDS_Shape& aShape = BROwnr->Shape();
1109 if (aShape.IsNull())
1112 Handle(ModuleBase_BRepOwner) aCustomOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
1114 NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)> >
1115 ::Iterator aShapeIt(theShapesToBeSelected);
1116 for (; aShapeIt.More(); aShapeIt.Next()) {
1117 const TopoDS_Shape& aParameterShape = aShapeIt.Key();
1118 // In case of compound we cannot rely on simple comparison method.
1119 // If the compound is generated by Group feature then this compound is alwais new.
1120 // So, we have to compare content of these compounds
1122 // isSame should be used here as it does not check orientation of shapes
1123 // despite on isEqual of shapes or IsBound for shape in QMap. Orientation is
1124 // different for Edges shapes in model shape and owner even if this is the same shape
1125 if (ModuleBase_Tools::isSameShape(aParameterShape, aShape)) {
1126 Handle(AIS_InteractiveObject) anOwnerPresentation =
1127 Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
1128 NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations =
1129 theShapesToBeSelected.Find(aParameterShape);
1130 if (aPresentations.Contains(anOwnerPresentation)) {
1131 theContext->AddOrRemoveSelected(anOwner, Standard_False);
1132 anOwner->SetSelected(Standard_True);
1133 // collect selected presentations to do not select them if compsolid is selected
1134 if (!aSelectedPresentations.Contains(anOwnerPresentation))
1135 aSelectedPresentations.Add(anOwnerPresentation);
1138 else if (!aCustomOwner.IsNull()) { // CompSolid processing #2219
1139 // shape of owner is compound, but shape to be selected is compsolid, so
1140 // we need to compare shape to AIS presentation of owner(rule of the owner creation)
1141 Handle(AIS_Shape) anOwnerPresentation =
1142 Handle(AIS_Shape)::DownCast(anOwner->Selectable());
1143 const TopoDS_Shape& aPresentationShape = anOwnerPresentation->Shape();
1144 if (aParameterShape.IsSame(anOwnerPresentation->Shape()) &&
1145 !aCompsolidPresentations.Contains(anOwnerPresentation))
1146 aCompsolidPresentations.Add(anOwnerPresentation);
1151 // select CompSolid presentations if their owners was not selected yet
1152 NCollection_Map<Handle(AIS_InteractiveObject)>::Iterator anIt (aCompsolidPresentations);
1153 for (; anIt.More(); anIt.Next()) {
1154 if (aSelectedPresentations.Contains(anIt.Value()))
1156 theContext->AddOrRemoveSelected(anIt.Value(), Standard_False);
1160 //**************************************************************
1161 XGUI_SelectionActivate* XGUI_Displayer::selectionActivate() const
1163 return myWorkshop->selectionActivate();
1166 //**************************************************************
1167 GeomPlanePtr XGUI_Displayer::getScreenPlane() const
1169 GeomPlanePtr aResult;
1170 Handle(AIS_InteractiveContext) aContext = AISContext();
1171 if (!aContext.IsNull()) {
1172 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1173 Handle(V3d_View) aView;
1174 for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews()) {
1175 aView = aViewer->ActiveView();
1178 if (!aView.IsNull()) {
1179 double aEyeX, aEyeY, aEyeZ;
1180 aView->Eye(aEyeX, aEyeY, aEyeZ);
1182 double aProjX, aProjY, aProjZ;
1183 aView->Proj(aProjX, aProjY, aProjZ);
1185 GeomPointPtr aPnt = GeomPointPtr(new GeomAPI_Pnt(aEyeX, aEyeY, aEyeZ));
1186 GeomDirPtr aDir = GeomDirPtr(new GeomAPI_Dir(aProjX, aProjY, aProjZ));
1188 aResult = GeomPlanePtr(new GeomAPI_Pln(aPnt, aDir));
1194 double XGUI_Displayer::getViewScale() const
1196 Handle(AIS_InteractiveContext) aContext = AISContext();
1197 if (!aContext.IsNull()) {
1198 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1199 Handle(V3d_View) aView;
1200 for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews()) {
1201 aView = aViewer->ActiveView();
1204 return aView->Camera()->Scale();