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