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() && aContext->HasOpenedContext()) {
68 QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
69 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
70 ModuleBase_ViewerPrs aPrs;
71 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
73 if (aSelectedIds.contains((long)anOwner.Access()))
75 aSelectedIds.append((long)anOwner.Access());
77 fillPresentation(aPrs, anOwner);
79 if (!thePresentations.contains(aPrs)) // TODO: check whether the presentation in a list
80 thePresentations.append(aPrs);
85 void XGUI_Selection::getSelectedInBrowser(QList<ModuleBase_ViewerPrs>& thePresentations) const
87 // collect the objects of the parameter presentation to avoid a repeted objects in the result
88 QObjectPtrList aPresentationObjects;
89 QList<ModuleBase_ViewerPrs>::const_iterator aPrsIt = thePresentations.begin(),
90 aPrsLast = thePresentations.end();
91 for (; aPrsIt != aPrsLast; aPrsIt++) {
92 aPresentationObjects.push_back((*aPrsIt).object());
95 QObjectPtrList anObjects = selectedObjects();
96 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
97 for (; anIt != aLast; anIt++) {
98 ObjectPtr anObject = *anIt;
99 if (anObject.get() != NULL && !aPresentationObjects.contains(anObject)) {
100 thePresentations.append(ModuleBase_ViewerPrs(anObject, TopoDS_Shape(), NULL));
105 void XGUI_Selection::fillPresentation(ModuleBase_ViewerPrs& thePrs,
106 const Handle(SelectMgr_EntityOwner)& theOwner) const
108 thePrs.setOwner(theOwner);
110 Handle(AIS_InteractiveObject) anIO =
111 Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
112 thePrs.setInteractive(anIO);
114 // we should not check the appearance of this feature because there can be some selected shapes
116 Handle(StdSelect_BRepOwner) aBRO = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
117 if( !aBRO.IsNull() && aBRO->HasShape() ) {
118 // the located method is called in the context to obtain the shape by the SelectedShape() method,
119 // so the shape is located by the same rules
120 TopoDS_Shape aShape = aBRO->Shape().Located (aBRO->Location() * aBRO->Shape().Location());
121 if (!aShape.IsNull())
122 thePrs.setShape(aShape);
125 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
126 ObjectPtr aFeature = aDisplayer->getObject(anIO);
127 thePrs.setObject(aFeature);
130 QList<ModuleBase_ViewerPrs> XGUI_Selection::getHighlighted() const
132 QList<ModuleBase_ViewerPrs> aPresentations;
133 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
134 if (aContext.IsNull())
135 return aPresentations;
137 QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
138 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
139 for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
140 ModuleBase_ViewerPrs aPrs;
141 Handle(AIS_InteractiveObject) anIO = aContext->DetectedInteractive();
142 if (aSelectedIds.contains((long)anIO.Access()))
145 aSelectedIds.append((long)anIO.Access());
146 aPrs.setInteractive(anIO);
148 ObjectPtr aResult = aDisplayer->getObject(anIO);
149 // we should not check the appearance of this feature because there can be some selected shapes
151 aPrs.setObject(aResult);
152 if (aContext->HasOpenedContext()) {
153 TopoDS_Shape aShape = aContext->DetectedShape();
154 if (!aShape.IsNull())
155 aPrs.setShape(aShape);
157 aPresentations.push_back(aPrs);
159 return aPresentations;
162 QObjectPtrList XGUI_Selection::selectedObjects() const
164 return myWorkshop->objectBrowser()->selectedObjects();
167 void XGUI_Selection::setSelectedObjects( const QObjectPtrList& theObjects ) const
169 return myWorkshop->objectBrowser()->setObjectsSelected( theObjects );
172 QObjectPtrList XGUI_Selection::selectedPresentations() const
174 QObjectPtrList aSelectedList;
176 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
177 if (!aContext.IsNull()) {
178 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
179 Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
180 ObjectPtr aResult = myWorkshop->displayer()->getObject(anIO);
182 aSelectedList.append(aResult);
185 return aSelectedList;
188 //**************************************************************
189 QModelIndexList XGUI_Selection::selectedIndexes() const
191 return myWorkshop->objectBrowser()->selectedIndexes();
194 //**************************************************************
195 void XGUI_Selection::selectedAISObjects(AIS_ListOfInteractive& theList) const
199 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
200 if (!aContext.IsNull()) {
201 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
202 theList.Append(aContext->SelectedInteractive());
206 //**************************************************************
207 ObjectPtr XGUI_Selection::getSelectableObject(const Handle(SelectMgr_EntityOwner)& theOwner) const
211 Handle(SelectMgr_EntityOwner) aEO = theOwner;
213 Handle(AIS_InteractiveObject) anObj =
214 Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
215 anObject = myWorkshop->displayer()->getObject(anObj);
220 //**************************************************************
221 void XGUI_Selection::selectedShapes(NCollection_List<TopoDS_Shape>& theList,
222 std::list<ObjectPtr>& theOwners) const
225 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
226 if (aContext.IsNull())
229 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
230 TopoDS_Shape aShape = aContext->SelectedShape();
231 if (!aShape.IsNull()) {
232 theList.Append(aShape);
233 Handle(SelectMgr_EntityOwner) aEO = aContext->SelectedOwner();
235 Handle(AIS_InteractiveObject) anObj =
236 Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
237 ObjectPtr anObject = myWorkshop->displayer()->getObject(anObj);
238 theOwners.push_back(anObject);
244 //**************************************************************
245 void XGUI_Selection::selectedOwners(SelectMgr_IndexedMapOfOwner& theSelectedOwners) const
247 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
248 if (!aContext.IsNull()) {
249 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
250 theSelectedOwners.Add(aContext->SelectedOwner());
255 //**************************************************************
256 void XGUI_Selection::entityOwners(const Handle(AIS_InteractiveObject)& theObject,
257 SelectMgr_IndexedMapOfOwner& theOwners) const
259 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
260 if (aContext.IsNull() || theObject.IsNull())
263 TColStd_ListOfInteger aModes;
264 aContext->ActivatedModes(theObject, aModes);
266 TColStd_ListIteratorOfListOfInteger anIt(aModes);
267 for (; anIt.More(); anIt.Next()) {
268 int aMode = anIt.Value();
269 if (!theObject->HasSelection(aMode))
272 Handle(SelectMgr_Selection) aSelection = theObject->Selection(aMode);
273 for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
274 Handle(SelectMgr_SensitiveEntity) anEntity = aSelection->Sensitive();
275 if (anEntity.IsNull())
277 Handle(SelectMgr_EntityOwner) anOwner =
278 Handle(SelectMgr_EntityOwner)::DownCast(anEntity->BaseSensitive()->OwnerId());
279 if (!anOwner.IsNull())
280 theOwners.Add(anOwner);