Salome HOME
d046bce2066ac30090333b5d0bb50dd89aa207a3
[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     theName = "";
274     TColStd_IndexedMapOfInteger aMapIndex;
275     theSelector->GetIndex(theIO,aMapIndex);
276
277     typedef std::set<int> TIdContainer;
278     std::set<int> anIdContainer;
279     for( int i = 1; i <= aMapIndex.Extent(); i++)
280       anIdContainer.insert(aMapIndex(i));
281
282     std::set<int>::const_iterator anIter = anIdContainer.begin();
283     for( ; anIter != anIdContainer.end(); anIter++)
284       theName += QString(" %1").arg(*anIter);
285
286     //std::cout << "GetNameOfSelectedElements name =>" << theName.toStdString() << std::endl;
287     return aMapIndex.Extent();
288 }
289
290 string shape2string( const TopoDS_Shape& aShape )
291 {
292   ostringstream streamShape;
293 //   string  strShape;
294   BRepTools::Write(aShape, streamShape);
295 //   BRepTools::Write(aShape, strShape);
296
297   return streamShape.str();
298 }
299
300
301
302
303
304 // SALOME_View* LightApp_Displayer::GetActiveView()
305 // {
306 //   SUIT_Session* session = SUIT_Session::session();
307 //   if (  SUIT_Application* app = session->activeApplication() ) {
308 //     if ( LightApp_Application* sApp = dynamic_cast<LightApp_Application*>( app ) ) {
309 //       if( SUIT_ViewManager* vman = sApp->activeViewManager() ) {
310 //         if ( SUIT_ViewModel* vmod = vman->getViewModel() )
311 //           return dynamic_cast<SALOME_View*>( vmod );
312 //       }
313 //     }
314 //   }
315 //   return 0;
316 // }
317
318
319
320 MyGEOM_Displayer::MyGEOM_Displayer( SalomeApp_Study* app ):
321 GEOM_Displayer( app )
322 {
323 }
324
325 MyGEOM_Displayer::~MyGEOM_Displayer()
326 {
327 }
328
329 SALOME_Prs* MyGEOM_Displayer::BuildPrs( GEOM::GEOM_Object_ptr theObj )
330 {
331   //std::cout << "MyGEOM_Displayer::BuildPrs( GEOM::GEOM_Object_ptr theObj )" << std::endl;
332   if ( theObj->_is_nil() )
333     return 0;
334
335   SALOME_View*      view = NULL;
336   SUIT_ViewManager* vman = HEXABLOCKGUI::currentOccView->getViewManager();
337   SUIT_ViewModel* vmodel = NULL;
338   if ( vman )
339     vmodel = vman->getViewModel();
340   if ( vmodel )
341     view = dynamic_cast<SALOME_View*>(vmodel);
342
343   myViewFrame = view ;//GetActiveView();
344   if ( myViewFrame == 0 )
345     return 0;
346
347   SALOME_Prs* aPrs = myViewFrame->CreatePrs();
348   if ( aPrs == 0 )
349     return 0;
350
351   internalReset();
352   setShape( GEOM_Client::get_client().GetShape( GeometryGUI::GetGeomGen(), theObj ) );
353   myType = theObj->GetType();
354
355   // Update presentation
356   UpdatePrs( aPrs );
357
358   return aPrs;
359 }
360
361 } //namespace GUI{
362
363 }//namespace HEXABLOCK{
364
365 //////////////////////////////////////////////////////////////////////////
366
367
368 //   SUIT_Study* GetActiveStudy();
369 //   std::string name( _PTR(SObject) theSO );
370 //   std::string name( const std::string& entry );
371 //   SVTK_ViewWindow* GetActiveVTKViewWindow();
372 //   SOCC_ViewWindow* GetActiveOCCViewWindow();
373 //   OCCViewer_ViewWindow* GetActiveOCCViewerWindow();
374 //   int GetNameOfSelectedNodes( SVTK_ViewWindow *theWindow,
375 //                               const Handle(SALOME_InteractiveObject)& theIO,
376 //                               QString& theName );
377
378 //   QString addInStudy   ( GEOM::GEOM_Object_ptr o, const char* theName )
379 //   {
380 //     QString res;
381 // 
382 //     std::cout << "getStudyId()  => " << getStudyId() << std::endl;
383 //     std::cout << "getStudy()  => "   << getStudy() << std::endl;
384 // 
385 //     openCommand();
386 //     res = GEOMBase_Helper::addInStudy(o, theName);
387 //     std::cout << "addInStudy => " << res.toStdString() << std::endl;
388 //     commitCommand();
389 //     return res; 
390 //   }
391 // 
392 // 
393 // // displayPreview( obj, true, activate, false, lineWidth, displayMode, color );
394 //   void displayPreview( GEOM::GEOM_Object_ptr obj, 
395 //                                  const bool   append = false, 
396 //                                  const bool   activate = false, 
397 //                                  const bool   update = true,
398 //                                  const double lineWidth = -1, 
399 //                                  const int    displayMode = -1,
400 //                                  const int    color  = -1 )
401 //   {
402 //     std::cout << "AAAAAAAAAA => "     <<  std::endl;
403 // //     GEOM::GEOM_ITransformOperations_var anOp =
404 // //         getGeomEngine()->GetITransformOperations(getStudyId());
405 // //     GEOM::GEOM_Object_ptr obj2 = anOp->TranslateDXDYDZ (obj, 100, 100, 100);
406 // 
407 //     GEOM::GEOM_IBasicOperations_var anOp =
408 //         getGeomEngine()->GetIBasicOperations(getStudyId());
409 //       std::cout << "BBBBBBBBBB => "     <<  std::endl;
410 //     GEOM::GEOM_Object_ptr obj2 = anOp->MakePointXYZ (100, 125, 150);
411 // 
412 //     std::cout << "obj2->GetEntry() => "     << obj2->GetEntry()<< std::endl;
413 //     std::cout << "obj2->GetStudyID() => "   << obj2->GetStudyID()<< std::endl;
414 //     std::cout << "obj2->GetType() => "      << obj2->GetType()<< std::endl;
415 //     std::cout << "obj2->GetShapeType() => " << obj2->GetShapeType()<< std::endl;
416 // 
417 //     QString res = addInStudy   ( obj2, "trans");
418 //     std::cout << "trans  => "   << res.toStdString() << std::endl;
419 // 
420 //     globalSelection(); // close local contexts, if any
421 //     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
422 //     GEOMBase_Helper::activate( 39);//GEOM_MARKER );
423 // 
424 // //     getDisplayer()->SetColor( Quantity_NOC_RED );
425 // //     getDisplayer()->SetColor( Quantity_NOC_CYAN1 );
426 // 
427 //     // set width of displayed shape
428 // //     getDisplayer()->SetWidth( (lineWidth == -1)?8:lineWidth );
429 // 
430 //     GEOMBase_Helper::displayPreview( obj2,
431 //                                      true, //append,
432 //                                      true, //activate,
433 //                                      update,
434 //                                      8,//lineWidth,
435 //                                      displayMode,
436 //                                      Quantity_NOC_CYAN1);//Quantity_NOC_RED);//color );
437 //     
438 // 
439 //   }
440 // 
441 //   void erase( GEOM::GEOM_Object_ptr obj, const bool d = true)
442 //   {
443 //     GEOMBase_Helper::erase( obj, d);
444 //   }
445
446
447
448 // TopoDS_Shape GEOMBase::GetShapeFromIOR(QString IOR)
449 // {
450 //   TopoDS_Shape result;
451 //   if(IOR.trimmed().isEmpty())
452 //     return result;
453 // 
454 //   CORBA::Object_var obj = SalomeApp_Application::orb()->string_to_object(IOR.toLatin1().data());
455 //   if(CORBA::is_nil(obj))
456 //     return result;
457 //   GEOM::GEOM_Object_var GeomObject = GEOM::GEOM_Object::_narrow( obj );
458 //   if (GeomObject->_is_nil())
459 //     return result;
460 // 
461 //   result = GEOM_Client().GetShape(GeometryGUI::GetGeomGen(), GeomObject);
462 //   return result;
463 // }