Salome HOME
sources v1.2
[modules/yacs.git] / src / SALOMEDS / SALOMEDS_AttributeTableOfReal_i.cxx
1 //  SALOME SALOMEDS : data structure of SALOME and sources of Salome data server 
2 //
3 //  Copyright (C) 2003  CEA/DEN, EDF R&D
4 //
5 //
6 //
7 //  File   : SALOMEDS_AttributeTableOfReal_i.cxx
8 //  Author : Michael Ponikarov
9 //  Module : SALOME
10 //  $Header$
11
12 using namespace std;
13 #include "SALOMEDS_AttributeTableOfReal_i.hxx"
14 #include "SALOMEDS_SObject_i.hxx"
15 #include <TColStd_HSequenceOfReal.hxx>
16 #include <Standard_Failure.hxx>
17 #include <Standard_ErrorHandler.hxx>
18
19 #include <strstream>
20 #include <string>
21
22 #define SEPARATOR '\1'
23
24 static TCollection_ExtendedString getUnit(TCollection_ExtendedString theString)
25 {
26   TCollection_ExtendedString aString(theString);
27   int aPos = aString.Search(SEPARATOR);
28   if(aPos <= 0 || aPos == aString.Length() ) return TCollection_ExtendedString();
29   return aString.Split(aPos);
30 }
31
32 static TCollection_ExtendedString getTitle(TCollection_ExtendedString theString)
33 {
34   TCollection_ExtendedString aString(theString);
35   int aPos = aString.Search(SEPARATOR);
36   if(aPos < 1) return aString;
37   if(aPos == 1) return TCollection_ExtendedString();
38   aString.Split(aPos-1);
39   return aString;
40 }
41
42
43
44 void SALOMEDS_AttributeTableOfReal_i::SetTitle(const char* theTitle) {
45   CheckLocked();
46   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
47   CORBA::String_var aStr = CORBA::string_dup(theTitle);
48   aTable->SetTitle(TCollection_ExtendedString(aStr));
49 }
50
51 char* SALOMEDS_AttributeTableOfReal_i::GetTitle() {
52   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
53   CORBA::String_var c_s = CORBA::string_dup(TCollection_AsciiString(aTable->GetTitle()).ToCString());
54   return c_s._retn();
55 }
56
57 void SALOMEDS_AttributeTableOfReal_i::SetRowTitle(CORBA::Long theIndex, const char* theTitle)
58      throw (SALOMEDS::AttributeTableOfReal::IncorrectIndex)
59 {
60   CheckLocked();
61   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
62   if (theIndex <= 0 || theIndex > aTable->GetNbRows()) throw SALOMEDS::AttributeTableOfReal::IncorrectIndex();
63   CORBA::String_var aStr = CORBA::string_dup(theTitle);
64   TCollection_ExtendedString aTitle(aStr);
65   TCollection_ExtendedString aUnit = getUnit(aTable->GetRowTitle(theIndex));
66   if(aUnit.Length() > 0) {
67     aTitle += SEPARATOR;
68     aTitle += aUnit;
69   }
70
71   aTable->SetRowTitle(theIndex, aTitle);
72 }
73
74 void SALOMEDS_AttributeTableOfReal_i::SetRowTitles(const SALOMEDS::StringSeq& theTitles)
75      throw (SALOMEDS::AttributeTableOfReal::IncorrectArgumentLength)
76 {
77   CheckLocked();
78   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
79   if (theTitles.length() != aTable->GetNbRows()) throw SALOMEDS::AttributeTableOfReal::IncorrectArgumentLength();
80   for (int i = 0; i < theTitles.length(); i++) {
81     SetRowTitle(i + 1, theTitles[i]);
82   }
83 }
84
85 SALOMEDS::StringSeq* SALOMEDS_AttributeTableOfReal_i::GetRowTitles() {
86   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
87   SALOMEDS::StringSeq_var aTitles = new SALOMEDS::StringSeq;
88   aTitles->length(aTable->GetNbRows());
89   for(int i = 0; i < aTitles->length(); i++)
90     aTitles[i] = CORBA::string_dup(TCollection_AsciiString(getTitle(aTable->GetRowTitle(i + 1))).ToCString());
91   return aTitles._retn();
92 }
93
94 void SALOMEDS_AttributeTableOfReal_i::SetColumnTitle(CORBA::Long theIndex, const char* theTitle)
95      throw (SALOMEDS::AttributeTableOfReal::IncorrectIndex)
96 {
97   CheckLocked();
98   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
99   if (theIndex <= 0 || theIndex > aTable->GetNbColumns()) throw SALOMEDS::AttributeTableOfReal::IncorrectIndex();
100   CORBA::String_var aStr = CORBA::string_dup(theTitle);
101   aTable->SetColumnTitle(theIndex, TCollection_ExtendedString(aStr));
102 }
103
104 void SALOMEDS_AttributeTableOfReal_i::SetColumnTitles(const SALOMEDS::StringSeq& theTitles)
105      throw (SALOMEDS::AttributeTableOfReal::IncorrectArgumentLength)
106 {
107   CheckLocked();
108   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
109   if (theTitles.length() != aTable->GetNbColumns()) throw SALOMEDS::AttributeTableOfReal::IncorrectArgumentLength();
110   for (int i = 0; i < theTitles.length(); i++) {
111     SetColumnTitle(i + 1, theTitles[i]);
112   }
113 }
114
115 SALOMEDS::StringSeq* SALOMEDS_AttributeTableOfReal_i::GetColumnTitles() {
116   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
117   SALOMEDS::StringSeq_var aTitles = new SALOMEDS::StringSeq;
118   aTitles->length(aTable->GetNbColumns());
119   for(int i = 0; i < aTitles->length(); i++)
120     aTitles[i] = CORBA::string_dup(TCollection_AsciiString(aTable->GetColumnTitle(i + 1)).ToCString());
121   return aTitles._retn();
122 }
123
124 //Units support
125 void SALOMEDS_AttributeTableOfReal_i::SetRowUnit(CORBA::Long theIndex, const char* theUnit)
126      throw (SALOMEDS::AttributeTableOfReal::IncorrectIndex)
127 {
128   CheckLocked();
129   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
130   if (theIndex <= 0 || theIndex > aTable->GetNbRows()) throw SALOMEDS::AttributeTableOfReal::IncorrectIndex();
131   CORBA::String_var aStr = CORBA::string_dup(theUnit);
132   TCollection_ExtendedString aTitle = getTitle(aTable->GetRowTitle(theIndex));
133   TCollection_ExtendedString aUnit(aStr);
134   aTitle += SEPARATOR;
135   aTitle += aUnit;
136   aTable->SetRowTitle(theIndex, aTitle);
137 }
138
139 void SALOMEDS_AttributeTableOfReal_i::SetRowUnits(const SALOMEDS::StringSeq& theUnits)
140      throw (SALOMEDS::AttributeTableOfReal::IncorrectArgumentLength)
141 {
142   CheckLocked();
143   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
144   if (theUnits.length() != aTable->GetNbRows()) throw SALOMEDS::AttributeTableOfReal::IncorrectArgumentLength();
145   for (int i = 0; i < theUnits.length(); i++) {
146     SetRowUnit(i + 1, theUnits[i]);
147   }
148 }
149
150 SALOMEDS::StringSeq* SALOMEDS_AttributeTableOfReal_i::GetRowUnits() {
151   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
152   SALOMEDS::StringSeq_var aUnits = new SALOMEDS::StringSeq;
153   aUnits->length(aTable->GetNbRows());
154   for(int i = 0; i < aUnits->length(); i++)
155     aUnits[i] = CORBA::string_dup(TCollection_AsciiString(getUnit(aTable->GetRowTitle(i + 1))).ToCString());
156   return aUnits._retn();
157 }
158
159
160 CORBA::Long SALOMEDS_AttributeTableOfReal_i::GetNbRows() {
161   return Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr)->GetNbRows();
162 }
163 CORBA::Long SALOMEDS_AttributeTableOfReal_i::GetNbColumns() {
164   return Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr)->GetNbColumns();
165 }
166
167 void SALOMEDS_AttributeTableOfReal_i::AddRow(const SALOMEDS::DoubleSeq& theData)
168      throw (SALOMEDS::AttributeTableOfReal::IncorrectArgumentLength)
169 {
170   CheckLocked();
171   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
172   
173   Handle(TColStd_HSequenceOfReal) aRow = new TColStd_HSequenceOfReal;
174   for (int i = 0; i < theData.length(); i++) aRow->Append(theData[i]);
175   aTable->SetRowData(aTable->GetNbRows() + 1, aRow);
176 }
177
178 void SALOMEDS_AttributeTableOfReal_i::SetRow(CORBA::Long theRow, const SALOMEDS::DoubleSeq& theData)
179      throw (SALOMEDS::AttributeTableOfReal::IncorrectArgumentLength, SALOMEDS::AttributeTableOfReal::IncorrectIndex)
180 {
181   CheckLocked();
182   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
183   
184   Handle(TColStd_HSequenceOfReal) aRow = new TColStd_HSequenceOfReal;
185   for (int i = 0; i < theData.length(); i++) aRow->Append(theData[i]);
186   aTable->SetRowData(theRow, aRow);
187 }
188
189 SALOMEDS::DoubleSeq* SALOMEDS_AttributeTableOfReal_i::GetRow(CORBA::Long theRow)
190      throw (SALOMEDS::AttributeTableOfReal::IncorrectIndex)
191 {
192   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
193   if (theRow <= 0 || theRow > aTable->GetNbRows()) throw SALOMEDS::AttributeTableOfReal::IncorrectIndex();
194
195   SALOMEDS::DoubleSeq_var CorbaSeq = new SALOMEDS::DoubleSeq;
196   Handle(TColStd_HSequenceOfReal) aRow = aTable->GetRowData(theRow);
197   CorbaSeq->length(aRow->Length());
198   for (int i = 0; i < aRow->Length(); i++) {
199     CorbaSeq[i] = aRow->Value(i + 1);
200   }
201   return CorbaSeq._retn();
202 }
203
204 void SALOMEDS_AttributeTableOfReal_i::AddColumn(const SALOMEDS::DoubleSeq& theData)
205      throw (SALOMEDS::AttributeTableOfReal::IncorrectArgumentLength)
206 {
207   CheckLocked();
208   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
209   
210   Handle(TColStd_HSequenceOfReal) aColumn = new TColStd_HSequenceOfReal;
211   for (int i = 0; i < theData.length(); i++) aColumn->Append(theData[i]);
212   aTable->SetColumnData(aTable->GetNbColumns() + 1, aColumn);
213 }
214
215 void SALOMEDS_AttributeTableOfReal_i::SetColumn(CORBA::Long theColumn, const SALOMEDS::DoubleSeq& theData)
216      throw (SALOMEDS::AttributeTableOfReal::IncorrectArgumentLength, SALOMEDS::AttributeTableOfReal::IncorrectIndex)
217 {
218   CheckLocked();
219   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
220   
221   Handle(TColStd_HSequenceOfReal) aColumn = new TColStd_HSequenceOfReal;
222   for (int i = 0; i < theData.length(); i++) aColumn->Append(theData[i]);
223   aTable->SetColumnData(theColumn, aColumn);
224 }
225
226 SALOMEDS::DoubleSeq* SALOMEDS_AttributeTableOfReal_i::GetColumn(CORBA::Long theColumn)
227      throw (SALOMEDS::AttributeTableOfReal::IncorrectIndex)
228 {
229   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
230   if (theColumn <= 0 || theColumn > aTable->GetNbColumns()) throw SALOMEDS::AttributeTableOfReal::IncorrectIndex();
231
232   SALOMEDS::DoubleSeq_var CorbaSeq = new SALOMEDS::DoubleSeq;
233   Handle(TColStd_HSequenceOfReal) aColumn = aTable->GetColumnData(theColumn);
234   CorbaSeq->length(aColumn->Length());
235   for (int i = 0; i < aColumn->Length(); i++) {
236     CorbaSeq[i] = aColumn->Value(i + 1);
237   }
238   return CorbaSeq._retn();
239 }
240
241 void SALOMEDS_AttributeTableOfReal_i::PutValue(CORBA::Double theValue, CORBA::Long theRow, CORBA::Long theColumn)
242      throw (SALOMEDS::AttributeTableOfReal::IncorrectIndex)
243 {
244   CheckLocked();
245   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
246
247   aTable->PutValue(theValue, theRow, theColumn);
248 }
249
250 CORBA::Boolean SALOMEDS_AttributeTableOfReal_i::HasValue(CORBA::Long theRow, CORBA::Long theColumn) {
251   return Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr)->HasValue(theRow, theColumn);
252 }
253
254 CORBA::Double SALOMEDS_AttributeTableOfReal_i::GetValue(CORBA::Long theRow, CORBA::Long theColumn)
255      throw (SALOMEDS::AttributeTableOfReal::IncorrectIndex)
256 {
257   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
258   if (theRow > aTable->GetNbRows()) throw SALOMEDS::AttributeTableOfReal::IncorrectIndex();
259
260   CORBA::Double aValue;
261
262   try {
263     aValue = aTable->GetValue(theRow, theColumn);
264   }
265   catch(Standard_Failure) {
266     throw SALOMEDS::AttributeTableOfReal::IncorrectIndex();
267   }
268
269   return aValue;
270 }
271
272
273 SALOMEDS::LongSeq* SALOMEDS_AttributeTableOfReal_i::GetRowSetIndices(CORBA::Long theRow) 
274 {
275   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
276
277   if(theRow <= 0 || theRow > aTable->GetNbRows()) throw SALOMEDS::AttributeTableOfReal::IncorrectIndex();
278
279   SALOMEDS::LongSeq_var CorbaSeq = new SALOMEDS::LongSeq;
280   Handle(TColStd_HSequenceOfInteger) aSeq = aTable->GetSetRowIndices(theRow);
281   CorbaSeq->length(aSeq->Length());
282   for (int i = 0; i < aSeq->Length(); i++) {
283     CorbaSeq[i] = aSeq->Value(i + 1);
284   }
285   return CorbaSeq._retn(); 
286 }
287
288
289 void SALOMEDS_AttributeTableOfReal_i::SetNbColumns(CORBA::Long theNbColumns)
290 {
291   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
292   aTable->SetNbColumns(theNbColumns);
293 }
294
295 bool SALOMEDS_AttributeTableOfReal_i::ReadFromFile(const SALOMEDS::TMPFile& theStream) 
296 {
297   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
298
299   istrstream aStream((char*)&theStream[0], theStream.length());
300   return aTable->RestoreFromString(aStream);
301 }
302
303 SALOMEDS::TMPFile*  SALOMEDS_AttributeTableOfReal_i::SaveToFile()
304 {
305   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
306
307   ostrstream ostr;
308   string aString;
309   aTable->ConvertToString(ostr);
310
311   aString = ostr.rdbuf()->str();
312
313   char* aBuffer = (char*)CORBA::string_dup(aString.c_str());
314   int aBufferSize = strlen((char*)aBuffer);
315
316   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)aBuffer;
317
318   SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile(aBufferSize, aBufferSize, anOctetBuf, 1);
319
320   return aStreamFile._retn();
321 }
322