1 // File : SALOMEDSImpl_AttributeTableOfReal.cxx
2 // Author : Michael Ponikarov
5 #include <SALOMEDSImpl_AttributeTableOfReal.hxx>
6 #include <Standard_Failure.hxx>
7 #include <TColStd_DataMapIteratorOfDataMapOfIntegerReal.hxx>
8 #include <Standard_GUID.hxx>
10 #include <TColStd_HSequenceOfExtendedString.hxx>
14 IMPLEMENT_STANDARD_HANDLE( SALOMEDSImpl_AttributeTableOfReal, SALOMEDSImpl_GenericAttribute )
15 IMPLEMENT_STANDARD_RTTIEXT( SALOMEDSImpl_AttributeTableOfReal, SALOMEDSImpl_GenericAttribute )
17 #define SEPARATOR '\1'
19 static TCollection_ExtendedString getUnit(TCollection_ExtendedString theString)
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);
27 static TCollection_ExtendedString getTitle(TCollection_ExtendedString theString)
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);
37 const Standard_GUID& SALOMEDSImpl_AttributeTableOfReal::GetID()
39 static Standard_GUID SALOMEDSImpl_AttributeTableOfRealID ("128371A1-8F52-11d6-A8A3-0001021E8C7F");
40 return SALOMEDSImpl_AttributeTableOfRealID;
43 Handle(SALOMEDSImpl_AttributeTableOfReal) SALOMEDSImpl_AttributeTableOfReal::Set(const TDF_Label& label)
45 Handle(SALOMEDSImpl_AttributeTableOfReal) anAttr;
46 if (!label.FindAttribute(SALOMEDSImpl_AttributeTableOfReal::GetID(),anAttr)) {
47 anAttr = new SALOMEDSImpl_AttributeTableOfReal();
48 label.AddAttribute(anAttr);
53 SALOMEDSImpl_AttributeTableOfReal::SALOMEDSImpl_AttributeTableOfReal()
54 :SALOMEDSImpl_GenericAttribute("AttributeTableOfReal")
56 myRows = new TColStd_HSequenceOfExtendedString();
57 myCols = new TColStd_HSequenceOfExtendedString();
62 void SALOMEDSImpl_AttributeTableOfReal::SetNbColumns(const Standard_Integer theNbColumns)
67 TColStd_DataMapOfIntegerReal aMap;
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());
81 myNbColumns = theNbColumns;
83 while (myCols->Length() < myNbColumns) { // append empty columns titles
84 myCols->Append(TCollection_ExtendedString(""));
87 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
90 void SALOMEDSImpl_AttributeTableOfReal::SetTitle(const TCollection_ExtendedString& theTitle)
96 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
99 TCollection_ExtendedString SALOMEDSImpl_AttributeTableOfReal::GetTitle() const
104 void SALOMEDSImpl_AttributeTableOfReal::SetRowData(const Standard_Integer theRow,
105 const Handle(TColStd_HSequenceOfReal)& theData)
108 if(theData->Length() > myNbColumns) SetNbColumns(theData->Length());
112 while (myRows->Length() < theRow) { // append new row titles
113 myRows->Append(TCollection_ExtendedString(""));
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));
121 if(theRow > myNbRows) myNbRows = theRow;
123 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
126 Handle(TColStd_HSequenceOfReal) SALOMEDSImpl_AttributeTableOfReal::GetRowData(const Standard_Integer theRow)
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));
141 void SALOMEDSImpl_AttributeTableOfReal::SetRowTitle(const Standard_Integer theRow,
142 const TCollection_ExtendedString& theTitle)
146 TCollection_ExtendedString aTitle(theTitle), aUnit = GetRowUnit(theRow);
147 if(aUnit.Length()>0) {
151 myRows->SetValue(theRow, aTitle);
153 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
156 void SALOMEDSImpl_AttributeTableOfReal::SetRowUnit(const Standard_Integer theRow,
157 const TCollection_ExtendedString& theUnit)
161 TCollection_ExtendedString aTitle = GetRowTitle(theRow);
165 myRows->SetValue(theRow, aTitle);
167 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
170 void SALOMEDSImpl_AttributeTableOfReal::SetRowUnits(const Handle(TColStd_HSequenceOfExtendedString)& theUnits)
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));
176 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
179 Handle(TColStd_HSequenceOfExtendedString) SALOMEDSImpl_AttributeTableOfReal::GetRowUnits()
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)));
187 void SALOMEDSImpl_AttributeTableOfReal::SetRowTitles(const Handle(TColStd_HSequenceOfExtendedString)& theTitles)
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));
193 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
196 Handle(TColStd_HSequenceOfExtendedString) SALOMEDSImpl_AttributeTableOfReal::GetRowTitles()
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)));
205 TCollection_ExtendedString SALOMEDSImpl_AttributeTableOfReal::GetRowTitle(const Standard_Integer theRow) const
207 return getTitle(myRows->Value(theRow));
211 TCollection_ExtendedString SALOMEDSImpl_AttributeTableOfReal::GetRowUnit(const Standard_Integer theRow) const
213 return getUnit(myRows->Value(theRow));
216 void SALOMEDSImpl_AttributeTableOfReal::SetColumnData(const Standard_Integer theColumn,
217 const Handle(TColStd_HSequenceOfReal)& theData)
220 if(theColumn > myNbColumns) SetNbColumns(theColumn);
224 Standard_Integer i, aLength = theData->Length();
225 for(i = 1; i <= aLength; i++) {
226 myTable.Bind(myNbColumns*(i-1)+theColumn, theData->Value(i));
229 if(aLength > myNbRows) {
231 while (myRows->Length() < myNbRows) { // append empty row titles
232 myRows->Append(TCollection_ExtendedString(""));
236 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
240 Handle(TColStd_HSequenceOfReal) SALOMEDSImpl_AttributeTableOfReal::GetColumnData(const Standard_Integer theColumn)
242 Handle(TColStd_HSequenceOfReal) aSeq = new TColStd_HSequenceOfReal;
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));
256 void SALOMEDSImpl_AttributeTableOfReal::SetColumnTitle(const Standard_Integer theColumn,
257 const TCollection_ExtendedString& theTitle)
261 while(myCols->Length() < theColumn) myCols->Append(TCollection_ExtendedString(""));
262 myCols->SetValue(theColumn,theTitle);
264 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
267 TCollection_ExtendedString SALOMEDSImpl_AttributeTableOfReal::GetColumnTitle(const Standard_Integer theColumn) const
269 if(myCols.IsNull()) return "";
270 if(myCols->Length() < theColumn) return "";
271 return myCols->Value(theColumn);
274 void SALOMEDSImpl_AttributeTableOfReal::SetColumnTitles(const Handle(TColStd_HSequenceOfExtendedString)& theTitles)
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));
280 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
283 Handle(TColStd_HSequenceOfExtendedString) SALOMEDSImpl_AttributeTableOfReal::GetColumnTitles()
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));
291 Standard_Integer SALOMEDSImpl_AttributeTableOfReal::GetNbRows() const
296 Standard_Integer SALOMEDSImpl_AttributeTableOfReal::GetNbColumns() const
301 void SALOMEDSImpl_AttributeTableOfReal::PutValue(const Standard_Real theValue,
302 const Standard_Integer theRow,
303 const Standard_Integer theColumn)
306 if(theColumn > myNbColumns) SetNbColumns(theColumn);
308 Standard_Integer anIndex = (theRow-1)*myNbColumns + theColumn;
309 myTable.Bind(anIndex, theValue);
311 if(theRow > myNbRows) {
312 while (myRows->Length() < theRow) { // append empty row titles
313 myRows->Append(TCollection_ExtendedString(""));
318 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
321 Standard_Boolean SALOMEDSImpl_AttributeTableOfReal::HasValue(const Standard_Integer theRow,
322 const Standard_Integer theColumn)
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);
330 Standard_Real SALOMEDSImpl_AttributeTableOfReal::GetValue(const Standard_Integer theRow,
331 const Standard_Integer theColumn)
333 if(theRow > myNbRows || theRow < 1) Standard_Failure::Raise("Invalid cell index");
334 if(theColumn > myNbColumns || theColumn < 1) Standard_Failure::Raise("Invalid cell index");
336 Standard_Integer anIndex = (theRow-1)*myNbColumns + theColumn;
337 if(myTable.IsBound(anIndex)) return myTable.Find(anIndex);
339 Standard_Failure::Raise("Invalid cell index");
343 const Standard_GUID& SALOMEDSImpl_AttributeTableOfReal::ID() const
348 void SALOMEDSImpl_AttributeTableOfReal::Restore(const Handle(TDF_Attribute)& with)
350 Standard_Integer anIndex;
351 Handle(SALOMEDSImpl_AttributeTableOfReal) aTable = Handle(SALOMEDSImpl_AttributeTableOfReal)::DownCast(with);
357 myTable = aTable->myTable;
358 myNbRows = aTable->myNbRows;
359 myNbColumns = aTable->myNbColumns;
360 myTitle = aTable->myTitle;
362 for(anIndex = 1; anIndex <= aTable->GetNbRows();anIndex++)
363 myRows->Append(aTable->GetRowTitle(anIndex));
365 for(anIndex = 1; anIndex <= aTable->GetNbColumns(); anIndex++)
366 myCols->Append(aTable->GetColumnTitle(anIndex));
369 Handle(TDF_Attribute) SALOMEDSImpl_AttributeTableOfReal::NewEmpty() const
371 return new SALOMEDSImpl_AttributeTableOfReal();
374 void SALOMEDSImpl_AttributeTableOfReal::Paste(const Handle(TDF_Attribute)& into,
375 const Handle(TDF_RelocationTable)&) const
377 Standard_Integer anIndex;
378 Handle(SALOMEDSImpl_AttributeTableOfReal) aTable = Handle(SALOMEDSImpl_AttributeTableOfReal)::DownCast(into);
380 aTable->myTable.Clear();
381 aTable->myCols->Clear();
382 aTable->myRows->Clear();
384 aTable->myTable = myTable;
385 aTable->myTitle = myTitle;
386 aTable->myNbRows = myNbRows;
387 aTable->myNbColumns = myNbColumns;
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));
396 Handle_TColStd_HSequenceOfInteger SALOMEDSImpl_AttributeTableOfReal::GetSetRowIndices(const Standard_Integer theRow)
398 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
400 Standard_Integer i, aShift = myNbColumns*(theRow-1);
401 for(i = 1; i <= myNbColumns; i++) {
402 if(myTable.IsBound(aShift + i)) aSeq->Append(i);
408 Handle_TColStd_HSequenceOfInteger SALOMEDSImpl_AttributeTableOfReal::GetSetColumnIndices(const Standard_Integer theColumn)
410 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
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);
423 void SALOMEDSImpl_AttributeTableOfReal::ConvertToString(ostrstream& theStream)
429 l = myTitle.Length();
430 theStream << l << "\n";
432 theStream << myTitle.Value(i) << "\n";
435 theStream << myNbRows << "\n";
438 for(i=1; i<=myNbRows; i++) {
439 l = myRows->Value(i).Length();
440 theStream << l << "\n";
442 theStream << myRows->Value(i).Value(j) << "\n";
446 theStream << myNbColumns << "\n";
449 for(i=1; i<=myNbColumns; i++) {
450 l = myCols->Value(i).Length();
451 theStream << l << "\n";
453 theStream << myCols->Value(i).Value(j) << "\n";
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";
472 bool SALOMEDSImpl_AttributeTableOfReal::RestoreFromString(istrstream& theStream)
478 Standard_ExtCharacter anExtChar;
479 TCollection_ExtendedString aStr;
484 myTitle = TCollection_ExtendedString(l, 0);
485 for(i=1; i<=l; i++) {
486 theStream >> anExtChar;
487 myTitle.SetValue(i, anExtChar);
491 theStream >> myNbRows;
495 for(i=1; i<=myNbRows; i++) {
497 aStr = TCollection_ExtendedString(l,0);
498 for(j=1; j<=l; j++) {
499 theStream >> anExtChar;
500 aStr.SetValue(j, anExtChar);
502 myRows->Append(aStr);
506 theStream >> myNbColumns;
510 for(i=1; i<=myNbColumns; i++) {
512 aStr = TCollection_ExtendedString(l,0);
513 for(j=1; j<=l; j++) {
514 theStream >> anExtChar;
515 aStr.SetValue(j, anExtChar);
517 myCols->Append(aStr);
520 //Restore the table values
523 for(i=1; i<=l; i++) {
524 Standard_Integer aKey;
525 Standard_Real aValue;
528 myTable.Bind(aKey, aValue);
534 TCollection_AsciiString SALOMEDSImpl_AttributeTableOfReal::Save()
537 ConvertToString(ostr);
538 TCollection_AsciiString aString((char*)ostr.rdbuf()->str());
542 void SALOMEDSImpl_AttributeTableOfReal::Load(const TCollection_AsciiString& value)
544 istrstream aStream(value.ToCString(), strlen(value.ToCString()));
545 RestoreFromString(aStream);