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>
41 #include <ModelAPI_ResultField.h>
43 #include <ModuleBase_BRepOwner.h>
44 #include <ModuleBase_IModule.h>
45 #include <ModuleBase_Preferences.h>
46 #include <ModuleBase_ResultPrs.h>
47 #include <ModuleBase_Tools.h>
48 #include <ModuleBase_ViewerPrs.h>
49 #include <ModuleBase_IViewer.h>
51 #include <GeomAPI_Shape.h>
52 #include <GeomAPI_IPresentable.h>
53 #include <GeomAPI_ICustomPrs.h>
54 #include <GeomAPI_Pnt.h>
55 #include <GeomAPI_IScreenParams.h>
57 #include <SUIT_ResourceMgr.h>
59 #include <AIS_InteractiveContext.hxx>
60 #include <AIS_ListOfInteractive.hxx>
61 #include <AIS_ListIteratorOfListOfInteractive.hxx>
62 #include <AIS_DimensionSelectionMode.hxx>
63 #include <AIS_Shape.hxx>
64 #include <AIS_Dimension.hxx>
65 #include <AIS_Trihedron.hxx>
66 #ifdef BEFORE_TRIHEDRON_PATCH
67 #include <AIS_Axis.hxx>
68 #include <AIS_Plane.hxx>
69 #include <AIS_Point.hxx>
71 #include <AIS_Selection.hxx>
72 #include <Prs3d_Drawer.hxx>
73 #include <Prs3d_IsoAspect.hxx>
74 #include <SelectMgr_ListOfFilter.hxx>
75 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
76 #include <SelectMgr_SelectionManager.hxx>
77 #include <TColStd_ListIteratorOfListOfInteger.hxx>
79 #include <StdSelect_ViewerSelector3d.hxx>
81 #include <TColStd_MapOfTransient.hxx>
82 #include <TColStd_MapIteratorOfMapOfTransient.hxx>
85 #include <inspector/VInspectorAPI_CallBack.hxx>
88 #include <Events_Loop.h>
89 #include <ModelAPI_Events.h>
90 #include <Config_PropManager.h>
94 /// defines the local context mouse selection sensitivity
95 const int MOUSE_SENSITIVITY_IN_PIXEL = 10;
97 //#define DEBUG_DISPLAY
98 //#define DEBUG_FEATURE_REDISPLAY
99 //#define DEBUG_SELECTION_FILTERS
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 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 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
162 if (aResult.get() != NULL) {
163 anAIS = createPresentation(aResult);
168 std::dynamic_pointer_cast<ModelAPI_ResultField::ModelAPI_FieldStep>(theObject);
170 GeomShapePtr aShapePtr = aStep->field()->shape();
171 if (aShapePtr.get() != NULL) {
172 Handle(AIS_InteractiveObject) anAISPrs = new ModuleBase_ResultPrs(aStep);
173 anAIS = AISObjectPtr(new GeomAPI_AISObject());
174 anAIS->setImpl(new Handle(AIS_InteractiveObject)(anAISPrs));
181 aDisplayed = display(theObject, anAIS, isShading, theUpdateViewer);
187 //**************************************************************
188 AISObjectPtr XGUI_Displayer::createPresentation(const ResultPtr& theResult) const
191 if (theResult.get() != NULL) {
192 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(theResult);
193 if (aShapePtr.get() != NULL) {
194 anAIS = AISObjectPtr(new GeomAPI_AISObject());
195 Handle(AIS_InteractiveObject) anAISPrs =
196 myWorkshop->module()->createPresentation(theResult);
197 if (anAISPrs.IsNull())
198 anAISPrs = new ModuleBase_ResultPrs(theResult);
200 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(anAISPrs);
201 if (!aShapePrs.IsNull())
202 ModuleBase_Tools::setPointBallHighlighting((AIS_Shape*)aShapePrs.get());
204 anAIS->setImpl(new Handle(AIS_InteractiveObject)(anAISPrs));
211 //**************************************************************
212 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS, ModuleBase_IModule* theModule)
214 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
215 if (!aShapePrs.IsNull()) {
216 TopoDS_Shape aShape = aShapePrs->Shape();
219 TopAbs_ShapeEnum aType = aShape.ShapeType();
220 if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
223 // Check that the presentation is not a sketch
224 return theModule->canBeShaded(theAIS);
230 //**************************************************************
231 bool XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
232 bool isShading, bool theUpdateViewer)
234 bool aDisplayed = false;
236 Handle(AIS_InteractiveContext) aContext = AISContext();
237 if (aContext.IsNull())
240 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
241 if (!anAISIO.IsNull()) {
242 appendResultObject(theObject, theAIS);
244 bool isCustomized = customizeObject(theObject);
246 int aDispMode = isShading? Shading : Wireframe;
248 anAISIO->Attributes()->SetFaceBoundaryDraw( Standard_True );
249 anAISIO->SetDisplayMode(aDispMode);
250 aContext->Display(anAISIO, aDispMode, 0, false, true, AIS_DS_Displayed);
252 if (getCallBack()) getCallBack()->Display(anAISIO);
256 emit objectDisplayed(theObject, theAIS);
257 selectionActivate()->activate(anAISIO, theUpdateViewer);
265 //**************************************************************
266 bool XGUI_Displayer::erase(ObjectPtr theObject, const bool theUpdateViewer)
268 bool aErased = false;
269 if (!isVisible(theObject))
272 Handle(AIS_InteractiveContext) aContext = AISContext();
273 if (aContext.IsNull())
276 AISObjectPtr anObject = myResult2AISObjectMap.value(theObject);
278 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
279 if (!anAIS.IsNull()) {
280 emit beforeObjectErase(theObject, anObject);
281 aContext->Remove(anAIS, false/*update viewer*/);
283 if (getCallBack()) getCallBack()->Remove(anAIS);
288 myResult2AISObjectMap.remove(theObject);
291 std::ostringstream aPtrStr;
292 aPtrStr << theObject.get();
293 qDebug(QString("erase object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
294 qDebug(getResult2AISObjectMapInfo().c_str());
303 //**************************************************************
304 bool XGUI_Displayer::redisplay(ObjectPtr theObject, bool theUpdateViewer)
306 bool aRedisplayed = false;
307 if (!isVisible(theObject))
310 AISObjectPtr aAISObj = getAISObject(theObject);
311 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
313 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
315 GeomScreenParamsPtr aScreen = std::dynamic_pointer_cast<GeomAPI_IScreenParams>(theObject);
317 aScreen->setScreenPlane(getScreenPlane());
318 aScreen->setViewScale(getViewScale());
320 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
322 aRedisplayed = erase(theObject, theUpdateViewer);
325 if (aAIS_Obj != aAISObj) {
326 erase(theObject, theUpdateViewer);
327 appendResultObject(theObject, aAIS_Obj);
329 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
332 Handle(AIS_InteractiveContext) aContext = AISContext();
333 if (!aContext.IsNull() && !aAISIO.IsNull()) {
334 // Check that the visualized shape is the same and the redisplay is not necessary
335 // Redisplay of AIS object leads to this object selection compute and the selection
336 // in the browser is lost
337 // this check is not necessary anymore because the selection store/restore is realized
338 // before and after the values modification.
339 // Moreother, this check avoids customize and redisplay presentation if the presentable
340 // parameter is changed.
341 bool isEqualShapes = false;
342 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
343 if (aResult.get() != NULL) {
344 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(aAISIO);
345 if (!aShapePrs.IsNull()) {
346 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
347 if (aShapePtr.get()) {
348 const TopoDS_Shape& aOldShape = aShapePrs->Shape();
349 if (!aOldShape.IsNull())
350 isEqualShapes = aOldShape.IsEqual(aShapePtr->impl<TopoDS_Shape>());
354 // Customization of presentation
355 bool isCustomized = customizeObject(theObject);
356 #ifdef DEBUG_FEATURE_REDISPLAY
357 qDebug(QString("Redisplay: %1, isEqualShapes=%2, isCustomized=%3").
358 arg(!isEqualShapes || isCustomized).arg(isEqualShapes)
359 .arg(isCustomized).toStdString().c_str());
361 if (!isEqualShapes || isCustomized) {
362 /// if shapes are equal and presentation are customized, selection should be restored
363 bool aNeedToRestoreSelection = isEqualShapes && isCustomized;
364 if (aNeedToRestoreSelection)
365 myWorkshop->module()->storeSelection();
367 #ifdef CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
368 myWorkshop->selector()->deselectPresentation(aAISIO);
370 if (aContext->IsDisplayed(aAISIO))
371 aContext->Redisplay(aAISIO, false);
373 aContext->Display(aAISIO, false);
376 if (getCallBack()) getCallBack()->Redisplay(aAISIO);
379 if (aNeedToRestoreSelection)
380 myWorkshop->module()->restoreSelection();
383 #ifdef DEBUG_FEATURE_REDISPLAY
384 qDebug(" Redisplay happens");
393 //**************************************************************
394 void XGUI_Displayer::redisplayObjects()
396 // redisplay objects visualized in the viewer
397 static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
398 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
399 QObjectPtrList aDisplayed = myWorkshop->displayer()->displayedObjects();
400 QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
401 for (; anIt != aLast; anIt++) {
402 aECreator->sendUpdated(*anIt, EVENT_DISP);
404 Events_Loop::loop()->flush(EVENT_DISP);
407 //**************************************************************
408 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
409 const bool theUpdateViewer)
411 //Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
412 //if (!aTrihedron.IsNull())
413 // deactivateAIS(aTrihedron);
415 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
416 for (; anIt != aLast; anIt++) {
417 selectionActivate()->deactivate(*anIt, false);
419 //VSV It seems that there is no necessity to update viewer on deactivation
420 //if (theUpdateViewer)
424 //**************************************************************
425 bool XGUI_Displayer::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
427 bool aVisible = false;
428 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
429 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
430 if (aPrs.get() || aResult.get()) {
431 aVisible = theDisplayer->isVisible(theObject);
432 // compsolid is not visualized in the viewer,
433 // but should have presentation when all sub solids are
434 // visible. It is useful for highlight presentation where compsolid shape is selectable
435 if (!aVisible && aResult.get() && aResult->groupName() == ModelAPI_ResultBody::group()) {
436 ResultBodyPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
437 if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
438 int aNumberOfSubs = aCompsolidResult->numberOfSubs();
439 bool anAllSubsVisible = aNumberOfSubs > 0;
440 for(int i = 0; i < aNumberOfSubs && anAllSubsVisible; i++) {
441 anAllSubsVisible = theDisplayer->isVisible(aCompsolidResult->subResult(i));
443 aVisible = anAllSubsVisible;
447 // it is possible that feature is presentable and has results, so we should check visibility
448 // of results if presentation is not shown (e.g. Sketch Circle/Arc features)
450 // check if all results of the feature are visible
451 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
452 std::list<ResultPtr> aResults;
453 ModelAPI_Tools::allResults(aFeature, aResults);
454 std::list<ResultPtr>::const_iterator aIt;
455 aVisible = !aResults.empty();
456 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
457 aVisible = aVisible && theDisplayer->isVisible(*aIt);
463 //**************************************************************
464 void XGUI_Displayer::setSelected(const QList<ModuleBase_ViewerPrsPtr>& theValues,
465 bool theUpdateViewer)
467 Handle(AIS_InteractiveContext) aContext = AISContext();
468 if (aContext.IsNull())
470 aContext->UnhilightSelected(false);
471 aContext->ClearSelected(false);
473 if (getCallBack()) getCallBack()->ClearSelected();
475 NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)>>
478 foreach (ModuleBase_ViewerPrsPtr aPrs, theValues) {
479 const GeomShapePtr& aGeomShape = aPrs->shape();
480 if (aGeomShape.get() && !aGeomShape->isNull()) {
481 const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
482 #ifdef DEBUG_OCCT_SHAPE_SELECTION
483 // problem 1: performance
484 // problem 2: IO is not specified, so the first found owner is selected, as a result
485 // it might belong to another result
486 aContext->AddOrRemoveSelected(aShape, false);
488 if (getCallBack()) getCallBack()->AddOrRemoveSelected(aShape);
491 NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations;
492 if (aShapesToBeSelected.IsBound(aShape))
493 aPresentations = aShapesToBeSelected.Find(aShape);
494 ObjectPtr anObject = aPrs->object();
495 getPresentations(anObject, aPresentations);
497 aShapesToBeSelected.Bind(aShape, aPresentations);
500 ObjectPtr anObject = aPrs->object();
501 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
502 if (aResult.get() && isVisible(aResult)) {
503 AISObjectPtr anObj = myResult2AISObjectMap.value(aResult);
504 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
505 if (!anAIS.IsNull()) {
506 // The methods are replaced in order to provide multi-selection, e.g. restore selection
507 // by activating multi selector widget. It also gives an advantage that the multi
508 // selection in OB gives multi-selection in the viewer
509 //aContext->SetSelected(anAIS, false);
510 // The selection in the context was cleared, so the method sets the objects are selected
511 aContext->AddOrRemoveSelected(anAIS, false);
513 if (getCallBack()) getCallBack()->AddOrRemoveSelected(anAIS);
519 if (!aShapesToBeSelected.IsEmpty())
520 XGUI_Displayer::AddOrRemoveSelectedShapes(aContext, aShapesToBeSelected);
526 //**************************************************************
527 void XGUI_Displayer::clearSelected(const bool theUpdateViewer)
529 Handle(AIS_InteractiveContext) aContext = AISContext();
530 if (!aContext.IsNull()) {
531 aContext->UnhilightSelected(false);//UnhilightCurrents(false);
532 aContext->ClearSelected(theUpdateViewer);
534 if (getCallBack()) getCallBack()->ClearSelected();
539 //**************************************************************
540 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
542 bool aErased = false;
543 Handle(AIS_InteractiveContext) aContext = AISContext();
544 if (!aContext.IsNull()) {
546 foreach(ObjectPtr aObj, myResult2AISObjectMap.objects()) {
547 AISObjectPtr aAISObj = myResult2AISObjectMap.value(aObj);
549 foreach(ObjectPtr aObj, myResult2AISObjectMap.keys()) {
550 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
553 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
554 if (!anIO.IsNull()) {
555 emit beforeObjectErase(aObj, aAISObj);
556 aContext->Remove(anIO, false/*update viewer*/);
558 if (getCallBack()) getCallBack()->Remove(anIO);
566 myResult2AISObjectMap.clear();
569 qDebug(getResult2AISObjectMapInfo().c_str());
575 //**************************************************************
576 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
579 return myResult2AISObjectMap.value(theObject);
582 if (myResult2AISObjectMap.contains(theObject))
583 anIO = myResult2AISObjectMap[theObject];
588 //**************************************************************
589 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
591 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
592 return getObject(aRefAIS);
595 //**************************************************************
596 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
599 ObjectPtr anObject = myResult2AISObjectMap.value(theIO);
602 ResultToAISMap::const_iterator aMapIter = myResult2AISObjectMap.cbegin();
603 for (; aMapIter != myResult2AISObjectMap.cend(); aMapIter++) {
604 const AISObjectPtr& aAIS = aMapIter.value();
605 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
607 anObject = aMapIter.key();
612 if (!anObject.get()) {
613 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
614 if (!theIO.IsNull()) {
615 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
617 anObject = myWorkshop->module()->findPresentedObject(anAISObj);
622 //**************************************************************
623 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
625 bool aWasEnabled = isUpdateEnabled();
627 myViewerBlockedRecursiveCount--;
629 myViewerBlockedRecursiveCount++;
631 #ifdef DEBUG_VIEWER_BLOCKED_COUNT
632 std::cout << "myViewerBlockedRecursiveCount = " << myViewerBlockedRecursiveCount << std::endl;
635 if (myNeedUpdate && isUpdateEnabled()) {
637 myNeedUpdate = false;
642 //**************************************************************
643 bool XGUI_Displayer::isUpdateEnabled() const
645 return myViewerBlockedRecursiveCount == 0;
648 //**************************************************************
649 void XGUI_Displayer::updateViewer() const
651 Handle(AIS_InteractiveContext) aContext = AISContext();
653 #ifdef DEBUG_VIEWER_BLOCKED_COUNT
654 std::cout << "updateViewer: " << (myViewerBlockedRecursiveCount == 0 ? " done" : " later")
658 if (!aContext.IsNull() && isUpdateEnabled()) {
659 //myWorkshop->viewer()->Zfitall();
660 aContext->UpdateCurrentViewer();
666 //**************************************************************
667 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
669 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
670 if (!aContext.IsNull() && myIsFirstAISContextUse/*&& !aContext->HasOpenedContext()*/) {
671 XGUI_Displayer* aDisplayer = (XGUI_Displayer*)this;
672 aDisplayer->myIsFirstAISContextUse = false;
673 if (!myWorkshop->selectionActivate()->isTrihedronActive())
674 selectionActivate()->deactivateTrihedron(true);
675 aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
676 aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
678 ModuleBase_IViewer::DefaultHighlightDrawer = aContext->HighlightStyle();
679 // Commented out according to discussion in bug #2825
680 //Handle(Prs3d_Drawer) aSelStyle = aContext->SelectionStyle();
681 //double aDeflection =
682 // QString(ModelAPI_ResultConstruction::DEFAULT_DEFLECTION().c_str()).toDouble();
684 // aDeflection = Config_PropManager::real("Visualization", "construction_deflection");
687 //ModuleBase_IViewer::DefaultHighlightDrawer->SetDeviationCoefficient(aDeflection);
688 //aSelStyle->SetDeviationCoefficient(aDeflection);
693 //**************************************************************
694 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
696 Handle(AIS_InteractiveContext) aContext = AISContext();
697 if (!aContext.IsNull() && myAndFilter.IsNull()) {
698 myAndFilter = new SelectMgr_AndFilter();
699 aContext->AddFilter(myAndFilter);
704 //**************************************************************
705 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
706 const Standard_Integer theDisplayMode, bool theUpdateViewer)
708 bool aDisplayed = false;
709 Handle(AIS_InteractiveContext) aContext = AISContext();
710 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
711 if (!aContext.IsNull() && !anAISIO.IsNull()) {
712 aContext->Display(anAISIO, theDisplayMode, 0, false/*update viewer*/, true, AIS_DS_Displayed);
714 if (getCallBack()) getCallBack()->Display(anAISIO);
717 aContext->Deactivate(anAISIO);
719 if (getCallBack()) getCallBack()->Deactivate(anAISIO);
721 aContext->Load(anAISIO);
723 if (getCallBack()) getCallBack()->Load(anAISIO);
725 if (toActivateInSelectionModes)
726 myWorkshop->selectionActivate()->activateOnDisplay(anAISIO, theUpdateViewer);
734 //**************************************************************
735 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
737 bool aErased = false;
738 Handle(AIS_InteractiveContext) aContext = AISContext();
739 if (!aContext.IsNull()) {
740 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
741 if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
742 aContext->Remove(anAISIO, false/*update viewer*/);
744 if (getCallBack()) getCallBack()->Remove(anAISIO);
749 if (aErased && theUpdateViewer)
754 //**************************************************************
755 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
757 if (theMode == NoMode)
760 Handle(AIS_InteractiveContext) aContext = AISContext();
761 if (aContext.IsNull())
764 AISObjectPtr aAISObj = getAISObject(theObject);
768 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
769 aContext->SetDisplayMode(aAISIO, theMode, false);
770 // Redisplay in order to update new mode because it could be not computed before
775 //**************************************************************
776 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
778 Handle(AIS_InteractiveContext) aContext = AISContext();
779 if (aContext.IsNull())
782 AISObjectPtr aAISObj = getAISObject(theObject);
786 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
787 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
790 //**************************************************************
791 void XGUI_Displayer::deactivateSelectionFilters(const bool theAddFilterOnly)
793 Handle(AIS_InteractiveContext) aContext = AISContext();
794 if (!myAndFilter.IsNull()) {
796 if (!aContext.IsNull()) {
797 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
798 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
799 for (; anIt.More() && !aFound; anIt.Next()) {
800 Handle(SelectMgr_Filter) aFilter = anIt.Value();
801 aFound = aFilter == myAndFilter;
804 aContext->RemoveFilter(myAndFilter);
806 myAndFilter.Nullify();
810 //**************************************************************
811 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
813 Handle(AIS_InteractiveContext) aContext = AISContext();
814 if (aContext.IsNull() || hasSelectionFilter(theFilter))
817 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
818 if (!aCompositeFilter.IsNull()) {
819 aCompositeFilter->Add(theFilter);
820 #ifdef DEBUG_SELECTION_FILTERS
821 int aCount = aCompositeFilter->StoredFilters().Extent();
822 qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
827 //**************************************************************
828 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
830 Handle(AIS_InteractiveContext) aContext = AISContext();
831 if (aContext.IsNull())
834 Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
835 if (!aCompositeFilter.IsNull() && aCompositeFilter->IsIn(theFilter)) {
836 aCompositeFilter->Remove(theFilter);
837 #ifdef DEBUG_SELECTION_FILTERS
838 int aCount = aCompositeFilter->StoredFilters().Extent();
839 qDebug(QString("removeSelectionFilter: filters.count() = %1")
840 .arg(aCount).toStdString().c_str());
845 //**************************************************************
846 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
848 bool aFilterFound = false;
850 Handle(AIS_InteractiveContext) aContext = AISContext();
851 if (aContext.IsNull())
853 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
854 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
855 for (; aIt.More() && !aFilterFound; aIt.Next()) {
856 if (theFilter.get() == aIt.Value().get())
859 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
860 if (!aCompositeFilter.IsNull()) {
861 const SelectMgr_ListOfFilter& aStoredFilters = aCompositeFilter->StoredFilters();
862 for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
863 if (theFilter.get() == aIt.Value().get())
870 //**************************************************************
871 void XGUI_Displayer::removeFilters()
873 Handle(AIS_InteractiveContext) aContext = AISContext();
874 if (aContext.IsNull())
877 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
878 if (!aCompositeFilter.IsNull())
879 aCompositeFilter->Clear();
882 //**************************************************************
883 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
886 QObjectPtrList aDispList = myResult2AISObjectMap.objects();
888 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
890 foreach(ObjectPtr aObj, aDispList) {
891 if (!theList.contains(aObj))
894 foreach(ObjectPtr aObj, theList) {
895 if (!isVisible(aObj))
896 display(aObj, false);
901 //**************************************************************
902 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
904 if (!isVisible(theObject))
907 AISObjectPtr aAISObj = getAISObject(theObject);
908 if (aAISObj.get() == NULL)
911 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
912 return ::canBeShaded(anAIS, myWorkshop->module());
915 //**************************************************************
916 bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
918 AISObjectPtr anAISObj = getAISObject(theObject);
919 // correct the result's color it it has the attribute
920 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
922 // Customization of presentation
923 GeomCustomPrsPtr aCustomPrs;
924 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
925 if (aFeature.get() != NULL) {
926 GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
927 if (aCustPrs.get() != NULL)
928 aCustomPrs = aCustPrs;
930 if (aCustomPrs.get() == NULL) {
931 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
932 // we ignore presentable not customized objects
933 if (aPrs.get() == NULL)
934 aCustomPrs = myCustomPrs;
936 bool isCustomized = aCustomPrs.get() &&
937 aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
938 isCustomized = myWorkshop->module()->afterCustomisePresentation(aResult, anAISObj, myCustomPrs)
941 // update presentation state if faces panel is active
942 if (anAISObj.get() && myWorkshop->facesPanel())
943 isCustomized = myWorkshop->facesPanel()->customizeObject(theObject, anAISObj) || isCustomized;
948 //**************************************************************
949 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject,
950 const QColor& theColor,
951 bool theUpdateViewer)
953 if (!isVisible(theObject))
956 AISObjectPtr anAISObj = getAISObject(theObject);
958 anAISObj->getColor(aR, aG, aB);
959 anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
962 return QColor(aR, aG, aB);
965 //**************************************************************
966 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
969 myResult2AISObjectMap.add(theObject, theAIS);
971 myResult2AISObjectMap[theObject] = theAIS;
975 std::ostringstream aPtrStr;
976 aPtrStr << theObject.get();
977 qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
978 qDebug(getResult2AISObjectMapInfo().c_str());
983 //**************************************************************
984 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
986 QStringList aContent;
988 foreach(ObjectPtr aObj, myResult2AISObjectMap.objects()) {
989 AISObjectPtr aAISObj = myResult2AISObjectMap.value(aObj);
991 foreach(ObjectPtr aObj, myResult2AISObjectMap.keys()) {
992 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
994 std::ostringstream aPtrStr;
995 aPtrStr << "aObj = " << aObj.get() << ":";
996 aPtrStr << "anAIS = " << aAISObj.get() << ":";
997 aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
999 aContent.append(aPtrStr.str().c_str());
1001 return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
1002 arg(aContent.join("\n")).toStdString().c_str();
1006 //**************************************************************
1007 void XGUI_Displayer::getPresentations(const ObjectPtr& theObject,
1008 NCollection_Map<Handle(AIS_InteractiveObject)>& thePresentations)
1010 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1011 if (aResult.get()) {
1012 AISObjectPtr aAISObj = getAISObject(aResult);
1013 if (aAISObj.get() == NULL) {
1014 // if result is a result of a composite feature, it is visualized by visualization of
1015 // composite children, so we should get one of this presentations
1016 ResultBodyPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
1017 if (aCompSolid.get() && aCompSolid->numberOfSubs() > 0) {
1018 aAISObj = getAISObject(aCompSolid->subResult(0));
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);
1028 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
1029 // find presentation of the feature
1030 AISObjectPtr aAISObj = getAISObject(aFeature);
1031 if (aAISObj.get() != NULL) {
1032 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1033 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1034 thePresentations.Add(anAIS);
1036 // find presentations of the feature results
1037 std::list<ResultPtr> aResults;
1038 ModelAPI_Tools::allResults(aFeature, aResults);
1039 std::list<ResultPtr>::const_iterator anIt = aResults.begin(), aLast = aResults.end();
1040 for (; anIt != aLast; ++anIt) {
1041 AISObjectPtr aAISObj = getAISObject(*anIt);
1042 if (aAISObj.get() != NULL) {
1043 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1044 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1045 thePresentations.Add(anAIS);
1051 //**************************************************************
1052 void XGUI_Displayer::displayTrihedron(bool theToDisplay) const
1054 Handle(AIS_InteractiveContext) aContext = AISContext();
1055 if (aContext.IsNull())
1058 Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
1060 XGUI_SelectionActivate* aSelectionActive = selectionActivate();
1062 if (!aContext->IsDisplayed(aTrihedron))
1063 aContext->Display(aTrihedron,
1065 -1 /* selection mode */,
1066 Standard_True /* update viewer*/,
1067 Standard_False /* allow decomposition */,
1068 AIS_DS_Displayed /* xdisplay status */);
1070 if (getCallBack()) getCallBack()->Display(aTrihedron);
1073 if (!aSelectionActive->isTrihedronActive())
1074 aSelectionActive->deactivateTrihedron(false);
1076 aSelectionActive->activate(aTrihedron, false);
1078 aSelectionActive->deactivateTrihedron(false);
1080 aContext->Erase(aTrihedron, Standard_True);
1082 if (getCallBack()) getCallBack()->Remove(aTrihedron);
1089 //**************************************************************
1090 void XGUI_Displayer::AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
1091 const NCollection_DataMap<TopoDS_Shape,
1092 NCollection_Map<Handle(AIS_InteractiveObject)>>& theShapesToBeSelected)
1094 NCollection_Map<Handle(AIS_InteractiveObject)> aCompsolidPresentations;
1095 NCollection_Map<Handle(AIS_InteractiveObject)> aSelectedPresentations;
1097 NCollection_List<Handle(SelectBasics_EntityOwner)> anActiveOwners;
1098 theContext->MainSelector()->ActiveOwners(anActiveOwners);
1099 NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (anActiveOwners);
1100 Handle(SelectMgr_EntityOwner) anOwner;
1102 /// It is very important to check that the owner is processed only once and has a map of
1103 /// processed owners because SetSelected works as a switch.
1104 /// If count of calls setSelectec is even, the object stays in the previous state
1105 /// (selected, deselected)
1106 /// OCCT: to write about the problem that active owners method returns one owner several times
1107 QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
1108 for (; anOwnersIt.More(); anOwnersIt.Next()) {
1109 anOwner = Handle(SelectMgr_EntityOwner)::DownCast (anOwnersIt.Value());
1110 if (aSelectedIds.contains((size_t)anOwner.get()))
1112 aSelectedIds.append((size_t)anOwner.get());
1114 Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
1115 if (!BROwnr.IsNull() && BROwnr->HasShape()) {
1116 const TopoDS_Shape& aShape = BROwnr->Shape();
1117 if (aShape.IsNull())
1120 Handle(ModuleBase_BRepOwner) aCustomOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
1122 NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)> >
1123 ::Iterator aShapeIt(theShapesToBeSelected);
1124 for (; aShapeIt.More(); aShapeIt.Next()) {
1125 const TopoDS_Shape& aParameterShape = aShapeIt.Key();
1126 // isSame should be used here as it does not check orientation of shapes
1127 // despite on isEqual of shapes or IsBound for shape in QMap. Orientation is
1128 // different for Edges shapes in model shape and owner even if this is the same shape
1129 if (aParameterShape.IsSame(aShape)) {
1130 Handle(AIS_InteractiveObject) anOwnerPresentation =
1131 Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
1132 NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations =
1133 theShapesToBeSelected.Find(aParameterShape);
1134 if (aPresentations.Contains(anOwnerPresentation)) {
1135 theContext->AddOrRemoveSelected(anOwner, Standard_False);
1136 anOwner->SetSelected (Standard_True);
1137 // collect selected presentations to do not select them if compsolid is selected
1138 if (!aSelectedPresentations.Contains(anOwnerPresentation))
1139 aSelectedPresentations.Add(anOwnerPresentation);
1142 else if (!aCustomOwner.IsNull()) { // CompSolid processing #2219
1143 // shape of owner is compound, but shape to be selected is compsolid, so
1144 // we need to compare shape to AIS presentation of owner(rule of the owner creation)
1145 Handle(AIS_Shape) anOwnerPresentation =
1146 Handle(AIS_Shape)::DownCast(anOwner->Selectable());
1147 const TopoDS_Shape& aPresentationShape = anOwnerPresentation->Shape();
1148 if (aParameterShape.IsSame(anOwnerPresentation->Shape()) &&
1149 !aCompsolidPresentations.Contains(anOwnerPresentation))
1150 aCompsolidPresentations.Add(anOwnerPresentation);
1155 // select CompSolid presentations if their owners was not selected yet
1156 NCollection_Map<Handle(AIS_InteractiveObject)>::Iterator anIt (aCompsolidPresentations);
1157 for (; anIt.More(); anIt.Next()) {
1158 if (aSelectedPresentations.Contains(anIt.Value()))
1160 theContext->AddOrRemoveSelected(anIt.Value(), Standard_False);
1164 //**************************************************************
1165 XGUI_SelectionActivate* XGUI_Displayer::selectionActivate() const
1167 return myWorkshop->selectionActivate();
1170 //**************************************************************
1171 GeomPlanePtr XGUI_Displayer::getScreenPlane() const
1173 GeomPlanePtr aResult;
1174 Handle(AIS_InteractiveContext) aContext = AISContext();
1175 if (!aContext.IsNull()) {
1176 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1177 Handle(V3d_View) aView;
1178 for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews()) {
1179 aView = aViewer->ActiveView();
1182 if (!aView.IsNull()) {
1183 double aEyeX, aEyeY, aEyeZ;
1184 aView->Eye(aEyeX, aEyeY, aEyeZ);
1186 double aProjX, aProjY, aProjZ;
1187 aView->Proj(aProjX, aProjY, aProjZ);
1189 GeomPointPtr aPnt = GeomPointPtr(new GeomAPI_Pnt(aEyeX, aEyeY, aEyeZ));
1190 GeomDirPtr aDir = GeomDirPtr(new GeomAPI_Dir(aProjX, aProjY, aProjZ));
1192 aResult = GeomPlanePtr(new GeomAPI_Pln(aPnt, aDir));
1198 double XGUI_Displayer::getViewScale() const
1200 Handle(AIS_InteractiveContext) aContext = AISContext();
1201 if (!aContext.IsNull()) {
1202 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1203 Handle(V3d_View) aView;
1204 for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews()) {
1205 aView = aViewer->ActiveView();
1208 return aView->Camera()->Scale();