]> SALOME platform Git repositories - modules/smesh.git/blob - src/SMESHGUI/SMESHGUI_Operation.cxx
Salome HOME
new operation for SMESHGUI_InitMeshDlg
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_Operation.cxx
1 //  SALOME SMESHGUI
2 //
3 //  Copyright (C) 2005  CEA/DEN, EDF R&D
4 //
5 //
6 //
7 //  File   : SMESHGUI_Operation.h
8 //  Author : Sergey LITONIN
9 //  Module : SALOME
10
11 #include "SMESHGUI_Operation.h"
12 #include "SMESHGUI.h"
13 #include "SMESHGUI_VTKUtils.h"
14 #include "SMESHGUI_Selection.h"
15
16 #include <SVTK_ViewWindow.h>
17 #include <SVTK_Selector.h>
18 #include <SUIT_MessageBox.h>
19 #include <SUIT_Desktop.h>
20
21 #include <SalomeApp_Study.h>
22 #include <SalomeApp_VTKSelector.h>
23 #include <SalomeApp_SelectionMgr.h>
24
25 #include <SALOMEDS_SObject.hxx>
26
27 #include <SMESHDS_Mesh.hxx>
28
29 /*
30   Class       : SMESHGUI_Operation
31   Description : Base class for all SMESH operations
32 */
33
34 //=======================================================================
35 // name    : SMESHGUI_Operation
36 // Purpose : Constructor
37 //=======================================================================
38 SMESHGUI_Operation::SMESHGUI_Operation( SalomeApp_Application* app )
39 : SalomeApp_Operation( app )
40 {
41   
42 }
43
44 SMESHGUI_Operation::~SMESHGUI_Operation()
45 {
46   
47 }
48
49 //=======================================================================
50 // name    : setSelectionMode
51 // Purpose : Set selection mode
52 //=======================================================================
53 void SMESHGUI_Operation::setSelectionMode( const Selection_Mode mode )
54 {
55   SVTK_ViewWindow* wnd = viewWindow();
56   if( wnd )
57     wnd->SetSelectionMode( mode );
58 }
59
60 //=======================================================================
61 // name    : highlight
62 // Purpose : Highlight object in 3d viewer
63 //=======================================================================
64 void SMESHGUI_Operation::highlight( const Handle( SALOME_InteractiveObject )& obj,
65                                     const bool hilight, const bool immediately )
66 {
67   SVTK_ViewWindow* wnd = viewWindow();
68   if( wnd )
69     wnd->highlight( obj, hilight, immediately );
70 }
71
72 //=======================================================================
73 // name    : addOrRemoveIndex
74 // Purpose : Select/deselect cells of mesh
75 //=======================================================================
76 void SMESHGUI_Operation::addOrRemoveIndex( const Handle( SALOME_InteractiveObject )& obj,
77                                            const TColStd_MapOfInteger& indices,
78                                            const bool isModeShift )
79 {
80   SVTK_Selector* sel = selector();
81   if( sel )
82     sel->AddOrRemoveIndex( obj, indices, isModeShift );
83 }
84
85 //=======================================================================
86 // name    : getSMESHGUI
87 // Purpose : Get SMESH module
88 //=======================================================================
89 SMESHGUI* SMESHGUI_Operation::getSMESHGUI() const
90 {
91   return dynamic_cast<SMESHGUI*>( module() );
92 }
93
94 //=======================================================================
95 // name    : viewWindow
96 // Purpose : Get active view window
97 //=======================================================================
98 SVTK_ViewWindow* SMESHGUI_Operation::viewWindow() const
99 {
100   return SMESH::GetViewWindow( getSMESHGUI() );
101 }
102
103 //=======================================================================
104 // name    : selector
105 // Purpose : Get selector
106 //=======================================================================
107 SVTK_Selector* SMESHGUI_Operation::selector() const
108 {
109   SVTK_ViewWindow* wnd = viewWindow();
110   return wnd ? wnd->GetSelector() : 0;
111 }
112
113 //=======================================================================
114 // name    : startOperation
115 // Purpose : Start opeartion
116 //=======================================================================
117 void SMESHGUI_Operation::startOperation()
118 {
119   SalomeApp_Operation::startOperation();
120 }
121
122 //=======================================================================
123 // name    : isReadyToStart
124 // Purpose : Verify whether operation is ready to start
125 //=======================================================================
126 bool SMESHGUI_Operation::isReadyToStart()
127 {
128   if ( !SalomeApp_Operation::isReadyToStart() )
129     return false;
130     
131   if ( getSMESHGUI() == 0 )
132   {
133     SUIT_MessageBox::warn1( SMESHGUI::desktop(), tr( "SMESH_WRN_WARNING" ),
134       tr( "NO_MODULE" ), tr( "SMESH_BUT_OK" ) );
135     return false;
136   }
137
138   return true;
139 }
140
141 //=======================================================================
142 // name    : typeById
143 // Purpose : Find type by id
144 //=======================================================================
145 int SMESHGUI_Operation::typeById( const QString& str, const SelectedObjectType objtype ) const
146 {
147   SalomeApp_Study* _study = dynamic_cast<SalomeApp_Study*>( study() );
148   if( !_study )
149     return -1;
150
151   _PTR( Study ) st = _study->studyDS();
152
153   int res = -1;
154   if( objtype == Object )
155   {
156     SalomeApp_Study* _study = dynamic_cast<SalomeApp_Study*>( study() );
157     if( _study )
158     {
159       int t = SMESHGUI_Selection::type( str, _study->studyDS() );
160       if( t<0 )
161       {
162         //try to get GEOM type
163         _PTR( SObject ) sobj = st->FindObjectID( str.latin1() );
164         if( sobj )
165         {
166           GEOM::GEOM_Object_var obj = GEOM::GEOM_Object::_narrow( dynamic_cast<SALOMEDS_SObject*>( sobj.get() )->GetObject() );
167           if( !CORBA::is_nil( obj ) )
168             res = prefix( "GEOM" ) + obj->GetType();
169         }
170       }
171       else
172         res = prefix( "SMESH" ) + t;
173     }
174   }
175   else  
176   {
177     int pos = str.find( idChar() );
178     QString entry = str.left( pos-1 ),
179             _id = str.mid( pos+1 );
180     bool ok;
181     int id = _id.toInt( &ok );
182     if( ok )
183     {
184       _PTR( SObject ) sobj = st->FindObjectID( entry.latin1() );
185       SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( dynamic_cast<SALOMEDS_SObject*>( sobj.get() )->GetObject() );
186       SMESH::SMESH_subMesh_var submesh = SMESH::SMESH_subMesh::_narrow( dynamic_cast<SALOMEDS_SObject*>( sobj.get() )->GetObject() );
187       if( !CORBA::is_nil( mesh ) )
188         res = prefix( "SMESH element" ) + mesh->GetElementType( id, objtype==MeshElement );
189         
190       else if( !CORBA::is_nil( submesh ) )
191         res = prefix( "SMESH element" ) + submesh->GetElementType( id, objtype==MeshElement );
192     }
193   }
194
195   return res;
196 }
197
198 //=======================================================================
199 // name    : prefix
200 // Purpose : Get prefix for module types
201 //=======================================================================
202 int SMESHGUI_Operation::prefix( const QString& name ) const
203 {
204   if( name == "GEOM" )
205     return 100;
206   else if( name == "SMESH" )
207     return 200;
208   else if( name == "SMESH element" )
209     return 300;
210   else
211     return 0;
212 }
213
214 //=======================================================================
215 // name    : selected
216 // Purpose : Get names, types and ids of selected objects
217 //=======================================================================
218 void SMESHGUI_Operation::selected( QStringList& names, SalomeApp_Dialog::TypesList& types, QStringList& ids ) const
219 {
220   SUIT_DataOwnerPtrList list; selectionMgr()->selected( list );
221   SUIT_DataOwnerPtrList::const_iterator anIt = list.begin(),
222                                         aLast = list.end();
223   for( ; anIt!=aLast; anIt++ )
224   {
225     SalomeApp_DataOwner* owner = dynamic_cast<SalomeApp_DataOwner*>( (*anIt).operator->() );
226     SalomeApp_SVTKDataOwner* vtkowner = dynamic_cast<SalomeApp_SVTKDataOwner*>( (*anIt).operator->() );
227
228     if( vtkowner )
229     {
230       QString id_str = QString( "%1%2%3" ).arg( vtkowner->entry() ).arg( idChar() ), current_id_str;
231       Selection_Mode mode = vtkowner->GetMode();
232       SelectedObjectType objtype = mode == NodeSelection ? MeshNode : MeshElement;
233       const TColStd_IndexedMapOfInteger& ownerids = vtkowner->GetIds();
234
235       for( int i=1, n=ownerids.Extent(); i<=n; i++ )
236       {
237         int curid = ownerids( i );
238         current_id_str = id_str.arg( curid );
239         ids.append( current_id_str );
240         types.append( typeById( current_id_str, objtype ) );
241         names.append( QString( "%1" ).arg( curid ) );
242       }
243     }
244
245     else if( owner )
246     {
247       QString id = owner->entry();
248       ids.append( id );
249       types.append( typeById( id, Object ) );
250       names.append( owner->IO()->getName() );
251     }
252   }
253 }
254
255 //=======================================================================
256 // name    : idChar
257 // Purpose : Char using to divide <entry> and <id> in string id representation. By default, '#'
258 //=======================================================================
259 QChar SMESHGUI_Operation::idChar() const
260 {
261   return '#';
262 }