1 // Copyright (C) 2007-2014 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"
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>
41 #include <SALOME_Event.h>
44 #include <SALOMEconfig.h>
45 #include CORBA_SERVER_HEADER(GEOM_Gen)
50 GEOM_Swig::GEOM_Swig()
58 GEOM_Swig::~GEOM_Swig()
63 \brief Internal initialization
65 void GEOM_Swig::init()
67 class TEvent: public SALOME_Event
72 virtual void Execute()
75 SUIT_Application* app = SUIT_Session::session()->activeApplication();
78 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
81 _PTR(Study) studyDS( study->studyDS() );
82 _PTR(StudyBuilder) builder = studyDS->NewBuilder();
84 // get/init GEOM engine
85 GEOM::GEOM_Gen_var engine = GeometryGUI::GetGeomGen();
86 if ( CORBA::is_nil( engine ) )
89 // find GEOM component in the study
90 _PTR(SComponent) component = studyDS->FindComponent( "GEOM" );
94 // load GEOM data (if it is not done yet)
96 if ( !component->ComponentIOR( ior ) ) {
97 CORBA::String_var engineIOR = SalomeApp_Application::orb()->object_to_string( engine );
98 builder->LoadWith( component, engineIOR.in() );
101 // update Object browser
102 if ( dynamic_cast<SalomeApp_Application*>( app ) )
103 dynamic_cast<SalomeApp_Application*>( app )->updateObjectBrowser( true );
107 ProcessVoidEvent( new TEvent() );
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
115 void GEOM_Swig::createAndDisplayGO( const char* theEntry, bool theUpdateViewer )
117 class TEvent: public SALOME_Event
122 TEvent( const char* _entry, bool _update ):
123 myEntry( _entry ), myUpdateViewer( _update )
125 virtual void Execute()
127 SUIT_Application* app = SUIT_Session::session()->activeApplication();
129 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
130 if ( !study ) return;
132 Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( myEntry.c_str(), "GEOM", "" );
134 GEOM_Displayer( study ).Display( io, myUpdateViewer );
138 ProcessVoidEvent( new TEvent( theEntry, theUpdateViewer ) );
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
146 void GEOM_Swig::createAndDisplayFitAllGO( const char* theEntry )
149 createAndDisplayGO( theEntry );
152 class TEventFitAll: public SALOME_Event
156 virtual void Execute()
158 SUIT_Application* app = SUIT_Session::session()->activeApplication();
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();
169 ProcessVoidEvent( new TEventFitAll() );
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
177 void GEOM_Swig::eraseGO( const char* theEntry, bool theUpdateViewer )
179 class TEvent: public SALOME_Event
184 TEvent( const char* _entry, bool _update ):
185 myEntry( _entry ), myUpdateViewer( _update )
187 virtual void Execute()
189 SUIT_Application* app = SUIT_Session::session()->activeApplication();
191 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
192 if ( !study ) return;
194 Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( myEntry.c_str(), "GEOM", "" );
196 GEOM_Displayer( study ).Erase( io, true, myUpdateViewer );
200 ProcessVoidEvent( new TEvent( theEntry, theUpdateViewer ) );
204 \brief Update active viewer contents
206 void GEOM_Swig::UpdateViewer()
208 class TEventUpdateViewer: public SALOME_Event
213 virtual void Execute()
215 SUIT_Application* app = SUIT_Session::session()->activeApplication();
218 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
219 if ( !study ) return;
221 GEOM_Displayer( study ).UpdateViewer();
225 ProcessVoidEvent(new TEventUpdateViewer());
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)
234 int GEOM_Swig::getIndexTopology( const char* theSubIOR, const char* theMainIOR )
239 GEOM::GEOM_Gen_var aGeomGen = GeometryGUI::GetGeomGen();
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() );
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 );
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)
264 const char* GEOM_Swig::getShapeTypeString( const char* theIOR )
266 QString aTypeName = "Shape of unknown type";
269 GEOM::GEOM_Gen_var aGeomGen = GeometryGUI::GetGeomGen();
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() );
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 );
283 return strdup( qPrintable( aTypeName ) );
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)
291 const char* GEOM_Swig::getShapeTypeIcon( const char* theIOR )
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"
304 const char* anIcon = "None";
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 ];
317 catch ( CORBA::Exception& ) {
323 class TSetPropertyEvent: public SALOME_Event
331 TSetPropertyEvent( const QString& _entry,
332 const QString& _property,
333 const QVariant& _value,
334 bool _update = true );
335 virtual void Execute();
338 TSetPropertyEvent::TSetPropertyEvent( const QString& _entry,
339 const QString& _property,
340 const QVariant& _value,
343 myProperty( _property ),
345 myUpdateViewer( _update )
349 void TSetPropertyEvent::Execute()
351 SUIT_Application* app = SUIT_Session::session()->activeApplication();
354 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
355 if ( !study ) return;
357 GEOM_Displayer displayer( study );
359 SALOME_View* window = displayer.GetActiveView();
360 if ( !window ) return;
362 int mgrId = dynamic_cast<SUIT_ViewModel*>( window )->getViewManager()->getGlobalId();
364 study->setObjectProperty( mgrId, myEntry, myProperty, myValue );
366 Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( myEntry.toLatin1().data(), "GEOM" );
367 if ( window->isVisible( io ) ) displayer.Redisplay( io, myUpdateViewer );
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
376 void GEOM_Swig::setDisplayMode( const char* theEntry, int theMode, bool theUpdateViewer )
378 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::DisplayMode ),
379 theMode, theUpdateViewer ) );
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
388 void GEOM_Swig::setVectorsMode( const char* theEntry, bool theOn, bool theUpdateViewer )
390 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::EdgesDirection ),
391 theOn, theUpdateViewer ) );
395 \brief Change color of the presentation
396 \param theEntry geometry object's entry
397 \param theRed red component of the component (0-255)
398 \param theGreen green component of the component (0-255)
399 \param theBlue blue component of the component (0-255)
400 \param theUpdateViewer \c true to update active view's contents
402 void GEOM_Swig::setColor( const char* theEntry, int theRed, int theGreen, int theBlue, bool theUpdateViewer )
404 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Color ),
405 QColor( theRed, theGreen, theBlue ), theUpdateViewer ) );
409 \brief Set number of iso-lines to the presentation
410 \param theEntry geometry object's entry
411 \param theNbU number of iso-lines along U axis (interger value >= 0)
412 \param theNbV number of iso-lines along V axis (interger value >= 0)
413 \param theUpdateViewer \c true to update active view's contents
415 void GEOM_Swig::setIsos( const char* theEntry, int theNbU, int theNbV, bool theUpdateViewer )
417 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::NbIsos ),
418 QString( "%1%2%3" ).arg( theNbU ).arg( GEOM::subSectionSeparator() ).arg( theNbV ),
423 \brief Set transparency of the presentation
424 \param theEntry geometry object's entry
425 \param theTransparency transparency (floating point value between 0 and 1)
426 \param theUpdateViewer \c true to update active view's contents
428 void GEOM_Swig::setTransparency( const char* theEntry, float theTransparency, bool theUpdateViewer )
430 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Transparency ),
431 theTransparency, theUpdateViewer ) );
435 \brief Set deflection coefficient of the presentation
436 \param theEntry geometry object's entry
437 \param theDeflection deflection coefficient (floating point value)
438 \param theUpdateViewer \c true to update active view's contents
440 void GEOM_Swig::setDeflection( const char* theEntry, float theDeflection, bool theUpdateViewer )
442 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Deflection ),
443 theDeflection, theUpdateViewer ) );
447 \brief Set material to the presentation
448 \param theEntry geometry object's entry
449 \param theMaterial material name (string)
450 \param theUpdateViewer \c true to update active view's contents
452 void GEOM_Swig::setMaterial( const char* theEntry, const char* theMaterial, bool theUpdateViewer )
454 Material_Model material;
455 material.fromResources( theMaterial );
456 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Material ),
457 material.toProperties(), theUpdateViewer ) );
461 \brief Set material property to the presentation
462 \param theEntry geometry object's entry
463 \param theMaterial material property string
464 \param theUpdateViewer \c true to update active view's contents
466 void GEOM_Swig::setMaterialProperty( const char* theEntry, const char* theMaterial, bool theUpdateViewer )
468 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Material ),
469 theMaterial, theUpdateViewer ) );
472 class TInitGeomGenEvent: public SALOME_Event
475 typedef bool TResult;
477 TInitGeomGenEvent() : myResult(false)
479 virtual void Execute()
481 myResult = GeometryGUI::InitGeomGen();
486 \brief Initialize GEOM module's engine
487 \return \c true if initialization succeedes or \c false otherwise
489 bool GEOM_Swig::initGeomGen()
491 return ProcessEvent( new TInitGeomGenEvent() );