1 // Copyright (C) 2007-2020 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : libGEOM_Swig.cxx
23 // Author : Nicolas REJNERI, Paul RASCLE
25 #include "libGEOM_Swig.h"
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"
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>
46 #include <SALOME_Event.h>
47 #include <utilities.h>
50 #include <SALOMEconfig.h>
51 #include CORBA_SERVER_HEADER(GEOM_Gen)
53 GEOM_Swig_LocalSelector* GEOM_Swig::myLocalSelector = 0;
54 GEOMGUI_OCCSelector* GEOM_Swig::myOCCSelector =0;
59 GEOM_Swig::GEOM_Swig( bool updateOB )
67 GEOM_Swig::~GEOM_Swig()
72 \brief Internal initialization
74 void GEOM_Swig::init( bool updateOB )
76 class TEvent: public SALOME_Event
80 TEvent( bool _updateOB ):
83 virtual void Execute()
86 SUIT_Application* app = SUIT_Session::session()->activeApplication();
89 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
92 _PTR(Study) studyDS( study->studyDS() );
93 _PTR(StudyBuilder) builder = studyDS->NewBuilder();
95 // get/init GEOM engine
96 GEOM::GEOM_Gen_var engine = GeometryGUI::GetGeomGen();
97 if ( CORBA::is_nil( engine ) )
100 // find GEOM component in the study
101 _PTR(SComponent) component = studyDS->FindComponent( "GEOM" );
105 // load GEOM data (if it is not done yet)
107 if ( !component->ComponentIOR( ior ) ) {
108 CORBA::String_var engineIOR = SalomeApp_Application::orb()->object_to_string( engine );
109 builder->LoadWith( component, engineIOR.in() );
112 // update Object browser
113 if (myUpdateOB && dynamic_cast<SalomeApp_Application*>( app ) )
114 dynamic_cast<SalomeApp_Application*>( app )->updateObjectBrowser( true );
116 //myLocalSelector = 0;
120 ProcessVoidEvent( new TEvent(updateOB) );
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
128 void GEOM_Swig::createAndDisplayGO( const char* theEntry, bool theUpdateViewer )
130 class TEvent: public SALOME_Event
135 TEvent( const char* _entry, bool _update ):
136 myEntry( _entry ), myUpdateViewer( _update )
138 virtual void Execute()
140 Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( myEntry.c_str(), "GEOM", "" );
142 GEOM_Displayer().Display( io, myUpdateViewer );
146 ProcessVoidEvent( new TEvent( theEntry, theUpdateViewer ) );
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
154 void GEOM_Swig::createAndDisplayFitAllGO( const char* theEntry )
157 createAndDisplayGO( theEntry );
160 class TEventFitAll: public SALOME_Event
164 virtual void Execute()
166 SUIT_Application* app = SUIT_Session::session()->activeApplication();
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();
177 ProcessVoidEvent( new TEventFitAll() );
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
185 void GEOM_Swig::eraseGO( const char* theEntry, bool theUpdateViewer )
187 class TEvent: public SALOME_Event
192 TEvent( const char* _entry, bool _update ):
193 myEntry( _entry ), myUpdateViewer( _update )
195 virtual void Execute()
197 Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( myEntry.c_str(), "GEOM", "" );
199 GEOM_Displayer().Erase( io, true, myUpdateViewer );
203 ProcessVoidEvent( new TEvent( theEntry, theUpdateViewer ) );
207 \brief Update active viewer contents
209 void GEOM_Swig::UpdateViewer()
211 class TEventUpdateViewer: public SALOME_Event
216 virtual void Execute()
218 GEOM_Displayer().UpdateViewer();
222 ProcessVoidEvent(new TEventUpdateViewer());
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)
231 int GEOM_Swig::getIndexTopology( const char* theSubIOR, const char* theMainIOR )
236 GEOM::GEOM_Gen_var aGeomGen = GeometryGUI::GetGeomGen();
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() );
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 );
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)
261 const char* GEOM_Swig::getShapeTypeString( const char* theIOR )
263 QString aTypeName = "Shape of unknown type";
266 GEOM::GEOM_Gen_var aGeomGen = GeometryGUI::GetGeomGen();
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() );
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 );
280 return strdup( qPrintable( aTypeName ) );
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)
288 const char* GEOM_Swig::getShapeTypeIcon( const char* theIOR )
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"
301 const char* anIcon = "None";
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 ];
314 catch ( CORBA::Exception& ) {
320 class TSetPropertyEvent: public SALOME_Event
328 TSetPropertyEvent( const QString& _entry,
329 const QString& _property,
330 const QVariant& _value,
331 bool _update = true );
332 virtual void Execute();
335 TSetPropertyEvent::TSetPropertyEvent( const QString& _entry,
336 const QString& _property,
337 const QVariant& _value,
340 myProperty( _property ),
342 myUpdateViewer( _update )
346 void TSetPropertyEvent::Execute()
348 SUIT_Application* app = SUIT_Session::session()->activeApplication();
351 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
352 if ( !study ) return;
354 GEOM_Displayer displayer;
356 SALOME_View* window = displayer.GetActiveView();
357 if ( !window ) return;
359 int mgrId = dynamic_cast<SUIT_ViewModel*>( window )->getViewManager()->getGlobalId();
361 study->setObjectProperty( mgrId, myEntry, myProperty, myValue );
363 Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( myEntry.toUtf8().data(), "GEOM" );
364 if ( window->isVisible( io ) ) displayer.Redisplay( io, myUpdateViewer );
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
373 void GEOM_Swig::setDisplayMode( const char* theEntry, int theMode, bool theUpdateViewer )
375 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::DisplayMode ),
376 theMode, theUpdateViewer ) );
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
385 void GEOM_Swig::setVectorsMode( const char* theEntry, bool theOn, bool theUpdateViewer )
387 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::EdgesDirection ),
388 theOn, theUpdateViewer ) );
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
397 void GEOM_Swig::setVerticesMode( const char* theEntry, bool theOn, bool theUpdateViewer )
399 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Vertices ),
400 theOn, theUpdateViewer ) );
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
409 void GEOM_Swig::setNameMode( const char* theEntry, bool theOn, bool theUpdateViewer )
411 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::ShowName ),
412 theOn, theUpdateViewer ) );
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
423 void GEOM_Swig::setColor( const char* theEntry, int theRed, int theGreen, int theBlue, bool theUpdateViewer )
425 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Color ),
426 QColor( theRed, theGreen, theBlue ), theUpdateViewer ) );
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
436 void GEOM_Swig::setIsos( const char* theEntry, int theNbU, int theNbV, bool theUpdateViewer )
438 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::NbIsos ),
439 QString( "%1%2%3" ).arg( theNbU ).arg( GEOM::subSectionSeparator() ).arg( theNbV ),
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
449 void GEOM_Swig::setTransparency( const char* theEntry, float theTransparency, bool theUpdateViewer )
451 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Transparency ),
452 theTransparency, theUpdateViewer ) );
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
461 void GEOM_Swig::setDeflection( const char* theEntry, float theDeflection, bool theUpdateViewer )
463 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Deflection ),
464 theDeflection, theUpdateViewer ) );
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
473 void GEOM_Swig::setMaterial( const char* theEntry, const char* theMaterial, bool theUpdateViewer )
475 Material_Model material;
476 material.fromResources( theMaterial );
477 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Material ),
478 material.toProperties(), theUpdateViewer ) );
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
487 void GEOM_Swig::setMaterialProperty( const char* theEntry, const char* theMaterial, bool theUpdateViewer )
489 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Material ),
490 theMaterial, theUpdateViewer ) );
494 \brief initialize local subShapes selection on a given shape
495 \param theEntry geometry object's entry
496 \param theMode from enum TopAbs_ShapeEnum
498 void GEOM_Swig::initLocalSelection( const char* theEntry, int theMode)
500 class TEventInitLocalSelection: public SALOME_Event
505 TEventInitLocalSelection(const char* _entry, int _mode)
506 : myEntry(_entry), myMode(_mode)
508 virtual void Execute()
512 SUIT_Application* app = SUIT_Session::session()->activeApplication();
515 //SUIT_ViewWindow* window = app->desktop()->activeWindow();
516 SUIT_ViewWindow* wnd = 0;
517 LightApp_Application* lapp = dynamic_cast<LightApp_Application*>(app);
520 SUIT_ViewManager* viewMgr = lapp->activeViewManager();
523 wnd = viewMgr->getActiveView();
525 LightApp_SelectionMgr* mgr = lapp->selectionMgr();
528 myOCCSelector = new GEOMGUI_OCCSelector( ((OCCViewer_ViewManager*)viewMgr)->getOCCViewer(), mgr );
531 QList<SUIT_Selector*> aSelectorList;
532 mgr->selectors( "OCCViewer", aSelectorList );
533 for (int i=0; i< aSelectorList.size(); ++i)
535 if ( LightApp_OCCSelector* aSelector = dynamic_cast<LightApp_OCCSelector*>( aSelectorList.at(i) ) )
537 aSelector->setEnabled(false);
539 if ( GEOMGUI_OCCSelector* aSelector = dynamic_cast<GEOMGUI_OCCSelector*>( aSelectorList.at(i) ) )
541 aSelector->setEnabled(true);
544 myOCCSelector->setEnabled(true);
547 myLocalSelector = new GEOM_Swig_LocalSelector(app->desktop(), wnd, myEntry, myMode);
548 MESSAGE("TEventInitLocalSelection myLocalSelector: " << myLocalSelector);
553 ProcessVoidEvent(new TEventInitLocalSelection(theEntry, theMode));
557 \brief get local subShapes selection on a given shape
558 \return a list of selected subShapes indexes
560 std::vector<int> GEOM_Swig::getLocalSelection()
562 class TEventGetLocalSelection: public SALOME_Event
565 typedef std::vector<int> TResult;
568 TEventGetLocalSelection(){}
570 virtual void Execute()
572 MESSAGE("TEventGetLocalSelection myLocalSelector: " << myLocalSelector);
574 myResult = myLocalSelector->getSelection();
578 std::vector<int> result = ProcessEvent(new TEventGetLocalSelection());
583 \brief close local subShapes selection on a given shape
585 void GEOM_Swig::closeLocalSelection()
587 class TEventCloseLocalSelection: public SALOME_Event
590 TEventCloseLocalSelection()
592 virtual void Execute()
594 MESSAGE("TEventCloseLocalSelection myLocalSelector: " << myLocalSelector);
597 delete myLocalSelector;
603 ProcessVoidEvent(new TEventCloseLocalSelection());
606 class TInitGeomGenEvent: public SALOME_Event
609 typedef bool TResult;
611 TInitGeomGenEvent() : myResult(false)
613 virtual void Execute()
615 myResult = GeometryGUI::InitGeomGen();
620 \brief Initialize GEOM module's engine
621 \return \c true if initialization succeeds or \c false otherwise
623 bool GEOM_Swig::initGeomGen()
625 return ProcessEvent( new TInitGeomGenEvent() );