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"
14 #include <ModuleBase_ViewerPrs.h>
16 #include <ModelAPI_Feature.h>
17 #include <ModelAPI_Tools.h>
18 #include <ModelAPI_Session.h>
19 #include <ModelAPI_ResultConstruction.h>
21 #include <AIS_InteractiveContext.hxx>
22 #ifdef BEFORE_TRIHEDRON_PATCH
23 #include <AIS_Axis.hxx>
24 #include <AIS_Point.hxx>
26 #include <AIS_TrihedronOwner.hxx>
28 #include <Geom_Line.hxx>
29 #include <BRep_Builder.hxx>
30 #include <TopoDS_Edge.hxx>
31 #include <Geom_Point.hxx>
32 #include <Geom_TrimmedCurve.hxx>
33 #include <Prs3d_DatumAspect.hxx>
35 #include <TColStd_ListIteratorOfListOfInteger.hxx>
36 #include <StdSelect_BRepOwner.hxx>
40 #define DEBUG_DELIVERY
42 XGUI_Selection::XGUI_Selection(XGUI_Workshop* theWorkshop)
43 : myWorkshop(theWorkshop)
47 QList<ModuleBase_ViewerPrsPtr> XGUI_Selection::getSelected(const SelectionPlace& thePlace) const
49 QList<ModuleBase_ViewerPrsPtr> aPresentations;
53 getSelectedInBrowser(aPresentations);
56 getSelectedInViewer(aPresentations);
59 getSelectedInViewer(aPresentations);
60 getSelectedInBrowser(aPresentations);
63 return aPresentations;
66 Handle(AIS_InteractiveObject) XGUI_Selection::getIO(const ModuleBase_ViewerPrsPtr& thePrs)
68 Handle(AIS_InteractiveObject) anIO = thePrs->interactive();
70 Handle(SelectMgr_EntityOwner) anOwner = thePrs->owner();
71 if (!anOwner.IsNull())
72 anIO = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
74 if (anIO.IsNull() && thePrs->object()) {
75 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
76 AISObjectPtr anAISObject = aDisplayer->getAISObject(thePrs->object());
77 if (anAISObject.get())
78 anIO = anAISObject->impl<Handle(AIS_InteractiveObject)>();
84 void XGUI_Selection::getSelectedInViewer(QList<ModuleBase_ViewerPrsPtr>& thePresentations) const
86 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
87 if (!aContext.IsNull()) {
88 QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
89 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
90 ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs());
91 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
93 if (aSelectedIds.contains((long)anOwner.get()))
95 aSelectedIds.append((long)anOwner.get());
97 fillPresentation(aPrs, anOwner);
99 if (!thePresentations.contains(aPrs)) // TODO: check whether the presentation in a list
100 thePresentations.append(aPrs);
105 void XGUI_Selection::getSelectedInBrowser(QList<ModuleBase_ViewerPrsPtr>& thePresentations) const
107 // collect the objects of the parameter presentation to avoid a repeted objects in the result
108 QObjectPtrList aPresentationObjects;
109 QList<ModuleBase_ViewerPrsPtr>::const_iterator aPrsIt = thePresentations.begin(),
110 aPrsLast = thePresentations.end();
111 for (; aPrsIt != aPrsLast; aPrsIt++) {
112 aPresentationObjects.push_back((*aPrsIt)->object());
115 QObjectPtrList anObjects = selectedObjects();
116 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
117 for (; anIt != aLast; anIt++) {
118 ObjectPtr anObject = *anIt;
119 if (anObject.get() != NULL && !aPresentationObjects.contains(anObject)) {
120 thePresentations.append(std::shared_ptr<ModuleBase_ViewerPrs>(
121 new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
126 void XGUI_Selection::fillPresentation(ModuleBase_ViewerPrsPtr& thePrs,
127 const Handle(SelectMgr_EntityOwner)& theOwner) const
129 thePrs->setOwner(theOwner);
130 Handle(SelectMgr_SelectableObject) aSelectable = theOwner->Selectable();
131 Handle(AIS_InteractiveObject) anIO =
132 Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
133 thePrs->setInteractive(anIO);
135 // we should not check the appearance of this feature because there can be some selected shapes
137 Handle(StdSelect_BRepOwner) aBRO = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
138 if( !aBRO.IsNull() && aBRO->HasShape() ) {
139 // the located method is called in the context to obtain the shape by the SelectedShape()
140 // method, so the shape is located by the same rules
141 TopoDS_Shape aShape = aBRO->Shape().Located (aBRO->Location() * aBRO->Shape().Location());
142 #ifdef BEFORE_TRIHEDRON_PATCH
143 #ifndef DEBUG_DELIVERY
145 aShape = findAxisShape(anIO);
148 if (!aShape.IsNull()) {
149 std::shared_ptr<GeomAPI_Shape> aGeomShape =
150 std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
151 aGeomShape->setImpl(new TopoDS_Shape(aShape));
152 thePrs->setShape(aGeomShape);
155 #ifdef DEBUG_DELIVERY
156 // Fill by trihedron shapes
157 #ifdef BEFORE_TRIHEDRON_PATCH
158 Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(anIO);
159 DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
160 int aSize = aDoc->size(ModelAPI_ResultConstruction::group());
162 if (!aAxis.IsNull()) {
163 // an Axis from Trihedron
164 gp_Lin aLine = aAxis->Component()->Lin();
165 gp_Dir aDir = aLine.Direction();
169 else if (aDir.Y() == 1.)
171 else if (aDir.Z() == 1.)
173 if (aAxName.length() > 0) {
175 for (int i = 0; i < aSize; i++) {
176 aObj = aDoc->object(ModelAPI_ResultConstruction::group(), i);
177 if (aObj->data()->name() == aAxName) {
178 aAx = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
183 thePrs->setObject(aAx);
184 thePrs->setShape(aAx->shape());
189 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(anIO);
190 if (!aPoint.IsNull()) {
191 // An origin point from trihedron
193 for (int i = 0; i < aSize; i++) {
194 aObj = aDoc->object(ModelAPI_ResultConstruction::group(), i);
195 if (aObj->data()->name() == "Origin") {
196 aOrigin = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
201 thePrs->setObject(aOrigin);
202 thePrs->setShape(aOrigin->shape());
208 /// find axis direction
209 Handle(AIS_TrihedronOwner) aTrihedronOwner = Handle(AIS_TrihedronOwner)::DownCast(theOwner);
210 if (!aTrihedronOwner.IsNull()) {
211 const Prs3d_DatumParts& aPart = aTrihedronOwner->DatumPart();
214 case Prs3d_DP_Origin: aName = "Origin"; break;
215 case Prs3d_DP_XAxis: aName = "OX"; break;
216 case Prs3d_DP_YAxis: aName = "OY"; break;
217 case Prs3d_DP_ZAxis: aName = "OZ"; break;
220 if (aName.length() > 0) {
221 DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
222 int aSize = aDoc->size(ModelAPI_ResultConstruction::group());
225 for (int i = 0; i < aSize; i++) {
226 aObj = aDoc->object(ModelAPI_ResultConstruction::group(), i);
227 if (aObj->data()->name() == aName) {
228 aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
233 thePrs->setObject(aResult);
234 thePrs->setShape(aResult->shape());
243 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
244 ObjectPtr aFeature = aDisplayer->getObject(anIO);
246 Handle(ModuleBase_BRepOwner) aCompSolidBRO = Handle(ModuleBase_BRepOwner)::DownCast(theOwner);
247 if (!aCompSolidBRO.IsNull()) {
248 // If ModuleBase_BRepOwner object is created then it means that TopAbs_COMPSOLID selection mode
249 // is On and we have to use parent result which corresponds to the CompSolid shape
250 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aFeature);
252 ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(aResult);
253 if (aCompSolid.get()) {
254 GeomShapePtr aShape = aCompSolid->shape();
255 if (aShape.get() && aShape->isEqual(thePrs->shape())) {
256 thePrs->setObject(aCompSolid);
262 thePrs->setObject(aFeature);
265 QList<ModuleBase_ViewerPrsPtr> XGUI_Selection::getHighlighted() const
267 QList<ModuleBase_ViewerPrsPtr> aPresentations;
268 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
269 if (aContext.IsNull())
270 return aPresentations;
272 QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
273 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
274 for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
275 Handle(SelectMgr_EntityOwner) anOwner = aContext->DetectedOwner();
276 if (!anOwner.IsNull()) {
277 if (aSelectedIds.contains((long)anOwner.get()))
279 aSelectedIds.append((long)anOwner.get());
281 ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs());
282 fillPresentation(aPrs, anOwner);
283 aPresentations.push_back(aPrs);
286 return aPresentations;
289 QObjectPtrList XGUI_Selection::selectedObjects() const
291 if (myWorkshop->objectBrowser())
292 return myWorkshop->objectBrowser()->selectedObjects();
293 return QObjectPtrList();
296 void XGUI_Selection::setSelectedObjects( const QObjectPtrList& theObjects ) const
298 return myWorkshop->objectBrowser()->setObjectsSelected( theObjects );
301 QObjectPtrList XGUI_Selection::selectedPresentations() const
303 QObjectPtrList aSelectedList;
305 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
306 if (!aContext.IsNull()) {
307 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
308 Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
309 ObjectPtr aResult = myWorkshop->displayer()->getObject(anIO);
311 aSelectedList.append(aResult);
314 return aSelectedList;
317 //**************************************************************
318 QModelIndexList XGUI_Selection::selectedIndexes() const
320 return myWorkshop->objectBrowser()->selectedIndexes();
323 //**************************************************************
324 void XGUI_Selection::selectedAISObjects(AIS_ListOfInteractive& theList) const
328 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
329 if (!aContext.IsNull()) {
330 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
331 theList.Append(aContext->SelectedInteractive());
335 //**************************************************************
336 ObjectPtr XGUI_Selection::getSelectableObject(const Handle(SelectMgr_EntityOwner)& theOwner) const
340 Handle(SelectMgr_EntityOwner) aEO = theOwner;
342 Handle(AIS_InteractiveObject) anObj =
343 Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
344 anObject = myWorkshop->displayer()->getObject(anObj);
349 //**************************************************************
350 void XGUI_Selection::selectedOwners(SelectMgr_IndexedMapOfOwner& theSelectedOwners) const
352 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
353 if (!aContext.IsNull()) {
354 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
355 theSelectedOwners.Add(aContext->SelectedOwner());
360 //**************************************************************
361 void XGUI_Selection::entityOwners(const Handle(AIS_InteractiveObject)& theObject,
362 SelectMgr_IndexedMapOfOwner& theOwners) const
364 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
365 if (aContext.IsNull() || theObject.IsNull())
368 TColStd_ListOfInteger aModes;
369 aContext->ActivatedModes(theObject, aModes);
371 TColStd_ListIteratorOfListOfInteger anIt(aModes);
372 for (; anIt.More(); anIt.Next()) {
373 int aMode = anIt.Value();
374 if (!theObject->HasSelection(aMode))
377 Handle(SelectMgr_Selection) aSelection = theObject->Selection(aMode);
378 for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
379 Handle(SelectMgr_SensitiveEntity) anEntity = aSelection->Sensitive();
380 if (anEntity.IsNull())
382 Handle(SelectMgr_EntityOwner) anOwner =
383 Handle(SelectMgr_EntityOwner)::DownCast(anEntity->BaseSensitive()->OwnerId());
384 if (!anOwner.IsNull())
385 theOwners.Add(anOwner);
390 //**************************************************************
391 #ifdef BEFORE_TRIHEDRON_PATCH
392 TopoDS_Shape XGUI_Selection::findAxisShape(Handle(AIS_InteractiveObject) theIO) const
395 // Fill by trihedron shapes
396 Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(theIO);
397 if (!aAxis.IsNull()) {
398 // an Axis from Trihedron
399 Handle(Geom_Line) aLine = aAxis->Component();
400 Handle(Prs3d_DatumAspect) DA = aAxis->Attributes()->DatumAspect();
401 Handle(Geom_TrimmedCurve) aTLine = new Geom_TrimmedCurve(aLine, 0, DA->FirstAxisLength());
403 BRep_Builder aBuilder;
405 aBuilder.MakeEdge(aEdge, aTLine, Precision::Confusion());
409 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(theIO);
410 if (!aPoint.IsNull()) {
411 // A point from trihedron
412 Handle(Geom_Point) aPnt = aPoint->Component();
413 BRep_Builder aBuilder;
414 TopoDS_Vertex aVertex;
415 aBuilder.MakeVertex(aVertex, aPnt->Pnt(), Precision::Confusion());
416 if (!aVertex.IsNull())