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 <SelectMgr_Selection.hxx>
18 #include <SelectMgr_EntityOwner.hxx>
20 #include <SelectBasics_SensitiveEntity.hxx>
21 #include <TColStd_ListIteratorOfListOfInteger.hxx>
22 #include <StdSelect_BRepOwner.hxx>
26 XGUI_Selection::XGUI_Selection(XGUI_Workshop* theWorkshop)
27 : myWorkshop(theWorkshop)
31 QList<ModuleBase_ViewerPrs> XGUI_Selection::getSelected() const
33 QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
35 QList<ModuleBase_ViewerPrs> aPresentations;
36 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
38 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
39 if (aContext.IsNull())
40 return aPresentations;
42 if (aContext->HasOpenedContext()) {
43 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
44 ModuleBase_ViewerPrs aPrs;
45 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
47 if (aSelectedIds.contains((long)anOwner.Access()))
49 aSelectedIds.append((long)anOwner.Access());
51 fillPresentation(aPrs, anOwner);
53 aPresentations.append(aPrs);
57 for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent()) {
58 ModuleBase_ViewerPrs aPrs;
59 Handle(AIS_InteractiveObject) anIO = aContext->Current();
60 if (aSelectedIds.contains((long)anIO.Access()))
63 aSelectedIds.append((long)anIO.Access());
64 aPrs.setInteractive(anIO);
66 ObjectPtr aFeature = aDisplayer->getObject(anIO);
67 aPrs.setFeature(aFeature);
68 aPresentations.append(aPrs);
71 return aPresentations;
74 void XGUI_Selection::fillPresentation(ModuleBase_ViewerPrs& thePrs,
75 const Handle(SelectMgr_EntityOwner)& theOwner) const
77 thePrs.setOwner(theOwner);
79 Handle(AIS_InteractiveObject) anIO =
80 Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
81 thePrs.setInteractive(anIO);
83 // we should not check the appearance of this feature because there can be some selected shapes
85 Handle(StdSelect_BRepOwner) aBRO = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
86 if( !aBRO.IsNull() ) {
87 // the located method is called in the context to obtain the shape by the SelectedShape() method,
88 // so the shape is located by the same rules
89 TopoDS_Shape aShape = aBRO->Shape().Located (aBRO->Location() * aBRO->Shape().Location());
91 thePrs.setShape(aShape);
94 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
95 ObjectPtr aFeature = aDisplayer->getObject(anIO);
96 thePrs.setObject(aFeature);
99 QList<ModuleBase_ViewerPrs> XGUI_Selection::getHighlighted() const
101 QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
102 QList<ModuleBase_ViewerPrs> aPresentations;
103 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
105 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
106 for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
107 ModuleBase_ViewerPrs aPrs;
108 Handle(AIS_InteractiveObject) anIO = aContext->DetectedInteractive();
109 if (aSelectedIds.contains((long)anIO.Access()))
112 aSelectedIds.append((long)anIO.Access());
113 aPrs.setInteractive(anIO);
115 ObjectPtr aResult = aDisplayer->getObject(anIO);
116 // we should not check the appearance of this feature because there can be some selected shapes
118 aPrs.setObject(aResult);
119 if (aContext->HasOpenedContext()) {
120 TopoDS_Shape aShape = aContext->DetectedShape();
121 if (!aShape.IsNull())
122 aPrs.setShape(aShape);
124 aPresentations.push_back(aPrs);
126 return aPresentations;
129 QObjectPtrList XGUI_Selection::selectedObjects() const
131 return myWorkshop->objectBrowser()->selectedObjects();
134 QObjectPtrList XGUI_Selection::selectedPresentations() const
136 QObjectPtrList aSelectedList;
138 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
139 if (!aContext.IsNull()) {
140 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
141 Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
142 ObjectPtr aResult = myWorkshop->displayer()->getObject(anIO);
144 aSelectedList.append(aResult);
147 return aSelectedList;
150 //**************************************************************
151 QModelIndexList XGUI_Selection::selectedIndexes() const
153 return myWorkshop->objectBrowser()->selectedIndexes();
156 //**************************************************************
157 void XGUI_Selection::selectedAISObjects(AIS_ListOfInteractive& theList) const
159 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
161 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
162 theList.Append(aContext->SelectedInteractive());
165 //**************************************************************
166 ObjectPtr XGUI_Selection::getSelectableObject(const Handle(SelectMgr_EntityOwner)& theOwner) const
170 Handle(SelectMgr_EntityOwner) aEO = theOwner;
172 Handle(AIS_InteractiveObject) anObj =
173 Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
174 anObject = myWorkshop->displayer()->getObject(anObj);
179 //**************************************************************
180 void XGUI_Selection::selectedShapes(NCollection_List<TopoDS_Shape>& theList,
181 std::list<ObjectPtr>& theOwners) const
184 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
185 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
186 TopoDS_Shape aShape = aContext->SelectedShape();
187 if (!aShape.IsNull()) {
188 theList.Append(aShape);
189 Handle(SelectMgr_EntityOwner) aEO = aContext->SelectedOwner();
191 Handle(AIS_InteractiveObject) anObj =
192 Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
193 ObjectPtr anObject = myWorkshop->displayer()->getObject(anObj);
194 theOwners.push_back(anObject);
200 //**************************************************************
201 void XGUI_Selection::selectedOwners(SelectMgr_IndexedMapOfOwner& theSelectedOwners) const
203 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
205 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
206 theSelectedOwners.Add(aContext->SelectedOwner());
210 //**************************************************************
211 void XGUI_Selection::entityOwners(const Handle(AIS_InteractiveObject)& theObject,
212 SelectMgr_IndexedMapOfOwner& theOwners) const
214 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
216 if (theObject.IsNull() || aContext.IsNull())
219 TColStd_ListOfInteger aModes;
220 aContext->ActivatedModes(theObject, aModes);
222 TColStd_ListIteratorOfListOfInteger anIt(aModes);
223 for (; anIt.More(); anIt.Next()) {
224 int aMode = anIt.Value();
225 if (!theObject->HasSelection(aMode))
228 Handle(SelectMgr_Selection) aSelection = theObject->Selection(aMode);
229 for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
230 Handle(SelectBasics_SensitiveEntity) anEntity = aSelection->Sensitive();
231 if (anEntity.IsNull())
233 Handle(SelectMgr_EntityOwner) anOwner =
234 Handle(SelectMgr_EntityOwner)::DownCast(anEntity->OwnerId());
235 if (!anOwner.IsNull())
236 theOwners.Add(anOwner);