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 "ModuleBase_ResultPrs.h"
15 #include <ModelAPI_Feature.h>
16 #include <ModelAPI_Tools.h>
18 #include <AIS_InteractiveContext.hxx>
19 #include <AIS_Axis.hxx>
20 #include <AIS_Point.hxx>
21 #include <Geom_Line.hxx>
22 #include <BRep_Builder.hxx>
23 #include <TopoDS_Edge.hxx>
24 #include <Geom_Point.hxx>
25 #include <Geom_TrimmedCurve.hxx>
26 #include <Prs3d_DatumAspect.hxx>
28 #include <TColStd_ListIteratorOfListOfInteger.hxx>
29 #include <StdSelect_BRepOwner.hxx>
33 #define DEBUG_DELIVERY
35 XGUI_Selection::XGUI_Selection(XGUI_Workshop* theWorkshop)
36 : myWorkshop(theWorkshop)
40 QList<ModuleBase_ViewerPrsPtr> XGUI_Selection::getSelected(const SelectionPlace& thePlace) const
42 QList<ModuleBase_ViewerPrsPtr> aPresentations;
46 getSelectedInBrowser(aPresentations);
49 getSelectedInViewer(aPresentations);
52 getSelectedInViewer(aPresentations);
53 getSelectedInBrowser(aPresentations);
56 return aPresentations;
59 Handle(AIS_InteractiveObject) XGUI_Selection::getIO(const ModuleBase_ViewerPrsPtr& thePrs)
61 Handle(AIS_InteractiveObject) anIO = thePrs->interactive();
63 Handle(SelectMgr_EntityOwner) anOwner = thePrs->owner();
64 if (!anOwner.IsNull())
65 anIO = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
67 if (anIO.IsNull() && thePrs->object()) {
68 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
69 AISObjectPtr anAISObject = aDisplayer->getAISObject(thePrs->object());
70 if (anAISObject.get())
71 anIO = anAISObject->impl<Handle(AIS_InteractiveObject)>();
77 void XGUI_Selection::getSelectedInViewer(QList<ModuleBase_ViewerPrsPtr>& thePresentations) const
79 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
80 if (!aContext.IsNull() && aContext->HasOpenedContext()) {
81 QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
82 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
83 ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs());
84 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
86 if (aSelectedIds.contains((long)anOwner.Access()))
88 aSelectedIds.append((long)anOwner.Access());
90 fillPresentation(aPrs, anOwner);
92 if (!thePresentations.contains(aPrs)) // TODO: check whether the presentation in a list
93 thePresentations.append(aPrs);
98 void XGUI_Selection::getSelectedInBrowser(QList<ModuleBase_ViewerPrsPtr>& thePresentations) const
100 // collect the objects of the parameter presentation to avoid a repeted objects in the result
101 QObjectPtrList aPresentationObjects;
102 QList<ModuleBase_ViewerPrsPtr>::const_iterator aPrsIt = thePresentations.begin(),
103 aPrsLast = thePresentations.end();
104 for (; aPrsIt != aPrsLast; aPrsIt++) {
105 aPresentationObjects.push_back((*aPrsIt)->object());
108 QObjectPtrList anObjects = selectedObjects();
109 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
110 for (; anIt != aLast; anIt++) {
111 ObjectPtr anObject = *anIt;
112 if (anObject.get() != NULL && !aPresentationObjects.contains(anObject)) {
113 thePresentations.append(std::shared_ptr<ModuleBase_ViewerPrs>(
114 new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
119 void XGUI_Selection::fillPresentation(ModuleBase_ViewerPrsPtr& thePrs,
120 const Handle(SelectMgr_EntityOwner)& theOwner) const
122 thePrs->setOwner(theOwner);
124 Handle(AIS_InteractiveObject) anIO =
125 Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
126 thePrs->setInteractive(anIO);
128 // we should not check the appearance of this feature because there can be some selected shapes
130 Handle(StdSelect_BRepOwner) aBRO = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
131 if( !aBRO.IsNull() && aBRO->HasShape() ) {
132 // the located method is called in the context to obtain the shape by the SelectedShape() method,
133 // so the shape is located by the same rules
134 TopoDS_Shape aShape = aBRO->Shape().Located (aBRO->Location() * aBRO->Shape().Location());
135 #ifndef DEBUG_DELIVERY
137 aShape = findAxisShape(anIO);
139 if (!aShape.IsNull()) {
140 std::shared_ptr<GeomAPI_Shape> aGeomShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
141 aGeomShape->setImpl(new TopoDS_Shape(aShape));
142 thePrs->setShape(aGeomShape);
145 #ifdef DEBUG_DELIVERY
146 // Fill by trihedron shapes
147 Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(anIO);
148 if (!aAxis.IsNull()) {
149 // an Axis from Trihedron
150 Handle(Geom_Line) aLine = aAxis->Component();
151 Handle(Prs3d_DatumAspect) DA = aAxis->Attributes()->DatumAspect();
152 Handle(Geom_TrimmedCurve) aTLine = new Geom_TrimmedCurve(aLine, 0, DA->FirstAxisLength());
154 BRep_Builder aBuilder;
156 aBuilder.MakeEdge(aEdge, aTLine, Precision::Confusion());
157 if (!aEdge.IsNull()) {
158 std::shared_ptr<GeomAPI_Shape> aGeomShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
159 aGeomShape->setImpl(new TopoDS_Shape(aEdge));
160 thePrs->setShape(aGeomShape);
163 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(anIO);
164 if (!aPoint.IsNull()) {
165 // A point from trihedron
166 Handle(Geom_Point) aPnt = aPoint->Component();
167 BRep_Builder aBuilder;
168 TopoDS_Vertex aVertex;
169 aBuilder.MakeVertex(aVertex, aPnt->Pnt(), Precision::Confusion());
170 if (!aVertex.IsNull()) {
171 std::shared_ptr<GeomAPI_Shape> aGeomShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
172 aGeomShape->setImpl(new TopoDS_Shape(aVertex));
173 thePrs->setShape(aGeomShape);
180 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
181 ObjectPtr aFeature = aDisplayer->getObject(anIO);
183 Handle(ModuleBase_BRepOwner) aCompSolidBRO = Handle(ModuleBase_BRepOwner)::DownCast(theOwner);
184 if (!aCompSolidBRO.IsNull()) {
185 // If ModuleBase_BRepOwner object is created then it means that TopAbs_COMPSOLID selection mode
186 // is On and we have to use parent result which corresponds to the CompSolid shape
187 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aFeature);
189 ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(aResult);
190 if (aCompSolid.get()) {
191 GeomShapePtr aShape = aCompSolid->shape();
192 if (aShape.get() && aShape->isEqual(thePrs->shape())) {
193 thePrs->setObject(aCompSolid);
199 thePrs->setObject(aFeature);
202 QList<ModuleBase_ViewerPrsPtr> XGUI_Selection::getHighlighted() const
204 QList<ModuleBase_ViewerPrsPtr> aPresentations;
205 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
206 if (aContext.IsNull())
207 return aPresentations;
209 QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
210 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
211 for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
212 ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs());
213 Handle(AIS_InteractiveObject) anIO = aContext->DetectedInteractive();
214 if (aSelectedIds.contains((long)anIO.Access()))
217 aSelectedIds.append((long)anIO.Access());
218 aPrs->setInteractive(anIO);
220 ObjectPtr aResult = aDisplayer->getObject(anIO);
221 // we should not check the appearance of this feature because there can be some selected shapes
223 aPrs->setObject(aResult);
224 if (aContext->HasOpenedContext()) {
225 TopoDS_Shape aShape = aContext->DetectedShape();
226 if (!aShape.IsNull()) {
227 std::shared_ptr<GeomAPI_Shape> aGeomShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
228 aGeomShape->setImpl(new TopoDS_Shape(aShape));
229 aPrs->setShape(aGeomShape);
232 aPresentations.push_back(aPrs);
234 return aPresentations;
237 QObjectPtrList XGUI_Selection::selectedObjects() const
239 return myWorkshop->objectBrowser()->selectedObjects();
242 void XGUI_Selection::setSelectedObjects( const QObjectPtrList& theObjects ) const
244 return myWorkshop->objectBrowser()->setObjectsSelected( theObjects );
247 QObjectPtrList XGUI_Selection::selectedPresentations() const
249 QObjectPtrList aSelectedList;
251 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
252 if (!aContext.IsNull()) {
253 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
254 Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
255 ObjectPtr aResult = myWorkshop->displayer()->getObject(anIO);
257 aSelectedList.append(aResult);
260 return aSelectedList;
263 //**************************************************************
264 QModelIndexList XGUI_Selection::selectedIndexes() const
266 return myWorkshop->objectBrowser()->selectedIndexes();
269 //**************************************************************
270 void XGUI_Selection::selectedAISObjects(AIS_ListOfInteractive& theList) const
274 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
275 if (!aContext.IsNull()) {
276 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
277 theList.Append(aContext->SelectedInteractive());
281 //**************************************************************
282 ObjectPtr XGUI_Selection::getSelectableObject(const Handle(SelectMgr_EntityOwner)& theOwner) const
286 Handle(SelectMgr_EntityOwner) aEO = theOwner;
288 Handle(AIS_InteractiveObject) anObj =
289 Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
290 anObject = myWorkshop->displayer()->getObject(anObj);
295 //**************************************************************
296 void XGUI_Selection::selectedShapes(NCollection_List<TopoDS_Shape>& theList,
297 std::list<ObjectPtr>& theOwners) const
300 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
301 if (aContext.IsNull())
304 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
305 TopoDS_Shape aShape = aContext->SelectedShape();
306 if (aShape.IsNull()) {
307 aShape = findAxisShape(aContext->SelectedInteractive());
309 if (!aShape.IsNull()) {
310 theList.Append(aShape);
311 Handle(SelectMgr_EntityOwner) aEO = aContext->SelectedOwner();
313 Handle(AIS_InteractiveObject) anObj =
314 Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
315 ObjectPtr anObject = myWorkshop->displayer()->getObject(anObj);
316 theOwners.push_back(anObject);
322 //**************************************************************
323 void XGUI_Selection::selectedOwners(SelectMgr_IndexedMapOfOwner& theSelectedOwners) const
325 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
326 if (!aContext.IsNull()) {
327 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
328 theSelectedOwners.Add(aContext->SelectedOwner());
333 //**************************************************************
334 void XGUI_Selection::entityOwners(const Handle(AIS_InteractiveObject)& theObject,
335 SelectMgr_IndexedMapOfOwner& theOwners) const
337 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
338 if (aContext.IsNull() || theObject.IsNull())
341 TColStd_ListOfInteger aModes;
342 aContext->ActivatedModes(theObject, aModes);
344 TColStd_ListIteratorOfListOfInteger anIt(aModes);
345 for (; anIt.More(); anIt.Next()) {
346 int aMode = anIt.Value();
347 if (!theObject->HasSelection(aMode))
350 Handle(SelectMgr_Selection) aSelection = theObject->Selection(aMode);
351 for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
352 Handle(SelectMgr_SensitiveEntity) anEntity = aSelection->Sensitive();
353 if (anEntity.IsNull())
355 Handle(SelectMgr_EntityOwner) anOwner =
356 Handle(SelectMgr_EntityOwner)::DownCast(anEntity->BaseSensitive()->OwnerId());
357 if (!anOwner.IsNull())
358 theOwners.Add(anOwner);
363 //**************************************************************
364 TopoDS_Shape XGUI_Selection::findAxisShape(Handle(AIS_InteractiveObject) theIO) const
367 // Fill by trihedron shapes
368 Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(theIO);
369 if (!aAxis.IsNull()) {
370 // an Axis from Trihedron
371 Handle(Geom_Line) aLine = aAxis->Component();
372 Handle(Prs3d_DatumAspect) DA = aAxis->Attributes()->DatumAspect();
373 Handle(Geom_TrimmedCurve) aTLine = new Geom_TrimmedCurve(aLine, 0, DA->FirstAxisLength());
375 BRep_Builder aBuilder;
377 aBuilder.MakeEdge(aEdge, aTLine, Precision::Confusion());
381 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(theIO);
382 if (!aPoint.IsNull()) {
383 // A point from trihedron
384 Handle(Geom_Point) aPnt = aPoint->Component();
385 BRep_Builder aBuilder;
386 TopoDS_Vertex aVertex;
387 aBuilder.MakeVertex(aVertex, aPnt->Pnt(), Precision::Confusion());
388 if (!aVertex.IsNull())