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 anObject = myWorkshop->module()->findPresentedObject(anAISObj);
586 //**************************************************************
587 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
589 bool aWasEnabled = isUpdateEnabled();
591 myViewerBlockedRecursiveCount--;
593 myViewerBlockedRecursiveCount++;
595 #ifdef DEBUG_VIEWER_BLOCKED_COUNT
596 std::cout << "myViewerBlockedRecursiveCount = " << myViewerBlockedRecursiveCount << std::endl;
599 if (myNeedUpdate && isUpdateEnabled()) {
601 myNeedUpdate = false;
606 //**************************************************************
607 void XGUI_Displayer::updateViewer() const
609 Handle(AIS_InteractiveContext) aContext = AISContext();
611 #ifdef DEBUG_VIEWER_BLOCKED_COUNT
612 std::cout << "updateViewer: " << (myViewerBlockedRecursiveCount == 0 ? " done" : " later")
616 if (!aContext.IsNull() && isUpdateEnabled()) {
617 //myWorkshop->viewer()->Zfitall();
618 aContext->UpdateCurrentViewer();
624 //**************************************************************
625 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
627 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
628 if (!aContext.IsNull() && (myContextId != aContext.get())) {
629 myContextId = aContext.get();
630 if (!myWorkshop->selectionActivate()->isTrihedronActive())
631 selectionActivate()->deactivateTrihedron(true);
632 // Do not modify default drawer. The same is done in ModuleBase_ResultPrs
633 //aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
634 //aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
636 //Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
637 //aTrihedron->getHighlightPointAspect()->SetScale(2.0);
638 //aTrihedron->getHighlightPointAspect()->SetTypeOfMarker(Aspect_TOM_O_STAR);
640 // Commented out according to discussion in bug #2825
641 ModuleBase_IViewer::DefaultHighlightDrawer = aContext->HighlightStyle();
642 //Handle(Prs3d_Drawer) aSelStyle = aContext->SelectionStyle();
643 //double aDeflection =
644 // QString(ModelAPI_ResultConstruction::DEFAULT_DEFLECTION().c_str()).toDouble();
646 // aDeflection = Config_PropManager::real("Visualization", "construction_deflection");
649 //ModuleBase_IViewer::DefaultHighlightDrawer->SetDeviationCoefficient(aDeflection);
650 //aSelStyle->SetDeviationCoefficient(aDeflection);
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*/, true, 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 //bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
911 // AISObjectPtr anAISObj = getAISObject(theObject);
912 // // correct the result's color it it has the attribute
913 // ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
915 // // Customization of presentation
916 // GeomCustomPrsPtr aCustomPrs;
917 // FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
918 // if (aFeature.get() != NULL) {
919 // GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
920 // if (aCustPrs.get() != NULL)
921 // aCustomPrs = aCustPrs;
923 // if (aCustomPrs.get() == NULL) {
924 // GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
925 // // we ignore presentable not customized objects
926 // if (aPrs.get() == NULL)
927 // aCustomPrs = myCustomPrs;
929 // bool isCustomized = aCustomPrs.get() &&
930 // aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
931 // isCustomized = myWorkshop->module()->afterCustomisePresentation(aResult, anAISObj, myCustomPrs)
934 // // update presentation state if faces panel is active
935 // if (anAISObj.get() && myWorkshop->facesPanel())
936 // isCustomized = myWorkshop->facesPanel()->customizeObject(theObject, anAISObj) || isCustomized;
938 // return isCustomized;
941 //**************************************************************
942 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject,
943 const QColor& theColor,
944 bool theUpdateViewer)
946 if (!isVisible(theObject))
949 AISObjectPtr anAISObj = getAISObject(theObject);
951 anAISObj->getColor(aR, aG, aB);
952 anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
955 return QColor(aR, aG, aB);
958 //**************************************************************
959 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
962 myResult2AISObjectMap.add(theObject, theAIS);
964 myResult2AISObjectMap[theObject] = theAIS;
968 std::ostringstream aPtrStr;
969 aPtrStr << theObject.get();
970 qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
971 qDebug(getResult2AISObjectMapInfo().c_str());
976 //**************************************************************
977 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
979 QStringList aContent;
981 foreach(ObjectPtr aObj, myResult2AISObjectMap.objects()) {
982 AISObjectPtr aAISObj = myResult2AISObjectMap.value(aObj);
984 foreach(ObjectPtr aObj, myResult2AISObjectMap.keys()) {
985 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
987 std::ostringstream aPtrStr;
988 aPtrStr << "aObj = " << aObj.get() << ":";
989 aPtrStr << "anAIS = " << aAISObj.get() << ":";
990 aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
992 aContent.append(aPtrStr.str().c_str());
994 return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
995 arg(aContent.join("\n")).toStdString().c_str();
999 //**************************************************************
1000 void XGUI_Displayer::getPresentations(const ObjectPtr& theObject,
1001 NCollection_Map<Handle(AIS_InteractiveObject)>& thePresentations)
1003 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1004 if (aResult.get()) {
1005 AISObjectPtr aAISObj = getAISObject(aResult);
1006 if (aAISObj.get() == NULL) {
1007 // if result is a result of a composite feature, it is visualized by visualization of
1008 // composite children, so we should get one of this presentations
1009 ResultBodyPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
1010 if (aCompSolid.get() && aCompSolid->numberOfSubs() > 0) {
1011 aAISObj = getAISObject(aCompSolid->subResult(0));
1014 if (aAISObj.get() != NULL) {
1015 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1016 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1017 thePresentations.Add(anAIS);
1021 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
1022 // find presentation of the feature
1023 AISObjectPtr aAISObj = getAISObject(aFeature);
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);
1029 // find presentations of the feature results
1030 std::list<ResultPtr> aResults;
1031 ModelAPI_Tools::allResults(aFeature, aResults);
1032 std::list<ResultPtr>::const_iterator anIt = aResults.begin(), aLast = aResults.end();
1033 for (; anIt != aLast; ++anIt) {
1034 AISObjectPtr aAISObj = getAISObject(*anIt);
1035 if (aAISObj.get() != NULL) {
1036 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1037 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1038 thePresentations.Add(anAIS);
1044 //**************************************************************
1045 void XGUI_Displayer::displayTrihedron(bool theToDisplay) const
1047 Handle(AIS_InteractiveContext) aContext = AISContext();
1048 if (aContext.IsNull())
1051 Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
1053 XGUI_SelectionActivate* aSelectionActive = selectionActivate();
1055 if (!aContext->IsDisplayed(aTrihedron))
1056 aContext->Display(aTrihedron,
1058 -1 /* selection mode */,
1059 Standard_True /* update viewer*/,
1060 Standard_False /* allow decomposition */,
1061 AIS_DS_Displayed /* xdisplay status */);
1063 if (getCallBack()) getCallBack()->Display(aTrihedron);
1066 if (!aSelectionActive->isTrihedronActive())
1067 aSelectionActive->deactivateTrihedron(false);
1069 aSelectionActive->activate(aTrihedron, false);
1071 aSelectionActive->deactivateTrihedron(false);
1073 aContext->Erase(aTrihedron, Standard_True);
1075 if (getCallBack()) getCallBack()->Remove(aTrihedron);
1080 //**************************************************************
1081 void XGUI_Displayer::AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
1082 const NCollection_DataMap<TopoDS_Shape,
1083 NCollection_Map<Handle(AIS_InteractiveObject)>>& theShapesToBeSelected)
1085 NCollection_Map<Handle(AIS_InteractiveObject)> aCompsolidPresentations;
1086 NCollection_Map<Handle(AIS_InteractiveObject)> aSelectedPresentations;
1088 NCollection_List<Handle(SelectBasics_EntityOwner)> anActiveOwners;
1089 theContext->MainSelector()->ActiveOwners(anActiveOwners);
1090 NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (anActiveOwners);
1091 Handle(SelectMgr_EntityOwner) anOwner;
1093 /// It is very important to check that the owner is processed only once and has a map of
1094 /// processed owners because SetSelected works as a switch.
1095 /// If count of calls setSelectec is even, the object stays in the previous state
1096 /// (selected, deselected)
1097 /// OCCT: to write about the problem that active owners method returns one owner several times
1098 QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
1099 for (; anOwnersIt.More(); anOwnersIt.Next()) {
1100 anOwner = Handle(SelectMgr_EntityOwner)::DownCast (anOwnersIt.Value());
1101 if (aSelectedIds.contains((size_t)anOwner.get()))
1103 aSelectedIds.append((size_t)anOwner.get());
1105 Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
1106 if (!BROwnr.IsNull() && BROwnr->HasShape()) {
1107 const TopoDS_Shape& aShape = BROwnr->Shape();
1108 if (aShape.IsNull())
1111 Handle(ModuleBase_BRepOwner) aCustomOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
1113 NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)> >
1114 ::Iterator aShapeIt(theShapesToBeSelected);
1115 for (; aShapeIt.More(); aShapeIt.Next()) {
1116 const TopoDS_Shape& aParameterShape = aShapeIt.Key();
1117 // In case of compound we cannot rely on simple comparison method.
1118 // If the compound is generated by Group feature then this compound is alwais new.
1119 // So, we have to compare content of these compounds
1121 // isSame should be used here as it does not check orientation of shapes
1122 // despite on isEqual of shapes or IsBound for shape in QMap. Orientation is
1123 // different for Edges shapes in model shape and owner even if this is the same shape
1124 if (ModuleBase_Tools::isSameShape(aParameterShape, aShape)) {
1125 Handle(AIS_InteractiveObject) anOwnerPresentation =
1126 Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
1127 NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations =
1128 theShapesToBeSelected.Find(aParameterShape);
1129 if (aPresentations.Contains(anOwnerPresentation)) {
1130 theContext->AddOrRemoveSelected(anOwner, Standard_False);
1131 anOwner->SetSelected(Standard_True);
1132 // collect selected presentations to do not select them if compsolid is selected
1133 if (!aSelectedPresentations.Contains(anOwnerPresentation))
1134 aSelectedPresentations.Add(anOwnerPresentation);
1137 else if (!aCustomOwner.IsNull()) { // CompSolid processing #2219
1138 // shape of owner is compound, but shape to be selected is compsolid, so
1139 // we need to compare shape to AIS presentation of owner(rule of the owner creation)
1140 Handle(AIS_Shape) anOwnerPresentation =
1141 Handle(AIS_Shape)::DownCast(anOwner->Selectable());
1142 const TopoDS_Shape& aPresentationShape = anOwnerPresentation->Shape();
1143 if (aParameterShape.IsSame(anOwnerPresentation->Shape()) &&
1144 !aCompsolidPresentations.Contains(anOwnerPresentation))
1145 aCompsolidPresentations.Add(anOwnerPresentation);
1150 // select CompSolid presentations if their owners was not selected yet
1151 NCollection_Map<Handle(AIS_InteractiveObject)>::Iterator anIt (aCompsolidPresentations);
1152 for (; anIt.More(); anIt.Next()) {
1153 if (aSelectedPresentations.Contains(anIt.Value()))
1155 theContext->AddOrRemoveSelected(anIt.Value(), Standard_False);
1159 //**************************************************************
1160 XGUI_SelectionActivate* XGUI_Displayer::selectionActivate() const
1162 return myWorkshop->selectionActivate();
1165 //**************************************************************
1166 GeomPlanePtr XGUI_Displayer::getScreenPlane() const
1168 GeomPlanePtr aResult;
1169 Handle(AIS_InteractiveContext) aContext = AISContext();
1170 if (!aContext.IsNull()) {
1171 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1172 Handle(V3d_View) aView;
1173 for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews()) {
1174 aView = aViewer->ActiveView();
1177 if (!aView.IsNull()) {
1178 double aEyeX, aEyeY, aEyeZ;
1179 aView->Eye(aEyeX, aEyeY, aEyeZ);
1181 double aProjX, aProjY, aProjZ;
1182 aView->Proj(aProjX, aProjY, aProjZ);
1184 GeomPointPtr aPnt = GeomPointPtr(new GeomAPI_Pnt(aEyeX, aEyeY, aEyeZ));
1185 GeomDirPtr aDir = GeomDirPtr(new GeomAPI_Dir(aProjX, aProjY, aProjZ));
1187 aResult = GeomPlanePtr(new GeomAPI_Pln(aPnt, aDir));
1193 double XGUI_Displayer::getViewScale() const
1195 Handle(AIS_InteractiveContext) aContext = AISContext();
1196 if (!aContext.IsNull()) {
1197 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1198 Handle(V3d_View) aView;
1199 for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews()) {
1200 aView = aViewer->ActiveView();
1203 return aView->Camera()->Scale();