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 void XGUI_Selection::getSelectedInViewer(QList<ModuleBase_ViewerPrs>& thePresentations) const
48 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
49 if (aContext.IsNull())
52 if (aContext->HasOpenedContext()) {
53 QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
54 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
55 ModuleBase_ViewerPrs aPrs;
56 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
58 if (aSelectedIds.contains((long)anOwner.Access()))
60 aSelectedIds.append((long)anOwner.Access());
62 fillPresentation(aPrs, anOwner);
64 if (!thePresentations.contains(aPrs)) // TODO: check whether the presentation in a list
65 thePresentations.append(aPrs);
70 void XGUI_Selection::getSelectedInBrowser(QList<ModuleBase_ViewerPrs>& thePresentations) const
72 // collect the objects of the parameter presentation to avoid a repeted objects in the result
73 QObjectPtrList aPresentationObjects;
74 QList<ModuleBase_ViewerPrs>::const_iterator aPrsIt = thePresentations.begin(),
75 aPrsLast = thePresentations.end();
76 for (; aPrsIt != aPrsLast; aPrsIt++) {
77 aPresentationObjects.push_back((*aPrsIt).object());
80 QObjectPtrList anObjects = selectedObjects();
81 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
82 for (; anIt != aLast; anIt++) {
83 ObjectPtr anObject = *anIt;
84 if (anObject.get() != NULL && !aPresentationObjects.contains(anObject)) {
85 thePresentations.append(ModuleBase_ViewerPrs(anObject, TopoDS_Shape(), NULL));
90 void XGUI_Selection::fillPresentation(ModuleBase_ViewerPrs& thePrs,
91 const Handle(SelectMgr_EntityOwner)& theOwner) const
93 thePrs.setOwner(theOwner);
95 Handle(AIS_InteractiveObject) anIO =
96 Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
97 thePrs.setInteractive(anIO);
99 // we should not check the appearance of this feature because there can be some selected shapes
101 Handle(StdSelect_BRepOwner) aBRO = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
102 if( !aBRO.IsNull() ) {
103 // the located method is called in the context to obtain the shape by the SelectedShape() method,
104 // so the shape is located by the same rules
105 TopoDS_Shape aShape = aBRO->Shape().Located (aBRO->Location() * aBRO->Shape().Location());
106 if (!aShape.IsNull())
107 thePrs.setShape(aShape);
110 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
111 ObjectPtr aFeature = aDisplayer->getObject(anIO);
112 thePrs.setObject(aFeature);
115 QList<ModuleBase_ViewerPrs> XGUI_Selection::getHighlighted() const
117 QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
118 QList<ModuleBase_ViewerPrs> aPresentations;
119 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
121 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
122 for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
123 ModuleBase_ViewerPrs aPrs;
124 Handle(AIS_InteractiveObject) anIO = aContext->DetectedInteractive();
125 if (aSelectedIds.contains((long)anIO.Access()))
128 aSelectedIds.append((long)anIO.Access());
129 aPrs.setInteractive(anIO);
131 ObjectPtr aResult = aDisplayer->getObject(anIO);
132 // we should not check the appearance of this feature because there can be some selected shapes
134 aPrs.setObject(aResult);
135 if (aContext->HasOpenedContext()) {
136 TopoDS_Shape aShape = aContext->DetectedShape();
137 if (!aShape.IsNull())
138 aPrs.setShape(aShape);
140 aPresentations.push_back(aPrs);
142 return aPresentations;
145 QObjectPtrList XGUI_Selection::selectedObjects() const
147 return myWorkshop->objectBrowser()->selectedObjects();
150 QObjectPtrList XGUI_Selection::selectedPresentations() const
152 QObjectPtrList aSelectedList;
154 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
155 if (!aContext.IsNull()) {
156 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
157 Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
158 ObjectPtr aResult = myWorkshop->displayer()->getObject(anIO);
160 aSelectedList.append(aResult);
163 return aSelectedList;
166 //**************************************************************
167 QModelIndexList XGUI_Selection::selectedIndexes() const
169 return myWorkshop->objectBrowser()->selectedIndexes();
172 //**************************************************************
173 void XGUI_Selection::selectedAISObjects(AIS_ListOfInteractive& theList) const
175 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
177 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
178 theList.Append(aContext->SelectedInteractive());
181 //**************************************************************
182 ObjectPtr XGUI_Selection::getSelectableObject(const Handle(SelectMgr_EntityOwner)& theOwner) const
186 Handle(SelectMgr_EntityOwner) aEO = theOwner;
188 Handle(AIS_InteractiveObject) anObj =
189 Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
190 anObject = myWorkshop->displayer()->getObject(anObj);
195 //**************************************************************
196 void XGUI_Selection::selectedShapes(NCollection_List<TopoDS_Shape>& theList,
197 std::list<ObjectPtr>& theOwners) const
200 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
201 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
202 TopoDS_Shape aShape = aContext->SelectedShape();
203 if (!aShape.IsNull()) {
204 theList.Append(aShape);
205 Handle(SelectMgr_EntityOwner) aEO = aContext->SelectedOwner();
207 Handle(AIS_InteractiveObject) anObj =
208 Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
209 ObjectPtr anObject = myWorkshop->displayer()->getObject(anObj);
210 theOwners.push_back(anObject);
216 //**************************************************************
217 void XGUI_Selection::selectedOwners(SelectMgr_IndexedMapOfOwner& theSelectedOwners) const
219 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
221 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
222 theSelectedOwners.Add(aContext->SelectedOwner());
226 //**************************************************************
227 void XGUI_Selection::entityOwners(const Handle(AIS_InteractiveObject)& theObject,
228 SelectMgr_IndexedMapOfOwner& theOwners) const
230 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
232 if (theObject.IsNull() || aContext.IsNull())
235 TColStd_ListOfInteger aModes;
236 aContext->ActivatedModes(theObject, aModes);
238 TColStd_ListIteratorOfListOfInteger anIt(aModes);
239 for (; anIt.More(); anIt.Next()) {
240 int aMode = anIt.Value();
241 if (!theObject->HasSelection(aMode))
244 Handle(SelectMgr_Selection) aSelection = theObject->Selection(aMode);
245 for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
246 Handle(SelectMgr_SensitiveEntity) anEntity = aSelection->Sensitive();
247 if (anEntity.IsNull())
249 Handle(SelectMgr_EntityOwner) anOwner =
250 Handle(SelectMgr_EntityOwner)::DownCast(anEntity->BaseSensitive()->OwnerId());
251 if (!anOwner.IsNull())
252 theOwners.Add(anOwner);