1 // Copyright (C) 2014-2017 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<mailto: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_ResultPrs.h"
27 #include <ModuleBase_ViewerPrs.h>
29 #include <ModelAPI_Feature.h>
30 #include <ModelAPI_Tools.h>
31 #include <ModelAPI_Session.h>
32 #include <ModelAPI_ResultCompSolid.h>
33 #include <ModelAPI_ResultConstruction.h>
35 #include <AIS_InteractiveContext.hxx>
36 #ifdef BEFORE_TRIHEDRON_PATCH
37 #include <AIS_Axis.hxx>
38 #include <AIS_Point.hxx>
40 #include <AIS_TrihedronOwner.hxx>
42 #include <Geom_Line.hxx>
43 #include <BRep_Builder.hxx>
44 #include <TopoDS_Edge.hxx>
45 #include <Geom_Point.hxx>
46 #include <Geom_TrimmedCurve.hxx>
47 #include <Prs3d_DatumAspect.hxx>
49 #include <TColStd_ListIteratorOfListOfInteger.hxx>
50 #include <StdSelect_BRepOwner.hxx>
54 #define DEBUG_DELIVERY
56 XGUI_Selection::XGUI_Selection(XGUI_Workshop* theWorkshop)
57 : myWorkshop(theWorkshop)
61 QList<ModuleBase_ViewerPrsPtr> XGUI_Selection::getSelected(const SelectionPlace& thePlace) const
63 QList<ModuleBase_ViewerPrsPtr> aPresentations;
67 getSelectedInBrowser(aPresentations);
70 getSelectedInViewer(aPresentations);
73 getSelectedInViewer(aPresentations);
74 getSelectedInBrowser(aPresentations);
77 return aPresentations;
80 Handle(AIS_InteractiveObject) XGUI_Selection::getIO(const ModuleBase_ViewerPrsPtr& thePrs)
82 Handle(AIS_InteractiveObject) anIO = thePrs->interactive();
84 Handle(SelectMgr_EntityOwner) anOwner = thePrs->owner();
85 if (!anOwner.IsNull())
86 anIO = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
88 if (anIO.IsNull() && thePrs->object()) {
89 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
90 AISObjectPtr anAISObject = aDisplayer->getAISObject(thePrs->object());
91 if (anAISObject.get())
92 anIO = anAISObject->impl<Handle(AIS_InteractiveObject)>();
98 void XGUI_Selection::getSelectedInViewer(QList<ModuleBase_ViewerPrsPtr>& thePresentations) const
100 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
101 if (!aContext.IsNull()) {
102 QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
103 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
104 ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs());
105 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
107 if (aSelectedIds.contains((long)anOwner.get()))
109 aSelectedIds.append((long)anOwner.get());
111 fillPresentation(aPrs, anOwner);
113 if (!thePresentations.contains(aPrs)) // TODO: check whether the presentation in a list
114 thePresentations.append(aPrs);
119 void XGUI_Selection::getSelectedInBrowser(QList<ModuleBase_ViewerPrsPtr>& thePresentations) const
121 // collect the objects of the parameter presentation to avoid a repeted objects in the result
122 QObjectPtrList aPresentationObjects;
123 QList<ModuleBase_ViewerPrsPtr>::const_iterator aPrsIt = thePresentations.begin(),
124 aPrsLast = thePresentations.end();
125 for (; aPrsIt != aPrsLast; aPrsIt++) {
126 aPresentationObjects.push_back((*aPrsIt)->object());
129 QObjectPtrList anObjects = selectedObjects();
130 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
131 for (; anIt != aLast; anIt++) {
132 ObjectPtr anObject = *anIt;
133 if (anObject.get() != NULL && !aPresentationObjects.contains(anObject)) {
134 thePresentations.append(std::shared_ptr<ModuleBase_ViewerPrs>(
135 new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
140 void XGUI_Selection::fillPresentation(ModuleBase_ViewerPrsPtr& thePrs,
141 const Handle(SelectMgr_EntityOwner)& theOwner) const
143 thePrs->setOwner(theOwner);
144 Handle(SelectMgr_SelectableObject) aSelectable = theOwner->Selectable();
145 Handle(AIS_InteractiveObject) anIO =
146 Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
147 thePrs->setInteractive(anIO);
149 // we should not check the appearance of this feature because there can be some selected shapes
151 Handle(StdSelect_BRepOwner) aBRO = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
152 if( !aBRO.IsNull() && aBRO->HasShape() ) {
153 // the located method is called in the context to obtain the shape by the SelectedShape()
154 // method, so the shape is located by the same rules
155 TopoDS_Shape aShape = aBRO->Shape().Located (aBRO->Location() * aBRO->Shape().Location());
156 #ifdef BEFORE_TRIHEDRON_PATCH
157 #ifndef DEBUG_DELIVERY
159 aShape = findAxisShape(anIO);
162 if (!aShape.IsNull()) {
163 std::shared_ptr<GeomAPI_Shape> aGeomShape =
164 std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
165 aGeomShape->setImpl(new TopoDS_Shape(aShape));
166 thePrs->setShape(aGeomShape);
169 #ifdef DEBUG_DELIVERY
170 // Fill by trihedron shapes
171 #ifdef BEFORE_TRIHEDRON_PATCH
172 Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(anIO);
173 DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
174 int aSize = aDoc->size(ModelAPI_ResultConstruction::group());
176 if (!aAxis.IsNull()) {
177 // an Axis from Trihedron
178 gp_Lin aLine = aAxis->Component()->Lin();
179 gp_Dir aDir = aLine.Direction();
183 else if (aDir.Y() == 1.)
185 else if (aDir.Z() == 1.)
187 if (aAxName.length() > 0) {
189 for (int i = 0; i < aSize; i++) {
190 aObj = aDoc->object(ModelAPI_ResultConstruction::group(), i);
191 if (aObj->data()->name() == aAxName) {
192 aAx = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
197 thePrs->setObject(aAx);
198 thePrs->setShape(aAx->shape());
203 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(anIO);
204 if (!aPoint.IsNull()) {
205 // An origin point from trihedron
207 for (int i = 0; i < aSize; i++) {
208 aObj = aDoc->object(ModelAPI_ResultConstruction::group(), i);
209 if (aObj->data()->name() == "Origin") {
210 aOrigin = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
215 thePrs->setObject(aOrigin);
216 thePrs->setShape(aOrigin->shape());
222 /// find axis direction
223 Handle(AIS_TrihedronOwner) aTrihedronOwner = Handle(AIS_TrihedronOwner)::DownCast(theOwner);
224 if (!aTrihedronOwner.IsNull()) {
225 const Prs3d_DatumParts& aPart = aTrihedronOwner->DatumPart();
228 case Prs3d_DP_Origin: aName = "Origin"; break;
229 case Prs3d_DP_XAxis: aName = "OX"; break;
230 case Prs3d_DP_YAxis: aName = "OY"; break;
231 case Prs3d_DP_ZAxis: aName = "OZ"; break;
234 if (aName.length() > 0) {
235 DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
236 int aSize = aDoc->size(ModelAPI_ResultConstruction::group());
239 for (int i = 0; i < aSize; i++) {
240 aObj = aDoc->object(ModelAPI_ResultConstruction::group(), i);
241 if (aObj->data()->name() == aName) {
242 aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
247 thePrs->setObject(aResult);
248 thePrs->setShape(aResult->shape());
257 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
258 ObjectPtr aFeature = aDisplayer->getObject(anIO);
260 Handle(ModuleBase_BRepOwner) aCompSolidBRO = Handle(ModuleBase_BRepOwner)::DownCast(theOwner);
261 if (!aCompSolidBRO.IsNull()) {
262 // If ModuleBase_BRepOwner object is created then it means that TopAbs_COMPSOLID selection mode
263 // is On and we have to use parent result which corresponds to the CompSolid shape
264 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aFeature);
266 ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(aResult);
267 if (aCompSolid.get()) {
268 GeomShapePtr aShape = aCompSolid->shape();
269 if (aShape.get() && aShape->isEqual(thePrs->shape())) {
270 thePrs->setObject(aCompSolid);
276 thePrs->setObject(aFeature);
279 QList<ModuleBase_ViewerPrsPtr> XGUI_Selection::getHighlighted() const
281 QList<ModuleBase_ViewerPrsPtr> aPresentations;
282 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
283 if (aContext.IsNull())
284 return aPresentations;
286 QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
287 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
288 for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
289 Handle(SelectMgr_EntityOwner) anOwner = aContext->DetectedOwner();
290 if (!anOwner.IsNull()) {
291 if (aSelectedIds.contains((long)anOwner.get()))
293 aSelectedIds.append((long)anOwner.get());
295 ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs());
296 fillPresentation(aPrs, anOwner);
297 aPresentations.push_back(aPrs);
300 return aPresentations;
303 QObjectPtrList XGUI_Selection::selectedObjects() const
305 if (myWorkshop->objectBrowser())
306 return myWorkshop->objectBrowser()->selectedObjects();
307 return QObjectPtrList();
310 void XGUI_Selection::setSelectedObjects( const QObjectPtrList& theObjects ) const
312 return myWorkshop->objectBrowser()->setObjectsSelected( theObjects );
315 QObjectPtrList XGUI_Selection::selectedPresentations() const
317 QObjectPtrList aSelectedList;
319 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
320 if (!aContext.IsNull()) {
321 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
322 Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
323 ObjectPtr aResult = myWorkshop->displayer()->getObject(anIO);
325 aSelectedList.append(aResult);
328 return aSelectedList;
331 //**************************************************************
332 QModelIndexList XGUI_Selection::selectedIndexes() const
334 return myWorkshop->objectBrowser()->selectedIndexes();
337 //**************************************************************
338 void XGUI_Selection::selectedAISObjects(AIS_ListOfInteractive& theList) const
342 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
343 if (!aContext.IsNull()) {
344 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
345 theList.Append(aContext->SelectedInteractive());
349 //**************************************************************
350 ObjectPtr XGUI_Selection::getSelectableObject(const Handle(SelectMgr_EntityOwner)& theOwner) const
354 Handle(SelectMgr_EntityOwner) aEO = theOwner;
356 Handle(AIS_InteractiveObject) anObj =
357 Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
358 anObject = myWorkshop->displayer()->getObject(anObj);
363 //**************************************************************
364 void XGUI_Selection::selectedOwners(SelectMgr_IndexedMapOfOwner& theSelectedOwners) const
366 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
367 if (!aContext.IsNull()) {
368 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
369 theSelectedOwners.Add(aContext->SelectedOwner());
374 //**************************************************************
375 void XGUI_Selection::entityOwners(const Handle(AIS_InteractiveObject)& theObject,
376 SelectMgr_IndexedMapOfOwner& theOwners) const
378 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
379 if (aContext.IsNull() || theObject.IsNull())
382 TColStd_ListOfInteger aModes;
383 aContext->ActivatedModes(theObject, aModes);
385 TColStd_ListIteratorOfListOfInteger anIt(aModes);
386 for (; anIt.More(); anIt.Next()) {
387 int aMode = anIt.Value();
388 if (!theObject->HasSelection(aMode))
391 Handle(SelectMgr_Selection) aSelection = theObject->Selection(aMode);
392 for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
393 Handle(SelectMgr_SensitiveEntity) anEntity = aSelection->Sensitive();
394 if (anEntity.IsNull())
396 Handle(SelectMgr_EntityOwner) anOwner =
397 Handle(SelectMgr_EntityOwner)::DownCast(anEntity->BaseSensitive()->OwnerId());
398 if (!anOwner.IsNull())
399 theOwners.Add(anOwner);
404 //**************************************************************
405 #ifdef BEFORE_TRIHEDRON_PATCH
406 TopoDS_Shape XGUI_Selection::findAxisShape(Handle(AIS_InteractiveObject) theIO) const
409 // Fill by trihedron shapes
410 Handle(AIS_Axis) aAxis = Handle(AIS_Axis)::DownCast(theIO);
411 if (!aAxis.IsNull()) {
412 // an Axis from Trihedron
413 Handle(Geom_Line) aLine = aAxis->Component();
414 Handle(Prs3d_DatumAspect) DA = aAxis->Attributes()->DatumAspect();
415 Handle(Geom_TrimmedCurve) aTLine = new Geom_TrimmedCurve(aLine, 0, DA->FirstAxisLength());
417 BRep_Builder aBuilder;
419 aBuilder.MakeEdge(aEdge, aTLine, Precision::Confusion());
423 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast(theIO);
424 if (!aPoint.IsNull()) {
425 // A point from trihedron
426 Handle(Geom_Point) aPnt = aPoint->Component();
427 BRep_Builder aBuilder;
428 TopoDS_Vertex aVertex;
429 aBuilder.MakeVertex(aVertex, aPnt->Pnt(), Precision::Confusion());
430 if (!aVertex.IsNull())