1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "XGUI_Displayer.h"
22 #include "XGUI_FacesPanel.h"
23 #include "XGUI_Selection.h"
24 #include "XGUI_SelectionActivate.h"
25 #include "XGUI_SelectionMgr.h"
26 #include "XGUI_ViewerProxy.h"
27 #include "XGUI_Workshop.h"
30 #include <AppElements_Viewer.h>
33 #include <ModelAPI_Document.h>
34 #include <ModelAPI_Data.h>
35 #include <ModelAPI_Object.h>
36 #include <ModelAPI_Tools.h>
37 #include <ModelAPI_AttributeIntArray.h>
38 #include <ModelAPI_ResultBody.h>
39 #include <ModelAPI_ResultConstruction.h>
41 #include <ModuleBase_BRepOwner.h>
42 #include <ModuleBase_IModule.h>
43 #include <ModuleBase_Preferences.h>
44 #include <ModuleBase_Tools.h>
45 #include <ModuleBase_ViewerPrs.h>
46 #include <ModuleBase_IViewer.h>
48 #include <GeomAPI_Shape.h>
49 #include <GeomAPI_IPresentable.h>
50 #include <GeomAPI_ICustomPrs.h>
51 #include <GeomAPI_Pnt.h>
52 #include <GeomAPI_IScreenParams.h>
54 #include <SUIT_ResourceMgr.h>
56 #include <AIS_InteractiveContext.hxx>
57 #include <AIS_ListOfInteractive.hxx>
58 #include <AIS_ListIteratorOfListOfInteractive.hxx>
59 #include <AIS_DimensionSelectionMode.hxx>
60 #include <AIS_Shape.hxx>
61 #include <AIS_Dimension.hxx>
62 #include <AIS_Trihedron.hxx>
63 #ifdef BEFORE_TRIHEDRON_PATCH
64 #include <AIS_Axis.hxx>
65 #include <AIS_Plane.hxx>
66 #include <AIS_Point.hxx>
68 #include <AIS_Selection.hxx>
69 #include <Prs3d_Drawer.hxx>
70 #include <Prs3d_IsoAspect.hxx>
71 #include <SelectMgr_ListOfFilter.hxx>
72 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
73 #include <SelectMgr_SelectionManager.hxx>
74 #include <TColStd_ListIteratorOfListOfInteger.hxx>
76 #include <StdSelect_ViewerSelector3d.hxx>
78 #include <TColStd_MapOfTransient.hxx>
79 #include <TColStd_MapIteratorOfMapOfTransient.hxx>
82 #include <inspector/VInspectorAPI_CallBack.hxx>
85 #include <Events_Loop.h>
86 #include <ModelAPI_Events.h>
87 #include <Config_PropManager.h>
91 /// defines the local context mouse selection sensitivity
92 const int MOUSE_SENSITIVITY_IN_PIXEL = 10;
94 //#define DEBUG_DISPLAY
95 //#define DEBUG_FEATURE_REDISPLAY
96 //#define DEBUG_SELECTION_FILTERS
98 //#define DEBUG_OCCT_SHAPE_SELECTION
100 #define CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
102 //#define DEBUG_VIEWER_BLOCKED_COUNT
104 //**************************************************************
105 void displayedObjects(const Handle(AIS_InteractiveContext)& theAIS, AIS_ListOfInteractive& theList)
107 // Get from null point
108 theAIS->DisplayedObjects(theList, true);
111 QString qIntListInfo(const QIntList& theValues, const QString& theSeparator = QString(", "))
114 QIntList::const_iterator anIt = theValues.begin(), aLast = theValues.end();
115 for (; anIt != aLast; anIt++) {
116 anInfo.append(QString::number(*anIt));
118 return anInfo.join(theSeparator);
121 //**************************************************************
122 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
123 : myWorkshop(theWorkshop), myNeedUpdate(false),
124 myViewerBlockedRecursiveCount(0), myContextId(0)
128 //**************************************************************
129 XGUI_Displayer::~XGUI_Displayer()
133 //**************************************************************
134 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
136 return myResult2AISObjectMap.contains(theObject);
139 //**************************************************************
140 bool XGUI_Displayer::display(ObjectPtr theObject, bool theUpdateViewer)
142 bool aDisplayed = false;
143 if (isVisible(theObject)) {
144 aDisplayed = redisplay(theObject, theUpdateViewer);
147 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
148 bool isShading = false;
149 if (aPrs.get() != NULL) {
150 GeomScreenParamsPtr aScreen = std::dynamic_pointer_cast<GeomAPI_IScreenParams>(theObject);
152 aScreen->setScreenPlane(getScreenPlane());
153 aScreen->setViewScale(getViewScale());
155 anAIS = aPrs->getAISObject(anAIS);
157 anAIS = myWorkshop->module()->createPresentation(theObject);
161 aDisplayed = display(theObject, anAIS, isShading, theUpdateViewer);
167 //**************************************************************
168 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS, ModuleBase_IModule* theModule)
170 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
171 if (!aShapePrs.IsNull()) {
172 TopoDS_Shape aShape = aShapePrs->Shape();
175 TopAbs_ShapeEnum aType = aShape.ShapeType();
176 if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
179 // Check that the presentation is not a sketch
180 return theModule->canBeShaded(theAIS);
186 //**************************************************************
187 bool XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
188 bool isShading, bool theUpdateViewer)
190 bool aDisplayed = false;
192 Handle(AIS_InteractiveContext) aContext = AISContext();
193 if (aContext.IsNull())
196 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
197 if (!anAISIO.IsNull()) {
198 appendResultObject(theObject, theAIS);
200 //bool isCustomized = customizeObject(theObject);
202 int aDispMode = isShading? Shading : Wireframe;
204 anAISIO->Attributes()->SetFaceBoundaryDraw( Standard_True );
205 anAISIO->SetDisplayMode(aDispMode);
206 aContext->Display(anAISIO, aDispMode, 0, false, true, AIS_DS_Displayed);
208 if (getCallBack()) getCallBack()->Display(anAISIO);
212 emit objectDisplayed(theObject, theAIS);
213 selectionActivate()->activate(anAISIO, theUpdateViewer);
221 //**************************************************************
222 bool XGUI_Displayer::erase(ObjectPtr theObject, const bool theUpdateViewer)
224 bool aErased = false;
225 if (!isVisible(theObject))
228 Handle(AIS_InteractiveContext) aContext = AISContext();
229 if (aContext.IsNull())
232 AISObjectPtr anObject = myResult2AISObjectMap.value(theObject);
234 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
235 if (!anAIS.IsNull()) {
236 emit beforeObjectErase(theObject, anObject);
237 aContext->Remove(anAIS, false/*update viewer*/);
239 if (getCallBack()) getCallBack()->Remove(anAIS);
244 myResult2AISObjectMap.remove(theObject);
247 std::ostringstream aPtrStr;
248 aPtrStr << theObject.get();
249 qDebug(QString("erase object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
250 qDebug(getResult2AISObjectMapInfo().c_str());
259 //**************************************************************
260 bool XGUI_Displayer::redisplay(ObjectPtr theObject, bool theUpdateViewer)
262 bool aRedisplayed = false;
263 Handle(AIS_InteractiveContext) aContext = AISContext();
264 if (aContext.IsNull())
267 if (!isVisible(theObject))
270 AISObjectPtr aAISObj = getAISObject(theObject);
271 Handle(AIS_InteractiveObject) aAISIO;
273 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
275 GeomScreenParamsPtr aScreen = std::dynamic_pointer_cast<GeomAPI_IScreenParams>(theObject);
277 aScreen->setScreenPlane(getScreenPlane());
278 aScreen->setViewScale(getViewScale());
280 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
282 aRedisplayed = erase(theObject, theUpdateViewer);
285 if (aAIS_Obj != aAISObj) {
286 erase(theObject, theUpdateViewer);
287 appendResultObject(theObject, aAIS_Obj);
289 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
292 aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
295 if (!aAISIO.IsNull()) {
296 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
299 std::vector<int> aColor;
300 ModelAPI_Tools::getColor(aResult, aColor);
301 if (aColor.size() > 0) {
303 aCol(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB);
304 aAISIO->SetColor(aCol);
307 double aDeflection = ModelAPI_Tools::getDeflection(aResult);
308 if ((aDeflection >= 0) && (aDeflection != aAISObj->getDeflection()))
309 aAISObj->setDeflection(aDeflection);
312 double aTransparency = ModelAPI_Tools::getTransparency(aResult);
313 if ((aTransparency >= 0) && (aTransparency != aAISObj->getTransparency()))
314 aAISObj->setTransparency(aTransparency);
316 myWorkshop->module()->storeSelection();
318 #ifdef CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
319 myWorkshop->selector()->deselectPresentation(aAISIO);
322 if (aContext->IsDisplayed(aAISIO))
323 aContext->Redisplay(aAISIO, false);
325 aContext->Display(aAISIO, false);
328 if (getCallBack()) getCallBack()->Redisplay(aAISIO);
331 //if (aNeedToRestoreSelection)
332 myWorkshop->module()->restoreSelection();
335 #ifdef DEBUG_FEATURE_REDISPLAY
336 qDebug(" Redisplay happens");
344 //**************************************************************
345 void XGUI_Displayer::redisplayObjects()
347 // redisplay objects visualized in the viewer
348 static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
349 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
350 QObjectPtrList aDisplayed = displayedObjects();
351 QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
352 for (; anIt != aLast; anIt++) {
353 aECreator->sendUpdated(*anIt, EVENT_DISP);
355 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
356 if (aViewer->isColorScaleVisible()) {
357 aViewer->setupColorScale();
358 aViewer->setColorScaleShown(true);
360 Events_Loop::loop()->flush(EVENT_DISP);
363 //**************************************************************
364 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
365 const bool theUpdateViewer)
367 //Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
368 //if (!aTrihedron.IsNull())
369 // deactivateAIS(aTrihedron);
371 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
372 for (; anIt != aLast; anIt++) {
373 selectionActivate()->deactivate(*anIt, false);
375 //VSV It seems that there is no necessity to update viewer on deactivation
376 //if (theUpdateViewer)
380 //**************************************************************
381 bool XGUI_Displayer::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
383 bool aVisible = false;
384 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
385 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
386 if (aPrs.get() || aResult.get()) {
387 aVisible = theDisplayer->isVisible(theObject);
388 // compsolid is not visualized in the viewer,
389 // but should have presentation when all sub solids are
390 // visible. It is useful for highlight presentation where compsolid shape is selectable
391 if (!aVisible && aResult.get() && aResult->groupName() == ModelAPI_ResultBody::group()) {
392 ResultBodyPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
393 if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
394 int aNumberOfSubs = aCompsolidResult->numberOfSubs();
395 bool anAllSubsVisible = aNumberOfSubs > 0;
396 for(int i = 0; i < aNumberOfSubs && anAllSubsVisible; i++) {
397 anAllSubsVisible = theDisplayer->isVisible(aCompsolidResult->subResult(i));
399 aVisible = anAllSubsVisible;
403 // it is possible that feature is presentable and has results, so we should check visibility
404 // of results if presentation is not shown (e.g. Sketch Circle/Arc features)
406 // check if all results of the feature are visible
407 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
408 std::list<ResultPtr> aResults;
409 ModelAPI_Tools::allResults(aFeature, aResults);
410 std::list<ResultPtr>::const_iterator aIt;
411 aVisible = !aResults.empty();
412 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
413 aVisible = aVisible && theDisplayer->isVisible(*aIt);
419 //**************************************************************
420 void XGUI_Displayer::setSelected(const QList<ModuleBase_ViewerPrsPtr>& theValues,
421 bool theUpdateViewer)
423 Handle(AIS_InteractiveContext) aContext = AISContext();
424 if (aContext.IsNull())
426 aContext->UnhilightSelected(false);
427 aContext->ClearSelected(false);
429 if (getCallBack()) getCallBack()->ClearSelected();
431 NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)>>
434 foreach (ModuleBase_ViewerPrsPtr aPrs, theValues) {
435 const GeomShapePtr& aGeomShape = aPrs->shape();
436 if (aGeomShape.get() && !aGeomShape->isNull()) {
437 const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
438 #ifdef DEBUG_OCCT_SHAPE_SELECTION
439 // problem 1: performance
440 // problem 2: IO is not specified, so the first found owner is selected, as a result
441 // it might belong to another result
442 aContext->AddOrRemoveSelected(aShape, false);
444 if (getCallBack()) getCallBack()->AddOrRemoveSelected(aShape);
447 NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations;
448 if (aShapesToBeSelected.IsBound(aShape))
449 aPresentations = aShapesToBeSelected.Find(aShape);
450 ObjectPtr anObject = aPrs->object();
451 getPresentations(anObject, aPresentations);
453 aShapesToBeSelected.Bind(aShape, aPresentations);
456 ObjectPtr anObject = aPrs->object();
457 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
458 if (aResult.get() && isVisible(aResult)) {
459 AISObjectPtr anObj = myResult2AISObjectMap.value(aResult);
460 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
461 if (!anAIS.IsNull()) {
462 // The methods are replaced in order to provide multi-selection, e.g. restore selection
463 // by activating multi selector widget. It also gives an advantage that the multi
464 // selection in OB gives multi-selection in the viewer
465 //aContext->SetSelected(anAIS, false);
466 // The selection in the context was cleared, so the method sets the objects are selected
467 aContext->AddOrRemoveSelected(anAIS, false);
469 if (getCallBack()) getCallBack()->AddOrRemoveSelected(anAIS);
475 if (!aShapesToBeSelected.IsEmpty())
476 XGUI_Displayer::AddOrRemoveSelectedShapes(aContext, aShapesToBeSelected);
482 //**************************************************************
483 void XGUI_Displayer::clearSelected(const bool theUpdateViewer)
485 Handle(AIS_InteractiveContext) aContext = AISContext();
486 if (!aContext.IsNull()) {
487 aContext->UnhilightSelected(false);//UnhilightCurrents(false);
488 aContext->ClearSelected(theUpdateViewer);
490 if (getCallBack()) getCallBack()->ClearSelected();
495 //**************************************************************
496 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
498 bool aErased = false;
499 Handle(AIS_InteractiveContext) aContext = AISContext();
500 if (!aContext.IsNull()) {
502 foreach(ObjectPtr aObj, myResult2AISObjectMap.objects()) {
503 AISObjectPtr aAISObj = myResult2AISObjectMap.value(aObj);
505 foreach(ObjectPtr aObj, myResult2AISObjectMap.keys()) {
506 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
509 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
510 if (!anIO.IsNull()) {
511 emit beforeObjectErase(aObj, aAISObj);
512 aContext->Remove(anIO, false/*update viewer*/);
514 if (getCallBack()) getCallBack()->Remove(anIO);
522 myResult2AISObjectMap.clear();
525 qDebug(getResult2AISObjectMapInfo().c_str());
531 //**************************************************************
532 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
535 return myResult2AISObjectMap.value(theObject);
538 if (myResult2AISObjectMap.contains(theObject))
539 anIO = myResult2AISObjectMap[theObject];
544 //**************************************************************
545 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
547 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
548 return getObject(aRefAIS);
551 //**************************************************************
552 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
555 ObjectPtr anObject = myResult2AISObjectMap.value(theIO);
558 ResultToAISMap::const_iterator aMapIter = myResult2AISObjectMap.cbegin();
559 for (; aMapIter != myResult2AISObjectMap.cend(); aMapIter++) {
560 const AISObjectPtr& aAIS = aMapIter.value();
561 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
563 anObject = aMapIter.key();
568 if (!anObject.get()) {
569 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
570 if (!theIO.IsNull()) {
571 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
573 anObject = myWorkshop->module()->findPresentedObject(anAISObj);
578 //**************************************************************
579 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
581 bool aWasEnabled = isUpdateEnabled();
583 myViewerBlockedRecursiveCount--;
585 myViewerBlockedRecursiveCount++;
587 #ifdef DEBUG_VIEWER_BLOCKED_COUNT
588 std::cout << "myViewerBlockedRecursiveCount = " << myViewerBlockedRecursiveCount << std::endl;
591 if (myNeedUpdate && isUpdateEnabled()) {
593 myNeedUpdate = false;
598 //**************************************************************
599 void XGUI_Displayer::updateViewer() const
601 Handle(AIS_InteractiveContext) aContext = AISContext();
603 #ifdef DEBUG_VIEWER_BLOCKED_COUNT
604 std::cout << "updateViewer: " << (myViewerBlockedRecursiveCount == 0 ? " done" : " later")
608 if (!aContext.IsNull() && isUpdateEnabled()) {
609 //myWorkshop->viewer()->Zfitall();
610 aContext->UpdateCurrentViewer();
616 //**************************************************************
617 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
619 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
620 if (!aContext.IsNull() && (myContextId != aContext.get())) {
621 myContextId = aContext.get();
622 if (!myWorkshop->selectionActivate()->isTrihedronActive())
623 selectionActivate()->deactivateTrihedron(true);
624 aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
625 aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
627 //Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
628 //aTrihedron->getHighlightPointAspect()->SetScale(2.0);
629 //aTrihedron->getHighlightPointAspect()->SetTypeOfMarker(Aspect_TOM_O_STAR);
631 // Commented out according to discussion in bug #2825
632 //ModuleBase_IViewer::DefaultHighlightDrawer = aContext->HighlightStyle();
633 //Handle(Prs3d_Drawer) aSelStyle = aContext->SelectionStyle();
634 //double aDeflection =
635 // QString(ModelAPI_ResultConstruction::DEFAULT_DEFLECTION().c_str()).toDouble();
637 // aDeflection = Config_PropManager::real("Visualization", "construction_deflection");
640 //ModuleBase_IViewer::DefaultHighlightDrawer->SetDeviationCoefficient(aDeflection);
641 //aSelStyle->SetDeviationCoefficient(aDeflection);
646 //**************************************************************
647 void XGUI_Displayer::setSelectionColor(const std::vector<int>& theColor)
649 Handle(AIS_InteractiveContext) aContext = AISContext();
650 if (!aContext.IsNull()) {
651 Quantity_Color aQColor(theColor[0] / 255.,
653 theColor[2] / 255., Quantity_TOC_RGB);
654 aContext->SelectionStyle()->SetColor(aQColor);
655 aContext->SelectionStyle()->PointAspect()->SetColor(aQColor);
656 aContext->SelectionStyle()->LineAspect()->SetColor(aQColor);
657 aContext->HighlightStyle(Prs3d_TypeOfHighlight_LocalSelected)->SetColor(aQColor);
662 //**************************************************************
663 std::vector<int> XGUI_Displayer::selectionColor() const
665 std::vector<int> aColor;
666 Handle(AIS_InteractiveContext) aContext = AISContext();
667 if (!aContext.IsNull()) {
668 Quantity_Color aQColor = aContext->SelectionStyle()->Color();
669 aColor.push_back((int)(aQColor.Red() * 255));
670 aColor.push_back((int)(aQColor.Green() * 255));
671 aColor.push_back((int)(aQColor.Blue() * 255));
677 //**************************************************************
678 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
680 Handle(AIS_InteractiveContext) aContext = AISContext();
681 if (!aContext.IsNull() && myAndFilter.IsNull()) {
682 myAndFilter = new SelectMgr_AndFilter();
683 aContext->AddFilter(myAndFilter);
688 //**************************************************************
689 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
690 const Standard_Integer theDisplayMode, bool theUpdateViewer)
692 bool aDisplayed = false;
693 Handle(AIS_InteractiveContext) aContext = AISContext();
694 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
695 if (!aContext.IsNull() && !anAISIO.IsNull()) {
696 aContext->Display(anAISIO, theDisplayMode, 0, false/*update viewer*/, true, AIS_DS_Displayed);
698 if (getCallBack()) getCallBack()->Display(anAISIO);
701 aContext->Deactivate(anAISIO);
703 if (getCallBack()) getCallBack()->Deactivate(anAISIO);
705 aContext->Load(anAISIO);
707 if (getCallBack()) getCallBack()->Load(anAISIO);
709 if (toActivateInSelectionModes)
710 myWorkshop->selectionActivate()->activateOnDisplay(anAISIO, theUpdateViewer);
718 //**************************************************************
719 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
721 bool aErased = false;
722 Handle(AIS_InteractiveContext) aContext = AISContext();
723 if (!aContext.IsNull()) {
724 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
725 if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
726 aContext->Remove(anAISIO, false/*update viewer*/);
728 if (getCallBack()) getCallBack()->Remove(anAISIO);
733 if (aErased && theUpdateViewer)
738 //**************************************************************
739 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
741 if (theMode == NoMode)
744 Handle(AIS_InteractiveContext) aContext = AISContext();
745 if (aContext.IsNull())
748 AISObjectPtr aAISObj = getAISObject(theObject);
752 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
753 aContext->SetDisplayMode(aAISIO, theMode, false);
754 // Redisplay in order to update new mode because it could be not computed before
759 //**************************************************************
760 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
762 Handle(AIS_InteractiveContext) aContext = AISContext();
763 if (aContext.IsNull())
766 AISObjectPtr aAISObj = getAISObject(theObject);
770 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
771 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
774 //**************************************************************
775 void XGUI_Displayer::deactivateSelectionFilters(const bool theAddFilterOnly)
777 Handle(AIS_InteractiveContext) aContext = AISContext();
778 if (!myAndFilter.IsNull()) {
780 if (!aContext.IsNull()) {
781 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
782 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
783 for (; anIt.More() && !aFound; anIt.Next()) {
784 Handle(SelectMgr_Filter) aFilter = anIt.Value();
785 aFound = aFilter == myAndFilter;
788 aContext->RemoveFilter(myAndFilter);
790 myAndFilter.Nullify();
794 //**************************************************************
795 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
797 Handle(AIS_InteractiveContext) aContext = AISContext();
798 if (aContext.IsNull() || hasSelectionFilter(theFilter))
801 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
802 if (!aCompositeFilter.IsNull()) {
803 aCompositeFilter->Add(theFilter);
804 #ifdef DEBUG_SELECTION_FILTERS
805 int aCount = aCompositeFilter->StoredFilters().Extent();
806 qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
811 //**************************************************************
812 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
814 Handle(AIS_InteractiveContext) aContext = AISContext();
815 if (aContext.IsNull())
818 Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
819 if (!aCompositeFilter.IsNull() && aCompositeFilter->IsIn(theFilter)) {
820 aCompositeFilter->Remove(theFilter);
821 #ifdef DEBUG_SELECTION_FILTERS
822 int aCount = aCompositeFilter->StoredFilters().Extent();
823 qDebug(QString("removeSelectionFilter: filters.count() = %1")
824 .arg(aCount).toStdString().c_str());
829 //**************************************************************
830 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
832 bool aFilterFound = false;
834 Handle(AIS_InteractiveContext) aContext = AISContext();
835 if (aContext.IsNull())
837 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
838 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
839 for (; aIt.More() && !aFilterFound; aIt.Next()) {
840 if (theFilter.get() == aIt.Value().get())
843 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
844 if (!aCompositeFilter.IsNull()) {
845 const SelectMgr_ListOfFilter& aStoredFilters = aCompositeFilter->StoredFilters();
846 for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
847 if (theFilter.get() == aIt.Value().get())
854 //**************************************************************
855 void XGUI_Displayer::removeFilters()
857 Handle(AIS_InteractiveContext) aContext = AISContext();
858 if (aContext.IsNull())
861 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
862 if (!aCompositeFilter.IsNull())
863 aCompositeFilter->Clear();
866 //**************************************************************
867 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
870 QObjectPtrList aDispList = myResult2AISObjectMap.objects();
872 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
874 foreach(ObjectPtr aObj, aDispList) {
875 if (!theList.contains(aObj))
878 foreach(ObjectPtr aObj, theList) {
879 if (!isVisible(aObj))
880 display(aObj, false);
885 //**************************************************************
886 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
888 if (!isVisible(theObject))
891 AISObjectPtr aAISObj = getAISObject(theObject);
892 if (aAISObj.get() == NULL)
895 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
896 return ::canBeShaded(anAIS, myWorkshop->module());
899 //**************************************************************
900 //bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
902 // AISObjectPtr anAISObj = getAISObject(theObject);
903 // // correct the result's color it it has the attribute
904 // ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
906 // // Customization of presentation
907 // GeomCustomPrsPtr aCustomPrs;
908 // FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
909 // if (aFeature.get() != NULL) {
910 // GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
911 // if (aCustPrs.get() != NULL)
912 // aCustomPrs = aCustPrs;
914 // if (aCustomPrs.get() == NULL) {
915 // GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
916 // // we ignore presentable not customized objects
917 // if (aPrs.get() == NULL)
918 // aCustomPrs = myCustomPrs;
920 // bool isCustomized = aCustomPrs.get() &&
921 // aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
922 // isCustomized = myWorkshop->module()->afterCustomisePresentation(aResult, anAISObj, myCustomPrs)
925 // // update presentation state if faces panel is active
926 // if (anAISObj.get() && myWorkshop->facesPanel())
927 // isCustomized = myWorkshop->facesPanel()->customizeObject(theObject, anAISObj) || isCustomized;
929 // return isCustomized;
932 //**************************************************************
933 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject,
934 const QColor& theColor,
935 bool theUpdateViewer)
937 if (!isVisible(theObject))
940 AISObjectPtr anAISObj = getAISObject(theObject);
942 anAISObj->getColor(aR, aG, aB);
943 anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
946 return QColor(aR, aG, aB);
949 //**************************************************************
950 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
953 myResult2AISObjectMap.add(theObject, theAIS);
955 myResult2AISObjectMap[theObject] = theAIS;
959 std::ostringstream aPtrStr;
960 aPtrStr << theObject.get();
961 qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
962 qDebug(getResult2AISObjectMapInfo().c_str());
967 //**************************************************************
968 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
970 QStringList aContent;
972 foreach(ObjectPtr aObj, myResult2AISObjectMap.objects()) {
973 AISObjectPtr aAISObj = myResult2AISObjectMap.value(aObj);
975 foreach(ObjectPtr aObj, myResult2AISObjectMap.keys()) {
976 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
978 std::ostringstream aPtrStr;
979 aPtrStr << "aObj = " << aObj.get() << ":";
980 aPtrStr << "anAIS = " << aAISObj.get() << ":";
981 aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
983 aContent.append(aPtrStr.str().c_str());
985 return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
986 arg(aContent.join("\n")).toStdString().c_str();
990 //**************************************************************
991 void XGUI_Displayer::getPresentations(const ObjectPtr& theObject,
992 NCollection_Map<Handle(AIS_InteractiveObject)>& thePresentations)
994 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
996 AISObjectPtr aAISObj = getAISObject(aResult);
997 if (aAISObj.get() == NULL) {
998 // if result is a result of a composite feature, it is visualized by visualization of
999 // composite children, so we should get one of this presentations
1000 ResultBodyPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
1001 if (aCompSolid.get() && aCompSolid->numberOfSubs() > 0) {
1002 aAISObj = getAISObject(aCompSolid->subResult(0));
1005 if (aAISObj.get() != NULL) {
1006 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1007 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1008 thePresentations.Add(anAIS);
1012 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
1013 // find presentation of the feature
1014 AISObjectPtr aAISObj = getAISObject(aFeature);
1015 if (aAISObj.get() != NULL) {
1016 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1017 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1018 thePresentations.Add(anAIS);
1020 // find presentations of the feature results
1021 std::list<ResultPtr> aResults;
1022 ModelAPI_Tools::allResults(aFeature, aResults);
1023 std::list<ResultPtr>::const_iterator anIt = aResults.begin(), aLast = aResults.end();
1024 for (; anIt != aLast; ++anIt) {
1025 AISObjectPtr aAISObj = getAISObject(*anIt);
1026 if (aAISObj.get() != NULL) {
1027 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1028 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1029 thePresentations.Add(anAIS);
1035 //**************************************************************
1036 void XGUI_Displayer::displayTrihedron(bool theToDisplay) const
1038 Handle(AIS_InteractiveContext) aContext = AISContext();
1039 if (aContext.IsNull())
1042 Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
1044 XGUI_SelectionActivate* aSelectionActive = selectionActivate();
1046 if (!aContext->IsDisplayed(aTrihedron))
1047 aContext->Display(aTrihedron,
1049 -1 /* selection mode */,
1050 Standard_True /* update viewer*/,
1051 Standard_False /* allow decomposition */,
1052 AIS_DS_Displayed /* xdisplay status */);
1054 if (getCallBack()) getCallBack()->Display(aTrihedron);
1057 if (!aSelectionActive->isTrihedronActive())
1058 aSelectionActive->deactivateTrihedron(false);
1060 aSelectionActive->activate(aTrihedron, false);
1062 aSelectionActive->deactivateTrihedron(false);
1064 aContext->Erase(aTrihedron, Standard_True);
1066 if (getCallBack()) getCallBack()->Remove(aTrihedron);
1071 //**************************************************************
1072 void XGUI_Displayer::AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
1073 const NCollection_DataMap<TopoDS_Shape,
1074 NCollection_Map<Handle(AIS_InteractiveObject)>>& theShapesToBeSelected)
1076 NCollection_Map<Handle(AIS_InteractiveObject)> aCompsolidPresentations;
1077 NCollection_Map<Handle(AIS_InteractiveObject)> aSelectedPresentations;
1079 NCollection_List<Handle(SelectBasics_EntityOwner)> anActiveOwners;
1080 theContext->MainSelector()->ActiveOwners(anActiveOwners);
1081 NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (anActiveOwners);
1082 Handle(SelectMgr_EntityOwner) anOwner;
1084 /// It is very important to check that the owner is processed only once and has a map of
1085 /// processed owners because SetSelected works as a switch.
1086 /// If count of calls setSelectec is even, the object stays in the previous state
1087 /// (selected, deselected)
1088 /// OCCT: to write about the problem that active owners method returns one owner several times
1089 QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
1090 for (; anOwnersIt.More(); anOwnersIt.Next()) {
1091 anOwner = Handle(SelectMgr_EntityOwner)::DownCast (anOwnersIt.Value());
1092 if (aSelectedIds.contains((size_t)anOwner.get()))
1094 aSelectedIds.append((size_t)anOwner.get());
1096 Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
1097 if (!BROwnr.IsNull() && BROwnr->HasShape()) {
1098 const TopoDS_Shape& aShape = BROwnr->Shape();
1099 if (aShape.IsNull())
1102 Handle(ModuleBase_BRepOwner) aCustomOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
1104 NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)> >
1105 ::Iterator aShapeIt(theShapesToBeSelected);
1106 for (; aShapeIt.More(); aShapeIt.Next()) {
1107 const TopoDS_Shape& aParameterShape = aShapeIt.Key();
1108 // In case of compound we cannot rely on simple comparison method.
1109 // If the compound is generated by Group feature then this compound is alwais new.
1110 // So, we have to compare content of these compounds
1112 // isSame should be used here as it does not check orientation of shapes
1113 // despite on isEqual of shapes or IsBound for shape in QMap. Orientation is
1114 // different for Edges shapes in model shape and owner even if this is the same shape
1115 if (ModuleBase_Tools::isSameShape(aParameterShape, aShape)) {
1116 Handle(AIS_InteractiveObject) anOwnerPresentation =
1117 Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
1118 NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations =
1119 theShapesToBeSelected.Find(aParameterShape);
1120 if (aPresentations.Contains(anOwnerPresentation)) {
1121 theContext->AddOrRemoveSelected(anOwner, Standard_False);
1122 anOwner->SetSelected(Standard_True);
1123 // collect selected presentations to do not select them if compsolid is selected
1124 if (!aSelectedPresentations.Contains(anOwnerPresentation))
1125 aSelectedPresentations.Add(anOwnerPresentation);
1128 else if (!aCustomOwner.IsNull()) { // CompSolid processing #2219
1129 // shape of owner is compound, but shape to be selected is compsolid, so
1130 // we need to compare shape to AIS presentation of owner(rule of the owner creation)
1131 Handle(AIS_Shape) anOwnerPresentation =
1132 Handle(AIS_Shape)::DownCast(anOwner->Selectable());
1133 const TopoDS_Shape& aPresentationShape = anOwnerPresentation->Shape();
1134 if (aParameterShape.IsSame(anOwnerPresentation->Shape()) &&
1135 !aCompsolidPresentations.Contains(anOwnerPresentation))
1136 aCompsolidPresentations.Add(anOwnerPresentation);
1141 // select CompSolid presentations if their owners was not selected yet
1142 NCollection_Map<Handle(AIS_InteractiveObject)>::Iterator anIt (aCompsolidPresentations);
1143 for (; anIt.More(); anIt.Next()) {
1144 if (aSelectedPresentations.Contains(anIt.Value()))
1146 theContext->AddOrRemoveSelected(anIt.Value(), Standard_False);
1150 //**************************************************************
1151 XGUI_SelectionActivate* XGUI_Displayer::selectionActivate() const
1153 return myWorkshop->selectionActivate();
1156 //**************************************************************
1157 GeomPlanePtr XGUI_Displayer::getScreenPlane() const
1159 GeomPlanePtr aResult;
1160 Handle(AIS_InteractiveContext) aContext = AISContext();
1161 if (!aContext.IsNull()) {
1162 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1163 Handle(V3d_View) aView;
1164 for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews()) {
1165 aView = aViewer->ActiveView();
1168 if (!aView.IsNull()) {
1169 double aEyeX, aEyeY, aEyeZ;
1170 aView->Eye(aEyeX, aEyeY, aEyeZ);
1172 double aProjX, aProjY, aProjZ;
1173 aView->Proj(aProjX, aProjY, aProjZ);
1175 GeomPointPtr aPnt = GeomPointPtr(new GeomAPI_Pnt(aEyeX, aEyeY, aEyeZ));
1176 GeomDirPtr aDir = GeomDirPtr(new GeomAPI_Dir(aProjX, aProjY, aProjZ));
1178 aResult = GeomPlanePtr(new GeomAPI_Pln(aPnt, aDir));
1184 double XGUI_Displayer::getViewScale() const
1186 Handle(AIS_InteractiveContext) aContext = AISContext();
1187 if (!aContext.IsNull()) {
1188 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1189 Handle(V3d_View) aView;
1190 for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews()) {
1191 aView = aViewer->ActiveView();
1194 return aView->Camera()->Scale();