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>
82 #include <inspector/VInspectorAPI_CallBack.hxx>
85 #include <Events_Loop.h>
86 #include <ModelAPI_Events.h>
87 #include <Config_PropManager.h>
91 /// defines the local context mouse selection sensitivity
92 const int MOUSE_SENSITIVITY_IN_PIXEL = 10;
94 //#define DEBUG_DISPLAY
95 //#define DEBUG_FEATURE_REDISPLAY
96 //#define DEBUG_SELECTION_FILTERS
98 //#define DEBUG_OCCT_SHAPE_SELECTION
100 #define CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
102 //#define DEBUG_VIEWER_BLOCKED_COUNT
104 //**************************************************************
105 void displayedObjects(const Handle(AIS_InteractiveContext)& theAIS, AIS_ListOfInteractive& theList)
107 // Get from null point
108 #if OCC_VERSION_HEX < 0x070400
109 theAIS->DisplayedObjects(theList, true);
111 theAIS->DisplayedObjects(theList);
115 QString qIntListInfo(const QIntList& theValues, const QString& theSeparator = QString(", "))
118 QIntList::const_iterator anIt = theValues.begin(), aLast = theValues.end();
119 for (; anIt != aLast; anIt++) {
120 anInfo.append(QString::number(*anIt));
122 return anInfo.join(theSeparator);
125 //**************************************************************
126 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
127 : myWorkshop(theWorkshop), myNeedUpdate(false),
128 myViewerBlockedRecursiveCount(0), myContextId(0)
132 //**************************************************************
133 XGUI_Displayer::~XGUI_Displayer()
137 //**************************************************************
138 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
140 return myResult2AISObjectMap.contains(theObject);
143 //**************************************************************
144 bool XGUI_Displayer::display(ObjectPtr theObject, bool theUpdateViewer)
146 bool aDisplayed = false;
147 if (isVisible(theObject)) {
148 aDisplayed = redisplay(theObject, theUpdateViewer);
151 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
152 bool isShading = false;
153 if (aPrs.get() != NULL) {
154 GeomScreenParamsPtr aScreen = std::dynamic_pointer_cast<GeomAPI_IScreenParams>(theObject);
156 aScreen->setScreenPlane(getScreenPlane());
157 aScreen->setViewScale(getViewScale());
159 anAIS = aPrs->getAISObject(anAIS);
161 anAIS = myWorkshop->module()->createPresentation(theObject);
165 aDisplayed = display(theObject, anAIS, isShading, theUpdateViewer);
171 //**************************************************************
172 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS, ModuleBase_IModule* theModule)
174 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
175 if (!aShapePrs.IsNull()) {
176 TopoDS_Shape aShape = aShapePrs->Shape();
179 TopAbs_ShapeEnum aType = aShape.ShapeType();
180 if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
183 // Check that the presentation is not a sketch
184 return theModule->canBeShaded(theAIS);
190 //**************************************************************
191 bool XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
192 bool isShading, bool theUpdateViewer)
194 bool aDisplayed = false;
196 Handle(AIS_InteractiveContext) aContext = AISContext();
197 if (aContext.IsNull())
200 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
201 if (!anAISIO.IsNull()) {
202 appendResultObject(theObject, theAIS);
204 //bool isCustomized = customizeObject(theObject);
206 int aDispMode = isShading? Shading : Wireframe;
207 anAISIO->SetDisplayMode(aDispMode);
208 aContext->Display(anAISIO, aDispMode, 0, false, true, AIS_DS_Displayed);
210 if (getCallBack()) getCallBack()->Display(anAISIO);
214 emit objectDisplayed(theObject, theAIS);
215 selectionActivate()->activate(anAISIO, theUpdateViewer);
223 //**************************************************************
224 bool XGUI_Displayer::erase(ObjectPtr theObject, const bool theUpdateViewer)
226 bool aErased = false;
227 if (!isVisible(theObject))
230 Handle(AIS_InteractiveContext) aContext = AISContext();
231 if (aContext.IsNull())
234 AISObjectPtr anObject = myResult2AISObjectMap.value(theObject);
236 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
237 if (!anAIS.IsNull()) {
238 emit beforeObjectErase(theObject, anObject);
239 aContext->Remove(anAIS, false/*update viewer*/);
241 if (getCallBack()) getCallBack()->Remove(anAIS);
246 myResult2AISObjectMap.remove(theObject);
249 std::ostringstream aPtrStr;
250 aPtrStr << theObject.get();
251 qDebug(QString("erase object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
252 qDebug(getResult2AISObjectMapInfo().c_str());
261 //**************************************************************
262 bool XGUI_Displayer::redisplay(ObjectPtr theObject, bool theUpdateViewer)
264 bool aRedisplayed = false;
265 Handle(AIS_InteractiveContext) aContext = AISContext();
266 if (aContext.IsNull())
269 if (!isVisible(theObject))
272 AISObjectPtr aAISObj = getAISObject(theObject);
273 Handle(AIS_InteractiveObject) aAISIO;
275 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
277 GeomScreenParamsPtr aScreen = std::dynamic_pointer_cast<GeomAPI_IScreenParams>(theObject);
279 aScreen->setScreenPlane(getScreenPlane());
280 aScreen->setViewScale(getViewScale());
282 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
284 aRedisplayed = erase(theObject, theUpdateViewer);
287 if (aAIS_Obj != aAISObj) {
288 erase(theObject, theUpdateViewer);
289 appendResultObject(theObject, aAIS_Obj);
291 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
294 aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
297 if (!aAISIO.IsNull()) {
298 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
301 std::vector<int> aColor;
302 ModelAPI_Tools::getColor(aResult, aColor);
303 if (aColor.size() > 0) {
305 aCol(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB);
306 aAISIO->SetColor(aCol);
309 double aDeflection = ModelAPI_Tools::getDeflection(aResult);
310 if ((aDeflection >= 0) && (aDeflection != aAISObj->getDeflection()))
311 aAISObj->setDeflection(aDeflection);
314 double aTransparency = ModelAPI_Tools::getTransparency(aResult);
315 if ((aTransparency >= 0) && (aTransparency != aAISObj->getTransparency()))
316 aAISObj->setTransparency(aTransparency);
318 myWorkshop->module()->storeSelection();
320 #ifdef CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
321 myWorkshop->selector()->deselectPresentation(aAISIO);
324 if (aContext->IsDisplayed(aAISIO))
325 aContext->Redisplay(aAISIO, false);
327 aContext->Display(aAISIO, false);
330 if (getCallBack()) getCallBack()->Redisplay(aAISIO);
333 //if (aNeedToRestoreSelection)
334 myWorkshop->module()->restoreSelection();
337 #ifdef DEBUG_FEATURE_REDISPLAY
338 qDebug(" Redisplay happens");
346 //**************************************************************
347 void XGUI_Displayer::redisplayObjects()
349 // redisplay objects visualized in the viewer
350 static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
351 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
352 QObjectPtrList aDisplayed = displayedObjects();
353 QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
354 for (; anIt != aLast; anIt++) {
355 aECreator->sendUpdated(*anIt, EVENT_DISP);
357 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
358 if (aViewer->isColorScaleVisible()) {
359 aViewer->setupColorScale();
360 aViewer->setColorScaleShown(true);
362 Events_Loop::loop()->flush(EVENT_DISP);
365 //**************************************************************
366 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
367 const bool theUpdateViewer)
369 //Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
370 //if (!aTrihedron.IsNull())
371 // deactivateAIS(aTrihedron);
373 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
374 for (; anIt != aLast; anIt++) {
375 selectionActivate()->deactivate(*anIt, false);
377 //VSV It seems that there is no necessity to update viewer on deactivation
378 //if (theUpdateViewer)
382 //**************************************************************
383 bool XGUI_Displayer::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
385 bool aVisible = false;
386 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
387 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
388 if (aPrs.get() || aResult.get()) {
389 aVisible = theDisplayer->isVisible(theObject);
390 // compsolid is not visualized in the viewer,
391 // but should have presentation when all sub solids are
392 // visible. It is useful for highlight presentation where compsolid shape is selectable
393 if (!aVisible && aResult.get() && aResult->groupName() == ModelAPI_ResultBody::group()) {
394 ResultBodyPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
395 if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
396 int aNumberOfSubs = aCompsolidResult->numberOfSubs();
397 bool anAllSubsVisible = aNumberOfSubs > 0;
398 for(int i = 0; i < aNumberOfSubs && anAllSubsVisible; i++) {
399 anAllSubsVisible = theDisplayer->isVisible(aCompsolidResult->subResult(i));
401 aVisible = anAllSubsVisible;
405 // it is possible that feature is presentable and has results, so we should check visibility
406 // of results if presentation is not shown (e.g. Sketch Circle/Arc features)
408 // check if all results of the feature are visible
409 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
410 std::list<ResultPtr> aResults;
411 ModelAPI_Tools::allResults(aFeature, aResults);
412 std::list<ResultPtr>::const_iterator aIt;
413 aVisible = !aResults.empty();
414 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
415 aVisible = aVisible && theDisplayer->isVisible(*aIt);
421 //**************************************************************
422 void XGUI_Displayer::setSelected(const QList<ModuleBase_ViewerPrsPtr>& theValues,
423 bool theUpdateViewer)
425 Handle(AIS_InteractiveContext) aContext = AISContext();
426 if (aContext.IsNull())
428 aContext->UnhilightSelected(false);
429 aContext->ClearSelected(false);
431 if (getCallBack()) getCallBack()->ClearSelected();
433 NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)>>
436 foreach (ModuleBase_ViewerPrsPtr aPrs, theValues) {
437 const GeomShapePtr& aGeomShape = aPrs->shape();
438 if (aGeomShape.get() && !aGeomShape->isNull()) {
439 const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
440 #ifdef DEBUG_OCCT_SHAPE_SELECTION
441 // problem 1: performance
442 // problem 2: IO is not specified, so the first found owner is selected, as a result
443 // it might belong to another result
444 aContext->AddOrRemoveSelected(aShape, false);
446 if (getCallBack()) getCallBack()->AddOrRemoveSelected(aShape);
449 NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations;
450 if (aShapesToBeSelected.IsBound(aShape))
451 aPresentations = aShapesToBeSelected.Find(aShape);
452 ObjectPtr anObject = aPrs->object();
453 getPresentations(anObject, aPresentations);
455 aShapesToBeSelected.Bind(aShape, aPresentations);
458 ObjectPtr anObject = aPrs->object();
459 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
460 if (aResult.get() && isVisible(aResult)) {
461 AISObjectPtr anObj = myResult2AISObjectMap.value(aResult);
462 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
463 if (!anAIS.IsNull()) {
464 // The methods are replaced in order to provide multi-selection, e.g. restore selection
465 // by activating multi selector widget. It also gives an advantage that the multi
466 // selection in OB gives multi-selection in the viewer
467 //aContext->SetSelected(anAIS, false);
468 // The selection in the context was cleared, so the method sets the objects are selected
469 aContext->AddOrRemoveSelected(anAIS, false);
471 if (getCallBack()) getCallBack()->AddOrRemoveSelected(anAIS);
477 if (!aShapesToBeSelected.IsEmpty())
478 XGUI_Displayer::AddOrRemoveSelectedShapes(aContext, aShapesToBeSelected);
484 //**************************************************************
485 void XGUI_Displayer::clearSelected(const bool theUpdateViewer)
487 Handle(AIS_InteractiveContext) aContext = AISContext();
488 if (!aContext.IsNull()) {
489 aContext->UnhilightSelected(false);//UnhilightCurrents(false);
490 aContext->ClearSelected(theUpdateViewer);
492 if (getCallBack()) getCallBack()->ClearSelected();
497 //**************************************************************
498 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
500 bool aErased = false;
501 Handle(AIS_InteractiveContext) aContext = AISContext();
502 if (!aContext.IsNull()) {
504 foreach(ObjectPtr aObj, myResult2AISObjectMap.objects()) {
505 AISObjectPtr aAISObj = myResult2AISObjectMap.value(aObj);
507 foreach(ObjectPtr aObj, myResult2AISObjectMap.keys()) {
508 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
511 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
512 if (!anIO.IsNull()) {
513 emit beforeObjectErase(aObj, aAISObj);
514 aContext->Remove(anIO, false/*update viewer*/);
516 if (getCallBack()) getCallBack()->Remove(anIO);
524 myResult2AISObjectMap.clear();
527 qDebug(getResult2AISObjectMapInfo().c_str());
533 //**************************************************************
534 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
537 return myResult2AISObjectMap.value(theObject);
540 if (myResult2AISObjectMap.contains(theObject))
541 anIO = myResult2AISObjectMap[theObject];
546 //**************************************************************
547 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
549 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
550 return getObject(aRefAIS);
553 //**************************************************************
554 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
557 ObjectPtr anObject = myResult2AISObjectMap.value(theIO);
560 ResultToAISMap::const_iterator aMapIter = myResult2AISObjectMap.cbegin();
561 for (; aMapIter != myResult2AISObjectMap.cend(); aMapIter++) {
562 const AISObjectPtr& aAIS = aMapIter.value();
563 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
565 anObject = aMapIter.key();
570 if (!anObject.get()) {
571 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
572 if (!theIO.IsNull()) {
573 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
575 anObject = myWorkshop->module()->findPresentedObject(anAISObj);
580 //**************************************************************
581 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
583 bool aWasEnabled = isUpdateEnabled();
585 myViewerBlockedRecursiveCount--;
587 myViewerBlockedRecursiveCount++;
589 #ifdef DEBUG_VIEWER_BLOCKED_COUNT
590 std::cout << "myViewerBlockedRecursiveCount = " << myViewerBlockedRecursiveCount << std::endl;
593 if (myNeedUpdate && isUpdateEnabled()) {
595 myNeedUpdate = false;
600 //**************************************************************
601 void XGUI_Displayer::updateViewer() const
603 Handle(AIS_InteractiveContext) aContext = AISContext();
605 #ifdef DEBUG_VIEWER_BLOCKED_COUNT
606 std::cout << "updateViewer: " << (myViewerBlockedRecursiveCount == 0 ? " done" : " later")
610 if (!aContext.IsNull() && isUpdateEnabled()) {
611 //myWorkshop->viewer()->Zfitall();
612 aContext->UpdateCurrentViewer();
618 //**************************************************************
619 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
621 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
622 if (!aContext.IsNull() && (myContextId != aContext.get())) {
623 myContextId = aContext.get();
624 if (!myWorkshop->selectionActivate()->isTrihedronActive())
625 selectionActivate()->deactivateTrihedron(true);
626 // Do not modify default drawer. The same is done in ModuleBase_ResultPrs
627 //aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
628 //aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
630 //Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
631 //aTrihedron->getHighlightPointAspect()->SetScale(2.0);
632 //aTrihedron->getHighlightPointAspect()->SetTypeOfMarker(Aspect_TOM_O_STAR);
634 // Commented out according to discussion in bug #2825
635 ModuleBase_IViewer::DefaultHighlightDrawer = aContext->HighlightStyle();
636 //Handle(Prs3d_Drawer) aSelStyle = aContext->SelectionStyle();
637 //double aDeflection =
638 // QString(ModelAPI_ResultConstruction::DEFAULT_DEFLECTION().c_str()).toDouble();
640 // aDeflection = Config_PropManager::real("Visualization", "construction_deflection");
643 //ModuleBase_IViewer::DefaultHighlightDrawer->SetDeviationCoefficient(aDeflection);
644 //aSelStyle->SetDeviationCoefficient(aDeflection);
649 //**************************************************************
650 void XGUI_Displayer::setSelectionColor(const std::vector<int>& theColor)
652 Handle(AIS_InteractiveContext) aContext = AISContext();
653 if (!aContext.IsNull()) {
654 Quantity_Color aQColor(theColor[0] / 255.,
656 theColor[2] / 255., Quantity_TOC_RGB);
657 aContext->SelectionStyle()->SetColor(aQColor);
658 aContext->SelectionStyle()->PointAspect()->SetColor(aQColor);
659 aContext->SelectionStyle()->LineAspect()->SetColor(aQColor);
660 aContext->HighlightStyle(Prs3d_TypeOfHighlight_LocalSelected)->SetColor(aQColor);
665 //**************************************************************
666 std::vector<int> XGUI_Displayer::selectionColor() const
668 std::vector<int> aColor;
669 Handle(AIS_InteractiveContext) aContext = AISContext();
670 if (!aContext.IsNull()) {
671 Quantity_Color aQColor = aContext->SelectionStyle()->Color();
672 aColor.push_back((int)(aQColor.Red() * 255));
673 aColor.push_back((int)(aQColor.Green() * 255));
674 aColor.push_back((int)(aQColor.Blue() * 255));
680 //**************************************************************
681 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
683 Handle(AIS_InteractiveContext) aContext = AISContext();
684 if (!aContext.IsNull() && myAndFilter.IsNull()) {
685 myAndFilter = new SelectMgr_AndFilter();
686 aContext->AddFilter(myAndFilter);
691 //**************************************************************
692 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
693 const Standard_Integer theDisplayMode, bool theUpdateViewer)
695 bool aDisplayed = false;
696 Handle(AIS_InteractiveContext) aContext = AISContext();
697 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
698 if (!aContext.IsNull() && !anAISIO.IsNull()) {
699 aContext->Display(anAISIO, theDisplayMode, 0, false/*update viewer*/, true, AIS_DS_Displayed);
701 if (getCallBack()) getCallBack()->Display(anAISIO);
704 aContext->Deactivate(anAISIO);
706 if (getCallBack()) getCallBack()->Deactivate(anAISIO);
708 aContext->Load(anAISIO);
710 if (getCallBack()) getCallBack()->Load(anAISIO);
712 if (toActivateInSelectionModes)
713 myWorkshop->selectionActivate()->activateOnDisplay(anAISIO, theUpdateViewer);
721 //**************************************************************
722 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
724 bool aErased = false;
725 Handle(AIS_InteractiveContext) aContext = AISContext();
726 if (!aContext.IsNull()) {
727 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
728 if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
729 aContext->Remove(anAISIO, false/*update viewer*/);
731 if (getCallBack()) getCallBack()->Remove(anAISIO);
736 if (aErased && theUpdateViewer)
741 //**************************************************************
742 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
744 if (theMode == NoMode)
747 Handle(AIS_InteractiveContext) aContext = AISContext();
748 if (aContext.IsNull())
751 AISObjectPtr aAISObj = getAISObject(theObject);
755 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
756 aContext->SetDisplayMode(aAISIO, theMode, false);
757 // Redisplay in order to update new mode because it could be not computed before
762 //**************************************************************
763 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
765 Handle(AIS_InteractiveContext) aContext = AISContext();
766 if (aContext.IsNull())
769 AISObjectPtr aAISObj = getAISObject(theObject);
773 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
774 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
777 //**************************************************************
778 void XGUI_Displayer::deactivateSelectionFilters(const bool theAddFilterOnly)
780 Handle(AIS_InteractiveContext) aContext = AISContext();
781 if (!myAndFilter.IsNull()) {
783 if (!aContext.IsNull()) {
784 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
785 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
786 for (; anIt.More() && !aFound; anIt.Next()) {
787 Handle(SelectMgr_Filter) aFilter = anIt.Value();
788 aFound = aFilter == myAndFilter;
791 aContext->RemoveFilter(myAndFilter);
793 myAndFilter.Nullify();
797 //**************************************************************
798 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
800 Handle(AIS_InteractiveContext) aContext = AISContext();
801 if (aContext.IsNull() || hasSelectionFilter(theFilter))
804 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
805 if (!aCompositeFilter.IsNull()) {
806 aCompositeFilter->Add(theFilter);
807 #ifdef DEBUG_SELECTION_FILTERS
808 int aCount = aCompositeFilter->StoredFilters().Extent();
809 qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
814 //**************************************************************
815 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
817 Handle(AIS_InteractiveContext) aContext = AISContext();
818 if (aContext.IsNull())
821 Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
822 if (!aCompositeFilter.IsNull() && aCompositeFilter->IsIn(theFilter)) {
823 aCompositeFilter->Remove(theFilter);
824 #ifdef DEBUG_SELECTION_FILTERS
825 int aCount = aCompositeFilter->StoredFilters().Extent();
826 qDebug(QString("removeSelectionFilter: filters.count() = %1")
827 .arg(aCount).toStdString().c_str());
832 //**************************************************************
833 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
835 bool aFilterFound = false;
837 Handle(AIS_InteractiveContext) aContext = AISContext();
838 if (aContext.IsNull())
840 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
841 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
842 for (; aIt.More() && !aFilterFound; aIt.Next()) {
843 if (theFilter.get() == aIt.Value().get())
846 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
847 if (!aCompositeFilter.IsNull()) {
848 const SelectMgr_ListOfFilter& aStoredFilters = aCompositeFilter->StoredFilters();
849 for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
850 if (theFilter.get() == aIt.Value().get())
857 //**************************************************************
858 void XGUI_Displayer::removeFilters()
860 Handle(AIS_InteractiveContext) aContext = AISContext();
861 if (aContext.IsNull())
864 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
865 if (!aCompositeFilter.IsNull())
866 aCompositeFilter->Clear();
869 //**************************************************************
870 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
873 QObjectPtrList aDispList = myResult2AISObjectMap.objects();
875 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
877 foreach(ObjectPtr aObj, aDispList) {
878 if (!theList.contains(aObj))
881 foreach(ObjectPtr aObj, theList) {
882 if (!isVisible(aObj))
883 display(aObj, false);
888 //**************************************************************
889 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
891 if (!isVisible(theObject))
894 AISObjectPtr aAISObj = getAISObject(theObject);
895 if (aAISObj.get() == NULL)
898 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
899 return ::canBeShaded(anAIS, myWorkshop->module());
902 //**************************************************************
903 //bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
905 // AISObjectPtr anAISObj = getAISObject(theObject);
906 // // correct the result's color it it has the attribute
907 // ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
909 // // Customization of presentation
910 // GeomCustomPrsPtr aCustomPrs;
911 // FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
912 // if (aFeature.get() != NULL) {
913 // GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
914 // if (aCustPrs.get() != NULL)
915 // aCustomPrs = aCustPrs;
917 // if (aCustomPrs.get() == NULL) {
918 // GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
919 // // we ignore presentable not customized objects
920 // if (aPrs.get() == NULL)
921 // aCustomPrs = myCustomPrs;
923 // bool isCustomized = aCustomPrs.get() &&
924 // aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
925 // isCustomized = myWorkshop->module()->afterCustomisePresentation(aResult, anAISObj, myCustomPrs)
928 // // update presentation state if faces panel is active
929 // if (anAISObj.get() && myWorkshop->facesPanel())
930 // isCustomized = myWorkshop->facesPanel()->customizeObject(theObject, anAISObj) || isCustomized;
932 // return isCustomized;
935 //**************************************************************
936 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject,
937 const QColor& theColor,
938 bool theUpdateViewer)
940 if (!isVisible(theObject))
943 AISObjectPtr anAISObj = getAISObject(theObject);
945 anAISObj->getColor(aR, aG, aB);
946 anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
949 return QColor(aR, aG, aB);
952 //**************************************************************
953 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
956 myResult2AISObjectMap.add(theObject, theAIS);
958 myResult2AISObjectMap[theObject] = theAIS;
962 std::ostringstream aPtrStr;
963 aPtrStr << theObject.get();
964 qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
965 qDebug(getResult2AISObjectMapInfo().c_str());
970 //**************************************************************
971 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
973 QStringList aContent;
975 foreach(ObjectPtr aObj, myResult2AISObjectMap.objects()) {
976 AISObjectPtr aAISObj = myResult2AISObjectMap.value(aObj);
978 foreach(ObjectPtr aObj, myResult2AISObjectMap.keys()) {
979 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
981 std::ostringstream aPtrStr;
982 aPtrStr << "aObj = " << aObj.get() << ":";
983 aPtrStr << "anAIS = " << aAISObj.get() << ":";
984 aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
986 aContent.append(aPtrStr.str().c_str());
988 return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
989 arg(aContent.join("\n")).toStdString().c_str();
993 //**************************************************************
994 void XGUI_Displayer::getPresentations(const ObjectPtr& theObject,
995 NCollection_Map<Handle(AIS_InteractiveObject)>& thePresentations)
997 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
999 AISObjectPtr aAISObj = getAISObject(aResult);
1000 if (aAISObj.get() == NULL) {
1001 // if result is a result of a composite feature, it is visualized by visualization of
1002 // composite children, so we should get one of this presentations
1003 ResultBodyPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
1004 if (aCompSolid.get() && aCompSolid->numberOfSubs() > 0) {
1005 aAISObj = getAISObject(aCompSolid->subResult(0));
1008 if (aAISObj.get() != NULL) {
1009 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1010 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1011 thePresentations.Add(anAIS);
1015 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
1016 // find presentation of the feature
1017 AISObjectPtr aAISObj = getAISObject(aFeature);
1018 if (aAISObj.get() != NULL) {
1019 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1020 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1021 thePresentations.Add(anAIS);
1023 // find presentations of the feature results
1024 std::list<ResultPtr> aResults;
1025 ModelAPI_Tools::allResults(aFeature, aResults);
1026 std::list<ResultPtr>::const_iterator anIt = aResults.begin(), aLast = aResults.end();
1027 for (; anIt != aLast; ++anIt) {
1028 AISObjectPtr aAISObj = getAISObject(*anIt);
1029 if (aAISObj.get() != NULL) {
1030 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1031 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1032 thePresentations.Add(anAIS);
1038 //**************************************************************
1039 void XGUI_Displayer::displayTrihedron(bool theToDisplay) const
1041 Handle(AIS_InteractiveContext) aContext = AISContext();
1042 if (aContext.IsNull())
1045 Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
1047 XGUI_SelectionActivate* aSelectionActive = selectionActivate();
1049 if (!aContext->IsDisplayed(aTrihedron))
1050 aContext->Display(aTrihedron,
1052 -1 /* selection mode */,
1053 Standard_True /* update viewer*/,
1054 Standard_False /* allow decomposition */,
1055 AIS_DS_Displayed /* xdisplay status */);
1057 if (getCallBack()) getCallBack()->Display(aTrihedron);
1060 if (!aSelectionActive->isTrihedronActive())
1061 aSelectionActive->deactivateTrihedron(false);
1063 aSelectionActive->activate(aTrihedron, false);
1065 aSelectionActive->deactivateTrihedron(false);
1067 aContext->Erase(aTrihedron, Standard_True);
1069 if (getCallBack()) getCallBack()->Remove(aTrihedron);
1074 //**************************************************************
1075 void XGUI_Displayer::AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
1076 const NCollection_DataMap<TopoDS_Shape,
1077 NCollection_Map<Handle(AIS_InteractiveObject)>>& theShapesToBeSelected)
1079 NCollection_Map<Handle(AIS_InteractiveObject)> aCompsolidPresentations;
1080 NCollection_Map<Handle(AIS_InteractiveObject)> aSelectedPresentations;
1082 NCollection_List<Handle(SelectBasics_EntityOwner)> anActiveOwners;
1083 theContext->MainSelector()->ActiveOwners(anActiveOwners);
1084 NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (anActiveOwners);
1085 Handle(SelectMgr_EntityOwner) anOwner;
1087 /// It is very important to check that the owner is processed only once and has a map of
1088 /// processed owners because SetSelected works as a switch.
1089 /// If count of calls setSelectec is even, the object stays in the previous state
1090 /// (selected, deselected)
1091 /// OCCT: to write about the problem that active owners method returns one owner several times
1092 QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
1093 for (; anOwnersIt.More(); anOwnersIt.Next()) {
1094 anOwner = Handle(SelectMgr_EntityOwner)::DownCast (anOwnersIt.Value());
1095 if (aSelectedIds.contains((size_t)anOwner.get()))
1097 aSelectedIds.append((size_t)anOwner.get());
1099 Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
1100 if (!BROwnr.IsNull() && BROwnr->HasShape()) {
1101 const TopoDS_Shape& aShape = BROwnr->Shape();
1102 if (aShape.IsNull())
1105 Handle(ModuleBase_BRepOwner) aCustomOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
1107 NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)> >
1108 ::Iterator aShapeIt(theShapesToBeSelected);
1109 for (; aShapeIt.More(); aShapeIt.Next()) {
1110 const TopoDS_Shape& aParameterShape = aShapeIt.Key();
1111 // In case of compound we cannot rely on simple comparison method.
1112 // If the compound is generated by Group feature then this compound is alwais new.
1113 // So, we have to compare content of these compounds
1115 // isSame should be used here as it does not check orientation of shapes
1116 // despite on isEqual of shapes or IsBound for shape in QMap. Orientation is
1117 // different for Edges shapes in model shape and owner even if this is the same shape
1118 if (ModuleBase_Tools::isSameShape(aParameterShape, aShape)) {
1119 Handle(AIS_InteractiveObject) anOwnerPresentation =
1120 Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
1121 NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations =
1122 theShapesToBeSelected.Find(aParameterShape);
1123 if (aPresentations.Contains(anOwnerPresentation)) {
1124 theContext->AddOrRemoveSelected(anOwner, Standard_False);
1125 anOwner->SetSelected(Standard_True);
1126 // collect selected presentations to do not select them if compsolid is selected
1127 if (!aSelectedPresentations.Contains(anOwnerPresentation))
1128 aSelectedPresentations.Add(anOwnerPresentation);
1131 else if (!aCustomOwner.IsNull()) { // CompSolid processing #2219
1132 // shape of owner is compound, but shape to be selected is compsolid, so
1133 // we need to compare shape to AIS presentation of owner(rule of the owner creation)
1134 Handle(AIS_Shape) anOwnerPresentation =
1135 Handle(AIS_Shape)::DownCast(anOwner->Selectable());
1136 const TopoDS_Shape& aPresentationShape = anOwnerPresentation->Shape();
1137 if (aParameterShape.IsSame(anOwnerPresentation->Shape()) &&
1138 !aCompsolidPresentations.Contains(anOwnerPresentation))
1139 aCompsolidPresentations.Add(anOwnerPresentation);
1144 // select CompSolid presentations if their owners was not selected yet
1145 NCollection_Map<Handle(AIS_InteractiveObject)>::Iterator anIt (aCompsolidPresentations);
1146 for (; anIt.More(); anIt.Next()) {
1147 if (aSelectedPresentations.Contains(anIt.Value()))
1149 theContext->AddOrRemoveSelected(anIt.Value(), Standard_False);
1153 //**************************************************************
1154 XGUI_SelectionActivate* XGUI_Displayer::selectionActivate() const
1156 return myWorkshop->selectionActivate();
1159 //**************************************************************
1160 GeomPlanePtr XGUI_Displayer::getScreenPlane() const
1162 GeomPlanePtr aResult;
1163 Handle(AIS_InteractiveContext) aContext = AISContext();
1164 if (!aContext.IsNull()) {
1165 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1166 Handle(V3d_View) aView;
1167 for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews()) {
1168 aView = aViewer->ActiveView();
1171 if (!aView.IsNull()) {
1172 double aEyeX, aEyeY, aEyeZ;
1173 aView->Eye(aEyeX, aEyeY, aEyeZ);
1175 double aProjX, aProjY, aProjZ;
1176 aView->Proj(aProjX, aProjY, aProjZ);
1178 GeomPointPtr aPnt = GeomPointPtr(new GeomAPI_Pnt(aEyeX, aEyeY, aEyeZ));
1179 GeomDirPtr aDir = GeomDirPtr(new GeomAPI_Dir(aProjX, aProjY, aProjZ));
1181 aResult = GeomPlanePtr(new GeomAPI_Pln(aPnt, aDir));
1187 double XGUI_Displayer::getViewScale() const
1189 Handle(AIS_InteractiveContext) aContext = AISContext();
1190 if (!aContext.IsNull()) {
1191 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1192 Handle(V3d_View) aView;
1193 for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews()) {
1194 aView = aViewer->ActiveView();
1197 return aView->Camera()->Scale();