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_CustomPrs.h"
23 #include "XGUI_FacesPanel.h"
24 #include "XGUI_Selection.h"
25 #include "XGUI_SelectionActivate.h"
26 #include "XGUI_SelectionMgr.h"
27 #include "XGUI_ViewerProxy.h"
28 #include "XGUI_Workshop.h"
31 #include <AppElements_Viewer.h>
34 #include <ModelAPI_Document.h>
35 #include <ModelAPI_Data.h>
36 #include <ModelAPI_Object.h>
37 #include <ModelAPI_Tools.h>
38 #include <ModelAPI_AttributeIntArray.h>
39 #include <ModelAPI_ResultBody.h>
40 #include <ModelAPI_ResultConstruction.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>
89 #include <Config_PropManager.h>
93 /// defines the local context mouse selection sensitivity
94 const int MOUSE_SENSITIVITY_IN_PIXEL = 10;
96 //#define DEBUG_DISPLAY
97 //#define DEBUG_FEATURE_REDISPLAY
98 //#define DEBUG_SELECTION_FILTERS
100 //#define DEBUG_OCCT_SHAPE_SELECTION
102 #define CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
104 //#define DEBUG_VIEWER_BLOCKED_COUNT
106 //**************************************************************
107 void displayedObjects(const Handle(AIS_InteractiveContext)& theAIS, AIS_ListOfInteractive& theList)
109 // Get from null point
110 theAIS->DisplayedObjects(theList, true);
113 QString qIntListInfo(const QIntList& theValues, const QString& theSeparator = QString(", "))
116 QIntList::const_iterator anIt = theValues.begin(), aLast = theValues.end();
117 for (; anIt != aLast; anIt++) {
118 anInfo.append(QString::number(*anIt));
120 return anInfo.join(theSeparator);
123 //**************************************************************
124 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
125 : myWorkshop(theWorkshop), myNeedUpdate(false),
126 myViewerBlockedRecursiveCount(0), myIsFirstAISContextUse(true)
128 myCustomPrs = std::shared_ptr<GeomAPI_ICustomPrs>(new XGUI_CustomPrs(theWorkshop));
131 //**************************************************************
132 XGUI_Displayer::~XGUI_Displayer()
136 //**************************************************************
137 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
139 return myResult2AISObjectMap.contains(theObject);
142 //**************************************************************
143 bool XGUI_Displayer::display(ObjectPtr theObject, bool theUpdateViewer)
145 bool aDisplayed = false;
146 if (isVisible(theObject)) {
147 aDisplayed = redisplay(theObject, theUpdateViewer);
150 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
151 bool isShading = false;
152 if (aPrs.get() != NULL) {
153 GeomScreenParamsPtr aScreen = std::dynamic_pointer_cast<GeomAPI_IScreenParams>(theObject);
155 aScreen->setScreenPlane(getScreenPlane());
156 aScreen->setViewScale(getViewScale());
158 anAIS = aPrs->getAISObject(anAIS);
160 Handle(AIS_InteractiveObject) anAISPrs =
161 myWorkshop->module()->createPresentation(theObject);
162 if (anAISPrs.IsNull()) {
163 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
164 if (aResult.get() != NULL) {
165 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
166 if (aShapePtr.get() != NULL) {
167 anAISPrs = new ModuleBase_ResultPrs(aResult);
171 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(anAISPrs);
172 if (!aShapePrs.IsNull())
173 ModuleBase_Tools::setPointBallHighlighting((AIS_Shape*)aShapePrs.get());
174 anAIS = AISObjectPtr(new GeomAPI_AISObject());
175 anAIS->setImpl(new Handle(AIS_InteractiveObject)(anAISPrs));
179 aDisplayed = display(theObject, anAIS, isShading, theUpdateViewer);
185 //**************************************************************
186 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS, ModuleBase_IModule* theModule)
188 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
189 if (!aShapePrs.IsNull()) {
190 TopoDS_Shape aShape = aShapePrs->Shape();
193 TopAbs_ShapeEnum aType = aShape.ShapeType();
194 if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
197 // Check that the presentation is not a sketch
198 return theModule->canBeShaded(theAIS);
204 //**************************************************************
205 bool XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
206 bool isShading, bool theUpdateViewer)
208 bool aDisplayed = false;
210 Handle(AIS_InteractiveContext) aContext = AISContext();
211 if (aContext.IsNull())
214 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
215 if (!anAISIO.IsNull()) {
216 appendResultObject(theObject, theAIS);
218 bool isCustomized = customizeObject(theObject);
220 int aDispMode = isShading? Shading : Wireframe;
222 anAISIO->Attributes()->SetFaceBoundaryDraw( Standard_True );
223 anAISIO->SetDisplayMode(aDispMode);
224 aContext->Display(anAISIO, aDispMode, 0, false, true, AIS_DS_Displayed);
226 if (getCallBack()) getCallBack()->Display(anAISIO);
230 emit objectDisplayed(theObject, theAIS);
231 selectionActivate()->activate(anAISIO, theUpdateViewer);
239 //**************************************************************
240 bool XGUI_Displayer::erase(ObjectPtr theObject, const bool theUpdateViewer)
242 bool aErased = false;
243 if (!isVisible(theObject))
246 Handle(AIS_InteractiveContext) aContext = AISContext();
247 if (aContext.IsNull())
250 AISObjectPtr anObject = myResult2AISObjectMap.value(theObject);
252 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
253 if (!anAIS.IsNull()) {
254 emit beforeObjectErase(theObject, anObject);
255 aContext->Remove(anAIS, false/*update viewer*/);
257 if (getCallBack()) getCallBack()->Remove(anAIS);
262 myResult2AISObjectMap.remove(theObject);
265 std::ostringstream aPtrStr;
266 aPtrStr << theObject.get();
267 qDebug(QString("erase object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
268 qDebug(getResult2AISObjectMapInfo().c_str());
277 //**************************************************************
278 bool XGUI_Displayer::redisplay(ObjectPtr theObject, bool theUpdateViewer)
280 bool aRedisplayed = false;
281 if (!isVisible(theObject))
284 AISObjectPtr aAISObj = getAISObject(theObject);
285 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
287 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
289 GeomScreenParamsPtr aScreen = std::dynamic_pointer_cast<GeomAPI_IScreenParams>(theObject);
291 aScreen->setScreenPlane(getScreenPlane());
292 aScreen->setViewScale(getViewScale());
294 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
296 aRedisplayed = erase(theObject, theUpdateViewer);
299 if (aAIS_Obj != aAISObj) {
300 erase(theObject, theUpdateViewer);
301 appendResultObject(theObject, aAIS_Obj);
303 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
306 Handle(AIS_InteractiveContext) aContext = AISContext();
307 if (!aContext.IsNull() && !aAISIO.IsNull()) {
308 // Check that the visualized shape is the same and the redisplay is not necessary
309 // Redisplay of AIS object leads to this object selection compute and the selection
310 // in the browser is lost
311 // this check is not necessary anymore because the selection store/restore is realized
312 // before and after the values modification.
313 // Moreother, this check avoids customize and redisplay presentation if the presentable
314 // parameter is changed.
315 bool isEqualShapes = false;
316 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
317 if (aResult.get() != NULL) {
318 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(aAISIO);
319 if (!aShapePrs.IsNull()) {
320 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
321 if (aShapePtr.get()) {
322 const TopoDS_Shape& aOldShape = aShapePrs->Shape();
323 if (!aOldShape.IsNull())
324 isEqualShapes = aOldShape.IsEqual(aShapePtr->impl<TopoDS_Shape>());
328 // Customization of presentation
329 bool isCustomized = customizeObject(theObject);
330 #ifdef DEBUG_FEATURE_REDISPLAY
331 qDebug(QString("Redisplay: %1, isEqualShapes=%2, isCustomized=%3").
332 arg(!isEqualShapes || isCustomized).arg(isEqualShapes)
333 .arg(isCustomized).toStdString().c_str());
335 if (!isEqualShapes || isCustomized) {
336 /// if shapes are equal and presentation are customized, selection should be restored
337 bool aNeedToRestoreSelection = isEqualShapes && isCustomized;
338 if (aNeedToRestoreSelection)
339 myWorkshop->module()->storeSelection();
341 #ifdef CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
342 myWorkshop->selector()->deselectPresentation(aAISIO);
344 if (aContext->IsDisplayed(aAISIO))
345 aContext->Redisplay(aAISIO, false);
347 aContext->Display(aAISIO, false);
350 if (getCallBack()) getCallBack()->Redisplay(aAISIO);
353 if (aNeedToRestoreSelection)
354 myWorkshop->module()->restoreSelection();
357 #ifdef DEBUG_FEATURE_REDISPLAY
358 qDebug(" Redisplay happens");
367 //**************************************************************
368 void XGUI_Displayer::redisplayObjects()
370 // redisplay objects visualized in the viewer
371 static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
372 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
373 QObjectPtrList aDisplayed = displayedObjects();
374 QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
375 for (; anIt != aLast; anIt++) {
376 aECreator->sendUpdated(*anIt, EVENT_DISP);
378 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
379 if (aViewer->isColorScaleVisible()) {
380 aViewer->setupColorScale();
381 aViewer->setColorScaleShown(true);
383 Events_Loop::loop()->flush(EVENT_DISP);
386 //**************************************************************
387 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
388 const bool theUpdateViewer)
390 //Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
391 //if (!aTrihedron.IsNull())
392 // deactivateAIS(aTrihedron);
394 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
395 for (; anIt != aLast; anIt++) {
396 selectionActivate()->deactivate(*anIt, false);
398 //VSV It seems that there is no necessity to update viewer on deactivation
399 //if (theUpdateViewer)
403 //**************************************************************
404 bool XGUI_Displayer::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
406 bool aVisible = false;
407 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
408 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
409 if (aPrs.get() || aResult.get()) {
410 aVisible = theDisplayer->isVisible(theObject);
411 // compsolid is not visualized in the viewer,
412 // but should have presentation when all sub solids are
413 // visible. It is useful for highlight presentation where compsolid shape is selectable
414 if (!aVisible && aResult.get() && aResult->groupName() == ModelAPI_ResultBody::group()) {
415 ResultBodyPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
416 if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
417 int aNumberOfSubs = aCompsolidResult->numberOfSubs();
418 bool anAllSubsVisible = aNumberOfSubs > 0;
419 for(int i = 0; i < aNumberOfSubs && anAllSubsVisible; i++) {
420 anAllSubsVisible = theDisplayer->isVisible(aCompsolidResult->subResult(i));
422 aVisible = anAllSubsVisible;
426 // it is possible that feature is presentable and has results, so we should check visibility
427 // of results if presentation is not shown (e.g. Sketch Circle/Arc features)
429 // check if all results of the feature are visible
430 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
431 std::list<ResultPtr> aResults;
432 ModelAPI_Tools::allResults(aFeature, aResults);
433 std::list<ResultPtr>::const_iterator aIt;
434 aVisible = !aResults.empty();
435 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
436 aVisible = aVisible && theDisplayer->isVisible(*aIt);
442 //**************************************************************
443 void XGUI_Displayer::setSelected(const QList<ModuleBase_ViewerPrsPtr>& theValues,
444 bool theUpdateViewer)
446 Handle(AIS_InteractiveContext) aContext = AISContext();
447 if (aContext.IsNull())
449 aContext->UnhilightSelected(false);
450 aContext->ClearSelected(false);
452 if (getCallBack()) getCallBack()->ClearSelected();
454 NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)>>
457 foreach (ModuleBase_ViewerPrsPtr aPrs, theValues) {
458 const GeomShapePtr& aGeomShape = aPrs->shape();
459 if (aGeomShape.get() && !aGeomShape->isNull()) {
460 const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
461 #ifdef DEBUG_OCCT_SHAPE_SELECTION
462 // problem 1: performance
463 // problem 2: IO is not specified, so the first found owner is selected, as a result
464 // it might belong to another result
465 aContext->AddOrRemoveSelected(aShape, false);
467 if (getCallBack()) getCallBack()->AddOrRemoveSelected(aShape);
470 NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations;
471 if (aShapesToBeSelected.IsBound(aShape))
472 aPresentations = aShapesToBeSelected.Find(aShape);
473 ObjectPtr anObject = aPrs->object();
474 getPresentations(anObject, aPresentations);
476 aShapesToBeSelected.Bind(aShape, aPresentations);
479 ObjectPtr anObject = aPrs->object();
480 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
481 if (aResult.get() && isVisible(aResult)) {
482 AISObjectPtr anObj = myResult2AISObjectMap.value(aResult);
483 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
484 if (!anAIS.IsNull()) {
485 // The methods are replaced in order to provide multi-selection, e.g. restore selection
486 // by activating multi selector widget. It also gives an advantage that the multi
487 // selection in OB gives multi-selection in the viewer
488 //aContext->SetSelected(anAIS, false);
489 // The selection in the context was cleared, so the method sets the objects are selected
490 aContext->AddOrRemoveSelected(anAIS, false);
492 if (getCallBack()) getCallBack()->AddOrRemoveSelected(anAIS);
498 if (!aShapesToBeSelected.IsEmpty())
499 XGUI_Displayer::AddOrRemoveSelectedShapes(aContext, aShapesToBeSelected);
505 //**************************************************************
506 void XGUI_Displayer::clearSelected(const bool theUpdateViewer)
508 Handle(AIS_InteractiveContext) aContext = AISContext();
509 if (!aContext.IsNull()) {
510 aContext->UnhilightSelected(false);//UnhilightCurrents(false);
511 aContext->ClearSelected(theUpdateViewer);
513 if (getCallBack()) getCallBack()->ClearSelected();
518 //**************************************************************
519 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
521 bool aErased = false;
522 Handle(AIS_InteractiveContext) aContext = AISContext();
523 if (!aContext.IsNull()) {
525 foreach(ObjectPtr aObj, myResult2AISObjectMap.objects()) {
526 AISObjectPtr aAISObj = myResult2AISObjectMap.value(aObj);
528 foreach(ObjectPtr aObj, myResult2AISObjectMap.keys()) {
529 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
532 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
533 if (!anIO.IsNull()) {
534 emit beforeObjectErase(aObj, aAISObj);
535 aContext->Remove(anIO, false/*update viewer*/);
537 if (getCallBack()) getCallBack()->Remove(anIO);
545 myResult2AISObjectMap.clear();
548 qDebug(getResult2AISObjectMapInfo().c_str());
554 //**************************************************************
555 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
558 return myResult2AISObjectMap.value(theObject);
561 if (myResult2AISObjectMap.contains(theObject))
562 anIO = myResult2AISObjectMap[theObject];
567 //**************************************************************
568 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
570 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
571 return getObject(aRefAIS);
574 //**************************************************************
575 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
578 ObjectPtr anObject = myResult2AISObjectMap.value(theIO);
581 ResultToAISMap::const_iterator aMapIter = myResult2AISObjectMap.cbegin();
582 for (; aMapIter != myResult2AISObjectMap.cend(); aMapIter++) {
583 const AISObjectPtr& aAIS = aMapIter.value();
584 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
586 anObject = aMapIter.key();
591 if (!anObject.get()) {
592 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
593 if (!theIO.IsNull()) {
594 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
596 anObject = myWorkshop->module()->findPresentedObject(anAISObj);
601 //**************************************************************
602 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
604 bool aWasEnabled = isUpdateEnabled();
606 myViewerBlockedRecursiveCount--;
608 myViewerBlockedRecursiveCount++;
610 #ifdef DEBUG_VIEWER_BLOCKED_COUNT
611 std::cout << "myViewerBlockedRecursiveCount = " << myViewerBlockedRecursiveCount << std::endl;
614 if (myNeedUpdate && isUpdateEnabled()) {
616 myNeedUpdate = false;
621 //**************************************************************
622 bool XGUI_Displayer::isUpdateEnabled() const
624 return myViewerBlockedRecursiveCount == 0;
627 //**************************************************************
628 void XGUI_Displayer::updateViewer() const
630 Handle(AIS_InteractiveContext) aContext = AISContext();
632 #ifdef DEBUG_VIEWER_BLOCKED_COUNT
633 std::cout << "updateViewer: " << (myViewerBlockedRecursiveCount == 0 ? " done" : " later")
637 if (!aContext.IsNull() && isUpdateEnabled()) {
638 //myWorkshop->viewer()->Zfitall();
639 aContext->UpdateCurrentViewer();
645 //**************************************************************
646 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
648 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
649 if (!aContext.IsNull() && myIsFirstAISContextUse/*&& !aContext->HasOpenedContext()*/) {
650 XGUI_Displayer* aDisplayer = (XGUI_Displayer*)this;
651 aDisplayer->myIsFirstAISContextUse = false;
652 if (!myWorkshop->selectionActivate()->isTrihedronActive())
653 selectionActivate()->deactivateTrihedron(true);
654 aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
655 aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
657 ModuleBase_IViewer::DefaultHighlightDrawer = aContext->HighlightStyle();
658 // Commented out according to discussion in bug #2825
659 //Handle(Prs3d_Drawer) aSelStyle = aContext->SelectionStyle();
660 //double aDeflection =
661 // QString(ModelAPI_ResultConstruction::DEFAULT_DEFLECTION().c_str()).toDouble();
663 // aDeflection = Config_PropManager::real("Visualization", "construction_deflection");
666 //ModuleBase_IViewer::DefaultHighlightDrawer->SetDeviationCoefficient(aDeflection);
667 //aSelStyle->SetDeviationCoefficient(aDeflection);
672 //**************************************************************
673 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
675 Handle(AIS_InteractiveContext) aContext = AISContext();
676 if (!aContext.IsNull() && myAndFilter.IsNull()) {
677 myAndFilter = new SelectMgr_AndFilter();
678 aContext->AddFilter(myAndFilter);
683 //**************************************************************
684 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
685 const Standard_Integer theDisplayMode, bool theUpdateViewer)
687 bool aDisplayed = false;
688 Handle(AIS_InteractiveContext) aContext = AISContext();
689 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
690 if (!aContext.IsNull() && !anAISIO.IsNull()) {
691 aContext->Display(anAISIO, theDisplayMode, 0, false/*update viewer*/, true, AIS_DS_Displayed);
693 if (getCallBack()) getCallBack()->Display(anAISIO);
696 aContext->Deactivate(anAISIO);
698 if (getCallBack()) getCallBack()->Deactivate(anAISIO);
700 aContext->Load(anAISIO);
702 if (getCallBack()) getCallBack()->Load(anAISIO);
704 if (toActivateInSelectionModes)
705 myWorkshop->selectionActivate()->activateOnDisplay(anAISIO, theUpdateViewer);
713 //**************************************************************
714 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
716 bool aErased = false;
717 Handle(AIS_InteractiveContext) aContext = AISContext();
718 if (!aContext.IsNull()) {
719 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
720 if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
721 aContext->Remove(anAISIO, false/*update viewer*/);
723 if (getCallBack()) getCallBack()->Remove(anAISIO);
728 if (aErased && theUpdateViewer)
733 //**************************************************************
734 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
736 if (theMode == NoMode)
739 Handle(AIS_InteractiveContext) aContext = AISContext();
740 if (aContext.IsNull())
743 AISObjectPtr aAISObj = getAISObject(theObject);
747 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
748 aContext->SetDisplayMode(aAISIO, theMode, false);
749 // Redisplay in order to update new mode because it could be not computed before
754 //**************************************************************
755 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
757 Handle(AIS_InteractiveContext) aContext = AISContext();
758 if (aContext.IsNull())
761 AISObjectPtr aAISObj = getAISObject(theObject);
765 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
766 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
769 //**************************************************************
770 void XGUI_Displayer::deactivateSelectionFilters(const bool theAddFilterOnly)
772 Handle(AIS_InteractiveContext) aContext = AISContext();
773 if (!myAndFilter.IsNull()) {
775 if (!aContext.IsNull()) {
776 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
777 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
778 for (; anIt.More() && !aFound; anIt.Next()) {
779 Handle(SelectMgr_Filter) aFilter = anIt.Value();
780 aFound = aFilter == myAndFilter;
783 aContext->RemoveFilter(myAndFilter);
785 myAndFilter.Nullify();
789 //**************************************************************
790 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
792 Handle(AIS_InteractiveContext) aContext = AISContext();
793 if (aContext.IsNull() || hasSelectionFilter(theFilter))
796 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
797 if (!aCompositeFilter.IsNull()) {
798 aCompositeFilter->Add(theFilter);
799 #ifdef DEBUG_SELECTION_FILTERS
800 int aCount = aCompositeFilter->StoredFilters().Extent();
801 qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
806 //**************************************************************
807 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
809 Handle(AIS_InteractiveContext) aContext = AISContext();
810 if (aContext.IsNull())
813 Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
814 if (!aCompositeFilter.IsNull() && aCompositeFilter->IsIn(theFilter)) {
815 aCompositeFilter->Remove(theFilter);
816 #ifdef DEBUG_SELECTION_FILTERS
817 int aCount = aCompositeFilter->StoredFilters().Extent();
818 qDebug(QString("removeSelectionFilter: filters.count() = %1")
819 .arg(aCount).toStdString().c_str());
824 //**************************************************************
825 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
827 bool aFilterFound = false;
829 Handle(AIS_InteractiveContext) aContext = AISContext();
830 if (aContext.IsNull())
832 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
833 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
834 for (; aIt.More() && !aFilterFound; aIt.Next()) {
835 if (theFilter.get() == aIt.Value().get())
838 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
839 if (!aCompositeFilter.IsNull()) {
840 const SelectMgr_ListOfFilter& aStoredFilters = aCompositeFilter->StoredFilters();
841 for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
842 if (theFilter.get() == aIt.Value().get())
849 //**************************************************************
850 void XGUI_Displayer::removeFilters()
852 Handle(AIS_InteractiveContext) aContext = AISContext();
853 if (aContext.IsNull())
856 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
857 if (!aCompositeFilter.IsNull())
858 aCompositeFilter->Clear();
861 //**************************************************************
862 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
865 QObjectPtrList aDispList = myResult2AISObjectMap.objects();
867 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
869 foreach(ObjectPtr aObj, aDispList) {
870 if (!theList.contains(aObj))
873 foreach(ObjectPtr aObj, theList) {
874 if (!isVisible(aObj))
875 display(aObj, false);
880 //**************************************************************
881 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
883 if (!isVisible(theObject))
886 AISObjectPtr aAISObj = getAISObject(theObject);
887 if (aAISObj.get() == NULL)
890 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
891 return ::canBeShaded(anAIS, myWorkshop->module());
894 //**************************************************************
895 bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
897 AISObjectPtr anAISObj = getAISObject(theObject);
898 // correct the result's color it it has the attribute
899 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
901 // Customization of presentation
902 GeomCustomPrsPtr aCustomPrs;
903 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
904 if (aFeature.get() != NULL) {
905 GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
906 if (aCustPrs.get() != NULL)
907 aCustomPrs = aCustPrs;
909 if (aCustomPrs.get() == NULL) {
910 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
911 // we ignore presentable not customized objects
912 if (aPrs.get() == NULL)
913 aCustomPrs = myCustomPrs;
915 bool isCustomized = aCustomPrs.get() &&
916 aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
917 isCustomized = myWorkshop->module()->afterCustomisePresentation(aResult, anAISObj, myCustomPrs)
920 // update presentation state if faces panel is active
921 if (anAISObj.get() && myWorkshop->facesPanel())
922 isCustomized = myWorkshop->facesPanel()->customizeObject(theObject, anAISObj) || isCustomized;
927 //**************************************************************
928 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject,
929 const QColor& theColor,
930 bool theUpdateViewer)
932 if (!isVisible(theObject))
935 AISObjectPtr anAISObj = getAISObject(theObject);
937 anAISObj->getColor(aR, aG, aB);
938 anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
941 return QColor(aR, aG, aB);
944 //**************************************************************
945 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
948 myResult2AISObjectMap.add(theObject, theAIS);
950 myResult2AISObjectMap[theObject] = theAIS;
954 std::ostringstream aPtrStr;
955 aPtrStr << theObject.get();
956 qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
957 qDebug(getResult2AISObjectMapInfo().c_str());
962 //**************************************************************
963 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
965 QStringList aContent;
967 foreach(ObjectPtr aObj, myResult2AISObjectMap.objects()) {
968 AISObjectPtr aAISObj = myResult2AISObjectMap.value(aObj);
970 foreach(ObjectPtr aObj, myResult2AISObjectMap.keys()) {
971 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
973 std::ostringstream aPtrStr;
974 aPtrStr << "aObj = " << aObj.get() << ":";
975 aPtrStr << "anAIS = " << aAISObj.get() << ":";
976 aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
978 aContent.append(aPtrStr.str().c_str());
980 return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
981 arg(aContent.join("\n")).toStdString().c_str();
985 //**************************************************************
986 void XGUI_Displayer::getPresentations(const ObjectPtr& theObject,
987 NCollection_Map<Handle(AIS_InteractiveObject)>& thePresentations)
989 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
991 AISObjectPtr aAISObj = getAISObject(aResult);
992 if (aAISObj.get() == NULL) {
993 // if result is a result of a composite feature, it is visualized by visualization of
994 // composite children, so we should get one of this presentations
995 ResultBodyPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
996 if (aCompSolid.get() && aCompSolid->numberOfSubs() > 0) {
997 aAISObj = getAISObject(aCompSolid->subResult(0));
1000 if (aAISObj.get() != NULL) {
1001 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1002 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1003 thePresentations.Add(anAIS);
1007 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
1008 // find presentation of the feature
1009 AISObjectPtr aAISObj = getAISObject(aFeature);
1010 if (aAISObj.get() != NULL) {
1011 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1012 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1013 thePresentations.Add(anAIS);
1015 // find presentations of the feature results
1016 std::list<ResultPtr> aResults;
1017 ModelAPI_Tools::allResults(aFeature, aResults);
1018 std::list<ResultPtr>::const_iterator anIt = aResults.begin(), aLast = aResults.end();
1019 for (; anIt != aLast; ++anIt) {
1020 AISObjectPtr aAISObj = getAISObject(*anIt);
1021 if (aAISObj.get() != NULL) {
1022 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1023 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1024 thePresentations.Add(anAIS);
1030 //**************************************************************
1031 void XGUI_Displayer::displayTrihedron(bool theToDisplay) const
1033 Handle(AIS_InteractiveContext) aContext = AISContext();
1034 if (aContext.IsNull())
1037 Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
1039 XGUI_SelectionActivate* aSelectionActive = selectionActivate();
1041 if (!aContext->IsDisplayed(aTrihedron))
1042 aContext->Display(aTrihedron,
1044 -1 /* selection mode */,
1045 Standard_True /* update viewer*/,
1046 Standard_False /* allow decomposition */,
1047 AIS_DS_Displayed /* xdisplay status */);
1049 if (getCallBack()) getCallBack()->Display(aTrihedron);
1052 if (!aSelectionActive->isTrihedronActive())
1053 aSelectionActive->deactivateTrihedron(false);
1055 aSelectionActive->activate(aTrihedron, false);
1057 aSelectionActive->deactivateTrihedron(false);
1059 aContext->Erase(aTrihedron, Standard_True);
1061 if (getCallBack()) getCallBack()->Remove(aTrihedron);
1068 //**************************************************************
1069 void XGUI_Displayer::AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
1070 const NCollection_DataMap<TopoDS_Shape,
1071 NCollection_Map<Handle(AIS_InteractiveObject)>>& theShapesToBeSelected)
1073 NCollection_Map<Handle(AIS_InteractiveObject)> aCompsolidPresentations;
1074 NCollection_Map<Handle(AIS_InteractiveObject)> aSelectedPresentations;
1076 NCollection_List<Handle(SelectBasics_EntityOwner)> anActiveOwners;
1077 theContext->MainSelector()->ActiveOwners(anActiveOwners);
1078 NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (anActiveOwners);
1079 Handle(SelectMgr_EntityOwner) anOwner;
1081 /// It is very important to check that the owner is processed only once and has a map of
1082 /// processed owners because SetSelected works as a switch.
1083 /// If count of calls setSelectec is even, the object stays in the previous state
1084 /// (selected, deselected)
1085 /// OCCT: to write about the problem that active owners method returns one owner several times
1086 QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
1087 for (; anOwnersIt.More(); anOwnersIt.Next()) {
1088 anOwner = Handle(SelectMgr_EntityOwner)::DownCast (anOwnersIt.Value());
1089 if (aSelectedIds.contains((size_t)anOwner.get()))
1091 aSelectedIds.append((size_t)anOwner.get());
1093 Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
1094 if (!BROwnr.IsNull() && BROwnr->HasShape()) {
1095 const TopoDS_Shape& aShape = BROwnr->Shape();
1096 if (aShape.IsNull())
1099 Handle(ModuleBase_BRepOwner) aCustomOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
1101 NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)> >
1102 ::Iterator aShapeIt(theShapesToBeSelected);
1103 for (; aShapeIt.More(); aShapeIt.Next()) {
1104 const TopoDS_Shape& aParameterShape = aShapeIt.Key();
1105 // In case of compound we cannot rely on simple comparison method.
1106 // If the compound is generated by Group feature then this compound is alwais new.
1107 // So, we have to compare content of these compounds
1109 // isSame should be used here as it does not check orientation of shapes
1110 // despite on isEqual of shapes or IsBound for shape in QMap. Orientation is
1111 // different for Edges shapes in model shape and owner even if this is the same shape
1112 if (ModuleBase_Tools::isSameShape(aParameterShape, aShape)) {
1113 Handle(AIS_InteractiveObject) anOwnerPresentation =
1114 Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
1115 NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations =
1116 theShapesToBeSelected.Find(aParameterShape);
1117 if (aPresentations.Contains(anOwnerPresentation)) {
1118 theContext->AddOrRemoveSelected(anOwner, Standard_False);
1119 anOwner->SetSelected(Standard_True);
1120 // collect selected presentations to do not select them if compsolid is selected
1121 if (!aSelectedPresentations.Contains(anOwnerPresentation))
1122 aSelectedPresentations.Add(anOwnerPresentation);
1125 else if (!aCustomOwner.IsNull()) { // CompSolid processing #2219
1126 // shape of owner is compound, but shape to be selected is compsolid, so
1127 // we need to compare shape to AIS presentation of owner(rule of the owner creation)
1128 Handle(AIS_Shape) anOwnerPresentation =
1129 Handle(AIS_Shape)::DownCast(anOwner->Selectable());
1130 const TopoDS_Shape& aPresentationShape = anOwnerPresentation->Shape();
1131 if (aParameterShape.IsSame(anOwnerPresentation->Shape()) &&
1132 !aCompsolidPresentations.Contains(anOwnerPresentation))
1133 aCompsolidPresentations.Add(anOwnerPresentation);
1138 // select CompSolid presentations if their owners was not selected yet
1139 NCollection_Map<Handle(AIS_InteractiveObject)>::Iterator anIt (aCompsolidPresentations);
1140 for (; anIt.More(); anIt.Next()) {
1141 if (aSelectedPresentations.Contains(anIt.Value()))
1143 theContext->AddOrRemoveSelected(anIt.Value(), Standard_False);
1147 //**************************************************************
1148 XGUI_SelectionActivate* XGUI_Displayer::selectionActivate() const
1150 return myWorkshop->selectionActivate();
1153 //**************************************************************
1154 GeomPlanePtr XGUI_Displayer::getScreenPlane() const
1156 GeomPlanePtr aResult;
1157 Handle(AIS_InteractiveContext) aContext = AISContext();
1158 if (!aContext.IsNull()) {
1159 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1160 Handle(V3d_View) aView;
1161 for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews()) {
1162 aView = aViewer->ActiveView();
1165 if (!aView.IsNull()) {
1166 double aEyeX, aEyeY, aEyeZ;
1167 aView->Eye(aEyeX, aEyeY, aEyeZ);
1169 double aProjX, aProjY, aProjZ;
1170 aView->Proj(aProjX, aProjY, aProjZ);
1172 GeomPointPtr aPnt = GeomPointPtr(new GeomAPI_Pnt(aEyeX, aEyeY, aEyeZ));
1173 GeomDirPtr aDir = GeomDirPtr(new GeomAPI_Dir(aProjX, aProjY, aProjZ));
1175 aResult = GeomPlanePtr(new GeomAPI_Pln(aPnt, aDir));
1181 double XGUI_Displayer::getViewScale() const
1183 Handle(AIS_InteractiveContext) aContext = AISContext();
1184 if (!aContext.IsNull()) {
1185 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1186 Handle(V3d_View) aView;
1187 for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews()) {
1188 aView = aViewer->ActiveView();
1191 return aView->Camera()->Scale();