Salome HOME
Unicode support
[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       Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( myEntry.c_str(), "GEOM", "" );
128
129       GEOM_Displayer().Display( io, myUpdateViewer );
130     }
131   };
132
133   ProcessVoidEvent( new TEvent( theEntry, theUpdateViewer ) );
134 }
135
136 /*!
137   \brief Same as createAndDisplayGO, but also fits the active view to the contents
138   \param theEntry geometry object's entry
139   \param theUpdateViewer \c true to update active view's contents
140 */
141 void GEOM_Swig::createAndDisplayFitAllGO( const char* theEntry )
142 {
143   // display object
144   createAndDisplayGO( theEntry );
145
146   // fit all the view
147   class TEventFitAll: public SALOME_Event
148   {
149   public:
150     TEventFitAll() {}
151     virtual void Execute()
152     {
153       SUIT_Application* app = SUIT_Session::session()->activeApplication();
154       if ( app ) {
155         SUIT_ViewWindow* window = app->desktop()->activeWindow();
156         if ( dynamic_cast<SVTK_ViewWindow*>( window ) )
157           dynamic_cast<SVTK_ViewWindow*>( window )->onFitAll();
158         else if ( dynamic_cast<OCCViewer_ViewFrame*>( window ) )
159           dynamic_cast<OCCViewer_ViewFrame*>( window )->onViewFitAll();
160       }
161     }
162   };
163     
164   ProcessVoidEvent( new TEventFitAll() );
165 }
166
167 /*!
168   \brief Erase presentation in the currently active viewer
169   \param theEntry geometry object's entry
170   \param theUpdateViewer \c true to update active view's contents
171 */
172 void GEOM_Swig::eraseGO( const char* theEntry, bool theUpdateViewer )
173 {
174   class TEvent: public SALOME_Event
175   {
176     std::string myEntry;
177     bool        myUpdateViewer;
178   public:
179     TEvent( const char* _entry, bool _update ):
180       myEntry( _entry ), myUpdateViewer( _update )
181     {}
182     virtual void Execute()
183     {
184       Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( myEntry.c_str(), "GEOM", "" );
185
186       GEOM_Displayer().Erase( io, true, myUpdateViewer );
187     }
188   };
189
190   ProcessVoidEvent( new TEvent( theEntry, theUpdateViewer ) );
191 }
192
193 /*!
194   \brief Update active viewer contents
195 */
196 void GEOM_Swig::UpdateViewer()
197 {
198   class TEventUpdateViewer: public SALOME_Event
199   {
200   public:
201     TEventUpdateViewer()
202     {}
203     virtual void Execute()
204     {
205       GEOM_Displayer().UpdateViewer();
206     }
207   };
208   
209   ProcessVoidEvent(new TEventUpdateViewer());
210 }
211
212 /*!
213   \brief Get sub-shape index inside main shape
214   \param theSubIOR sub-shape geometry object's IOR
215   \param theMainIOR main shape geometry object's IOR
216   \return sub-shape index (-1 in case of error)
217 */
218 int GEOM_Swig::getIndexTopology( const char* theSubIOR, const char* theMainIOR )
219 {
220   int index = -1;
221
222   // get geom engine
223   GEOM::GEOM_Gen_var aGeomGen = GeometryGUI::GetGeomGen();
224
225   // get main shape's geom object by IOR
226   CORBA::Object_var anObject = SalomeApp_Application::orb()->string_to_object( theMainIOR );
227   GEOM::GEOM_Object_var aMainShape = GEOM::GEOM_Object::_narrow( anObject.in() );
228   // get sub-shape's geom object by IOR
229   anObject = SalomeApp_Application::orb()->string_to_object( theSubIOR );
230   GEOM::GEOM_Object_var aSubShape = GEOM::GEOM_Object::_narrow( anObject.in() );
231
232   if ( !CORBA::is_nil( aGeomGen ) && !CORBA::is_nil( aMainShape ) && !CORBA::is_nil( aSubShape ) ) {
233     // get shapes operations interface
234     GEOM::GEOM_IShapesOperations_var anIShapesOperations =
235       aGeomGen->GetIShapesOperations();
236     if ( !CORBA::is_nil( anIShapesOperations ) )
237       index = anIShapesOperations->GetTopologyIndex( aMainShape, aSubShape );
238   }
239
240   return index;
241 }
242
243 /*!
244   \brief Get shape type name
245   \param theIOR geometry object's IOR
246   \return shape type name ("Shape of unknown type" in case of error)
247 */
248 const char* GEOM_Swig::getShapeTypeString( const char* theIOR )
249 {
250   QString aTypeName = "Shape of unknown type";
251
252   // get geom engine
253   GEOM::GEOM_Gen_var aGeomGen = GeometryGUI::GetGeomGen();
254
255   // get shape's geom object by IOR
256   CORBA::Object_var anObject = SalomeApp_Application::orb()->string_to_object( theIOR );
257   GEOM::GEOM_Object_var aShape = GEOM::GEOM_Object::_narrow( anObject.in() );
258
259   if ( !CORBA::is_nil( aGeomGen ) && !CORBA::is_nil( aShape ) ) {
260     // get shapes operations interface
261     GEOM::GEOM_IShapesOperations_var anIShapesOperations =
262       aGeomGen->GetIShapesOperations();
263     if ( !CORBA::is_nil( anIShapesOperations ) )
264       aTypeName = anIShapesOperations->GetShapeTypeString( aShape );
265   }
266
267   return strdup( qPrintable( aTypeName ) );
268 }
269
270 /*!
271   \brief Get shape's icon ID (specified by its type)
272   \param theIOR geometry object's IOR
273   \return icon ID ("None" in case of error)
274 */
275 const char* GEOM_Swig::getShapeTypeIcon( const char* theIOR )
276 {
277   static const char* icons[] = {
278     "ICON_OBJBROWSER_COMPOUND",
279     "ICON_OBJBROWSER_COMPSOLID",
280     "ICON_OBJBROWSER_SOLID",
281     "ICON_OBJBROWSER_SHELL",
282     "ICON_OBJBROWSER_FACE",
283     "ICON_OBJBROWSER_WIRE",
284     "ICON_OBJBROWSER_EDGE",
285     "ICON_OBJBROWSER_VERTEX"
286   };
287
288   const char* anIcon = "None";
289
290   try {
291     CORBA::Object_var anObject = SalomeApp_Application::orb()->string_to_object( theIOR );
292     if ( !CORBA::is_nil( anObject ) ) {
293       GEOM::GEOM_Object_var aShape = GEOM::GEOM_Object::_narrow( anObject.in() );
294       if ( !CORBA::is_nil( aShape ) ) {
295         GEOM::shape_type aType = aShape->GetShapeType();
296         if ( aType >= GEOM::COMPOUND && aType < GEOM::SHAPE )
297           anIcon = icons[ (int)aType ];
298       }
299     }
300   }
301   catch ( CORBA::Exception& ) {
302   }
303
304   return anIcon;
305 }
306
307 class TSetPropertyEvent: public SALOME_Event
308 {
309   QString  myEntry;
310   QString  myProperty;
311   QVariant myValue;
312   bool     myUpdateViewer;
313   
314 public:
315   TSetPropertyEvent( const QString& _entry,
316                      const QString& _property,
317                      const QVariant& _value,
318                      bool _update = true );
319   virtual void Execute();
320 };
321   
322 TSetPropertyEvent::TSetPropertyEvent( const QString& _entry,
323                                       const QString& _property,
324                                       const QVariant& _value,
325                                       bool _update ):
326   myEntry( _entry ),
327   myProperty( _property ),
328   myValue( _value ),
329   myUpdateViewer( _update )
330 {
331 }
332
333 void TSetPropertyEvent::Execute()
334 {
335   SUIT_Application* app = SUIT_Session::session()->activeApplication();
336   if ( !app ) return;
337   
338   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
339   if ( !study ) return;
340   
341   GEOM_Displayer displayer;
342   
343   SALOME_View* window = displayer.GetActiveView();
344   if ( !window ) return;
345   
346   int mgrId = dynamic_cast<SUIT_ViewModel*>( window )->getViewManager()->getGlobalId();
347
348   study->setObjectProperty( mgrId, myEntry, myProperty, myValue );
349   
350   Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( myEntry.toUtf8().data(), "GEOM" );
351   if ( window->isVisible( io ) ) displayer.Redisplay( io, myUpdateViewer );
352 }
353
354 /*!
355   \brief Set display mode to the presentation
356   \param theEntry geometry object's entry
357   \param theMode display mode: 0 - wireframe, 1 - shading, 2 - shading+edges, 3-textured
358   \param theUpdateViewer \c true to update active view's contents
359 */
360 void GEOM_Swig::setDisplayMode( const char* theEntry, int theMode, bool theUpdateViewer )
361 {
362   ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::DisplayMode ), 
363                                            theMode, theUpdateViewer ) );
364 }
365
366 /*!
367   \brief Show / hide edges direction vectors for the presentation
368   \param theEntry geometry object's entry
369   \param theOn \c true to show edges direction vectors or \c false otherwise
370   \param theUpdateViewer \c true to update active view's contents
371 */
372 void GEOM_Swig::setVectorsMode( const char* theEntry, bool theOn, bool theUpdateViewer )
373 {
374   ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::EdgesDirection ), 
375                                            theOn, theUpdateViewer ) );
376 }
377
378 /*!
379   \brief Show / hide vertices for the presentation
380   \param theEntry geometry object's entry
381   \param theOn \c true to show vertices or \c false otherwise
382   \param theUpdateViewer \c true to update active view's contents
383 */
384 void GEOM_Swig::setVerticesMode( const char* theEntry, bool theOn, bool theUpdateViewer )
385 {
386   ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Vertices ), 
387                                            theOn, theUpdateViewer ) );
388 }
389
390 /*!
391   \brief Show / hide name of shape for the presentation
392   \param theEntry geometry object's entry
393   \param theOn \c true to show name or \c false otherwise
394   \param theUpdateViewer \c true to update active view's contents
395 */
396 void GEOM_Swig::setNameMode( const char* theEntry, bool theOn, bool theUpdateViewer )
397 {
398   ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::ShowName ),
399                                            theOn, theUpdateViewer ) );
400 }
401
402 /*!
403   \brief Change color of the presentation
404   \param theEntry geometry object's entry
405   \param theRed red component of the component (0-255)
406   \param theGreen green component of the component (0-255)
407   \param theBlue blue component of the component (0-255)
408   \param theUpdateViewer \c true to update active view's contents
409 */
410 void GEOM_Swig::setColor( const char* theEntry, int theRed, int theGreen, int theBlue, bool theUpdateViewer )
411 {
412   ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Color ), 
413                                            QColor( theRed, theGreen, theBlue ), theUpdateViewer ) );
414 }
415
416 /*!
417   \brief Set number of iso-lines to the presentation
418   \param theEntry geometry object's entry
419   \param theNbU number of iso-lines along U axis (integer value >= 0)
420   \param theNbV number of iso-lines along V axis (integer value >= 0)
421   \param theUpdateViewer \c true to update active view's contents
422 */
423 void GEOM_Swig::setIsos( const char* theEntry, int theNbU, int theNbV, bool theUpdateViewer )
424 {
425   ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::NbIsos ), 
426                                            QString( "%1%2%3" ).arg( theNbU ).arg( GEOM::subSectionSeparator() ).arg( theNbV ), 
427                                            theUpdateViewer ) );
428 }
429
430 /*!
431   \brief Set transparency of the presentation
432   \param theEntry geometry object's entry
433   \param theTransparency transparency (floating point value between 0 and 1)
434   \param theUpdateViewer \c true to update active view's contents
435 */
436 void GEOM_Swig::setTransparency( const char* theEntry, float theTransparency, bool theUpdateViewer )
437 {
438   ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Transparency ), 
439                                            theTransparency, theUpdateViewer ) );
440 }
441
442 /*!
443   \brief Set deflection coefficient of the presentation
444   \param theEntry geometry object's entry
445   \param theDeflection deflection coefficient (floating point value)
446   \param theUpdateViewer \c true to update active view's contents
447 */
448 void GEOM_Swig::setDeflection( const char* theEntry, float theDeflection, bool theUpdateViewer )
449 {
450   ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Deflection ), 
451                                            theDeflection, theUpdateViewer ) );
452 }
453
454 /*!
455   \brief Set material to the presentation
456   \param theEntry geometry object's entry
457   \param theMaterial material name (string)
458   \param theUpdateViewer \c true to update active view's contents
459 */
460 void GEOM_Swig::setMaterial( const char* theEntry, const char* theMaterial, bool theUpdateViewer )
461 {
462   Material_Model material;
463   material.fromResources( theMaterial );
464   ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Material ), 
465                                            material.toProperties(), theUpdateViewer ) );
466 }
467
468 /*!
469   \brief Set material property to the presentation
470   \param theEntry geometry object's entry
471   \param theMaterial material property string
472   \param theUpdateViewer \c true to update active view's contents
473 */
474 void GEOM_Swig::setMaterialProperty( const char* theEntry, const char* theMaterial, bool theUpdateViewer )
475 {
476   ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Material ), 
477                                            theMaterial, theUpdateViewer ) );
478 }
479
480 class TInitGeomGenEvent: public SALOME_Event
481 {
482 public:
483   typedef bool TResult;
484   TResult myResult;
485   TInitGeomGenEvent() : myResult(false)
486   {}
487   virtual void Execute()
488   {
489     myResult = GeometryGUI::InitGeomGen();
490   }
491 };
492
493 /*!
494   \brief Initialize GEOM module's engine
495   \return \c true if initialization succeeds or \c false otherwise
496 */
497 bool GEOM_Swig::initGeomGen()
498 {
499   return ProcessEvent( new TInitGeomGenEvent() );
500 }