Salome HOME
95966a4c945bd04c0742f75f091ad09c165edb29
[modules/geom.git] / src / GEOM_SWIG_WITHIHM / libGEOM_Swig.cxx
1 // Copyright (C) 2007-2021  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 #include "GEOM_Swig_LocalSelector.h"
32 #include "GEOMGUI_OCCSelector.h"
33 #include "OCCViewer_ViewManager.h"
34
35 #include <SUIT_Desktop.h>
36 #include <SUIT_Session.h>
37 #include <SUIT_ViewManager.h>
38 #include <SUIT_ViewModel.h>
39 #include <SUIT_SelectionMgr.h>
40 #include <LightApp_SelectionMgr.h>
41 #include <SalomeApp_Application.h>
42 #include <SalomeApp_Study.h>
43 #include <OCCViewer_ViewFrame.h>
44 #include <SVTK_ViewWindow.h>
45
46 #include <SALOME_Event.h>
47 #include <utilities.h>
48
49 // IDL Headers
50 #include <SALOMEconfig.h>
51 #include CORBA_SERVER_HEADER(GEOM_Gen)
52
53 GEOM_Swig_LocalSelector* GEOM_Swig::myLocalSelector = 0;
54 GEOMGUI_OCCSelector* GEOM_Swig::myOCCSelector =0;
55
56 /*!
57   \brief Constructor
58 */
59 GEOM_Swig::GEOM_Swig( bool updateOB )
60 {
61   init(updateOB);
62 }
63
64 /*!
65   \brief Destructor
66 */
67 GEOM_Swig::~GEOM_Swig()
68 {
69 }
70
71 /*!
72   \brief Internal initialization
73 */
74 void GEOM_Swig::init( bool updateOB )
75 {
76   class TEvent: public SALOME_Event
77   {
78     bool myUpdateOB;
79   public:
80     TEvent( bool _updateOB ):
81       myUpdateOB(_updateOB)
82     {}
83     virtual void Execute()
84     {
85       // check active study
86       SUIT_Application* app = SUIT_Session::session()->activeApplication();
87       if (!app) return;
88       
89       SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
90       if ( !study ) return;
91
92       _PTR(Study) studyDS( study->studyDS() );
93       _PTR(StudyBuilder) builder = studyDS->NewBuilder();
94
95       // get/init GEOM engine
96       GEOM::GEOM_Gen_var engine = GeometryGUI::GetGeomGen();
97       if ( CORBA::is_nil( engine ) )
98         return;
99
100       // find GEOM component in the study
101       _PTR(SComponent) component = studyDS->FindComponent( "GEOM" );
102       if ( !component )
103         return;
104
105       // load GEOM data (if it is not done yet)
106       std::string ior;
107       if ( !component->ComponentIOR( ior ) ) {
108         CORBA::String_var engineIOR = SalomeApp_Application::orb()->object_to_string( engine );
109         builder->LoadWith( component, engineIOR.in() );
110       }
111
112       // update Object browser
113       if (myUpdateOB && dynamic_cast<SalomeApp_Application*>( app ) )
114         dynamic_cast<SalomeApp_Application*>( app )->updateObjectBrowser( true );
115
116       //myLocalSelector = 0;
117     }
118   };
119
120   ProcessVoidEvent( new TEvent(updateOB) );
121 }
122
123 /*!
124   \brief Display the presenation in the currently active view
125   \param theEntry geometry object's entry
126   \param theUpdateViewer \c true to update active view's contents
127 */
128 void GEOM_Swig::createAndDisplayGO( const char* theEntry, bool theUpdateViewer )
129 {
130   class TEvent: public SALOME_Event
131   {
132     std::string myEntry;
133     bool        myUpdateViewer;
134   public:
135     TEvent( const char* _entry, bool _update ):
136       myEntry( _entry ), myUpdateViewer( _update )
137     {}
138     virtual void Execute()
139     {
140       Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( myEntry.c_str(), "GEOM", "" );
141
142       GEOM_Displayer().Display( io, myUpdateViewer );
143     }
144   };
145
146   ProcessVoidEvent( new TEvent( theEntry, theUpdateViewer ) );
147 }
148
149 /*!
150   \brief Same as createAndDisplayGO, but also fits the active view to the contents
151   \param theEntry geometry object's entry
152   \param theUpdateViewer \c true to update active view's contents
153 */
154 void GEOM_Swig::createAndDisplayFitAllGO( const char* theEntry )
155 {
156   // display object
157   createAndDisplayGO( theEntry );
158
159   // fit all the view
160   class TEventFitAll: public SALOME_Event
161   {
162   public:
163     TEventFitAll() {}
164     virtual void Execute()
165     {
166       SUIT_Application* app = SUIT_Session::session()->activeApplication();
167       if ( app ) {
168         SUIT_ViewWindow* window = app->desktop()->activeWindow();
169         if ( dynamic_cast<SVTK_ViewWindow*>( window ) )
170           dynamic_cast<SVTK_ViewWindow*>( window )->onFitAll();
171         else if ( dynamic_cast<OCCViewer_ViewFrame*>( window ) )
172           dynamic_cast<OCCViewer_ViewFrame*>( window )->onViewFitAll();
173       }
174     }
175   };
176     
177   ProcessVoidEvent( new TEventFitAll() );
178 }
179
180 /*!
181   \brief Erase presentation in the currently active viewer
182   \param theEntry geometry object's entry
183   \param theUpdateViewer \c true to update active view's contents
184 */
185 void GEOM_Swig::eraseGO( const char* theEntry, bool theUpdateViewer )
186 {
187   class TEvent: public SALOME_Event
188   {
189     std::string myEntry;
190     bool        myUpdateViewer;
191   public:
192     TEvent( const char* _entry, bool _update ):
193       myEntry( _entry ), myUpdateViewer( _update )
194     {}
195     virtual void Execute()
196     {
197       Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( myEntry.c_str(), "GEOM", "" );
198
199       GEOM_Displayer().Erase( io, true, myUpdateViewer );
200     }
201   };
202
203   ProcessVoidEvent( new TEvent( theEntry, theUpdateViewer ) );
204 }
205
206 /*!
207   \brief Update active viewer contents
208 */
209 void GEOM_Swig::UpdateViewer()
210 {
211   class TEventUpdateViewer: public SALOME_Event
212   {
213   public:
214     TEventUpdateViewer()
215     {}
216     virtual void Execute()
217     {
218       GEOM_Displayer().UpdateViewer();
219     }
220   };
221   
222   ProcessVoidEvent(new TEventUpdateViewer());
223 }
224
225 /*!
226   \brief Get sub-shape index inside main shape
227   \param theSubIOR sub-shape geometry object's IOR
228   \param theMainIOR main shape geometry object's IOR
229   \return sub-shape index (-1 in case of error)
230 */
231 int GEOM_Swig::getIndexTopology( const char* theSubIOR, const char* theMainIOR )
232 {
233   int index = -1;
234
235   // get geom engine
236   GEOM::GEOM_Gen_var aGeomGen = GeometryGUI::GetGeomGen();
237
238   // get main shape's geom object by IOR
239   CORBA::Object_var anObject = SalomeApp_Application::orb()->string_to_object( theMainIOR );
240   GEOM::GEOM_Object_var aMainShape = GEOM::GEOM_Object::_narrow( anObject.in() );
241   // get sub-shape's geom object by IOR
242   anObject = SalomeApp_Application::orb()->string_to_object( theSubIOR );
243   GEOM::GEOM_Object_var aSubShape = GEOM::GEOM_Object::_narrow( anObject.in() );
244
245   if ( !CORBA::is_nil( aGeomGen ) && !CORBA::is_nil( aMainShape ) && !CORBA::is_nil( aSubShape ) ) {
246     // get shapes operations interface
247     GEOM::GEOM_IShapesOperations_var anIShapesOperations =
248       aGeomGen->GetIShapesOperations();
249     if ( !CORBA::is_nil( anIShapesOperations ) )
250       index = anIShapesOperations->GetTopologyIndex( aMainShape, aSubShape );
251   }
252
253   return index;
254 }
255
256 /*!
257   \brief Get shape type name
258   \param theIOR geometry object's IOR
259   \return shape type name ("Shape of unknown type" in case of error)
260 */
261 const char* GEOM_Swig::getShapeTypeString( const char* theIOR )
262 {
263   QString aTypeName = "Shape of unknown type";
264
265   // get geom engine
266   GEOM::GEOM_Gen_var aGeomGen = GeometryGUI::GetGeomGen();
267
268   // get shape's geom object by IOR
269   CORBA::Object_var anObject = SalomeApp_Application::orb()->string_to_object( theIOR );
270   GEOM::GEOM_Object_var aShape = GEOM::GEOM_Object::_narrow( anObject.in() );
271
272   if ( !CORBA::is_nil( aGeomGen ) && !CORBA::is_nil( aShape ) ) {
273     // get shapes operations interface
274     GEOM::GEOM_IShapesOperations_var anIShapesOperations =
275       aGeomGen->GetIShapesOperations();
276     if ( !CORBA::is_nil( anIShapesOperations ) )
277       aTypeName = anIShapesOperations->GetShapeTypeString( aShape );
278   }
279
280   return strdup( qPrintable( aTypeName ) );
281 }
282
283 /*!
284   \brief Get shape's icon ID (specified by its type)
285   \param theIOR geometry object's IOR
286   \return icon ID ("None" in case of error)
287 */
288 const char* GEOM_Swig::getShapeTypeIcon( const char* theIOR )
289 {
290   static const char* icons[] = {
291     "ICON_OBJBROWSER_COMPOUND",
292     "ICON_OBJBROWSER_COMPSOLID",
293     "ICON_OBJBROWSER_SOLID",
294     "ICON_OBJBROWSER_SHELL",
295     "ICON_OBJBROWSER_FACE",
296     "ICON_OBJBROWSER_WIRE",
297     "ICON_OBJBROWSER_EDGE",
298     "ICON_OBJBROWSER_VERTEX"
299   };
300
301   const char* anIcon = "None";
302
303   try {
304     CORBA::Object_var anObject = SalomeApp_Application::orb()->string_to_object( theIOR );
305     if ( !CORBA::is_nil( anObject ) ) {
306       GEOM::GEOM_Object_var aShape = GEOM::GEOM_Object::_narrow( anObject.in() );
307       if ( !CORBA::is_nil( aShape ) ) {
308         GEOM::shape_type aType = aShape->GetShapeType();
309         if ( aType >= GEOM::COMPOUND && aType < GEOM::SHAPE )
310           anIcon = icons[ (int)aType ];
311       }
312     }
313   }
314   catch ( CORBA::Exception& ) {
315   }
316
317   return anIcon;
318 }
319
320 class TSetPropertyEvent: public SALOME_Event
321 {
322   QString  myEntry;
323   QString  myProperty;
324   QVariant myValue;
325   bool     myUpdateViewer;
326   
327 public:
328   TSetPropertyEvent( const QString& _entry,
329                      const QString& _property,
330                      const QVariant& _value,
331                      bool _update = true );
332   virtual void Execute();
333 };
334   
335 TSetPropertyEvent::TSetPropertyEvent( const QString& _entry,
336                                       const QString& _property,
337                                       const QVariant& _value,
338                                       bool _update ):
339   myEntry( _entry ),
340   myProperty( _property ),
341   myValue( _value ),
342   myUpdateViewer( _update )
343 {
344 }
345
346 void TSetPropertyEvent::Execute()
347 {
348   SUIT_Application* app = SUIT_Session::session()->activeApplication();
349   if ( !app ) return;
350   
351   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
352   if ( !study ) return;
353   
354   GEOM_Displayer displayer;
355   
356   SALOME_View* window = displayer.GetActiveView();
357   if ( !window ) return;
358   
359   int mgrId = dynamic_cast<SUIT_ViewModel*>( window )->getViewManager()->getGlobalId();
360
361   study->setObjectProperty( mgrId, myEntry, myProperty, myValue );
362   
363   Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( myEntry.toUtf8().data(), "GEOM" );
364   if ( window->isVisible( io ) ) displayer.Redisplay( io, myUpdateViewer );
365 }
366
367 /*!
368   \brief Set display mode to the presentation
369   \param theEntry geometry object's entry
370   \param theMode display mode: 0 - wireframe, 1 - shading, 2 - shading+edges, 3-textured
371   \param theUpdateViewer \c true to update active view's contents
372 */
373 void GEOM_Swig::setDisplayMode( const char* theEntry, int theMode, bool theUpdateViewer )
374 {
375   ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::DisplayMode ), 
376                                            theMode, theUpdateViewer ) );
377 }
378
379 /*!
380   \brief Show / hide edges direction vectors for the presentation
381   \param theEntry geometry object's entry
382   \param theOn \c true to show edges direction vectors or \c false otherwise
383   \param theUpdateViewer \c true to update active view's contents
384 */
385 void GEOM_Swig::setVectorsMode( const char* theEntry, bool theOn, bool theUpdateViewer )
386 {
387   ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::EdgesDirection ), 
388                                            theOn, theUpdateViewer ) );
389 }
390
391 /*!
392   \brief Show / hide vertices for the presentation
393   \param theEntry geometry object's entry
394   \param theOn \c true to show vertices or \c false otherwise
395   \param theUpdateViewer \c true to update active view's contents
396 */
397 void GEOM_Swig::setVerticesMode( const char* theEntry, bool theOn, bool theUpdateViewer )
398 {
399   ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Vertices ), 
400                                            theOn, theUpdateViewer ) );
401 }
402
403 /*!
404   \brief Show / hide name of shape for the presentation
405   \param theEntry geometry object's entry
406   \param theOn \c true to show name or \c false otherwise
407   \param theUpdateViewer \c true to update active view's contents
408 */
409 void GEOM_Swig::setNameMode( const char* theEntry, bool theOn, bool theUpdateViewer )
410 {
411   ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::ShowName ),
412                                            theOn, theUpdateViewer ) );
413 }
414
415 /*!
416   \brief Change color of the presentation
417   \param theEntry geometry object's entry
418   \param theRed red component of the component (0-255)
419   \param theGreen green component of the component (0-255)
420   \param theBlue blue component of the component (0-255)
421   \param theUpdateViewer \c true to update active view's contents
422 */
423 void GEOM_Swig::setColor( const char* theEntry, int theRed, int theGreen, int theBlue, bool theUpdateViewer )
424 {
425   ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Color ), 
426                                            QColor( theRed, theGreen, theBlue ), theUpdateViewer ) );
427 }
428
429 /*!
430   \brief Set number of iso-lines to the presentation
431   \param theEntry geometry object's entry
432   \param theNbU number of iso-lines along U axis (integer value >= 0)
433   \param theNbV number of iso-lines along V axis (integer value >= 0)
434   \param theUpdateViewer \c true to update active view's contents
435 */
436 void GEOM_Swig::setIsos( const char* theEntry, int theNbU, int theNbV, bool theUpdateViewer )
437 {
438   ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::NbIsos ), 
439                                            QString( "%1%2%3" ).arg( theNbU ).arg( GEOM::subSectionSeparator() ).arg( theNbV ), 
440                                            theUpdateViewer ) );
441 }
442
443 /*!
444   \brief Set transparency of the presentation
445   \param theEntry geometry object's entry
446   \param theTransparency transparency (floating point value between 0 and 1)
447   \param theUpdateViewer \c true to update active view's contents
448 */
449 void GEOM_Swig::setTransparency( const char* theEntry, float theTransparency, bool theUpdateViewer )
450 {
451   ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Transparency ), 
452                                            theTransparency, theUpdateViewer ) );
453 }
454
455 /*!
456   \brief Set deflection coefficient of the presentation
457   \param theEntry geometry object's entry
458   \param theDeflection deflection coefficient (floating point value)
459   \param theUpdateViewer \c true to update active view's contents
460 */
461 void GEOM_Swig::setDeflection( const char* theEntry, float theDeflection, bool theUpdateViewer )
462 {
463   ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Deflection ), 
464                                            theDeflection, theUpdateViewer ) );
465 }
466
467 /*!
468   \brief Set material to the presentation
469   \param theEntry geometry object's entry
470   \param theMaterial material name (string)
471   \param theUpdateViewer \c true to update active view's contents
472 */
473 void GEOM_Swig::setMaterial( const char* theEntry, const char* theMaterial, bool theUpdateViewer )
474 {
475   Material_Model material;
476   material.fromResources( theMaterial );
477   ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Material ), 
478                                            material.toProperties(), theUpdateViewer ) );
479 }
480
481 /*!
482   \brief Set material property to the presentation
483   \param theEntry geometry object's entry
484   \param theMaterial material property string
485   \param theUpdateViewer \c true to update active view's contents
486 */
487 void GEOM_Swig::setMaterialProperty( const char* theEntry, const char* theMaterial, bool theUpdateViewer )
488 {
489   ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Material ), 
490                                            theMaterial, theUpdateViewer ) );
491 }
492
493 /*!
494   \brief initialize local subShapes selection on a given shape
495   \param theEntry geometry object's entry
496   \param theMode from enum TopAbs_ShapeEnum
497  */
498 void GEOM_Swig::initLocalSelection( const char* theEntry, int theMode)
499 {
500   class TEventInitLocalSelection: public SALOME_Event
501   {
502     std::string myEntry;
503     int myMode;
504   public:
505     TEventInitLocalSelection(const char* _entry, int _mode)
506     : myEntry(_entry), myMode(_mode)
507     {}
508     virtual void Execute()
509     {
510       if (myLocalSelector)
511         return;
512       SUIT_Application* app = SUIT_Session::session()->activeApplication();
513       if ( app )
514         {
515           //SUIT_ViewWindow* window = app->desktop()->activeWindow();
516           SUIT_ViewWindow* wnd = 0;
517           LightApp_Application* lapp = dynamic_cast<LightApp_Application*>(app);
518           if ( lapp )
519             {
520               SUIT_ViewManager* viewMgr = lapp->activeViewManager();
521               if ( viewMgr )
522                 {
523                   wnd = viewMgr->getActiveView();
524                 }
525               LightApp_SelectionMgr* mgr = lapp->selectionMgr();
526               if (!myOCCSelector)
527                 {
528                   myOCCSelector = new GEOMGUI_OCCSelector( ((OCCViewer_ViewManager*)viewMgr)->getOCCViewer(), mgr );
529                 }
530
531               QList<SUIT_Selector*> aSelectorList;
532               mgr->selectors( "OCCViewer", aSelectorList );
533               for (int i=0; i< aSelectorList.size(); ++i)
534                 {
535                   if ( LightApp_OCCSelector* aSelector = dynamic_cast<LightApp_OCCSelector*>( aSelectorList.at(i) ) )
536                     {
537                       aSelector->setEnabled(false);
538                     }
539                   if ( GEOMGUI_OCCSelector* aSelector = dynamic_cast<GEOMGUI_OCCSelector*>( aSelectorList.at(i) ) )
540                     {
541                       aSelector->setEnabled(true);
542                     }
543                 }
544               myOCCSelector->setEnabled(true);
545             }
546
547           myLocalSelector = new GEOM_Swig_LocalSelector(app->desktop(), wnd, myEntry, myMode);
548           MESSAGE("TEventInitLocalSelection myLocalSelector: " << myLocalSelector);
549         }
550     }
551   };
552
553   ProcessVoidEvent(new TEventInitLocalSelection(theEntry, theMode));
554 }
555
556 /*!
557   \brief get local subShapes selection on a given shape
558   \return a list of selected subShapes indexes
559  */
560 std::vector<int> GEOM_Swig::getLocalSelection()
561 {
562   class TEventGetLocalSelection: public SALOME_Event
563   {
564   public:
565     typedef std::vector<int> TResult;
566     TResult myResult;
567
568     TEventGetLocalSelection(){}
569
570     virtual void Execute()
571     {
572       MESSAGE("TEventGetLocalSelection myLocalSelector: " << myLocalSelector);
573       if (myLocalSelector)
574         myResult = myLocalSelector->getSelection();
575     }
576   };
577
578   std::vector<int> result = ProcessEvent(new TEventGetLocalSelection());
579   return result;
580 }
581
582 /*!
583   \brief close local subShapes selection on a given shape
584  */
585 void GEOM_Swig::closeLocalSelection()
586 {
587   class TEventCloseLocalSelection: public SALOME_Event
588   {
589   public:
590     TEventCloseLocalSelection()
591     {}
592     virtual void Execute()
593     {
594       MESSAGE("TEventCloseLocalSelection myLocalSelector: " << myLocalSelector);
595       if (myLocalSelector)
596         {
597           delete myLocalSelector;
598           myLocalSelector = 0;
599         }
600     }
601   };
602
603   ProcessVoidEvent(new TEventCloseLocalSelection());
604 }
605
606 class TInitGeomGenEvent: public SALOME_Event
607 {
608 public:
609   typedef bool TResult;
610   TResult myResult;
611   TInitGeomGenEvent() : myResult(false)
612   {}
613   virtual void Execute()
614   {
615     myResult = GeometryGUI::InitGeomGen();
616   }
617 };
618
619 /*!
620   \brief Initialize GEOM module's engine
621   \return \c true if initialization succeeds or \c false otherwise
622 */
623 bool GEOM_Swig::initGeomGen()
624 {
625   return ProcessEvent( new TInitGeomGenEvent() );
626 }