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