Salome HOME
Merge from BR_siman_phase1 14/02/2013
[samples/pyhello.git] / src / PYHELLO / PYHELLO.py
1 # Copyright (C) 2007-2012  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.
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 SALOME_DataContainerPy import *
33
34 from PYHELLO_utils import *
35
36 class PYHELLO(PYHELLO_ORB__POA.PYHELLO_Gen,
37               SALOME_ComponentPy.SALOME_ComponentPy_i,
38               SALOME_DriverPy.SALOME_DriverPy_i):
39     """
40     Construct an instance of PYHELLO module engine.
41     The class PYHELLO implements CORBA interface PYHELLO_Gen (see PYHELLO_Gen.idl).
42     It is inherited from the classes SALOME_ComponentPy_i (implementation of
43     Engines::EngineComponent CORBA interface - SALOME component) and SALOME_DriverPy_i
44     (implementation of SALOMEDS::Driver CORBA interface - SALOME module's engine).
45     """
46     def __init__ ( self, orb, poa, contID, containerName, instanceName, 
47                    interfaceName ):
48         SALOME_ComponentPy.SALOME_ComponentPy_i.__init__(self, orb, poa,
49                     contID, containerName, instanceName, interfaceName, 0)
50         SALOME_DriverPy.SALOME_DriverPy_i.__init__(self, interfaceName)
51         #
52         self._naming_service = SALOME_ComponentPy.SALOME_NamingServicePy_i( self._orb )
53         #
54         pass
55
56     """
57     Get version information.
58     """
59     def getVersion( self ):
60         import salome_version
61         return salome_version.getVersion("PYHELLO", True)
62
63     """
64     Generate hello banner.
65     """
66     def makeBanner( self, name ):
67         banner = "Hello %s!" % name
68         return banner
69
70     """
71     Create object.
72     """
73     def createObject( self, study, name ):
74         self._createdNew = True # used for getModifiedData method
75         builder = study.NewBuilder()
76         father  = findOrCreateComponent( study )
77         object  = builder.NewObject( father )
78         attr    = builder.FindOrCreateAttribute( object, "AttributeName" )
79         attr.SetValue( name )
80         attr    = builder.FindOrCreateAttribute( object, "AttributeLocalID" )
81         attr.SetValue( objectID() )
82         pass
83
84     """
85     Dump module data to the Python script.
86     """
87     def DumpPython( self, study, isPublished ):
88         abuffer = []
89         abuffer.append( "def RebuildData( theStudy ):" )
90         names = []
91         father = study.FindComponent( moduleName() )
92         if father:
93             iter = study.NewChildIterator( father )
94             while iter.More():
95                 name = iter.Value().GetName()
96                 if name: names.append( name )
97                 iter.Next()
98                 pass
99             pass
100         if names:
101             abuffer += [ "  from batchmode_salome import lcc" ]
102             abuffer += [ "  import PYHELLO_ORB" ]
103             abuffer += [ "  " ]
104             abuffer += [ "  pyhello = lcc.FindOrLoadComponent( 'FactoryServerPy', '%s' )" % moduleName() ]
105             abuffer += [ "  " ]
106             abuffer += [ "  pyhello.createObject( theStudy, '%s' )" % name for name in names ]
107             pass
108         abuffer += [ "  " ]
109         abuffer.append( "  pass" )
110         abuffer.append( "\0" )
111         return ("\n".join( abuffer ), 1)
112
113     """
114     Import file to restore module data
115     """
116     def importData(self, studyId, dataContainer, options):
117       # get study by Id
118       obj = self._naming_service.Resolve("myStudyManager")
119       myStudyManager = obj._narrow(SALOMEDS.StudyManager)
120       study = myStudyManager.GetStudyByID(studyId)
121       # create all objects from the imported stream
122       stream = dataContainer.get()
123       for objname in stream.split("\n"):
124         if len(objname) != 0:
125           self.createObject(study, objname)
126       self._createdNew = False # to store the modification of the study information later
127       return ["objects"] # identifier what is in this file
128
129     def getModifiedData(self, studyId):
130       if self._createdNew:
131         # get study by Id
132         obj = self._naming_service.Resolve("myStudyManager")
133         myStudyManager = obj._narrow(SALOMEDS.StudyManager)
134         study = myStudyManager.GetStudyByID(studyId)
135         # iterate all objects to get their names and store this information in stream
136         stream=""
137         father = study.FindComponent( moduleName() )
138         if father:
139             iter = study.NewChildIterator( father )
140             while iter.More():
141                 name = iter.Value().GetName()
142                 stream += name + "\n"
143                 iter.Next()
144         # store stream to the temporary file to send it in DataContainer
145         dataContainer = SALOME_DataContainerPy_i(stream, "", "objects", False, True)
146         aVar = dataContainer._this()
147         return [aVar]
148       return []