1 // Copyright (C) 2014-2020 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>
75 #include <BRepMesh_IncrementalMesh.hxx>
77 #include <StdSelect_ViewerSelector3d.hxx>
79 #include <TColStd_MapOfTransient.hxx>
80 #include <TColStd_MapIteratorOfMapOfTransient.hxx>
81 #include <Standard_Version.hxx>
84 #include <inspector/VInspectorAPI_CallBack.hxx>
87 #include <Events_Loop.h>
88 #include <ModelAPI_Events.h>
89 #include <Config_PropManager.h>
93 /// defines the local context mouse selection sensitivity
94 const int MOUSE_SENSITIVITY_IN_PIXEL = 10;
96 //#define DEBUG_DISPLAY
97 //#define DEBUG_FEATURE_REDISPLAY
98 //#define DEBUG_SELECTION_FILTERS
100 //#define DEBUG_OCCT_SHAPE_SELECTION
102 #define CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
104 //#define DEBUG_VIEWER_BLOCKED_COUNT
106 //**************************************************************
107 void displayedObjects(const Handle(AIS_InteractiveContext)& theAIS, AIS_ListOfInteractive& theList)
109 // Get from null point
110 #if OCC_VERSION_HEX < 0x070400
111 theAIS->DisplayedObjects(theList, true);
113 theAIS->DisplayedObjects(theList);
117 QString qIntListInfo(const QIntList& theValues, const QString& theSeparator = QString(", "))
120 QIntList::const_iterator anIt = theValues.begin(), aLast = theValues.end();
121 for (; anIt != aLast; anIt++) {
122 anInfo.append(QString::number(*anIt));
124 return anInfo.join(theSeparator);
127 //**************************************************************
128 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
129 : myWorkshop(theWorkshop), myNeedUpdate(false),
130 myViewerBlockedRecursiveCount(0), myContextId(0)
132 BRepMesh_IncrementalMesh::SetParallelDefault(Standard_True);
135 //**************************************************************
136 XGUI_Displayer::~XGUI_Displayer()
140 //**************************************************************
141 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
143 return myResult2AISObjectMap.contains(theObject);
146 //**************************************************************
147 bool XGUI_Displayer::display(ObjectPtr theObject, bool theUpdateViewer)
149 bool aDisplayed = false;
150 if (isVisible(theObject)) {
151 aDisplayed = redisplay(theObject, theUpdateViewer);
154 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
155 bool isShading = false;
156 if (aPrs.get() != NULL) {
157 GeomScreenParamsPtr aScreen = std::dynamic_pointer_cast<GeomAPI_IScreenParams>(theObject);
159 aScreen->setScreenPlane(getScreenPlane());
160 aScreen->setViewScale(getViewScale());
162 anAIS = aPrs->getAISObject(anAIS);
164 anAIS = myWorkshop->module()->createPresentation(theObject);
168 aDisplayed = display(theObject, anAIS, isShading, theUpdateViewer);
174 //**************************************************************
175 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS, ModuleBase_IModule* theModule)
177 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
178 if (!aShapePrs.IsNull()) {
179 TopoDS_Shape aShape = aShapePrs->Shape();
182 TopAbs_ShapeEnum aType = aShape.ShapeType();
183 if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
186 // Check that the presentation is not a sketch
187 return theModule->canBeShaded(theAIS);
193 //**************************************************************
194 bool XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
195 bool isShading, bool theUpdateViewer)
197 bool aDisplayed = false;
199 Handle(AIS_InteractiveContext) aContext = AISContext();
200 if (aContext.IsNull())
203 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
204 if (!anAISIO.IsNull()) {
205 appendResultObject(theObject, theAIS);
207 //bool isCustomized = customizeObject(theObject);
209 int aDispMode = isShading? Shading : Wireframe;
210 anAISIO->SetDisplayMode(aDispMode);
211 aContext->Display(anAISIO, aDispMode, 0, false, AIS_DS_Displayed);
213 if (getCallBack()) getCallBack()->Display(anAISIO);
217 emit objectDisplayed(theObject, theAIS);
218 selectionActivate()->activate(anAISIO, theUpdateViewer);
226 //**************************************************************
227 bool XGUI_Displayer::erase(ObjectPtr theObject, const bool theUpdateViewer)
229 bool aErased = false;
230 if (!isVisible(theObject))
233 Handle(AIS_InteractiveContext) aContext = AISContext();
234 if (aContext.IsNull())
237 AISObjectPtr anObject = myResult2AISObjectMap.value(theObject);
239 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
240 if (!anAIS.IsNull()) {
241 emit beforeObjectErase(theObject, anObject);
242 aContext->Remove(anAIS, false/*update viewer*/);
244 if (getCallBack()) getCallBack()->Remove(anAIS);
249 myResult2AISObjectMap.remove(theObject);
252 std::ostringstream aPtrStr;
253 aPtrStr << theObject.get();
254 qDebug(QString("erase object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
255 qDebug(getResult2AISObjectMapInfo().c_str());
264 //**************************************************************
265 bool XGUI_Displayer::redisplay(ObjectPtr theObject, bool theUpdateViewer)
267 bool aRedisplayed = false;
268 Handle(AIS_InteractiveContext) aContext = AISContext();
269 if (aContext.IsNull())
272 if (!isVisible(theObject))
275 AISObjectPtr aAISObj = getAISObject(theObject);
276 Handle(AIS_InteractiveObject) aAISIO;
278 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
280 GeomScreenParamsPtr aScreen = std::dynamic_pointer_cast<GeomAPI_IScreenParams>(theObject);
282 aScreen->setScreenPlane(getScreenPlane());
283 aScreen->setViewScale(getViewScale());
285 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
287 aRedisplayed = erase(theObject, theUpdateViewer);
290 if (aAIS_Obj != aAISObj) {
291 erase(theObject, theUpdateViewer);
292 appendResultObject(theObject, aAIS_Obj);
294 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
297 aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
300 if (!aAISIO.IsNull()) {
301 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
304 std::vector<int> aColor;
305 ModelAPI_Tools::getColor(aResult, aColor);
306 if (aColor.size() > 0) {
308 aCol(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB);
309 aAISIO->SetColor(aCol);
312 double aDeflection = ModelAPI_Tools::getDeflection(aResult);
313 if ((aDeflection >= 0) && (aDeflection != aAISObj->getDeflection()))
314 aAISObj->setDeflection(aDeflection);
317 double aTransparency = ModelAPI_Tools::getTransparency(aResult);
318 if ((aTransparency >= 0) && (aTransparency != aAISObj->getTransparency()))
319 aAISObj->setTransparency(aTransparency);
322 Handle(ModuleBase_ResultPrs) aResPrs = Handle(ModuleBase_ResultPrs)::DownCast(aAISIO);
323 if (!aResPrs.IsNull())
324 aResPrs->updateIsoLines();
326 //myWorkshop->module()->storeSelection();
328 //#ifdef CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
329 //myWorkshop->selector()->deselectPresentation(aAISIO);
332 if (aContext->IsDisplayed(aAISIO))
333 aContext->Redisplay(aAISIO, false);
335 aContext->Display(aAISIO, false);
338 if (getCallBack()) getCallBack()->Redisplay(aAISIO);
341 //if (aNeedToRestoreSelection)
342 //myWorkshop->module()->restoreSelection();
345 #ifdef DEBUG_FEATURE_REDISPLAY
346 qDebug(" Redisplay happens");
354 //**************************************************************
355 void XGUI_Displayer::redisplayObjects()
357 // redisplay objects visualized in the viewer
358 static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
359 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
360 QObjectPtrList aDisplayed = displayedObjects();
361 QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
362 for (; anIt != aLast; anIt++) {
363 aECreator->sendUpdated(*anIt, EVENT_DISP);
365 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
366 if (aViewer->isColorScaleVisible()) {
367 aViewer->setupColorScale();
368 aViewer->setColorScaleShown(true);
370 Events_Loop::loop()->flush(EVENT_DISP);
373 //**************************************************************
374 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
375 const bool theUpdateViewer)
377 //Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
378 //if (!aTrihedron.IsNull())
379 // deactivateAIS(aTrihedron);
381 QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
382 for (; anIt != aLast; anIt++) {
383 selectionActivate()->deactivate(*anIt, false);
385 //VSV It seems that there is no necessity to update viewer on deactivation
386 //if (theUpdateViewer)
390 //**************************************************************
391 bool XGUI_Displayer::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
393 bool aVisible = false;
394 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
395 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
396 if (aPrs.get() || aResult.get()) {
397 aVisible = theDisplayer->isVisible(theObject);
398 // compsolid is not visualized in the viewer,
399 // but should have presentation when all sub solids are
400 // visible. It is useful for highlight presentation where compsolid shape is selectable
401 if (!aVisible && aResult.get() && aResult->groupName() == ModelAPI_ResultBody::group()) {
402 ResultBodyPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
403 if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
404 int aNumberOfSubs = aCompsolidResult->numberOfSubs();
405 bool anAllSubsVisible = aNumberOfSubs > 0;
406 for(int i = 0; i < aNumberOfSubs && anAllSubsVisible; i++) {
407 anAllSubsVisible = theDisplayer->isVisible(aCompsolidResult->subResult(i));
409 aVisible = anAllSubsVisible;
413 // it is possible that feature is presentable and has results, so we should check visibility
414 // of results if presentation is not shown (e.g. Sketch Circle/Arc features)
416 // check if all results of the feature are visible
417 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
418 std::list<ResultPtr> aResults;
419 ModelAPI_Tools::allResults(aFeature, aResults);
420 std::list<ResultPtr>::const_iterator aIt;
421 aVisible = !aResults.empty();
422 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
423 aVisible = aVisible && theDisplayer->isVisible(*aIt);
429 //**************************************************************
430 void XGUI_Displayer::setSelected(const QList<ModuleBase_ViewerPrsPtr>& theValues,
431 bool theUpdateViewer)
433 Handle(AIS_InteractiveContext) aContext = AISContext();
434 if (aContext.IsNull())
436 aContext->UnhilightSelected(false);
437 aContext->ClearSelected(false);
439 if (getCallBack()) getCallBack()->ClearSelected();
441 NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)>>
444 foreach (ModuleBase_ViewerPrsPtr aPrs, theValues) {
445 const GeomShapePtr& aGeomShape = aPrs->shape();
446 if (aGeomShape.get() && !aGeomShape->isNull()) {
447 const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
448 #ifdef DEBUG_OCCT_SHAPE_SELECTION
449 // problem 1: performance
450 // problem 2: IO is not specified, so the first found owner is selected, as a result
451 // it might belong to another result
452 aContext->AddOrRemoveSelected(aShape, false);
454 if (getCallBack()) getCallBack()->AddOrRemoveSelected(aShape);
457 NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations;
458 if (aShapesToBeSelected.IsBound(aShape))
459 aPresentations = aShapesToBeSelected.Find(aShape);
460 ObjectPtr anObject = aPrs->object();
461 getPresentations(anObject, aPresentations);
463 aShapesToBeSelected.Bind(aShape, aPresentations);
466 ObjectPtr anObject = aPrs->object();
467 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
468 if (aResult.get() && isVisible(aResult)) {
469 AISObjectPtr anObj = myResult2AISObjectMap.value(aResult);
470 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
471 if (!anAIS.IsNull()) {
472 // The methods are replaced in order to provide multi-selection, e.g. restore selection
473 // by activating multi selector widget. It also gives an advantage that the multi
474 // selection in OB gives multi-selection in the viewer
475 //aContext->SetSelected(anAIS, false);
476 // The selection in the context was cleared, so the method sets the objects are selected
477 aContext->AddOrRemoveSelected(anAIS, false);
479 if (getCallBack()) getCallBack()->AddOrRemoveSelected(anAIS);
485 if (!aShapesToBeSelected.IsEmpty())
486 XGUI_Displayer::AddOrRemoveSelectedShapes(aContext, aShapesToBeSelected);
492 //**************************************************************
493 void XGUI_Displayer::clearSelected(const bool theUpdateViewer)
495 Handle(AIS_InteractiveContext) aContext = AISContext();
496 if (!aContext.IsNull()) {
497 aContext->UnhilightSelected(false);//UnhilightCurrents(false);
498 aContext->ClearSelected(theUpdateViewer);
500 if (getCallBack()) getCallBack()->ClearSelected();
505 //**************************************************************
506 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
508 bool aErased = false;
509 Handle(AIS_InteractiveContext) aContext = AISContext();
510 if (!aContext.IsNull()) {
512 foreach(ObjectPtr aObj, myResult2AISObjectMap.objects()) {
513 AISObjectPtr aAISObj = myResult2AISObjectMap.value(aObj);
515 foreach(ObjectPtr aObj, myResult2AISObjectMap.keys()) {
516 AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
519 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
520 if (!anIO.IsNull()) {
521 emit beforeObjectErase(aObj, aAISObj);
522 aContext->Remove(anIO, false/*update viewer*/);
524 if (getCallBack()) getCallBack()->Remove(anIO);
532 myResult2AISObjectMap.clear();
535 qDebug(getResult2AISObjectMapInfo().c_str());
541 //**************************************************************
542 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
545 return myResult2AISObjectMap.value(theObject);
548 if (myResult2AISObjectMap.contains(theObject))
549 anIO = myResult2AISObjectMap[theObject];
554 //**************************************************************
555 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
557 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
558 return getObject(aRefAIS);
561 //**************************************************************
562 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
565 ObjectPtr anObject = myResult2AISObjectMap.value(theIO);
568 ResultToAISMap::const_iterator aMapIter = myResult2AISObjectMap.cbegin();
569 for (; aMapIter != myResult2AISObjectMap.cend(); aMapIter++) {
570 const AISObjectPtr& aAIS = aMapIter.value();
571 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
573 anObject = aMapIter.key();
578 if (!anObject.get()) {
579 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
580 if (!theIO.IsNull()) {
581 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
583 if (myWorkshop->module())
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 // 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);
651 Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
652 if (!aTrihedron.IsNull())
653 ModuleBase_Tools::setPointBallHighlighting(aTrihedron.get());
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*/, 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 AIS_DS_Displayed /* xdisplay status */);
1065 if (getCallBack()) getCallBack()->Display(aTrihedron);
1068 if (!aSelectionActive->isTrihedronActive())
1069 aSelectionActive->deactivateTrihedron(false);
1071 aSelectionActive->activate(aTrihedron, false);
1073 aSelectionActive->deactivateTrihedron(false);
1075 aContext->Erase(aTrihedron, Standard_True);
1077 if (getCallBack()) getCallBack()->Remove(aTrihedron);
1082 //**************************************************************
1083 void XGUI_Displayer::AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
1084 const NCollection_DataMap<TopoDS_Shape,
1085 NCollection_Map<Handle(AIS_InteractiveObject)>>& theShapesToBeSelected)
1087 NCollection_Map<Handle(AIS_InteractiveObject)> aCompsolidPresentations;
1088 NCollection_Map<Handle(AIS_InteractiveObject)> aSelectedPresentations;
1090 NCollection_List<Handle(SelectBasics_EntityOwner)> anActiveOwners;
1091 theContext->MainSelector()->ActiveOwners(anActiveOwners);
1092 NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (anActiveOwners);
1093 Handle(SelectMgr_EntityOwner) anOwner;
1095 /// It is very important to check that the owner is processed only once and has a map of
1096 /// processed owners because SetSelected works as a switch.
1097 /// If count of calls setSelectec is even, the object stays in the previous state
1098 /// (selected, deselected)
1099 /// OCCT: to write about the problem that active owners method returns one owner several times
1100 QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
1101 for (; anOwnersIt.More(); anOwnersIt.Next()) {
1102 anOwner = anOwnersIt.Value();
1103 if (aSelectedIds.contains((size_t)anOwner.get()))
1105 aSelectedIds.append((size_t)anOwner.get());
1107 Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
1108 if (!BROwnr.IsNull() && BROwnr->HasShape()) {
1109 const TopoDS_Shape& aShape = BROwnr->Shape();
1110 if (aShape.IsNull())
1113 Handle(ModuleBase_BRepOwner) aCustomOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
1115 NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)> >
1116 ::Iterator aShapeIt(theShapesToBeSelected);
1117 for (; aShapeIt.More(); aShapeIt.Next()) {
1118 const TopoDS_Shape& aParameterShape = aShapeIt.Key();
1119 // In case of compound we cannot rely on simple comparison method.
1120 // If the compound is generated by Group feature then this compound is alwais new.
1121 // So, we have to compare content of these compounds
1123 // isSame should be used here as it does not check orientation of shapes
1124 // despite on isEqual of shapes or IsBound for shape in QMap. Orientation is
1125 // different for Edges shapes in model shape and owner even if this is the same shape
1126 if (ModuleBase_Tools::isSameShape(aParameterShape, aShape)) {
1127 Handle(AIS_InteractiveObject) anOwnerPresentation =
1128 Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
1129 NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations =
1130 theShapesToBeSelected.Find(aParameterShape);
1131 if (aPresentations.Contains(anOwnerPresentation)) {
1132 theContext->AddOrRemoveSelected(anOwner, Standard_False);
1133 anOwner->SetSelected(Standard_True);
1134 // collect selected presentations to do not select them if compsolid is selected
1135 if (!aSelectedPresentations.Contains(anOwnerPresentation))
1136 aSelectedPresentations.Add(anOwnerPresentation);
1139 else if (!aCustomOwner.IsNull()) { // CompSolid processing #2219
1140 // shape of owner is compound, but shape to be selected is compsolid, so
1141 // we need to compare shape to AIS presentation of owner(rule of the owner creation)
1142 Handle(AIS_Shape) anOwnerPresentation =
1143 Handle(AIS_Shape)::DownCast(anOwner->Selectable());
1144 const TopoDS_Shape& aPresentationShape = anOwnerPresentation->Shape();
1145 if (aParameterShape.IsSame(anOwnerPresentation->Shape()) &&
1146 !aCompsolidPresentations.Contains(anOwnerPresentation))
1147 aCompsolidPresentations.Add(anOwnerPresentation);
1152 // select CompSolid presentations if their owners was not selected yet
1153 NCollection_Map<Handle(AIS_InteractiveObject)>::Iterator anIt (aCompsolidPresentations);
1154 for (; anIt.More(); anIt.Next()) {
1155 if (aSelectedPresentations.Contains(anIt.Value()))
1157 theContext->AddOrRemoveSelected(anIt.Value(), Standard_False);
1161 //**************************************************************
1162 XGUI_SelectionActivate* XGUI_Displayer::selectionActivate() const
1164 return myWorkshop->selectionActivate();
1167 //**************************************************************
1168 GeomPlanePtr XGUI_Displayer::getScreenPlane() const
1170 GeomPlanePtr aResult;
1171 Handle(AIS_InteractiveContext) aContext = AISContext();
1172 if (!aContext.IsNull()) {
1173 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1174 Handle(V3d_View) aView;
1175 for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews()) {
1176 aView = aViewer->ActiveView();
1179 if (!aView.IsNull()) {
1180 double aEyeX, aEyeY, aEyeZ;
1181 aView->Eye(aEyeX, aEyeY, aEyeZ);
1183 double aProjX, aProjY, aProjZ;
1184 aView->Proj(aProjX, aProjY, aProjZ);
1186 GeomPointPtr aPnt = GeomPointPtr(new GeomAPI_Pnt(aEyeX, aEyeY, aEyeZ));
1187 GeomDirPtr aDir = GeomDirPtr(new GeomAPI_Dir(aProjX, aProjY, aProjZ));
1189 aResult = GeomPlanePtr(new GeomAPI_Pln(aPnt, aDir));
1195 double XGUI_Displayer::getViewScale() const
1197 Handle(AIS_InteractiveContext) aContext = AISContext();
1198 if (!aContext.IsNull()) {
1199 Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1200 Handle(V3d_View) aView;
1201 for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews()) {
1202 aView = aViewer->ActiveView();
1205 return aView->Camera()->Scale();