2 // File : SALOMEDS_TableOfRealAttribute.cxx
3 // Created : Tue Oct 8 10:13:30 2002
4 // Author : Michael Ponikarov
7 // Copyright : Open CASCADE
8 // Modified by: Sergey Ruin
11 #include <SALOMEDS_TableOfRealAttribute.ixx>
12 #include <Standard_Failure.hxx>
13 #include <TColStd_DataMapIteratorOfDataMapOfIntegerReal.hxx>
18 const Standard_GUID& SALOMEDS_TableOfRealAttribute::GetID()
20 static Standard_GUID SALOMEDS_TableOfRealAttributeID ("128371A1-8F52-11d6-A8A3-0001021E8C7F");
21 return SALOMEDS_TableOfRealAttributeID;
24 Handle(SALOMEDS_TableOfRealAttribute) SALOMEDS_TableOfRealAttribute::Set(const TDF_Label& label)
26 Handle(SALOMEDS_TableOfRealAttribute) anAttr;
27 if (!label.FindAttribute(SALOMEDS_TableOfRealAttribute::GetID(),anAttr)) {
28 anAttr = new SALOMEDS_TableOfRealAttribute();
29 label.AddAttribute(anAttr);
34 SALOMEDS_TableOfRealAttribute::SALOMEDS_TableOfRealAttribute() {
35 myRows = new TColStd_HSequenceOfExtendedString();
36 myCols = new TColStd_HSequenceOfExtendedString();
41 void SALOMEDS_TableOfRealAttribute::SetNbColumns(const Standard_Integer theNbColumns)
45 TColStd_DataMapOfIntegerReal aMap;
49 TColStd_DataMapIteratorOfDataMapOfIntegerReal anIterator(aMap);
50 for(; anIterator.More(); anIterator.Next()) {
51 int aRow = (int)(anIterator.Key()/myNbColumns) + 1;
52 int aCol = (int)(anIterator.Key() - myNbColumns*(aRow-1));
53 if(aCol == 0) { aCol = myNbColumns; aRow--; }
54 if(aCol > theNbColumns) continue;
55 int aKey = (aRow-1)*theNbColumns+aCol;
56 myTable.Bind(aKey, anIterator.Value());
59 myNbColumns = theNbColumns;
61 while (myCols->Length() < myNbColumns) { // append empty columns titles
62 myCols->Append(TCollection_ExtendedString(""));
66 void SALOMEDS_TableOfRealAttribute::SetTitle(const TCollection_ExtendedString& theTitle) {
71 TCollection_ExtendedString SALOMEDS_TableOfRealAttribute::GetTitle() const {
75 void SALOMEDS_TableOfRealAttribute::SetRowData(const Standard_Integer theRow,
76 const Handle(TColStd_HSequenceOfReal)& theData)
78 if(theData->Length() > myNbColumns) SetNbColumns(theData->Length());
82 while (myRows->Length() < theRow) { // append new row titles
83 myRows->Append(TCollection_ExtendedString(""));
86 Standard_Integer i, aShift = (theRow-1)*myNbColumns, aLength = theData->Length();
87 for(i = 1; i <= aLength; i++) {
88 myTable.Bind(aShift + i, theData->Value(i));
91 if(theRow > myNbRows) myNbRows = theRow;
94 Handle(TColStd_HSequenceOfReal) SALOMEDS_TableOfRealAttribute::GetRowData(const Standard_Integer theRow)
96 Handle(TColStd_HSequenceOfReal) aSeq = new TColStd_HSequenceOfReal();
97 Standard_Integer i, aShift = (theRow-1)*myNbColumns;
98 for(i = 1; i <= myNbColumns; i++) {
99 if(myTable.IsBound(aShift+i))
100 aSeq->Append(myTable.Find(aShift+i));
108 void SALOMEDS_TableOfRealAttribute::SetRowTitle(const Standard_Integer theRow,
109 const TCollection_ExtendedString& theTitle) {
111 myRows->SetValue(theRow,theTitle);
114 TCollection_ExtendedString SALOMEDS_TableOfRealAttribute::GetRowTitle(const Standard_Integer theRow) const {
115 return myRows->Value(theRow);
119 void SALOMEDS_TableOfRealAttribute::SetColumnData(const Standard_Integer theColumn,
120 const Handle(TColStd_HSequenceOfReal)& theData)
122 if(theColumn > myNbColumns) SetNbColumns(theColumn);
126 Standard_Integer i, aLength = theData->Length();
127 for(i = 1; i <= aLength; i++) {
128 myTable.Bind(myNbColumns*(i-1)+theColumn, theData->Value(i));
131 if(aLength > myNbRows) {
133 while (myRows->Length() < myNbRows) { // append empty row titles
134 myRows->Append(TCollection_ExtendedString(""));
140 Handle(TColStd_HSequenceOfReal) SALOMEDS_TableOfRealAttribute::GetColumnData(const Standard_Integer theColumn)
142 Handle(TColStd_HSequenceOfReal) aSeq = new TColStd_HSequenceOfReal;
144 Standard_Integer i, anIndex;
145 for(i = 1; i <= myNbRows; i++) {
146 anIndex = myNbColumns*(i-1) + theColumn;
147 if(myTable.IsBound(anIndex))
148 aSeq->Append(myTable.Find(anIndex));
156 void SALOMEDS_TableOfRealAttribute::SetColumnTitle(const Standard_Integer theColumn,
157 const TCollection_ExtendedString& theTitle) {
159 while(myCols->Length() < theColumn) myCols->Append(TCollection_ExtendedString(""));
160 myCols->SetValue(theColumn,theTitle);
163 TCollection_ExtendedString SALOMEDS_TableOfRealAttribute::GetColumnTitle(const Standard_Integer theColumn) const {
164 if(myCols.IsNull()) return "";
165 if(myCols->Length() < theColumn) return "";
166 return myCols->Value(theColumn);
170 Standard_Integer SALOMEDS_TableOfRealAttribute::GetNbRows() const
175 Standard_Integer SALOMEDS_TableOfRealAttribute::GetNbColumns() const
180 void SALOMEDS_TableOfRealAttribute::PutValue(const Standard_Real theValue,
181 const Standard_Integer theRow,
182 const Standard_Integer theColumn)
184 if(theColumn > myNbColumns) SetNbColumns(theColumn);
186 Standard_Integer anIndex = (theRow-1)*myNbColumns + theColumn;
187 myTable.Bind(anIndex, theValue);
189 if(theRow > myNbRows) {
190 while (myRows->Length() < theRow) { // append empty row titles
191 myRows->Append(TCollection_ExtendedString(""));
197 Standard_Boolean SALOMEDS_TableOfRealAttribute::HasValue(const Standard_Integer theRow,
198 const Standard_Integer theColumn)
200 Standard_Integer anIndex = (theRow-1)*myNbColumns + theColumn;
201 return myTable.IsBound(anIndex);
204 Standard_Real SALOMEDS_TableOfRealAttribute::GetValue(const Standard_Integer theRow,
205 const Standard_Integer theColumn)
207 Standard_Integer anIndex = (theRow-1)*myNbColumns + theColumn;
208 if(myTable.IsBound(anIndex)) return myTable.Find(anIndex);
210 Standard_Failure::Raise("Invalid cell index");
214 const Standard_GUID& SALOMEDS_TableOfRealAttribute::ID() const
219 void SALOMEDS_TableOfRealAttribute::Restore(const Handle(TDF_Attribute)& with)
221 Standard_Integer anIndex;
222 Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(with);
228 myTable = aTable->myTable;
229 myNbRows = aTable->myNbRows;
230 myNbColumns = aTable->myNbColumns;
231 myTitle = aTable->myTitle;
233 for(anIndex = 1; anIndex <= aTable->GetNbRows();anIndex++) {
234 SetRowTitle(anIndex,aTable->GetRowTitle(anIndex));
236 for(anIndex = 1; anIndex <= aTable->GetNbColumns(); anIndex++)
237 SetColumnTitle(anIndex, aTable->GetColumnTitle(anIndex));
240 Handle(TDF_Attribute) SALOMEDS_TableOfRealAttribute::NewEmpty() const
242 return new SALOMEDS_TableOfRealAttribute();
245 void SALOMEDS_TableOfRealAttribute::Paste(const Handle(TDF_Attribute)& into,
246 const Handle(TDF_RelocationTable)&) const
248 Standard_Integer anIndex;
249 Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(into);
251 aTable->myTable.Clear();
252 aTable->myCols->Clear();
253 aTable->myRows->Clear();
255 aTable->myTable = myTable;
256 aTable->myTitle = myTitle;
257 aTable->myNbRows = myNbRows;
258 aTable->myNbColumns = myNbColumns;
260 for(anIndex = 1; anIndex <= GetNbRows();anIndex++) {
261 aTable->SetRowTitle(anIndex,GetRowTitle(anIndex));
263 for(anIndex = 1; anIndex <= GetNbColumns(); anIndex++)
264 aTable->SetColumnTitle(anIndex, GetColumnTitle(anIndex));
268 Handle_TColStd_HSequenceOfInteger SALOMEDS_TableOfRealAttribute::GetSetRowIndices(const Standard_Integer theRow)
270 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
272 Standard_Integer i, aShift = myNbColumns*(theRow-1);
273 for(i = 1; i <= myNbColumns; i++) {
274 if(myTable.IsBound(aShift + i)) aSeq->Append(i);
280 Handle_TColStd_HSequenceOfInteger SALOMEDS_TableOfRealAttribute::GetSetColumnIndices(const Standard_Integer theColumn)
282 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
284 Standard_Integer i, anIndex;
285 for(i = 1; i <= myNbRows; i++) {
286 anIndex = myNbColumns*(i-1)+theColumn;
287 if(myTable.IsBound(anIndex)) aSeq->Append(i);
295 void SALOMEDS_TableOfRealAttribute::ConvertToString(ostrstream& theStream)
301 l = myTitle.Length();
302 theStream << l << "\n";
304 theStream << myTitle.Value(i) << "\n";
307 theStream << myNbRows << "\n";
310 for(i=1; i<=myNbRows; i++) {
311 l = myRows->Value(i).Length();
312 theStream << l << "\n";
314 theStream << myRows->Value(i).Value(j) << "\n";
318 theStream << myNbColumns << "\n";
321 for(i=1; i<=myNbColumns; i++) {
322 l = myCols->Value(i).Length();
323 theStream << l << "\n";
325 theStream << myCols->Value(i).Value(j) << "\n";
328 //Store the table values
329 l = myTable.Extent();
330 theStream << l << "\n";
331 char *aBuffer = new char[128];
332 TColStd_DataMapIteratorOfDataMapOfIntegerReal anIterator(myTable);
333 for(; anIterator.More(); anIterator.Next()) {
334 theStream << anIterator.Key() << "\n";
335 sprintf(aBuffer, "%.64e", anIterator.Value());
336 theStream << aBuffer << "\n";
344 bool SALOMEDS_TableOfRealAttribute::RestoreFromString(istrstream& theStream)
350 Standard_ExtCharacter anExtChar;
351 TCollection_ExtendedString aStr;
356 myTitle = TCollection_ExtendedString(l, 0);
357 for(i=1; i<=l; i++) {
358 theStream >> anExtChar;
359 myTitle.SetValue(i, anExtChar);
363 theStream >> myNbRows;
367 for(i=1; i<=myNbRows; i++) {
369 aStr = TCollection_ExtendedString(l,0);
370 for(j=1; j<=l; j++) {
371 theStream >> anExtChar;
372 aStr.SetValue(j, anExtChar);
374 myRows->Append(aStr);
378 theStream >> myNbColumns;
382 for(i=1; i<=myNbColumns; i++) {
384 aStr = TCollection_ExtendedString(l,0);
385 for(j=1; j<=l; j++) {
386 theStream >> anExtChar;
387 aStr.SetValue(j, anExtChar);
389 myCols->Append(aStr);
392 //Restore the table values
395 for(i=1; i<=l; i++) {
396 Standard_Integer aKey;
397 Standard_Real aValue;
400 myTable.Bind(aKey, aValue);