Salome HOME
Merge remote branch 'origin/hydro/imps_2015'
[modules/geom.git] / src / GEOM_SWIG_WITHIHM / libGEOM_Swig.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // File   : libGEOM_Swig.cxx
23 // Author : Nicolas REJNERI, Paul RASCLE
24
25 #include "libGEOM_Swig.h"
26
27 #include "GeometryGUI.h"
28 #include "GEOM_Displayer.h"
29 #include "GEOM_Constants.h"
30 #include "Material_Model.h"
31
32 #include <SUIT_Desktop.h>
33 #include <SUIT_Session.h>
34 #include <SUIT_ViewManager.h>
35 #include <SUIT_ViewModel.h>
36 #include <SalomeApp_Application.h>
37 #include <SalomeApp_Study.h>
38 #include <OCCViewer_ViewFrame.h>
39 #include <SVTK_ViewWindow.h>
40
41 #include <SALOME_Event.h>
42
43 // IDL Headers
44 #include <SALOMEconfig.h>
45 #include CORBA_SERVER_HEADER(GEOM_Gen)
46
47 /*!
48   \brief Constructor
49 */
50 GEOM_Swig::GEOM_Swig()
51 {
52   init();
53 }
54
55 /*!
56   \brief Destructor
57 */
58 GEOM_Swig::~GEOM_Swig()
59 {
60 }
61
62 /*!
63   \brief Internal initialization
64 */
65 void GEOM_Swig::init()
66 {
67   class TEvent: public SALOME_Event
68   {
69   public:
70     TEvent()
71     {}
72     virtual void Execute()
73     {
74       // check active study
75       SUIT_Application* app = SUIT_Session::session()->activeApplication();
76       if (!app) return;
77       
78       SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
79       if ( !study ) return;
80
81       _PTR(Study) studyDS( study->studyDS() );
82       _PTR(StudyBuilder) builder = studyDS->NewBuilder();
83
84       // get/init GEOM engine
85       GEOM::GEOM_Gen_var engine = GeometryGUI::GetGeomGen();
86       if ( CORBA::is_nil( engine ) )
87         return;
88
89       // find GEOM component in the study
90       _PTR(SComponent) component = studyDS->FindComponent( "GEOM" );
91       if ( !component )
92         return;
93
94       // load GEOM data (if it is not done yet)
95       std::string ior;
96       if ( !component->ComponentIOR( ior ) ) {
97         CORBA::String_var engineIOR = SalomeApp_Application::orb()->object_to_string( engine );
98         builder->LoadWith( component, engineIOR.in() );
99       }
100
101       // update Object browser
102       if ( dynamic_cast<SalomeApp_Application*>( app ) )
103         dynamic_cast<SalomeApp_Application*>( app )->updateObjectBrowser( true );
104     }
105   };
106
107   ProcessVoidEvent( new TEvent() );
108 }
109
110 /*!
111   \brief Display the presenation in the currently active view
112   \param theEntry geometry object's entry
113   \param theUpdateViewer \c true to update active view's contents
114 */
115 void GEOM_Swig::createAndDisplayGO( const char* theEntry, bool theUpdateViewer )
116 {
117   class TEvent: public SALOME_Event
118   {
119     std::string myEntry;
120     bool        myUpdateViewer;
121   public:
122     TEvent( const char* _entry, bool _update ):
123       myEntry( _entry ), myUpdateViewer( _update )
124     {}
125     virtual void Execute()
126     {
127       SUIT_Application* app = SUIT_Session::session()->activeApplication();
128       if ( !app ) return;
129       SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
130       if ( !study ) return;
131
132       Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( myEntry.c_str(), "GEOM", "" );
133
134       GEOM_Displayer( study ).Display( io, myUpdateViewer );
135     }
136   };
137
138   ProcessVoidEvent( new TEvent( theEntry, theUpdateViewer ) );
139 }
140
141 /*!
142   \brief Same as createAndDisplayGO, but also fits the active view to the contents
143   \param theEntry geometry object's entry
144   \param theUpdateViewer \c true to update active view's contents
145 */
146 void GEOM_Swig::createAndDisplayFitAllGO( const char* theEntry )
147 {
148   // display object
149   createAndDisplayGO( theEntry );
150
151   // fit all the view
152   class TEventFitAll: public SALOME_Event
153   {
154   public:
155     TEventFitAll() {}
156     virtual void Execute()
157     {
158       SUIT_Application* app = SUIT_Session::session()->activeApplication();
159       if ( app ) {
160         SUIT_ViewWindow* window = app->desktop()->activeWindow();
161         if ( dynamic_cast<SVTK_ViewWindow*>( window ) )
162           dynamic_cast<SVTK_ViewWindow*>( window )->onFitAll();
163         else if ( dynamic_cast<OCCViewer_ViewFrame*>( window ) )
164           dynamic_cast<OCCViewer_ViewFrame*>( window )->onViewFitAll();
165       }
166     }
167   };
168     
169   ProcessVoidEvent( new TEventFitAll() );
170 }
171
172 /*!
173   \brief Erase presentation in the currently active viewer
174   \param theEntry geometry object's entry
175   \param theUpdateViewer \c true to update active view's contents
176 */
177 void GEOM_Swig::eraseGO( const char* theEntry, bool theUpdateViewer )
178 {
179   class TEvent: public SALOME_Event
180   {
181     std::string myEntry;
182     bool        myUpdateViewer;
183   public:
184     TEvent( const char* _entry, bool _update ):
185       myEntry( _entry ), myUpdateViewer( _update )
186     {}
187     virtual void Execute()
188     {
189       SUIT_Application* app = SUIT_Session::session()->activeApplication();
190       if ( !app ) return;
191       SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
192       if ( !study ) return;
193
194       Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( myEntry.c_str(), "GEOM", "" );
195
196       GEOM_Displayer( study ).Erase( io, true, myUpdateViewer );
197     }
198   };
199
200   ProcessVoidEvent( new TEvent( theEntry, theUpdateViewer ) );
201 }
202
203 /*!
204   \brief Update active viewer contents
205 */
206 void GEOM_Swig::UpdateViewer()
207 {
208   class TEventUpdateViewer: public SALOME_Event
209   {
210   public:
211     TEventUpdateViewer()
212     {}
213     virtual void Execute()
214     {
215       SUIT_Application* app = SUIT_Session::session()->activeApplication();
216       if ( !app ) return;
217
218       SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
219       if ( !study ) return;
220       
221       GEOM_Displayer( study ).UpdateViewer();
222     }
223   };
224   
225   ProcessVoidEvent(new TEventUpdateViewer());
226 }
227
228 /*!
229   \brief Get sub-shape index inside main shape
230   \param theSubIOR sub-shape geometry object's IOR
231   \param theMainIOR main shape geometry object's IOR
232   \return sub-shape index (-1 in case of error)
233 */
234 int GEOM_Swig::getIndexTopology( const char* theSubIOR, const char* theMainIOR )
235 {
236   int index = -1;
237
238   // get geom engine
239   GEOM::GEOM_Gen_var aGeomGen = GeometryGUI::GetGeomGen();
240
241   // get main shape's geom object by IOR
242   CORBA::Object_var anObject = SalomeApp_Application::orb()->string_to_object( theMainIOR );
243   GEOM::GEOM_Object_var aMainShape = GEOM::GEOM_Object::_narrow( anObject.in() );
244   // get sub-shape's geom object by IOR
245   anObject = SalomeApp_Application::orb()->string_to_object( theSubIOR );
246   GEOM::GEOM_Object_var aSubShape = GEOM::GEOM_Object::_narrow( anObject.in() );
247
248   if ( !CORBA::is_nil( aGeomGen ) && !CORBA::is_nil( aMainShape ) && !CORBA::is_nil( aSubShape ) ) {
249     // get shapes operations interface
250     GEOM::GEOM_IShapesOperations_var anIShapesOperations =
251       aGeomGen->GetIShapesOperations( aMainShape->GetStudyID() );
252     if ( !CORBA::is_nil( anIShapesOperations ) )
253       index = anIShapesOperations->GetTopologyIndex( aMainShape, aSubShape );
254   }
255
256   return index;
257 }
258
259 /*!
260   \brief Get shape type name
261   \param theIOR geometry object's IOR
262   \return shape type name ("Shape of unknown type" in case of error)
263 */
264 const char* GEOM_Swig::getShapeTypeString( const char* theIOR )
265 {
266   QString aTypeName = "Shape of unknown type";
267
268   // get geom engine
269   GEOM::GEOM_Gen_var aGeomGen = GeometryGUI::GetGeomGen();
270
271   // get shape's geom object by IOR
272   CORBA::Object_var anObject = SalomeApp_Application::orb()->string_to_object( theIOR );
273   GEOM::GEOM_Object_var aShape = GEOM::GEOM_Object::_narrow( anObject.in() );
274
275   if ( !CORBA::is_nil( aGeomGen ) && !CORBA::is_nil( aShape ) ) {
276     // get shapes operations interface
277     GEOM::GEOM_IShapesOperations_var anIShapesOperations =
278       aGeomGen->GetIShapesOperations( aShape->GetStudyID() );
279     if ( !CORBA::is_nil( anIShapesOperations ) )
280       aTypeName = anIShapesOperations->GetShapeTypeString( aShape );
281   }
282
283   return strdup( qPrintable( aTypeName ) );
284 }
285
286 /*!
287   \brief Get shape's icon ID (specified by its type)
288   \param theIOR geometry object's IOR
289   \return icon ID ("None" in case of error)
290 */
291 const char* GEOM_Swig::getShapeTypeIcon( const char* theIOR )
292 {
293   static const char* icons[] = {
294     "ICON_OBJBROWSER_COMPOUND",
295     "ICON_OBJBROWSER_COMPSOLID",
296     "ICON_OBJBROWSER_SOLID",
297     "ICON_OBJBROWSER_SHELL",
298     "ICON_OBJBROWSER_FACE",
299     "ICON_OBJBROWSER_WIRE",
300     "ICON_OBJBROWSER_EDGE",
301     "ICON_OBJBROWSER_VERTEX"
302   };
303
304   const char* anIcon = "None";
305
306   try {
307     CORBA::Object_var anObject = SalomeApp_Application::orb()->string_to_object( theIOR );
308     if ( !CORBA::is_nil( anObject ) ) {
309       GEOM::GEOM_Object_var aShape = GEOM::GEOM_Object::_narrow( anObject.in() );
310       if ( !CORBA::is_nil( aShape ) ) {
311         GEOM::shape_type aType = aShape->GetShapeType();
312         if ( aType >= GEOM::COMPOUND && aType < GEOM::SHAPE )
313           anIcon = icons[ (int)aType ];
314       }
315     }
316   }
317   catch ( CORBA::Exception& ) {
318   }
319
320   return anIcon;
321 }
322
323 class TSetPropertyEvent: public SALOME_Event
324 {
325   QString  myEntry;
326   QString  myProperty;
327   QVariant myValue;
328   bool     myUpdateViewer;
329   
330 public:
331   TSetPropertyEvent( const QString& _entry,
332                      const QString& _property,
333                      const QVariant& _value,
334                      bool _update = true );
335   virtual void Execute();
336 };
337   
338 TSetPropertyEvent::TSetPropertyEvent( const QString& _entry,
339                                       const QString& _property,
340                                       const QVariant& _value,
341                                       bool _update ):
342   myEntry( _entry ),
343   myProperty( _property ),
344   myValue( _value ),
345   myUpdateViewer( _update )
346 {
347 }
348
349 void TSetPropertyEvent::Execute()
350 {
351   SUIT_Application* app = SUIT_Session::session()->activeApplication();
352   if ( !app ) return;
353   
354   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
355   if ( !study ) return;
356   
357   GEOM_Displayer displayer( study );
358   
359   SALOME_View* window = displayer.GetActiveView();
360   if ( !window ) return;
361   
362   int mgrId = dynamic_cast<SUIT_ViewModel*>( window )->getViewManager()->getGlobalId();
363
364   study->setObjectProperty( mgrId, myEntry, myProperty, myValue );
365   
366   Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( myEntry.toLatin1().data(), "GEOM" );
367   if ( window->isVisible( io ) ) displayer.Redisplay( io, myUpdateViewer );
368 }
369
370 /*!
371   \brief Set display mode to the presentation
372   \param theEntry geometry object's entry
373   \param theMode display mode: 0 - wireframe, 1 - shading, 2 - shading+edges, 3-textured
374   \param theUpdateViewer \c true to update active view's contents
375 */
376 void GEOM_Swig::setDisplayMode( const char* theEntry, int theMode, bool theUpdateViewer )
377 {
378   ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::DisplayMode ), 
379                                            theMode, theUpdateViewer ) );
380 }
381
382 /*!
383   \brief Show / hide edges direction vectors for the presentation
384   \param theEntry geometry object's entry
385   \param theOn \c true to show edges direction vectors or \c false otherwise
386   \param theUpdateViewer \c true to update active view's contents
387 */
388 void GEOM_Swig::setVectorsMode( const char* theEntry, bool theOn, bool theUpdateViewer )
389 {
390   ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::EdgesDirection ), 
391                                            theOn, theUpdateViewer ) );
392 }
393
394 /*!
395   \brief Show / hide vertices for the presentation
396   \param theEntry geometry object's entry
397   \param theOn \c true to show vertices or \c false otherwise
398   \param theUpdateViewer \c true to update active view's contents
399 */
400 void GEOM_Swig::setVerticesMode( const char* theEntry, bool theOn, bool theUpdateViewer )
401 {
402   ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Vertices ), 
403                                            theOn, theUpdateViewer ) );
404 }
405
406 /*!
407   \brief Show / hide name of shape for the presentation
408   \param theEntry geometry object's entry
409   \param theOn \c true to show name or \c false otherwise
410   \param theUpdateViewer \c true to update active view's contents
411 */
412 void GEOM_Swig::setNameMode( const char* theEntry, bool theOn, bool theUpdateViewer )
413 {
414   ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::ShowName ),
415                                            theOn, theUpdateViewer ) );
416 }
417
418 /*!
419   \brief Change color of the presentation
420   \param theEntry geometry object's entry
421   \param theRed red component of the component (0-255)
422   \param theGreen green component of the component (0-255)
423   \param theBlue blue component of the component (0-255)
424   \param theUpdateViewer \c true to update active view's contents
425 */
426 void GEOM_Swig::setColor( const char* theEntry, int theRed, int theGreen, int theBlue, bool theUpdateViewer )
427 {
428   ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Color ), 
429                                            QColor( theRed, theGreen, theBlue ), theUpdateViewer ) );
430 }
431
432 /*!
433   \brief Set number of iso-lines to the presentation
434   \param theEntry geometry object's entry
435   \param theNbU number of iso-lines along U axis (interger value >= 0)
436   \param theNbV number of iso-lines along V axis (interger value >= 0)
437   \param theUpdateViewer \c true to update active view's contents
438 */
439 void GEOM_Swig::setIsos( const char* theEntry, int theNbU, int theNbV, bool theUpdateViewer )
440 {
441   ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::NbIsos ), 
442                                            QString( "%1%2%3" ).arg( theNbU ).arg( GEOM::subSectionSeparator() ).arg( theNbV ), 
443                                            theUpdateViewer ) );
444 }
445
446 /*!
447   \brief Set transparency of the presentation
448   \param theEntry geometry object's entry
449   \param theTransparency transparency (floating point value between 0 and 1)
450   \param theUpdateViewer \c true to update active view's contents
451 */
452 void GEOM_Swig::setTransparency( const char* theEntry, float theTransparency, bool theUpdateViewer )
453 {
454   ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Transparency ), 
455                                            theTransparency, theUpdateViewer ) );
456 }
457
458 /*!
459   \brief Set deflection coefficient of the presentation
460   \param theEntry geometry object's entry
461   \param theDeflection deflection coefficient (floating point value)
462   \param theUpdateViewer \c true to update active view's contents
463 */
464 void GEOM_Swig::setDeflection( const char* theEntry, float theDeflection, bool theUpdateViewer )
465 {
466   ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Deflection ), 
467                                            theDeflection, theUpdateViewer ) );
468 }
469
470 /*!
471   \brief Set material to the presentation
472   \param theEntry geometry object's entry
473   \param theMaterial material name (string)
474   \param theUpdateViewer \c true to update active view's contents
475 */
476 void GEOM_Swig::setMaterial( const char* theEntry, const char* theMaterial, bool theUpdateViewer )
477 {
478   Material_Model material;
479   material.fromResources( theMaterial );
480   ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Material ), 
481                                            material.toProperties(), theUpdateViewer ) );
482 }
483
484 /*!
485   \brief Set material property to the presentation
486   \param theEntry geometry object's entry
487   \param theMaterial material property string
488   \param theUpdateViewer \c true to update active view's contents
489 */
490 void GEOM_Swig::setMaterialProperty( const char* theEntry, const char* theMaterial, bool theUpdateViewer )
491 {
492   ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Material ), 
493                                            theMaterial, theUpdateViewer ) );
494 }
495
496 class TInitGeomGenEvent: public SALOME_Event
497 {
498 public:
499   typedef bool TResult;
500   TResult myResult;
501   TInitGeomGenEvent() : myResult(false)
502   {}
503   virtual void Execute()
504   {
505     myResult = GeometryGUI::InitGeomGen();
506   }
507 };
508
509 /*!
510   \brief Initialize GEOM module's engine
511   \return \c true if initialization succeedes or \c false otherwise
512 */
513 bool GEOM_Swig::initGeomGen()
514 {
515   return ProcessEvent( new TInitGeomGenEvent() );
516 }