From: boulant Date: Thu, 20 Oct 2011 16:03:31 +0000 (+0000) Subject: Add the KernelHelpers package that contains helper functions to deal with KERNEL... X-Git-Tag: Before_0020136~13 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=5c3738ef9f5bc7877c57ee32a9efa582e3d9da5c;p=modules%2Fkernel.git Add the KernelHelpers package that contains helper functions to deal with KERNEL services and the study objects. Tests and Documentation are initiated. To be completed. --- diff --git a/configure.ac b/configure.ac index d59d02a60..692c572c6 100644 --- a/configure.ac +++ b/configure.ac @@ -612,4 +612,5 @@ AC_OUTPUT([ \ src/UnitTests/Makefile \ src/Utils/Makefile \ src/Utils/Test/Makefile \ + src/KernelHelpers/Makefile \ ]) diff --git a/doc/salome/Makefile.am b/doc/salome/Makefile.am index fed65146d..52c2f45b7 100644 --- a/doc/salome/Makefile.am +++ b/doc/salome/Makefile.am @@ -40,6 +40,6 @@ dev_docs: (cd tui && $(MAKE) $(AM_MAKEFLAGS) dev_docs) EXTRA_DIST= main.dox install.dox \ - kernel_resources.dox kernel_services.dox \ + kernel_resources.dox kernel_services.dox kernel_services_cpphelpers.dox \ salome_application.dox unittests.dox \ salome_file.dox kernel_salome.dox diff --git a/doc/salome/main.dox b/doc/salome/main.dox index c9462be44..803b2b1df 100644 --- a/doc/salome/main.dox +++ b/doc/salome/main.dox @@ -18,8 +18,12 @@ See \ref SALOME_Application. -# How to use KERNEL services in Python scripts\n The %SALOME KERNEL offers a list of services available in Python. See \subpage KERNEL_Services. +-# How to use KERNEL services from a C++ context\n + The %SALOME KERNEL provides you with helper functions to + manipulate the %SALOME KERNEL services from a C++ programming + context. See \subpage KERNEL_Services_CppHelpers. -\section S3_main Application Integrator +\Section S3_main Application Integrator Applications integrators are in charge of configuration and installation of specific %SALOME applications over a local network. Application Integrators diff --git a/src/Basics/Basics_Utils.hxx b/src/Basics/Basics_Utils.hxx index 56abceee2..507217dbe 100644 --- a/src/Basics/Basics_Utils.hxx +++ b/src/Basics/Basics_Utils.hxx @@ -62,6 +62,13 @@ namespace Kernel_Utils #endif } + +// +// ============================================================= +// Helper macro for time analysis +// ============================================================= +// + #define START_TIMING(name) static long name##tcount=0;static long name##cumul;long name##tt0; timeval name##tv; gettimeofday(&name##tv,0); \ name##tt0=name##tv.tv_usec+name##tv.tv_sec*1000000; \ if(name##tcount==0)std::cerr<<__FILE__<<":"<<__LINE__<<":"<<#name< +#include +#include + +template < class T > +std::string ToString(const T &arg) +{ + std::stringstream out; + out << arg; + return(out.str()); +} + +template < class T > +double ToDouble(const T &arg) { + std::stringstream out; + out << arg; + double value = atof(out.str().c_str()); + return value; +} + +// +// ============================================================= +// Simple Logger macros (no dependency with SALOME) +// ============================================================= +// +#define STDLOG(msg) {std::cerr< +#include "SALOMEDS_DriverDefaultImpl.hxx" + + +//=========================================================================== + SALOMEDS_DriverDefaultImpl::SALOMEDS_DriverDefaultImpl(CORBA::ORB_ptr orb, + PortableServer::POA_ptr poa, + PortableServer::ObjectId * contId, + const char *instanceName, + const char *interfaceName) +// :Engines_Component_i(orb, poa, contId, instanceName, interfaceName) + { + MESSAGE("SALOMEDS_DriverDefaultImpl::SALOMEDS_DriverDefaultImpl : "); + }; + +//=========================================================================== + SALOMEDS_DriverDefaultImpl::~SALOMEDS_DriverDefaultImpl() + { + MESSAGE("SALOMEDS_DriverDefaultImpl::~SALOMEDS_DriverDefaultImpl"); + }; + +//=========================================================================== + CORBA::Boolean SALOMEDS_DriverDefaultImpl::Load(SALOMEDS::SComponent_ptr theComponent, + const SALOMEDS::TMPFile & theStream, + const char* theURL, + bool isMultiFile) + { + MESSAGE("--------------------------------------"); + MESSAGE("--------------------------------------"); + MESSAGE("--------------------------------------"); + MESSAGE("SALOMEDS_DriverDefaultImpl::Load"); + MESSAGE("Should be implemented"); + MESSAGE("--------------------------------------"); + MESSAGE("--------------------------------------"); + MESSAGE("--------------------------------------"); + return false; + }; + +//=========================================================================== + CORBA::Boolean SALOMEDS_DriverDefaultImpl::LoadASCII(SALOMEDS::SComponent_ptr theComponent, + const SALOMEDS::TMPFile & theStream, + const char* theURL, + bool isMultiFile) + { + MESSAGE("--------------------------------------"); + MESSAGE("--------------------------------------"); + MESSAGE("--------------------------------------"); + MESSAGE("SALOMEDS_DriverDefaultImpl::LoadASCII"); + MESSAGE("Should be implemented"); + MESSAGE("--------------------------------------"); + MESSAGE("--------------------------------------"); + MESSAGE("--------------------------------------"); + return false; + }; + +//=========================================================================== + char* SALOMEDS_DriverDefaultImpl::LocalPersistentIDToIOR(SALOMEDS::SObject_ptr theSObject, + const char* aLocalPersistentID, + CORBA::Boolean isMultiFile, + CORBA::Boolean isASCII) + { + MESSAGE("--------------------------------------"); + MESSAGE("--------------------------------------"); + MESSAGE("--------------------------------------"); + MESSAGE("SALOMEDS_DriverDefaultImpl::LocalPersistentIDToIOR"); + MESSAGE("Should be implemented"); + MESSAGE("--------------------------------------"); + MESSAGE("--------------------------------------"); + MESSAGE("--------------------------------------"); + CORBA::String_var aString(""); + return aString._retn(); + }; + +//=========================================================================== + SALOMEDS::TMPFile* SALOMEDS_DriverDefaultImpl::Save(SALOMEDS::SComponent_ptr theComponent, + const char* theURL, + bool isMultiFile) + { + MESSAGE("--------------------------------------"); + MESSAGE("--------------------------------------"); + MESSAGE("--------------------------------------"); + MESSAGE("SALOMEDS_DriverDefaultImpl::Save"); + MESSAGE("Should be implemented"); + MESSAGE("--------------------------------------"); + MESSAGE("--------------------------------------"); + MESSAGE("--------------------------------------"); + SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile(0); + return aStreamFile._retn(); + }; + +//=========================================================================== + SALOMEDS::TMPFile* SALOMEDS_DriverDefaultImpl::SaveASCII(SALOMEDS::SComponent_ptr theComponent, + const char* theURL, + bool isMultiFile) + { + MESSAGE("--------------------------------------"); + MESSAGE("--------------------------------------"); + MESSAGE("--------------------------------------"); + MESSAGE("SALOMEDS_DriverDefaultImpl::SaveASCII"); + MESSAGE("Should be implemented"); + MESSAGE("--------------------------------------"); + MESSAGE("--------------------------------------"); + MESSAGE("--------------------------------------"); + SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile(0); + return aStreamFile._retn(); + }; + +//=========================================================================== + char* SALOMEDS_DriverDefaultImpl::IORToLocalPersistentID(SALOMEDS::SObject_ptr theSObject, + const char* IORString, + CORBA::Boolean isMultiFile, + CORBA::Boolean isASCII) + { + MESSAGE("-----------------------------------------"); + MESSAGE("-----------------------------------------"); + MESSAGE("-----------------------------------------"); + MESSAGE("SALOMEDS_DriverDefaultImpl::IORToLocalPersistentID"); + MESSAGE("Should be implemented"); + MESSAGE("-----------------------------------------"); + MESSAGE("-----------------------------------------"); + MESSAGE("-----------------------------------------"); + CORBA::String_var aString(""); + return aString._retn(); + }; + + +//=========================================================================== + void SALOMEDS_DriverDefaultImpl::Close(SALOMEDS::SComponent_ptr theComponent) + { + MESSAGE("------------------------"); + MESSAGE("SALOMEDS_DriverDefaultImpl::Close"); + MESSAGE("------------------------"); + }; + +//=========================================================================== + char* SALOMEDS_DriverDefaultImpl::ComponentDataType() + { + MESSAGE("-----------------------------------------"); + MESSAGE("-----------------------------------------"); + MESSAGE("-----------------------------------------"); + MESSAGE("SALOMEDS_DriverDefaultImpl::ComponentDataType"); + MESSAGE("Should be implemented"); + MESSAGE("-----------------------------------------"); + MESSAGE("-----------------------------------------"); + MESSAGE("-----------------------------------------"); + return CORBA::string_dup("J aurais du nommer mon type prefere"); + }; + +//=========================================================================== + bool SALOMEDS_DriverDefaultImpl::CanPublishInStudy(CORBA::Object_ptr theIOR) + { + MESSAGE("-----------------------------------------"); + MESSAGE("-----------------------------------------"); + MESSAGE("-----------------------------------------"); + MESSAGE("SALOMEDS_DriverDefaultImpl::CanPublishInStudy"); + MESSAGE("Should be implemented"); + MESSAGE("-----------------------------------------"); + MESSAGE("-----------------------------------------"); + MESSAGE("-----------------------------------------"); + return false; + }; + +//=========================================================================== + SALOMEDS::SObject_ptr SALOMEDS_DriverDefaultImpl::PublishInStudy(SALOMEDS::Study_ptr theStudy, + SALOMEDS::SObject_ptr theSObject, + CORBA::Object_ptr theObject, + const char* theName) + throw (SALOME::SALOME_Exception) + { + MESSAGE("-----------------------------------------"); + MESSAGE("-----------------------------------------"); + MESSAGE("-----------------------------------------"); + MESSAGE("SALOMEDS_DriverDefaultImpl::PublishInStudy"); + MESSAGE("Should be implemented"); + MESSAGE("-----------------------------------------"); + MESSAGE("-----------------------------------------"); + MESSAGE("-----------------------------------------"); + SALOMEDS::SObject_var aResultSO; + return aResultSO._retn(); + }; + +//=========================================================================== + CORBA::Boolean SALOMEDS_DriverDefaultImpl::CanCopy(SALOMEDS::SObject_ptr theObject) + { + MESSAGE("-----------------------------------------"); + MESSAGE("-----------------------------------------"); + MESSAGE("-----------------------------------------"); + MESSAGE("SALOMEDS_DriverDefaultImpl::CanCopy"); + MESSAGE("Should be implemented"); + MESSAGE("-----------------------------------------"); + MESSAGE("-----------------------------------------"); + MESSAGE("-----------------------------------------"); + //return false; + return true; + }; + +//=========================================================================== + SALOMEDS::TMPFile* SALOMEDS_DriverDefaultImpl::CopyFrom(SALOMEDS::SObject_ptr theObject, + CORBA::Long& theObjectID) +{ + MESSAGE("--------------------------------------"); + MESSAGE("--------------------------------------"); + MESSAGE("--------------------------------------"); + MESSAGE("SALOMEDS_DriverDefaultImpl::CopyFrom"); + MESSAGE("Should be implemented"); + MESSAGE("--------------------------------------"); + MESSAGE("--------------------------------------"); + MESSAGE("--------------------------------------"); + SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile(0); + return aStreamFile._retn(); +}; + +//=========================================================================== + SALOMEDS::SObject_ptr SALOMEDS_DriverDefaultImpl::PasteInto( const SALOMEDS::TMPFile & theStream, + CORBA::Long theObjectID, + SALOMEDS::SObject_ptr theSObject) + { + MESSAGE("-----------------------------------------"); + MESSAGE("-----------------------------------------"); + MESSAGE("-----------------------------------------"); + MESSAGE("SALOMEDS_DriverDefaultImpl::PasteInto"); + MESSAGE("Should be implemented"); + MESSAGE("-----------------------------------------"); + MESSAGE("-----------------------------------------"); + MESSAGE("-----------------------------------------"); + SALOMEDS::SObject_var aResultSO; + return aResultSO._retn(); + }; + +//=========================================================================== + CORBA::Boolean SALOMEDS_DriverDefaultImpl::CanPaste ( const char *theComponentName, + CORBA::Long theObjectID) + { + MESSAGE("-----------------------------------------"); + MESSAGE("-----------------------------------------"); + MESSAGE("-----------------------------------------"); + MESSAGE("SALOMEDS_DriverDefaultImpl::CanPaste"); + MESSAGE("Should be implemented"); + MESSAGE("-----------------------------------------"); + MESSAGE("-----------------------------------------"); + MESSAGE("-----------------------------------------"); + return false; + }; + + diff --git a/src/KernelHelpers/SALOMEDS_DriverDefaultImpl.hxx b/src/KernelHelpers/SALOMEDS_DriverDefaultImpl.hxx new file mode 100644 index 000000000..6f44ca6a9 --- /dev/null +++ b/src/KernelHelpers/SALOMEDS_DriverDefaultImpl.hxx @@ -0,0 +1,87 @@ +#ifndef __SALOMEDS_DRIVER_DEFAULT_IMPL_H__ +#define __SALOMEDS_DRIVER_DEFAULT_IMPL_H__ + +#include "SALOME_Component_i.hxx" +#include "SALOME_NamingService.hxx" + +#include CORBA_CLIENT_HEADER(SALOMEDS) + + +//--------------------------------------------------------- + +class SALOMEDS_DriverDefaultImpl: + public virtual POA_SALOMEDS::Driver +//--------------------------------------------------------- + { + + + SALOMEDS_DriverDefaultImpl(); + public: + SALOMEDS_DriverDefaultImpl(CORBA::ORB_ptr orb, + PortableServer::POA_ptr poa, + PortableServer::ObjectId * contId, + const char *instanceName, + const char *interfaceName); + + + virtual ~SALOMEDS_DriverDefaultImpl(); + + //--------------------------------------------- + // inherited methods from SALOMEDS::Driver + //--------------------------------------------- + + //virtual SALOMEDS::TMPFile* Save(SALOMEDS::SComponent_ptr theComponent, + SALOMEDS::TMPFile* Save(SALOMEDS::SComponent_ptr theComponent, + const char* theURL, + bool isMultiFile); + + virtual SALOMEDS::TMPFile* SaveASCII(SALOMEDS::SComponent_ptr theComponent, + const char* theURL, + bool isMultiFile); + + virtual bool Load(SALOMEDS::SComponent_ptr, + const SALOMEDS::TMPFile &, + const char* theURL, + bool isMultiFile); + + virtual bool LoadASCII(SALOMEDS::SComponent_ptr, + const SALOMEDS::TMPFile &, + const char* theURL, + bool isMultiFile); + + virtual void Close(SALOMEDS::SComponent_ptr IORSComponent); + + virtual char* ComponentDataType(); + + virtual char* IORToLocalPersistentID(SALOMEDS::SObject_ptr theSObject, + const char* IORString, + CORBA::Boolean isMultiFile, + CORBA::Boolean isASCII); + + virtual char* LocalPersistentIDToIOR(SALOMEDS::SObject_ptr theSObject, + const char* aLocalPersistentID, + CORBA::Boolean isMultiFile, + CORBA::Boolean isASCII); + + virtual bool CanPublishInStudy(CORBA::Object_ptr theIOR); + + virtual SALOMEDS::SObject_ptr PublishInStudy + (SALOMEDS::Study_ptr theStudy, + SALOMEDS::SObject_ptr theSObject, + CORBA::Object_ptr theObject, + const char* theName) + throw (SALOME::SALOME_Exception); + + virtual CORBA::Boolean CanCopy(SALOMEDS::SObject_ptr theObject); + + virtual SALOMEDS::TMPFile* CopyFrom(SALOMEDS::SObject_ptr theObject, + CORBA::Long& theObjectID); + + virtual CORBA::Boolean CanPaste(const char* theComponentName, + CORBA::Long theObjectID); + + virtual SALOMEDS::SObject_ptr PasteInto(const SALOMEDS::TMPFile& theStream, + CORBA::Long theObjectID, + SALOMEDS::SObject_ptr theObject); + }; +#endif diff --git a/src/KernelHelpers/SALOME_KernelServices.cxx b/src/KernelHelpers/SALOME_KernelServices.cxx new file mode 100644 index 000000000..fbd763be0 --- /dev/null +++ b/src/KernelHelpers/SALOME_KernelServices.cxx @@ -0,0 +1,185 @@ +// Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// Author: Guillaume Boulant (EDF/R&D) + +#include "SALOME_KernelServices.hxx" + +namespace KERNEL { + + /** + * This function returns a static reference to the orb. The orb can + * be used for example to initialize CORBA variables or to serialize + * and unserialize the CORBA objet to/from an IOR string. + */ + CORBA::ORB_ptr getORB() { + static CORBA::ORB_ptr orb; + if(CORBA::is_nil(orb)){ + int argc=0; + orb = CORBA::ORB_init(argc,0); + } + return orb; + } + + /** + * This function returns a static reference to the SALOME naming service. + */ + SALOME_NamingService * getNamingService() { + static SALOME_NamingService * namingService; + if ( namingService == NULL ) { + namingService = new SALOME_NamingService(getORB()); + } + return namingService; + } + + /** + * This function returns a static reference to the SALOME life cycle CORBA. + */ + SALOME_LifeCycleCORBA * getLifeCycleCORBA() { + static SALOME_LifeCycleCORBA * lifeCycleCORBA; + if ( lifeCycleCORBA == NULL ) { + SALOME_NamingService *aNamingService = getNamingService(); + lifeCycleCORBA = new SALOME_LifeCycleCORBA(aNamingService); + } + return lifeCycleCORBA; + } + + + /** + * This returns a static reference to the SALOME study manager. The + * study manager can be used to retrieve a study or to get + * informations about a study. + */ + SALOMEDS::StudyManager_ptr getStudyManager() { + static SALOMEDS::StudyManager_ptr aStudyManager; + if(CORBA::is_nil(aStudyManager)){ + SALOME_NamingService *aNamingService = getNamingService(); + CORBA::Object_ptr anObject = aNamingService->Resolve("/myStudyManager"); + aStudyManager = SALOMEDS::StudyManager::_narrow(anObject); + } + return aStudyManager; + } + + /** + * This returns a static reference to the SALOME session. The + * SALOME session can be used to retrieve some objects of the + * current session, as the SALOME study. + */ + SALOME::Session_ptr getSalomeSession() { + static SALOME::Session_ptr salomeSession; + if(CORBA::is_nil(salomeSession)){ + SALOME_NamingService *aNamingService = getNamingService(); + CORBA::Object_ptr obj = aNamingService->Resolve("/Kernel/Session"); + salomeSession = SALOME::Session::_narrow(obj); + } + return salomeSession; + } + + /** + * This returns a static reference to the SALOME launcher. The + * SALOME launcher can be used to schedule jobs, local or remote, + * using a batch system or not (see SALOME documentation). + */ + Engines::SalomeLauncher_ptr getSalomeLauncher() { + //LOG("KERNEL_services::getSalomeLauncher()"); + static Engines::SalomeLauncher_ptr salomeLauncher; + if(CORBA::is_nil(salomeLauncher)){ + //LOG("KERNEL_services::getSalomeLauncher(): creating the static instance"); + SALOME_NamingService *aNamingService = getNamingService(); + CORBA::Object_ptr obj = aNamingService->Resolve("/SalomeLauncher"); + salomeLauncher = Engines::SalomeLauncher::_narrow(obj); + } + return salomeLauncher; + } + + Engines::ResourcesManager_ptr getResourcesManager() { + static Engines::ResourcesManager_ptr resourcesManager; + if(CORBA::is_nil(resourcesManager)){ + SALOME_NamingService *aNamingService = getNamingService(); + CORBA::Object_ptr obj = aNamingService->Resolve("/ResourcesManager"); + resourcesManager = Engines::ResourcesManager::_narrow(obj); + } + return resourcesManager; + } + + /** + * This returns the study with the specified id if it's defined in + * the SALOME study manager. Returns null otherwise. + * Please not that it is just a shortcut, and you may prefer use + * directly the study manager: + * KERNEL::getStudyManager()->GetStudyByID(aStudyId) + */ + SALOMEDS::Study_ptr getStudyById(int aStudyId) { + if ( aStudyId < 0 ) { + INFOS("ERR: trying to get a study with ID<0"); + return SALOMEDS::Study::_nil(); + } + return getStudyManager()->GetStudyByID(aStudyId); + } + + int getStudyId(SALOMEDS::Study_ptr study) { + if( CORBA::is_nil(study) ) return -1; + return study->StudyId(); + } + + + /** + * This function retrieve the CORBA object reference from the study + * object wrapping it. + */ + CORBA::Object_ptr SObjectToObject(SALOMEDS::SObject_ptr theSObject) { + + SALOMEDS::GenericAttribute_var anAttr; + CORBA::Object_var anObject; + if(CORBA::is_nil(theSObject)) + return anObject; + try{ + if(theSObject->FindAttribute(anAttr, "AttributeIOR")){ + SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr); + CORBA::String_var aValue = anIOR->Value(); + CORBA::ORB_ptr anORB = getORB(); + if(strcmp(aValue,"") != 0) + anObject = anORB->string_to_object(aValue); + } + }catch(...){ + INFOS("SObjectToObject - Unknown exception was occured!!!"); + } + return anObject._retn(); + } + + /*! + * This function provides a CORBA pointer to a servant from its IOR + * given as a string of characters. + */ + CORBA::Object_ptr IORToObject(char * IOR) { + return getORB()->string_to_object(IOR); + } + + // + // __GBO__ See the file ./src/SMESHGUI/SMESHGUI_Utils.h of SMESH_SRC + // for other helper functions + // + + SALOME::SALOME_Exception createSalomeException(const char * text) { + SALOME::ExceptionStruct es; + es.type = SALOME::INTERNAL_ERROR; + es.text = CORBA::string_dup(text); + return SALOME::SALOME_Exception(es); + } + +} diff --git a/src/KernelHelpers/SALOME_KernelServices.hxx b/src/KernelHelpers/SALOME_KernelServices.hxx new file mode 100644 index 000000000..875a1c46d --- /dev/null +++ b/src/KernelHelpers/SALOME_KernelServices.hxx @@ -0,0 +1,116 @@ +// Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// Author: Guillaume Boulant (EDF/R&D) + +#ifndef __KERNEL_SERVICES_H__ +#define __KERNEL_SERVICES_H__ + +#include "SALOMEconfig.h" +#include CORBA_SERVER_HEADER(SALOMEDS) +#include CORBA_SERVER_HEADER(SALOMEDS_Attributes) +#include CORBA_SERVER_HEADER(SALOME_ContainerManager) +#include CORBA_CLIENT_HEADER(SALOME_Session) +#include CORBA_SERVER_HEADER(SALOME_Exception) + +#include "SALOME_NamingService.hxx" +#include "SALOME_LifeCycleCORBA.hxx" + +namespace KERNEL { + + // --------------------------------------------- + // SALOME KERNEL main services + CORBA::ORB_ptr getORB(); + SALOME_NamingService * getNamingService(); + SALOME_LifeCycleCORBA * getLifeCycleCORBA(); + SALOME::Session_ptr getSalomeSession(); + SALOMEDS::StudyManager_ptr getStudyManager(); + Engines::SalomeLauncher_ptr getSalomeLauncher(); + Engines::ResourcesManager_ptr getResourcesManager(); + + // --------------------------------------------- + // SALOME KERNEL services to deal with a SALOME study, SObject and + // SComponent. + // + SALOMEDS::Study_ptr getStudyById(int aStudyId); + int getStudyId(SALOMEDS::Study_ptr study); + CORBA::Object_ptr IORToObject(char * IOR); + CORBA::Object_ptr SObjectToObject(SALOMEDS::SObject_ptr); + + + + /*! + * This template function provides you with the servant (CORBA + * object narrowed to its interface) corresponding to the specified + * CORBA object (naked CORBA pointer). + */ + template typename TInterface::_var_type + ObjectToInterface(CORBA::Object_ptr object) { + if(!CORBA::is_nil(object)) + return TInterface::_narrow(object); + return TInterface::_nil(); + } + + template typename TInterface::_var_type + SObjectToInterface(SALOMEDS::SObject_ptr sobject) { + CORBA::Object_ptr object = SObjectToObject(sobject); + return ObjectToInterface(object); + } + + // _MEM_: note that template functions have to be declared AND + // implemented in the header because they are not compiled in this + // library but in every client library that includes this header + // file. The effective compilation depends on the particular class + // used for TInterface. + + // --------------------------------------------- + // To create a standard SALOME exception embedding a simple text + SALOME::SALOME_Exception createSalomeException(const char * text); +} + + +// +// ============================================================= +// SALOME Logger macros +// ============================================================= +// +// We can use the macros defined by SALOMELocalTrace/utilities.h +#include "utilities.h" +#define SALOMELOG(msg) {MESS_BEGIN("[XSALOME]") << msg << MESS_END} +#define LOG SALOMELOG + +// This can help to LOG (or use in stream) the CORBA exceptions +// Ex: LOG("An exception occurs: "< +#include +static std::ostream & +operator<<(std::ostream & os, const CORBA::Exception & e) +{ + CORBA::Any tmp; + tmp <<=e ; + CORBA::TypeCode_var tc = tmp.type(); + const char * p = tc->name (); + if (*p != '\0') + os << p; + else + os << tc->id(); + return os; +} + + +#endif // KERNEL_SERVICES diff --git a/src/KernelHelpers/SALOME_StudyEditor.cxx b/src/KernelHelpers/SALOME_StudyEditor.cxx new file mode 100644 index 000000000..958c5c4c2 --- /dev/null +++ b/src/KernelHelpers/SALOME_StudyEditor.cxx @@ -0,0 +1,164 @@ +// Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// Author: Guillaume Boulant (EDF/R&D) + +#include "SALOME_StudyEditor.hxx" +#include "SALOME_KernelServices.hxx" + +/** Canonic constructor. The object can't be used without a setStudy() */ +SALOME_StudyEditor::SALOME_StudyEditor() { +} + +void SALOME_StudyEditor::setStudy(SALOMEDS::Study_ptr study) { + _study = study; + _sbuilder = _study->NewBuilder(); +} + +void SALOME_StudyEditor::setStudyById(int studyId) { + this->setStudy(KERNEL::getStudyManager()->GetStudyByID(studyId)); +} + +int SALOME_StudyEditor::getStudyId() { + if ( _study->_is_nil() ) return UNDEFINED; + return _study->StudyId(); +} + +SALOME_StudyEditor::SALOME_StudyEditor(int studyId) { + this->setStudyById(studyId); +} +SALOME_StudyEditor::SALOME_StudyEditor(SALOMEDS::Study_ptr study) { + this->setStudy(study); +} + +SALOMEDS::SObject_ptr SALOME_StudyEditor::newObject(SALOMEDS::SObject_ptr parent) { + return _sbuilder->NewObject(parent); +} + +SALOMEDS::SObject_ptr SALOME_StudyEditor::findObject(const char * entry) { + SALOMEDS::SObject_var sobject = _study->FindObjectID(entry); + return sobject._retn(); +} + +SALOMEDS::SComponent_ptr SALOME_StudyEditor::newRoot(const char * moduleName) { + SALOMEDS::SComponent_var sroot = findRoot(moduleName); + if ( sroot->_is_nil() ) { + sroot = _sbuilder->NewComponent(moduleName); + _sbuilder->SetName(sroot,moduleName); + } + return sroot._retn(); +} + +bool SALOME_StudyEditor::bindEngine(SALOMEDS::SComponent_var studyRoot, + Engines::EngineComponent_var engine) +{ + // WARN: The engine should be associated to the SComponent IF + // AND ONLY IF it is a SALOMEDS::Driver. Otherwise, there is no + // need to do that, and it could even lead to exception + // raising (eh, you work on SALOME isn't it?) + SALOMEDS::Driver_var driver = SALOMEDS::Driver::_narrow(engine); + if ( driver->_is_nil() || studyRoot->_is_nil() ) return false; + _sbuilder->DefineComponentInstance(studyRoot, engine); + return true; +} + +SALOMEDS::SComponent_ptr SALOME_StudyEditor::findRoot(const char * moduleName) { + return _study->FindComponent(moduleName); +} + +void SALOME_StudyEditor::setName(SALOMEDS::SObject_var sobject, const char * value) { + _sbuilder->SetName(sobject,value); +} +const char * SALOME_StudyEditor::getName(SALOMEDS::SObject_var sobject) { + if (sobject->_is_nil()) return NULL; + return sobject->GetName(); +} + +/** + * This function specifies which resource file is to be used to + * associate an icon to the specified object. Note that even if the + * icon as no sens outside the GUI context, it can be defined here + * because only the resource name is provided. The effective rendering + * is done in the object browser of course, and use this string + * attribute. WARN: note that the resource name is supposed to be the + * base name of a file managed by the SALOME resource manager (i.e. a + * file located in the directory specified in the SalomeApp.xml). + */ +void SALOME_StudyEditor::setIcon(SALOMEDS::SObject_var sobject, const char * resourcename) { + SALOMEDS::GenericAttribute_var anAttr; + SALOMEDS::AttributePixMap_var aPixmap; + anAttr = _sbuilder->FindOrCreateAttribute(sobject, "AttributePixMap"); + aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr); + aPixmap->SetPixMap(resourcename); +} + +// +// MEM: AttributeParameter +// Note that the AttributeParameter is a multitype dictionnary whose +// keys are the names specified when setting the value +// + +/** + * Add a parameter attribute of type integer or simply set its value + * if it already exists. + */ +void SALOME_StudyEditor::setParameterInt(SALOMEDS::SObject_var sobject, const char * name, int value) { + SALOMEDS::GenericAttribute_var anAttr; + SALOMEDS::AttributeParameter_var aParam; + anAttr = _sbuilder->FindOrCreateAttribute(sobject, "AttributeParameter"); + aParam = SALOMEDS::AttributeParameter::_narrow(anAttr); + aParam->SetInt(name,value); +} + +int SALOME_StudyEditor::getParameterInt(SALOMEDS::SObject_var sobject, const char * name) { + if (sobject->_is_nil()) return UNDEFINED; + + SALOMEDS::GenericAttribute_var anAttr; + SALOMEDS::AttributeParameter_var aParam; + if ( sobject->FindAttribute(anAttr,"AttributeParameter") ) { + aParam = SALOMEDS::AttributeParameter::_narrow(anAttr); + return aParam->GetInt(name); + } + return UNDEFINED; +} + +/** + * Add a parameter attribute of type boolean or simply set its value + * if it already exists. + */ +void SALOME_StudyEditor::setParameterBool(SALOMEDS::SObject_var sobject, const char * name, bool value) { + SALOMEDS::GenericAttribute_var anAttr; + SALOMEDS::AttributeParameter_var aParam; + anAttr = _sbuilder->FindOrCreateAttribute(sobject, "AttributeParameter"); + aParam = SALOMEDS::AttributeParameter::_narrow(anAttr); + aParam->SetBool(name,value); +} + +bool SALOME_StudyEditor::getParameterBool(SALOMEDS::SObject_var sobject, const char * name) { + if (sobject->_is_nil()) return false; + // _GBO_ Q: Maybe it's better in this case to raise an exception? + + SALOMEDS::GenericAttribute_var anAttr; + SALOMEDS::AttributeParameter_var aParam; + if ( sobject->FindAttribute(anAttr,"AttributeParameter") ) { + aParam = SALOMEDS::AttributeParameter::_narrow(anAttr); + return aParam->GetBool(name); + } + return false; + +} diff --git a/src/KernelHelpers/SALOME_StudyEditor.hxx b/src/KernelHelpers/SALOME_StudyEditor.hxx new file mode 100644 index 000000000..b3b56d52d --- /dev/null +++ b/src/KernelHelpers/SALOME_StudyEditor.hxx @@ -0,0 +1,68 @@ +// Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// Author: Guillaume Boulant (EDF/R&D) + +#ifndef __STUDY_EDITOR_HXX__ +#define __STUDY_EDITOR_HXX__ + +#include +#include CORBA_CLIENT_HEADER(SALOMEDS) +#include CORBA_SERVER_HEADER(SALOME_Component) + +#include + +class SALOME_StudyEditor { + +public: + SALOME_StudyEditor(int studyId); + SALOME_StudyEditor(SALOMEDS::Study_ptr study); + + typedef std::vector SObjectList; + + SALOMEDS::SComponent_ptr newRoot(const char * moduleName); + SALOMEDS::SComponent_ptr findRoot(const char * moduleName); + bool bindEngine(SALOMEDS::SComponent_var studyRoot,Engines::EngineComponent_var engine); + + SALOMEDS::SObject_ptr newObject(SALOMEDS::SObject_ptr parent); + SALOMEDS::SObject_ptr findObject(const char * entry); + void setName(SALOMEDS::SObject_var sobject, const char * value); + const char * getName(SALOMEDS::SObject_var sobject); + + void setIcon(SALOMEDS::SObject_var sobject, const char * resourcename); + + void setParameterInt(SALOMEDS::SObject_var sobject, const char * name, int value); + int getParameterInt(SALOMEDS::SObject_var sobject, const char * name); + void setParameterBool(SALOMEDS::SObject_var sobject, const char * name, bool value); + bool getParameterBool(SALOMEDS::SObject_var sobject, const char * name); + + static const int UNDEFINED = -1; + +protected: + SALOME_StudyEditor(); + void setStudy(SALOMEDS::Study_ptr study); + void setStudyById(int studyId); + int getStudyId(); + + SALOMEDS::Study_var _study; + +private: + SALOMEDS::StudyBuilder_var _sbuilder; +}; + +#endif // __STUDY_EDITOR_HXX__ diff --git a/src/KernelHelpers/TestKernelHelpers.cxx b/src/KernelHelpers/TestKernelHelpers.cxx new file mode 100644 index 000000000..53173419e --- /dev/null +++ b/src/KernelHelpers/TestKernelHelpers.cxx @@ -0,0 +1,48 @@ +// Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// Author: Guillaume Boulant (EDF/R&D) + +#include "SALOME_KernelServices.hxx" +#include "Basics_Utils.hxx" + +#include +#include CORBA_CLIENT_HEADER(SALOME_TestComponent) + +void TEST_corba() { + CORBA::ORB_var orb = KERNEL::getORB(); + SALOME_NamingService * ns = KERNEL::getNamingService(); + SALOME_LifeCycleCORBA * lcc = KERNEL::getLifeCycleCORBA(); +} + +void TEST_getLifeCycleCORBA() { + Engines::EngineComponent_var component = + KERNEL::getLifeCycleCORBA()->FindOrLoad_Component( "FactoryServer","SalomeTestComponent" ); + + Engines::TestComponent_var engine = Engines::TestComponent::_narrow(component); + STDLOG(engine->Coucou(123.)); +} + +// TODO: +// - complete the coverture of the KernelService interface +// - provide use case for the StudyEditor + +int main (int argc, char * argv[]) { + TEST_getLifeCycleCORBA(); + return 0; +} diff --git a/src/Makefile.am b/src/Makefile.am index dd9bece68..931471000 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -52,6 +52,7 @@ SUBDIR_CORBA = \ TOOLSDS \ SALOMEDSImpl \ SALOMEDS \ + KernelHelpers \ ModuleGenerator \ Communication \ Communication_SWIG \