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>
16 #include <AIS_Axis.hxx>
17 #include <AIS_Point.hxx>
18 #include <Geom_Line.hxx>
19 #include <BRep_Builder.hxx>
20 #include <TopoDS_Edge.hxx>
21 #include <Geom_Point.hxx>
22 #include <Geom_TrimmedCurve.hxx>
23 #include <Prs3d_DatumAspect.hxx>
25 #include <TColStd_ListIteratorOfListOfInteger.hxx>
26 #include <StdSelect_BRepOwner.hxx>
30 XGUI_Selection::XGUI_Selection(XGUI_Workshop* theWorkshop)
31 : myWorkshop(theWorkshop)
35 QList<ModuleBase_ViewerPrs> XGUI_Selection::getSelected(const SelectionPlace& thePlace) const
37 QList<ModuleBase_ViewerPrs> aPresentations;
41 getSelectedInBrowser(aPresentations);
44 getSelectedInViewer(aPresentations);
47 getSelectedInViewer(aPresentations);
48 getSelectedInBrowser(aPresentations);
51 return aPresentations;
54 Handle(AIS_InteractiveObject) XGUI_Selection::getIO(const ModuleBase_ViewerPrs& thePrs)
56 Handle(AIS_InteractiveObject) anIO = thePrs.interactive();
58 Handle(SelectMgr_EntityOwner) anOwner = thePrs.owner();
59 if (!anOwner.IsNull())
60 anIO = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
62 if (anIO.IsNull() && thePrs.object()) {
63 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
64 AISObjectPtr anAISObject = aDisplayer->getAISObject(thePrs.object());
65 if (anAISObject.get())
66 anIO = anAISObject->impl<Handle(AIS_InteractiveObject)>();
72 void XGUI_Selection::getSelectedInViewer(QList<ModuleBase_ViewerPrs>& thePresentations) const
74 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
75 if (!aContext.IsNull() && aContext->HasOpenedContext()) {
76 QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
77 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
78 ModuleBase_ViewerPrs aPrs;
79 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
81 if (aSelectedIds.contains((long)anOwner.Access()))
83 aSelectedIds.append((long)anOwner.Access());
85 fillPresentation(aPrs, anOwner);
87 if (!thePresentations.contains(aPrs)) // TODO: check whether the presentation in a list
88 thePresentations.append(aPrs);
93 void XGUI_Selection::getSelectedInBrowser(QList<ModuleBase_ViewerPrs>& thePresentations) const
95 // collect the objects of the parameter presentation to avoid a repeted objects in the result
96 QObjectPtrList aPresentationObjects;
97 QList<ModuleBase_ViewerPrs>::const_iterator aPrsIt = thePresentations.begin(),
98 aPrsLast = thePresentations.end();
99 for (; aPrsIt != aPrsLast; aPrsIt++) {
100 aPresentationObjects.push_back((*aPrsIt).object());
103 QObjectPtrList anObjects = selectedObjects();
104 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
105 for (; anIt != aLast; anIt++) {
106 ObjectPtr anObject = *anIt;
107 if (anObject.get() != NULL && !aPresentationObjects.contains(anObject)) {
108 thePresentations.append(ModuleBase_ViewerPrs(anObject, TopoDS_Shape(), NULL));
113 void XGUI_Selection::fillPresentation(ModuleBase_ViewerPrs& thePrs,
114 const Handle(SelectMgr_EntityOwner)& theOwner) const
116 thePrs.setOwner(theOwner);
118 Handle(AIS_InteractiveObject) anIO =
119 Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
120 thePrs.setInteractive(anIO);
122 // we should not check the appearance of this feature because there can be some selected shapes
124 Handle(StdSelect_BRepOwner) aBRO = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
125 if( !aBRO.IsNull() && aBRO->HasShape() ) {
126 // the located method is called in the context to obtain the shape by the SelectedShape() method,
127 // so the shape is located by the same rules
128 TopoDS_Shape aShape = aBRO->Shape().Located (aBRO->Location() * aBRO->Shape().Location());
129 if (!aShape.IsNull())
130 thePrs.setShape(aShape);
132 // Fill by trihedron shapes
133 Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(anIO);
134 if (!aAxis.IsNull()) {
135 // an Axis from Trihedron
136 Handle(Geom_Line) aLine = aAxis->Component();
137 Handle(Prs3d_DatumAspect) DA = aAxis->Attributes()->DatumAspect();
138 Handle(Geom_TrimmedCurve) aTLine = new Geom_TrimmedCurve(aLine, 0, DA->FirstAxisLength());
140 BRep_Builder aBuilder;
142 aBuilder.MakeEdge(aEdge, aTLine, Precision::Confusion());
144 thePrs.setShape(aEdge);
146 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(anIO);
147 if (!aPoint.IsNull()) {
148 // A point from trihedron
149 Handle(Geom_Point) aPnt = aPoint->Component();
150 BRep_Builder aBuilder;
151 TopoDS_Vertex aVertex;
152 aBuilder.MakeVertex(aVertex, aPnt->Pnt(), Precision::Confusion());
153 if (!aVertex.IsNull())
154 thePrs.setShape(aVertex);
159 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
160 ObjectPtr aFeature = aDisplayer->getObject(anIO);
161 thePrs.setObject(aFeature);
164 QList<ModuleBase_ViewerPrs> XGUI_Selection::getHighlighted() const
166 QList<ModuleBase_ViewerPrs> aPresentations;
167 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
168 if (aContext.IsNull())
169 return aPresentations;
171 QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
172 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
173 for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
174 ModuleBase_ViewerPrs aPrs;
175 Handle(AIS_InteractiveObject) anIO = aContext->DetectedInteractive();
176 if (aSelectedIds.contains((long)anIO.Access()))
179 aSelectedIds.append((long)anIO.Access());
180 aPrs.setInteractive(anIO);
182 ObjectPtr aResult = aDisplayer->getObject(anIO);
183 // we should not check the appearance of this feature because there can be some selected shapes
185 aPrs.setObject(aResult);
186 if (aContext->HasOpenedContext()) {
187 TopoDS_Shape aShape = aContext->DetectedShape();
188 if (!aShape.IsNull())
189 aPrs.setShape(aShape);
191 aPresentations.push_back(aPrs);
193 return aPresentations;
196 QObjectPtrList XGUI_Selection::selectedObjects() const
198 return myWorkshop->objectBrowser()->selectedObjects();
201 void XGUI_Selection::setSelectedObjects( const QObjectPtrList& theObjects ) const
203 return myWorkshop->objectBrowser()->setObjectsSelected( theObjects );
206 QObjectPtrList XGUI_Selection::selectedPresentations() const
208 QObjectPtrList aSelectedList;
210 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
211 if (!aContext.IsNull()) {
212 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
213 Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
214 ObjectPtr aResult = myWorkshop->displayer()->getObject(anIO);
216 aSelectedList.append(aResult);
219 return aSelectedList;
222 //**************************************************************
223 QModelIndexList XGUI_Selection::selectedIndexes() const
225 return myWorkshop->objectBrowser()->selectedIndexes();
228 //**************************************************************
229 void XGUI_Selection::selectedAISObjects(AIS_ListOfInteractive& theList) const
233 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
234 if (!aContext.IsNull()) {
235 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
236 theList.Append(aContext->SelectedInteractive());
240 //**************************************************************
241 ObjectPtr XGUI_Selection::getSelectableObject(const Handle(SelectMgr_EntityOwner)& theOwner) const
245 Handle(SelectMgr_EntityOwner) aEO = theOwner;
247 Handle(AIS_InteractiveObject) anObj =
248 Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
249 anObject = myWorkshop->displayer()->getObject(anObj);
254 //**************************************************************
255 void XGUI_Selection::selectedShapes(NCollection_List<TopoDS_Shape>& theList,
256 std::list<ObjectPtr>& theOwners) const
259 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
260 if (aContext.IsNull())
263 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
264 TopoDS_Shape aShape = aContext->SelectedShape();
265 if (!aShape.IsNull()) {
266 theList.Append(aShape);
267 Handle(SelectMgr_EntityOwner) aEO = aContext->SelectedOwner();
269 Handle(AIS_InteractiveObject) anObj =
270 Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
271 ObjectPtr anObject = myWorkshop->displayer()->getObject(anObj);
272 theOwners.push_back(anObject);
278 //**************************************************************
279 void XGUI_Selection::selectedOwners(SelectMgr_IndexedMapOfOwner& theSelectedOwners) const
281 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
282 if (!aContext.IsNull()) {
283 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
284 theSelectedOwners.Add(aContext->SelectedOwner());
289 //**************************************************************
290 void XGUI_Selection::entityOwners(const Handle(AIS_InteractiveObject)& theObject,
291 SelectMgr_IndexedMapOfOwner& theOwners) const
293 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
294 if (aContext.IsNull() || theObject.IsNull())
297 TColStd_ListOfInteger aModes;
298 aContext->ActivatedModes(theObject, aModes);
300 TColStd_ListIteratorOfListOfInteger anIt(aModes);
301 for (; anIt.More(); anIt.Next()) {
302 int aMode = anIt.Value();
303 if (!theObject->HasSelection(aMode))
306 Handle(SelectMgr_Selection) aSelection = theObject->Selection(aMode);
307 for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
308 Handle(SelectMgr_SensitiveEntity) anEntity = aSelection->Sensitive();
309 if (anEntity.IsNull())
311 Handle(SelectMgr_EntityOwner) anOwner =
312 Handle(SelectMgr_EntityOwner)::DownCast(anEntity->BaseSensitive()->OwnerId());
313 if (!anOwner.IsNull())
314 theOwners.Add(anOwner);