1 // Copyright (C) 2007-2016 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 Show / hide vertices for the presentation
396 \param theEntry geometry object's entry
397 \param theOn \c true to show vertices or \c false otherwise
398 \param theUpdateViewer \c true to update active view's contents
400 void GEOM_Swig::setVerticesMode( const char* theEntry, bool theOn, bool theUpdateViewer )
402 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Vertices ),
403 theOn, theUpdateViewer ) );
407 \brief Show / hide name of shape for the presentation
408 \param theEntry geometry object's entry
409 \param theOn \c true to show name or \c false otherwise
410 \param theUpdateViewer \c true to update active view's contents
412 void GEOM_Swig::setNameMode( const char* theEntry, bool theOn, bool theUpdateViewer )
414 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::ShowName ),
415 theOn, theUpdateViewer ) );
419 \brief Change color of the presentation
420 \param theEntry geometry object's entry
421 \param theRed red component of the component (0-255)
422 \param theGreen green component of the component (0-255)
423 \param theBlue blue component of the component (0-255)
424 \param theUpdateViewer \c true to update active view's contents
426 void GEOM_Swig::setColor( const char* theEntry, int theRed, int theGreen, int theBlue, bool theUpdateViewer )
428 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Color ),
429 QColor( theRed, theGreen, theBlue ), theUpdateViewer ) );
433 \brief Set number of iso-lines to the presentation
434 \param theEntry geometry object's entry
435 \param theNbU number of iso-lines along U axis (interger value >= 0)
436 \param theNbV number of iso-lines along V axis (interger value >= 0)
437 \param theUpdateViewer \c true to update active view's contents
439 void GEOM_Swig::setIsos( const char* theEntry, int theNbU, int theNbV, bool theUpdateViewer )
441 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::NbIsos ),
442 QString( "%1%2%3" ).arg( theNbU ).arg( GEOM::subSectionSeparator() ).arg( theNbV ),
447 \brief Set transparency of the presentation
448 \param theEntry geometry object's entry
449 \param theTransparency transparency (floating point value between 0 and 1)
450 \param theUpdateViewer \c true to update active view's contents
452 void GEOM_Swig::setTransparency( const char* theEntry, float theTransparency, bool theUpdateViewer )
454 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Transparency ),
455 theTransparency, theUpdateViewer ) );
459 \brief Set deflection coefficient of the presentation
460 \param theEntry geometry object's entry
461 \param theDeflection deflection coefficient (floating point value)
462 \param theUpdateViewer \c true to update active view's contents
464 void GEOM_Swig::setDeflection( const char* theEntry, float theDeflection, bool theUpdateViewer )
466 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Deflection ),
467 theDeflection, theUpdateViewer ) );
471 \brief Set material to the presentation
472 \param theEntry geometry object's entry
473 \param theMaterial material name (string)
474 \param theUpdateViewer \c true to update active view's contents
476 void GEOM_Swig::setMaterial( const char* theEntry, const char* theMaterial, bool theUpdateViewer )
478 Material_Model material;
479 material.fromResources( theMaterial );
480 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Material ),
481 material.toProperties(), theUpdateViewer ) );
485 \brief Set material property to the presentation
486 \param theEntry geometry object's entry
487 \param theMaterial material property string
488 \param theUpdateViewer \c true to update active view's contents
490 void GEOM_Swig::setMaterialProperty( const char* theEntry, const char* theMaterial, bool theUpdateViewer )
492 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Material ),
493 theMaterial, theUpdateViewer ) );
496 class TInitGeomGenEvent: public SALOME_Event
499 typedef bool TResult;
501 TInitGeomGenEvent() : myResult(false)
503 virtual void Execute()
505 myResult = GeometryGUI::InitGeomGen();
510 \brief Initialize GEOM module's engine
511 \return \c true if initialization succeedes or \c false otherwise
513 bool GEOM_Swig::initGeomGen()
515 return ProcessEvent( new TInitGeomGenEvent() );