1 // Copyright (C) 2013-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 // File : HEXABLOCKGUI_OCCSelector.cxx
23 #include "HEXABLOCKGUI_OCCSelector.h"
24 #include "HEXABLOCKGUI_SalomeTools.hxx"
26 #include <LightApp_DataSubOwner.h>
28 #include <OCCViewer_ViewModel.h>
30 #include <SUIT_Session.h>
31 #include <SalomeApp_Study.h>
32 #include <SALOME_InteractiveObject.hxx>
34 #include <StdSelect_BRepOwner.hxx>
35 #include <SelectMgr_Selection.hxx>
36 #include <SelectMgr_EntityOwner.hxx>
37 #include <SelectMgr_IndexedMapOfOwner.hxx>
38 #include <SelectBasics_SensitiveEntity.hxx>
40 #include <AIS_Shape.hxx>
41 #include <AIS_ListOfInteractive.hxx>
42 #include <AIS_ListIteratorOfListOfInteractive.hxx>
44 #include <TopTools_IndexedMapOfShape.hxx>
45 #include <TColStd_ListIteratorOfListOfInteger.hxx>
46 #include <TColStd_IndexedMapOfInteger.hxx>
47 #include <NCollection_DataMap.hxx>
49 #include <Basics_OCCTVersion.hxx>
51 using namespace HEXABLOCK::GUI;
53 //================================================================
54 // Function : HEXABLOCKGUI_OCCSelector
56 //================================================================
57 HEXABLOCKGUI_OCCSelector::HEXABLOCKGUI_OCCSelector( OCCViewer_Viewer* viewer, SUIT_SelectionMgr* mgr )
58 : LightApp_OCCSelector( viewer, mgr )
62 //================================================================
63 // Function : ~HEXABLOCKGUI_OCCSelector
65 //================================================================
66 HEXABLOCKGUI_OCCSelector::~HEXABLOCKGUI_OCCSelector()
70 //================================================================
71 // Function : getSelection
73 //================================================================
74 void HEXABLOCKGUI_OCCSelector::getSelection( SUIT_DataOwnerPtrList& aList ) const
76 OCCViewer_Viewer* vw = viewer();
80 Handle(AIS_InteractiveContext) ic = vw->getAISContext();
81 #if OCC_VERSION_LARGE <= 0x07030000
82 if (ic->HasOpenedContext())
85 TopoDS_Shape curBigShape;
86 TopTools_IndexedMapOfShape subShapes;
88 for (ic->InitSelected(); ic->MoreSelected(); ic->NextSelected())
90 Handle(AIS_InteractiveObject) io = Handle(AIS_InteractiveObject)::DownCast(ic->SelectedInteractive());
92 QString entryStr = entry(io);
95 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(ic->SelectedOwner());
97 if (!anOwner.IsNull() && anOwner->ComesFromDecomposition()) // == Local Selection
99 TopoDS_Shape subShape = anOwner->Shape();
100 Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast(io);
101 if (!aisShape.IsNull())
103 TopoDS_Shape bigShape = aisShape->Shape();
105 if (!bigShape.IsEqual(curBigShape))
107 curBigShape = bigShape;
109 TopExp::MapShapes(bigShape, subShapes);
111 index = subShapes.FindIndex(subShape);
115 if (!entryStr.isEmpty())
117 Handle(SALOME_InteractiveObject) anIO = Handle(SALOME_InteractiveObject)::DownCast(io->GetOwner());
118 LightApp_DataOwner* owner;
119 if (index > -1) // Local Selection
120 owner = new LightApp_DataSubOwner (entryStr, index);
121 else if ( !anIO.IsNull() ) // Global Selection
122 owner = new LightApp_DataOwner( anIO );
124 aList.append(SUIT_DataOwnerPtr(owner));
127 #if OCC_VERSION_LARGE <= 0x07030000
131 for (ic->InitCurrent(); ic->MoreCurrent(); ic->NextCurrent())
133 Handle(AIS_InteractiveObject) io = ic->Current();
135 QString entryStr = entry( io );
137 if ( !entryStr.isEmpty() )
139 Handle(SALOME_InteractiveObject) anIO = Handle(SALOME_InteractiveObject)::DownCast(io->GetOwner());
140 if ( !anIO.IsNull() ) {
141 LightApp_DataOwner* owner = new LightApp_DataOwner( anIO );
142 aList.append( SUIT_DataOwnerPtr( owner ) );
149 // add externally selected objects
150 SUIT_DataOwnerPtrList::const_iterator anExtIter, itEnd;
151 for (anExtIter = mySelectedExternals.begin(), itEnd = mySelectedExternals.end(); anExtIter != itEnd; anExtIter++)
153 aList.append(*anExtIter);
157 //================================================================
158 // Function : setSelection
160 //================================================================
161 void HEXABLOCKGUI_OCCSelector::setSelection( const SUIT_DataOwnerPtrList& aList )
163 OCCViewer_Viewer* vw = viewer();
167 Handle(AIS_InteractiveContext) ic = vw->getAISContext();
169 // "entry - list_of_int" map for LOCAL selection
170 NCollection_DataMap<TCollection_AsciiString, TColStd_IndexedMapOfInteger> indexesMap;
172 QMap<QString,int> globalSelMap; // only Key=entry from this map is used. value(int) is NOT used at all.
173 SelectMgr_IndexedMapOfOwner ownersmap; // map of owners to be selected
175 AIS_ListOfInteractive aDispList;
176 ic->DisplayedObjects( aDispList );
178 // build a map of data owner indexes to be selected.
179 // "entry - to - list_of_ids" map
180 for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(), itEnd = aList.end(); itr != itEnd; ++itr )
182 const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( (*itr).operator->() );
185 QString entry = subOwner->entry();
186 if ( indexesMap.IsBound( TCollection_AsciiString(entry.toLatin1().data())))
188 TColStd_IndexedMapOfInteger& subIndexes = indexesMap.ChangeFind(entry.toLatin1().data());
189 subIndexes.Add( subOwner->index() );
190 //indexesMap.replace( entry, subIndexes );
194 TColStd_IndexedMapOfInteger subIndexes;
195 subIndexes.Add( subOwner->index() );
196 indexesMap.Bind(entry.toLatin1().data(), subIndexes);
199 else // the owner is NOT a sub owner, maybe it is a DataOwner == GLOBAL selection
201 const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
204 SalomeApp_Study* appStudy =
205 dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
206 QString anEntry = appStudy->referencedToEntry( owner->entry() );
208 globalSelMap[anEntry] = 1;
213 // get all owners. Fill "entry - list_of_owners" map.
214 for ( AIS_ListIteratorOfListOfInteractive it( aDispList ); it.More(); it.Next() )
216 Handle(AIS_InteractiveObject) io = it.Value();
217 QString entryStr = entry( io );
218 if ( !entryStr.isEmpty() )
220 //EntryToListOfOwnerMap entryOwnersMap; // "entry - list_of_owners" map. temporary use.
221 SelectMgr_IndexedMapOfOwner owners;
222 getEntityOwners( io, ic, owners ); // get all owners
224 int i, n = owners.Extent();
226 // 1. Prepare map of shapes for local selection
227 TopTools_IndexedMapOfShape aMapOfShapes;
228 bool isLocal = false;
230 Handle(StdSelect_BRepOwner) anOwner;
231 for (i = 1; i <= n && !isLocal; i++)
233 anOwner = Handle(StdSelect_BRepOwner)::DownCast(owners( i ));
234 if (!anOwner.IsNull() && anOwner->HasShape())
236 if (anOwner->ComesFromDecomposition() || !globalSelMap.contains(entryStr))
238 // has a local selection
239 Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast( io );
240 if (!aisShape.IsNull() && indexesMap.IsBound(entryStr.toLatin1().data()))
243 TopoDS_Shape shape = aisShape->Shape();
244 aMapOfShapes.Clear();
245 TopExp::MapShapes(shape, aMapOfShapes);
251 // 2. Process all owners
252 for (i = 1; i <= n; i++)
254 anOwner = Handle(StdSelect_BRepOwner)::DownCast(owners( i ));
256 if ( anOwner.IsNull() || !anOwner->HasShape() )
260 if ( !anOwner->ComesFromDecomposition() && globalSelMap.contains( entryStr ) )
262 ownersmap.Add( anOwner );
263 globalSelMap[entryStr]++;
270 const TColStd_IndexedMapOfInteger& subIndexes =
271 indexesMap.ChangeFind(entryStr.toLatin1().data());
273 const TopoDS_Shape& aSubShape = anOwner->Shape();
274 int aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
276 // check if the "sub_shape_index" is found in the "map of indexes for this entry",
277 // which was passes in the parameter
278 if ( subIndexes.Contains( aSubShapeId ) )
280 ownersmap.Add( anOwner );
283 } // end of LOCAL selection
284 } // end of for(owners)
285 } // end of if(entry)
286 } // end of for(AIS_all_ios)
288 vw->unHighlightAll( false );
291 int i = 1, n = ownersmap.Extent();
292 bool isAutoHilight = ic->AutomaticHilight();
293 ic->SetAutomaticHilight(Standard_False); //Bug 17269: for better performance
296 Handle(SelectMgr_EntityOwner) owner = ownersmap( i );
297 if ( owner->State() )
300 if ( owner->ComesFromDecomposition() )
301 ic->AddOrRemoveSelected( owner, false );
303 ic->AddOrRemoveSelected( Handle(AIS_InteractiveObject)::DownCast(owner->Selectable()), false );
305 ic->SetAutomaticHilight(isAutoHilight); //Bug 17269: restore mode
307 ic->HilightSelected(/*updateviewer*/Standard_True);
311 // fill extra selected
312 mySelectedExternals.clear();
313 for ( SUIT_DataOwnerPtrList::const_iterator itr2 = aList.begin(), itEnd = aList.end(); itr2 != itEnd; ++itr2 )
315 const LightApp_DataSubOwner* subOwner =
316 dynamic_cast<const LightApp_DataSubOwner*>( (*itr2).operator->() );
319 const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr2).operator->() );
322 SalomeApp_Study* appStudy =
323 dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
324 QString anEntry = appStudy->referencedToEntry( owner->entry() );
325 if (globalSelMap[anEntry] == 1) mySelectedExternals.append(*itr2);