Salome HOME
Remove unused variables
[modules/visu.git] / src / VISU_I / VISU_Table_i.cc
1 //  VISU OBJECT : interactive object for VISU entities implementation
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //  File   : VISU_Table_i.cc
24 //  Author : Vadim SANDLER
25 //  Module : VISU
26
27 #include "VISU_Table_i.hh"
28
29 #include "QAD_Application.h"
30 #include "QAD_Desktop.h"
31 #include "QAD_Study.h"
32
33 #include "VISU_CutLines_i.hh"
34 #include "VISU_Result_i.hh"
35
36 #include <memory>       
37 #include <fstream>      
38 #include <strstream>
39
40 #include <qfileinfo.h>
41 #include <qstring.h>
42 #include <qfile.h>
43
44 using namespace std;
45
46 #ifdef _DEBUG_
47 static int MYDEBUG = 0;
48 #else
49 static int MYDEBUG = 0;
50 #endif
51 //----------------------------------------------------------------
52 //                      Table Object
53 //----------------------------------------------------------------
54 int VISU::Table_i::myNbPresent = 0;
55 const string VISU::Table_i::myComment  = "TABLE";
56 /*! 
57   Generate unique name
58 */
59 const char* VISU::Table_i::GenerateName() 
60
61   return VISU::GenerateName( "Table", ++myNbPresent ); 
62 }
63 /*!
64   Gets comment string
65 */
66 const char* VISU::Table_i::GetComment() const 
67
68   return myComment.c_str(); 
69 }
70 /*!
71   Constructor
72 */
73 VISU::Table_i::Table_i( SALOMEDS::Study_ptr theStudy, const char* theObjectEntry )
74      : PrsObject_i(theStudy)
75 {
76   mySObj = SALOMEDS::SObject::_duplicate((theStudy->FindObjectID(theObjectEntry)));
77   myOrientation = VISU::Table::HORIZONTAL;
78 }
79 /*!
80   Destructor
81 */
82 VISU::Table_i::~Table_i()
83 {
84   MESSAGE("Table_i::~Table_i");
85 }
86 /*!
87   Gets number of rows in table
88 */
89 CORBA::Long VISU::Table_i::GetNbRows()
90 {
91   SALOMEDS::SObject_var SO = mySObj;
92   SALOMEDS::StudyBuilder_var Builder = myStudy->NewBuilder();
93   if ( !SO->_is_nil() ) {
94     SALOMEDS::GenericAttribute_var        anAttr;
95     if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfInteger" ) ) {
96       SALOMEDS::AttributeTableOfInteger_var anInt =  SALOMEDS::AttributeTableOfInteger::_narrow( anAttr );
97         return anInt->GetNbRows();
98     }
99     else if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfReal" ) ) {
100       SALOMEDS::AttributeTableOfReal_var aReal =  SALOMEDS::AttributeTableOfReal::_narrow( anAttr );
101       return aReal->GetNbRows();
102     }
103   }
104   return 0;
105 }
106 /*!
107   Gets number of columns in table
108 */
109 CORBA::Long VISU::Table_i::GetNbColumns()
110 {
111   SALOMEDS::SObject_var SO = mySObj;
112   SALOMEDS::StudyBuilder_var Builder = myStudy->NewBuilder();
113   if ( !SO->_is_nil() ) {
114     SALOMEDS::GenericAttribute_var        anAttr;
115     if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfInteger" ) ) {
116       SALOMEDS::AttributeTableOfInteger_var anInt =  SALOMEDS::AttributeTableOfInteger::_narrow( anAttr );
117       return anInt->GetNbColumns();
118     }
119     else if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfReal" ) ) {
120       SALOMEDS::AttributeTableOfReal_var aReal =  SALOMEDS::AttributeTableOfReal::_narrow( anAttr );
121       return aReal->GetNbColumns();
122     }
123   }
124   return 0;
125 }
126 /*!
127   Creates table object
128 */
129 VISU::Storable* VISU::Table_i::Create()
130 {
131   // generate name ... 
132   myName = GetTableTitle(); 
133
134   // mpv (PAL 5357): if name attribute already exist at this label, use it as name of table
135   if ( myName == "" )
136     if ( !mySObj->_is_nil() ) {
137       CutLines_i* pCutLines = NULL;
138       CORBA::Object_var anObj = SObjectToObject(mySObj);
139       if(!CORBA::is_nil(anObj)){
140         VISU::CutLines_var aCutLines = VISU::CutLines::_narrow(anObj);
141           if(!aCutLines->_is_nil())
142             pCutLines = dynamic_cast<CutLines_i*>(GetServant(aCutLines).in());
143         }
144       if (!pCutLines)
145         if (mySObj->GetName()) myName = mySObj->GetName();
146     }
147
148   if ( myName == "" )
149     myName = GenerateName();
150   // ... and build the object
151   return Build( false );
152 }
153 /*!
154   Builds presentation of table
155 */
156 VISU::Storable* VISU::Table_i::Build( int theRestoring ) 
157 {
158   
159   // look for reference SObject with table attribute
160   SALOMEDS::SObject_var SO = mySObj;
161
162   if ( !SO->_is_nil() ) {
163     CutLines_i* pCutLines = NULL;
164     CORBA::Object_var anObj = SObjectToObject(SO);
165     if(!CORBA::is_nil(anObj)){
166       VISU::CutLines_var aCutLines = VISU::CutLines::_narrow(anObj);
167       if(!aCutLines->_is_nil())
168         pCutLines = dynamic_cast<CutLines_i*>(GetServant(aCutLines).in());
169     }
170     SALOMEDS::StudyBuilder_var Builder = myStudy->NewBuilder();
171     SALOMEDS::GenericAttribute_var anAttr;
172     // look for component
173     if ( !theRestoring ) {
174         SALOMEDS::SComponent_var SComponent = VISU::FindOrCreateVisuComponent( myStudy );
175         // create SObject and set attributes
176         QString aComment;
177         aComment.sprintf("myComment=%s;myType=%d",GetComment(),VISU::TTABLE);
178         string anEntry = CreateAttributes( myStudy, 
179                                           SO->GetID(),//SComponent->GetID(),
180                                           "",
181                                           GetID(),
182                                           GetName(),
183                                           "",
184                                           aComment.latin1(), 
185                                           pCutLines );
186         // create SObject referenced to real table object
187         mySObj = SALOMEDS::SObject::_duplicate(myStudy->FindObjectID( anEntry.c_str() ));
188         if(pCutLines) {
189           pCutLines->BuildTableOfReal(mySObj);
190         }
191         // mpv (PAL5357): reference attributes are unnecessary now
192         //SALOMEDS::SObject_var refSO = Builder->NewObject( mySObj );
193         //Builder->Addreference( refSO, SO );
194       }
195     return this;
196   }
197   return NULL;
198 }
199 /*!
200   Restores table object from stream
201 */
202 VISU::Storable* VISU::Table_i::Restore( const Storable::TRestoringMap& theMap, SALOMEDS::SObject_ptr SO)
203 {
204   if(MYDEBUG) MESSAGE(GetComment());
205   myName = (const char*)(VISU::Storable::FindValue(theMap,"myName"));
206   myTitle = (const char*)(VISU::Storable::FindValue(theMap,"myTitle"));
207   myOrientation = ( VISU::Table::Orientation )( VISU::Storable::FindValue(theMap,"myOrientation").toInt() );
208   mySObj = SALOMEDS::SObject::_duplicate(SO);
209   return Build( true );
210 }
211 /*!
212   Flushes table data into stream
213 */
214 void VISU::Table_i::ToStream( std::ostringstream& theStr )
215 {
216   Storable::DataToStream( theStr, "myName",        myName.c_str() );
217   Storable::DataToStream( theStr, "myTitle",       myTitle.c_str() );
218   Storable::DataToStream( theStr, "myOrientation", myOrientation );
219 }
220 /*!
221   Called from engine to restore table from the file
222 */
223 VISU::Storable* VISU::Table_i::Restore(SALOMEDS::SObject_ptr theSObject, 
224                                        const string& thePrefix, const Storable::TRestoringMap& theMap)
225 {
226   SALOMEDS::Study_var aStudy = theSObject->GetStudy();
227   VISU::Table_i* pResent = new VISU::Table_i( aStudy, "" );
228   return pResent->Restore( theMap, theSObject);
229 }
230 /*!
231   Gets title for the original table object
232 */
233 const char* VISU::Table_i::GetTableTitle()
234 {
235   SALOMEDS::SObject_var SO = mySObj;
236   SALOMEDS::StudyBuilder_var Builder = myStudy->NewBuilder();
237   SALOMEDS::GenericAttribute_var        anAttr;
238   SALOMEDS::AttributeTableOfInteger_var anInt;
239   SALOMEDS::AttributeTableOfReal_var    aReal;
240   if ( !SO->_is_nil() ) { 
241     if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfInteger" ) ) {
242       anInt = SALOMEDS::AttributeTableOfInteger::_narrow( anAttr );
243       return anInt->GetTitle();
244     }
245     else if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfReal" ) ) {
246       aReal = SALOMEDS::AttributeTableOfReal::_narrow( anAttr );
247       return aReal->GetTitle();
248     }
249   }
250   return ""; 
251 }
252
253 void VISU::Table_i::RemoveFromStudy(){
254   VISU::RemoveFromStudy(mySObj,false);
255 }
256
257 //----------------------------------------------------------------
258 //                      Curve Object
259 //----------------------------------------------------------------
260 /*!
261   Restores table object from the stream [ static ]
262 */
263 static VISU::Table_i* GetTable( SALOMEDS::Study_ptr theStudy, SALOMEDS::SObject_ptr theSO ) {
264   CORBA::Object_var anObject = VISU::SObjectToObject( theSO );
265   if( !CORBA::is_nil( anObject ) ) {
266     CORBA::Object_ptr aTable = VISU::Table::_narrow( anObject );
267     if( !CORBA::is_nil( aTable ) )
268       return dynamic_cast<VISU::Table_i*>(VISU::GetServant(aTable).in());
269   }
270   return NULL;
271 }
272
273 int VISU::Curve_i::myNbPresent = 0;
274 const string VISU::Curve_i::myComment  = "CURVE";
275 /*! 
276   Generate unique name
277 */
278 const char* VISU::Curve_i::GenerateName() 
279
280   return VISU::GenerateName( "Curve", ++myNbPresent ); 
281 }
282 /*!
283   Gets comment string
284 */
285 const char* VISU::Curve_i::GetComment() const 
286
287   return myComment.c_str(); 
288 }
289 /*!
290   Constructor
291   NB : theHRow, theVRow are the indexes of rows in the Table object and numbered from the 1 to GetNbRows()
292 */
293 VISU::Curve_i::Curve_i(SALOMEDS::Study_ptr theStudy, Table_i* theTable, CORBA::Long theHRow, CORBA::Long theVRow )
294      : PrsObject_i(theStudy), myTable( theTable ), myHRow( theHRow ), myVRow( theVRow )
295 {
296   myAuto = true;
297   myLine = VISU::Curve::SOLIDLINE;
298   myLineWidth = 0;
299   myMarker = VISU::Curve::CIRCLE;
300   myColor.R = 0.0; myColor.G = 0.0; myColor.B = 0.0;
301 }
302 /*!
303   Destructor
304 */
305 VISU::Curve_i::~Curve_i()
306 {
307   MESSAGE("Curve_i::~Curve_i");
308 }
309 /*!
310   Creates curve object
311 */
312 VISU::Storable* VISU::Curve_i::Create()
313 {
314   // generate name ... 
315   myName = GetVerTitle(); 
316   if ( myName == "" )
317     myName = GenerateName();
318   // ... and build the object
319   return Build( false );
320 }
321 /*!
322   Builds presentation of curve
323 */
324 VISU::Storable* VISU::Curve_i::Build(int theRestoring ) 
325 {
326   if ( myTable != NULL ) {
327     // getting table SObject by it's entry
328     int nbRows = myTable->GetNbRows();
329     if ( myHRow > 0 && myHRow <= nbRows && myVRow > 0 && myVRow <= nbRows ) {
330       if ( !theRestoring ) {
331         // look for component
332         SALOMEDS::SComponent_var SComponent = VISU::FindOrCreateVisuComponent( myStudy );
333         // create SObject and set attributes
334         QString aComment;
335         aComment.sprintf("myComment=%s;myType=%d",GetComment(),VISU::TCURVE);
336         string anEntry = CreateAttributes( myStudy, 
337                                           myTable->GetObjectEntry(),
338                                           "",
339                                           GetID(),
340                                           GetName(),
341                                           "",
342                                           aComment.latin1(),
343                                           true );
344         // create SObject referenced to real table object
345         mySObj = SALOMEDS::SObject::_duplicate(myStudy->FindObjectID(anEntry.c_str()));
346       }
347       return this;
348     }
349   }
350   return NULL;
351 }
352
353 /*!
354   Returns CORBA::True if curve refers to valid table data
355 */
356 CORBA::Boolean VISU::Curve_i::IsValid()
357 {
358   // getting table SObject by it's entry
359   SALOMEDS::SObject_var SO = myStudy->FindObjectID(myTable->GetObjectEntry());
360   SALOMEDS::StudyBuilder_var Builder = myStudy->NewBuilder();
361   SALOMEDS::GenericAttribute_var        anAttr;
362   SALOMEDS::AttributeTableOfInteger_var anInt;
363   SALOMEDS::AttributeTableOfReal_var    aReal;
364   if ( !SO->_is_nil() ) { 
365     if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfInteger" ) ) {
366       anInt = SALOMEDS::AttributeTableOfInteger::_narrow( anAttr );
367       if ( myHRow > 0 && myHRow <= anInt->GetNbRows() && myVRow > 0 && myVRow <= anInt->GetNbRows() ) {
368         return true;
369       }
370     }
371     else if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfReal" ) ) {
372       aReal = SALOMEDS::AttributeTableOfReal::_narrow( anAttr );
373       if ( myHRow > 0 && myHRow <= aReal->GetNbRows() && myVRow > 0 && myVRow <= aReal->GetNbRows() ) {
374         return true;
375       }
376     }
377   }
378   return false;
379 }
380 /*!
381   Returns hor.axis title
382 */
383 string VISU::Curve_i::GetHorTitle()
384 {
385   string title;
386   // getting table SObject by it's entry
387   SALOMEDS::SObject_var SO = myStudy->FindObjectID(myTable->GetObjectEntry());
388   SALOMEDS::StudyBuilder_var Builder = myStudy->NewBuilder();
389   SALOMEDS::GenericAttribute_var        anAttr;
390   SALOMEDS::AttributeTableOfInteger_var anInt;
391   SALOMEDS::AttributeTableOfReal_var    aReal;
392   if ( !SO->_is_nil() ) { 
393     if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfInteger" ) ) {
394       anInt = SALOMEDS::AttributeTableOfInteger::_narrow( anAttr );
395       SALOMEDS::StringSeq_var rowTitles = anInt->GetRowTitles();
396       if ( rowTitles->length() > 0 && myHRow > 0 && myHRow <= anInt->GetNbRows() ) {
397         title = rowTitles[ myHRow-1 ];
398       }
399     }
400     else if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfReal" ) ) {
401       aReal = SALOMEDS::AttributeTableOfReal::_narrow( anAttr );
402       SALOMEDS::StringSeq_var rowTitles = aReal->GetRowTitles();
403       if ( rowTitles->length() > 0 && myHRow > 0 && myHRow <= aReal->GetNbRows() ) {
404         title = rowTitles[ myHRow-1 ];
405       }
406     }
407   }
408   return title;
409 }
410 /*!
411   Returns ver.axis title
412 */
413 string VISU::Curve_i::GetVerTitle()
414 {
415   string title;
416   // getting table SObject by it's entry
417   SALOMEDS::SObject_var SO = myStudy->FindObjectID(myTable->GetObjectEntry());
418   SALOMEDS::StudyBuilder_var Builder = myStudy->NewBuilder();
419   SALOMEDS::GenericAttribute_var        anAttr;
420   SALOMEDS::AttributeTableOfInteger_var anInt;
421   SALOMEDS::AttributeTableOfReal_var    aReal;
422   if ( !SO->_is_nil() ) { 
423     if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfInteger" ) ) {
424       anInt = SALOMEDS::AttributeTableOfInteger::_narrow( anAttr );
425       SALOMEDS::StringSeq_var rowTitles = anInt->GetRowTitles();
426       if ( rowTitles->length() > 0 && myVRow > 0 && myVRow <= anInt->GetNbRows() )
427         title = rowTitles[ myVRow-1 ];
428     }
429     else if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfReal" ) ) {
430       aReal = SALOMEDS::AttributeTableOfReal::_narrow( anAttr );
431       SALOMEDS::StringSeq_var rowTitles = aReal->GetRowTitles();
432       if ( rowTitles->length() > 0 && myVRow > 0 && myVRow <= aReal->GetNbRows() )
433         title = rowTitles[ myVRow-1 ];
434     }
435   }
436   return title;
437 }
438 /*!
439   Returns hor.axis units
440 */
441 string VISU::Curve_i::GetHorUnits()
442 {
443   string units;
444   // getting table SObject by it's entry
445   SALOMEDS::SObject_var SO = myStudy->FindObjectID(myTable->GetObjectEntry());
446   SALOMEDS::StudyBuilder_var Builder = myStudy->NewBuilder();
447   SALOMEDS::GenericAttribute_var        anAttr;
448   SALOMEDS::AttributeTableOfInteger_var anInt;
449   SALOMEDS::AttributeTableOfReal_var    aReal;
450   if ( !SO->_is_nil()  ) { 
451     if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfInteger" ) ) {
452       anInt = SALOMEDS::AttributeTableOfInteger::_narrow( anAttr );
453       SALOMEDS::StringSeq_var rowUnits = anInt->GetRowUnits();
454       if ( rowUnits->length() > 0 && myHRow > 0 && myHRow <= anInt->GetNbRows() )
455         units = rowUnits[ myHRow-1 ];
456     }
457     else if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfReal" ) ) {
458       aReal = SALOMEDS::AttributeTableOfReal::_narrow( anAttr );
459       SALOMEDS::StringSeq_var rowUnits = aReal->GetRowUnits();
460       if ( rowUnits->length() > 0 && myHRow > 0 && myHRow <= aReal->GetNbRows() )
461         units = rowUnits[ myHRow-1 ];
462     }
463   }
464   return units;
465 }
466 /*!
467   Returns ver.axis units
468 */
469 string VISU::Curve_i::GetVerUnits()
470 {
471   string units;
472   // getting table SObject by it's entry
473   SALOMEDS::SObject_var SO = myStudy->FindObjectID(myTable->GetObjectEntry());
474   SALOMEDS::StudyBuilder_var Builder = myStudy->NewBuilder();
475   SALOMEDS::GenericAttribute_var        anAttr;
476   SALOMEDS::AttributeTableOfInteger_var anInt;
477   SALOMEDS::AttributeTableOfReal_var    aReal;
478   if ( !SO->_is_nil() ) { 
479     if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfInteger" ) ) {
480       anInt = SALOMEDS::AttributeTableOfInteger::_narrow( anAttr );
481       SALOMEDS::StringSeq_var rowUnits = anInt->GetRowUnits();
482       if ( rowUnits->length() > 0 && myVRow > 0 && myVRow <= anInt->GetNbRows() )
483         units = rowUnits[ myVRow-1];
484     }
485     else if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfReal" ) ) {
486       aReal = SALOMEDS::AttributeTableOfReal::_narrow( anAttr );
487       SALOMEDS::StringSeq_var rowUnits = aReal->GetRowUnits();
488       if ( rowUnits->length() > 0 && myVRow > 0 && myVRow <= aReal->GetNbRows() )
489         units = rowUnits[ myVRow-1 ];
490     }
491   }
492   return units;
493 }
494 /*!
495   Gets curve data
496 */
497 int VISU::Curve_i::GetData( double*& theHorList, double*& theVerList )
498 {
499   theHorList = 0; theVerList = 0;
500   // getting table SObject by it's entry
501   SALOMEDS::SObject_var SO = myStudy->FindObjectID(myTable->GetObjectEntry());
502   SALOMEDS::StudyBuilder_var Builder = myStudy->NewBuilder();
503   SALOMEDS::GenericAttribute_var        anAttr;
504   SALOMEDS::AttributeTableOfInteger_var anInt;
505   SALOMEDS::AttributeTableOfReal_var    aReal;
506   if ( !SO->_is_nil() ) { 
507     if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfInteger" ) ) {
508       anInt = SALOMEDS::AttributeTableOfInteger::_narrow( anAttr );
509       int nbCols = anInt->GetNbColumns() ; 
510       if ( nbCols > 0 && myHRow > 0 && myHRow <= anInt->GetNbRows() && myVRow > 0 && myVRow <= anInt->GetNbRows() ) {
511         int nbPoints = 0;
512         for ( int j = 1; j <= nbCols; j++ ) {
513           if ( anInt->HasValue( myHRow, j ) && anInt->HasValue( myVRow, j ) )
514             nbPoints++;
515         }
516         if ( nbPoints > 0 ) {
517           theHorList = new double[ nbPoints ];
518           theVerList = new double[ nbPoints ];
519           int k = 0;
520           for ( int j = 1; j <= nbCols; j++ ) {
521             if ( anInt->HasValue( myHRow, j ) && anInt->HasValue( myVRow, j ) ) {
522               theHorList[k] = anInt->GetValue( myHRow, j );
523               theVerList[k] = anInt->GetValue( myVRow, j );
524               k++;
525             }
526           }
527         }
528         return nbPoints;
529       }
530     }
531     else if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfReal" ) ) {
532       aReal = SALOMEDS::AttributeTableOfReal::_narrow( anAttr );
533       int nbCols = aReal->GetNbColumns() ; 
534       if ( nbCols > 0 && myHRow > 0 && myHRow <= aReal->GetNbRows() && myVRow > 0 && myVRow <= aReal->GetNbRows() ) {
535         int nbPoints = 0;
536         for ( int j = 1; j <= nbCols; j++ ) {
537           if ( aReal->HasValue( myHRow, j ) && aReal->HasValue( myVRow, j ) )
538             nbPoints++;
539         }
540         if ( nbPoints > 0 ) {
541           theHorList = new double[ nbPoints ];
542           theVerList = new double[ nbPoints ];
543           int k = 0;
544           for ( int j = 1; j <= nbCols; j++ ) {
545             if ( aReal->HasValue( myHRow, j ) && aReal->HasValue( myVRow, j ) ) {
546               theHorList[k] = aReal->GetValue( myHRow, j );
547               theVerList[k] = aReal->GetValue( myVRow, j );
548               k++;
549             }
550           }
551         }
552         return nbPoints;
553       }
554     }
555   }
556   return 0;
557 }
558 /*!
559   Creates curve Plot2d presentation object
560 */
561 Plot2d_Curve* VISU::Curve_i::CreatePresentation()
562 {
563   Plot2d_Curve* crv = new Plot2d_Curve();
564   crv->setHorTitle( GetHorTitle().c_str() );
565   string tlt = GetTitle(); 
566   if ( tlt.length() <= 0 )
567     tlt = GetVerTitle();
568   //crv->setVerTitle( strdup( GetVerTitle().c_str() ) );
569   //crv->setVerTitle( strdup( GetName() ) );
570   crv->setVerTitle( tlt.c_str() );
571   crv->setHorUnits( GetHorUnits().c_str() );
572   crv->setVerUnits( GetVerUnits().c_str() );
573   double* xList = 0;
574   double* yList = 0;
575   int     nbPoints = GetData( xList, yList );
576   if ( nbPoints > 0 && xList && yList ) {
577     crv->setData( xList, yList, nbPoints );
578   }
579   //cout << "********** Number of points: " << nbPoints <<endl;
580   //for ( int i =0 ; i < nbPoints; i++ ) {
581   //  cout << i<<"\t"<<xList[i] << "\t"<< yList[i] << endl;
582   //}
583   crv->setLine( (Plot2d_Curve::LineType)GetLine(), GetLineWidth() );
584   crv->setMarker( (Plot2d_Curve::MarkerType)GetMarker() ); 
585   SALOMEDS::Color color = GetColor();
586   crv->setColor( QColor( (int)(color.R*255.), (int)(color.G*255.), (int)(color.B*255.) ) );
587   crv->setAutoAssign( IsAuto() );
588   crv->setIO(new SALOME_InteractiveObject(mySObj->GetID(),"VISU",GetName()));
589   if ( myTable )
590     crv->setTableIO(new SALOME_InteractiveObject(myTable->GetObjectEntry(),"VISU",myTable->GetName()));
591   return crv;
592 }
593 /*!
594   Restores curve object from stream
595 */
596 VISU::Storable* VISU::Curve_i::Restore( const Storable::TRestoringMap& theMap, SALOMEDS::SObject_ptr theSO)
597 {
598   if(MYDEBUG) MESSAGE(GetComment());
599   mySObj = SALOMEDS::SObject::_duplicate(theSO);
600   myName = VISU::Storable::FindValue(theMap,"myName").latin1();
601   myHRow = VISU::Storable::FindValue(theMap,"myHRow").toInt();
602   myVRow = VISU::Storable::FindValue(theMap,"myVRow").toInt();
603   myColor.R = VISU::Storable::FindValue(theMap,"myColor.R").toDouble();
604   myColor.G = VISU::Storable::FindValue(theMap,"myColor.G").toDouble();
605   myColor.B = VISU::Storable::FindValue(theMap,"myColor.B").toDouble();
606   myMarker = ( VISU::Curve::MarkerType )( VISU::Storable::FindValue(theMap,"myMarker").toInt() );
607   myLine = ( VISU::Curve::LineType )( VISU::Storable::FindValue(theMap,"myLine").toInt() );
608   myLineWidth = VISU::Storable::FindValue(theMap,"myLineWidth").toInt();
609   myAuto = VISU::Storable::FindValue(theMap,"myAuto").toInt();
610   return Build( true );
611 }
612 /*!
613   Flushes curve data into stream
614 */
615 void VISU::Curve_i::ToStream( std::ostringstream& theStr )
616 {
617   Storable::DataToStream( theStr, "myName",      myName.c_str() );
618   Storable::DataToStream( theStr, "myHRow",      myHRow );
619   Storable::DataToStream( theStr, "myVRow",      myVRow );
620   Storable::DataToStream( theStr, "myColor.R",   myColor.R );
621   Storable::DataToStream( theStr, "myColor.G",   myColor.G );
622   Storable::DataToStream( theStr, "myColor.B",   myColor.B );
623   Storable::DataToStream( theStr, "myMarker",    myMarker );
624   Storable::DataToStream( theStr, "myLine",      myLine );
625   Storable::DataToStream( theStr, "myLineWidth", myLineWidth );
626   Storable::DataToStream( theStr, "myAuto",      myAuto );
627 }
628 /*!
629   Gets reference table's entry
630 */
631 const char* VISU::Curve_i::GetTableID() { 
632   return myTable->GetObjectEntry();
633 }
634 /*!
635   Called from engine to restore curve from the file
636 */
637 VISU::Storable* VISU::Curve_i::Restore(SALOMEDS::SObject_ptr theSObject, 
638                                        const string& thePrefix, const Storable::TRestoringMap& theMap)
639 {
640   SALOMEDS::Study_var aStudy = theSObject->GetStudy();
641   VISU::Table_i* pTable = GetTable(aStudy, theSObject->GetFather());
642   if( pTable != NULL ) {
643     VISU::Curve_i* pResent = new VISU::Curve_i( aStudy, pTable, 0, 0 );
644     return pResent->Restore( theMap, theSObject);
645   }
646   return NULL;
647 }
648
649 void VISU::Curve_i::RemoveFromStudy(){
650   VISU::RemoveFromStudy(mySObj,false);
651 }
652
653 //----------------------------------------------------------------
654 //                      Container Object
655 //----------------------------------------------------------------
656 int VISU::Container_i::myNbPresent = 0;
657 const string VISU::Container_i::myComment  = "CONTAINER";
658 /*! 
659   Generate unique name
660 */
661 const char* VISU::Container_i::GenerateName() 
662
663   return VISU::GenerateName( "Plot2DView", ++myNbPresent ); 
664 }
665 /*!
666   Gets comment string
667 */
668 const char* VISU::Container_i::GetComment() const 
669
670   return myComment.c_str(); 
671 }
672 /*!
673   Constructor
674 */
675 VISU::Container_i::Container_i( SALOMEDS::Study_ptr theStudy )
676      : PrsObject_i( theStudy )
677 {
678 }
679 /*!
680   Destructor
681 */
682 VISU::Container_i::~Container_i()
683 {
684   MESSAGE("Container_i::~Container_i");
685   myCurves.clear();
686 }
687 /*!
688   Inserts curve into the container
689 */
690 void VISU::Container_i::AddCurve( Curve_ptr theCurve )
691 {
692   if ( myStudy->_is_nil() )
693     return;
694   SALOMEDS::SObject_var mySO = myStudy->FindObjectID( GetEntry() );
695   if ( mySO->_is_nil() )
696     return;
697   PortableServer::POA_ptr aPOA = GetPOA();
698   Curve_i* pCurve = dynamic_cast<Curve_i*>( aPOA->reference_to_servant( theCurve ) );
699   if( pCurve ) {
700     QString entry = pCurve->GetEntry();
701     SALOMEDS::SObject_var SO = myStudy->FindObjectID( entry.latin1() );
702     if ( !SO->_is_nil() && myCurves.find( entry ) == myCurves.end() ) {
703       myCurves.append( entry );
704       SALOMEDS::StudyBuilder_var Builder = myStudy->NewBuilder();
705       SALOMEDS::SObject_var newSO = Builder->NewObject( mySO );
706       Builder->Addreference( newSO, SO );
707     }
708   }
709 }
710 /*!
711   Removes curve from the container
712 */
713 void VISU::Container_i::RemoveCurve( Curve_ptr theCurve )
714 {
715   if ( myStudy->_is_nil() )
716     return;
717   SALOMEDS::SObject_var mySO = myStudy->FindObjectID( GetEntry() );
718   if ( mySO->_is_nil() )
719     return;
720   PortableServer::POA_ptr aPOA = GetPOA();
721   Curve_i* pCurve = dynamic_cast<Curve_i*>( aPOA->reference_to_servant( theCurve ) );
722   if( pCurve ) {
723     QString entry = pCurve->GetEntry();
724     if ( myCurves.find( entry ) != myCurves.end() ) {
725       // found !!!
726       myCurves.remove( entry );
727       SALOMEDS::StudyBuilder_var Builder = myStudy->NewBuilder();
728       SALOMEDS::ChildIterator_var CI = myStudy->NewChildIterator( mySO );
729       for ( ; CI->More(); CI->Next() ) {
730         SALOMEDS::SObject_var childSO = CI->Value();
731         SALOMEDS::SObject_var refSO;
732         if ( childSO->ReferencedObject( refSO ) && !refSO->_is_nil() && entry == QString( refSO->GetID() ) ) {
733           Builder->RemoveObject( childSO );
734         }
735       }
736     }
737   }
738 }
739 /*!
740   Gets number of curves in the container
741 */
742 CORBA::Long VISU::Container_i::GetNbCurves()
743 {
744   Update();
745   return myCurves.count();
746 }
747 /*!
748   Clears container
749 */
750 void VISU::Container_i::Clear()
751 {
752   if ( myStudy->_is_nil() )
753     return;
754   SALOMEDS::SObject_var mySO = myStudy->FindObjectID( GetEntry() );
755   if ( mySO->_is_nil() )
756     return;
757   QStringList toDelete;
758   SALOMEDS::ChildIterator_var CI = myStudy->NewChildIterator( mySO );
759   for ( ; CI->More(); CI->Next() ) {
760     toDelete.append( CI->Value()->GetID() );
761   }
762   SALOMEDS::StudyBuilder_var Builder = myStudy->NewBuilder();
763   for ( int i = 0; i < toDelete.count(); i++ ) {
764     SALOMEDS::SObject_var SO = myStudy->FindObjectID( toDelete[i].latin1() );
765     Builder->RemoveObject( SO );
766   }
767   myCurves.clear();
768 }
769 /*!
770   Creates container object
771 */
772 VISU::Storable* VISU::Container_i::Create()
773 {
774   // generate name ... 
775   myName = GenerateName();
776   // ... and build the object
777   return Build( false );
778 }
779 /*!
780   Builds presentation of container
781 */
782 VISU::Storable* VISU::Container_i::Build( int theRestoring ) 
783 {
784   if ( !theRestoring ) {
785     // looking for component
786     SALOMEDS::SComponent_var SComponent = VISU::FindOrCreateVisuComponent( myStudy );
787     // create SObject and set attributes
788     QString aComment;
789     aComment.sprintf("myComment=%s;myType=%d",GetComment(),VISU::TCONTAINER);
790     string anEntry = CreateAttributes( myStudy, 
791                                        SComponent->GetID(),
792                                        "",
793                                        GetID(),
794                                        GetName(),
795                                        "",
796                                        aComment.latin1(),
797                                        true );
798     mySObj = SALOMEDS::SObject::_duplicate(myStudy->FindObjectID(anEntry.c_str()));
799   }
800   return this;
801 }
802 /*!
803   Updates presentation of container
804 */
805 void VISU::Container_i::Update()
806 {
807   if ( myStudy->_is_nil() )
808     return;
809   SALOMEDS::StudyBuilder_var Builder = myStudy->NewBuilder();
810   SALOMEDS::SObject_var mySO = myStudy->FindObjectID( GetEntry() );
811   SALOMEDS::GenericAttribute_var anAttr;
812   if ( !mySO->_is_nil() ) {
813     QStringList toDelete;
814     int i;
815     for ( i = 0; i < myCurves.count(); i++ ) {
816       SALOMEDS::SObject_var SO = myStudy->FindObjectID( myCurves[i].latin1() );
817       if ( !SO->_is_nil() && Builder->FindAttribute( SO, anAttr, "AttributeIOR" ) ) {
818         // if real Curve Object still exists 
819         SALOMEDS::ChildIterator_var CI = myStudy->NewChildIterator( mySO );
820         bool bFound = false;
821         for ( ; CI->More(); CI->Next() ) {
822           SALOMEDS::SObject_var childSO = CI->Value();
823           SALOMEDS::SObject_var refSO;
824           if ( childSO->ReferencedObject( refSO ) && !refSO->_is_nil() && myCurves[i] == QString( refSO->GetID() ) ) {
825             bFound = true; break;
826           }
827         }
828         if (! bFound ) {
829           // create SObject referenced to real curve object if is not yet added
830           SALOMEDS::SObject_var newSO = Builder->NewObject( mySO );
831           Builder->Addreference( newSO, SO );
832         }
833       }
834       else {
835         // real Curve Object doesn't exist (might be removed)
836         toDelete.append( myCurves[i] );
837       }
838     }
839     for ( i = 0; i < toDelete.count(); i++ ) { 
840       myCurves.remove( toDelete[i] );
841     }
842     toDelete.clear();
843     SALOMEDS::ChildIterator_var CI = myStudy->NewChildIterator( mySO );
844     for ( ; CI->More(); CI->Next() ) {
845       SALOMEDS::SObject_var childSO = CI->Value();
846       SALOMEDS::SObject_var refSO;
847       if ( childSO->ReferencedObject( refSO ) && ( refSO->_is_nil() || !Builder->FindAttribute( refSO, anAttr, "AttributeIOR" ) ||
848                                                    myCurves.find( refSO->GetID() ) == myCurves.end() ) ) {
849         toDelete.append( childSO->GetID() );
850       }
851     }
852     for ( i = 0; i < toDelete.count(); i++ ) { 
853       SALOMEDS::ChildIterator_var CI = myStudy->NewChildIterator( mySO );
854       for ( ; CI->More(); CI->Next() ) {
855         SALOMEDS::SObject_var childSO = CI->Value();
856         if ( toDelete[i] == CI->Value()->GetID() ) {
857           Builder->RemoveObject( childSO );
858         }
859       }
860     }
861   }
862 }
863 /*!
864   Gets curve from container by index
865   NB : curves are numbered from 1
866 */
867 VISU::Curve_i* VISU::Container_i::GetCurve( CORBA::Long theIndex )
868 {
869   if ( theIndex > 0 && theIndex <= myCurves.count()  ) {
870     SALOMEDS::StudyBuilder_var Builder = myStudy->NewBuilder();
871     SALOMEDS::GenericAttribute_var anAttr;
872     SALOMEDS::SObject_var SO = myStudy->FindObjectID( myCurves[  theIndex-1 ].latin1() );
873     CORBA::Object_var anObject = VISU::SObjectToObject( SO );
874     if( !CORBA::is_nil( anObject ) ) {
875       // if real Curve Object exists 
876       CORBA::Object_ptr aCurve = VISU::Curve::_narrow( anObject );
877       if( !CORBA::is_nil( aCurve ) )
878       return dynamic_cast<VISU::Curve_i*>(VISU::GetServant(aCurve).in());
879     }
880   }
881   return NULL;
882 }
883 /*!
884   Restores container data from the stream
885 */
886 VISU::Storable* VISU::Container_i::Restore( const Storable::TRestoringMap& theMap, SALOMEDS::SObject_ptr SO )
887 {
888   if(MYDEBUG) MESSAGE(GetComment());
889   mySObj = SALOMEDS::SObject::_duplicate(SO);
890   myName = VISU::Storable::FindValue( theMap, "myName" ).latin1(); 
891   QString val = VISU::Storable::FindValue( theMap, "myCurves" );
892   myCurves = QStringList::split( QString( "*" ), val, false );
893   return Build( true );
894 }
895 /*!
896   Flushes container data into the stream
897 */
898 void VISU::Container_i::ToStream( std::ostringstream& theStr )
899 {
900   Storable::DataToStream( theStr, "myName",   myName.c_str() );
901   Storable::DataToStream( theStr, "myCurves", myCurves.join( QString( "*" ) ) );
902 //  theStr<<" myName "<<myName;
903 //  theStr<<" myCurves "<<myCurves.join( QString( "*" ) ).latin1()<<"* ";
904 }
905 /*!
906   Called from engine to restore container from the file
907 */
908 VISU::Storable* VISU::Container_i::Restore(SALOMEDS::SObject_ptr theSObject, 
909                                            const string& thePrefix, const Storable::TRestoringMap& theMap)
910 {
911   SALOMEDS::Study_var aStudy = theSObject->GetStudy();
912   VISU::Container_i* pResent = new VISU::Container_i( aStudy );
913   return pResent->Restore( theMap, theSObject );
914 }
915
916 void VISU::Container_i::RemoveFromStudy(){
917   VISU::RemoveFromStudy(mySObj,false);
918 }
919
920 //-------------------------------------------------------------
921 //             Implementation of reading from file
922 //-------------------------------------------------------------
923 typedef string TValue;
924 typedef vector<TValue> TValues;
925
926 struct TRow{
927   string myTitle;
928   string myUnit;
929   TValues myValues;
930 };
931
932 typedef vector<TRow> TRows;
933
934 struct TTable2D {
935   string myTitle;
936   vector<string> myColumnUnits;
937   vector<string> myColumnTitles;
938   TRows myRows;
939   int Check(){
940     if(myRows.empty()) return 0;
941     int iEnd = myRows[0].myValues.size();
942     if(iEnd == 0) return 0;
943     if(myColumnTitles.size() != iEnd) myColumnTitles.resize(iEnd);
944     if(myColumnUnits.size() != iEnd) myColumnUnits.resize(iEnd);
945     int jEnd = myRows.size();
946     for(int j = 0; j < jEnd; j++)
947       if(myRows[j].myValues.size() != iEnd) 
948         return 0;
949     return 1;
950   }
951   void getColumns(TTable2D& theTable2D) const {
952     TRows& aRows = theTable2D.myRows;
953     aRows.clear();
954     if(myRows.empty()) return;
955     int jEnd = myRows.size();
956     //Define Titles & Units
957     theTable2D.myColumnTitles.resize(jEnd);
958     theTable2D.myColumnUnits.resize(jEnd);
959     for(int j = 0; j < jEnd; j++){
960       theTable2D.myColumnTitles[j] = myRows[j].myTitle;
961       theTable2D.myColumnUnits[j] = myRows[j].myUnit;
962     }
963     //Define Rows
964     int iEnd = myRows[0].myValues.size();
965     for(int i = 0; i < iEnd; i++){
966       TRow aNewRow;
967       aNewRow.myTitle = myColumnTitles[i];
968       aNewRow.myUnit = myColumnUnits[i];
969       aNewRow.myValues.resize(jEnd);
970       for(int j = 0; j < jEnd; j++){
971         aNewRow.myValues[j] = myRows[j].myValues[i];
972       }
973       aRows.push_back(aNewRow);
974     }
975   }
976 };
977
978 typedef vector<TTable2D> TTableCont;
979
980 int getLine(ifstream& theStmIn, QString& theString){
981   char tmp;
982   ostrstream aStrOut;
983   while(theStmIn.get(tmp)){
984     aStrOut<<tmp;
985     if(tmp == '\n') break;
986   }
987   aStrOut<<ends;
988   theString = aStrOut.str();
989   return !theStmIn.eof();
990 }
991
992 void ImportTables(const char* theFileName, TTableCont& theTableCont){
993   ifstream aStmIn;
994   QFileInfo aFileInfo( theFileName );
995   if( !aFileInfo.isFile() || !aFileInfo.isReadable() || !aFileInfo.size() )
996     return;
997   aStmIn.open( theFileName );
998   QString aTmp;
999   do {
1000     // find beginning of table (tables are separated by empty lines)
1001     while( getLine( aStmIn, aTmp ) && aTmp.stripWhiteSpace() == "");
1002     TTable2D aTable2D;
1003     if(MYDEBUG) cout << "New table is found" << endl;
1004     while( !aStmIn.eof() && aTmp.stripWhiteSpace() != "" ){
1005       QString data = aTmp.stripWhiteSpace();
1006       QString cmt = "";
1007       QString keyword = "";
1008       // split string to data and comment (comment starts from '#' symbol)
1009       int index = aTmp.find( "#" );
1010       if ( index >= 0 ) {
1011         data = aTmp.left( index ).stripWhiteSpace();
1012         cmt = aTmp.mid( index+1 ).stripWhiteSpace();
1013       }
1014       // if comment is not empty, try to get keyword from it (separated by ':' symbol)
1015       if ( !cmt.isEmpty() ) {
1016         int index1 = cmt.find( ":" );
1017         if ( index1 >= 0 ) {
1018           QString tmpstr = cmt.left( index1 ).stripWhiteSpace();
1019           if ( tmpstr == QString( "TITLE" ) ||
1020                tmpstr == QString( "COLUMN_TITLES" ) ||
1021                tmpstr == QString( "COLUMN_UNITS" ) ||
1022                tmpstr == QString( "COMMENT" ) ) {
1023             keyword = tmpstr;
1024             cmt = cmt.mid( index1+1 ).stripWhiteSpace();
1025           }
1026         }
1027       }
1028       // if data is empty, process only comment
1029       if ( data.isEmpty() ) {
1030         // if keyword is found, try to process it
1031         // elsewise it is a simple comment, just ignore it
1032         if ( !keyword.isEmpty() ) {
1033           if ( keyword == QString( "TITLE" ) ) {
1034             QString title = cmt;
1035             if ( aTable2D.myTitle != "" )
1036               title = QString( aTable2D.myTitle.c_str() ) + QString( " " ) + title;
1037             if(MYDEBUG) cout << "...Table TITLE is: " << title.latin1() << endl;
1038             aTable2D.myTitle = title.latin1();
1039           }
1040           else if ( keyword == QString( "COLUMN_TITLES" ) ) {
1041             // comment may contain column headers
1042             QStringList aStrList = QStringList::split( "|", cmt );
1043             if(MYDEBUG) cout << "...Column TITLES are: ";
1044             for ( int i = 0; i < aStrList.count(); i++ ) {
1045               QString tmpstr = aStrList[ i ].stripWhiteSpace();
1046               if(MYDEBUG) cout << tmpstr.latin1() << " ";
1047               aTable2D.myColumnTitles.push_back( tmpstr.latin1() );
1048             }
1049             if(MYDEBUG) cout << endl;
1050           }
1051           else if ( keyword == QString( "COLUMN_UNITS" ) ) {
1052             // comment may contain column units
1053             QStringList aStrList = QStringList::split( " ", cmt );
1054             if(MYDEBUG) cout << "...Column UNITS are: ";
1055             for ( int i = 0; i < aStrList.count(); i++ ) {
1056               QString tmpstr = aStrList[ i ].stripWhiteSpace();
1057               if(MYDEBUG) cout << tmpstr.latin1() << " ";
1058               aTable2D.myColumnUnits.push_back( tmpstr.latin1() );
1059             }
1060             if(MYDEBUG) cout << endl;
1061           }
1062           else if ( keyword == QString( "COMMENT" ) ) {
1063             // keyword 'COMMENT' processing can be here
1064             // currently it is ignored
1065             if(MYDEBUG) cout << "...COMMENT: " << cmt.latin1() << endl;
1066           }
1067         }
1068         else {
1069           if(MYDEBUG) cout << "...comment: " << cmt.latin1() << endl;
1070           // simple comment processing can be here
1071           // currently it is ignored
1072         }
1073       }
1074       // if data is not empty, try to process it
1075       else {
1076         TRow aRow; 
1077         if(MYDEBUG) cout << "...New row is found: " << endl;
1078         if ( !cmt.isEmpty() ) {
1079           aRow.myTitle = cmt.latin1();
1080           if(MYDEBUG) cout << "......ROW TITLE is: " << cmt.latin1() << endl;
1081         }
1082         QStringList aValList = QStringList::split( " ", data );
1083         for ( int i = 0; i < aValList.count(); i++ ) {
1084           if ( aValList[i].stripWhiteSpace() != "" ) {
1085             TValue aVal = aValList[i].stripWhiteSpace().latin1();
1086             aRow.myValues.push_back( aVal );
1087           }
1088         }
1089         if( aRow.myValues.size() > 0 )
1090           aTable2D.myRows.push_back( aRow );
1091         // ************** OLD CODE ******************
1092         /*
1093         TValue aVal;
1094         istrstream aStream( data );
1095         aStream.precision( STRPRECISION );
1096         while( aStream >> aVal ) {
1097           aRow.myValues.push_back( aVal );
1098         }
1099         if( aRow.myValues.size() > 0 )
1100           aTable2D.myRows.push_back( aRow );
1101         */
1102         // ************** OLD CODE ******************
1103       }
1104       getLine( aStmIn, aTmp );
1105     }
1106     if( aTable2D.Check() ) {
1107       if(MYDEBUG) cout << "aTable2D is checked OK " << aTable2D.myTitle << endl;
1108       theTableCont.push_back( aTable2D );
1109     }
1110   }
1111   while( !aStmIn.eof() );
1112   aStmIn.close();
1113   if(MYDEBUG) cout << "After close" << endl;
1114 }
1115
1116 SALOMEDS::SObject_var VISU::ImportTables(const char* theFileName, SALOMEDS::Study_ptr theStudy){
1117   TTableCont aTableCont;
1118   ImportTables(theFileName,aTableCont);
1119   if(aTableCont.empty()) return SALOMEDS::SObject::_nil();
1120   SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
1121   SALOMEDS::SComponent_var theSComponent = VISU::FindOrCreateVisuComponent(theStudy);
1122   SALOMEDS::SObject_var aFileObject = aStudyBuilder->NewObject(theSComponent);
1123   SALOMEDS::GenericAttribute_var anAttr = 
1124     aStudyBuilder->FindOrCreateAttribute(aFileObject, "AttributeName");
1125   SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
1126   QFileInfo aFileInfo(theFileName);
1127   aName->SetValue(aFileInfo.fileName().latin1());
1128   for(int i = 0, iEnd = aTableCont.size(); i < iEnd; i++){
1129     const TTable2D& aTable2D = aTableCont[i];
1130     SALOMEDS::SObject_var aRealObject = aStudyBuilder->NewObject(aFileObject);
1131     anAttr = aStudyBuilder->FindOrCreateAttribute(aRealObject, "AttributeName");
1132     aName = SALOMEDS::AttributeName::_narrow(anAttr);
1133     if(MYDEBUG) cout<<"aTable2D.myTitle = "<<aTable2D.myTitle<<endl;
1134     if(aTable2D.myTitle != "")
1135       aName->SetValue(aTable2D.myTitle.c_str());
1136     else{
1137       QString aNewName;
1138       aNewName.sprintf("Table:%d",i);
1139       aName->SetValue(aNewName.latin1());
1140     }
1141     anAttr = aStudyBuilder->FindOrCreateAttribute(aRealObject, "AttributeTableOfReal");
1142     SALOMEDS::AttributeTableOfReal_var aTableOfReal = SALOMEDS::AttributeTableOfReal::_narrow(anAttr);
1143     aTableOfReal->SetTitle(aTable2D.myTitle.c_str());
1144     TTable2D aNewTable2D;
1145     aTable2D.getColumns(aNewTable2D);
1146     int kEnd = aNewTable2D.myRows[0].myValues.size();
1147     aTableOfReal->SetNbColumns(kEnd);
1148     for(int j = 0, jEnd = aNewTable2D.myRows.size(); j < jEnd; j++){
1149       if(MYDEBUG) cout<<"j = "<<j<<"; kEnd = "<<kEnd<<endl;
1150
1151       for(int k = 0; k < kEnd; k++){
1152         QString aVal = aNewTable2D.myRows[j].myValues[k].c_str();
1153         bool anIsOk = false;
1154         double aValue = aVal.toDouble(&anIsOk);
1155         if(anIsOk && !aVal.contains("NAN",false) && !aVal.contains("INF",false))
1156           aTableOfReal->PutValue(aValue,j+1,k+1);
1157       }
1158
1159       aTableOfReal->SetRowTitle(j+1,aNewTable2D.myRows[j].myTitle.c_str());
1160       aTableOfReal->SetRowUnit(j+1,aNewTable2D.myRows[j].myUnit.c_str());
1161     }
1162     for(int k = 0; k < kEnd; k++)
1163       aTableOfReal->SetColumnTitle(k+1,aNewTable2D.myColumnTitles[k].c_str());
1164   }
1165   return aFileObject;
1166 }
1167
1168 template<class TTableAttr> bool ExportTableToFile(const TTableAttr& aTabAttr,  
1169                                                   const char* theFileName)
1170 {
1171   if (!CORBA::is_nil(aTabAttr)) {
1172     QFile aFile(theFileName);  
1173     aFile.open(IO_WriteOnly);  
1174
1175     /* extract the tabe info and write it into file */ 
1176     
1177     QString aTitle(aTabAttr->GetTitle()); /*Table title*/ 
1178     int aRowsNb = aTabAttr->GetNbRows();  
1179     int aColNb  = aTabAttr->GetNbColumns(); 
1180     
1181     SALOMEDS::StringSeq_var aRowTitles = aTabAttr->GetRowTitles(); 
1182     QString anAbscissTitle(aRowTitles[0]); /*Absciss row title (X coord)*/ 
1183     anAbscissTitle.stripWhiteSpace();       
1184
1185     SALOMEDS::StringSeq_var aRowUnits = aTabAttr->GetRowUnits(); 
1186     QString anAbscissUnit(aRowUnits[0]); 
1187     anAbscissUnit.stripWhiteSpace();     
1188     
1189     SALOMEDS::StringSeq_var aColumnTitles = aTabAttr->GetColumnTitles(); 
1190     if (aRowsNb > 2 && aTitle.length() )  aTitle = aTitle + " - ";
1191
1192     QString aLine;                      
1193     for (int i = 2; i <= aRowsNb; i++ ) 
1194       {                                 
1195         /* TITLE */                     
1196         QString anOrdinate(aRowTitles[i-1]), aTail; 
1197         anOrdinate.stripWhiteSpace();                      
1198
1199         aLine = "#TITLE: " + aTitle +                              
1200           ((anOrdinate.length())?  anOrdinate : 
1201                                   (aRowsNb>2)? aTail.sprintf("%d",i-1) : aTail.sprintf("") ) + "\n"; 
1202         aFile.writeBlock(aLine, aLine.length() );                               
1203                                                                                     
1204         /* COLUMN_TITLES */                                                     
1205         if ( anAbscissTitle.length() || anOrdinate.length() ) {                    
1206           aLine = "#COLUMN_TITLES: " + anAbscissTitle + " | " + anOrdinate + "\n";    
1207           aFile.writeBlock(aLine, aLine.length() );                                
1208         }                                                                          
1209
1210         /* COLUMN_UNITS */                           
1211         aLine = anAbscissUnit + " " +aRowUnits[i-1]; 
1212         if (!aLine.stripWhiteSpace().isEmpty()) {    
1213           aLine = "#COLUMN_UNITS: " + aLine  + "\n"; 
1214           aFile.writeBlock(aLine, aLine.length() );  
1215         }    
1216
1217         /* CURVE COORDINATES */                      
1218         for (int j = 1; j <= aColNb; j++)            
1219           {                                          
1220             if ( aTabAttr -> HasValue(i,j) &&  aTabAttr -> HasValue(1, j)) { 
1221               aLine = aLine.sprintf("%.16g %.16g",            
1222                                     (double)(aTabAttr->GetValue(1,j)),  
1223                                     (double)(aTabAttr->GetValue(i,j)));  /* aTabAttr->GetValue(1,j) - X coord */ 
1224               if ( !aLine.stripWhiteSpace().isEmpty() ) {     
1225                 QString aColTitle(aColumnTitles[j-1]);        
1226                 if ( !aColTitle.stripWhiteSpace().isEmpty() ) 
1227                   aLine = aLine + "  #TITLE: " + aColTitle ;  
1228                 aFile.writeBlock(aLine + "\n", aLine.length() + 1); 
1229               }     
1230             }       
1231           }   
1232         aFile.writeBlock("\n", 1); 
1233       }             
1234     aFile.close();  
1235     return true;    
1236   }                 
1237   return false;
1238 }
1239  
1240 bool VISU::ExportTableToFile(SALOMEDS::SObject_ptr theTable, const char* theFileName)
1241 {
1242   //Find table
1243   SALOMEDS::GenericAttribute_var anAttr ;
1244   if (theTable->FindAttribute(anAttr, "AttributeTableOfReal"))
1245     {
1246       SALOMEDS::AttributeTableOfReal_var aTabAttr = SALOMEDS::AttributeTableOfReal ::_narrow(anAttr); 
1247       return ExportTableToFile ( aTabAttr , theFileName);
1248       
1249     }
1250   else if (theTable->FindAttribute(anAttr, "AttributeTableOfInteger")) {
1251
1252     SALOMEDS::AttributeTableOfInteger_var aTabAttr = SALOMEDS::AttributeTableOfInteger ::_narrow(anAttr); 
1253     return ExportTableToFile ( aTabAttr , theFileName);
1254     
1255   }
1256   return false;
1257 }