1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: XGUI_Selection.cpp
4 // Created: 8 July 2014
5 // Author: Vitaly SMETANNIKOV
7 #include "XGUI_Selection.h"
8 #include "XGUI_Workshop.h"
9 #include "XGUI_Displayer.h"
10 #include "XGUI_ViewerProxy.h"
11 #include "XGUI_ObjectsBrowser.h"
13 #include <ModelAPI_Feature.h>
15 #include <AIS_InteractiveContext.hxx>
17 #include <TColStd_ListIteratorOfListOfInteger.hxx>
18 #include <StdSelect_BRepOwner.hxx>
22 XGUI_Selection::XGUI_Selection(XGUI_Workshop* theWorkshop)
23 : myWorkshop(theWorkshop)
27 QList<ModuleBase_ViewerPrs> XGUI_Selection::getSelected() const
29 QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
31 QList<ModuleBase_ViewerPrs> aPresentations;
32 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
34 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
35 if (aContext.IsNull())
36 return aPresentations;
38 if (aContext->HasOpenedContext()) {
39 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
40 ModuleBase_ViewerPrs aPrs;
41 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
43 if (aSelectedIds.contains((long)anOwner.Access()))
45 aSelectedIds.append((long)anOwner.Access());
47 fillPresentation(aPrs, anOwner);
49 aPresentations.append(aPrs);
53 for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent()) {
54 ModuleBase_ViewerPrs aPrs;
55 Handle(AIS_InteractiveObject) anIO = aContext->Current();
56 if (aSelectedIds.contains((long)anIO.Access()))
59 aSelectedIds.append((long)anIO.Access());
60 aPrs.setInteractive(anIO);
62 ObjectPtr aFeature = aDisplayer->getObject(anIO);
63 aPrs.setFeature(aFeature);
64 aPresentations.append(aPrs);
67 return aPresentations;
70 void XGUI_Selection::fillPresentation(ModuleBase_ViewerPrs& thePrs,
71 const Handle(SelectMgr_EntityOwner)& theOwner) const
73 thePrs.setOwner(theOwner);
75 Handle(AIS_InteractiveObject) anIO =
76 Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
77 thePrs.setInteractive(anIO);
79 // we should not check the appearance of this feature because there can be some selected shapes
81 Handle(StdSelect_BRepOwner) aBRO = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
82 if( !aBRO.IsNull() ) {
83 // the located method is called in the context to obtain the shape by the SelectedShape() method,
84 // so the shape is located by the same rules
85 TopoDS_Shape aShape = aBRO->Shape().Located (aBRO->Location() * aBRO->Shape().Location());
87 thePrs.setShape(aShape);
90 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
91 ObjectPtr aFeature = aDisplayer->getObject(anIO);
92 thePrs.setObject(aFeature);
95 QList<ModuleBase_ViewerPrs> XGUI_Selection::getHighlighted() const
97 QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
98 QList<ModuleBase_ViewerPrs> aPresentations;
99 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
101 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
102 for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
103 ModuleBase_ViewerPrs aPrs;
104 Handle(AIS_InteractiveObject) anIO = aContext->DetectedInteractive();
105 if (aSelectedIds.contains((long)anIO.Access()))
108 aSelectedIds.append((long)anIO.Access());
109 aPrs.setInteractive(anIO);
111 ObjectPtr aResult = aDisplayer->getObject(anIO);
112 // we should not check the appearance of this feature because there can be some selected shapes
114 aPrs.setObject(aResult);
115 if (aContext->HasOpenedContext()) {
116 TopoDS_Shape aShape = aContext->DetectedShape();
117 if (!aShape.IsNull())
118 aPrs.setShape(aShape);
120 aPresentations.push_back(aPrs);
122 return aPresentations;
125 QObjectPtrList XGUI_Selection::selectedObjects() const
127 return myWorkshop->objectBrowser()->selectedObjects();
130 QObjectPtrList XGUI_Selection::selectedPresentations() const
132 QObjectPtrList aSelectedList;
134 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
135 if (!aContext.IsNull()) {
136 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
137 Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
138 ObjectPtr aResult = myWorkshop->displayer()->getObject(anIO);
140 aSelectedList.append(aResult);
143 return aSelectedList;
146 //**************************************************************
147 QModelIndexList XGUI_Selection::selectedIndexes() const
149 return myWorkshop->objectBrowser()->selectedIndexes();
152 //**************************************************************
153 void XGUI_Selection::selectedAISObjects(AIS_ListOfInteractive& theList) const
155 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
157 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
158 theList.Append(aContext->SelectedInteractive());
161 //**************************************************************
162 ObjectPtr XGUI_Selection::getSelectableObject(const Handle(SelectMgr_EntityOwner)& theOwner) const
166 Handle(SelectMgr_EntityOwner) aEO = theOwner;
168 Handle(AIS_InteractiveObject) anObj =
169 Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
170 anObject = myWorkshop->displayer()->getObject(anObj);
175 //**************************************************************
176 void XGUI_Selection::selectedShapes(NCollection_List<TopoDS_Shape>& theList,
177 std::list<ObjectPtr>& theOwners) const
180 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
181 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
182 TopoDS_Shape aShape = aContext->SelectedShape();
183 if (!aShape.IsNull()) {
184 theList.Append(aShape);
185 Handle(SelectMgr_EntityOwner) aEO = aContext->SelectedOwner();
187 Handle(AIS_InteractiveObject) anObj =
188 Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
189 ObjectPtr anObject = myWorkshop->displayer()->getObject(anObj);
190 theOwners.push_back(anObject);
196 //**************************************************************
197 void XGUI_Selection::selectedOwners(SelectMgr_IndexedMapOfOwner& theSelectedOwners) const
199 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
201 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
202 theSelectedOwners.Add(aContext->SelectedOwner());
206 //**************************************************************
207 void XGUI_Selection::entityOwners(const Handle(AIS_InteractiveObject)& theObject,
208 SelectMgr_IndexedMapOfOwner& theOwners) const
210 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
212 if (theObject.IsNull() || aContext.IsNull())
215 TColStd_ListOfInteger aModes;
216 aContext->ActivatedModes(theObject, aModes);
218 TColStd_ListIteratorOfListOfInteger anIt(aModes);
219 for (; anIt.More(); anIt.Next()) {
220 int aMode = anIt.Value();
221 if (!theObject->HasSelection(aMode))
224 Handle(SelectMgr_Selection) aSelection = theObject->Selection(aMode);
225 for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
226 Handle(SelectMgr_SensitiveEntity) anEntity = aSelection->Sensitive();
227 if (anEntity.IsNull())
229 Handle(SelectMgr_EntityOwner) anOwner =
230 Handle(SelectMgr_EntityOwner)::DownCast(anEntity->BaseSensitive()->OwnerId());
231 if (!anOwner.IsNull())
232 theOwners.Add(anOwner);