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 Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( myEntry.c_str(), "GEOM", "" );
129 GEOM_Displayer().Display( io, myUpdateViewer );
133 ProcessVoidEvent( new TEvent( theEntry, theUpdateViewer ) );
137 \brief Same as createAndDisplayGO, but also fits the active view to the contents
138 \param theEntry geometry object's entry
139 \param theUpdateViewer \c true to update active view's contents
141 void GEOM_Swig::createAndDisplayFitAllGO( const char* theEntry )
144 createAndDisplayGO( theEntry );
147 class TEventFitAll: public SALOME_Event
151 virtual void Execute()
153 SUIT_Application* app = SUIT_Session::session()->activeApplication();
155 SUIT_ViewWindow* window = app->desktop()->activeWindow();
156 if ( dynamic_cast<SVTK_ViewWindow*>( window ) )
157 dynamic_cast<SVTK_ViewWindow*>( window )->onFitAll();
158 else if ( dynamic_cast<OCCViewer_ViewFrame*>( window ) )
159 dynamic_cast<OCCViewer_ViewFrame*>( window )->onViewFitAll();
164 ProcessVoidEvent( new TEventFitAll() );
168 \brief Erase presentation in the currently active viewer
169 \param theEntry geometry object's entry
170 \param theUpdateViewer \c true to update active view's contents
172 void GEOM_Swig::eraseGO( const char* theEntry, bool theUpdateViewer )
174 class TEvent: public SALOME_Event
179 TEvent( const char* _entry, bool _update ):
180 myEntry( _entry ), myUpdateViewer( _update )
182 virtual void Execute()
184 Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( myEntry.c_str(), "GEOM", "" );
186 GEOM_Displayer().Erase( io, true, myUpdateViewer );
190 ProcessVoidEvent( new TEvent( theEntry, theUpdateViewer ) );
194 \brief Update active viewer contents
196 void GEOM_Swig::UpdateViewer()
198 class TEventUpdateViewer: public SALOME_Event
203 virtual void Execute()
205 GEOM_Displayer().UpdateViewer();
209 ProcessVoidEvent(new TEventUpdateViewer());
213 \brief Get sub-shape index inside main shape
214 \param theSubIOR sub-shape geometry object's IOR
215 \param theMainIOR main shape geometry object's IOR
216 \return sub-shape index (-1 in case of error)
218 int GEOM_Swig::getIndexTopology( const char* theSubIOR, const char* theMainIOR )
223 GEOM::GEOM_Gen_var aGeomGen = GeometryGUI::GetGeomGen();
225 // get main shape's geom object by IOR
226 CORBA::Object_var anObject = SalomeApp_Application::orb()->string_to_object( theMainIOR );
227 GEOM::GEOM_Object_var aMainShape = GEOM::GEOM_Object::_narrow( anObject.in() );
228 // get sub-shape's geom object by IOR
229 anObject = SalomeApp_Application::orb()->string_to_object( theSubIOR );
230 GEOM::GEOM_Object_var aSubShape = GEOM::GEOM_Object::_narrow( anObject.in() );
232 if ( !CORBA::is_nil( aGeomGen ) && !CORBA::is_nil( aMainShape ) && !CORBA::is_nil( aSubShape ) ) {
233 // get shapes operations interface
234 GEOM::GEOM_IShapesOperations_var anIShapesOperations =
235 aGeomGen->GetIShapesOperations();
236 if ( !CORBA::is_nil( anIShapesOperations ) )
237 index = anIShapesOperations->GetTopologyIndex( aMainShape, aSubShape );
244 \brief Get shape type name
245 \param theIOR geometry object's IOR
246 \return shape type name ("Shape of unknown type" in case of error)
248 const char* GEOM_Swig::getShapeTypeString( const char* theIOR )
250 QString aTypeName = "Shape of unknown type";
253 GEOM::GEOM_Gen_var aGeomGen = GeometryGUI::GetGeomGen();
255 // get shape's geom object by IOR
256 CORBA::Object_var anObject = SalomeApp_Application::orb()->string_to_object( theIOR );
257 GEOM::GEOM_Object_var aShape = GEOM::GEOM_Object::_narrow( anObject.in() );
259 if ( !CORBA::is_nil( aGeomGen ) && !CORBA::is_nil( aShape ) ) {
260 // get shapes operations interface
261 GEOM::GEOM_IShapesOperations_var anIShapesOperations =
262 aGeomGen->GetIShapesOperations();
263 if ( !CORBA::is_nil( anIShapesOperations ) )
264 aTypeName = anIShapesOperations->GetShapeTypeString( aShape );
267 return strdup( qPrintable( aTypeName ) );
271 \brief Get shape's icon ID (specified by its type)
272 \param theIOR geometry object's IOR
273 \return icon ID ("None" in case of error)
275 const char* GEOM_Swig::getShapeTypeIcon( const char* theIOR )
277 static const char* icons[] = {
278 "ICON_OBJBROWSER_COMPOUND",
279 "ICON_OBJBROWSER_COMPSOLID",
280 "ICON_OBJBROWSER_SOLID",
281 "ICON_OBJBROWSER_SHELL",
282 "ICON_OBJBROWSER_FACE",
283 "ICON_OBJBROWSER_WIRE",
284 "ICON_OBJBROWSER_EDGE",
285 "ICON_OBJBROWSER_VERTEX"
288 const char* anIcon = "None";
291 CORBA::Object_var anObject = SalomeApp_Application::orb()->string_to_object( theIOR );
292 if ( !CORBA::is_nil( anObject ) ) {
293 GEOM::GEOM_Object_var aShape = GEOM::GEOM_Object::_narrow( anObject.in() );
294 if ( !CORBA::is_nil( aShape ) ) {
295 GEOM::shape_type aType = aShape->GetShapeType();
296 if ( aType >= GEOM::COMPOUND && aType < GEOM::SHAPE )
297 anIcon = icons[ (int)aType ];
301 catch ( CORBA::Exception& ) {
307 class TSetPropertyEvent: public SALOME_Event
315 TSetPropertyEvent( const QString& _entry,
316 const QString& _property,
317 const QVariant& _value,
318 bool _update = true );
319 virtual void Execute();
322 TSetPropertyEvent::TSetPropertyEvent( const QString& _entry,
323 const QString& _property,
324 const QVariant& _value,
327 myProperty( _property ),
329 myUpdateViewer( _update )
333 void TSetPropertyEvent::Execute()
335 SUIT_Application* app = SUIT_Session::session()->activeApplication();
338 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
339 if ( !study ) return;
341 GEOM_Displayer displayer;
343 SALOME_View* window = displayer.GetActiveView();
344 if ( !window ) return;
346 int mgrId = dynamic_cast<SUIT_ViewModel*>( window )->getViewManager()->getGlobalId();
348 study->setObjectProperty( mgrId, myEntry, myProperty, myValue );
350 Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( myEntry.toUtf8().data(), "GEOM" );
351 if ( window->isVisible( io ) ) displayer.Redisplay( io, myUpdateViewer );
355 \brief Set display mode to the presentation
356 \param theEntry geometry object's entry
357 \param theMode display mode: 0 - wireframe, 1 - shading, 2 - shading+edges, 3-textured
358 \param theUpdateViewer \c true to update active view's contents
360 void GEOM_Swig::setDisplayMode( const char* theEntry, int theMode, bool theUpdateViewer )
362 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::DisplayMode ),
363 theMode, theUpdateViewer ) );
367 \brief Show / hide edges direction vectors for the presentation
368 \param theEntry geometry object's entry
369 \param theOn \c true to show edges direction vectors or \c false otherwise
370 \param theUpdateViewer \c true to update active view's contents
372 void GEOM_Swig::setVectorsMode( const char* theEntry, bool theOn, bool theUpdateViewer )
374 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::EdgesDirection ),
375 theOn, theUpdateViewer ) );
379 \brief Show / hide vertices for the presentation
380 \param theEntry geometry object's entry
381 \param theOn \c true to show vertices or \c false otherwise
382 \param theUpdateViewer \c true to update active view's contents
384 void GEOM_Swig::setVerticesMode( const char* theEntry, bool theOn, bool theUpdateViewer )
386 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Vertices ),
387 theOn, theUpdateViewer ) );
391 \brief Show / hide name of shape for the presentation
392 \param theEntry geometry object's entry
393 \param theOn \c true to show name or \c false otherwise
394 \param theUpdateViewer \c true to update active view's contents
396 void GEOM_Swig::setNameMode( const char* theEntry, bool theOn, bool theUpdateViewer )
398 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::ShowName ),
399 theOn, theUpdateViewer ) );
403 \brief Change color of the presentation
404 \param theEntry geometry object's entry
405 \param theRed red component of the component (0-255)
406 \param theGreen green component of the component (0-255)
407 \param theBlue blue component of the component (0-255)
408 \param theUpdateViewer \c true to update active view's contents
410 void GEOM_Swig::setColor( const char* theEntry, int theRed, int theGreen, int theBlue, bool theUpdateViewer )
412 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Color ),
413 QColor( theRed, theGreen, theBlue ), theUpdateViewer ) );
417 \brief Set number of iso-lines to the presentation
418 \param theEntry geometry object's entry
419 \param theNbU number of iso-lines along U axis (integer value >= 0)
420 \param theNbV number of iso-lines along V axis (integer value >= 0)
421 \param theUpdateViewer \c true to update active view's contents
423 void GEOM_Swig::setIsos( const char* theEntry, int theNbU, int theNbV, bool theUpdateViewer )
425 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::NbIsos ),
426 QString( "%1%2%3" ).arg( theNbU ).arg( GEOM::subSectionSeparator() ).arg( theNbV ),
431 \brief Set transparency of the presentation
432 \param theEntry geometry object's entry
433 \param theTransparency transparency (floating point value between 0 and 1)
434 \param theUpdateViewer \c true to update active view's contents
436 void GEOM_Swig::setTransparency( const char* theEntry, float theTransparency, bool theUpdateViewer )
438 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Transparency ),
439 theTransparency, theUpdateViewer ) );
443 \brief Set deflection coefficient of the presentation
444 \param theEntry geometry object's entry
445 \param theDeflection deflection coefficient (floating point value)
446 \param theUpdateViewer \c true to update active view's contents
448 void GEOM_Swig::setDeflection( const char* theEntry, float theDeflection, bool theUpdateViewer )
450 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Deflection ),
451 theDeflection, theUpdateViewer ) );
455 \brief Set material to the presentation
456 \param theEntry geometry object's entry
457 \param theMaterial material name (string)
458 \param theUpdateViewer \c true to update active view's contents
460 void GEOM_Swig::setMaterial( const char* theEntry, const char* theMaterial, bool theUpdateViewer )
462 Material_Model material;
463 material.fromResources( theMaterial );
464 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Material ),
465 material.toProperties(), theUpdateViewer ) );
469 \brief Set material property to the presentation
470 \param theEntry geometry object's entry
471 \param theMaterial material property string
472 \param theUpdateViewer \c true to update active view's contents
474 void GEOM_Swig::setMaterialProperty( const char* theEntry, const char* theMaterial, bool theUpdateViewer )
476 ProcessVoidEvent( new TSetPropertyEvent( theEntry, GEOM::propertyName( GEOM::Material ),
477 theMaterial, theUpdateViewer ) );
480 class TInitGeomGenEvent: public SALOME_Event
483 typedef bool TResult;
485 TInitGeomGenEvent() : myResult(false)
487 virtual void Execute()
489 myResult = GeometryGUI::InitGeomGen();
494 \brief Initialize GEOM module's engine
495 \return \c true if initialization succeeds or \c false otherwise
497 bool GEOM_Swig::initGeomGen()
499 return ProcessEvent( new TInitGeomGenEvent() );