Back to index

salome-geom  6.5.0
GEOMGUI_OCCSelector.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // File   : GEOMGUI_OCCSelector.cxx
00024 // Author : Alexander SOLOVYOV, Open CASCADE S.A.S. (alexander.solovyov@opencascade.com)
00025 //
00026 #include "GEOMGUI_OCCSelector.h"
00027 
00028 #include <LightApp_DataSubOwner.h>
00029 
00030 #include <OCCViewer_ViewModel.h>
00031 
00032 #include <SUIT_Session.h>
00033 #include <SalomeApp_Study.h>
00034 #include <SALOME_InteractiveObject.hxx>
00035 
00036 #include <StdSelect_BRepOwner.hxx>
00037 #include <SelectMgr_Selection.hxx>
00038 #include <SelectMgr_EntityOwner.hxx>
00039 #include <SelectMgr_IndexedMapOfOwner.hxx>
00040 #include <SelectBasics_SensitiveEntity.hxx>
00041 
00042 #include <AIS_Shape.hxx>
00043 #include <AIS_ListOfInteractive.hxx>
00044 #include <AIS_ListIteratorOfListOfInteractive.hxx>
00045 #include <TopExp.hxx>
00046 #include <TopTools_IndexedMapOfShape.hxx>
00047 #include <TColStd_ListIteratorOfListOfInteger.hxx>
00048 #include <TColStd_IndexedMapOfInteger.hxx>
00049 #include <NCollection_DataMap.hxx>
00050 
00051 
00052 //================================================================
00053 // Function : GEOMGUI_OCCSelector
00054 // Purpose  :
00055 //================================================================
00056 GEOMGUI_OCCSelector::GEOMGUI_OCCSelector( OCCViewer_Viewer* viewer, SUIT_SelectionMgr* mgr )
00057 : LightApp_OCCSelector( viewer, mgr )
00058 {
00059 }
00060 
00061 //================================================================
00062 // Function : ~GEOMGUI_OCCSelector
00063 // Purpose  :
00064 //================================================================
00065 GEOMGUI_OCCSelector::~GEOMGUI_OCCSelector()
00066 {
00067 }
00068 
00069 //================================================================
00070 // Function : getSelection
00071 // Purpose  :
00072 //================================================================
00073 void GEOMGUI_OCCSelector::getSelection( SUIT_DataOwnerPtrList& aList ) const
00074 {
00075   OCCViewer_Viewer* vw = viewer();
00076   if (!vw)
00077     return;
00078 
00079   Handle(AIS_InteractiveContext) ic = vw->getAISContext();
00080 
00081   if (ic->HasOpenedContext())
00082   {
00083     TopoDS_Shape curBigShape;
00084     TopTools_IndexedMapOfShape subShapes;
00085 
00086     for (ic->InitSelected(); ic->MoreSelected(); ic->NextSelected())
00087     {
00088       Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(ic->SelectedOwner());
00089       if (anOwner.IsNull())
00090         continue;
00091 
00092       Handle(AIS_InteractiveObject) io = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
00093 
00094       QString entryStr = entry(io);
00095       int index = -1;
00096 
00097       if (anOwner->ComesFromDecomposition()) // == Local Selection
00098       {
00099         TopoDS_Shape subShape = anOwner->Shape();
00100         Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast(io);
00101         if (!aisShape.IsNull())
00102         {
00103           TopoDS_Shape bigShape = aisShape->Shape();
00104 
00105           if (!bigShape.IsEqual(curBigShape))
00106           {
00107             curBigShape = bigShape;
00108             subShapes.Clear();
00109             TopExp::MapShapes(bigShape, subShapes);
00110           }
00111           index = subShapes.FindIndex(subShape);
00112         }
00113       }
00114 
00115       if (!entryStr.isEmpty())
00116       {
00117         Handle(SALOME_InteractiveObject) anIO = Handle(SALOME_InteractiveObject)::DownCast(io->GetOwner()); 
00118         LightApp_DataOwner* owner;
00119         if (index > -1) // Local Selection
00120           owner = new LightApp_DataSubOwner (entryStr, index);
00121         else if ( !anIO.IsNull() ) // Global Selection
00122           owner = new LightApp_DataOwner( anIO );
00123 
00124         aList.append(SUIT_DataOwnerPtr(owner));
00125       }
00126     }
00127   }
00128   else
00129   {
00130     for (ic->InitCurrent(); ic->MoreCurrent(); ic->NextCurrent())
00131     {
00132       Handle(AIS_InteractiveObject) io = ic->Current();
00133 
00134       QString entryStr = entry( io );
00135 
00136       if ( !entryStr.isEmpty() )
00137       {
00138         Handle(SALOME_InteractiveObject) anIO = Handle(SALOME_InteractiveObject)::DownCast(io->GetOwner()); 
00139         if ( !anIO.IsNull() ) {
00140           LightApp_DataOwner* owner = new LightApp_DataOwner( anIO );
00141           aList.append( SUIT_DataOwnerPtr( owner ) );
00142         }
00143       }
00144     }
00145   }
00146 
00147   // add externally selected objects
00148   SUIT_DataOwnerPtrList::const_iterator anExtIter;
00149   for (anExtIter = mySelectedExternals.begin(); anExtIter != mySelectedExternals.end(); anExtIter++)
00150   {
00151     aList.append(*anExtIter);
00152   }
00153 }
00154 
00155 //================================================================
00156 // Function : getEntityOwners
00157 // Purpose  :
00158 //================================================================
00159 static void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
00160                              const Handle(AIS_InteractiveContext)& theIC,
00161                              SelectMgr_IndexedMapOfOwner& theMap )
00162 {
00163   if ( theObj.IsNull() || theIC.IsNull() )
00164     return;
00165 
00166   TColStd_ListOfInteger modes;
00167   theIC->ActivatedModes( theObj, modes );
00168 
00169   TColStd_ListIteratorOfListOfInteger itr( modes );
00170   for (; itr.More(); itr.Next() ) {
00171     int m = itr.Value();
00172     if ( !theObj->HasSelection( m ) )
00173       continue;
00174 
00175     Handle(SelectMgr_Selection) sel = theObj->Selection( m );
00176 
00177     for ( sel->Init(); sel->More(); sel->Next() ) {
00178       Handle(SelectBasics_SensitiveEntity) entity = sel->Sensitive();
00179       if ( entity.IsNull() )
00180         continue;
00181 
00182       Handle(SelectMgr_EntityOwner) owner =
00183         Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
00184       if ( !owner.IsNull() )
00185         theMap.Add( owner );
00186     }
00187   }
00188 }
00189 
00190 //================================================================
00191 // Function : setSelection
00192 // Purpose  :
00193 //================================================================
00194 void GEOMGUI_OCCSelector::setSelection( const SUIT_DataOwnerPtrList& aList )
00195 {
00196   OCCViewer_Viewer* vw = viewer();
00197   if ( !vw )
00198     return;
00199 
00200   Handle(AIS_InteractiveContext) ic = vw->getAISContext();
00201 
00202   // "entry - list_of_int" map for LOCAL selection
00203   NCollection_DataMap<TCollection_AsciiString, TColStd_IndexedMapOfInteger> indexesMap;
00204 
00205   QMap<QString,int> globalSelMap; // only Key=entry from this map is used.  value(int) is NOT used at all.
00206   SelectMgr_IndexedMapOfOwner ownersmap; // map of owners to be selected
00207 
00208   AIS_ListOfInteractive aDispList;
00209   ic->DisplayedObjects( aDispList );
00210 
00211   // build a map of data owner indexes to be selected.
00212   // "entry - to - list_of_ids" map
00213   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
00214   {
00215     const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( (*itr).operator->() );
00216     if ( subOwner )
00217     {
00218       QString entry = subOwner->entry();
00219       if ( indexesMap.IsBound( TCollection_AsciiString(entry.toLatin1().data())))
00220       {
00221         TColStd_IndexedMapOfInteger& subIndexes = indexesMap.ChangeFind(entry.toLatin1().data());
00222         subIndexes.Add( subOwner->index() );
00223         //indexesMap.replace( entry, subIndexes );
00224       }
00225       else
00226       {
00227         TColStd_IndexedMapOfInteger subIndexes;
00228         subIndexes.Add( subOwner->index() );
00229         indexesMap.Bind(entry.toLatin1().data(), subIndexes);
00230       }
00231     }
00232     else // the owner is NOT a sub owner, maybe it is a DataOwner == GLOBAL selection
00233     {
00234       const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
00235       if ( owner )
00236       {
00237         SalomeApp_Study* appStudy =
00238           dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
00239         QString anEntry = appStudy->referencedToEntry( owner->entry() );
00240 
00241         globalSelMap[anEntry] = 1;
00242       }
00243     }
00244   }
00245 
00246   // get all owners.  Fill "entry - list_of_owners" map.
00247   for ( AIS_ListIteratorOfListOfInteractive it( aDispList ); it.More(); it.Next() )
00248   {
00249     Handle(AIS_InteractiveObject) io = it.Value();
00250     QString entryStr = entry( io );
00251     if ( !entryStr.isEmpty() )
00252     {
00253       //EntryToListOfOwnerMap entryOwnersMap; // "entry - list_of_owners" map.  temporary use.
00254       SelectMgr_IndexedMapOfOwner owners;
00255       getEntityOwners( io, ic, owners ); // get all owners
00256 
00257       int i, n = owners.Extent();
00258 
00259       // 1. Prepare map of shapes for local selection
00260       TopTools_IndexedMapOfShape aMapOfShapes;
00261       bool isLocal = false;
00262 
00263       Handle(StdSelect_BRepOwner) anOwner;
00264       for (i = 1; i <= n && !isLocal; i++)
00265       {
00266         anOwner = Handle(StdSelect_BRepOwner)::DownCast(owners( i ));
00267         if (!anOwner.IsNull() && anOwner->HasShape())
00268         {
00269           if (anOwner->ComesFromDecomposition() || !globalSelMap.contains(entryStr))
00270           {
00271             // has a local selection
00272             Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast( io );
00273             if (!aisShape.IsNull() && indexesMap.IsBound(entryStr.toLatin1().data()))
00274             {
00275               isLocal = true;
00276               TopoDS_Shape shape = aisShape->Shape();
00277               aMapOfShapes.Clear();
00278               TopExp::MapShapes(shape, aMapOfShapes);
00279             }
00280           }
00281         }
00282       }
00283 
00284       // 2. Process all owners
00285       for (i = 1; i <= n; i++)
00286       {
00287         anOwner = Handle(StdSelect_BRepOwner)::DownCast(owners( i ));
00288 
00289         if ( anOwner.IsNull() || !anOwner->HasShape() )
00290           continue;
00291 
00292         // GLOBAL selection
00293         if ( !anOwner->ComesFromDecomposition() && globalSelMap.contains( entryStr ) )
00294         {
00295           ownersmap.Add( anOwner );
00296           globalSelMap[entryStr]++;
00297         }
00298         // LOCAL selection
00299         else
00300         {
00301           if (isLocal)
00302           {
00303             const TColStd_IndexedMapOfInteger& subIndexes =
00304               indexesMap.ChangeFind(entryStr.toLatin1().data());
00305 
00306             const TopoDS_Shape& aSubShape = anOwner->Shape();
00307             int aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
00308 
00309             // check if the "sub_shape_index" is found in the "map of indexes for this entry",
00310             // which was passes in the parameter
00311             if ( subIndexes.Contains( aSubShapeId ) )
00312             {
00313               ownersmap.Add( anOwner );
00314             }
00315           }
00316         } // end of LOCAL selection
00317       } // end of for(owners)
00318     } // end of if(entry)
00319   } // end of for(AIS_all_ios)
00320 
00321   vw->unHighlightAll( false );
00322 
00323   // DO the selection
00324   int i = 1, n = ownersmap.Extent();
00325   bool isAutoHilight = ic->AutomaticHilight();
00326   ic->SetAutomaticHilight(Standard_False); //Bug 17269: for better performance
00327   for  (; i <= n; i++)
00328   {
00329     Handle(SelectMgr_EntityOwner) owner = ownersmap( i );
00330     if ( owner->State() )
00331       continue;
00332 
00333     if ( ic->HasOpenedContext() )
00334       ic->AddOrRemoveSelected( owner, false );
00335     else
00336       ic->AddOrRemoveSelected( Handle(AIS_InteractiveObject)::DownCast(owner->Selectable()), false );
00337   }
00338   ic->SetAutomaticHilight(isAutoHilight); //Bug 17269: restore mode
00339   if (n < 3000)
00340     ic->HilightSelected(/*updateviewer*/Standard_True);
00341   else
00342     vw->update();
00343 
00344   // fill extra selected
00345   mySelectedExternals.clear();
00346   for ( SUIT_DataOwnerPtrList::const_iterator itr2 = aList.begin(); itr2 != aList.end(); ++itr2 )
00347   {
00348     const LightApp_DataSubOwner* subOwner =
00349       dynamic_cast<const LightApp_DataSubOwner*>( (*itr2).operator->() );
00350     if ( !subOwner )
00351     {
00352       const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr2).operator->() );
00353       if ( owner )
00354       {
00355         SalomeApp_Study* appStudy =
00356           dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
00357         QString anEntry = appStudy->referencedToEntry( owner->entry() );
00358         if (globalSelMap[anEntry] == 1) mySelectedExternals.append(*itr2);
00359       }
00360     }
00361   }
00362 }