1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "XGUI_Displayer.h"
23 #include "XGUI_CustomPrs.h"
24 #include "XGUI_FacesPanel.h"
25 #include "XGUI_Selection.h"
26 #include "XGUI_SelectionActivate.h"
27 #include "XGUI_SelectionMgr.h"
28 #include "XGUI_ViewerProxy.h"
29 #include "XGUI_Workshop.h"
32 #include <AppElements_Viewer.h>
35 #include <ModelAPI_Document.h>
36 #include <ModelAPI_Data.h>
37 #include <ModelAPI_Object.h>
38 #include <ModelAPI_Tools.h>
39 #include <ModelAPI_AttributeIntArray.h>
40 #include <ModelAPI_ResultBody.h>
42 #include <ModuleBase_BRepOwner.h>
43 #include <ModuleBase_IModule.h>
44 #include <ModuleBase_Preferences.h>
45 #include <ModuleBase_ResultPrs.h>
46 #include <ModuleBase_Tools.h>
47 #include <ModuleBase_ViewerPrs.h>
48 #include <ModuleBase_IViewer.h>
50 #include <GeomAPI_Shape.h>
51 #include <GeomAPI_IPresentable.h>
52 #include <GeomAPI_ICustomPrs.h>
53 #include <GeomAPI_Pnt.h>
54 #include <GeomAPI_IScreenParams.h>
56 #include <SUIT_ResourceMgr.h>
58 #include <AIS_InteractiveContext.hxx>
59 #include <AIS_ListOfInteractive.hxx>
60 #include <AIS_ListIteratorOfListOfInteractive.hxx>
61 #include <AIS_DimensionSelectionMode.hxx>
62 #include <AIS_Shape.hxx>
63 #include <AIS_Dimension.hxx>
64 #include <AIS_Trihedron.hxx>
65 #ifdef BEFORE_TRIHEDRON_PATCH
66 #include <AIS_Axis.hxx>
67 #include <AIS_Plane.hxx>
68 #include <AIS_Point.hxx>
70 #include <AIS_Selection.hxx>
71 #include <Prs3d_Drawer.hxx>
72 #include <Prs3d_IsoAspect.hxx>
73 #include <SelectMgr_ListOfFilter.hxx>
74 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
75 #include <SelectMgr_SelectionManager.hxx>
76 #include <TColStd_ListIteratorOfListOfInteger.hxx>
78 #include <StdSelect_ViewerSelector3d.hxx>
80 #include <TColStd_MapOfTransient.hxx>
81 #include <TColStd_MapIteratorOfMapOfTransient.hxx>
84 #include <inspector/VInspectorAPI_CallBack.hxx>
87 #include <Events_Loop.h>
88 #include <ModelAPI_Events.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_COMPOSILID_DISPLAY
101 //#define DEBUG_OCCT_SHAPE_SELECTION
103 #define CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
105 //#define DEBUG_VIEWER_BLOCKED_COUNT
107 //**************************************************************
108 void displayedObjects(const Handle(AIS_InteractiveContext)& theAIS, AIS_ListOfInteractive& theList)
110 // Get from null point
111 theAIS->DisplayedObjects(theList, true);
114 QString qIntListInfo(const QIntList& theValues, const QString& theSeparator = QString(", "))
117 QIntList::const_iterator anIt = theValues.begin(), aLast = theValues.end();
118 for (; anIt != aLast; anIt++) {
119 anInfo.append(QString::number(*anIt));
121 return anInfo.join(theSeparator);
124 //**************************************************************
125 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
126 : myWorkshop(theWorkshop), myNeedUpdate(false),
127 myViewerBlockedRecursiveCount(0), myIsFirstAISContextUse(true)
129 myCustomPrs = std::shared_ptr<GeomAPI_ICustomPrs>(new XGUI_CustomPrs(theWorkshop));
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 #ifdef DEBUG_COMPOSILID_DISPLAY
149 ResultCompSolidPtr aCompsolidResult =
150 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
151 if (aCompsolidResult.get()) {
152 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
153 ResultPtr aSubResult = aCompsolidResult->subResult(i);
154 if (aSubResult.get())
155 redisplay(aSubResult, false);
162 aDisplayed = redisplay(theObject, theUpdateViewer);
165 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
166 bool isShading = false;
167 if (aPrs.get() != NULL) {
168 GeomScreenParamsPtr aScreen = std::dynamic_pointer_cast<GeomAPI_IScreenParams>(theObject);
170 aScreen->setScreenPlane(getScreenPlane());
171 aScreen->setViewScale(getViewScale());
173 anAIS = aPrs->getAISObject(anAIS);
175 // correct deviation coefficient for
176 /*Handle(AIS_InteractiveObject) anAISPrs = anAIS->impl<Handle(AIS_InteractiveObject)>();
177 if (!anAISPrs.IsNull()) {
178 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(anAISPrs);
179 if (!aShapePrs.IsNull()) {
180 TopoDS_Shape aShape = aShapePrs->Shape();
181 if (!aShape.IsNull())
182 //ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, anAISPrs->Attributes());
187 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
188 if (aResult.get() != NULL) {
189 #ifdef DEBUG_COMPOSILID_DISPLAY
190 ResultCompSolidPtr aCompsolidResult =
191 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
192 if (aCompsolidResult.get()) {
193 for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
194 ResultPtr aSubResult = aCompsolidResult->subResult(i);
195 if (aSubResult.get())
196 display(aSubResult, false);
203 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
204 if (aShapePtr.get() != NULL) {
205 anAIS = AISObjectPtr(new GeomAPI_AISObject());
206 Handle(AIS_InteractiveObject) anAISPrs =
207 myWorkshop->module()->createPresentation(aResult);
208 if (anAISPrs.IsNull())
209 anAISPrs = new ModuleBase_ResultPrs(aResult);
211 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(anAISPrs);
212 if (!aShapePrs.IsNull())
213 ModuleBase_Tools::setPointBallHighlighting((AIS_Shape*) aShapePrs.get());
215 anAIS->setImpl(new Handle(AIS_InteractiveObject)(anAISPrs));
216 //anAIS->createShape(aShapePtr);
219 #ifdef DEBUG_COMPOSILID_DISPLAY
225 aDisplayed = display(theObject, anAIS, isShading, theUpdateViewer);
230 //**************************************************************
231 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS, ModuleBase_IModule* theModule)
233 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
234 if (!aShapePrs.IsNull()) {
235 TopoDS_Shape aShape = aShapePrs->Shape();
238 TopAbs_ShapeEnum aType = aShape.ShapeType();
239 if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
242 // Check that the presentation is not a sketch
243 return theModule->canBeShaded(theAIS);
249 //**************************************************************
250 bool XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
251 bool isShading, bool theUpdateViewer)
253 bool aDisplayed = false;
255 Handle(AIS_InteractiveContext) aContext = AISContext();
256 if (aContext.IsNull())
259 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
260 if (!anAISIO.IsNull()) {
261 appendResultObject(theObject, theAIS);
263 bool isCustomized = customizeObject(theObject);
265 int aDispMode = isShading? Shading : Wireframe;
267 anAISIO->Attributes()->SetFaceBoundaryDraw( Standard_True );
268 anAISIO->SetDisplayMode(aDispMode);
269 aContext->Display(anAISIO, aDispMode, 0, false, true, AIS_DS_Displayed);
271 if (getCallBack()) getCallBack()->Display(anAISIO);
275 emit objectDisplayed(theObject, theAIS);
276 selectionActivate()->activate(anAISIO, theUpdateViewer);
284 //**************************************************************
285 bool XGUI_Displayer::erase(ObjectPtr theObject, const bool theUpdateViewer)
287 bool aErased = false;
288 if (!isVisible(theObject))
291 Handle(AIS_InteractiveContext) aContext = AISContext();
292 if (aContext.IsNull())
295 AISObjectPtr anObject = myResult2AISObjectMap.value(theObject);
297 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
298 if (!anAIS.IsNull()) {
299 emit beforeObjectErase(theObject, anObject);
300 aContext->Remove(anAIS, false/*update viewer*/);
302 if (getCallBack()) getCallBack()->Remove(anAIS);
307 myResult2AISObjectMap.remove(theObject);
310 std::ostringstream aPtrStr;
311 aPtrStr << theObject.get();
312 qDebug(QString("erase object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
313 qDebug(getResult2AISObjectMapInfo().c_str());
322 //**************************************************************
323 bool XGUI_Displayer::redisplay(ObjectPtr theObject, bool theUpdateViewer)
325 bool aRedisplayed = false;
326 if (!isVisible(theObject))
329 AISObjectPtr aAISObj = getAISObject(theObject);
330 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
332 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
334 GeomScreenParamsPtr aScreen = std::dynamic_pointer_cast<GeomAPI_IScreenParams>(theObject);
336 aScreen->setScreenPlane(getScreenPlane());
337 aScreen->setViewScale(getViewScale());
339 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
341 aRedisplayed = erase(theObject, theUpdateViewer);
344 if (aAIS_Obj != aAISObj) {
345 erase(theObject, theUpdateViewer);
346 appendResultObject(theObject, aAIS_Obj);
348 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
351 Handle(AIS_InteractiveContext) aContext = AISContext();
352 if (!aContext.IsNull() && !aAISIO.IsNull()) {
353 // Check that the visualized shape is the same and the redisplay is not necessary
354 // Redisplay of AIS object leads to this object selection compute and the selection
355 // in the browser is lost
356 // this check is not necessary anymore because the selection store/restore is realized
357 // before and after the values modification.
358 // Moreother, this check avoids customize and redisplay presentation if the presentable
359 // parameter is changed.
360 bool isEqualShapes = false;
361 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
362 if (aResult.get() != NULL) {
363 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(aAISIO);
364 if (!aShapePrs.IsNull()) {
365 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
366 if (aShapePtr.get()) {
367 const TopoDS_Shape& aOldShape = aShapePrs->Shape();
368 if (!aOldShape.IsNull())
369 isEqualShapes = aOldShape.IsEqual(aShapePtr->impl<TopoDS_Shape>());
373 // Customization of presentation
374 bool isCustomized = customizeObject(theObject);
375 #ifdef DEBUG_FEATURE_REDISPLAY
376 qDebug(QString("Redisplay: %1, isEqualShapes=%2, isCustomized=%3").
377 arg(!isEqualShapes || isCustomized).arg(isEqualShapes)
378 .arg(isCustomized).toStdString().c_str());
380 if (!isEqualShapes || isCustomized) {
381 /// if shapes are equal and presentation are customized, selection should be restored
382 bool aNeedToRestoreSelection = isEqualShapes && isCustomized;
383 if (aNeedToRestoreSelection)
384 myWorkshop->module()->storeSelection();
386 #ifdef CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
387 myWorkshop->selector()->deselectPresentation(aAISIO);
389 if (aContext->IsDisplayed(aAISIO))
390 aContext->Redisplay(aAISIO, false);
392 aContext->Display(aAISIO, false);
395 if (getCallBack()) getCallBack()->Redisplay(aAISIO);
398 if (aNeedToRestoreSelection)
399 myWorkshop->module()->restoreSelection();
402 #ifdef DEBUG_FEATURE_REDISPLAY
403 qDebug(" Redisplay happens");
412 //**************************************************************
413 void XGUI_Displayer::redisplayObjects()
415 // redisplay objects visualized in the viewer
416 static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
417 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
418 QObjectPtrList aDisplayed = myWorkshop->displayer()->displayedObjects();
419 QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
420 for (; anIt != aLast; anIt++) {
421 aECreator->sendUpdated(*anIt, EVENT_DISP);
423 Events_Loop::loop()->flush(EVENT_DISP);
426 //**************************************************************
427 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
428 const bool theUpdateViewer)
430 //Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
431 //if (!aTrihedron.IsNull())
432 // deactivateAIS(aTrihedron);
434 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
435 for (; anIt != aLast; anIt++) {
436 selectionActivate()->deactivate(*anIt, false);
438 //VSV It seems that there is no necessity to update viewer on deactivation
439 //if (theUpdateViewer)
443 //**************************************************************
444 bool XGUI_Displayer::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
446 bool aVisible = false;
447 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
448 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
449 if (aPrs.get() || aResult.get()) {
450 aVisible = theDisplayer->isVisible(theObject);
451 // compsolid is not visualized in the viewer,
452 // but should have presentation when all sub solids are
453 // visible. It is useful for highlight presentation where compsolid shape is selectable
454 if (!aVisible && aResult.get() && aResult->groupName() == ModelAPI_ResultBody::group()) {
455 ResultBodyPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
456 if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
457 int aNumberOfSubs = aCompsolidResult->numberOfSubs();
458 bool anAllSubsVisible = aNumberOfSubs > 0;
459 for(int i = 0; i < aNumberOfSubs && anAllSubsVisible; i++) {
460 anAllSubsVisible = theDisplayer->isVisible(aCompsolidResult->subResult(i));
462 aVisible = anAllSubsVisible;
466 // it is possible that feature is presentable and has results, so we should check visibility
467 // of results if presentation is not shown (e.g. Sketch Circle/Arc features)
469 // check if all results of the feature are visible
470 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
471 std::list<ResultPtr> aResults;
472 ModelAPI_Tools::allResults(aFeature, aResults);
473 std::list<ResultPtr>::const_iterator aIt;
474 aVisible = !aResults.empty();
475 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
476 aVisible = aVisible && theDisplayer->isVisible(*aIt);
482 //**************************************************************
483 void XGUI_Displayer::setSelected(const QList<ModuleBase_ViewerPrsPtr>& theValues,
484 bool theUpdateViewer)
486 Handle(AIS_InteractiveContext) aContext = AISContext();
487 if (aContext.IsNull())
489 aContext->UnhilightSelected(false);
490 aContext->ClearSelected(false);
492 if (getCallBack()) getCallBack()->ClearSelected();
494 NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)>>
497 foreach (ModuleBase_ViewerPrsPtr aPrs, theValues) {
498 const GeomShapePtr& aGeomShape = aPrs->shape();
499 if (aGeomShape.get() && !aGeomShape->isNull()) {
500 const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
501 #ifdef DEBUG_OCCT_SHAPE_SELECTION
502 // problem 1: performance
503 // problem 2: IO is not specified, so the first found owner is selected, as a result
504 // it might belong to another result
505 aContext->AddOrRemoveSelected(aShape, false);
507 if (getCallBack()) getCallBack()->AddOrRemoveSelected(aShape);
510 NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations;
511 if (aShapesToBeSelected.IsBound(aShape))
512 aPresentations = aShapesToBeSelected.Find(aShape);
513 ObjectPtr anObject = aPrs->object();
514 getPresentations(anObject, aPresentations);
516 aShapesToBeSelected.Bind(aShape, aPresentations);
519 ObjectPtr anObject = aPrs->object();
520 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
521 if (aResult.get() && isVisible(aResult)) {
522 AISObjectPtr anObj = myResult2AISObjectMap.value(aResult);
523 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
524 if (!anAIS.IsNull()) {
525 // The methods are replaced in order to provide multi-selection, e.g. restore selection
526 // by activating multi selector widget. It also gives an advantage that the multi
527 // selection in OB gives multi-selection in the viewer
528 //aContext->SetSelected(anAIS, false);
529 // The selection in the context was cleared, so the method sets the objects are selected
530 aContext->AddOrRemoveSelected(anAIS, false);
532 if (getCallBack()) getCallBack()->AddOrRemoveSelected(anAIS);
538 if (!aShapesToBeSelected.IsEmpty())
539 XGUI_Displayer::AddOrRemoveSelectedShapes(aContext, aShapesToBeSelected);
545 //**************************************************************
546 void XGUI_Displayer::clearSelected(const bool theUpdateViewer)
548 Handle(AIS_InteractiveContext) aContext = AISContext();
549 if (!aContext.IsNull()) {
550 aContext->UnhilightSelected(false);//UnhilightCurrents(false);
551 aContext->ClearSelected(theUpdateViewer);
553 if (getCallBack()) getCallBack()->ClearSelected();
558 //**************************************************************
559 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
561 bool aErased = false;
562 Handle(AIS_InteractiveContext) aContext = AISContext();
563 if (!aContext.IsNull()) {
565 foreach(ObjectPtr aObj, myResult2AISObjectMap.objects()) {
566 AISObjectPtr aAISObj = myResult2AISObjectMap.value(aObj);
568 foreach(ObjectPtr aObj, myResult2AISObjectMap.keys()) {
569 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
572 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
573 if (!anIO.IsNull()) {
574 emit beforeObjectErase(aObj, aAISObj);
575 aContext->Remove(anIO, false/*update viewer*/);
577 if (getCallBack()) getCallBack()->Remove(anIO);
585 myResult2AISObjectMap.clear();
588 qDebug(getResult2AISObjectMapInfo().c_str());
594 //**************************************************************
595 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
598 return myResult2AISObjectMap.value(theObject);
601 if (myResult2AISObjectMap.contains(theObject))
602 anIO = myResult2AISObjectMap[theObject];
607 //**************************************************************
608 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
610 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
611 return getObject(aRefAIS);
614 //**************************************************************
615 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
618 ObjectPtr anObject = myResult2AISObjectMap.value(theIO);
621 ResultToAISMap::const_iterator aMapIter = myResult2AISObjectMap.cbegin();
622 for (; aMapIter != myResult2AISObjectMap.cend(); aMapIter++) {
623 const AISObjectPtr& aAIS = aMapIter.value();
624 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
626 anObject = aMapIter.key();
631 if (!anObject.get()) {
632 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
633 if (!theIO.IsNull()) {
634 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
636 anObject = myWorkshop->module()->findPresentedObject(anAISObj);
641 //**************************************************************
642 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
644 bool aWasEnabled = isUpdateEnabled();
646 myViewerBlockedRecursiveCount--;
648 myViewerBlockedRecursiveCount++;
650 #ifdef DEBUG_VIEWER_BLOCKED_COUNT
651 std::cout << "myViewerBlockedRecursiveCount = " << myViewerBlockedRecursiveCount << std::endl;
654 if (myNeedUpdate && isUpdateEnabled()) {
656 myNeedUpdate = false;
661 //**************************************************************
662 bool XGUI_Displayer::isUpdateEnabled() const
664 return myViewerBlockedRecursiveCount == 0;
667 //**************************************************************
668 void XGUI_Displayer::updateViewer() const
670 Handle(AIS_InteractiveContext) aContext = AISContext();
672 #ifdef DEBUG_VIEWER_BLOCKED_COUNT
673 std::cout << "updateViewer: " << (myViewerBlockedRecursiveCount == 0 ? " done" : " later")
677 if (!aContext.IsNull() && isUpdateEnabled()) {
678 //myWorkshop->viewer()->Zfitall();
679 aContext->UpdateCurrentViewer();
685 //**************************************************************
686 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
688 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
689 if (!aContext.IsNull() && myIsFirstAISContextUse/*&& !aContext->HasOpenedContext()*/) {
690 XGUI_Displayer* aDisplayer = (XGUI_Displayer*)this;
691 aDisplayer->myIsFirstAISContextUse = false;
692 if (!myWorkshop->selectionActivate()->isTrihedronActive())
693 selectionActivate()->deactivateTrihedron(true);
694 aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
695 aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
696 ModuleBase_IViewer::DefaultHighlightDrawer = aContext->HighlightStyle();
701 //**************************************************************
702 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
704 Handle(AIS_InteractiveContext) aContext = AISContext();
705 if (!aContext.IsNull() && myAndFilter.IsNull()) {
706 myAndFilter = new SelectMgr_AndFilter();
707 aContext->AddFilter(myAndFilter);
712 //**************************************************************
713 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
714 const Standard_Integer theDisplayMode, bool theUpdateViewer)
716 bool aDisplayed = false;
717 Handle(AIS_InteractiveContext) aContext = AISContext();
718 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
719 if (!aContext.IsNull() && !anAISIO.IsNull()) {
720 aContext->Display(anAISIO, theDisplayMode, 0, false/*update viewer*/, true, AIS_DS_Displayed);
722 if (getCallBack()) getCallBack()->Display(anAISIO);
725 aContext->Deactivate(anAISIO);
727 if (getCallBack()) getCallBack()->Deactivate(anAISIO);
729 aContext->Load(anAISIO);
731 if (getCallBack()) getCallBack()->Load(anAISIO);
733 if (toActivateInSelectionModes)
734 myWorkshop->selectionActivate()->activateOnDisplay(anAISIO, theUpdateViewer);
742 //**************************************************************
743 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
745 bool aErased = false;
746 Handle(AIS_InteractiveContext) aContext = AISContext();
747 if (!aContext.IsNull()) {
748 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
749 if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
750 aContext->Remove(anAISIO, false/*update viewer*/);
752 if (getCallBack()) getCallBack()->Remove(anAISIO);
757 if (aErased && theUpdateViewer)
762 //**************************************************************
763 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
765 if (theMode == NoMode)
768 Handle(AIS_InteractiveContext) aContext = AISContext();
769 if (aContext.IsNull())
772 AISObjectPtr aAISObj = getAISObject(theObject);
776 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
777 aContext->SetDisplayMode(aAISIO, theMode, false);
778 // Redisplay in order to update new mode because it could be not computed before
783 //**************************************************************
784 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
786 Handle(AIS_InteractiveContext) aContext = AISContext();
787 if (aContext.IsNull())
790 AISObjectPtr aAISObj = getAISObject(theObject);
794 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
795 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
798 //**************************************************************
799 void XGUI_Displayer::deactivateSelectionFilters(const bool theAddFilterOnly)
801 Handle(AIS_InteractiveContext) aContext = AISContext();
802 if (!myAndFilter.IsNull()) {
804 if (!aContext.IsNull()) {
805 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
806 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
807 for (; anIt.More() && !aFound; anIt.Next()) {
808 Handle(SelectMgr_Filter) aFilter = anIt.Value();
809 aFound = aFilter == myAndFilter;
812 aContext->RemoveFilter(myAndFilter);
814 myAndFilter.Nullify();
818 //**************************************************************
819 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
821 Handle(AIS_InteractiveContext) aContext = AISContext();
822 if (aContext.IsNull() || hasSelectionFilter(theFilter))
825 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
826 if (!aCompositeFilter.IsNull()) {
827 aCompositeFilter->Add(theFilter);
828 #ifdef DEBUG_SELECTION_FILTERS
829 int aCount = aCompositeFilter->StoredFilters().Extent();
830 qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
835 //**************************************************************
836 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
838 Handle(AIS_InteractiveContext) aContext = AISContext();
839 if (aContext.IsNull())
842 Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
843 if (!aCompositeFilter.IsNull() && aCompositeFilter->IsIn(theFilter)) {
844 aCompositeFilter->Remove(theFilter);
845 #ifdef DEBUG_SELECTION_FILTERS
846 int aCount = aCompositeFilter->StoredFilters().Extent();
847 qDebug(QString("removeSelectionFilter: filters.count() = %1")
848 .arg(aCount).toStdString().c_str());
853 //**************************************************************
854 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
856 bool aFilterFound = false;
858 Handle(AIS_InteractiveContext) aContext = AISContext();
859 if (aContext.IsNull())
861 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
862 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
863 for (; aIt.More() && !aFilterFound; aIt.Next()) {
864 if (theFilter.get() == aIt.Value().get())
867 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
868 if (!aCompositeFilter.IsNull()) {
869 const SelectMgr_ListOfFilter& aStoredFilters = aCompositeFilter->StoredFilters();
870 for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
871 if (theFilter.get() == aIt.Value().get())
878 //**************************************************************
879 void XGUI_Displayer::removeFilters()
881 Handle(AIS_InteractiveContext) aContext = AISContext();
882 if (aContext.IsNull())
885 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
886 if (!aCompositeFilter.IsNull())
887 aCompositeFilter->Clear();
890 //**************************************************************
891 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
894 QObjectPtrList aDispList = myResult2AISObjectMap.objects();
896 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
898 foreach(ObjectPtr aObj, aDispList) {
899 if (!theList.contains(aObj))
902 foreach(ObjectPtr aObj, theList) {
903 if (!isVisible(aObj))
904 display(aObj, false);
909 //**************************************************************
910 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
912 if (!isVisible(theObject))
915 AISObjectPtr aAISObj = getAISObject(theObject);
916 if (aAISObj.get() == NULL)
919 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
920 return ::canBeShaded(anAIS, myWorkshop->module());
923 //**************************************************************
924 bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
926 AISObjectPtr anAISObj = getAISObject(theObject);
927 // correct the result's color it it has the attribute
928 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
930 // Customization of presentation
931 GeomCustomPrsPtr aCustomPrs;
932 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
933 if (aFeature.get() != NULL) {
934 GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
935 if (aCustPrs.get() != NULL)
936 aCustomPrs = aCustPrs;
938 if (aCustomPrs.get() == NULL) {
939 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
940 // we ignore presentable not customized objects
941 if (aPrs.get() == NULL)
942 aCustomPrs = myCustomPrs;
944 bool isCustomized = aCustomPrs.get() &&
945 aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
946 isCustomized = myWorkshop->module()->afterCustomisePresentation(aResult, anAISObj, myCustomPrs)
949 // update presentation state if faces panel is active
950 if (anAISObj.get() && myWorkshop->facesPanel())
951 isCustomized = myWorkshop->facesPanel()->customizeObject(theObject, anAISObj) || isCustomized;
956 //**************************************************************
957 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject,
958 const QColor& theColor,
959 bool theUpdateViewer)
961 if (!isVisible(theObject))
964 AISObjectPtr anAISObj = getAISObject(theObject);
966 anAISObj->getColor(aR, aG, aB);
967 anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
970 return QColor(aR, aG, aB);
973 //**************************************************************
974 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
977 myResult2AISObjectMap.add(theObject, theAIS);
979 myResult2AISObjectMap[theObject] = theAIS;
983 std::ostringstream aPtrStr;
984 aPtrStr << theObject.get();
985 qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
986 qDebug(getResult2AISObjectMapInfo().c_str());
991 //**************************************************************
992 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
994 QStringList aContent;
996 foreach(ObjectPtr aObj, myResult2AISObjectMap.objects()) {
997 AISObjectPtr aAISObj = myResult2AISObjectMap.value(aObj);
999 foreach(ObjectPtr aObj, myResult2AISObjectMap.keys()) {
1000 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
1002 std::ostringstream aPtrStr;
1003 aPtrStr << "aObj = " << aObj.get() << ":";
1004 aPtrStr << "anAIS = " << aAISObj.get() << ":";
1005 aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
1007 aContent.append(aPtrStr.str().c_str());
1009 return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
1010 arg(aContent.join("\n")).toStdString().c_str();
1014 //**************************************************************
1015 void XGUI_Displayer::getPresentations(const ObjectPtr& theObject,
1016 NCollection_Map<Handle(AIS_InteractiveObject)>& thePresentations)
1018 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1019 if (aResult.get()) {
1020 AISObjectPtr aAISObj = getAISObject(aResult);
1021 if (aAISObj.get() == NULL) {
1022 // if result is a result of a composite feature, it is visualized by visualization of
1023 // composite children, so we should get one of this presentations
1024 ResultBodyPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
1025 if (aCompSolid.get() && aCompSolid->numberOfSubs() > 0) {
1026 aAISObj = getAISObject(aCompSolid->subResult(0));
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);
1036 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
1037 // find presentation of the feature
1038 AISObjectPtr aAISObj = getAISObject(aFeature);
1039 if (aAISObj.get() != NULL) {
1040 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1041 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1042 thePresentations.Add(anAIS);
1044 // find presentations of the feature results
1045 std::list<ResultPtr> aResults;
1046 ModelAPI_Tools::allResults(aFeature, aResults);
1047 std::list<ResultPtr>::const_iterator anIt = aResults.begin(), aLast = aResults.end();
1048 for (; anIt != aLast; ++anIt) {
1049 AISObjectPtr aAISObj = getAISObject(*anIt);
1050 if (aAISObj.get() != NULL) {
1051 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1052 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1053 thePresentations.Add(anAIS);
1059 //**************************************************************
1060 void XGUI_Displayer::displayTrihedron(bool theToDisplay) const
1062 Handle(AIS_InteractiveContext) aContext = AISContext();
1063 if (aContext.IsNull())
1066 Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
1068 XGUI_SelectionActivate* aSelectionActive = selectionActivate();
1070 if (!aContext->IsDisplayed(aTrihedron))
1071 aContext->Display(aTrihedron,
1073 -1 /* selection mode */,
1074 Standard_True /* update viewer*/,
1075 Standard_False /* allow decomposition */,
1076 AIS_DS_Displayed /* xdisplay status */);
1078 if (getCallBack()) getCallBack()->Display(aTrihedron);
1081 if (!aSelectionActive->isTrihedronActive())
1082 aSelectionActive->deactivateTrihedron(false);
1084 aSelectionActive->activate(aTrihedron, false);
1086 aSelectionActive->deactivateTrihedron(false);
1088 aContext->Erase(aTrihedron, Standard_True);
1090 if (getCallBack()) getCallBack()->Remove(aTrihedron);
1097 //**************************************************************
1098 void XGUI_Displayer::AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
1099 const NCollection_DataMap<TopoDS_Shape,
1100 NCollection_Map<Handle(AIS_InteractiveObject)>>& theShapesToBeSelected)
1102 NCollection_Map<Handle(AIS_InteractiveObject)> aCompsolidPresentations;
1103 NCollection_Map<Handle(AIS_InteractiveObject)> aSelectedPresentations;
1105 NCollection_List<Handle(SelectBasics_EntityOwner)> anActiveOwners;
1106 theContext->MainSelector()->ActiveOwners(anActiveOwners);
1107 NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (anActiveOwners);
1108 Handle(SelectMgr_EntityOwner) anOwner;
1110 /// It is very important to check that the owner is processed only once and has a map of
1111 /// processed owners because SetSelected works as a switch.
1112 /// If count of calls setSelectec is even, the object stays in the previous state
1113 /// (selected, deselected)
1114 /// OCCT: to write about the problem that active owners method returns one owner several times
1115 QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
1116 for (; anOwnersIt.More(); anOwnersIt.Next()) {
1117 anOwner = Handle(SelectMgr_EntityOwner)::DownCast (anOwnersIt.Value());
1118 if (aSelectedIds.contains((size_t)anOwner.get()))
1120 aSelectedIds.append((size_t)anOwner.get());
1122 Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
1123 if (!BROwnr.IsNull() && BROwnr->HasShape()) {
1124 const TopoDS_Shape& aShape = BROwnr->Shape();
1125 if (aShape.IsNull())
1128 Handle(ModuleBase_BRepOwner) aCustomOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
1130 NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)> >
1131 ::Iterator aShapeIt(theShapesToBeSelected);
1132 for (; aShapeIt.More(); aShapeIt.Next()) {
1133 const TopoDS_Shape& aParameterShape = aShapeIt.Key();
1134 // isSame should be used here as it does not check orientation of shapes
1135 // despite on isEqual of shapes or IsBound for shape in QMap. Orientation is
1136 // different for Edges shapes in model shape and owner even if this is the same shape
1137 if (aParameterShape.IsSame(aShape)) {
1138 Handle(AIS_InteractiveObject) anOwnerPresentation =
1139 Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
1140 NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations =
1141 theShapesToBeSelected.Find(aParameterShape);
1142 if (aPresentations.Contains(anOwnerPresentation)) {
1143 theContext->AddOrRemoveSelected(anOwner, Standard_False);
1144 anOwner->SetSelected (Standard_True);
1145 // collect selected presentations to do not select them if compsolid is selected
1146 if (!aSelectedPresentations.Contains(anOwnerPresentation))
1147 aSelectedPresentations.Add(anOwnerPresentation);
1150 else if (!aCustomOwner.IsNull()) { // CompSolid processing #2219
1151 // shape of owner is compound, but shape to be selected is compsolid, so
1152 // we need to compare shape to AIS presentation of owner(rule of the owner creation)
1153 Handle(AIS_Shape) anOwnerPresentation =
1154 Handle(AIS_Shape)::DownCast(anOwner->Selectable());
1155 const TopoDS_Shape& aPresentationShape = anOwnerPresentation->Shape();
1156 if (aParameterShape.IsSame(anOwnerPresentation->Shape()) &&
1157 !aCompsolidPresentations.Contains(anOwnerPresentation))
1158 aCompsolidPresentations.Add(anOwnerPresentation);
1163 // select CompSolid presentations if their owners was not selected yet
1164 NCollection_Map<Handle(AIS_InteractiveObject)>::Iterator anIt (aCompsolidPresentations);
1165 for (; anIt.More(); anIt.Next()) {
1166 if (aSelectedPresentations.Contains(anIt.Value()))
1168 theContext->AddOrRemoveSelected(anIt.Value(), Standard_False);
1172 //**************************************************************
1173 XGUI_SelectionActivate* XGUI_Displayer::selectionActivate() const
1175 return myWorkshop->selectionActivate();
1178 //**************************************************************
1179 GeomPlanePtr XGUI_Displayer::getScreenPlane() const
1181 GeomPlanePtr aResult;
1182 Handle(AIS_InteractiveContext) aContext = AISContext();
1183 if (!aContext.IsNull()) {
1184 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1185 Handle(V3d_View) aView;
1186 for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews()) {
1187 aView = aViewer->ActiveView();
1190 if (!aView.IsNull()) {
1191 double aEyeX, aEyeY, aEyeZ;
1192 aView->Eye(aEyeX, aEyeY, aEyeZ);
1194 double aProjX, aProjY, aProjZ;
1195 aView->Proj(aProjX, aProjY, aProjZ);
1197 GeomPointPtr aPnt = GeomPointPtr(new GeomAPI_Pnt(aEyeX, aEyeY, aEyeZ));
1198 GeomDirPtr aDir = GeomDirPtr(new GeomAPI_Dir(aProjX, aProjY, aProjZ));
1200 aResult = GeomPlanePtr(new GeomAPI_Pln(aPnt, aDir));
1206 double XGUI_Displayer::getViewScale() const
1208 Handle(AIS_InteractiveContext) aContext = AISContext();
1209 if (!aContext.IsNull()) {
1210 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1211 Handle(V3d_View) aView;
1212 for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews()) {
1213 aView = aViewer->ActiveView();
1216 return aView->Camera()->Scale();