1 // Copyright (C) 2007-2024 CEA, EDF, 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()
69 // Delete selector because of sigsegv in TEventInitLocalSelection::Execute()
70 // when call GEOM_Swig.initLocalSelection() from Python, close the study, create a new one and
75 myOCCSelector = nullptr;
80 \brief Internal initialization
82 void GEOM_Swig::init( bool updateOB )
84 class TEvent: public SALOME_Event
88 TEvent( bool _updateOB ):
91 virtual void Execute()
94 SUIT_Application* app = SUIT_Session::session()->activeApplication();
97 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
100 _PTR(Study) studyDS( study->studyDS() );
101 _PTR(StudyBuilder) builder = studyDS->NewBuilder();
103 // get/init GEOM engine
104 GEOM::GEOM_Gen_var engine = GeometryGUI::GetGeomGen();
105 if ( CORBA::is_nil( engine ) )
108 // find GEOM component in the study
109 _PTR(SComponent) component = studyDS->FindComponent( "GEOM" );
113 // load GEOM data (if it is not done yet)
115 if ( !component->ComponentIOR( ior ) ) {
116 CORBA::String_var engineIOR = SalomeApp_Application::orb()->object_to_string( engine );
117 builder->LoadWith( component, engineIOR.in() );
120 // update Object browser
121 if (myUpdateOB && dynamic_cast<SalomeApp_Application*>( app ) )
122 dynamic_cast<SalomeApp_Application*>( app )->updateObjectBrowser( true );
124 //myLocalSelector = 0;
128 ProcessVoidEvent( new TEvent(updateOB) );
132 \brief Display the presenation in the currently active view
133 \param theEntry geometry object's entry
134 \param theUpdateViewer \c true to update active view's contents
136 void GEOM_Swig::createAndDisplayGO( const char* theEntry, bool theUpdateViewer )
138 class TEvent: public SALOME_Event
143 TEvent( const char* _entry, bool _update ):
144 myEntry( _entry ), myUpdateViewer( _update )
146 virtual void Execute()
148 Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( myEntry.c_str(), "GEOM", "" );
150 GEOM_Displayer().Display( io, myUpdateViewer );
154 ProcessVoidEvent( new TEvent( theEntry, theUpdateViewer ) );
158 \brief Same as createAndDisplayGO, but also fits the active view to the contents
159 \param theEntry geometry object's entry
160 \param theUpdateViewer \c true to update active view's contents
162 void GEOM_Swig::createAndDisplayFitAllGO( const char* theEntry )
165 createAndDisplayGO( theEntry );
168 class TEventFitAll: public SALOME_Event
172 virtual void Execute()
174 SUIT_Application* app = SUIT_Session::session()->activeApplication();
176 SUIT_ViewWindow* window = app->desktop()->activeWindow();
177 if ( dynamic_cast<SVTK_ViewWindow*>( window ) )
178 dynamic_cast<SVTK_ViewWindow*>( window )->onFitAll();
179 else if ( dynamic_cast<OCCViewer_ViewFrame*>( window ) )
180 dynamic_cast<OCCViewer_ViewFrame*>( window )->onViewFitAll();
185 ProcessVoidEvent( new TEventFitAll() );
189 \brief Erase presentation in the currently active viewer
190 \param theEntry geometry object's entry
191 \param theUpdateViewer \c true to update active view's contents
193 void GEOM_Swig::eraseGO( const char* theEntry, bool theUpdateViewer )
195 class TEvent: public SALOME_Event
200 TEvent( const char* _entry, bool _update ):
201 myEntry( _entry ), myUpdateViewer( _update )
203 virtual void Execute()
205 Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( myEntry.c_str(), "GEOM", "" );
207 GEOM_Displayer().Erase( io, true, myUpdateViewer );
211 ProcessVoidEvent( new TEvent( theEntry, theUpdateViewer ) );
215 \brief Update active viewer contents
217 void GEOM_Swig::UpdateViewer()
219 class TEventUpdateViewer: public SALOME_Event
224 virtual void Execute()
226 GEOM_Displayer().UpdateViewer();
230 ProcessVoidEvent(new TEventUpdateViewer());
234 \brief Get sub-shape index inside main shape
235 \param theSubIOR sub-shape geometry object's IOR
236 \param theMainIOR main shape geometry object's IOR
237 \return sub-shape index (-1 in case of error)
239 int GEOM_Swig::getIndexTopology( const char* theSubIOR, const char* theMainIOR )
244 GEOM::GEOM_Gen_var aGeomGen = GeometryGUI::GetGeomGen();
246 // get main shape's geom object by IOR
247 CORBA::Object_var anObject = SalomeApp_Application::orb()->string_to_object( theMainIOR );
248 GEOM::GEOM_Object_var aMainShape = GEOM::GEOM_Object::_narrow( anObject.in() );
249 // get sub-shape's geom object by IOR
250 anObject = SalomeApp_Application::orb()->string_to_object( theSubIOR );
251 GEOM::GEOM_Object_var aSubShape = GEOM::GEOM_Object::_narrow( anObject.in() );
253 if ( !CORBA::is_nil( aGeomGen ) && !CORBA::is_nil( aMainShape ) && !CORBA::is_nil( aSubShape ) ) {
254 // get shapes operations interface
255 GEOM::GEOM_IShapesOperations_var anIShapesOperations =
256 aGeomGen->GetIShapesOperations();
257 if ( !CORBA::is_nil( anIShapesOperations ) )
258 index = anIShapesOperations->GetTopologyIndex( aMainShape, aSubShape );
265 \brief Get shape type name
266 \param theIOR geometry object's IOR
267 \return shape type name ("Shape of unknown type" in case of error)
269 const char* GEOM_Swig::getShapeTypeString( const char* theIOR )
271 QString aTypeName = "Shape of unknown type";
274 GEOM::GEOM_Gen_var aGeomGen = GeometryGUI::GetGeomGen();
276 // get shape's geom object by IOR
277 CORBA::Object_var anObject = SalomeApp_Application::orb()->string_to_object( theIOR );
278 GEOM::GEOM_Object_var aShape = GEOM::GEOM_Object::_narrow( anObject.in() );
280 if ( !CORBA::is_nil( aGeomGen ) && !CORBA::is_nil( aShape ) ) {
281 // get shapes operations interface
282 GEOM::GEOM_IShapesOperations_var anIShapesOperations =
283 aGeomGen->GetIShapesOperations();
284 if ( !CORBA::is_nil( anIShapesOperations ) )
285 aTypeName = anIShapesOperations->GetShapeTypeString( aShape );
288 return strdup( qPrintable( aTypeName ) );
292 \brief Get shape's icon ID (specified by its type)
293 \param theIOR geometry object's IOR
294 \return icon ID ("None" in case of error)
296 const char* GEOM_Swig::getShapeTypeIcon( const char* theIOR )
298 static const char* icons[] = {
299 "ICON_OBJBROWSER_COMPOUND",
300 "ICON_OBJBROWSER_COMPSOLID",
301 "ICON_OBJBROWSER_SOLID",
302 "ICON_OBJBROWSER_SHELL",
303 "ICON_OBJBROWSER_FACE",
304 "ICON_OBJBROWSER_WIRE",
305 "ICON_OBJBROWSER_EDGE",
306 "ICON_OBJBROWSER_VERTEX"
309 const char* anIcon = "None";
312 CORBA::Object_var anObject = SalomeApp_Application::orb()->string_to_object( theIOR );
313 if ( !CORBA::is_nil( anObject ) ) {
314 GEOM::GEOM_Object_var aShape = GEOM::GEOM_Object::_narrow( anObject.in() );
315 if ( !CORBA::is_nil( aShape ) ) {
316 GEOM::shape_type aType = aShape->GetShapeType();
317 if ( aType >= GEOM::COMPOUND && aType < GEOM::SHAPE )
318 anIcon = icons[ (int)aType ];
322 catch ( CORBA::Exception& ) {
328 class TSetPropertyEvent: public SALOME_Event
336 TSetPropertyEvent( const QString& _entry,
337 const QString& _property,
338 const QVariant& _value,
339 bool _update = true );
340 virtual void Execute();
343 TSetPropertyEvent::TSetPropertyEvent( const QString& _entry,
344 const QString& _property,
345 const QVariant& _value,
348 myProperty( _property ),
350 myUpdateViewer( _update )
354 void TSetPropertyEvent::Execute()
356 SUIT_Application* app = SUIT_Session::session()->activeApplication();
359 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
360 if ( !study ) return;
362 GEOM_Displayer displayer;
364 SALOME_View* window = displayer.GetActiveView();
365 if ( !window ) return;
367 int mgrId = dynamic_cast<SUIT_ViewModel*>( window )->getViewManager()->getGlobalId();
369 study->setObjectProperty( mgrId, myEntry, myProperty, myValue );
371 Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( myEntry.toUtf8().data(), "GEOM" );
372 if ( window->isVisible( io ) ) displayer.Redisplay( io, myUpdateViewer );
376 \brief Set display mode to the presentation
377 \param theEntry geometry object's entry
378 \param theMode display mode: 0 - wireframe, 1 - shading, 2 - shading+edges, 3-textured
379 \param theUpdateViewer \c true to update active view's contents
381 void GEOM_Swig::setDisplayMode( const char* theEntry, int theMode, bool theUpdateViewer )
383 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::DisplayMode ),
384 theMode, theUpdateViewer ) );
388 \brief Show / hide edges direction vectors for the presentation
389 \param theEntry geometry object's entry
390 \param theOn \c true to show edges direction vectors or \c false otherwise
391 \param theUpdateViewer \c true to update active view's contents
393 void GEOM_Swig::setVectorsMode( const char* theEntry, bool theOn, bool theUpdateViewer )
395 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::EdgesDirection ),
396 theOn, theUpdateViewer ) );
400 \brief Show / hide vertices for the presentation
401 \param theEntry geometry object's entry
402 \param theOn \c true to show vertices or \c false otherwise
403 \param theUpdateViewer \c true to update active view's contents
405 void GEOM_Swig::setVerticesMode( const char* theEntry, bool theOn, bool theUpdateViewer )
407 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Vertices ),
408 theOn, theUpdateViewer ) );
412 \brief Show / hide name of shape for the presentation
413 \param theEntry geometry object's entry
414 \param theOn \c true to show name or \c false otherwise
415 \param theUpdateViewer \c true to update active view's contents
417 void GEOM_Swig::setNameMode( const char* theEntry, bool theOn, bool theUpdateViewer )
419 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::ShowName ),
420 theOn, theUpdateViewer ) );
424 \brief Change color of the presentation
425 \param theEntry geometry object's entry
426 \param theRed red component of the component (0-255)
427 \param theGreen green component of the component (0-255)
428 \param theBlue blue component of the component (0-255)
429 \param theUpdateViewer \c true to update active view's contents
431 void GEOM_Swig::setColor( const char* theEntry, int theRed, int theGreen, int theBlue, bool theUpdateViewer )
433 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Color ),
434 QColor( theRed, theGreen, theBlue ), theUpdateViewer ) );
438 \brief Set number of iso-lines to the presentation
439 \param theEntry geometry object's entry
440 \param theNbU number of iso-lines along U axis (integer value >= 0)
441 \param theNbV number of iso-lines along V axis (integer value >= 0)
442 \param theUpdateViewer \c true to update active view's contents
444 void GEOM_Swig::setIsos( const char* theEntry, int theNbU, int theNbV, bool theUpdateViewer )
446 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::NbIsos ),
447 QString( "%1%2%3" ).arg( theNbU ).arg( GEOM::subSectionSeparator() ).arg( theNbV ),
452 \brief Set transparency of the presentation
453 \param theEntry geometry object's entry
454 \param theTransparency transparency (floating point value between 0 and 1)
455 \param theUpdateViewer \c true to update active view's contents
457 void GEOM_Swig::setTransparency( const char* theEntry, float theTransparency, bool theUpdateViewer )
459 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Transparency ),
460 theTransparency, theUpdateViewer ) );
464 \brief Set deflection coefficient of the presentation
465 \param theEntry geometry object's entry
466 \param theDeflection deflection coefficient (floating point value)
467 \param theUpdateViewer \c true to update active view's contents
469 void GEOM_Swig::setDeflection( const char* theEntry, float theDeflection, bool theUpdateViewer )
471 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Deflection ),
472 theDeflection, theUpdateViewer ) );
476 \brief Set material to the presentation
477 \param theEntry geometry object's entry
478 \param theMaterial material name (string)
479 \param theUpdateViewer \c true to update active view's contents
481 void GEOM_Swig::setMaterial( const char* theEntry, const char* theMaterial, bool theUpdateViewer )
483 Material_Model material;
484 material.fromResources( theMaterial );
485 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Material ),
486 material.toProperties(), theUpdateViewer ) );
490 \brief Set material property to the presentation
491 \param theEntry geometry object's entry
492 \param theMaterial material property string
493 \param theUpdateViewer \c true to update active view's contents
495 void GEOM_Swig::setMaterialProperty( const char* theEntry, const char* theMaterial, bool theUpdateViewer )
497 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Material ),
498 theMaterial, theUpdateViewer ) );
502 \brief initialize local subShapes selection on a given shape
503 \param theEntry geometry object's entry
504 \param theMode from enum TopAbs_ShapeEnum
506 void GEOM_Swig::initLocalSelection( const char* theEntry, int theMode)
508 class TEventInitLocalSelection: public SALOME_Event
513 TEventInitLocalSelection(const char* _entry, int _mode)
514 : myEntry(_entry), myMode(_mode)
516 virtual void Execute()
520 SUIT_Application* app = SUIT_Session::session()->activeApplication();
523 //SUIT_ViewWindow* window = app->desktop()->activeWindow();
524 SUIT_ViewWindow* wnd = 0;
525 LightApp_Application* lapp = dynamic_cast<LightApp_Application*>(app);
528 SUIT_ViewManager* viewMgr = lapp->activeViewManager();
531 wnd = viewMgr->getActiveView();
533 LightApp_SelectionMgr* mgr = lapp->selectionMgr();
536 myOCCSelector = new GEOMGUI_OCCSelector( ((OCCViewer_ViewManager*)viewMgr)->getOCCViewer(), mgr );
539 QList<SUIT_Selector*> aSelectorList;
540 mgr->selectors( "OCCViewer", aSelectorList );
541 for (int i=0; i< aSelectorList.size(); ++i)
543 if ( LightApp_OCCSelector* aSelector = dynamic_cast<LightApp_OCCSelector*>( aSelectorList.at(i) ) )
545 aSelector->setEnabled(false);
547 if ( GEOMGUI_OCCSelector* aSelector = dynamic_cast<GEOMGUI_OCCSelector*>( aSelectorList.at(i) ) )
549 aSelector->setEnabled(true);
552 myOCCSelector->setEnabled(true);
555 myLocalSelector = new GEOM_Swig_LocalSelector(app->desktop(), wnd, myEntry, myMode);
556 MESSAGE("TEventInitLocalSelection myLocalSelector: " << myLocalSelector);
561 ProcessVoidEvent(new TEventInitLocalSelection(theEntry, theMode));
565 \brief get local subShapes selection on a given shape
566 \return a list of selected subShapes indexes
568 std::vector<int> GEOM_Swig::getLocalSelection()
570 class TEventGetLocalSelection: public SALOME_Event
573 typedef std::vector<int> TResult;
576 TEventGetLocalSelection(){}
578 virtual void Execute()
580 MESSAGE("TEventGetLocalSelection myLocalSelector: " << myLocalSelector);
582 myResult = myLocalSelector->getSelection();
586 std::vector<int> result = ProcessEvent(new TEventGetLocalSelection());
592 \brief Set local subShapes selection on a given shape
593 \param ids sub-shapes ids
595 void GEOM_Swig::setLocalSelection(const std::vector<int> ids)
597 class TEventSetLocalSelection: public SALOME_Event
600 typedef std::vector<int> TResult;
604 TEventSetLocalSelection(const std::vector<int> _ids) : myIds(_ids) {}
606 virtual void Execute()
608 MESSAGE("TEventSetLocalSelection myLocalSelector: " << myLocalSelector);
610 myLocalSelector->setSelection(myIds);
614 ProcessEvent(new TEventSetLocalSelection(ids));
618 \brief close local subShapes selection on a given shape
620 void GEOM_Swig::closeLocalSelection()
622 class TEventCloseLocalSelection: public SALOME_Event
625 TEventCloseLocalSelection()
627 virtual void Execute()
629 MESSAGE("TEventCloseLocalSelection myLocalSelector: " << myLocalSelector);
632 delete myLocalSelector;
638 ProcessVoidEvent(new TEventCloseLocalSelection());
641 class TInitGeomGenEvent: public SALOME_Event
644 typedef bool TResult;
646 TInitGeomGenEvent() : myResult(false)
648 virtual void Execute()
650 myResult = GeometryGUI::InitGeomGen();
655 \brief Initialize GEOM module's engine
656 \return \c true if initialization succeeds or \c false otherwise
658 bool GEOM_Swig::initGeomGen()
660 return ProcessEvent( new TInitGeomGenEvent() );