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