1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "XGUI_Displayer.h"
22 #include "XGUI_FacesPanel.h"
23 #include "XGUI_Selection.h"
24 #include "XGUI_SelectionActivate.h"
25 #include "XGUI_SelectionMgr.h"
26 #include "XGUI_ViewerProxy.h"
27 #include "XGUI_Workshop.h"
30 #include <AppElements_Viewer.h>
33 #include <ModelAPI_Document.h>
34 #include <ModelAPI_Data.h>
35 #include <ModelAPI_Object.h>
36 #include <ModelAPI_Tools.h>
37 #include <ModelAPI_AttributeIntArray.h>
38 #include <ModelAPI_ResultBody.h>
39 #include <ModelAPI_ResultConstruction.h>
41 #include <ModuleBase_BRepOwner.h>
42 #include <ModuleBase_IModule.h>
43 #include <ModuleBase_Preferences.h>
44 #include <ModuleBase_Tools.h>
45 #include <ModuleBase_ViewerPrs.h>
46 #include <ModuleBase_IViewer.h>
48 #include <GeomAPI_Shape.h>
49 #include <GeomAPI_IPresentable.h>
50 #include <GeomAPI_ICustomPrs.h>
51 #include <GeomAPI_Pnt.h>
52 #include <GeomAPI_IScreenParams.h>
54 #include <SUIT_ResourceMgr.h>
56 #include <AIS_InteractiveContext.hxx>
57 #include <AIS_ListOfInteractive.hxx>
58 #include <AIS_ListIteratorOfListOfInteractive.hxx>
59 #include <AIS_DimensionSelectionMode.hxx>
60 #include <AIS_Shape.hxx>
61 #include <AIS_Dimension.hxx>
62 #include <AIS_Trihedron.hxx>
63 #ifdef BEFORE_TRIHEDRON_PATCH
64 #include <AIS_Axis.hxx>
65 #include <AIS_Plane.hxx>
66 #include <AIS_Point.hxx>
68 #include <AIS_Selection.hxx>
69 #include <Prs3d_Drawer.hxx>
70 #include <Prs3d_IsoAspect.hxx>
71 #include <SelectMgr_ListOfFilter.hxx>
72 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
73 #include <SelectMgr_SelectionManager.hxx>
74 #include <TColStd_ListIteratorOfListOfInteger.hxx>
76 #include <StdSelect_ViewerSelector3d.hxx>
78 #include <TColStd_MapOfTransient.hxx>
79 #include <TColStd_MapIteratorOfMapOfTransient.hxx>
82 #include <inspector/VInspectorAPI_CallBack.hxx>
85 #include <Events_Loop.h>
86 #include <ModelAPI_Events.h>
87 #include <Config_PropManager.h>
91 /// defines the local context mouse selection sensitivity
92 const int MOUSE_SENSITIVITY_IN_PIXEL = 10;
94 //#define DEBUG_DISPLAY
95 //#define DEBUG_FEATURE_REDISPLAY
96 //#define DEBUG_SELECTION_FILTERS
98 //#define DEBUG_OCCT_SHAPE_SELECTION
100 #define CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
102 //#define DEBUG_VIEWER_BLOCKED_COUNT
104 //**************************************************************
105 void displayedObjects(const Handle(AIS_InteractiveContext)& theAIS, AIS_ListOfInteractive& theList)
107 // Get from null point
108 #if OCC_VERSION_HEX < 0x070400
109 theAIS->DisplayedObjects(theList, true);
111 theAIS->DisplayedObjects(theList);
115 QString qIntListInfo(const QIntList& theValues, const QString& theSeparator = QString(", "))
118 QIntList::const_iterator anIt = theValues.begin(), aLast = theValues.end();
119 for (; anIt != aLast; anIt++) {
120 anInfo.append(QString::number(*anIt));
122 return anInfo.join(theSeparator);
125 //**************************************************************
126 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
127 : myWorkshop(theWorkshop), myNeedUpdate(false),
128 myViewerBlockedRecursiveCount(0), myContextId(0)
132 //**************************************************************
133 XGUI_Displayer::~XGUI_Displayer()
137 //**************************************************************
138 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
140 return myResult2AISObjectMap.contains(theObject);
143 //**************************************************************
144 bool XGUI_Displayer::display(ObjectPtr theObject, bool theUpdateViewer)
146 bool aDisplayed = false;
147 if (isVisible(theObject)) {
148 aDisplayed = redisplay(theObject, theUpdateViewer);
151 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
152 bool isShading = false;
153 if (aPrs.get() != NULL) {
154 GeomScreenParamsPtr aScreen = std::dynamic_pointer_cast<GeomAPI_IScreenParams>(theObject);
156 aScreen->setScreenPlane(getScreenPlane());
157 aScreen->setViewScale(getViewScale());
159 anAIS = aPrs->getAISObject(anAIS);
161 anAIS = myWorkshop->module()->createPresentation(theObject);
165 aDisplayed = display(theObject, anAIS, isShading, theUpdateViewer);
171 //**************************************************************
172 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS, ModuleBase_IModule* theModule)
174 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
175 if (!aShapePrs.IsNull()) {
176 TopoDS_Shape aShape = aShapePrs->Shape();
179 TopAbs_ShapeEnum aType = aShape.ShapeType();
180 if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
183 // Check that the presentation is not a sketch
184 return theModule->canBeShaded(theAIS);
190 //**************************************************************
191 bool XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
192 bool isShading, bool theUpdateViewer)
194 bool aDisplayed = false;
196 Handle(AIS_InteractiveContext) aContext = AISContext();
197 if (aContext.IsNull())
200 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
201 if (!anAISIO.IsNull()) {
202 appendResultObject(theObject, theAIS);
204 //bool isCustomized = customizeObject(theObject);
206 int aDispMode = isShading? Shading : Wireframe;
208 anAISIO->Attributes()->SetFaceBoundaryDraw( Standard_True );
209 anAISIO->SetDisplayMode(aDispMode);
210 aContext->Display(anAISIO, aDispMode, 0, false, true, AIS_DS_Displayed);
212 if (getCallBack()) getCallBack()->Display(anAISIO);
216 emit objectDisplayed(theObject, theAIS);
217 selectionActivate()->activate(anAISIO, theUpdateViewer);
225 //**************************************************************
226 bool XGUI_Displayer::erase(ObjectPtr theObject, const bool theUpdateViewer)
228 bool aErased = false;
229 if (!isVisible(theObject))
232 Handle(AIS_InteractiveContext) aContext = AISContext();
233 if (aContext.IsNull())
236 AISObjectPtr anObject = myResult2AISObjectMap.value(theObject);
238 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
239 if (!anAIS.IsNull()) {
240 emit beforeObjectErase(theObject, anObject);
241 aContext->Remove(anAIS, false/*update viewer*/);
243 if (getCallBack()) getCallBack()->Remove(anAIS);
248 myResult2AISObjectMap.remove(theObject);
251 std::ostringstream aPtrStr;
252 aPtrStr << theObject.get();
253 qDebug(QString("erase object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
254 qDebug(getResult2AISObjectMapInfo().c_str());
263 //**************************************************************
264 bool XGUI_Displayer::redisplay(ObjectPtr theObject, bool theUpdateViewer)
266 bool aRedisplayed = false;
267 Handle(AIS_InteractiveContext) aContext = AISContext();
268 if (aContext.IsNull())
271 if (!isVisible(theObject))
274 AISObjectPtr aAISObj = getAISObject(theObject);
275 Handle(AIS_InteractiveObject) aAISIO;
277 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
279 GeomScreenParamsPtr aScreen = std::dynamic_pointer_cast<GeomAPI_IScreenParams>(theObject);
281 aScreen->setScreenPlane(getScreenPlane());
282 aScreen->setViewScale(getViewScale());
284 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
286 aRedisplayed = erase(theObject, theUpdateViewer);
289 if (aAIS_Obj != aAISObj) {
290 erase(theObject, theUpdateViewer);
291 appendResultObject(theObject, aAIS_Obj);
293 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
296 aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
299 if (!aAISIO.IsNull()) {
300 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
303 std::vector<int> aColor;
304 ModelAPI_Tools::getColor(aResult, aColor);
305 if (aColor.size() > 0) {
307 aCol(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB);
308 aAISIO->SetColor(aCol);
311 double aDeflection = ModelAPI_Tools::getDeflection(aResult);
312 if ((aDeflection >= 0) && (aDeflection != aAISObj->getDeflection()))
313 aAISObj->setDeflection(aDeflection);
316 double aTransparency = ModelAPI_Tools::getTransparency(aResult);
317 if ((aTransparency >= 0) && (aTransparency != aAISObj->getTransparency()))
318 aAISObj->setTransparency(aTransparency);
320 myWorkshop->module()->storeSelection();
322 #ifdef CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
323 myWorkshop->selector()->deselectPresentation(aAISIO);
326 if (aContext->IsDisplayed(aAISIO))
327 aContext->Redisplay(aAISIO, false);
329 aContext->Display(aAISIO, false);
332 if (getCallBack()) getCallBack()->Redisplay(aAISIO);
335 //if (aNeedToRestoreSelection)
336 myWorkshop->module()->restoreSelection();
339 #ifdef DEBUG_FEATURE_REDISPLAY
340 qDebug(" Redisplay happens");
348 //**************************************************************
349 void XGUI_Displayer::redisplayObjects()
351 // redisplay objects visualized in the viewer
352 static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
353 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
354 QObjectPtrList aDisplayed = displayedObjects();
355 QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
356 for (; anIt != aLast; anIt++) {
357 aECreator->sendUpdated(*anIt, EVENT_DISP);
359 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
360 if (aViewer->isColorScaleVisible()) {
361 aViewer->setupColorScale();
362 aViewer->setColorScaleShown(true);
364 Events_Loop::loop()->flush(EVENT_DISP);
367 //**************************************************************
368 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
369 const bool theUpdateViewer)
371 //Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
372 //if (!aTrihedron.IsNull())
373 // deactivateAIS(aTrihedron);
375 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
376 for (; anIt != aLast; anIt++) {
377 selectionActivate()->deactivate(*anIt, false);
379 //VSV It seems that there is no necessity to update viewer on deactivation
380 //if (theUpdateViewer)
384 //**************************************************************
385 bool XGUI_Displayer::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
387 bool aVisible = false;
388 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
389 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
390 if (aPrs.get() || aResult.get()) {
391 aVisible = theDisplayer->isVisible(theObject);
392 // compsolid is not visualized in the viewer,
393 // but should have presentation when all sub solids are
394 // visible. It is useful for highlight presentation where compsolid shape is selectable
395 if (!aVisible && aResult.get() && aResult->groupName() == ModelAPI_ResultBody::group()) {
396 ResultBodyPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
397 if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
398 int aNumberOfSubs = aCompsolidResult->numberOfSubs();
399 bool anAllSubsVisible = aNumberOfSubs > 0;
400 for(int i = 0; i < aNumberOfSubs && anAllSubsVisible; i++) {
401 anAllSubsVisible = theDisplayer->isVisible(aCompsolidResult->subResult(i));
403 aVisible = anAllSubsVisible;
407 // it is possible that feature is presentable and has results, so we should check visibility
408 // of results if presentation is not shown (e.g. Sketch Circle/Arc features)
410 // check if all results of the feature are visible
411 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
412 std::list<ResultPtr> aResults;
413 ModelAPI_Tools::allResults(aFeature, aResults);
414 std::list<ResultPtr>::const_iterator aIt;
415 aVisible = !aResults.empty();
416 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
417 aVisible = aVisible && theDisplayer->isVisible(*aIt);
423 //**************************************************************
424 void XGUI_Displayer::setSelected(const QList<ModuleBase_ViewerPrsPtr>& theValues,
425 bool theUpdateViewer)
427 Handle(AIS_InteractiveContext) aContext = AISContext();
428 if (aContext.IsNull())
430 aContext->UnhilightSelected(false);
431 aContext->ClearSelected(false);
433 if (getCallBack()) getCallBack()->ClearSelected();
435 NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)>>
438 foreach (ModuleBase_ViewerPrsPtr aPrs, theValues) {
439 const GeomShapePtr& aGeomShape = aPrs->shape();
440 if (aGeomShape.get() && !aGeomShape->isNull()) {
441 const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
442 #ifdef DEBUG_OCCT_SHAPE_SELECTION
443 // problem 1: performance
444 // problem 2: IO is not specified, so the first found owner is selected, as a result
445 // it might belong to another result
446 aContext->AddOrRemoveSelected(aShape, false);
448 if (getCallBack()) getCallBack()->AddOrRemoveSelected(aShape);
451 NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations;
452 if (aShapesToBeSelected.IsBound(aShape))
453 aPresentations = aShapesToBeSelected.Find(aShape);
454 ObjectPtr anObject = aPrs->object();
455 getPresentations(anObject, aPresentations);
457 aShapesToBeSelected.Bind(aShape, aPresentations);
460 ObjectPtr anObject = aPrs->object();
461 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
462 if (aResult.get() && isVisible(aResult)) {
463 AISObjectPtr anObj = myResult2AISObjectMap.value(aResult);
464 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
465 if (!anAIS.IsNull()) {
466 // The methods are replaced in order to provide multi-selection, e.g. restore selection
467 // by activating multi selector widget. It also gives an advantage that the multi
468 // selection in OB gives multi-selection in the viewer
469 //aContext->SetSelected(anAIS, false);
470 // The selection in the context was cleared, so the method sets the objects are selected
471 aContext->AddOrRemoveSelected(anAIS, false);
473 if (getCallBack()) getCallBack()->AddOrRemoveSelected(anAIS);
479 if (!aShapesToBeSelected.IsEmpty())
480 XGUI_Displayer::AddOrRemoveSelectedShapes(aContext, aShapesToBeSelected);
486 //**************************************************************
487 void XGUI_Displayer::clearSelected(const bool theUpdateViewer)
489 Handle(AIS_InteractiveContext) aContext = AISContext();
490 if (!aContext.IsNull()) {
491 aContext->UnhilightSelected(false);//UnhilightCurrents(false);
492 aContext->ClearSelected(theUpdateViewer);
494 if (getCallBack()) getCallBack()->ClearSelected();
499 //**************************************************************
500 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
502 bool aErased = false;
503 Handle(AIS_InteractiveContext) aContext = AISContext();
504 if (!aContext.IsNull()) {
506 foreach(ObjectPtr aObj, myResult2AISObjectMap.objects()) {
507 AISObjectPtr aAISObj = myResult2AISObjectMap.value(aObj);
509 foreach(ObjectPtr aObj, myResult2AISObjectMap.keys()) {
510 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
513 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
514 if (!anIO.IsNull()) {
515 emit beforeObjectErase(aObj, aAISObj);
516 aContext->Remove(anIO, false/*update viewer*/);
518 if (getCallBack()) getCallBack()->Remove(anIO);
526 myResult2AISObjectMap.clear();
529 qDebug(getResult2AISObjectMapInfo().c_str());
535 //**************************************************************
536 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
539 return myResult2AISObjectMap.value(theObject);
542 if (myResult2AISObjectMap.contains(theObject))
543 anIO = myResult2AISObjectMap[theObject];
548 //**************************************************************
549 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
551 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
552 return getObject(aRefAIS);
555 //**************************************************************
556 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
559 ObjectPtr anObject = myResult2AISObjectMap.value(theIO);
562 ResultToAISMap::const_iterator aMapIter = myResult2AISObjectMap.cbegin();
563 for (; aMapIter != myResult2AISObjectMap.cend(); aMapIter++) {
564 const AISObjectPtr& aAIS = aMapIter.value();
565 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
567 anObject = aMapIter.key();
572 if (!anObject.get()) {
573 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
574 if (!theIO.IsNull()) {
575 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
577 anObject = myWorkshop->module()->findPresentedObject(anAISObj);
582 //**************************************************************
583 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
585 bool aWasEnabled = isUpdateEnabled();
587 myViewerBlockedRecursiveCount--;
589 myViewerBlockedRecursiveCount++;
591 #ifdef DEBUG_VIEWER_BLOCKED_COUNT
592 std::cout << "myViewerBlockedRecursiveCount = " << myViewerBlockedRecursiveCount << std::endl;
595 if (myNeedUpdate && isUpdateEnabled()) {
597 myNeedUpdate = false;
602 //**************************************************************
603 void XGUI_Displayer::updateViewer() const
605 Handle(AIS_InteractiveContext) aContext = AISContext();
607 #ifdef DEBUG_VIEWER_BLOCKED_COUNT
608 std::cout << "updateViewer: " << (myViewerBlockedRecursiveCount == 0 ? " done" : " later")
612 if (!aContext.IsNull() && isUpdateEnabled()) {
613 //myWorkshop->viewer()->Zfitall();
614 aContext->UpdateCurrentViewer();
620 //**************************************************************
621 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
623 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
624 if (!aContext.IsNull() && (myContextId != aContext.get())) {
625 myContextId = aContext.get();
626 if (!myWorkshop->selectionActivate()->isTrihedronActive())
627 selectionActivate()->deactivateTrihedron(true);
628 // Do not modify default drawer. The same is done in ModuleBase_ResultPrs
629 //aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
630 //aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
632 //Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
633 //aTrihedron->getHighlightPointAspect()->SetScale(2.0);
634 //aTrihedron->getHighlightPointAspect()->SetTypeOfMarker(Aspect_TOM_O_STAR);
636 // Commented out according to discussion in bug #2825
637 ModuleBase_IViewer::DefaultHighlightDrawer = aContext->HighlightStyle();
638 //Handle(Prs3d_Drawer) aSelStyle = aContext->SelectionStyle();
639 //double aDeflection =
640 // QString(ModelAPI_ResultConstruction::DEFAULT_DEFLECTION().c_str()).toDouble();
642 // aDeflection = Config_PropManager::real("Visualization", "construction_deflection");
645 //ModuleBase_IViewer::DefaultHighlightDrawer->SetDeviationCoefficient(aDeflection);
646 //aSelStyle->SetDeviationCoefficient(aDeflection);
651 //**************************************************************
652 void XGUI_Displayer::setSelectionColor(const std::vector<int>& theColor)
654 Handle(AIS_InteractiveContext) aContext = AISContext();
655 if (!aContext.IsNull()) {
656 Quantity_Color aQColor(theColor[0] / 255.,
658 theColor[2] / 255., Quantity_TOC_RGB);
659 aContext->SelectionStyle()->SetColor(aQColor);
660 aContext->SelectionStyle()->PointAspect()->SetColor(aQColor);
661 aContext->SelectionStyle()->LineAspect()->SetColor(aQColor);
662 aContext->HighlightStyle(Prs3d_TypeOfHighlight_LocalSelected)->SetColor(aQColor);
667 //**************************************************************
668 std::vector<int> XGUI_Displayer::selectionColor() const
670 std::vector<int> aColor;
671 Handle(AIS_InteractiveContext) aContext = AISContext();
672 if (!aContext.IsNull()) {
673 Quantity_Color aQColor = aContext->SelectionStyle()->Color();
674 aColor.push_back((int)(aQColor.Red() * 255));
675 aColor.push_back((int)(aQColor.Green() * 255));
676 aColor.push_back((int)(aQColor.Blue() * 255));
682 //**************************************************************
683 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
685 Handle(AIS_InteractiveContext) aContext = AISContext();
686 if (!aContext.IsNull() && myAndFilter.IsNull()) {
687 myAndFilter = new SelectMgr_AndFilter();
688 aContext->AddFilter(myAndFilter);
693 //**************************************************************
694 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
695 const Standard_Integer theDisplayMode, bool theUpdateViewer)
697 bool aDisplayed = false;
698 Handle(AIS_InteractiveContext) aContext = AISContext();
699 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
700 if (!aContext.IsNull() && !anAISIO.IsNull()) {
701 aContext->Display(anAISIO, theDisplayMode, 0, false/*update viewer*/, true, AIS_DS_Displayed);
703 if (getCallBack()) getCallBack()->Display(anAISIO);
706 aContext->Deactivate(anAISIO);
708 if (getCallBack()) getCallBack()->Deactivate(anAISIO);
710 aContext->Load(anAISIO);
712 if (getCallBack()) getCallBack()->Load(anAISIO);
714 if (toActivateInSelectionModes)
715 myWorkshop->selectionActivate()->activateOnDisplay(anAISIO, theUpdateViewer);
723 //**************************************************************
724 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
726 bool aErased = false;
727 Handle(AIS_InteractiveContext) aContext = AISContext();
728 if (!aContext.IsNull()) {
729 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
730 if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
731 aContext->Remove(anAISIO, false/*update viewer*/);
733 if (getCallBack()) getCallBack()->Remove(anAISIO);
738 if (aErased && theUpdateViewer)
743 //**************************************************************
744 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
746 if (theMode == NoMode)
749 Handle(AIS_InteractiveContext) aContext = AISContext();
750 if (aContext.IsNull())
753 AISObjectPtr aAISObj = getAISObject(theObject);
757 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
758 aContext->SetDisplayMode(aAISIO, theMode, false);
759 // Redisplay in order to update new mode because it could be not computed before
764 //**************************************************************
765 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
767 Handle(AIS_InteractiveContext) aContext = AISContext();
768 if (aContext.IsNull())
771 AISObjectPtr aAISObj = getAISObject(theObject);
775 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
776 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
779 //**************************************************************
780 void XGUI_Displayer::deactivateSelectionFilters(const bool theAddFilterOnly)
782 Handle(AIS_InteractiveContext) aContext = AISContext();
783 if (!myAndFilter.IsNull()) {
785 if (!aContext.IsNull()) {
786 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
787 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
788 for (; anIt.More() && !aFound; anIt.Next()) {
789 Handle(SelectMgr_Filter) aFilter = anIt.Value();
790 aFound = aFilter == myAndFilter;
793 aContext->RemoveFilter(myAndFilter);
795 myAndFilter.Nullify();
799 //**************************************************************
800 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
802 Handle(AIS_InteractiveContext) aContext = AISContext();
803 if (aContext.IsNull() || hasSelectionFilter(theFilter))
806 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
807 if (!aCompositeFilter.IsNull()) {
808 aCompositeFilter->Add(theFilter);
809 #ifdef DEBUG_SELECTION_FILTERS
810 int aCount = aCompositeFilter->StoredFilters().Extent();
811 qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
816 //**************************************************************
817 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
819 Handle(AIS_InteractiveContext) aContext = AISContext();
820 if (aContext.IsNull())
823 Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
824 if (!aCompositeFilter.IsNull() && aCompositeFilter->IsIn(theFilter)) {
825 aCompositeFilter->Remove(theFilter);
826 #ifdef DEBUG_SELECTION_FILTERS
827 int aCount = aCompositeFilter->StoredFilters().Extent();
828 qDebug(QString("removeSelectionFilter: filters.count() = %1")
829 .arg(aCount).toStdString().c_str());
834 //**************************************************************
835 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
837 bool aFilterFound = false;
839 Handle(AIS_InteractiveContext) aContext = AISContext();
840 if (aContext.IsNull())
842 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
843 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
844 for (; aIt.More() && !aFilterFound; aIt.Next()) {
845 if (theFilter.get() == aIt.Value().get())
848 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
849 if (!aCompositeFilter.IsNull()) {
850 const SelectMgr_ListOfFilter& aStoredFilters = aCompositeFilter->StoredFilters();
851 for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
852 if (theFilter.get() == aIt.Value().get())
859 //**************************************************************
860 void XGUI_Displayer::removeFilters()
862 Handle(AIS_InteractiveContext) aContext = AISContext();
863 if (aContext.IsNull())
866 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
867 if (!aCompositeFilter.IsNull())
868 aCompositeFilter->Clear();
871 //**************************************************************
872 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
875 QObjectPtrList aDispList = myResult2AISObjectMap.objects();
877 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
879 foreach(ObjectPtr aObj, aDispList) {
880 if (!theList.contains(aObj))
883 foreach(ObjectPtr aObj, theList) {
884 if (!isVisible(aObj))
885 display(aObj, false);
890 //**************************************************************
891 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
893 if (!isVisible(theObject))
896 AISObjectPtr aAISObj = getAISObject(theObject);
897 if (aAISObj.get() == NULL)
900 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
901 return ::canBeShaded(anAIS, myWorkshop->module());
904 //**************************************************************
905 //bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
907 // AISObjectPtr anAISObj = getAISObject(theObject);
908 // // correct the result's color it it has the attribute
909 // ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
911 // // Customization of presentation
912 // GeomCustomPrsPtr aCustomPrs;
913 // FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
914 // if (aFeature.get() != NULL) {
915 // GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
916 // if (aCustPrs.get() != NULL)
917 // aCustomPrs = aCustPrs;
919 // if (aCustomPrs.get() == NULL) {
920 // GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
921 // // we ignore presentable not customized objects
922 // if (aPrs.get() == NULL)
923 // aCustomPrs = myCustomPrs;
925 // bool isCustomized = aCustomPrs.get() &&
926 // aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
927 // isCustomized = myWorkshop->module()->afterCustomisePresentation(aResult, anAISObj, myCustomPrs)
930 // // update presentation state if faces panel is active
931 // if (anAISObj.get() && myWorkshop->facesPanel())
932 // isCustomized = myWorkshop->facesPanel()->customizeObject(theObject, anAISObj) || isCustomized;
934 // return isCustomized;
937 //**************************************************************
938 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject,
939 const QColor& theColor,
940 bool theUpdateViewer)
942 if (!isVisible(theObject))
945 AISObjectPtr anAISObj = getAISObject(theObject);
947 anAISObj->getColor(aR, aG, aB);
948 anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
951 return QColor(aR, aG, aB);
954 //**************************************************************
955 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
958 myResult2AISObjectMap.add(theObject, theAIS);
960 myResult2AISObjectMap[theObject] = theAIS;
964 std::ostringstream aPtrStr;
965 aPtrStr << theObject.get();
966 qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
967 qDebug(getResult2AISObjectMapInfo().c_str());
972 //**************************************************************
973 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
975 QStringList aContent;
977 foreach(ObjectPtr aObj, myResult2AISObjectMap.objects()) {
978 AISObjectPtr aAISObj = myResult2AISObjectMap.value(aObj);
980 foreach(ObjectPtr aObj, myResult2AISObjectMap.keys()) {
981 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
983 std::ostringstream aPtrStr;
984 aPtrStr << "aObj = " << aObj.get() << ":";
985 aPtrStr << "anAIS = " << aAISObj.get() << ":";
986 aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
988 aContent.append(aPtrStr.str().c_str());
990 return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
991 arg(aContent.join("\n")).toStdString().c_str();
995 //**************************************************************
996 void XGUI_Displayer::getPresentations(const ObjectPtr& theObject,
997 NCollection_Map<Handle(AIS_InteractiveObject)>& thePresentations)
999 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1000 if (aResult.get()) {
1001 AISObjectPtr aAISObj = getAISObject(aResult);
1002 if (aAISObj.get() == NULL) {
1003 // if result is a result of a composite feature, it is visualized by visualization of
1004 // composite children, so we should get one of this presentations
1005 ResultBodyPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
1006 if (aCompSolid.get() && aCompSolid->numberOfSubs() > 0) {
1007 aAISObj = getAISObject(aCompSolid->subResult(0));
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);
1017 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
1018 // find presentation of the feature
1019 AISObjectPtr aAISObj = getAISObject(aFeature);
1020 if (aAISObj.get() != NULL) {
1021 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1022 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1023 thePresentations.Add(anAIS);
1025 // find presentations of the feature results
1026 std::list<ResultPtr> aResults;
1027 ModelAPI_Tools::allResults(aFeature, aResults);
1028 std::list<ResultPtr>::const_iterator anIt = aResults.begin(), aLast = aResults.end();
1029 for (; anIt != aLast; ++anIt) {
1030 AISObjectPtr aAISObj = getAISObject(*anIt);
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);
1040 //**************************************************************
1041 void XGUI_Displayer::displayTrihedron(bool theToDisplay) const
1043 Handle(AIS_InteractiveContext) aContext = AISContext();
1044 if (aContext.IsNull())
1047 Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
1049 XGUI_SelectionActivate* aSelectionActive = selectionActivate();
1051 if (!aContext->IsDisplayed(aTrihedron))
1052 aContext->Display(aTrihedron,
1054 -1 /* selection mode */,
1055 Standard_True /* update viewer*/,
1056 Standard_False /* allow decomposition */,
1057 AIS_DS_Displayed /* xdisplay status */);
1059 if (getCallBack()) getCallBack()->Display(aTrihedron);
1062 if (!aSelectionActive->isTrihedronActive())
1063 aSelectionActive->deactivateTrihedron(false);
1065 aSelectionActive->activate(aTrihedron, false);
1067 aSelectionActive->deactivateTrihedron(false);
1069 aContext->Erase(aTrihedron, Standard_True);
1071 if (getCallBack()) getCallBack()->Remove(aTrihedron);
1076 //**************************************************************
1077 void XGUI_Displayer::AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
1078 const NCollection_DataMap<TopoDS_Shape,
1079 NCollection_Map<Handle(AIS_InteractiveObject)>>& theShapesToBeSelected)
1081 NCollection_Map<Handle(AIS_InteractiveObject)> aCompsolidPresentations;
1082 NCollection_Map<Handle(AIS_InteractiveObject)> aSelectedPresentations;
1084 NCollection_List<Handle(SelectBasics_EntityOwner)> anActiveOwners;
1085 theContext->MainSelector()->ActiveOwners(anActiveOwners);
1086 NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (anActiveOwners);
1087 Handle(SelectMgr_EntityOwner) anOwner;
1089 /// It is very important to check that the owner is processed only once and has a map of
1090 /// processed owners because SetSelected works as a switch.
1091 /// If count of calls setSelectec is even, the object stays in the previous state
1092 /// (selected, deselected)
1093 /// OCCT: to write about the problem that active owners method returns one owner several times
1094 QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
1095 for (; anOwnersIt.More(); anOwnersIt.Next()) {
1096 anOwner = Handle(SelectMgr_EntityOwner)::DownCast (anOwnersIt.Value());
1097 if (aSelectedIds.contains((size_t)anOwner.get()))
1099 aSelectedIds.append((size_t)anOwner.get());
1101 Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
1102 if (!BROwnr.IsNull() && BROwnr->HasShape()) {
1103 const TopoDS_Shape& aShape = BROwnr->Shape();
1104 if (aShape.IsNull())
1107 Handle(ModuleBase_BRepOwner) aCustomOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
1109 NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)> >
1110 ::Iterator aShapeIt(theShapesToBeSelected);
1111 for (; aShapeIt.More(); aShapeIt.Next()) {
1112 const TopoDS_Shape& aParameterShape = aShapeIt.Key();
1113 // In case of compound we cannot rely on simple comparison method.
1114 // If the compound is generated by Group feature then this compound is alwais new.
1115 // So, we have to compare content of these compounds
1117 // isSame should be used here as it does not check orientation of shapes
1118 // despite on isEqual of shapes or IsBound for shape in QMap. Orientation is
1119 // different for Edges shapes in model shape and owner even if this is the same shape
1120 if (ModuleBase_Tools::isSameShape(aParameterShape, aShape)) {
1121 Handle(AIS_InteractiveObject) anOwnerPresentation =
1122 Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
1123 NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations =
1124 theShapesToBeSelected.Find(aParameterShape);
1125 if (aPresentations.Contains(anOwnerPresentation)) {
1126 theContext->AddOrRemoveSelected(anOwner, Standard_False);
1127 anOwner->SetSelected(Standard_True);
1128 // collect selected presentations to do not select them if compsolid is selected
1129 if (!aSelectedPresentations.Contains(anOwnerPresentation))
1130 aSelectedPresentations.Add(anOwnerPresentation);
1133 else if (!aCustomOwner.IsNull()) { // CompSolid processing #2219
1134 // shape of owner is compound, but shape to be selected is compsolid, so
1135 // we need to compare shape to AIS presentation of owner(rule of the owner creation)
1136 Handle(AIS_Shape) anOwnerPresentation =
1137 Handle(AIS_Shape)::DownCast(anOwner->Selectable());
1138 const TopoDS_Shape& aPresentationShape = anOwnerPresentation->Shape();
1139 if (aParameterShape.IsSame(anOwnerPresentation->Shape()) &&
1140 !aCompsolidPresentations.Contains(anOwnerPresentation))
1141 aCompsolidPresentations.Add(anOwnerPresentation);
1146 // select CompSolid presentations if their owners was not selected yet
1147 NCollection_Map<Handle(AIS_InteractiveObject)>::Iterator anIt (aCompsolidPresentations);
1148 for (; anIt.More(); anIt.Next()) {
1149 if (aSelectedPresentations.Contains(anIt.Value()))
1151 theContext->AddOrRemoveSelected(anIt.Value(), Standard_False);
1155 //**************************************************************
1156 XGUI_SelectionActivate* XGUI_Displayer::selectionActivate() const
1158 return myWorkshop->selectionActivate();
1161 //**************************************************************
1162 GeomPlanePtr XGUI_Displayer::getScreenPlane() const
1164 GeomPlanePtr aResult;
1165 Handle(AIS_InteractiveContext) aContext = AISContext();
1166 if (!aContext.IsNull()) {
1167 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1168 Handle(V3d_View) aView;
1169 for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews()) {
1170 aView = aViewer->ActiveView();
1173 if (!aView.IsNull()) {
1174 double aEyeX, aEyeY, aEyeZ;
1175 aView->Eye(aEyeX, aEyeY, aEyeZ);
1177 double aProjX, aProjY, aProjZ;
1178 aView->Proj(aProjX, aProjY, aProjZ);
1180 GeomPointPtr aPnt = GeomPointPtr(new GeomAPI_Pnt(aEyeX, aEyeY, aEyeZ));
1181 GeomDirPtr aDir = GeomDirPtr(new GeomAPI_Dir(aProjX, aProjY, aProjZ));
1183 aResult = GeomPlanePtr(new GeomAPI_Pln(aPnt, aDir));
1189 double XGUI_Displayer::getViewScale() const
1191 Handle(AIS_InteractiveContext) aContext = AISContext();
1192 if (!aContext.IsNull()) {
1193 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1194 Handle(V3d_View) aView;
1195 for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews()) {
1196 aView = aViewer->ActiveView();
1199 return aView->Camera()->Scale();