Salome HOME
a894bc967e8a39169c035a20f294361d822062c4
[samples/pyhello.git] / src / PYHELLO / PYHELLO.py
1 # Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 #
3 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 #
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.
10 #
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.
15 #
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
19 #
20 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #
22
23 # ---
24 # File   : PYHELLOGUI.py
25 # Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
26 # ---
27 #
28 import PYHELLO_ORB__POA
29 import SALOME_ComponentPy
30 import SALOME_DriverPy
31 import SALOMEDS
32 from PYHELLO_utils import findOrCreateComponent, objectID, moduleName, getStudy
33
34 class PYHELLO(PYHELLO_ORB__POA.PYHELLO_Gen,
35               SALOME_ComponentPy.SALOME_ComponentPy_i,
36               SALOME_DriverPy.SALOME_DriverPy_i):
37     """
38     Construct an instance of PYHELLO module engine.
39     The class PYHELLO implements CORBA interface PYHELLO_Gen (see PYHELLO_Gen.idl).
40     It is inherited from the classes SALOME_ComponentPy_i (implementation of
41     Engines::EngineComponent CORBA interface - SALOME component) and SALOME_DriverPy_i
42     (implementation of SALOMEDS::Driver CORBA interface - SALOME module's engine).
43     """
44     def __init__ ( self, orb, poa, contID, containerName, instanceName,
45                    interfaceName ):
46         SALOME_ComponentPy.SALOME_ComponentPy_i.__init__(self, orb, poa,
47                     contID, containerName, instanceName, interfaceName, False)
48         SALOME_DriverPy.SALOME_DriverPy_i.__init__(self, interfaceName)
49         #
50         self._naming_service = SALOME_ComponentPy.SALOME_NamingServicePy_i( self._orb )
51         #
52         pass
53
54     """
55     Get version information.
56     """
57     def getVersion( self ):
58         import salome_version
59         return salome_version.getVersion("PYHELLO", True)
60
61     """
62     Generate hello banner.
63     """
64     def makeBanner( self, name ):
65         banner = "Hello %s!" % name
66         return banner
67
68     """
69     Intentionnally raises an exception for test purposes.
70     """
71     def raiseAnException( self ):
72         import SALOME
73         exData = SALOME.ExceptionStruct( SALOME.BAD_PARAM, "Test exception in raiseAnException()",'',0)
74         raise SALOME.SALOME_Exception( exData )
75
76     """
77     Create object.
78     """
79     def createObject( self, name ):
80         study = getStudy()
81         builder = study.NewBuilder()
82         father  = findOrCreateComponent()
83         obj  = builder.NewObject( father )
84         attr    = builder.FindOrCreateAttribute( obj, "AttributeName" )
85         attr.SetValue( name )
86         attr    = builder.FindOrCreateAttribute( obj, "AttributeLocalID" )
87         attr.SetValue( objectID() )
88         pass
89
90     """
91     Dump module data to the Python script.
92     """
93     def DumpPython( self, isPublished, isMultiFile ):
94         abuffer = []
95         names = []
96         study = getStudy()
97         father = study.FindComponent( moduleName() )
98         if father:
99             iterator = study.NewChildIterator(father)
100             while iterator.More():
101                 name = iterator.Value().GetName()
102                 if name: names.append( name )
103                 iterator.Next()
104                 pass
105             pass
106         if names:
107             abuffer += [ "import salome" ]
108             abuffer += [ "import PYHELLO_ORB" ]
109             abuffer += [ "" ]
110             abuffer += [ "pyhello = salome.lcc.FindOrLoadComponent( 'FactoryServerPy', '%s' )" % moduleName() ]
111             abuffer += [ "" ]
112             abuffer += [ "pyhello.createObject( '%s')" % name for name in names ]
113             abuffer += [ "" ]
114             pass
115         if isMultiFile:
116             abuffer = [ "  " + s for s in abuffer ]
117             abuffer[0:0] = [ "def RebuildData():" ]
118             abuffer += [ "    pass" ]
119         abuffer += [ "\0" ]
120         res = "\n".join( abuffer )
121         return (res.encode(), 1)