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 SelectionPlace& thePlace) const
29 QList<ModuleBase_ViewerPrs> aPresentations;
33 getSelectedInBrowser(aPresentations);
36 getSelectedInViewer(aPresentations);
39 getSelectedInViewer(aPresentations);
40 getSelectedInBrowser(aPresentations);
43 return aPresentations;
46 Handle(AIS_InteractiveObject) XGUI_Selection::getIO(const ModuleBase_ViewerPrs& thePrs)
48 Handle(AIS_InteractiveObject) anIO = thePrs.interactive();
50 Handle(SelectMgr_EntityOwner) anOwner = thePrs.owner();
51 if (!anOwner.IsNull())
52 anIO = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
54 if (anIO.IsNull() && thePrs.object()) {
55 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
56 AISObjectPtr anAISObject = aDisplayer->getAISObject(thePrs.object());
57 if (anAISObject.get())
58 anIO = anAISObject->impl<Handle(AIS_InteractiveObject)>();
64 void XGUI_Selection::getSelectedInViewer(QList<ModuleBase_ViewerPrs>& thePresentations) const
66 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
67 if (aContext.IsNull())
70 if (aContext->HasOpenedContext()) {
71 QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
72 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
73 ModuleBase_ViewerPrs aPrs;
74 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
76 if (aSelectedIds.contains((long)anOwner.Access()))
78 aSelectedIds.append((long)anOwner.Access());
80 fillPresentation(aPrs, anOwner);
82 if (!thePresentations.contains(aPrs)) // TODO: check whether the presentation in a list
83 thePresentations.append(aPrs);
88 void XGUI_Selection::getSelectedInBrowser(QList<ModuleBase_ViewerPrs>& thePresentations) const
90 // collect the objects of the parameter presentation to avoid a repeted objects in the result
91 QObjectPtrList aPresentationObjects;
92 QList<ModuleBase_ViewerPrs>::const_iterator aPrsIt = thePresentations.begin(),
93 aPrsLast = thePresentations.end();
94 for (; aPrsIt != aPrsLast; aPrsIt++) {
95 aPresentationObjects.push_back((*aPrsIt).object());
98 QObjectPtrList anObjects = selectedObjects();
99 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
100 for (; anIt != aLast; anIt++) {
101 ObjectPtr anObject = *anIt;
102 if (anObject.get() != NULL && !aPresentationObjects.contains(anObject)) {
103 thePresentations.append(ModuleBase_ViewerPrs(anObject, TopoDS_Shape(), NULL));
108 void XGUI_Selection::fillPresentation(ModuleBase_ViewerPrs& thePrs,
109 const Handle(SelectMgr_EntityOwner)& theOwner) const
111 thePrs.setOwner(theOwner);
113 Handle(AIS_InteractiveObject) anIO =
114 Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
115 thePrs.setInteractive(anIO);
117 // we should not check the appearance of this feature because there can be some selected shapes
119 Handle(StdSelect_BRepOwner) aBRO = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
120 if( !aBRO.IsNull() ) {
121 // the located method is called in the context to obtain the shape by the SelectedShape() method,
122 // so the shape is located by the same rules
123 TopoDS_Shape aShape = aBRO->Shape().Located (aBRO->Location() * aBRO->Shape().Location());
124 if (!aShape.IsNull())
125 thePrs.setShape(aShape);
128 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
129 ObjectPtr aFeature = aDisplayer->getObject(anIO);
130 thePrs.setObject(aFeature);
133 QList<ModuleBase_ViewerPrs> XGUI_Selection::getHighlighted() const
135 QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
136 QList<ModuleBase_ViewerPrs> aPresentations;
137 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
139 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
140 for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
141 ModuleBase_ViewerPrs aPrs;
142 Handle(AIS_InteractiveObject) anIO = aContext->DetectedInteractive();
143 if (aSelectedIds.contains((long)anIO.Access()))
146 aSelectedIds.append((long)anIO.Access());
147 aPrs.setInteractive(anIO);
149 ObjectPtr aResult = aDisplayer->getObject(anIO);
150 // we should not check the appearance of this feature because there can be some selected shapes
152 aPrs.setObject(aResult);
153 if (aContext->HasOpenedContext()) {
154 TopoDS_Shape aShape = aContext->DetectedShape();
155 if (!aShape.IsNull())
156 aPrs.setShape(aShape);
158 aPresentations.push_back(aPrs);
160 return aPresentations;
163 QObjectPtrList XGUI_Selection::selectedObjects() const
165 return myWorkshop->objectBrowser()->selectedObjects();
168 void XGUI_Selection::setSelectedObjects( const QObjectPtrList& theObjects ) const
170 return myWorkshop->objectBrowser()->setObjectsSelected( theObjects );
173 QObjectPtrList XGUI_Selection::selectedPresentations() const
175 QObjectPtrList aSelectedList;
177 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
178 if (!aContext.IsNull()) {
179 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
180 Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
181 ObjectPtr aResult = myWorkshop->displayer()->getObject(anIO);
183 aSelectedList.append(aResult);
186 return aSelectedList;
189 //**************************************************************
190 QModelIndexList XGUI_Selection::selectedIndexes() const
192 return myWorkshop->objectBrowser()->selectedIndexes();
195 //**************************************************************
196 void XGUI_Selection::selectedAISObjects(AIS_ListOfInteractive& theList) const
198 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
200 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
201 theList.Append(aContext->SelectedInteractive());
204 //**************************************************************
205 ObjectPtr XGUI_Selection::getSelectableObject(const Handle(SelectMgr_EntityOwner)& theOwner) const
209 Handle(SelectMgr_EntityOwner) aEO = theOwner;
211 Handle(AIS_InteractiveObject) anObj =
212 Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
213 anObject = myWorkshop->displayer()->getObject(anObj);
218 //**************************************************************
219 void XGUI_Selection::selectedShapes(NCollection_List<TopoDS_Shape>& theList,
220 std::list<ObjectPtr>& theOwners) const
223 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
224 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
225 TopoDS_Shape aShape = aContext->SelectedShape();
226 if (!aShape.IsNull()) {
227 theList.Append(aShape);
228 Handle(SelectMgr_EntityOwner) aEO = aContext->SelectedOwner();
230 Handle(AIS_InteractiveObject) anObj =
231 Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
232 ObjectPtr anObject = myWorkshop->displayer()->getObject(anObj);
233 theOwners.push_back(anObject);
239 //**************************************************************
240 void XGUI_Selection::selectedOwners(SelectMgr_IndexedMapOfOwner& theSelectedOwners) const
242 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
244 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
245 theSelectedOwners.Add(aContext->SelectedOwner());
249 //**************************************************************
250 void XGUI_Selection::entityOwners(const Handle(AIS_InteractiveObject)& theObject,
251 SelectMgr_IndexedMapOfOwner& theOwners) const
253 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
255 if (theObject.IsNull() || aContext.IsNull())
258 TColStd_ListOfInteger aModes;
259 aContext->ActivatedModes(theObject, aModes);
261 TColStd_ListIteratorOfListOfInteger anIt(aModes);
262 for (; anIt.More(); anIt.Next()) {
263 int aMode = anIt.Value();
264 if (!theObject->HasSelection(aMode))
267 Handle(SelectMgr_Selection) aSelection = theObject->Selection(aMode);
268 for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
269 Handle(SelectMgr_SensitiveEntity) anEntity = aSelection->Sensitive();
270 if (anEntity.IsNull())
272 Handle(SelectMgr_EntityOwner) anOwner =
273 Handle(SelectMgr_EntityOwner)::DownCast(anEntity->BaseSensitive()->OwnerId());
274 if (!anOwner.IsNull())
275 theOwners.Add(anOwner);