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 std::vector<int> aIsoValues;
320 ModelAPI_Tools::getIsoLines(aResult, aIsoValues);
321 if (aIsoValues.size() > 0) {
322 Handle(ModuleBase_ResultPrs) aResPrs = Handle(ModuleBase_ResultPrs)::DownCast(aAISIO);
323 if (!aResPrs.IsNull())
324 aResPrs->setIsolinesNumber(aIsoValues[0], aIsoValues[1]);
327 myWorkshop->module()->storeSelection();
329 #ifdef CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
330 myWorkshop->selector()->deselectPresentation(aAISIO);
333 if (aContext->IsDisplayed(aAISIO))
334 aContext->Redisplay(aAISIO, false);
336 aContext->Display(aAISIO, false);
339 if (getCallBack()) getCallBack()->Redisplay(aAISIO);
342 //if (aNeedToRestoreSelection)
343 myWorkshop->module()->restoreSelection();
346 #ifdef DEBUG_FEATURE_REDISPLAY
347 qDebug(" Redisplay happens");
355 //**************************************************************
356 void XGUI_Displayer::redisplayObjects()
358 // redisplay objects visualized in the viewer
359 static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
360 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
361 QObjectPtrList aDisplayed = displayedObjects();
362 QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
363 for (; anIt != aLast; anIt++) {
364 aECreator->sendUpdated(*anIt, EVENT_DISP);
366 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
367 if (aViewer->isColorScaleVisible()) {
368 aViewer->setupColorScale();
369 aViewer->setColorScaleShown(true);
371 Events_Loop::loop()->flush(EVENT_DISP);
374 //**************************************************************
375 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
376 const bool theUpdateViewer)
378 //Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
379 //if (!aTrihedron.IsNull())
380 // deactivateAIS(aTrihedron);
382 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
383 for (; anIt != aLast; anIt++) {
384 selectionActivate()->deactivate(*anIt, false);
386 //VSV It seems that there is no necessity to update viewer on deactivation
387 //if (theUpdateViewer)
391 //**************************************************************
392 bool XGUI_Displayer::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
394 bool aVisible = false;
395 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
396 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
397 if (aPrs.get() || aResult.get()) {
398 aVisible = theDisplayer->isVisible(theObject);
399 // compsolid is not visualized in the viewer,
400 // but should have presentation when all sub solids are
401 // visible. It is useful for highlight presentation where compsolid shape is selectable
402 if (!aVisible && aResult.get() && aResult->groupName() == ModelAPI_ResultBody::group()) {
403 ResultBodyPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
404 if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
405 int aNumberOfSubs = aCompsolidResult->numberOfSubs();
406 bool anAllSubsVisible = aNumberOfSubs > 0;
407 for(int i = 0; i < aNumberOfSubs && anAllSubsVisible; i++) {
408 anAllSubsVisible = theDisplayer->isVisible(aCompsolidResult->subResult(i));
410 aVisible = anAllSubsVisible;
414 // it is possible that feature is presentable and has results, so we should check visibility
415 // of results if presentation is not shown (e.g. Sketch Circle/Arc features)
417 // check if all results of the feature are visible
418 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
419 std::list<ResultPtr> aResults;
420 ModelAPI_Tools::allResults(aFeature, aResults);
421 std::list<ResultPtr>::const_iterator aIt;
422 aVisible = !aResults.empty();
423 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
424 aVisible = aVisible && theDisplayer->isVisible(*aIt);
430 //**************************************************************
431 void XGUI_Displayer::setSelected(const QList<ModuleBase_ViewerPrsPtr>& theValues,
432 bool theUpdateViewer)
434 Handle(AIS_InteractiveContext) aContext = AISContext();
435 if (aContext.IsNull())
437 aContext->UnhilightSelected(false);
438 aContext->ClearSelected(false);
440 if (getCallBack()) getCallBack()->ClearSelected();
442 NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)>>
445 foreach (ModuleBase_ViewerPrsPtr aPrs, theValues) {
446 const GeomShapePtr& aGeomShape = aPrs->shape();
447 if (aGeomShape.get() && !aGeomShape->isNull()) {
448 const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
449 #ifdef DEBUG_OCCT_SHAPE_SELECTION
450 // problem 1: performance
451 // problem 2: IO is not specified, so the first found owner is selected, as a result
452 // it might belong to another result
453 aContext->AddOrRemoveSelected(aShape, false);
455 if (getCallBack()) getCallBack()->AddOrRemoveSelected(aShape);
458 NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations;
459 if (aShapesToBeSelected.IsBound(aShape))
460 aPresentations = aShapesToBeSelected.Find(aShape);
461 ObjectPtr anObject = aPrs->object();
462 getPresentations(anObject, aPresentations);
464 aShapesToBeSelected.Bind(aShape, aPresentations);
467 ObjectPtr anObject = aPrs->object();
468 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
469 if (aResult.get() && isVisible(aResult)) {
470 AISObjectPtr anObj = myResult2AISObjectMap.value(aResult);
471 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
472 if (!anAIS.IsNull()) {
473 // The methods are replaced in order to provide multi-selection, e.g. restore selection
474 // by activating multi selector widget. It also gives an advantage that the multi
475 // selection in OB gives multi-selection in the viewer
476 //aContext->SetSelected(anAIS, false);
477 // The selection in the context was cleared, so the method sets the objects are selected
478 aContext->AddOrRemoveSelected(anAIS, false);
480 if (getCallBack()) getCallBack()->AddOrRemoveSelected(anAIS);
486 if (!aShapesToBeSelected.IsEmpty())
487 XGUI_Displayer::AddOrRemoveSelectedShapes(aContext, aShapesToBeSelected);
493 //**************************************************************
494 void XGUI_Displayer::clearSelected(const bool theUpdateViewer)
496 Handle(AIS_InteractiveContext) aContext = AISContext();
497 if (!aContext.IsNull()) {
498 aContext->UnhilightSelected(false);//UnhilightCurrents(false);
499 aContext->ClearSelected(theUpdateViewer);
501 if (getCallBack()) getCallBack()->ClearSelected();
506 //**************************************************************
507 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
509 bool aErased = false;
510 Handle(AIS_InteractiveContext) aContext = AISContext();
511 if (!aContext.IsNull()) {
513 foreach(ObjectPtr aObj, myResult2AISObjectMap.objects()) {
514 AISObjectPtr aAISObj = myResult2AISObjectMap.value(aObj);
516 foreach(ObjectPtr aObj, myResult2AISObjectMap.keys()) {
517 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
520 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
521 if (!anIO.IsNull()) {
522 emit beforeObjectErase(aObj, aAISObj);
523 aContext->Remove(anIO, false/*update viewer*/);
525 if (getCallBack()) getCallBack()->Remove(anIO);
533 myResult2AISObjectMap.clear();
536 qDebug(getResult2AISObjectMapInfo().c_str());
542 //**************************************************************
543 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
546 return myResult2AISObjectMap.value(theObject);
549 if (myResult2AISObjectMap.contains(theObject))
550 anIO = myResult2AISObjectMap[theObject];
555 //**************************************************************
556 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
558 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
559 return getObject(aRefAIS);
562 //**************************************************************
563 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
566 ObjectPtr anObject = myResult2AISObjectMap.value(theIO);
569 ResultToAISMap::const_iterator aMapIter = myResult2AISObjectMap.cbegin();
570 for (; aMapIter != myResult2AISObjectMap.cend(); aMapIter++) {
571 const AISObjectPtr& aAIS = aMapIter.value();
572 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
574 anObject = aMapIter.key();
579 if (!anObject.get()) {
580 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
581 if (!theIO.IsNull()) {
582 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
584 anObject = myWorkshop->module()->findPresentedObject(anAISObj);
589 //**************************************************************
590 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
592 bool aWasEnabled = isUpdateEnabled();
594 myViewerBlockedRecursiveCount--;
596 myViewerBlockedRecursiveCount++;
598 #ifdef DEBUG_VIEWER_BLOCKED_COUNT
599 std::cout << "myViewerBlockedRecursiveCount = " << myViewerBlockedRecursiveCount << std::endl;
602 if (myNeedUpdate && isUpdateEnabled()) {
604 myNeedUpdate = false;
609 //**************************************************************
610 void XGUI_Displayer::updateViewer() const
612 Handle(AIS_InteractiveContext) aContext = AISContext();
614 #ifdef DEBUG_VIEWER_BLOCKED_COUNT
615 std::cout << "updateViewer: " << (myViewerBlockedRecursiveCount == 0 ? " done" : " later")
619 if (!aContext.IsNull() && isUpdateEnabled()) {
620 //myWorkshop->viewer()->Zfitall();
621 aContext->UpdateCurrentViewer();
627 //**************************************************************
628 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
630 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
631 if (!aContext.IsNull() && (myContextId != aContext.get())) {
632 myContextId = aContext.get();
633 if (!myWorkshop->selectionActivate()->isTrihedronActive())
634 selectionActivate()->deactivateTrihedron(true);
635 // Do not modify default drawer. The same is done in ModuleBase_ResultPrs
636 //aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
637 //aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
639 //Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
640 //aTrihedron->getHighlightPointAspect()->SetScale(2.0);
641 //aTrihedron->getHighlightPointAspect()->SetTypeOfMarker(Aspect_TOM_O_STAR);
643 // Commented out according to discussion in bug #2825
644 ModuleBase_IViewer::DefaultHighlightDrawer = aContext->HighlightStyle();
645 //Handle(Prs3d_Drawer) aSelStyle = aContext->SelectionStyle();
646 //double aDeflection =
647 // QString(ModelAPI_ResultConstruction::DEFAULT_DEFLECTION().c_str()).toDouble();
649 // aDeflection = Config_PropManager::real("Visualization", "construction_deflection");
652 //ModuleBase_IViewer::DefaultHighlightDrawer->SetDeviationCoefficient(aDeflection);
653 //aSelStyle->SetDeviationCoefficient(aDeflection);
658 //**************************************************************
659 void XGUI_Displayer::setSelectionColor(const std::vector<int>& theColor)
661 Handle(AIS_InteractiveContext) aContext = AISContext();
662 if (!aContext.IsNull()) {
663 Quantity_Color aQColor(theColor[0] / 255.,
665 theColor[2] / 255., Quantity_TOC_RGB);
666 aContext->SelectionStyle()->SetColor(aQColor);
667 aContext->SelectionStyle()->PointAspect()->SetColor(aQColor);
668 aContext->SelectionStyle()->LineAspect()->SetColor(aQColor);
669 aContext->HighlightStyle(Prs3d_TypeOfHighlight_LocalSelected)->SetColor(aQColor);
674 //**************************************************************
675 std::vector<int> XGUI_Displayer::selectionColor() const
677 std::vector<int> aColor;
678 Handle(AIS_InteractiveContext) aContext = AISContext();
679 if (!aContext.IsNull()) {
680 Quantity_Color aQColor = aContext->SelectionStyle()->Color();
681 aColor.push_back((int)(aQColor.Red() * 255));
682 aColor.push_back((int)(aQColor.Green() * 255));
683 aColor.push_back((int)(aQColor.Blue() * 255));
689 //**************************************************************
690 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
692 Handle(AIS_InteractiveContext) aContext = AISContext();
693 if (!aContext.IsNull() && myAndFilter.IsNull()) {
694 myAndFilter = new SelectMgr_AndFilter();
695 aContext->AddFilter(myAndFilter);
700 //**************************************************************
701 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
702 const Standard_Integer theDisplayMode, bool theUpdateViewer)
704 bool aDisplayed = false;
705 Handle(AIS_InteractiveContext) aContext = AISContext();
706 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
707 if (!aContext.IsNull() && !anAISIO.IsNull()) {
708 aContext->Display(anAISIO, theDisplayMode, 0, false/*update viewer*/, true, AIS_DS_Displayed);
710 if (getCallBack()) getCallBack()->Display(anAISIO);
713 aContext->Deactivate(anAISIO);
715 if (getCallBack()) getCallBack()->Deactivate(anAISIO);
717 aContext->Load(anAISIO);
719 if (getCallBack()) getCallBack()->Load(anAISIO);
721 if (toActivateInSelectionModes)
722 myWorkshop->selectionActivate()->activateOnDisplay(anAISIO, theUpdateViewer);
730 //**************************************************************
731 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
733 bool aErased = false;
734 Handle(AIS_InteractiveContext) aContext = AISContext();
735 if (!aContext.IsNull()) {
736 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
737 if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
738 aContext->Remove(anAISIO, false/*update viewer*/);
740 if (getCallBack()) getCallBack()->Remove(anAISIO);
745 if (aErased && theUpdateViewer)
750 //**************************************************************
751 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
753 if (theMode == NoMode)
756 Handle(AIS_InteractiveContext) aContext = AISContext();
757 if (aContext.IsNull())
760 AISObjectPtr aAISObj = getAISObject(theObject);
764 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
765 aContext->SetDisplayMode(aAISIO, theMode, false);
766 // Redisplay in order to update new mode because it could be not computed before
771 //**************************************************************
772 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
774 Handle(AIS_InteractiveContext) aContext = AISContext();
775 if (aContext.IsNull())
778 AISObjectPtr aAISObj = getAISObject(theObject);
782 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
783 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
786 //**************************************************************
787 void XGUI_Displayer::deactivateSelectionFilters(const bool theAddFilterOnly)
789 Handle(AIS_InteractiveContext) aContext = AISContext();
790 if (!myAndFilter.IsNull()) {
792 if (!aContext.IsNull()) {
793 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
794 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
795 for (; anIt.More() && !aFound; anIt.Next()) {
796 Handle(SelectMgr_Filter) aFilter = anIt.Value();
797 aFound = aFilter == myAndFilter;
800 aContext->RemoveFilter(myAndFilter);
802 myAndFilter.Nullify();
806 //**************************************************************
807 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
809 Handle(AIS_InteractiveContext) aContext = AISContext();
810 if (aContext.IsNull() || hasSelectionFilter(theFilter))
813 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
814 if (!aCompositeFilter.IsNull()) {
815 aCompositeFilter->Add(theFilter);
816 #ifdef DEBUG_SELECTION_FILTERS
817 int aCount = aCompositeFilter->StoredFilters().Extent();
818 qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
823 //**************************************************************
824 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
826 Handle(AIS_InteractiveContext) aContext = AISContext();
827 if (aContext.IsNull())
830 Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
831 if (!aCompositeFilter.IsNull() && aCompositeFilter->IsIn(theFilter)) {
832 aCompositeFilter->Remove(theFilter);
833 #ifdef DEBUG_SELECTION_FILTERS
834 int aCount = aCompositeFilter->StoredFilters().Extent();
835 qDebug(QString("removeSelectionFilter: filters.count() = %1")
836 .arg(aCount).toStdString().c_str());
841 //**************************************************************
842 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
844 bool aFilterFound = false;
846 Handle(AIS_InteractiveContext) aContext = AISContext();
847 if (aContext.IsNull())
849 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
850 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
851 for (; aIt.More() && !aFilterFound; aIt.Next()) {
852 if (theFilter.get() == aIt.Value().get())
855 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
856 if (!aCompositeFilter.IsNull()) {
857 const SelectMgr_ListOfFilter& aStoredFilters = aCompositeFilter->StoredFilters();
858 for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
859 if (theFilter.get() == aIt.Value().get())
866 //**************************************************************
867 void XGUI_Displayer::removeFilters()
869 Handle(AIS_InteractiveContext) aContext = AISContext();
870 if (aContext.IsNull())
873 Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
874 if (!aCompositeFilter.IsNull())
875 aCompositeFilter->Clear();
878 //**************************************************************
879 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
882 QObjectPtrList aDispList = myResult2AISObjectMap.objects();
884 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
886 foreach(ObjectPtr aObj, aDispList) {
887 if (!theList.contains(aObj))
890 foreach(ObjectPtr aObj, theList) {
891 if (!isVisible(aObj))
892 display(aObj, false);
897 //**************************************************************
898 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
900 if (!isVisible(theObject))
903 AISObjectPtr aAISObj = getAISObject(theObject);
904 if (aAISObj.get() == NULL)
907 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
908 return ::canBeShaded(anAIS, myWorkshop->module());
911 //**************************************************************
912 //bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
914 // AISObjectPtr anAISObj = getAISObject(theObject);
915 // // correct the result's color it it has the attribute
916 // ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
918 // // Customization of presentation
919 // GeomCustomPrsPtr aCustomPrs;
920 // FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
921 // if (aFeature.get() != NULL) {
922 // GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
923 // if (aCustPrs.get() != NULL)
924 // aCustomPrs = aCustPrs;
926 // if (aCustomPrs.get() == NULL) {
927 // GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
928 // // we ignore presentable not customized objects
929 // if (aPrs.get() == NULL)
930 // aCustomPrs = myCustomPrs;
932 // bool isCustomized = aCustomPrs.get() &&
933 // aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
934 // isCustomized = myWorkshop->module()->afterCustomisePresentation(aResult, anAISObj, myCustomPrs)
937 // // update presentation state if faces panel is active
938 // if (anAISObj.get() && myWorkshop->facesPanel())
939 // isCustomized = myWorkshop->facesPanel()->customizeObject(theObject, anAISObj) || isCustomized;
941 // return isCustomized;
944 //**************************************************************
945 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject,
946 const QColor& theColor,
947 bool theUpdateViewer)
949 if (!isVisible(theObject))
952 AISObjectPtr anAISObj = getAISObject(theObject);
954 anAISObj->getColor(aR, aG, aB);
955 anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
958 return QColor(aR, aG, aB);
961 //**************************************************************
962 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
965 myResult2AISObjectMap.add(theObject, theAIS);
967 myResult2AISObjectMap[theObject] = theAIS;
971 std::ostringstream aPtrStr;
972 aPtrStr << theObject.get();
973 qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
974 qDebug(getResult2AISObjectMapInfo().c_str());
979 //**************************************************************
980 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
982 QStringList aContent;
984 foreach(ObjectPtr aObj, myResult2AISObjectMap.objects()) {
985 AISObjectPtr aAISObj = myResult2AISObjectMap.value(aObj);
987 foreach(ObjectPtr aObj, myResult2AISObjectMap.keys()) {
988 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
990 std::ostringstream aPtrStr;
991 aPtrStr << "aObj = " << aObj.get() << ":";
992 aPtrStr << "anAIS = " << aAISObj.get() << ":";
993 aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
995 aContent.append(aPtrStr.str().c_str());
997 return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
998 arg(aContent.join("\n")).toStdString().c_str();
1002 //**************************************************************
1003 void XGUI_Displayer::getPresentations(const ObjectPtr& theObject,
1004 NCollection_Map<Handle(AIS_InteractiveObject)>& thePresentations)
1006 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1007 if (aResult.get()) {
1008 AISObjectPtr aAISObj = getAISObject(aResult);
1009 if (aAISObj.get() == NULL) {
1010 // if result is a result of a composite feature, it is visualized by visualization of
1011 // composite children, so we should get one of this presentations
1012 ResultBodyPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
1013 if (aCompSolid.get() && aCompSolid->numberOfSubs() > 0) {
1014 aAISObj = getAISObject(aCompSolid->subResult(0));
1017 if (aAISObj.get() != NULL) {
1018 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1019 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1020 thePresentations.Add(anAIS);
1024 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
1025 // find presentation of the feature
1026 AISObjectPtr aAISObj = getAISObject(aFeature);
1027 if (aAISObj.get() != NULL) {
1028 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1029 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1030 thePresentations.Add(anAIS);
1032 // find presentations of the feature results
1033 std::list<ResultPtr> aResults;
1034 ModelAPI_Tools::allResults(aFeature, aResults);
1035 std::list<ResultPtr>::const_iterator anIt = aResults.begin(), aLast = aResults.end();
1036 for (; anIt != aLast; ++anIt) {
1037 AISObjectPtr aAISObj = getAISObject(*anIt);
1038 if (aAISObj.get() != NULL) {
1039 Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1040 if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1041 thePresentations.Add(anAIS);
1047 //**************************************************************
1048 void XGUI_Displayer::displayTrihedron(bool theToDisplay) const
1050 Handle(AIS_InteractiveContext) aContext = AISContext();
1051 if (aContext.IsNull())
1054 Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
1056 XGUI_SelectionActivate* aSelectionActive = selectionActivate();
1058 if (!aContext->IsDisplayed(aTrihedron))
1059 aContext->Display(aTrihedron,
1061 -1 /* selection mode */,
1062 Standard_True /* update viewer*/,
1063 Standard_False /* allow decomposition */,
1064 AIS_DS_Displayed /* xdisplay status */);
1066 if (getCallBack()) getCallBack()->Display(aTrihedron);
1069 if (!aSelectionActive->isTrihedronActive())
1070 aSelectionActive->deactivateTrihedron(false);
1072 aSelectionActive->activate(aTrihedron, false);
1074 aSelectionActive->deactivateTrihedron(false);
1076 aContext->Erase(aTrihedron, Standard_True);
1078 if (getCallBack()) getCallBack()->Remove(aTrihedron);
1083 //**************************************************************
1084 void XGUI_Displayer::AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
1085 const NCollection_DataMap<TopoDS_Shape,
1086 NCollection_Map<Handle(AIS_InteractiveObject)>>& theShapesToBeSelected)
1088 NCollection_Map<Handle(AIS_InteractiveObject)> aCompsolidPresentations;
1089 NCollection_Map<Handle(AIS_InteractiveObject)> aSelectedPresentations;
1091 NCollection_List<Handle(SelectBasics_EntityOwner)> anActiveOwners;
1092 theContext->MainSelector()->ActiveOwners(anActiveOwners);
1093 NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (anActiveOwners);
1094 Handle(SelectMgr_EntityOwner) anOwner;
1096 /// It is very important to check that the owner is processed only once and has a map of
1097 /// processed owners because SetSelected works as a switch.
1098 /// If count of calls setSelectec is even, the object stays in the previous state
1099 /// (selected, deselected)
1100 /// OCCT: to write about the problem that active owners method returns one owner several times
1101 QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
1102 for (; anOwnersIt.More(); anOwnersIt.Next()) {
1103 anOwner = Handle(SelectMgr_EntityOwner)::DownCast (anOwnersIt.Value());
1104 if (aSelectedIds.contains((size_t)anOwner.get()))
1106 aSelectedIds.append((size_t)anOwner.get());
1108 Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
1109 if (!BROwnr.IsNull() && BROwnr->HasShape()) {
1110 const TopoDS_Shape& aShape = BROwnr->Shape();
1111 if (aShape.IsNull())
1114 Handle(ModuleBase_BRepOwner) aCustomOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
1116 NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)> >
1117 ::Iterator aShapeIt(theShapesToBeSelected);
1118 for (; aShapeIt.More(); aShapeIt.Next()) {
1119 const TopoDS_Shape& aParameterShape = aShapeIt.Key();
1120 // In case of compound we cannot rely on simple comparison method.
1121 // If the compound is generated by Group feature then this compound is alwais new.
1122 // So, we have to compare content of these compounds
1124 // isSame should be used here as it does not check orientation of shapes
1125 // despite on isEqual of shapes or IsBound for shape in QMap. Orientation is
1126 // different for Edges shapes in model shape and owner even if this is the same shape
1127 if (ModuleBase_Tools::isSameShape(aParameterShape, aShape)) {
1128 Handle(AIS_InteractiveObject) anOwnerPresentation =
1129 Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
1130 NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations =
1131 theShapesToBeSelected.Find(aParameterShape);
1132 if (aPresentations.Contains(anOwnerPresentation)) {
1133 theContext->AddOrRemoveSelected(anOwner, Standard_False);
1134 anOwner->SetSelected(Standard_True);
1135 // collect selected presentations to do not select them if compsolid is selected
1136 if (!aSelectedPresentations.Contains(anOwnerPresentation))
1137 aSelectedPresentations.Add(anOwnerPresentation);
1140 else if (!aCustomOwner.IsNull()) { // CompSolid processing #2219
1141 // shape of owner is compound, but shape to be selected is compsolid, so
1142 // we need to compare shape to AIS presentation of owner(rule of the owner creation)
1143 Handle(AIS_Shape) anOwnerPresentation =
1144 Handle(AIS_Shape)::DownCast(anOwner->Selectable());
1145 const TopoDS_Shape& aPresentationShape = anOwnerPresentation->Shape();
1146 if (aParameterShape.IsSame(anOwnerPresentation->Shape()) &&
1147 !aCompsolidPresentations.Contains(anOwnerPresentation))
1148 aCompsolidPresentations.Add(anOwnerPresentation);
1153 // select CompSolid presentations if their owners was not selected yet
1154 NCollection_Map<Handle(AIS_InteractiveObject)>::Iterator anIt (aCompsolidPresentations);
1155 for (; anIt.More(); anIt.Next()) {
1156 if (aSelectedPresentations.Contains(anIt.Value()))
1158 theContext->AddOrRemoveSelected(anIt.Value(), Standard_False);
1162 //**************************************************************
1163 XGUI_SelectionActivate* XGUI_Displayer::selectionActivate() const
1165 return myWorkshop->selectionActivate();
1168 //**************************************************************
1169 GeomPlanePtr XGUI_Displayer::getScreenPlane() const
1171 GeomPlanePtr aResult;
1172 Handle(AIS_InteractiveContext) aContext = AISContext();
1173 if (!aContext.IsNull()) {
1174 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1175 Handle(V3d_View) aView;
1176 for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews()) {
1177 aView = aViewer->ActiveView();
1180 if (!aView.IsNull()) {
1181 double aEyeX, aEyeY, aEyeZ;
1182 aView->Eye(aEyeX, aEyeY, aEyeZ);
1184 double aProjX, aProjY, aProjZ;
1185 aView->Proj(aProjX, aProjY, aProjZ);
1187 GeomPointPtr aPnt = GeomPointPtr(new GeomAPI_Pnt(aEyeX, aEyeY, aEyeZ));
1188 GeomDirPtr aDir = GeomDirPtr(new GeomAPI_Dir(aProjX, aProjY, aProjZ));
1190 aResult = GeomPlanePtr(new GeomAPI_Pln(aPnt, aDir));
1196 double XGUI_Displayer::getViewScale() const
1198 Handle(AIS_InteractiveContext) aContext = AISContext();
1199 if (!aContext.IsNull()) {
1200 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1201 Handle(V3d_View) aView;
1202 for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews()) {
1203 aView = aViewer->ActiveView();
1206 return aView->Camera()->Scale();