1 // Copyright (C) 2014-2023 CEA, EDF
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "XGUI_Selection.h"
21 #include "XGUI_Workshop.h"
22 #include "XGUI_Displayer.h"
23 #include "XGUI_ViewerProxy.h"
24 #include "XGUI_ObjectsBrowser.h"
27 #include <AIS_ViewCube.hxx>
30 #include "ModuleBase_BRepOwner.h"
31 #include "ModuleBase_ResultPrs.h"
32 #include "ModuleBase_ViewerPrs.h"
34 #include <ModelAPI_Feature.h>
35 #include <ModelAPI_Tools.h>
36 #include <ModelAPI_Session.h>
37 #include <ModelAPI_ResultBody.h>
38 #include <ModelAPI_ResultConstruction.h>
40 #include <AIS_InteractiveContext.hxx>
41 #ifdef BEFORE_TRIHEDRON_PATCH
42 #include <AIS_Axis.hxx>
43 #include <AIS_Point.hxx>
45 #include <AIS_TrihedronOwner.hxx>
47 #include <Geom_Line.hxx>
48 #include <BRep_Builder.hxx>
49 #include <TopoDS_Edge.hxx>
50 #include <Geom_Point.hxx>
51 #include <Geom_TrimmedCurve.hxx>
52 #include <Prs3d_DatumAspect.hxx>
54 #include <TColStd_ListIteratorOfListOfInteger.hxx>
55 #include <StdSelect_BRepOwner.hxx>
60 #pragma warning(disable : 4456) // for nested foreach
63 #define DEBUG_DELIVERY
65 XGUI_Selection::XGUI_Selection(XGUI_Workshop* theWorkshop)
66 : myWorkshop(theWorkshop)
70 QList<ModuleBase_ViewerPrsPtr> XGUI_Selection::getSelected(const SelectionPlace& thePlace) const
72 QList<ModuleBase_ViewerPrsPtr> aPresentations;
73 QList<ModuleBase_ViewerPrsPtr> aToRemove;
77 getSelectedInBrowser(aPresentations);
80 getSelectedInViewer(aPresentations);
83 // Get selection from object browser
84 getSelectedInBrowser(aPresentations);
86 // Filter out all objects except feature if there is no selected results in object browser
87 // Filter out all features if in object browser there are selected features and their results
88 bool aHasFeature = false;
89 bool aHasResult = false;
90 foreach(ModuleBase_ViewerPrsPtr aVal, aPresentations) {
91 if (aVal->object().get()) {
92 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aVal->object());
95 std::list<ResultPtr> aResList = aFeature->results();
96 std::list<ResultPtr>::const_iterator aIt;
97 for (aIt = aResList.cbegin(); aIt != aResList.cend(); aIt++) {
98 foreach(ModuleBase_ViewerPrsPtr aSel, aPresentations) {
99 if (aSel->object() == (*aIt)) {
109 if (aHasFeature && aHasResult)
112 //Get selection from a viewer
113 getSelectedInViewer(aPresentations);
115 // Filter out extra objects
116 if (aHasFeature && aHasResult) {
117 foreach(ModuleBase_ViewerPrsPtr aVal, aPresentations) {
118 if (aVal->object().get()) {
119 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aVal->object());
120 if (aFeature.get()) {
121 aToRemove.append(aVal);
126 else if (aHasFeature && (!aHasResult)) {
127 foreach(ModuleBase_ViewerPrsPtr aVal, aPresentations) {
128 if (aVal->object().get()) {
129 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aVal->object());
130 if (!aFeature.get()) {
131 aToRemove.append(aVal);
136 foreach(ModuleBase_ViewerPrsPtr aVal, aToRemove) {
137 aPresentations.removeAll(aVal);
141 return aPresentations;
144 Handle(AIS_InteractiveObject) XGUI_Selection::getIO(const ModuleBase_ViewerPrsPtr& thePrs)
146 Handle(AIS_InteractiveObject) anIO = thePrs->interactive();
148 Handle(SelectMgr_EntityOwner) anOwner = thePrs->owner();
149 if (!anOwner.IsNull())
150 anIO = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
152 if (anIO.IsNull() && thePrs->object()) {
153 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
154 AISObjectPtr anAISObject = aDisplayer->getAISObject(thePrs->object());
155 if (anAISObject.get())
156 anIO = anAISObject->impl<Handle(AIS_InteractiveObject)>();
162 void XGUI_Selection::getSelectedInViewer(QList<ModuleBase_ViewerPrsPtr>& thePresentations) const
164 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
165 if (!aContext.IsNull()) {
166 QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
167 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
168 ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs());
169 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
171 if (aSelectedIds.contains((size_t)anOwner.get()))
173 aSelectedIds.append((size_t)anOwner.get());
175 fillPresentation(aPrs, anOwner);
177 if (!thePresentations.contains(aPrs)) // TODO: check whether the presentation in a list
178 thePresentations.append(aPrs);
183 void XGUI_Selection::getSelectedInBrowser(QList<ModuleBase_ViewerPrsPtr>& thePresentations) const
185 // collect the objects of the parameter presentation to avoid a repeted objects in the result
186 QObjectPtrList aPresentationObjects;
187 QList<ModuleBase_ViewerPrsPtr>::const_iterator aPrsIt = thePresentations.begin(),
188 aPrsLast = thePresentations.end();
189 for (; aPrsIt != aPrsLast; aPrsIt++) {
190 aPresentationObjects.push_back((*aPrsIt)->object());
193 QObjectPtrList anObjects = selectedObjects();
194 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
195 for (; anIt != aLast; anIt++) {
196 ObjectPtr anObject = *anIt;
197 if (anObject.get() != NULL && !aPresentationObjects.contains(anObject)) {
198 thePresentations.append(std::shared_ptr<ModuleBase_ViewerPrs>(
199 new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
204 void XGUI_Selection::fillPresentation(ModuleBase_ViewerPrsPtr& thePrs,
205 const Handle(SelectMgr_EntityOwner)& theOwner) const
207 Handle(AIS_InteractiveObject) anIO =
208 Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
210 Handle(AIS_ViewCube) aCube = Handle(AIS_ViewCube)::DownCast(anIO);
215 thePrs->setOwner(theOwner);
216 thePrs->setInteractive(anIO);
218 // we should not check the appearance of this feature because there can be some selected shapes
220 Handle(StdSelect_BRepOwner) aBRO = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
221 if( !aBRO.IsNull() && aBRO->HasShape() ) {
222 TopoDS_Shape aShape = aBRO->Shape();
223 Handle(ModuleBase_ResultPrs) aPrsObj =
224 Handle(ModuleBase_ResultPrs)::DownCast(aBRO->Selectable());
225 if (!aPrsObj.IsNull()) {
226 if (aPrsObj->isSubstituted()) {
227 if (aPrsObj->Shape().IsSame(aShape))
228 aShape = aPrsObj->originalShape();
231 // the located method is called in the context to obtain the shape by the SelectedShape()
232 // method, so the shape is located by the same rules
233 aShape = aShape.Located(aBRO->Location() * aShape.Location());
234 #ifdef BEFORE_TRIHEDRON_PATCH
235 #ifndef DEBUG_DELIVERY
237 aShape = findAxisShape(anIO);
240 if (!aShape.IsNull()) {
241 std::shared_ptr<GeomAPI_Shape> aGeomShape =
242 std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
243 aGeomShape->setImpl(new TopoDS_Shape(aShape));
244 thePrs->setShape(aGeomShape);
247 #ifdef DEBUG_DELIVERY
248 // Fill by trihedron shapes
249 #ifdef BEFORE_TRIHEDRON_PATCH
250 Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(anIO);
251 DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
252 int aSize = aDoc->size(ModelAPI_ResultConstruction::group());
254 if (!aAxis.IsNull()) {
255 // an Axis from Trihedron
256 gp_Lin aLine = aAxis->Component()->Lin();
257 gp_Dir aDir = aLine.Direction();
261 else if (aDir.Y() == 1.)
263 else if (aDir.Z() == 1.)
265 if (aAxName.length() > 0) {
267 for (int i = 0; i < aSize; i++) {
268 aObj = aDoc->object(ModelAPI_ResultConstruction::group(), i);
269 if (aObj->data()->name() == aAxName) {
270 aAx = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
275 thePrs->setObject(aAx);
276 thePrs->setShape(aAx->shape());
281 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(anIO);
282 if (!aPoint.IsNull()) {
283 // An origin point from trihedron
285 for (int i = 0; i < aSize; i++) {
286 aObj = aDoc->object(ModelAPI_ResultConstruction::group(), i);
287 if (aObj->data()->name() == "Origin") {
288 aOrigin = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
293 thePrs->setObject(aOrigin);
294 thePrs->setShape(aOrigin->shape());
300 /// find axis direction
301 Handle(AIS_TrihedronOwner) aTrihedronOwner = Handle(AIS_TrihedronOwner)::DownCast(theOwner);
302 if (!aTrihedronOwner.IsNull()) {
303 const Prs3d_DatumParts& aPart = aTrihedronOwner->DatumPart();
306 case Prs3d_DP_Origin: aName = L"Origin"; break;
307 case Prs3d_DP_XAxis: aName = L"OX"; break;
308 case Prs3d_DP_YAxis: aName = L"OY"; break;
309 case Prs3d_DP_ZAxis: aName = L"OZ"; break;
312 if (aName.length() > 0) {
313 DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
314 int aSize = aDoc->size(ModelAPI_ResultConstruction::group());
317 for (int i = 0; i < aSize; i++) {
318 aObj = aDoc->object(ModelAPI_ResultConstruction::group(), i);
319 if (aObj->data()->name() == aName) {
320 aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
325 thePrs->setObject(aResult);
326 thePrs->setShape(aResult->shape());
335 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
336 ObjectPtr aFeature = aDisplayer->getObject(anIO);
338 Handle(ModuleBase_BRepOwner) aCompSolidBRO = Handle(ModuleBase_BRepOwner)::DownCast(theOwner);
339 if (!aCompSolidBRO.IsNull()) {
340 // If ModuleBase_BRepOwner object is created then it means that TopAbs_COMPSOLID selection mode
341 // is On and we have to use parent result which corresponds to the CompSolid shape
342 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aFeature);
344 ResultBodyPtr aCompSolid = ModelAPI_Tools::bodyOwner(aResult);
345 if (aCompSolid.get()) {
346 GeomShapePtr aShape = aCompSolid->shape();
347 if (aShape.get() && aShape->isEqual(thePrs->shape())) {
348 thePrs->setObject(aCompSolid);
354 thePrs->setObject(aFeature);
357 QList<ModuleBase_ViewerPrsPtr> XGUI_Selection::getHighlighted() const
359 QList<ModuleBase_ViewerPrsPtr> aPresentations;
360 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
361 if (aContext.IsNull())
362 return aPresentations;
364 QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
365 for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
366 Handle(SelectMgr_EntityOwner) anOwner = aContext->DetectedOwner();
367 if (!anOwner.IsNull()) {
368 if (aSelectedIds.contains((size_t)anOwner.get()))
370 aSelectedIds.append((size_t)anOwner.get());
372 ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs());
373 fillPresentation(aPrs, anOwner);
374 aPresentations.push_back(aPrs);
377 return aPresentations;
380 QObjectPtrList XGUI_Selection::selectedObjects() const
382 if (myWorkshop->objectBrowser())
383 return myWorkshop->objectBrowser()->selectedObjects();
384 return QObjectPtrList();
387 QObjectPtrList XGUI_Selection::selectedPresentations() const
389 QObjectPtrList aSelectedList;
391 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
392 if (!aContext.IsNull()) {
393 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
394 Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
395 ObjectPtr aResult = myWorkshop->displayer()->getObject(anIO);
397 aSelectedList.append(aResult);
400 return aSelectedList;
403 //**************************************************************
404 QModelIndexList XGUI_Selection::selectedIndexes() const
406 return myWorkshop->objectBrowser()->selectedIndexes();
409 //**************************************************************
410 ObjectPtr XGUI_Selection::getSelectableObject(const Handle(SelectMgr_EntityOwner)& theOwner) const
414 Handle(SelectMgr_EntityOwner) aEO = theOwner;
416 Handle(AIS_InteractiveObject) anObj =
417 Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
418 anObject = myWorkshop->displayer()->getObject(anObj);
423 //**************************************************************
424 void XGUI_Selection::selectedOwners(SelectMgr_IndexedMapOfOwner& theSelectedOwners) const
426 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
427 if (!aContext.IsNull()) {
428 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
429 theSelectedOwners.Add(aContext->SelectedOwner());
434 //**************************************************************
435 void XGUI_Selection::entityOwners(const Handle(AIS_InteractiveObject)& theObject,
436 SelectMgr_IndexedMapOfOwner& theOwners) const
438 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
439 if (aContext.IsNull() || theObject.IsNull())
442 TColStd_ListOfInteger aModes;
443 aContext->ActivatedModes(theObject, aModes);
445 TColStd_ListIteratorOfListOfInteger anIt(aModes);
446 for (; anIt.More(); anIt.Next()) {
447 int aMode = anIt.Value();
448 if (!theObject->HasSelection(aMode))
451 Handle(SelectMgr_Selection) aSelection = theObject->Selection(aMode);
452 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
453 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anEntIt(anEntities);
454 anEntIt.More(); anEntIt.Next()) {
455 Handle(SelectMgr_SensitiveEntity) anEntity = anEntIt.Value();
456 if (anEntity.IsNull())
458 Handle(SelectMgr_EntityOwner) anOwner = anEntity->BaseSensitive()->OwnerId();
459 if (!anOwner.IsNull())
460 theOwners.Add(anOwner);
465 //**************************************************************
466 #ifdef BEFORE_TRIHEDRON_PATCH
467 TopoDS_Shape XGUI_Selection::findAxisShape(Handle(AIS_InteractiveObject) theIO) const
470 // Fill by trihedron shapes
471 Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(theIO);
472 if (!aAxis.IsNull()) {
473 // an Axis from Trihedron
474 Handle(Geom_Line) aLine = aAxis->Component();
475 Handle(Prs3d_DatumAspect) DA = aAxis->Attributes()->DatumAspect();
476 Handle(Geom_TrimmedCurve) aTLine = new Geom_TrimmedCurve(aLine, 0, DA->FirstAxisLength());
478 BRep_Builder aBuilder;
480 aBuilder.MakeEdge(aEdge, aTLine, Precision::Confusion());
484 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(theIO);
485 if (!aPoint.IsNull()) {
486 // A point from trihedron
487 Handle(Geom_Point) aPnt = aPoint->Component();
488 BRep_Builder aBuilder;
489 TopoDS_Vertex aVertex;
490 aBuilder.MakeVertex(aVertex, aPnt->Pnt(), Precision::Confusion());
491 if (!aVertex.IsNull())