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;
207 anAISIO->SetDisplayMode(aDispMode);
208 aContext->Display(anAISIO, aDispMode, 0, false, true, AIS_DS_Displayed);
210 if (getCallBack()) getCallBack()->Display(anAISIO);
214 emit objectDisplayed(theObject, theAIS);
215 selectionActivate()->activate(anAISIO, theUpdateViewer);
223 //**************************************************************
224 bool XGUI_Displayer::erase(ObjectPtr theObject, const bool theUpdateViewer)
226 bool aErased = false;
227 if (!isVisible(theObject))
230 Handle(AIS_InteractiveContext) aContext = AISContext();
231 if (aContext.IsNull())
234 AISObjectPtr anObject = myResult2AISObjectMap.value(theObject);
236 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
237 if (!anAIS.IsNull()) {
238 emit beforeObjectErase(theObject, anObject);
239 aContext->Remove(anAIS, false/*update viewer*/);
241 if (getCallBack()) getCallBack()->Remove(anAIS);
246 myResult2AISObjectMap.remove(theObject);
249 std::ostringstream aPtrStr;
250 aPtrStr << theObject.get();
251 qDebug(QString("erase object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
252 qDebug(getResult2AISObjectMapInfo().c_str());
261 //**************************************************************
262 bool XGUI_Displayer::redisplay(ObjectPtr theObject, bool theUpdateViewer)
264 bool aRedisplayed = false;
265 Handle(AIS_InteractiveContext) aContext = AISContext();
266 if (aContext.IsNull())
269 if (!isVisible(theObject))
272 AISObjectPtr aAISObj = getAISObject(theObject);
273 Handle(AIS_InteractiveObject) aAISIO;
275 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
277 GeomScreenParamsPtr aScreen = std::dynamic_pointer_cast<GeomAPI_IScreenParams>(theObject);
279 aScreen->setScreenPlane(getScreenPlane());
280 aScreen->setViewScale(getViewScale());
282 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
284 aRedisplayed = erase(theObject, theUpdateViewer);
287 if (aAIS_Obj != aAISObj) {
288 erase(theObject, theUpdateViewer);
289 appendResultObject(theObject, aAIS_Obj);
291 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
294 aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
297 if (!aAISIO.IsNull()) {
298 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
301 std::vector<int> aColor;
302 ModelAPI_Tools::getColor(aResult, aColor);
303 if (aColor.size() > 0) {
305 aCol(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB);
306 aAISIO->SetColor(aCol);
309 double aDeflection = ModelAPI_Tools::getDeflection(aResult);
310 if ((aDeflection >= 0) && (aDeflection != aAISObj->getDeflection()))
311 aAISObj->setDeflection(aDeflection);
314 double aTransparency = ModelAPI_Tools::getTransparency(aResult);
315 if ((aTransparency >= 0) && (aTransparency != aAISObj->getTransparency()))
316 aAISObj->setTransparency(aTransparency);
319 Handle(ModuleBase_ResultPrs) aResPrs = Handle(ModuleBase_ResultPrs)::DownCast(aAISIO);
320 if (!aResPrs.IsNull())
321 aResPrs->updateIsoLines();
323 myWorkshop->module()->storeSelection();
325 #ifdef CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
326 myWorkshop->selector()->deselectPresentation(aAISIO);
329 if (aContext->IsDisplayed(aAISIO))
330 aContext->Redisplay(aAISIO, false);
332 aContext->Display(aAISIO, false);
335 if (getCallBack()) getCallBack()->Redisplay(aAISIO);
338 //if (aNeedToRestoreSelection)
339 myWorkshop->module()->restoreSelection();
342 #ifdef DEBUG_FEATURE_REDISPLAY
343 qDebug(" Redisplay happens");
351 //**************************************************************
352 void XGUI_Displayer::redisplayObjects()
354 // redisplay objects visualized in the viewer
355 static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
356 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
357 QObjectPtrList aDisplayed = displayedObjects();
358 QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
359 for (; anIt != aLast; anIt++) {
360 aECreator->sendUpdated(*anIt, EVENT_DISP);
362 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
363 if (aViewer->isColorScaleVisible()) {
364 aViewer->setupColorScale();
365 aViewer->setColorScaleShown(true);
367 Events_Loop::loop()->flush(EVENT_DISP);
370 //**************************************************************
371 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
372 const bool theUpdateViewer)
374 //Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
375 //if (!aTrihedron.IsNull())
376 // deactivateAIS(aTrihedron);
378 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
379 for (; anIt != aLast; anIt++) {
380 selectionActivate()->deactivate(*anIt, false);
382 //VSV It seems that there is no necessity to update viewer on deactivation
383 //if (theUpdateViewer)
387 //**************************************************************
388 bool XGUI_Displayer::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
390 bool aVisible = false;
391 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
392 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
393 if (aPrs.get() || aResult.get()) {
394 aVisible = theDisplayer->isVisible(theObject);
395 // compsolid is not visualized in the viewer,
396 // but should have presentation when all sub solids are
397 // visible. It is useful for highlight presentation where compsolid shape is selectable
398 if (!aVisible && aResult.get() && aResult->groupName() == ModelAPI_ResultBody::group()) {
399 ResultBodyPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
400 if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
401 int aNumberOfSubs = aCompsolidResult->numberOfSubs();
402 bool anAllSubsVisible = aNumberOfSubs > 0;
403 for(int i = 0; i < aNumberOfSubs && anAllSubsVisible; i++) {
404 anAllSubsVisible = theDisplayer->isVisible(aCompsolidResult->subResult(i));
406 aVisible = anAllSubsVisible;
410 // it is possible that feature is presentable and has results, so we should check visibility
411 // of results if presentation is not shown (e.g. Sketch Circle/Arc features)
413 // check if all results of the feature are visible
414 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
415 std::list<ResultPtr> aResults;
416 ModelAPI_Tools::allResults(aFeature, aResults);
417 std::list<ResultPtr>::const_iterator aIt;
418 aVisible = !aResults.empty();
419 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
420 aVisible = aVisible && theDisplayer->isVisible(*aIt);
426 //**************************************************************
427 void XGUI_Displayer::setSelected(const QList<ModuleBase_ViewerPrsPtr>& theValues,
428 bool theUpdateViewer)
430 Handle(AIS_InteractiveContext) aContext = AISContext();
431 if (aContext.IsNull())
433 aContext->UnhilightSelected(false);
434 aContext->ClearSelected(false);
436 if (getCallBack()) getCallBack()->ClearSelected();
438 NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)>>
441 foreach (ModuleBase_ViewerPrsPtr aPrs, theValues) {
442 const GeomShapePtr& aGeomShape = aPrs->shape();
443 if (aGeomShape.get() && !aGeomShape->isNull()) {
444 const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
445 #ifdef DEBUG_OCCT_SHAPE_SELECTION
446 // problem 1: performance
447 // problem 2: IO is not specified, so the first found owner is selected, as a result
448 // it might belong to another result
449 aContext->AddOrRemoveSelected(aShape, false);
451 if (getCallBack()) getCallBack()->AddOrRemoveSelected(aShape);
454 NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations;
455 if (aShapesToBeSelected.IsBound(aShape))
456 aPresentations = aShapesToBeSelected.Find(aShape);
457 ObjectPtr anObject = aPrs->object();
458 getPresentations(anObject, aPresentations);
460 aShapesToBeSelected.Bind(aShape, aPresentations);
463 ObjectPtr anObject = aPrs->object();
464 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
465 if (aResult.get() && isVisible(aResult)) {
466 AISObjectPtr anObj = myResult2AISObjectMap.value(aResult);
467 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
468 if (!anAIS.IsNull()) {
469 // The methods are replaced in order to provide multi-selection, e.g. restore selection
470 // by activating multi selector widget. It also gives an advantage that the multi
471 // selection in OB gives multi-selection in the viewer
472 //aContext->SetSelected(anAIS, false);
473 // The selection in the context was cleared, so the method sets the objects are selected
474 aContext->AddOrRemoveSelected(anAIS, false);
476 if (getCallBack()) getCallBack()->AddOrRemoveSelected(anAIS);
482 if (!aShapesToBeSelected.IsEmpty())
483 XGUI_Displayer::AddOrRemoveSelectedShapes(aContext, aShapesToBeSelected);
489 //**************************************************************
490 void XGUI_Displayer::clearSelected(const bool theUpdateViewer)
492 Handle(AIS_InteractiveContext) aContext = AISContext();
493 if (!aContext.IsNull()) {
494 aContext->UnhilightSelected(false);//UnhilightCurrents(false);
495 aContext->ClearSelected(theUpdateViewer);
497 if (getCallBack()) getCallBack()->ClearSelected();
502 //**************************************************************
503 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
505 bool aErased = false;
506 Handle(AIS_InteractiveContext) aContext = AISContext();
507 if (!aContext.IsNull()) {
509 foreach(ObjectPtr aObj, myResult2AISObjectMap.objects()) {
510 AISObjectPtr aAISObj = myResult2AISObjectMap.value(aObj);
512 foreach(ObjectPtr aObj, myResult2AISObjectMap.keys()) {
513 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
516 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
517 if (!anIO.IsNull()) {
518 emit beforeObjectErase(aObj, aAISObj);
519 aContext->Remove(anIO, false/*update viewer*/);
521 if (getCallBack()) getCallBack()->Remove(anIO);
529 myResult2AISObjectMap.clear();
532 qDebug(getResult2AISObjectMapInfo().c_str());
538 //**************************************************************
539 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
542 return myResult2AISObjectMap.value(theObject);
545 if (myResult2AISObjectMap.contains(theObject))
546 anIO = myResult2AISObjectMap[theObject];
551 //**************************************************************
552 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
554 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
555 return getObject(aRefAIS);
558 //**************************************************************
559 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
562 ObjectPtr anObject = myResult2AISObjectMap.value(theIO);
565 ResultToAISMap::const_iterator aMapIter = myResult2AISObjectMap.cbegin();
566 for (; aMapIter != myResult2AISObjectMap.cend(); aMapIter++) {
567 const AISObjectPtr& aAIS = aMapIter.value();
568 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
570 anObject = aMapIter.key();
575 if (!anObject.get()) {
576 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
577 if (!theIO.IsNull()) {
578 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
580 anObject = myWorkshop->module()->findPresentedObject(anAISObj);
585 //**************************************************************
586 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
588 bool aWasEnabled = isUpdateEnabled();
590 myViewerBlockedRecursiveCount--;
592 myViewerBlockedRecursiveCount++;
594 #ifdef DEBUG_VIEWER_BLOCKED_COUNT
595 std::cout << "myViewerBlockedRecursiveCount = " << myViewerBlockedRecursiveCount << std::endl;
598 if (myNeedUpdate && isUpdateEnabled()) {
600 myNeedUpdate = false;
605 //**************************************************************
606 void XGUI_Displayer::updateViewer() const
608 Handle(AIS_InteractiveContext) aContext = AISContext();
610 #ifdef DEBUG_VIEWER_BLOCKED_COUNT
611 std::cout << "updateViewer: " << (myViewerBlockedRecursiveCount == 0 ? " done" : " later")
615 if (!aContext.IsNull() && isUpdateEnabled()) {
616 //myWorkshop->viewer()->Zfitall();
617 aContext->UpdateCurrentViewer();
623 //**************************************************************
624 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
626 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
627 if (!aContext.IsNull() && (myContextId != aContext.get())) {
628 myContextId = aContext.get();
629 if (!myWorkshop->selectionActivate()->isTrihedronActive())
630 selectionActivate()->deactivateTrihedron(true);
631 // Do not modify default drawer. The same is done in ModuleBase_ResultPrs
632 //aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
633 //aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
635 //Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
636 //aTrihedron->getHighlightPointAspect()->SetScale(2.0);
637 //aTrihedron->getHighlightPointAspect()->SetTypeOfMarker(Aspect_TOM_O_STAR);
639 // Commented out according to discussion in bug #2825
640 ModuleBase_IViewer::DefaultHighlightDrawer = aContext->HighlightStyle();
641 //Handle(Prs3d_Drawer) aSelStyle = aContext->SelectionStyle();
642 //double aDeflection =
643 // QString(ModelAPI_ResultConstruction::DEFAULT_DEFLECTION().c_str()).toDouble();
645 // aDeflection = Config_PropManager::real("Visualization", "construction_deflection");
648 //ModuleBase_IViewer::DefaultHighlightDrawer->SetDeviationCoefficient(aDeflection);
649 //aSelStyle->SetDeviationCoefficient(aDeflection);
654 //**************************************************************
655 void XGUI_Displayer::setSelectionColor(const std::vector<int>& theColor)
657 Handle(AIS_InteractiveContext) aContext = AISContext();
658 if (!aContext.IsNull()) {
659 Quantity_Color aQColor(theColor[0] / 255.,
661 theColor[2] / 255., Quantity_TOC_RGB);
662 aContext->SelectionStyle()->SetColor(aQColor);
663 aContext->SelectionStyle()->PointAspect()->SetColor(aQColor);
664 aContext->SelectionStyle()->LineAspect()->SetColor(aQColor);
665 aContext->HighlightStyle(Prs3d_TypeOfHighlight_LocalSelected)->SetColor(aQColor);
670 //**************************************************************
671 std::vector<int> XGUI_Displayer::selectionColor() const
673 std::vector<int> aColor;
674 Handle(AIS_InteractiveContext) aContext = AISContext();
675 if (!aContext.IsNull()) {
676 Quantity_Color aQColor = aContext->SelectionStyle()->Color();
677 aColor.push_back((int)(aQColor.Red() * 255));
678 aColor.push_back((int)(aQColor.Green() * 255));
679 aColor.push_back((int)(aQColor.Blue() * 255));
685 //**************************************************************
686 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
688 Handle(AIS_InteractiveContext) aContext = AISContext();
689 if (!aContext.IsNull() && myAndFilter.IsNull()) {
690 myAndFilter = new SelectMgr_AndFilter();
691 aContext->AddFilter(myAndFilter);
696 //**************************************************************
697 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
698 const Standard_Integer theDisplayMode, bool theUpdateViewer)
700 bool aDisplayed = false;
701 Handle(AIS_InteractiveContext) aContext = AISContext();
702 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
703 if (!aContext.IsNull() && !anAISIO.IsNull()) {
704 aContext->Display(anAISIO, theDisplayMode, 0, false/*update viewer*/, true, AIS_DS_Displayed);
706 if (getCallBack()) getCallBack()->Display(anAISIO);
709 aContext->Deactivate(anAISIO);
711 if (getCallBack()) getCallBack()->Deactivate(anAISIO);
713 aContext->Load(anAISIO);
715 if (getCallBack()) getCallBack()->Load(anAISIO);
717 if (toActivateInSelectionModes)
718 myWorkshop->selectionActivate()->activateOnDisplay(anAISIO, theUpdateViewer);
726 //**************************************************************
727 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
729 bool aErased = false;
730 Handle(AIS_InteractiveContext) aContext = AISContext();
731 if (!aContext.IsNull()) {
732 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
733 if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
734 aContext->Remove(anAISIO, false/*update viewer*/);
736 if (getCallBack()) getCallBack()->Remove(anAISIO);
741 if (aErased && theUpdateViewer)
746 //**************************************************************
747 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
749 if (theMode == NoMode)
752 Handle(AIS_InteractiveContext) aContext = AISContext();
753 if (aContext.IsNull())
756 AISObjectPtr aAISObj = getAISObject(theObject);
760 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
761 aContext->SetDisplayMode(aAISIO, theMode, false);
762 // Redisplay in order to update new mode because it could be not computed before
767 //**************************************************************
768 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
770 Handle(AIS_InteractiveContext) aContext = AISContext();
771 if (aContext.IsNull())
774 AISObjectPtr aAISObj = getAISObject(theObject);
778 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
779 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
782 //**************************************************************
783 void XGUI_Displayer::deactivateSelectionFilters(const bool theAddFilterOnly)
785 Handle(AIS_InteractiveContext) aContext = AISContext();
786 if (!myAndFilter.IsNull()) {
788 if (!aContext.IsNull()) {
789 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
790 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
791 for (; anIt.More() && !aFound; anIt.Next()) {
792 Handle(SelectMgr_Filter) aFilter = anIt.Value();
793 aFound = aFilter == myAndFilter;
796 aContext->RemoveFilter(myAndFilter);
798 myAndFilter.Nullify();
802 //**************************************************************
803 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
805 Handle(AIS_InteractiveContext) aContext = AISContext();
806 if (aContext.IsNull() || hasSelectionFilter(theFilter))
809 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
810 if (!aCompositeFilter.IsNull()) {
811 aCompositeFilter->Add(theFilter);
812 #ifdef DEBUG_SELECTION_FILTERS
813 int aCount = aCompositeFilter->StoredFilters().Extent();
814 qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
819 //**************************************************************
820 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
822 Handle(AIS_InteractiveContext) aContext = AISContext();
823 if (aContext.IsNull())
826 Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
827 if (!aCompositeFilter.IsNull() && aCompositeFilter->IsIn(theFilter)) {
828 aCompositeFilter->Remove(theFilter);
829 #ifdef DEBUG_SELECTION_FILTERS
830 int aCount = aCompositeFilter->StoredFilters().Extent();
831 qDebug(QString("removeSelectionFilter: filters.count() = %1")
832 .arg(aCount).toStdString().c_str());
837 //**************************************************************
838 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
840 bool aFilterFound = false;
842 Handle(AIS_InteractiveContext) aContext = AISContext();
843 if (aContext.IsNull())
845 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
846 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
847 for (; aIt.More() && !aFilterFound; aIt.Next()) {
848 if (theFilter.get() == aIt.Value().get())
851 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
852 if (!aCompositeFilter.IsNull()) {
853 const SelectMgr_ListOfFilter& aStoredFilters = aCompositeFilter->StoredFilters();
854 for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
855 if (theFilter.get() == aIt.Value().get())
862 //**************************************************************
863 void XGUI_Displayer::removeFilters()
865 Handle(AIS_InteractiveContext) aContext = AISContext();
866 if (aContext.IsNull())
869 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
870 if (!aCompositeFilter.IsNull())
871 aCompositeFilter->Clear();
874 //**************************************************************
875 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
878 QObjectPtrList aDispList = myResult2AISObjectMap.objects();
880 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
882 foreach(ObjectPtr aObj, aDispList) {
883 if (!theList.contains(aObj))
886 foreach(ObjectPtr aObj, theList) {
887 if (!isVisible(aObj))
888 display(aObj, false);
893 //**************************************************************
894 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
896 if (!isVisible(theObject))
899 AISObjectPtr aAISObj = getAISObject(theObject);
900 if (aAISObj.get() == NULL)
903 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
904 return ::canBeShaded(anAIS, myWorkshop->module());
907 //**************************************************************
908 //bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
910 // AISObjectPtr anAISObj = getAISObject(theObject);
911 // // correct the result's color it it has the attribute
912 // ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
914 // // Customization of presentation
915 // GeomCustomPrsPtr aCustomPrs;
916 // FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
917 // if (aFeature.get() != NULL) {
918 // GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
919 // if (aCustPrs.get() != NULL)
920 // aCustomPrs = aCustPrs;
922 // if (aCustomPrs.get() == NULL) {
923 // GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
924 // // we ignore presentable not customized objects
925 // if (aPrs.get() == NULL)
926 // aCustomPrs = myCustomPrs;
928 // bool isCustomized = aCustomPrs.get() &&
929 // aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
930 // isCustomized = myWorkshop->module()->afterCustomisePresentation(aResult, anAISObj, myCustomPrs)
933 // // update presentation state if faces panel is active
934 // if (anAISObj.get() && myWorkshop->facesPanel())
935 // isCustomized = myWorkshop->facesPanel()->customizeObject(theObject, anAISObj) || isCustomized;
937 // return isCustomized;
940 //**************************************************************
941 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject,
942 const QColor& theColor,
943 bool theUpdateViewer)
945 if (!isVisible(theObject))
948 AISObjectPtr anAISObj = getAISObject(theObject);
950 anAISObj->getColor(aR, aG, aB);
951 anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
954 return QColor(aR, aG, aB);
957 //**************************************************************
958 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
961 myResult2AISObjectMap.add(theObject, theAIS);
963 myResult2AISObjectMap[theObject] = theAIS;
967 std::ostringstream aPtrStr;
968 aPtrStr << theObject.get();
969 qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
970 qDebug(getResult2AISObjectMapInfo().c_str());
975 //**************************************************************
976 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
978 QStringList aContent;
980 foreach(ObjectPtr aObj, myResult2AISObjectMap.objects()) {
981 AISObjectPtr aAISObj = myResult2AISObjectMap.value(aObj);
983 foreach(ObjectPtr aObj, myResult2AISObjectMap.keys()) {
984 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
986 std::ostringstream aPtrStr;
987 aPtrStr << "aObj = " << aObj.get() << ":";
988 aPtrStr << "anAIS = " << aAISObj.get() << ":";
989 aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
991 aContent.append(aPtrStr.str().c_str());
993 return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
994 arg(aContent.join("\n")).toStdString().c_str();
998 //**************************************************************
999 void XGUI_Displayer::getPresentations(const ObjectPtr& theObject,
1000 NCollection_Map<Handle(AIS_InteractiveObject)>& thePresentations)
1002 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1003 if (aResult.get()) {
1004 AISObjectPtr aAISObj = getAISObject(aResult);
1005 if (aAISObj.get() == NULL) {
1006 // if result is a result of a composite feature, it is visualized by visualization of
1007 // composite children, so we should get one of this presentations
1008 ResultBodyPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
1009 if (aCompSolid.get() && aCompSolid->numberOfSubs() > 0) {
1010 aAISObj = getAISObject(aCompSolid->subResult(0));
1013 if (aAISObj.get() != NULL) {
1014 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1015 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1016 thePresentations.Add(anAIS);
1020 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
1021 // find presentation of the feature
1022 AISObjectPtr aAISObj = getAISObject(aFeature);
1023 if (aAISObj.get() != NULL) {
1024 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1025 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1026 thePresentations.Add(anAIS);
1028 // find presentations of the feature results
1029 std::list<ResultPtr> aResults;
1030 ModelAPI_Tools::allResults(aFeature, aResults);
1031 std::list<ResultPtr>::const_iterator anIt = aResults.begin(), aLast = aResults.end();
1032 for (; anIt != aLast; ++anIt) {
1033 AISObjectPtr aAISObj = getAISObject(*anIt);
1034 if (aAISObj.get() != NULL) {
1035 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1036 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1037 thePresentations.Add(anAIS);
1043 //**************************************************************
1044 void XGUI_Displayer::displayTrihedron(bool theToDisplay) const
1046 Handle(AIS_InteractiveContext) aContext = AISContext();
1047 if (aContext.IsNull())
1050 Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
1052 XGUI_SelectionActivate* aSelectionActive = selectionActivate();
1054 if (!aContext->IsDisplayed(aTrihedron))
1055 aContext->Display(aTrihedron,
1057 -1 /* selection mode */,
1058 Standard_True /* update viewer*/,
1059 Standard_False /* allow decomposition */,
1060 AIS_DS_Displayed /* xdisplay status */);
1062 if (getCallBack()) getCallBack()->Display(aTrihedron);
1065 if (!aSelectionActive->isTrihedronActive())
1066 aSelectionActive->deactivateTrihedron(false);
1068 aSelectionActive->activate(aTrihedron, false);
1070 aSelectionActive->deactivateTrihedron(false);
1072 aContext->Erase(aTrihedron, Standard_True);
1074 if (getCallBack()) getCallBack()->Remove(aTrihedron);
1079 //**************************************************************
1080 void XGUI_Displayer::AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
1081 const NCollection_DataMap<TopoDS_Shape,
1082 NCollection_Map<Handle(AIS_InteractiveObject)>>& theShapesToBeSelected)
1084 NCollection_Map<Handle(AIS_InteractiveObject)> aCompsolidPresentations;
1085 NCollection_Map<Handle(AIS_InteractiveObject)> aSelectedPresentations;
1087 NCollection_List<Handle(SelectBasics_EntityOwner)> anActiveOwners;
1088 theContext->MainSelector()->ActiveOwners(anActiveOwners);
1089 NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (anActiveOwners);
1090 Handle(SelectMgr_EntityOwner) anOwner;
1092 /// It is very important to check that the owner is processed only once and has a map of
1093 /// processed owners because SetSelected works as a switch.
1094 /// If count of calls setSelectec is even, the object stays in the previous state
1095 /// (selected, deselected)
1096 /// OCCT: to write about the problem that active owners method returns one owner several times
1097 QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
1098 for (; anOwnersIt.More(); anOwnersIt.Next()) {
1099 anOwner = Handle(SelectMgr_EntityOwner)::DownCast (anOwnersIt.Value());
1100 if (aSelectedIds.contains((size_t)anOwner.get()))
1102 aSelectedIds.append((size_t)anOwner.get());
1104 Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
1105 if (!BROwnr.IsNull() && BROwnr->HasShape()) {
1106 const TopoDS_Shape& aShape = BROwnr->Shape();
1107 if (aShape.IsNull())
1110 Handle(ModuleBase_BRepOwner) aCustomOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
1112 NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)> >
1113 ::Iterator aShapeIt(theShapesToBeSelected);
1114 for (; aShapeIt.More(); aShapeIt.Next()) {
1115 const TopoDS_Shape& aParameterShape = aShapeIt.Key();
1116 // In case of compound we cannot rely on simple comparison method.
1117 // If the compound is generated by Group feature then this compound is alwais new.
1118 // So, we have to compare content of these compounds
1120 // isSame should be used here as it does not check orientation of shapes
1121 // despite on isEqual of shapes or IsBound for shape in QMap. Orientation is
1122 // different for Edges shapes in model shape and owner even if this is the same shape
1123 if (ModuleBase_Tools::isSameShape(aParameterShape, aShape)) {
1124 Handle(AIS_InteractiveObject) anOwnerPresentation =
1125 Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
1126 NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations =
1127 theShapesToBeSelected.Find(aParameterShape);
1128 if (aPresentations.Contains(anOwnerPresentation)) {
1129 theContext->AddOrRemoveSelected(anOwner, Standard_False);
1130 anOwner->SetSelected(Standard_True);
1131 // collect selected presentations to do not select them if compsolid is selected
1132 if (!aSelectedPresentations.Contains(anOwnerPresentation))
1133 aSelectedPresentations.Add(anOwnerPresentation);
1136 else if (!aCustomOwner.IsNull()) { // CompSolid processing #2219
1137 // shape of owner is compound, but shape to be selected is compsolid, so
1138 // we need to compare shape to AIS presentation of owner(rule of the owner creation)
1139 Handle(AIS_Shape) anOwnerPresentation =
1140 Handle(AIS_Shape)::DownCast(anOwner->Selectable());
1141 const TopoDS_Shape& aPresentationShape = anOwnerPresentation->Shape();
1142 if (aParameterShape.IsSame(anOwnerPresentation->Shape()) &&
1143 !aCompsolidPresentations.Contains(anOwnerPresentation))
1144 aCompsolidPresentations.Add(anOwnerPresentation);
1149 // select CompSolid presentations if their owners was not selected yet
1150 NCollection_Map<Handle(AIS_InteractiveObject)>::Iterator anIt (aCompsolidPresentations);
1151 for (; anIt.More(); anIt.Next()) {
1152 if (aSelectedPresentations.Contains(anIt.Value()))
1154 theContext->AddOrRemoveSelected(anIt.Value(), Standard_False);
1158 //**************************************************************
1159 XGUI_SelectionActivate* XGUI_Displayer::selectionActivate() const
1161 return myWorkshop->selectionActivate();
1164 //**************************************************************
1165 GeomPlanePtr XGUI_Displayer::getScreenPlane() const
1167 GeomPlanePtr aResult;
1168 Handle(AIS_InteractiveContext) aContext = AISContext();
1169 if (!aContext.IsNull()) {
1170 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1171 Handle(V3d_View) aView;
1172 for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews()) {
1173 aView = aViewer->ActiveView();
1176 if (!aView.IsNull()) {
1177 double aEyeX, aEyeY, aEyeZ;
1178 aView->Eye(aEyeX, aEyeY, aEyeZ);
1180 double aProjX, aProjY, aProjZ;
1181 aView->Proj(aProjX, aProjY, aProjZ);
1183 GeomPointPtr aPnt = GeomPointPtr(new GeomAPI_Pnt(aEyeX, aEyeY, aEyeZ));
1184 GeomDirPtr aDir = GeomDirPtr(new GeomAPI_Dir(aProjX, aProjY, aProjZ));
1186 aResult = GeomPlanePtr(new GeomAPI_Pln(aPnt, aDir));
1192 double XGUI_Displayer::getViewScale() const
1194 Handle(AIS_InteractiveContext) aContext = AISContext();
1195 if (!aContext.IsNull()) {
1196 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1197 Handle(V3d_View) aView;
1198 for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews()) {
1199 aView = aViewer->ActiveView();
1202 return aView->Camera()->Scale();