Salome HOME
7f2cd697eed7e68b4f58cca5f261a9753b22b3da
[modules/eficas.git] / src / EFICASGUI / salomedsgui.py
1 #  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 #  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
3
4 #  This library is free software; you can redistribute it and/or 
5 #  modify it under the terms of the GNU Lesser General Public 
6 #  License as published by the Free Software Foundation; either 
7 #  version 2.1 of the License. 
8
9 #  This library is distributed in the hope that it will be useful, 
10 #  but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 #  Lesser General Public License for more details. 
13
14 #  You should have received a copy of the GNU Lesser General Public 
15 #  License along with this library; if not, write to the Free Software 
16 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
17
18 #  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
19 #
20 #
21 #
22 #  File   : salomedsgui.py
23 #  Author : Paul RASCLE, EDF
24 #  Module : KERNEL
25 #
26 #--------------------------------------------------------------------------
27
28 import salome
29 import SALOMEDS
30 import string,os
31
32 class guiDS:
33     """
34     Study (SALOMEDS) interface from python gui (ex: IAPP embedded PyQt GUI)
35     """
36     _myStudyManager = None
37     _myStudy = None
38     _myBuilder = None
39     _father = None
40     _component = None
41
42     def __init__(self):
43         self._myStudyManager = salome.myStudyManager
44         self._myStudy = salome.myStudy
45         self._myBuilder = self._myStudy.NewBuilder()
46
47     def enregistre(self,myModule):
48         if self._father is None:
49             father = self._myStudy.FindComponent(myModule)
50             if father is None:
51                 father = self._myBuilder.NewComponent(myModule)
52                 A1 = self._myBuilder.FindOrCreateAttribute(father,"AttributeName")
53                 FName = A1._narrow(SALOMEDS.AttributeName)
54                 FName.SetValue(myModule)
55             self._father   = father
56             self._component = myModule
57         return self._father.GetID()
58
59     def createItemInStudy(self,fatherId,objectName):
60         objId = None
61         if self._component is not None:
62             listSO = self._myStudy.FindObjectByName(objectName,self._component)
63             if len(listSO) == 0:
64                 father = self._myStudy.FindObjectID(fatherId)
65                 newObj = self._myBuilder.NewObject(father)
66                 A1= self._myBuilder.FindOrCreateAttribute(newObj,"AttributeName")
67                 FName = A1._narrow(SALOMEDS.AttributeName)
68                 FName.SetValue(objectName)
69                 objId = newObj.GetID()
70             else:
71                 objId = listSO[0].GetID()
72         return objId
73
74     def getReference(self,objectId):
75         mySO = self._myStudy.FindObjectID(objectId)
76         boo,RefSO = mySO.ReferencedObject()
77         if boo:
78             objectId=RefSO.GetID()
79         return objectId        
80
81     def addReference(self,fatherId,refId):
82         father = self._myStudy.FindObjectID(fatherId)
83         ref = self._myStudy.FindObjectID(refId)
84         newObj = self._myBuilder.NewObject(father)
85         A1 =  self._myBuilder.FindOrCreateAttribute(ref,"AttributeName")
86         FName = A1._narrow(SALOMEDS.AttributeName)
87         Name_ref = FName.Value()
88         path_father , none = string.split(self._myStudy.GetObjectPath(ref),Name_ref)
89         path_father , none = os.path.split(path_father)
90         #print "salomedsgui::addReference : path_father_ref = ",path_father
91         #print "salomedsgui::addReference : Path_father = ",self._myStudy.GetObjectPath(father)
92         if self._myStudy.GetObjectPath(father) != path_father :
93             self._myBuilder.Addreference(newObj,ref)
94
95     def setExternalFileAttribute(self,objectId,filetype,filename):
96         mySO = self._myStudy.FindObjectID(objectId)
97         A1 = self._myBuilder.FindOrCreateAttribute(mySO,"AttributeExternalFileDef")
98         AFileName = A1._narrow(SALOMEDS.AttributeExternalFileDef)
99         AFileName.SetValue(filename)
100         print filename
101         A2 = self._myBuilder.FindOrCreateAttribute(mySO,"AttributeFileType")
102         AFileType = A2._narrow(SALOMEDS.AttributeFileType)
103         print filetype
104         AFileType.SetValue(filetype)
105         print filetype
106                           
107     def getExternalFileAttribute(self,filetype, objectId):
108         print filetype
109         print objectId
110         mySO = self._myStudy.FindObjectID(objectId)
111         boo,RefSO = mySO.ReferencedObject()
112         if boo:
113             print RefSO
114             mySO = RefSO
115         print mySO
116         val=""
117         boo,attr =  self._myBuilder.FindAttribute(mySO,"AttributeFileType")
118         print "AttributeFileType ",boo
119         if boo:
120             boo=0
121             val=attr.Value()
122             print val
123         if val==filetype:
124             boo,attr =  self._myBuilder.FindAttribute(mySO,"AttributeExternalFileDef")
125         val=""
126         if boo: 
127              val=attr.Value()
128         attribute=val
129         return (boo,attribute)
130
131     def getNameAttribute(self, objectId):
132         mySO = self._myStudy.FindObjectID(objectId)
133         boo,RefSO = mySO.ReferencedObject()
134         if boo:
135             mySO = RefSO
136         boo,attr =  self._myBuilder.FindAttribute(mySO,"AttributeName")
137         val=""
138         if boo:
139             val=attr.Value()
140             print val
141         return val
142             
143     def getTypeAttribute(self, objectId):
144         mySO = self._myStudy.FindObjectID(objectId)
145         boo,RefSO = mySO.ReferencedObject()
146         if boo:
147             mySO = RefSO
148         boo,attr =  self._myBuilder.FindAttribute(mySO,"AttributeFileType")
149         val=""
150         if boo:
151             val=attr.Value()
152         return val
153
154     def getChildren(self, objectId):
155         children=[]
156         mySO = self._myStudy.FindObjectID(objectId)
157         boo,RefSO = mySO.ReferencedObject()
158         if boo:
159             mySO = RefSO
160         it = self._myStudy.NewChildIterator(mySO)
161         while it.More():
162             CSO = it.Value()
163             children.append(CSO.GetID())
164             it.Next()
165         print children
166         return children