Salome HOME
aef9246882819d56becc00b37956215f55f4c8be
[modules/hexablock.git] / src / HEXABLOCKGUI / HEXABLOCKGUI_SalomeTools.cxx
1 // Copyright (C) 2009-2012  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <set>
21
22
23 #include <BRepTools.hxx>
24
25
26 // VTK includes
27 #include <vtkRenderer.h>
28 #include <vtkActorCollection.h>
29 #include <vtkUnstructuredGrid.h>
30
31
32
33 #include <SUIT_Session.h>
34
35 #include <SalomeApp_Study.h>
36 #include <SalomeApp_Application.h>
37 // #include <SALOME_Actor.h>
38 // #include <SALOME_ListIO.hxx>
39 // #include <SALOME_ListIteratorOfListIO.hxx>
40
41
42
43 #include <OCCViewer_ViewWindow.h>
44
45
46 // #include <SALOMEconfig.h>
47 // #include "SALOME_Component_i.hxx"
48 // #include <omniORB4/CORBA.h>
49
50
51 #include <SVTK_ViewManager.h>
52 #include <SVTK_ViewModel.h>
53 #include <SVTK_ViewWindow.h>
54 #include <SVTK_Prs.h>
55 #include <SALOME_Actor.h>
56 #include <VTKViewer_Algorithm.h>
57
58
59 #include "GeometryGUI.h"
60 #include CORBA_CLIENT_HEADER(GEOM_Gen)
61
62
63 #include "HEXABLOCKGUI.hxx"
64 #include "HEXABLOCKGUI_SalomeTools.hxx"
65
66
67
68 //#define _DEVDEBUG_
69 using namespace std;
70 // using namespace HEXABLOCK::GUI;
71
72
73
74 namespace HEXABLOCK{
75
76   namespace GUI{
77
78 SUIT_Study* GetActiveStudy()
79 {
80   SUIT_Application* app = SUIT_Session::session()->activeApplication();
81   if (app)
82     return app->activeStudy();
83   else
84     return NULL;
85 }
86
87
88 _PTR(Study) GetActiveStudyDocument()
89 {
90   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(GetActiveStudy());
91   if (aStudy)
92     return aStudy->studyDS();
93   else
94     return _PTR(Study)();
95 }
96
97
98 CORBA::Object_var corbaObj( _PTR(SObject) theSO )
99 {
100   //std::cout<< "corbaObj( _PTR(SObject) theSO )" << std::endl;
101   CORBA::Object_var aCorbaObj = CORBA::Object::_nil();
102   if ( theSO ) {
103     //std::cout<< "theSO" << std::endl;
104     std::string aValue = theSO->GetIOR();
105     //std::cout<< "aValue" << std::endl;
106     if (strcmp(aValue.c_str(), "") != 0) {
107       CORBA::ORB_ptr anORB = SalomeApp_Application::orb();
108       //std::cout<< "anORB" << anORB << std::endl;
109       aCorbaObj = anORB->string_to_object(aValue.c_str());
110       //std::cout<< "aCorbaObj" << aCorbaObj << std::endl;
111 //         anDoc = Document::_narrow(aCorbaObj);
112     }
113   }
114   return aCorbaObj._retn();
115 }
116
117
118 CORBA::Object_var corbaObj( const Handle(SALOME_InteractiveObject)& theIO )
119 {
120   CORBA::Object_var aCorbaObj = CORBA::Object::_nil();
121
122   if ( !theIO.IsNull() && theIO->hasEntry() ){
123     _PTR(Study)   aStudy = GetActiveStudyDocument();
124     _PTR(SObject) aSObj  = aStudy->FindObjectID(theIO->getEntry());
125     aCorbaObj = corbaObj(aSObj);
126   }
127   return aCorbaObj._retn();
128 }
129
130 /*
131 std::string name( _PTR(SObject) theSO ) 
132 {
133   std::cout << "name( _PTR(SObject) theSO )"<< theSO;
134   std::string aResName;
135   if ( theSO )
136   {
137     _PTR(GenericAttribute) anAttr;
138     _PTR(AttributeName)    aNameAttr;
139     if ( theSO->FindAttribute( anAttr, "AttributeName" ) )
140     {
141       std::cout << "FindAttribute";
142       aNameAttr = anAttr;
143       std::cout << "aNameAttr = anAttr";
144       aResName = aNameAttr->Value().c_str();
145     }
146   }
147   return aResName;
148 }
149
150
151 std::string name( const std::string& entry )
152 {
153   std::cout << "name( const std::string& entry )"<< entry;
154   std::string aResName;
155
156   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
157   if ( appStudy ){
158     std::cout << "appStudy => "<< appStudy ;
159     _PTR(Study) aStudy = appStudy->studyDS();
160     std::cout << "aStudy=> "<< appStudy ;
161     _PTR(SObject) obj( aStudy->FindObjectID( entry ) );
162     std::cout << "obj=> "<< obj;
163     aResName = name( obj );
164   }
165
166   return aResName;
167 }
168 */
169
170
171
172 SALOME_Actor* findActorByEntry( SVTK_ViewWindow *theVtkViewWindow, const char* theEntry)
173 {
174 //     SVTK_ViewWindow* aViewWindow = dynamic_cast<SVTK_ViewWindow*>(theWindow);
175   SALOME_Actor *foundActor = NULL;
176   vtkActor     *aVTKActor  = NULL; 
177   Handle(SALOME_InteractiveObject) anIO;
178  
179   vtkRenderer *aRenderer = theVtkViewWindow->getRenderer();
180   VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
181   vtkActorCollection *aCollection = aCopy.GetActors();
182   aCollection->InitTraversal();
183   while( aVTKActor = aCollection->GetNextActor() ){
184 //         if ( anAct->IsA("GEOM_Actor") ) std::cout<<"is an actor"<< std::endl;
185     foundActor = dynamic_cast<SALOME_Actor*>( aVTKActor );
186     if ( foundActor && foundActor->hasIO() ){
187         anIO = foundActor->getIO();
188         if( anIO->hasEntry() && strcmp(anIO->getEntry(), theEntry) == 0 )
189           return foundActor;
190     }
191   }
192
193   return NULL; // no actor found
194 }
195
196
197
198
199 /*
200 SVTK_ViewWindow* GetActiveVTKViewWindow()
201 {
202     SVTK_ViewWindow* aVtkView = NULL;
203     SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>
204         ( SUIT_Session::session()->activeApplication() );
205     if (anApp)
206       aVtkView = dynamic_cast<SVTK_ViewWindow*>(anApp->desktop()->activeWindow());
207     return aVtkView;
208 }
209
210 //       SUIT_ViewManager* aViewManager =
211 //         anApp->getViewManager(SVTK_Viewer::Type(), createIfNotFound);
212 //       if (aViewManager) {
213 //         if (SUIT_ViewWindow* aViewWindow = aViewManager->getActiveView()) {
214 //           if (SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow)) {
215 //             aViewWindow->raise();
216 //             aViewWindow->setFocus();
217 //             return aView;
218 //           }
219 //         }
220 //       }
221
222
223 SOCC_ViewWindow* GetActiveOCCViewWindow()
224 {
225   SOCC_ViewWindow* anOccView = NULL;
226     SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>
227         (SUIT_Session::session()->activeApplication());
228     if (anApp)
229       anOccView = dynamic_cast<SOCC_ViewWindow*>(anApp->desktop()->activeWindow());
230
231
232   OCCViewer_ViewWindow* aOCCFrame = dynamic_cast<OCCViewer_ViewWindow*>( anApp->desktop()->activeWindow() );
233   std::cout << "aOCCFrame => "<< aOCCFrame;
234
235     return anOccView ;
236 }
237
238
239 OCCViewer_ViewWindow* GetActiveOCCViewerWindow()
240 {
241   OCCViewer_ViewWindow* aOCCFrame = NULL;
242   SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>
243     (SUIT_Session::session()->activeApplication());
244   if (anApp) aOCCFrame = dynamic_cast<OCCViewer_ViewWindow*>( anApp->desktop()->activeWindow() );
245   std::cout << "aOCCFrame => "<< aOCCFrame;
246   return aOCCFrame;
247 }
248
249
250
251 int GetNameOfSelectedNodes( SVTK_ViewWindow *theWindow,
252                             const Handle(SALOME_InteractiveObject)& theIO,
253                             QString& theName )
254 {
255     SVTK_Selector* theSelector = theWindow->GetSelector();
256     theName = "";
257     TColStd_IndexedMapOfInteger aMapIndex;
258     theSelector->GetIndex(theIO,aMapIndex);
259
260     for( int i = 1; i <= aMapIndex.Extent(); i++ )
261       theName += QString(" %1").arg(aMapIndex(i));
262
263     return aMapIndex.Extent();
264 }
265 */
266
267
268 int GetNameOfSelectedElements( SVTK_ViewWindow *theWindow,
269                                const Handle(SALOME_InteractiveObject)& theIO,
270                                QString& theName )
271 {
272     SVTK_Selector* theSelector = theWindow->GetSelector();
273
274     theName = "";
275
276     TColStd_IndexedMapOfInteger aMapIndex;
277     theSelector->GetIndex(theIO,aMapIndex);
278
279     typedef std::set<int> TIdContainer;
280
281     std::set<int> anIdContainer;
282
283     for( int i = 1; i <= aMapIndex.Extent(); i++)
284       anIdContainer.insert(aMapIndex(i));
285
286     std::set<int>::const_iterator anIter = anIdContainer.begin();
287
288     for( ; anIter != anIdContainer.end(); anIter++)
289       theName += QString(" %1").arg(*anIter);
290
291     //std::cout << "GetNameOfSelectedElements name =>" << theName.toStdString() << std::endl;
292     return aMapIndex.Extent();
293 }
294
295 string shape2string( const TopoDS_Shape& aShape )
296 {
297   ostringstream streamShape;
298 //   string  strShape;
299   BRepTools::Write(aShape, streamShape);
300 //   BRepTools::Write(aShape, strShape);
301
302   return streamShape.str();
303 }
304
305
306
307
308
309 // SALOME_View* LightApp_Displayer::GetActiveView()
310 // {
311 //   SUIT_Session* session = SUIT_Session::session();
312 //   if (  SUIT_Application* app = session->activeApplication() ) {
313 //     if ( LightApp_Application* sApp = dynamic_cast<LightApp_Application*>( app ) ) {
314 //       if( SUIT_ViewManager* vman = sApp->activeViewManager() ) {
315 //         if ( SUIT_ViewModel* vmod = vman->getViewModel() )
316 //           return dynamic_cast<SALOME_View*>( vmod );
317 //       }
318 //     }
319 //   }
320 //   return 0;
321 // }
322
323
324
325 MyGEOM_Displayer::MyGEOM_Displayer( SalomeApp_Study* app ):
326 GEOM_Displayer( app )
327 {
328 }
329
330 MyGEOM_Displayer::~MyGEOM_Displayer()
331 {
332 }
333
334 SALOME_Prs* MyGEOM_Displayer::BuildPrs( GEOM::GEOM_Object_ptr theObj )
335 {
336   //std::cout << "MyGEOM_Displayer::BuildPrs( GEOM::GEOM_Object_ptr theObj )" << std::endl;
337   if ( theObj->_is_nil() )
338     return 0;
339
340   SALOME_View*      view = NULL;
341   SUIT_ViewManager* vman = HEXABLOCKGUI::currentOccView->getViewManager();
342   SUIT_ViewModel* vmodel = NULL;
343   if ( vman )
344     vmodel = vman->getViewModel();
345   if ( vmodel )
346     view = dynamic_cast<SALOME_View*>(vmodel);
347
348   myViewFrame = view ;//GetActiveView();
349   if ( myViewFrame == 0 )
350     return 0;
351
352   SALOME_Prs* aPrs = myViewFrame->CreatePrs();
353   if ( aPrs == 0 )
354     return 0;
355
356   internalReset();
357   setShape( GEOM_Client::get_client().GetShape( GeometryGUI::GetGeomGen(), theObj ) );
358   myType = theObj->GetType();
359
360   // Update presentation
361   UpdatePrs( aPrs );
362
363   return aPrs;
364 }
365
366 } //namespace GUI{
367
368 }//namespace HEXABLOCK{
369
370 //////////////////////////////////////////////////////////////////////////
371
372
373 //   SUIT_Study* GetActiveStudy();
374 //   std::string name( _PTR(SObject) theSO );
375 //   std::string name( const std::string& entry );
376 //   SVTK_ViewWindow* GetActiveVTKViewWindow();
377 //   SOCC_ViewWindow* GetActiveOCCViewWindow();
378 //   OCCViewer_ViewWindow* GetActiveOCCViewerWindow();
379 //   int GetNameOfSelectedNodes( SVTK_ViewWindow *theWindow,
380 //                               const Handle(SALOME_InteractiveObject)& theIO,
381 //                               QString& theName );
382
383 //   QString addInStudy   ( GEOM::GEOM_Object_ptr o, const char* theName )
384 //   {
385 //     QString res;
386 // 
387 //     std::cout << "getStudyId()  => " << getStudyId() << std::endl;
388 //     std::cout << "getStudy()  => "   << getStudy() << std::endl;
389 // 
390 //     openCommand();
391 //     res = GEOMBase_Helper::addInStudy(o, theName);
392 //     std::cout << "addInStudy => " << res.toStdString() << std::endl;
393 //     commitCommand();
394 //     return res; 
395 //   }
396 // 
397 // 
398 // // displayPreview( obj, true, activate, false, lineWidth, displayMode, color );
399 //   void displayPreview( GEOM::GEOM_Object_ptr obj, 
400 //                                  const bool   append = false, 
401 //                                  const bool   activate = false, 
402 //                                  const bool   update = true,
403 //                                  const double lineWidth = -1, 
404 //                                  const int    displayMode = -1,
405 //                                  const int    color  = -1 )
406 //   {
407 //     std::cout << "AAAAAAAAAA => "     <<  std::endl;
408 // //     GEOM::GEOM_ITransformOperations_var anOp =
409 // //         getGeomEngine()->GetITransformOperations(getStudyId());
410 // //     GEOM::GEOM_Object_ptr obj2 = anOp->TranslateDXDYDZ (obj, 100, 100, 100);
411 // 
412 //     GEOM::GEOM_IBasicOperations_var anOp =
413 //         getGeomEngine()->GetIBasicOperations(getStudyId());
414 //       std::cout << "BBBBBBBBBB => "     <<  std::endl;
415 //     GEOM::GEOM_Object_ptr obj2 = anOp->MakePointXYZ (100, 125, 150);
416 // 
417 //     std::cout << "obj2->GetEntry() => "     << obj2->GetEntry()<< std::endl;
418 //     std::cout << "obj2->GetStudyID() => "   << obj2->GetStudyID()<< std::endl;
419 //     std::cout << "obj2->GetType() => "      << obj2->GetType()<< std::endl;
420 //     std::cout << "obj2->GetShapeType() => " << obj2->GetShapeType()<< std::endl;
421 // 
422 //     QString res = addInStudy   ( obj2, "trans");
423 //     std::cout << "trans  => "   << res.toStdString() << std::endl;
424 // 
425 //     globalSelection(); // close local contexts, if any
426 //     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
427 //     GEOMBase_Helper::activate( 39);//GEOM_MARKER );
428 // 
429 // //     getDisplayer()->SetColor( Quantity_NOC_RED );
430 // //     getDisplayer()->SetColor( Quantity_NOC_CYAN1 );
431 // 
432 //     // set width of displayed shape
433 // //     getDisplayer()->SetWidth( (lineWidth == -1)?8:lineWidth );
434 // 
435 //     GEOMBase_Helper::displayPreview( obj2,
436 //                                      true, //append,
437 //                                      true, //activate,
438 //                                      update,
439 //                                      8,//lineWidth,
440 //                                      displayMode,
441 //                                      Quantity_NOC_CYAN1);//Quantity_NOC_RED);//color );
442 //     
443 // 
444 //   }
445 // 
446 //   void erase( GEOM::GEOM_Object_ptr obj, const bool d = true)
447 //   {
448 //     GEOMBase_Helper::erase( obj, d);
449 //   }
450
451
452
453 // TopoDS_Shape GEOMBase::GetShapeFromIOR(QString IOR)
454 // {
455 //   TopoDS_Shape result;
456 //   if(IOR.trimmed().isEmpty())
457 //     return result;
458 // 
459 //   CORBA::Object_var obj = SalomeApp_Application::orb()->string_to_object(IOR.toLatin1().data());
460 //   if(CORBA::is_nil(obj))
461 //     return result;
462 //   GEOM::GEOM_Object_var GeomObject = GEOM::GEOM_Object::_narrow( obj );
463 //   if (GeomObject->_is_nil())
464 //     return result;
465 // 
466 //   result = GEOM_Client().GetShape(GeometryGUI::GetGeomGen(), GeomObject);
467 //   return result;
468 // }