Salome HOME
NRI : Merge from V1_2.
[modules/visu.git] / src / VISUGUI / VisuGUI.py
1 #  VISU VISUGUI : GUI of VISU component
2 #
3 #  Copyright (C) 2003  CEA/DEN, EDF R&D
4 #
5 #
6 #
7 #  File   : VisuGUI.py
8 #  Module : VISU
9
10 class Row:
11     def __init__(self):
12         self.title = ""
13         self.unit = ""
14         self.values = []
15         return
16     pass
17
18 class Table2D:
19     def __init__(self):
20         self.title = ""
21         self.columnTitles = []
22         self.columnUnits = []
23         self.rows = []
24         return
25     def getColumns(self):
26         nb = len(self.rows[0].values)
27         cols = []
28         for i in range(nb):
29             col = Row()
30             for r in self.rows:
31                 col.values.append(r.values[i])
32                 pass
33             print self.columnTitles
34             if len(self.columnTitles)>i:
35                 col.title = self.columnTitles[i]
36                 print col.title
37                 pass
38             if len(self.columnUnits)>i:
39                 col.unit = self.columnUnits[i]
40                 pass
41             cols.append(col)
42             pass
43         return cols
44     pass
45
46 def AddTablesInStudyFromFile(ffile):
47     from exceptions import IOError
48     try:
49         file = open(ffile, 'r')
50     except :
51         msg="can't open file <%s>...\n"%ffile
52         raise IOError(msg)
53     # --
54     is_table_first_line = 1
55     tables = []
56     # --
57     num = 0
58     for line in file.readlines():
59         num += 1
60         line = line.strip()
61         if len(line)==0:
62             is_table_first_line = 1
63             continue
64         if is_table_first_line:
65             is_table_first_line = 0
66             t = Table2D()
67             tables.append(t)
68             pass
69         if line.find('#TITLE:')==0:
70             t.title = line.split('#TITLE:')[1].strip()
71             continue
72         if line.find('#COLUMN_TITLES:')==0:
73             titles = line.split('#COLUMN_TITLES:')[1].split("|")
74             t.columnTitles = [ title.strip() for title in titles ]
75             continue
76         if line.find('#COLUMN_UNITS:')==0:
77             units = line.split('#COLUMN_UNITS:')[1].split()
78             t.columnUnits = [ unit.strip() for unit in units ]
79             continue
80         if line[0]== '#':
81             continue
82         row = Row()
83         if line.find('#TITLE:')>0:
84             row.title = line.split('#TITLE:')[1].strip()
85             line = line.split('#TITLE:')[0]
86             pass
87         values = line.split()
88         floatingValues = []
89         for val in values :
90             try:
91                 val = float(val)
92             except ValueError:
93                 message  = "Syntax error at line " + str(num) + " ... \n"
94                 message += "Found field : " + str(val) + '\n'
95                 message += "instead of a floating point number"
96                 import qt
97                 qt.QMessageBox.warning(None,"Error",message)
98                 return
99             floatingValues.append(val)
100             pass
101         
102         if len(t.rows)>0:
103             if len(t.rows[0].values) != len(floatingValues):
104                 message  = "Syntax error at line " + str(num) + " ... \n"
105                 message += "All rows must have the same number of fields"
106                 import qt
107                 qt.QMessageBox.warning(None,"Error",message)
108                 return
109             pass
110         
111         row.values = floatingValues
112         t.rows.append(row)
113         pass
114     
115     import salome
116     myStudy = salome.myStudy
117     myBuilder = myStudy.NewBuilder()
118     
119     # >>> Getting (loading) VISU component =======================================
120     myVisu = salome.lcc.FindOrLoadComponent("FactoryServer", "VISU")
121     myComponent = myStudy.FindComponent("VISU")
122     if not myComponent:
123         myComponent = myBuilder.NewComponent("VISU")
124         aName = myBuilder.FindOrCreateAttribute(myComponent, "AttributeName")
125         #aName.SetValue("Visu")
126         aName.SetValue( salome.sg.getComponentUserName("VISU") )
127         myBuilder.DefineComponentInstance(myComponent,myVisu)
128         pass
129     
130     # >>> Creating object with file name =====================================
131     myFileObject = myBuilder.NewObject(myComponent)
132     AName = myBuilder.FindOrCreateAttribute(myFileObject, "AttributeName")
133     import os.path
134     AName.SetValue(os.path.basename(ffile))
135     
136     num = 1
137     for t in tables:
138         # >>> Creating object with Table of real =====================================
139         myTRealObject = myBuilder.NewObject(myFileObject)
140         AName = myBuilder.FindOrCreateAttribute(myTRealObject, "AttributeName")
141         if t.title:
142             AName.SetValue(t.title)
143         else:
144             AName.SetValue("Table "+str(num))
145             pass
146         num = num+1
147         ARealTable = myBuilder.FindOrCreateAttribute(myTRealObject, "AttributeTableOfReal")
148         if t.title:
149             ARealTable.SetTitle(t.title)
150             pass
151         cols = t.getColumns()
152         for col in cols:
153             ARealTable.AddRow(col.values)
154             ARealTable.SetRowTitle(cols.index(col)+1, col.title)
155             ARealTable.SetRowUnit(cols.index(col)+1, col.unit)
156             # ARealTable.SetColumnTitles(l.values())
157             pass
158         pass
159
160     
161
162     # >>> Updating Object Browser ================================================
163     salome.sg.updateObjBrowser(1)
164
165     
166     return
167