1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : SALOMEDSImpl_AttributeTableOfReal.cxx
21 // Author : Michael Ponikarov
24 #include <SALOMEDSImpl_AttributeTableOfReal.hxx>
25 #include <Standard_Failure.hxx>
26 #include <TColStd_DataMapIteratorOfDataMapOfIntegerReal.hxx>
27 #include <Standard_GUID.hxx>
29 #include <TColStd_HSequenceOfExtendedString.hxx>
33 IMPLEMENT_STANDARD_HANDLE( SALOMEDSImpl_AttributeTableOfReal, SALOMEDSImpl_GenericAttribute )
34 IMPLEMENT_STANDARD_RTTIEXT( SALOMEDSImpl_AttributeTableOfReal, SALOMEDSImpl_GenericAttribute )
36 #define SEPARATOR '\1'
38 static TCollection_ExtendedString getUnit(TCollection_ExtendedString theString)
40 TCollection_ExtendedString aString(theString);
41 int aPos = aString.Search(SEPARATOR);
42 if(aPos <= 0 || aPos == aString.Length() ) return TCollection_ExtendedString();
43 return aString.Split(aPos);
46 static TCollection_ExtendedString getTitle(TCollection_ExtendedString theString)
48 TCollection_ExtendedString aString(theString);
49 int aPos = aString.Search(SEPARATOR);
50 if(aPos < 1) return aString;
51 if(aPos == 1) return TCollection_ExtendedString();
52 aString.Split(aPos-1);
56 const Standard_GUID& SALOMEDSImpl_AttributeTableOfReal::GetID()
58 static Standard_GUID SALOMEDSImpl_AttributeTableOfRealID ("128371A1-8F52-11d6-A8A3-0001021E8C7F");
59 return SALOMEDSImpl_AttributeTableOfRealID;
62 Handle(SALOMEDSImpl_AttributeTableOfReal) SALOMEDSImpl_AttributeTableOfReal::Set(const TDF_Label& label)
64 Handle(SALOMEDSImpl_AttributeTableOfReal) anAttr;
65 if (!label.FindAttribute(SALOMEDSImpl_AttributeTableOfReal::GetID(),anAttr)) {
66 anAttr = new SALOMEDSImpl_AttributeTableOfReal();
67 label.AddAttribute(anAttr);
72 SALOMEDSImpl_AttributeTableOfReal::SALOMEDSImpl_AttributeTableOfReal()
73 :SALOMEDSImpl_GenericAttribute("AttributeTableOfReal")
75 myRows = new TColStd_HSequenceOfExtendedString();
76 myCols = new TColStd_HSequenceOfExtendedString();
81 void SALOMEDSImpl_AttributeTableOfReal::SetNbColumns(const Standard_Integer theNbColumns)
86 TColStd_DataMapOfIntegerReal aMap;
90 TColStd_DataMapIteratorOfDataMapOfIntegerReal anIterator(aMap);
91 for(; anIterator.More(); anIterator.Next()) {
92 int aRow = (int)(anIterator.Key()/myNbColumns) + 1;
93 int aCol = (int)(anIterator.Key() - myNbColumns*(aRow-1));
94 if(aCol == 0) { aCol = myNbColumns; aRow--; }
95 if(aCol > theNbColumns) continue;
96 int aKey = (aRow-1)*theNbColumns+aCol;
97 myTable.Bind(aKey, anIterator.Value());
100 myNbColumns = theNbColumns;
102 while (myCols->Length() < myNbColumns) { // append empty columns titles
103 myCols->Append(TCollection_ExtendedString(""));
106 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
109 void SALOMEDSImpl_AttributeTableOfReal::SetTitle(const TCollection_ExtendedString& theTitle)
115 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
118 TCollection_ExtendedString SALOMEDSImpl_AttributeTableOfReal::GetTitle() const
123 void SALOMEDSImpl_AttributeTableOfReal::SetRowData(const Standard_Integer theRow,
124 const Handle(TColStd_HSequenceOfReal)& theData)
127 if(theData->Length() > myNbColumns) SetNbColumns(theData->Length());
131 while (myRows->Length() < theRow) { // append new row titles
132 myRows->Append(TCollection_ExtendedString(""));
135 Standard_Integer i, aShift = (theRow-1)*myNbColumns, aLength = theData->Length();
136 for(i = 1; i <= aLength; i++) {
137 myTable.Bind(aShift + i, theData->Value(i));
140 if(theRow > myNbRows) myNbRows = theRow;
142 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
145 Handle(TColStd_HSequenceOfReal) SALOMEDSImpl_AttributeTableOfReal::GetRowData(const Standard_Integer theRow)
147 Handle(TColStd_HSequenceOfReal) aSeq = new TColStd_HSequenceOfReal();
148 Standard_Integer i, aShift = (theRow-1)*myNbColumns;
149 for(i = 1; i <= myNbColumns; i++) {
150 if(myTable.IsBound(aShift+i))
151 aSeq->Append(myTable.Find(aShift+i));
160 void SALOMEDSImpl_AttributeTableOfReal::SetRowTitle(const Standard_Integer theRow,
161 const TCollection_ExtendedString& theTitle)
165 TCollection_ExtendedString aTitle(theTitle), aUnit = GetRowUnit(theRow);
166 if(aUnit.Length()>0) {
170 myRows->SetValue(theRow, aTitle);
172 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
175 void SALOMEDSImpl_AttributeTableOfReal::SetRowUnit(const Standard_Integer theRow,
176 const TCollection_ExtendedString& theUnit)
180 TCollection_ExtendedString aTitle = GetRowTitle(theRow);
184 myRows->SetValue(theRow, aTitle);
186 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
189 void SALOMEDSImpl_AttributeTableOfReal::SetRowUnits(const Handle(TColStd_HSequenceOfExtendedString)& theUnits)
191 if (theUnits->Length() != GetNbRows()) Standard_Failure::Raise("Invalid number of rows");
192 int aLength = theUnits->Length(), i;
193 for(i = 1; i <= aLength; i++) SetRowUnit(i, theUnits->Value(i));
195 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
198 Handle(TColStd_HSequenceOfExtendedString) SALOMEDSImpl_AttributeTableOfReal::GetRowUnits()
200 Handle(TColStd_HSequenceOfExtendedString) aSeq = new TColStd_HSequenceOfExtendedString;
201 int aLength = myRows->Length(), i;
202 for(i=1; i<=aLength; i++) aSeq->Append(getUnit(myRows->Value(i)));
206 void SALOMEDSImpl_AttributeTableOfReal::SetRowTitles(const Handle(TColStd_HSequenceOfExtendedString)& theTitles)
208 if (theTitles->Length() != GetNbRows()) Standard_Failure::Raise("Invalid number of rows");
209 int aLength = theTitles->Length(), i;
210 for(i = 1; i <= aLength; i++) SetRowTitle(i, theTitles->Value(i));
212 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
215 Handle(TColStd_HSequenceOfExtendedString) SALOMEDSImpl_AttributeTableOfReal::GetRowTitles()
217 Handle(TColStd_HSequenceOfExtendedString) aSeq = new TColStd_HSequenceOfExtendedString;
218 int aLength = myRows->Length(), i;
219 for(i=1; i<=aLength; i++) aSeq->Append(getTitle(myRows->Value(i)));
224 TCollection_ExtendedString SALOMEDSImpl_AttributeTableOfReal::GetRowTitle(const Standard_Integer theRow) const
226 return getTitle(myRows->Value(theRow));
230 TCollection_ExtendedString SALOMEDSImpl_AttributeTableOfReal::GetRowUnit(const Standard_Integer theRow) const
232 return getUnit(myRows->Value(theRow));
235 void SALOMEDSImpl_AttributeTableOfReal::SetColumnData(const Standard_Integer theColumn,
236 const Handle(TColStd_HSequenceOfReal)& theData)
239 if(theColumn > myNbColumns) SetNbColumns(theColumn);
243 Standard_Integer i, aLength = theData->Length();
244 for(i = 1; i <= aLength; i++) {
245 myTable.Bind(myNbColumns*(i-1)+theColumn, theData->Value(i));
248 if(aLength > myNbRows) {
250 while (myRows->Length() < myNbRows) { // append empty row titles
251 myRows->Append(TCollection_ExtendedString(""));
255 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
259 Handle(TColStd_HSequenceOfReal) SALOMEDSImpl_AttributeTableOfReal::GetColumnData(const Standard_Integer theColumn)
261 Handle(TColStd_HSequenceOfReal) aSeq = new TColStd_HSequenceOfReal;
263 Standard_Integer i, anIndex;
264 for(i = 1; i <= myNbRows; i++) {
265 anIndex = myNbColumns*(i-1) + theColumn;
266 if(myTable.IsBound(anIndex))
267 aSeq->Append(myTable.Find(anIndex));
275 void SALOMEDSImpl_AttributeTableOfReal::SetColumnTitle(const Standard_Integer theColumn,
276 const TCollection_ExtendedString& theTitle)
280 while(myCols->Length() < theColumn) myCols->Append(TCollection_ExtendedString(""));
281 myCols->SetValue(theColumn,theTitle);
283 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
286 TCollection_ExtendedString SALOMEDSImpl_AttributeTableOfReal::GetColumnTitle(const Standard_Integer theColumn) const
288 if(myCols.IsNull()) return "";
289 if(myCols->Length() < theColumn) return "";
290 return myCols->Value(theColumn);
293 void SALOMEDSImpl_AttributeTableOfReal::SetColumnTitles(const Handle(TColStd_HSequenceOfExtendedString)& theTitles)
295 if (theTitles->Length() != myNbColumns) Standard_Failure::Raise("Invalid number of columns");
296 int aLength = theTitles->Length(), i;
297 for(i = 1; i <= aLength; i++) myCols->SetValue(i, theTitles->Value(i));
299 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
302 Handle(TColStd_HSequenceOfExtendedString) SALOMEDSImpl_AttributeTableOfReal::GetColumnTitles()
304 Handle(TColStd_HSequenceOfExtendedString) aSeq = new TColStd_HSequenceOfExtendedString;
305 int aLength = myCols->Length(), i;
306 for(i=1; i<=aLength; i++) aSeq->Append(myCols->Value(i));
310 Standard_Integer SALOMEDSImpl_AttributeTableOfReal::GetNbRows() const
315 Standard_Integer SALOMEDSImpl_AttributeTableOfReal::GetNbColumns() const
320 void SALOMEDSImpl_AttributeTableOfReal::PutValue(const Standard_Real theValue,
321 const Standard_Integer theRow,
322 const Standard_Integer theColumn)
325 if(theColumn > myNbColumns) SetNbColumns(theColumn);
327 Standard_Integer anIndex = (theRow-1)*myNbColumns + theColumn;
328 myTable.Bind(anIndex, theValue);
330 if(theRow > myNbRows) {
331 while (myRows->Length() < theRow) { // append empty row titles
332 myRows->Append(TCollection_ExtendedString(""));
337 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
340 Standard_Boolean SALOMEDSImpl_AttributeTableOfReal::HasValue(const Standard_Integer theRow,
341 const Standard_Integer theColumn)
343 if(theRow > myNbRows || theRow < 1) return Standard_False;
344 if(theColumn > myNbColumns || theColumn < 1) return Standard_False;
345 Standard_Integer anIndex = (theRow-1)*myNbColumns + theColumn;
346 return myTable.IsBound(anIndex);
349 Standard_Real SALOMEDSImpl_AttributeTableOfReal::GetValue(const Standard_Integer theRow,
350 const Standard_Integer theColumn)
352 if(theRow > myNbRows || theRow < 1) Standard_Failure::Raise("Invalid cell index");
353 if(theColumn > myNbColumns || theColumn < 1) Standard_Failure::Raise("Invalid cell index");
355 Standard_Integer anIndex = (theRow-1)*myNbColumns + theColumn;
356 if(myTable.IsBound(anIndex)) return myTable.Find(anIndex);
358 Standard_Failure::Raise("Invalid cell index");
362 const Standard_GUID& SALOMEDSImpl_AttributeTableOfReal::ID() const
367 void SALOMEDSImpl_AttributeTableOfReal::Restore(const Handle(TDF_Attribute)& with)
369 Standard_Integer anIndex;
370 Handle(SALOMEDSImpl_AttributeTableOfReal) aTable = Handle(SALOMEDSImpl_AttributeTableOfReal)::DownCast(with);
376 myTable = aTable->myTable;
377 myNbRows = aTable->myNbRows;
378 myNbColumns = aTable->myNbColumns;
379 myTitle = aTable->myTitle;
381 for(anIndex = 1; anIndex <= aTable->GetNbRows();anIndex++)
382 myRows->Append(aTable->GetRowTitle(anIndex));
384 for(anIndex = 1; anIndex <= aTable->GetNbColumns(); anIndex++)
385 myCols->Append(aTable->GetColumnTitle(anIndex));
388 Handle(TDF_Attribute) SALOMEDSImpl_AttributeTableOfReal::NewEmpty() const
390 return new SALOMEDSImpl_AttributeTableOfReal();
393 void SALOMEDSImpl_AttributeTableOfReal::Paste(const Handle(TDF_Attribute)& into,
394 const Handle(TDF_RelocationTable)&) const
396 Standard_Integer anIndex;
397 Handle(SALOMEDSImpl_AttributeTableOfReal) aTable = Handle(SALOMEDSImpl_AttributeTableOfReal)::DownCast(into);
399 aTable->myTable.Clear();
400 aTable->myCols->Clear();
401 aTable->myRows->Clear();
403 aTable->myTable = myTable;
404 aTable->myTitle = myTitle;
405 aTable->myNbRows = myNbRows;
406 aTable->myNbColumns = myNbColumns;
408 for(anIndex = 1; anIndex <= GetNbRows();anIndex++)
409 aTable->myRows->Append(GetRowTitle(anIndex));
410 for(anIndex = 1; anIndex <= GetNbColumns(); anIndex++)
411 aTable->myCols->Append(GetColumnTitle(anIndex));
415 Handle_TColStd_HSequenceOfInteger SALOMEDSImpl_AttributeTableOfReal::GetSetRowIndices(const Standard_Integer theRow)
417 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
419 Standard_Integer i, aShift = myNbColumns*(theRow-1);
420 for(i = 1; i <= myNbColumns; i++) {
421 if(myTable.IsBound(aShift + i)) aSeq->Append(i);
427 Handle_TColStd_HSequenceOfInteger SALOMEDSImpl_AttributeTableOfReal::GetSetColumnIndices(const Standard_Integer theColumn)
429 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
431 Standard_Integer i, anIndex;
432 for(i = 1; i <= myNbRows; i++) {
433 anIndex = myNbColumns*(i-1)+theColumn;
434 if(myTable.IsBound(anIndex)) aSeq->Append(i);
442 void SALOMEDSImpl_AttributeTableOfReal::ConvertToString(ostrstream& theStream)
448 l = myTitle.Length();
449 theStream << l << "\n";
451 theStream << myTitle.Value(i) << "\n";
454 theStream << myNbRows << "\n";
457 for(i=1; i<=myNbRows; i++) {
458 l = myRows->Value(i).Length();
459 theStream << l << "\n";
461 theStream << myRows->Value(i).Value(j) << "\n";
465 theStream << myNbColumns << "\n";
468 for(i=1; i<=myNbColumns; i++) {
469 l = myCols->Value(i).Length();
470 theStream << l << "\n";
472 theStream << myCols->Value(i).Value(j) << "\n";
475 //Store the table values
476 l = myTable.Extent();
477 theStream << l << "\n";
478 char *aBuffer = new char[128];
479 TColStd_DataMapIteratorOfDataMapOfIntegerReal anIterator(myTable);
480 for(; anIterator.More(); anIterator.Next()) {
481 theStream << anIterator.Key() << "\n";
482 sprintf(aBuffer, "%.64e", anIterator.Value());
483 theStream << aBuffer << "\n";
491 bool SALOMEDSImpl_AttributeTableOfReal::RestoreFromString(istrstream& theStream)
497 Standard_ExtCharacter anExtChar;
498 TCollection_ExtendedString aStr;
503 myTitle = TCollection_ExtendedString(l, 0);
504 for(i=1; i<=l; i++) {
505 theStream >> anExtChar;
506 myTitle.SetValue(i, anExtChar);
510 theStream >> myNbRows;
514 for(i=1; i<=myNbRows; i++) {
516 aStr = TCollection_ExtendedString(l,0);
517 for(j=1; j<=l; j++) {
518 theStream >> anExtChar;
519 aStr.SetValue(j, anExtChar);
521 myRows->Append(aStr);
525 theStream >> myNbColumns;
529 for(i=1; i<=myNbColumns; i++) {
531 aStr = TCollection_ExtendedString(l,0);
532 for(j=1; j<=l; j++) {
533 theStream >> anExtChar;
534 aStr.SetValue(j, anExtChar);
536 myCols->Append(aStr);
539 //Restore the table values
542 for(i=1; i<=l; i++) {
543 Standard_Integer aKey;
544 Standard_Real aValue;
547 myTable.Bind(aKey, aValue);
553 TCollection_AsciiString SALOMEDSImpl_AttributeTableOfReal::Save()
556 ConvertToString(ostr);
557 TCollection_AsciiString aString((char*)ostr.rdbuf()->str());
561 void SALOMEDSImpl_AttributeTableOfReal::Load(const TCollection_AsciiString& value)
563 istrstream aStream(value.ToCString(), strlen(value.ToCString()));
564 RestoreFromString(aStream);