]> SALOME platform Git repositories - modules/kernel.git/blob - src/SALOMEDSImpl/SALOMEDSImpl_AttributeTableOfReal.cxx
Salome HOME
a5767647226a8496e23c99f68e19d8b0b2ecacac
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_AttributeTableOfReal.cxx
1 //  File   : SALOMEDSImpl_AttributeTableOfReal.cxx
2 //  Author : Michael Ponikarov
3 //  Module : SALOME
4
5 #include <SALOMEDSImpl_AttributeTableOfReal.hxx>
6 #include <Standard_Failure.hxx>
7 #include <TColStd_DataMapIteratorOfDataMapOfIntegerReal.hxx>
8 #include <Standard_GUID.hxx>
9 #include <stdio.h>
10 #include <TColStd_HSequenceOfExtendedString.hxx>  
11
12 using namespace std;
13
14 IMPLEMENT_STANDARD_HANDLE( SALOMEDSImpl_AttributeTableOfReal, SALOMEDSImpl_GenericAttribute )
15 IMPLEMENT_STANDARD_RTTIEXT( SALOMEDSImpl_AttributeTableOfReal, SALOMEDSImpl_GenericAttribute )
16
17 #define SEPARATOR '\1'
18
19 static TCollection_ExtendedString getUnit(TCollection_ExtendedString theString)
20 {
21   TCollection_ExtendedString aString(theString);
22   int aPos = aString.Search(SEPARATOR);
23   if(aPos <= 0 || aPos == aString.Length() ) return TCollection_ExtendedString();
24   return aString.Split(aPos);
25 }
26
27 static TCollection_ExtendedString getTitle(TCollection_ExtendedString theString)
28 {
29   TCollection_ExtendedString aString(theString);
30   int aPos = aString.Search(SEPARATOR);
31   if(aPos < 1) return aString;
32   if(aPos == 1) return TCollection_ExtendedString();
33   aString.Split(aPos-1);
34   return aString;
35 }
36
37 const Standard_GUID& SALOMEDSImpl_AttributeTableOfReal::GetID() 
38 {
39   static Standard_GUID SALOMEDSImpl_AttributeTableOfRealID ("128371A1-8F52-11d6-A8A3-0001021E8C7F");
40   return SALOMEDSImpl_AttributeTableOfRealID;
41 }
42
43 Handle(SALOMEDSImpl_AttributeTableOfReal) SALOMEDSImpl_AttributeTableOfReal::Set(const TDF_Label& label) 
44 {
45   Handle(SALOMEDSImpl_AttributeTableOfReal) anAttr;
46   if (!label.FindAttribute(SALOMEDSImpl_AttributeTableOfReal::GetID(),anAttr)) {
47     anAttr = new SALOMEDSImpl_AttributeTableOfReal();
48     label.AddAttribute(anAttr);
49   }
50   return anAttr;
51 }
52
53 SALOMEDSImpl_AttributeTableOfReal::SALOMEDSImpl_AttributeTableOfReal() 
54 :SALOMEDSImpl_GenericAttribute("AttributeTableOfReal")
55 {
56   myRows = new TColStd_HSequenceOfExtendedString();
57   myCols = new TColStd_HSequenceOfExtendedString();
58   myNbRows = 0;
59   myNbColumns = 0;
60 }
61
62 void SALOMEDSImpl_AttributeTableOfReal::SetNbColumns(const Standard_Integer theNbColumns)
63 {
64   CheckLocked();  
65   Backup();
66   
67   TColStd_DataMapOfIntegerReal aMap;
68   aMap = myTable;
69   myTable.Clear();
70
71   TColStd_DataMapIteratorOfDataMapOfIntegerReal anIterator(aMap);
72   for(; anIterator.More(); anIterator.Next()) {
73     int aRow = (int)(anIterator.Key()/myNbColumns) + 1;
74     int aCol = (int)(anIterator.Key() - myNbColumns*(aRow-1));
75     if(aCol == 0) { aCol = myNbColumns; aRow--; }
76     if(aCol > theNbColumns) continue;
77     int aKey = (aRow-1)*theNbColumns+aCol;
78     myTable.Bind(aKey, anIterator.Value());
79   }
80
81   myNbColumns = theNbColumns;
82
83   while (myCols->Length() < myNbColumns) { // append empty columns titles
84     myCols->Append(TCollection_ExtendedString(""));
85   }
86   
87   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
88 }
89
90 void SALOMEDSImpl_AttributeTableOfReal::SetTitle(const TCollection_ExtendedString& theTitle) 
91 {
92   CheckLocked();  
93   Backup();
94   myTitle = theTitle;
95   
96   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
97 }
98
99 TCollection_ExtendedString SALOMEDSImpl_AttributeTableOfReal::GetTitle() const 
100 {
101   return myTitle;
102 }
103
104 void SALOMEDSImpl_AttributeTableOfReal::SetRowData(const Standard_Integer theRow,
105                                                    const Handle(TColStd_HSequenceOfReal)& theData) 
106 {
107   CheckLocked();  
108   if(theData->Length() > myNbColumns) SetNbColumns(theData->Length());
109
110   Backup();
111
112   while (myRows->Length() < theRow) { // append new row titles
113     myRows->Append(TCollection_ExtendedString(""));
114   }
115
116   Standard_Integer i, aShift = (theRow-1)*myNbColumns, aLength = theData->Length();
117   for(i = 1; i <= aLength; i++) {
118     myTable.Bind(aShift + i, theData->Value(i));
119   }
120
121   if(theRow > myNbRows) myNbRows = theRow;
122   
123   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
124 }
125
126 Handle(TColStd_HSequenceOfReal) SALOMEDSImpl_AttributeTableOfReal::GetRowData(const Standard_Integer theRow)
127 {
128   Handle(TColStd_HSequenceOfReal) aSeq = new TColStd_HSequenceOfReal();
129   Standard_Integer i, aShift = (theRow-1)*myNbColumns;
130   for(i = 1; i <= myNbColumns; i++) {
131      if(myTable.IsBound(aShift+i)) 
132        aSeq->Append(myTable.Find(aShift+i));
133      else
134        aSeq->Append(0.);
135   }
136   
137   return aSeq;
138 }
139
140
141 void SALOMEDSImpl_AttributeTableOfReal::SetRowTitle(const Standard_Integer theRow,
142                                                        const TCollection_ExtendedString& theTitle) 
143 {
144   CheckLocked();  
145   Backup();
146   TCollection_ExtendedString aTitle(theTitle), aUnit = GetRowUnit(theRow);
147   if(aUnit.Length()>0) {
148     aTitle += SEPARATOR;
149     aTitle += aUnit;
150   }
151   myRows->SetValue(theRow, aTitle);
152   
153   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
154 }
155
156 void SALOMEDSImpl_AttributeTableOfReal::SetRowUnit(const Standard_Integer theRow,
157                                                       const TCollection_ExtendedString& theUnit) 
158 {
159   CheckLocked();  
160   Backup();
161   TCollection_ExtendedString aTitle = GetRowTitle(theRow);
162   aTitle += SEPARATOR;
163   aTitle += theUnit;
164
165   myRows->SetValue(theRow, aTitle);
166   
167   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
168 }
169
170 void SALOMEDSImpl_AttributeTableOfReal::SetRowUnits(const Handle(TColStd_HSequenceOfExtendedString)& theUnits)
171 {
172   if (theUnits->Length() != GetNbRows()) Standard_Failure::Raise("Invalid number of rows");
173   int aLength = theUnits->Length(), i;
174   for(i = 1; i <= aLength; i++) SetRowUnit(i, theUnits->Value(i));
175   
176   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
177 }
178
179 Handle(TColStd_HSequenceOfExtendedString) SALOMEDSImpl_AttributeTableOfReal::GetRowUnits()
180 {
181   Handle(TColStd_HSequenceOfExtendedString) aSeq = new TColStd_HSequenceOfExtendedString;
182   int aLength = myRows->Length(), i;
183   for(i=1; i<=aLength; i++) aSeq->Append(getUnit(myRows->Value(i)));
184   return aSeq;
185 }
186
187 void SALOMEDSImpl_AttributeTableOfReal::SetRowTitles(const Handle(TColStd_HSequenceOfExtendedString)& theTitles)
188 {
189   if (theTitles->Length() != GetNbRows()) Standard_Failure::Raise("Invalid number of rows");
190   int aLength = theTitles->Length(), i;
191   for(i = 1; i <= aLength; i++) SetRowTitle(i, theTitles->Value(i));
192   
193   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
194 }
195
196 Handle(TColStd_HSequenceOfExtendedString) SALOMEDSImpl_AttributeTableOfReal::GetRowTitles()
197 {
198   Handle(TColStd_HSequenceOfExtendedString) aSeq = new TColStd_HSequenceOfExtendedString;
199   int aLength = myRows->Length(), i;
200   for(i=1; i<=aLength; i++) aSeq->Append(getTitle(myRows->Value(i)));
201   return aSeq;
202 }
203
204
205 TCollection_ExtendedString SALOMEDSImpl_AttributeTableOfReal::GetRowTitle(const Standard_Integer theRow) const 
206 {
207   return getTitle(myRows->Value(theRow));
208 }
209
210
211 TCollection_ExtendedString SALOMEDSImpl_AttributeTableOfReal::GetRowUnit(const Standard_Integer theRow) const 
212 {
213   return getUnit(myRows->Value(theRow));
214 }
215
216 void SALOMEDSImpl_AttributeTableOfReal::SetColumnData(const Standard_Integer theColumn,
217                                                       const Handle(TColStd_HSequenceOfReal)& theData) 
218 {
219   CheckLocked();  
220   if(theColumn > myNbColumns) SetNbColumns(theColumn);
221
222   Backup();
223
224   Standard_Integer i, aLength = theData->Length();
225   for(i = 1; i <= aLength; i++) {
226     myTable.Bind(myNbColumns*(i-1)+theColumn, theData->Value(i));
227   }
228
229   if(aLength > myNbRows) {
230     myNbRows = aLength;
231     while (myRows->Length() < myNbRows) { // append empty row titles
232       myRows->Append(TCollection_ExtendedString(""));
233     }
234   }
235   
236   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
237 }
238
239
240 Handle(TColStd_HSequenceOfReal) SALOMEDSImpl_AttributeTableOfReal::GetColumnData(const Standard_Integer theColumn)
241 {
242   Handle(TColStd_HSequenceOfReal) aSeq = new TColStd_HSequenceOfReal;
243   
244   Standard_Integer i, anIndex;
245   for(i = 1; i <= myNbRows; i++) {
246     anIndex = myNbColumns*(i-1) + theColumn;
247     if(myTable.IsBound(anIndex)) 
248       aSeq->Append(myTable.Find(anIndex));
249     else
250       aSeq->Append(0.);
251   }
252   
253   return aSeq;
254 }
255
256 void SALOMEDSImpl_AttributeTableOfReal::SetColumnTitle(const Standard_Integer theColumn,
257                                                        const TCollection_ExtendedString& theTitle) 
258 {
259   CheckLocked();  
260   Backup();
261   while(myCols->Length() < theColumn) myCols->Append(TCollection_ExtendedString(""));
262   myCols->SetValue(theColumn,theTitle);
263   
264   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
265 }
266
267 TCollection_ExtendedString SALOMEDSImpl_AttributeTableOfReal::GetColumnTitle(const Standard_Integer theColumn) const 
268 {
269   if(myCols.IsNull()) return "";
270   if(myCols->Length() < theColumn) return "";
271   return myCols->Value(theColumn);
272 }
273
274 void SALOMEDSImpl_AttributeTableOfReal::SetColumnTitles(const Handle(TColStd_HSequenceOfExtendedString)& theTitles)
275 {
276   if (theTitles->Length() != myNbColumns) Standard_Failure::Raise("Invalid number of columns");
277   int aLength = theTitles->Length(), i;
278   for(i = 1; i <= aLength; i++)  myCols->SetValue(i, theTitles->Value(i));
279   
280   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
281 }
282
283 Handle(TColStd_HSequenceOfExtendedString) SALOMEDSImpl_AttributeTableOfReal::GetColumnTitles()
284 {
285   Handle(TColStd_HSequenceOfExtendedString) aSeq = new TColStd_HSequenceOfExtendedString;
286   int aLength = myCols->Length(), i;
287   for(i=1; i<=aLength; i++) aSeq->Append(myCols->Value(i));
288   return aSeq;
289 }
290
291 Standard_Integer SALOMEDSImpl_AttributeTableOfReal::GetNbRows() const
292 {
293   return myNbRows;
294 }
295
296 Standard_Integer SALOMEDSImpl_AttributeTableOfReal::GetNbColumns() const
297 {
298   return myNbColumns;
299 }
300
301 void SALOMEDSImpl_AttributeTableOfReal::PutValue(const Standard_Real theValue,
302                                              const Standard_Integer theRow,
303                                              const Standard_Integer theColumn) 
304 {
305   CheckLocked();      
306   if(theColumn > myNbColumns) SetNbColumns(theColumn);
307
308   Standard_Integer anIndex = (theRow-1)*myNbColumns + theColumn;
309   myTable.Bind(anIndex, theValue);
310
311   if(theRow > myNbRows) {
312     while (myRows->Length() < theRow) { // append empty row titles
313       myRows->Append(TCollection_ExtendedString(""));
314     }
315     myNbRows = theRow;
316   }
317   
318   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
319 }
320
321 Standard_Boolean SALOMEDSImpl_AttributeTableOfReal::HasValue(const Standard_Integer theRow,
322                                                          const Standard_Integer theColumn) 
323 {
324   if(theRow > myNbRows || theRow < 1) return Standard_False;
325   if(theColumn > myNbColumns || theColumn < 1) return Standard_False;
326   Standard_Integer anIndex = (theRow-1)*myNbColumns + theColumn;
327   return myTable.IsBound(anIndex); 
328 }
329
330 Standard_Real SALOMEDSImpl_AttributeTableOfReal::GetValue(const Standard_Integer theRow,
331                                                       const Standard_Integer theColumn) 
332 {
333   if(theRow > myNbRows || theRow < 1) Standard_Failure::Raise("Invalid cell index");
334   if(theColumn > myNbColumns || theColumn < 1) Standard_Failure::Raise("Invalid cell index");
335
336   Standard_Integer anIndex = (theRow-1)*myNbColumns + theColumn;
337   if(myTable.IsBound(anIndex)) return myTable.Find(anIndex);
338   
339   Standard_Failure::Raise("Invalid cell index");
340   return 0.;
341 }
342
343 const Standard_GUID& SALOMEDSImpl_AttributeTableOfReal::ID() const
344 {
345   return GetID();
346 }
347
348 void SALOMEDSImpl_AttributeTableOfReal::Restore(const Handle(TDF_Attribute)& with) 
349 {
350   Standard_Integer anIndex;
351   Handle(SALOMEDSImpl_AttributeTableOfReal) aTable = Handle(SALOMEDSImpl_AttributeTableOfReal)::DownCast(with);
352
353   myTable.Clear();
354   myCols->Clear();
355   myRows->Clear();
356
357   myTable = aTable->myTable;
358   myNbRows = aTable->myNbRows;
359   myNbColumns = aTable->myNbColumns;
360   myTitle = aTable->myTitle;
361   
362   for(anIndex = 1; anIndex <= aTable->GetNbRows();anIndex++)
363     myRows->Append(aTable->GetRowTitle(anIndex));
364
365   for(anIndex = 1; anIndex <= aTable->GetNbColumns(); anIndex++) 
366     myCols->Append(aTable->GetColumnTitle(anIndex));
367 }
368
369 Handle(TDF_Attribute) SALOMEDSImpl_AttributeTableOfReal::NewEmpty() const
370 {
371   return new SALOMEDSImpl_AttributeTableOfReal();
372 }
373
374 void SALOMEDSImpl_AttributeTableOfReal::Paste(const Handle(TDF_Attribute)& into,
375                                              const Handle(TDF_RelocationTable)&) const
376 {
377   Standard_Integer anIndex;
378   Handle(SALOMEDSImpl_AttributeTableOfReal) aTable = Handle(SALOMEDSImpl_AttributeTableOfReal)::DownCast(into);
379
380   aTable->myTable.Clear();
381   aTable->myCols->Clear();
382   aTable->myRows->Clear();
383
384   aTable->myTable = myTable;
385   aTable->myTitle = myTitle;
386   aTable->myNbRows = myNbRows;
387   aTable->myNbColumns = myNbColumns;
388
389   for(anIndex = 1; anIndex <= GetNbRows();anIndex++)
390     aTable->myRows->Append(GetRowTitle(anIndex));
391   for(anIndex = 1; anIndex <= GetNbColumns(); anIndex++) 
392     aTable->myCols->Append(GetColumnTitle(anIndex));
393 }
394
395
396 Handle_TColStd_HSequenceOfInteger SALOMEDSImpl_AttributeTableOfReal::GetSetRowIndices(const Standard_Integer theRow)
397 {
398   Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
399
400   Standard_Integer i, aShift = myNbColumns*(theRow-1);
401   for(i = 1; i <= myNbColumns; i++) {
402     if(myTable.IsBound(aShift + i)) aSeq->Append(i);
403   }
404   
405   return aSeq;
406 }
407
408 Handle_TColStd_HSequenceOfInteger SALOMEDSImpl_AttributeTableOfReal::GetSetColumnIndices(const Standard_Integer theColumn)
409 {
410   Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
411
412   Standard_Integer i, anIndex;
413   for(i = 1; i <= myNbRows; i++) {
414     anIndex = myNbColumns*(i-1)+theColumn;
415     if(myTable.IsBound(anIndex)) aSeq->Append(i);
416   }
417   
418   return aSeq;
419 }
420
421
422
423 void SALOMEDSImpl_AttributeTableOfReal::ConvertToString(ostrstream& theStream)
424 {
425   int i, j, l;
426
427   
428   //Title
429   l = myTitle.Length();
430   theStream << l << "\n";
431   for(i=1; i<=l; i++)
432     theStream << myTitle.Value(i) << "\n";
433
434   //Nb rows
435   theStream << myNbRows << "\n";
436
437   //Rows titles
438   for(i=1; i<=myNbRows; i++) {
439     l = myRows->Value(i).Length();
440     theStream << l << "\n";
441     for(j=1; j<=l; j++)
442       theStream << myRows->Value(i).Value(j) << "\n";
443   }
444
445   //Nb columns
446   theStream << myNbColumns << "\n";
447
448   //Columns titles
449   for(i=1; i<=myNbColumns; i++) {
450     l = myCols->Value(i).Length();
451     theStream << l << "\n";
452     for(j=1; j<=l; j++)
453       theStream << myCols->Value(i).Value(j) << "\n";
454   }
455
456   //Store the table values
457   l = myTable.Extent();
458   theStream << l << "\n";
459   char *aBuffer = new char[128];
460   TColStd_DataMapIteratorOfDataMapOfIntegerReal anIterator(myTable);
461   for(; anIterator.More(); anIterator.Next()) {
462     theStream << anIterator.Key() << "\n";
463     sprintf(aBuffer, "%.64e", anIterator.Value());
464     theStream << aBuffer << "\n";
465   }
466   
467   delete []aBuffer;
468
469   return;
470 }
471
472 bool SALOMEDSImpl_AttributeTableOfReal::RestoreFromString(istrstream& theStream)
473 {
474   Backup();
475
476   int i, j, l;
477
478   Standard_ExtCharacter anExtChar;
479   TCollection_ExtendedString aStr;
480
481   //Title
482   theStream >> l;
483
484   myTitle = TCollection_ExtendedString(l, 0);
485   for(i=1; i<=l; i++) {
486     theStream >> anExtChar;
487     myTitle.SetValue(i, anExtChar);
488   }
489
490   //Nb rows
491   theStream >> myNbRows;
492
493   //Rows titles
494   myRows->Clear();  
495   for(i=1; i<=myNbRows; i++) { 
496     theStream >> l;
497     aStr = TCollection_ExtendedString(l,0);
498     for(j=1; j<=l; j++) {
499       theStream >> anExtChar;
500       aStr.SetValue(j, anExtChar);
501     }
502     myRows->Append(aStr);
503   }
504
505   //Nb columns
506   theStream >> myNbColumns;
507
508   //Columns titles
509   myCols->Clear();
510   for(i=1; i<=myNbColumns; i++) {
511     theStream >> l;
512     aStr = TCollection_ExtendedString(l,0);
513     for(j=1; j<=l; j++) {
514       theStream >> anExtChar;
515       aStr.SetValue(j, anExtChar);
516     }
517     myCols->Append(aStr);
518   }
519
520   //Restore the table values
521   theStream >> l;
522   myTable.Clear();
523   for(i=1; i<=l; i++) {
524     Standard_Integer aKey;
525     Standard_Real aValue;
526     theStream >> aKey;
527     theStream >> aValue;
528     myTable.Bind(aKey, aValue);
529   }
530
531   return true;
532 }
533
534 TCollection_AsciiString SALOMEDSImpl_AttributeTableOfReal::Save() 
535 {
536   ostrstream ostr;
537   ConvertToString(ostr);
538   TCollection_AsciiString aString((char*)ostr.rdbuf()->str());
539   return aString;
540 }
541
542 void SALOMEDSImpl_AttributeTableOfReal::Load(const TCollection_AsciiString& value) 
543 {
544   istrstream aStream(value.ToCString(), strlen(value.ToCString()));
545   RestoreFromString(aStream);
546 }