1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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"
26 #include "ModuleBase_BRepOwner.h"
27 #include "ModuleBase_ResultPrs.h"
28 #include "ModuleBase_ViewerPrs.h"
30 #include <ModelAPI_Feature.h>
31 #include <ModelAPI_Tools.h>
32 #include <ModelAPI_Session.h>
33 #include <ModelAPI_ResultBody.h>
34 #include <ModelAPI_ResultConstruction.h>
36 #include <AIS_InteractiveContext.hxx>
37 #ifdef BEFORE_TRIHEDRON_PATCH
38 #include <AIS_Axis.hxx>
39 #include <AIS_Point.hxx>
41 #include <AIS_TrihedronOwner.hxx>
43 #include <Geom_Line.hxx>
44 #include <BRep_Builder.hxx>
45 #include <TopoDS_Edge.hxx>
46 #include <Geom_Point.hxx>
47 #include <Geom_TrimmedCurve.hxx>
48 #include <Prs3d_DatumAspect.hxx>
50 #include <TColStd_ListIteratorOfListOfInteger.hxx>
51 #include <StdSelect_BRepOwner.hxx>
55 #define DEBUG_DELIVERY
57 XGUI_Selection::XGUI_Selection(XGUI_Workshop* theWorkshop)
58 : myWorkshop(theWorkshop)
62 QList<ModuleBase_ViewerPrsPtr> XGUI_Selection::getSelected(const SelectionPlace& thePlace) const
64 QList<ModuleBase_ViewerPrsPtr> aPresentations;
65 QList<ModuleBase_ViewerPrsPtr> aToRemove;
69 getSelectedInBrowser(aPresentations);
72 getSelectedInViewer(aPresentations);
75 // Get selection from object browser
76 getSelectedInBrowser(aPresentations);
78 // Filter out all objects except feature if there is no selected results in object browser
79 // Filter out all features if in object browser there are selected features and their results
80 bool aHasFeature = false;
81 bool aHasResult = false;
82 foreach(ModuleBase_ViewerPrsPtr aVal, aPresentations) {
83 if (aVal->object().get()) {
84 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aVal->object());
87 std::list<ResultPtr> aResList = aFeature->results();
88 std::list<ResultPtr>::const_iterator aIt;
89 for (aIt = aResList.cbegin(); aIt != aResList.cend(); aIt++) {
90 foreach(ModuleBase_ViewerPrsPtr aSel, aPresentations) {
91 if (aSel->object() == (*aIt)) {
101 if (aHasFeature && aHasResult)
104 //Get selection from a viewer
105 getSelectedInViewer(aPresentations);
107 // Filter out extra objects
108 if (aHasFeature && aHasResult) {
109 foreach(ModuleBase_ViewerPrsPtr aVal, aPresentations) {
110 if (aVal->object().get()) {
111 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aVal->object());
112 if (aFeature.get()) {
113 aToRemove.append(aVal);
118 else if (aHasFeature && (!aHasResult)) {
119 foreach(ModuleBase_ViewerPrsPtr aVal, aPresentations) {
120 if (aVal->object().get()) {
121 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aVal->object());
122 if (!aFeature.get()) {
123 aToRemove.append(aVal);
128 foreach(ModuleBase_ViewerPrsPtr aVal, aToRemove) {
129 aPresentations.removeAll(aVal);
133 return aPresentations;
136 Handle(AIS_InteractiveObject) XGUI_Selection::getIO(const ModuleBase_ViewerPrsPtr& thePrs)
138 Handle(AIS_InteractiveObject) anIO = thePrs->interactive();
140 Handle(SelectMgr_EntityOwner) anOwner = thePrs->owner();
141 if (!anOwner.IsNull())
142 anIO = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
144 if (anIO.IsNull() && thePrs->object()) {
145 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
146 AISObjectPtr anAISObject = aDisplayer->getAISObject(thePrs->object());
147 if (anAISObject.get())
148 anIO = anAISObject->impl<Handle(AIS_InteractiveObject)>();
154 void XGUI_Selection::getSelectedInViewer(QList<ModuleBase_ViewerPrsPtr>& thePresentations) const
156 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
157 if (!aContext.IsNull()) {
158 QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
159 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
160 ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs());
161 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
163 if (aSelectedIds.contains((size_t)anOwner.get()))
165 aSelectedIds.append((size_t)anOwner.get());
167 fillPresentation(aPrs, anOwner);
169 if (!thePresentations.contains(aPrs)) // TODO: check whether the presentation in a list
170 thePresentations.append(aPrs);
175 void XGUI_Selection::getSelectedInBrowser(QList<ModuleBase_ViewerPrsPtr>& thePresentations) const
177 // collect the objects of the parameter presentation to avoid a repeted objects in the result
178 QObjectPtrList aPresentationObjects;
179 QList<ModuleBase_ViewerPrsPtr>::const_iterator aPrsIt = thePresentations.begin(),
180 aPrsLast = thePresentations.end();
181 for (; aPrsIt != aPrsLast; aPrsIt++) {
182 aPresentationObjects.push_back((*aPrsIt)->object());
185 QObjectPtrList anObjects = selectedObjects();
186 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
187 for (; anIt != aLast; anIt++) {
188 ObjectPtr anObject = *anIt;
189 if (anObject.get() != NULL && !aPresentationObjects.contains(anObject)) {
190 thePresentations.append(std::shared_ptr<ModuleBase_ViewerPrs>(
191 new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
196 void XGUI_Selection::fillPresentation(ModuleBase_ViewerPrsPtr& thePrs,
197 const Handle(SelectMgr_EntityOwner)& theOwner) const
199 thePrs->setOwner(theOwner);
200 Handle(AIS_InteractiveObject) anIO =
201 Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
202 thePrs->setInteractive(anIO);
204 // we should not check the appearance of this feature because there can be some selected shapes
206 Handle(StdSelect_BRepOwner) aBRO = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
207 if( !aBRO.IsNull() && aBRO->HasShape() ) {
208 TopoDS_Shape aShape = aBRO->Shape();
209 Handle(ModuleBase_ResultPrs) aPrsObj = Handle(ModuleBase_ResultPrs)::DownCast(aBRO->Selectable());
210 if (!aPrsObj.IsNull()) {
211 if (aPrsObj->isSubstituted()) {
212 if (aPrsObj->Shape().IsSame(aShape))
213 aShape = aPrsObj->originalShape();
216 // the located method is called in the context to obtain the shape by the SelectedShape()
217 // method, so the shape is located by the same rules
218 aShape = aShape.Located(aBRO->Location() * aShape.Location());
219 #ifdef BEFORE_TRIHEDRON_PATCH
220 #ifndef DEBUG_DELIVERY
222 aShape = findAxisShape(anIO);
225 if (!aShape.IsNull()) {
226 std::shared_ptr<GeomAPI_Shape> aGeomShape =
227 std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
228 aGeomShape->setImpl(new TopoDS_Shape(aShape));
229 thePrs->setShape(aGeomShape);
232 #ifdef DEBUG_DELIVERY
233 // Fill by trihedron shapes
234 #ifdef BEFORE_TRIHEDRON_PATCH
235 Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(anIO);
236 DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
237 int aSize = aDoc->size(ModelAPI_ResultConstruction::group());
239 if (!aAxis.IsNull()) {
240 // an Axis from Trihedron
241 gp_Lin aLine = aAxis->Component()->Lin();
242 gp_Dir aDir = aLine.Direction();
246 else if (aDir.Y() == 1.)
248 else if (aDir.Z() == 1.)
250 if (aAxName.length() > 0) {
252 for (int i = 0; i < aSize; i++) {
253 aObj = aDoc->object(ModelAPI_ResultConstruction::group(), i);
254 if (aObj->data()->name() == aAxName) {
255 aAx = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
260 thePrs->setObject(aAx);
261 thePrs->setShape(aAx->shape());
266 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(anIO);
267 if (!aPoint.IsNull()) {
268 // An origin point from trihedron
270 for (int i = 0; i < aSize; i++) {
271 aObj = aDoc->object(ModelAPI_ResultConstruction::group(), i);
272 if (aObj->data()->name() == "Origin") {
273 aOrigin = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
278 thePrs->setObject(aOrigin);
279 thePrs->setShape(aOrigin->shape());
285 /// find axis direction
286 Handle(AIS_TrihedronOwner) aTrihedronOwner = Handle(AIS_TrihedronOwner)::DownCast(theOwner);
287 if (!aTrihedronOwner.IsNull()) {
288 const Prs3d_DatumParts& aPart = aTrihedronOwner->DatumPart();
291 case Prs3d_DP_Origin: aName = "Origin"; break;
292 case Prs3d_DP_XAxis: aName = "OX"; break;
293 case Prs3d_DP_YAxis: aName = "OY"; break;
294 case Prs3d_DP_ZAxis: aName = "OZ"; break;
297 if (aName.length() > 0) {
298 DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
299 int aSize = aDoc->size(ModelAPI_ResultConstruction::group());
302 for (int i = 0; i < aSize; i++) {
303 aObj = aDoc->object(ModelAPI_ResultConstruction::group(), i);
304 if (aObj->data()->name() == aName) {
305 aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
310 thePrs->setObject(aResult);
311 thePrs->setShape(aResult->shape());
320 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
321 ObjectPtr aFeature = aDisplayer->getObject(anIO);
323 Handle(ModuleBase_BRepOwner) aCompSolidBRO = Handle(ModuleBase_BRepOwner)::DownCast(theOwner);
324 if (!aCompSolidBRO.IsNull()) {
325 // If ModuleBase_BRepOwner object is created then it means that TopAbs_COMPSOLID selection mode
326 // is On and we have to use parent result which corresponds to the CompSolid shape
327 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aFeature);
329 ResultBodyPtr aCompSolid = ModelAPI_Tools::bodyOwner(aResult);
330 if (aCompSolid.get()) {
331 GeomShapePtr aShape = aCompSolid->shape();
332 if (aShape.get() && aShape->isEqual(thePrs->shape())) {
333 thePrs->setObject(aCompSolid);
339 thePrs->setObject(aFeature);
342 QList<ModuleBase_ViewerPrsPtr> XGUI_Selection::getHighlighted() const
344 QList<ModuleBase_ViewerPrsPtr> aPresentations;
345 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
346 if (aContext.IsNull())
347 return aPresentations;
349 QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
350 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
351 for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
352 Handle(SelectMgr_EntityOwner) anOwner = aContext->DetectedOwner();
353 if (!anOwner.IsNull()) {
354 if (aSelectedIds.contains((size_t)anOwner.get()))
356 aSelectedIds.append((size_t)anOwner.get());
358 ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs());
359 fillPresentation(aPrs, anOwner);
360 aPresentations.push_back(aPrs);
363 return aPresentations;
366 QObjectPtrList XGUI_Selection::selectedObjects() const
368 if (myWorkshop->objectBrowser())
369 return myWorkshop->objectBrowser()->selectedObjects();
370 return QObjectPtrList();
373 QObjectPtrList XGUI_Selection::selectedPresentations() const
375 QObjectPtrList aSelectedList;
377 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
378 if (!aContext.IsNull()) {
379 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
380 Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
381 ObjectPtr aResult = myWorkshop->displayer()->getObject(anIO);
383 aSelectedList.append(aResult);
386 return aSelectedList;
389 //**************************************************************
390 QModelIndexList XGUI_Selection::selectedIndexes() const
392 return myWorkshop->objectBrowser()->selectedIndexes();
395 //**************************************************************
396 ObjectPtr XGUI_Selection::getSelectableObject(const Handle(SelectMgr_EntityOwner)& theOwner) const
400 Handle(SelectMgr_EntityOwner) aEO = theOwner;
402 Handle(AIS_InteractiveObject) anObj =
403 Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
404 anObject = myWorkshop->displayer()->getObject(anObj);
409 //**************************************************************
410 void XGUI_Selection::selectedOwners(SelectMgr_IndexedMapOfOwner& theSelectedOwners) const
412 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
413 if (!aContext.IsNull()) {
414 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
415 theSelectedOwners.Add(aContext->SelectedOwner());
420 //**************************************************************
421 void XGUI_Selection::entityOwners(const Handle(AIS_InteractiveObject)& theObject,
422 SelectMgr_IndexedMapOfOwner& theOwners) const
424 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
425 if (aContext.IsNull() || theObject.IsNull())
428 TColStd_ListOfInteger aModes;
429 aContext->ActivatedModes(theObject, aModes);
431 TColStd_ListIteratorOfListOfInteger anIt(aModes);
432 for (; anIt.More(); anIt.Next()) {
433 int aMode = anIt.Value();
434 if (!theObject->HasSelection(aMode))
437 Handle(SelectMgr_Selection) aSelection = theObject->Selection(aMode);
438 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
439 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
442 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
443 if (anEntity.IsNull())
445 Handle(SelectMgr_EntityOwner) anOwner =
446 Handle(SelectMgr_EntityOwner)::DownCast(anEntity->BaseSensitive()->OwnerId());
447 if (!anOwner.IsNull())
448 theOwners.Add(anOwner);
453 //**************************************************************
454 #ifdef BEFORE_TRIHEDRON_PATCH
455 TopoDS_Shape XGUI_Selection::findAxisShape(Handle(AIS_InteractiveObject) theIO) const
458 // Fill by trihedron shapes
459 Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(theIO);
460 if (!aAxis.IsNull()) {
461 // an Axis from Trihedron
462 Handle(Geom_Line) aLine = aAxis->Component();
463 Handle(Prs3d_DatumAspect) DA = aAxis->Attributes()->DatumAspect();
464 Handle(Geom_TrimmedCurve) aTLine = new Geom_TrimmedCurve(aLine, 0, DA->FirstAxisLength());
466 BRep_Builder aBuilder;
468 aBuilder.MakeEdge(aEdge, aTLine, Precision::Confusion());
472 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(theIO);
473 if (!aPoint.IsNull()) {
474 // A point from trihedron
475 Handle(Geom_Point) aPnt = aPoint->Component();
476 BRep_Builder aBuilder;
477 TopoDS_Vertex aVertex;
478 aBuilder.MakeVertex(aVertex, aPnt->Pnt(), Precision::Confusion());
479 if (!aVertex.IsNull())