1 // Copyright (C) 2014-2024 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_SelectionMgr.h"
22 #include "XGUI_Workshop.h"
23 #include "XGUI_ObjectsBrowser.h"
24 #include "XGUI_SalomeConnector.h"
25 #include "XGUI_ViewerProxy.h"
26 #include "XGUI_Displayer.h"
27 #include "XGUI_Selection.h"
28 #include "XGUI_OperationMgr.h"
29 #include "XGUI_SelectionActivate.h"
32 #include <AppElements_MainWindow.h>
35 #include <ModelAPI_Feature.h>
36 #include <ModelAPI_Session.h>
37 #include <ModelAPI_AttributeDocRef.h>
38 #include <ModelAPI_Data.h>
39 #include <ModelAPI_Result.h>
40 #include <ModelAPI_Object.h>
41 #include <ModelAPI_ResultBody.h>
42 #include <ModelAPI_Tools.h>
43 #include <ModelAPI_ResultField.h>
45 #include <GeomAPI_Shape.h>
47 #include <ModuleBase_ViewerPrs.h>
48 #include <ModuleBase_Tools.h>
50 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
51 #include <TopExp_Explorer.hxx>
52 #include <TopoDS_Shape.hxx>
53 #include <TopTools_MapOfShape.hxx>
56 #include <inspector/VInspectorAPI_CallBack.hxx>
60 #pragma warning(disable : 4456) // for nested foreach
63 #define OPTIMIZATION_LEVEL 50
66 XGUI_SelectionMgr::XGUI_SelectionMgr(XGUI_Workshop* theParent)
70 mySelection = new XGUI_Selection(myWorkshop);
73 XGUI_SelectionMgr::~XGUI_SelectionMgr()
78 //**************************************************************
79 void XGUI_SelectionMgr::connectViewers()
81 connect(myWorkshop->objectBrowser(), SIGNAL(selectionChanged()), this,
82 SLOT(onObjectBrowserSelection()));
84 //Connect to other viewers
85 connect(myWorkshop->viewer(), SIGNAL(selectionChanged()), this, SLOT(onViewerSelection()));
88 //**************************************************************
89 void XGUI_SelectionMgr::setSelectedOwners(const SelectMgr_IndexedMapOfOwner& theSelectedOwners,
92 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
93 if (!aContext.IsNull()) {
94 /// previous selection should be cleared, else there will be decomposition of selections:
95 /// as AddOrRemoveSelected inverts current selection
96 aContext->ClearSelected(false);
98 for (Standard_Integer i = 1, n = theSelectedOwners.Extent(); i <= n; i++) {
99 Handle(SelectMgr_EntityOwner) anOwner = theSelectedOwners(i);
101 aContext->AddOrRemoveSelected(anOwner, isUpdateViewer);
103 if (myWorkshop->displayer()->getCallBack())
104 myWorkshop->displayer()->getCallBack()->AddOrRemoveSelected(anOwner);
110 //**************************************************************
111 void XGUI_SelectionMgr::onObjectBrowserSelection()
113 myLastSelectionPlace = ModuleBase_ISelection::Browser;
114 QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
115 myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
116 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
117 if (!myWorkshop->operationMgr()->hasOperation()) {
121 // Select all results of a selected feature in viewer
122 foreach(ModuleBase_ViewerPrsPtr aPrs, aSelectedPrs) {
123 aObject = aPrs->object();
125 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
126 if (aFeature.get()) {
127 std::list<ResultPtr> allRes;
128 ModelAPI_Tools::allResults(aFeature, allRes);
129 std::list<ResultPtr>::iterator aRes;
130 for(aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
131 aSelectedPrs.append(std::shared_ptr<ModuleBase_ViewerPrs>(
132 new ModuleBase_ViewerPrs(*aRes, GeomShapePtr(), NULL)));
138 aDisplayer->setSelected(aSelectedPrs);
139 myWorkshop->updateColorScaleVisibility();
140 emit selectionChanged();
143 //**************************************************************
144 void XGUI_SelectionMgr::onViewerSelection()
146 myLastSelectionPlace = ModuleBase_ISelection::Viewer;
147 QList<ModuleBase_ViewerPrsPtr> aValues;
148 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
149 if (!aContext.IsNull()) {
150 aValues = selection()->getSelected(ModuleBase_ISelection::Viewer);
151 // Update is necessary for OCCT 7.4.0: when it is clears selection it doesn't updates viewer
152 #if OCC_VERSION_HEX == 0x070400
153 if (aValues.isEmpty())
154 aContext->UpdateCurrentViewer();
157 QObjectPtrList anObjects;
158 convertToObjectBrowserSelection(aValues, anObjects);
159 myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
161 emit selectionChanged();
164 //**************************************************************
165 void XGUI_SelectionMgr::deselectPresentation(const Handle(AIS_InteractiveObject) theObject)
167 NCollection_List<Handle(SelectBasics_EntityOwner)> aResultOwners;
169 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
170 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
171 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
172 if (anOwner.IsNull()) // TODO: check why it is possible
174 if (anOwner->Selectable() == theObject && anOwner->IsSelected())
175 aResultOwners.Append(anOwner);
177 NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (aResultOwners);
178 Handle(SelectMgr_EntityOwner) anOwner;
179 for (; anOwnersIt.More(); anOwnersIt.Next()) {
180 anOwner = anOwnersIt.Value();
181 if (!anOwner.IsNull())
182 aContext->AddOrRemoveSelected(anOwner, false);
186 //**************************************************************
187 void XGUI_SelectionMgr::updateSelectionBy(const ModuleBase_ISelection::SelectionPlace& thePlace)
189 QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
190 myWorkshop->selector()->selection()->getSelected(thePlace);
191 if (thePlace == ModuleBase_ISelection::Browser) {
192 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
193 aDisplayer->setSelected(aSelectedPrs);
197 //**************************************************************
198 void XGUI_SelectionMgr::clearSelection()
200 QObjectPtrList aFeatures;
201 myWorkshop->objectBrowser()->setObjectsSelected(aFeatures);
203 QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
204 myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
206 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
207 aDisplayer->setSelected(aSelectedPrs);
209 emit selectionChanged();
211 //**************************************************************
212 void XGUI_SelectionMgr::setSelected(const QList<ModuleBase_ViewerPrsPtr>& theValues)
214 // update selection in Viewer
215 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
216 aDisplayer->setSelected(theValues);
218 // update selection in Object Browser
219 QObjectPtrList anObjects;
220 convertToObjectBrowserSelection(theValues, anObjects);
221 myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
224 //**************************************************************
225 void XGUI_SelectionMgr::convertToObjectBrowserSelection(
226 const QList<ModuleBase_ViewerPrsPtr>& theValues,
227 QObjectPtrList& theObjects)
233 bool aHasOperation = (myWorkshop->operationMgr()->currentOperation() != 0);
234 SessionPtr aMgr = ModelAPI_Session::get();
235 DocumentPtr anActiveDocument = aMgr->activeDocument();
237 TopTools_MapOfShape aShapeMap;
238 bool aToOptimize = (theValues.size() > OPTIMIZATION_LEVEL);
241 TopoDS_Shape aTShape;
242 foreach(ModuleBase_ViewerPrsPtr aPrs, theValues) {
243 if (aPrs->object().get()) {
244 if (!theObjects.contains(aPrs->object()))
245 theObjects.append(aPrs->object());
246 if (aPrs->shape().get() && (!aHasOperation)) {
247 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
249 aShape = aPrs->shape();
250 aTShape = aShape->impl<TopoDS_Shape>();
252 if (!aShapeMap.Contains(aTShape)) {
253 aFeature = anActiveDocument->producedByFeature(aResult, aShape);
254 if (aFeature.get()) {
255 QList<TopoDS_Shape> aResList = findAllShapes(aResult);
256 foreach(TopoDS_Shape aResShape, aResList) {
257 if (!aShapeMap.Contains(aResShape))
258 aShapeMap.Add(aResShape);
264 aFeature = anActiveDocument->producedByFeature(aResult, aShape);
266 if (aFeature.get() && (!theObjects.contains(aFeature)))
267 theObjects.append(aFeature);
274 std::list<FeaturePtr> XGUI_SelectionMgr::getSelectedFeatures()
276 std::list<FeaturePtr> aFeatures;
277 QObjectPtrList aObjects = selection()->selectedObjects();
278 if (aObjects.isEmpty())
282 foreach(ObjectPtr aObj, aObjects) {
283 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
284 if (aFeature.get()) {
285 ResultPtr aRes = aFeature->firstResult();
286 isPart = (aRes.get() && (aRes->groupName() == ModelAPI_ResultPart::group()));
288 aFeatures.push_back(aFeature);
294 QList<TopoDS_Shape> XGUI_SelectionMgr::findAllShapes(const ResultPtr& theResult) const
296 QIntList aModes = myWorkshop->selectionActivate()->activeSelectionModes();
297 GeomShapePtr aResShape = theResult->shape();
298 TopoDS_Shape aShape = aResShape->impl<TopoDS_Shape>();
299 QList<TopoDS_Shape> aResult;
300 foreach(int aShapeType, aModes) {
301 if (aShapeType < TopAbs_SHAPE) {
302 TopExp_Explorer aExp(aShape, (TopAbs_ShapeEnum)aShapeType);
303 for (; aExp.More(); aExp.Next()) {
304 aResult.append(aExp.Current());