1 // VISU OBJECT : interactive object for VISU entities implementation
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
23 // File : VISU_Table_i.cc
24 // Author : Vadim SANDLER
27 #include "VISU_Table_i.hh"
29 #include "QAD_Application.h"
30 #include "QAD_Desktop.h"
31 #include "QAD_Study.h"
33 #include "VISU_CutLines_i.hh"
39 #include <qfileinfo.h>
45 static int MYDEBUG = 0;
47 static int MYDEBUG = 0;
49 //----------------------------------------------------------------
51 //----------------------------------------------------------------
52 int VISU::Table_i::myNbPresent = 0;
53 const string VISU::Table_i::myComment = "TABLE";
57 const char* VISU::Table_i::GenerateName()
59 return VISU::GenerateName( "Table", ++myNbPresent );
64 const char* VISU::Table_i::GetComment() const
66 return myComment.c_str();
71 VISU::Table_i::Table_i( SALOMEDS::Study_ptr theStudy, const char* theObjectEntry )
72 : PrsObject_i(theStudy)
74 mySObj = SALOMEDS::SObject::_duplicate((theStudy->FindObjectID(theObjectEntry)));
75 myOrientation = VISU::Table::HORIZONTAL;
80 VISU::Table_i::~Table_i()
82 INFOS("Table_i::~Table_i");
85 Gets number of rows in table
87 CORBA::Long VISU::Table_i::GetNbRows()
89 SALOMEDS::SObject_var SO = mySObj;
90 SALOMEDS::StudyBuilder_var Builder = myStudy->NewBuilder();
91 if ( !SO->_is_nil() ) {
92 SALOMEDS::GenericAttribute_var anAttr;
93 if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfInteger" ) ) {
94 SALOMEDS::AttributeTableOfInteger_var anInt = SALOMEDS::AttributeTableOfInteger::_narrow( anAttr );
95 return anInt->GetNbRows();
97 else if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfReal" ) ) {
98 SALOMEDS::AttributeTableOfReal_var aReal = SALOMEDS::AttributeTableOfReal::_narrow( anAttr );
99 return aReal->GetNbRows();
105 Gets number of columns in table
107 CORBA::Long VISU::Table_i::GetNbColumns()
109 SALOMEDS::SObject_var SO = mySObj;
110 SALOMEDS::StudyBuilder_var Builder = myStudy->NewBuilder();
111 if ( !SO->_is_nil() ) {
112 SALOMEDS::GenericAttribute_var anAttr;
113 if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfInteger" ) ) {
114 SALOMEDS::AttributeTableOfInteger_var anInt = SALOMEDS::AttributeTableOfInteger::_narrow( anAttr );
115 return anInt->GetNbColumns();
117 else if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfReal" ) ) {
118 SALOMEDS::AttributeTableOfReal_var aReal = SALOMEDS::AttributeTableOfReal::_narrow( anAttr );
119 return aReal->GetNbColumns();
127 VISU::Storable* VISU::Table_i::Create()
130 myName = GetTableTitle();
132 myName = GenerateName();
133 // ... and build the object
134 return Build( false );
137 Builds presentation of table
139 VISU::Storable* VISU::Table_i::Build( int theRestoring )
142 // look for reference SObject with table attribute
143 SALOMEDS::SObject_var SO = mySObj;
145 if ( !SO->_is_nil() ) {
146 CutLines_i* pCutLines = NULL;
147 CORBA::Object_var anObj = SObjectToObject(SO);
148 if(!CORBA::is_nil(anObj)){
149 VISU::CutLines_var aCutLines = VISU::CutLines::_narrow(anObj);
150 if(!aCutLines->_is_nil())
151 pCutLines = dynamic_cast<CutLines_i*>(GetServant(aCutLines).in());
153 SALOMEDS::StudyBuilder_var Builder = myStudy->NewBuilder();
154 SALOMEDS::GenericAttribute_var anAttr;
155 // look for component
156 if ( !theRestoring ) {
157 SALOMEDS::SComponent_var SComponent = VISU::FindOrCreateVisuComponent( myStudy );
158 // create SObject and set attributes
160 aComment.sprintf("myComment=%s;myType=%d",GetComment(),VISU::TTABLE);
161 string anEntry = CreateAttributes( myStudy,
162 SO->GetID(),//SComponent->GetID(),
169 // create SObject referenced to real table object
170 mySObj = SALOMEDS::SObject::_duplicate(myStudy->FindObjectID( anEntry.c_str() ));
172 pCutLines->BuildTableOfReal(mySObj);
174 SALOMEDS::SObject_var refSO = Builder->NewObject( mySObj );
175 Builder->Addreference( refSO, SO );
182 Restores table object from stream
184 VISU::Storable* VISU::Table_i::Restore( const Storable::TRestoringMap& theMap, SALOMEDS::SObject_ptr SO)
185 throw( std::logic_error& )
187 if(MYDEBUG) MESSAGE(GetComment());
188 myName = (const char*)(VISU::Storable::FindValue(theMap,"myName"));
189 myTitle = (const char*)(VISU::Storable::FindValue(theMap,"myTitle"));
190 myOrientation = ( VISU::Table::Orientation )( VISU::Storable::FindValue(theMap,"myOrientation").toInt() );
191 mySObj = SALOMEDS::SObject::_duplicate(SO);
192 return Build( true );
195 Flushes table data into stream
197 void VISU::Table_i::ToStream( std::ostringstream& theStr )
199 Storable::DataToStream( theStr, "myName", myName.c_str() );
200 Storable::DataToStream( theStr, "myTitle", myTitle.c_str() );
201 Storable::DataToStream( theStr, "myOrientation", myOrientation );
204 Called from engine to restore table from the file
206 VISU::Storable* VISU::Table_i::Restore(SALOMEDS::SObject_ptr theSObject,
207 const string& thePrefix, const Storable::TRestoringMap& theMap)
208 throw(std::logic_error&)
210 SALOMEDS::Study_var aStudy = theSObject->GetStudy();
211 VISU::Table_i* pResent = new VISU::Table_i( aStudy, "" );
212 return pResent->Restore( theMap, theSObject);
215 Gets title for the original table object
217 const char* VISU::Table_i::GetTableTitle()
219 SALOMEDS::SObject_var SO = mySObj;
220 SALOMEDS::StudyBuilder_var Builder = myStudy->NewBuilder();
221 SALOMEDS::GenericAttribute_var anAttr;
222 SALOMEDS::AttributeTableOfInteger_var anInt;
223 SALOMEDS::AttributeTableOfReal_var aReal;
224 if ( !SO->_is_nil() ) {
225 if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfInteger" ) ) {
226 anInt = SALOMEDS::AttributeTableOfInteger::_narrow( anAttr );
227 return anInt->GetTitle();
229 else if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfReal" ) ) {
230 aReal = SALOMEDS::AttributeTableOfReal::_narrow( anAttr );
231 return aReal->GetTitle();
237 //----------------------------------------------------------------
239 //----------------------------------------------------------------
241 Restores table object from the stream [ static ]
243 static VISU::Table_i* GetTable( SALOMEDS::Study_ptr theStudy, SALOMEDS::SObject_ptr theSO ) {
244 CORBA::Object_var anObject = VISU::SObjectToObject( theSO );
245 if( !CORBA::is_nil( anObject ) ) {
246 CORBA::Object_ptr aTable = VISU::Table::_narrow( anObject );
247 if( !CORBA::is_nil( aTable ) )
248 return dynamic_cast<VISU::Table_i*>(VISU::GetServant(aTable).in());
253 int VISU::Curve_i::myNbPresent = 0;
254 const string VISU::Curve_i::myComment = "CURVE";
258 const char* VISU::Curve_i::GenerateName()
260 return VISU::GenerateName( "Curve", ++myNbPresent );
265 const char* VISU::Curve_i::GetComment() const
267 return myComment.c_str();
271 NB : theHRow, theVRow are the indexes of rows in the Table object and numbered from the 1 to GetNbRows()
273 VISU::Curve_i::Curve_i(SALOMEDS::Study_ptr theStudy, Table_i* theTable, CORBA::Long theHRow, CORBA::Long theVRow )
274 : PrsObject_i(theStudy), myTable( theTable ), myHRow( theHRow ), myVRow( theVRow )
277 myLine = VISU::Curve::SOLIDLINE;
279 myMarker = VISU::Curve::CIRCLE;
280 myColor.R = 0.0; myColor.G = 0.0; myColor.B = 0.0;
285 VISU::Curve_i::~Curve_i()
287 INFOS("Curve_i::~Curve_i");
292 VISU::Storable* VISU::Curve_i::Create()
295 myName = GetVerTitle();
297 myName = GenerateName();
298 // ... and build the object
299 return Build( false );
302 Builds presentation of curve
304 VISU::Storable* VISU::Curve_i::Build(int theRestoring )
306 if ( myTable != NULL ) {
307 // getting table SObject by it's entry
308 int nbRows = myTable->GetNbRows();
309 if ( myHRow > 0 && myHRow <= nbRows && myVRow > 0 && myVRow <= nbRows ) {
310 if ( !theRestoring ) {
311 // look for component
312 SALOMEDS::SComponent_var SComponent = VISU::FindOrCreateVisuComponent( myStudy );
313 // create SObject and set attributes
315 aComment.sprintf("myComment=%s;myType=%d",GetComment(),VISU::TCURVE);
316 string anEntry = CreateAttributes( myStudy,
317 myTable->GetObjectEntry(),
324 // create SObject referenced to real table object
325 mySObj = SALOMEDS::SObject::_duplicate(myStudy->FindObjectID(anEntry.c_str()));
334 Returns CORBA::True if curve refers to valid table data
336 CORBA::Boolean VISU::Curve_i::IsValid()
338 // getting table SObject by it's entry
339 SALOMEDS::SObject_var SO = myStudy->FindObjectID(myTable->GetObjectEntry());
340 SALOMEDS::StudyBuilder_var Builder = myStudy->NewBuilder();
341 SALOMEDS::GenericAttribute_var anAttr;
342 SALOMEDS::AttributeTableOfInteger_var anInt;
343 SALOMEDS::AttributeTableOfReal_var aReal;
344 if ( !SO->_is_nil() ) {
345 if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfInteger" ) ) {
346 anInt = SALOMEDS::AttributeTableOfInteger::_narrow( anAttr );
347 if ( myHRow > 0 && myHRow <= anInt->GetNbRows() && myVRow > 0 && myVRow <= anInt->GetNbRows() ) {
351 else if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfReal" ) ) {
352 aReal = SALOMEDS::AttributeTableOfReal::_narrow( anAttr );
353 if ( myHRow > 0 && myHRow <= aReal->GetNbRows() && myVRow > 0 && myVRow <= aReal->GetNbRows() ) {
361 Returns hor.axis title
363 string VISU::Curve_i::GetHorTitle()
366 // getting table SObject by it's entry
367 SALOMEDS::SObject_var SO = myStudy->FindObjectID(myTable->GetObjectEntry());
368 SALOMEDS::StudyBuilder_var Builder = myStudy->NewBuilder();
369 SALOMEDS::GenericAttribute_var anAttr;
370 SALOMEDS::AttributeTableOfInteger_var anInt;
371 SALOMEDS::AttributeTableOfReal_var aReal;
372 if ( !SO->_is_nil() ) {
373 if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfInteger" ) ) {
374 anInt = SALOMEDS::AttributeTableOfInteger::_narrow( anAttr );
375 SALOMEDS::StringSeq_var rowTitles = anInt->GetRowTitles();
376 if ( rowTitles->length() > 0 && myHRow > 0 && myHRow <= anInt->GetNbRows() ) {
377 title = strdup( rowTitles[ myHRow-1 ] );
380 else if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfReal" ) ) {
381 aReal = SALOMEDS::AttributeTableOfReal::_narrow( anAttr );
382 SALOMEDS::StringSeq_var rowTitles = aReal->GetRowTitles();
383 if ( rowTitles->length() > 0 && myHRow > 0 && myHRow <= aReal->GetNbRows() ) {
384 title = strdup( rowTitles[ myHRow-1 ] );
391 Returns ver.axis title
393 string VISU::Curve_i::GetVerTitle()
396 // getting table SObject by it's entry
397 SALOMEDS::SObject_var SO = myStudy->FindObjectID(myTable->GetObjectEntry());
398 SALOMEDS::StudyBuilder_var Builder = myStudy->NewBuilder();
399 SALOMEDS::GenericAttribute_var anAttr;
400 SALOMEDS::AttributeTableOfInteger_var anInt;
401 SALOMEDS::AttributeTableOfReal_var aReal;
402 if ( !SO->_is_nil() ) {
403 if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfInteger" ) ) {
404 anInt = SALOMEDS::AttributeTableOfInteger::_narrow( anAttr );
405 SALOMEDS::StringSeq_var rowTitles = anInt->GetRowTitles();
406 if ( rowTitles->length() > 0 && myVRow > 0 && myVRow <= anInt->GetNbRows() )
407 title = strdup( rowTitles[ myVRow-1 ] );
409 else if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfReal" ) ) {
410 aReal = SALOMEDS::AttributeTableOfReal::_narrow( anAttr );
411 SALOMEDS::StringSeq_var rowTitles = aReal->GetRowTitles();
412 if ( rowTitles->length() > 0 && myVRow > 0 && myVRow <= aReal->GetNbRows() )
413 title = strdup( rowTitles[ myVRow-1 ] );
419 Returns hor.axis units
421 string VISU::Curve_i::GetHorUnits()
424 // getting table SObject by it's entry
425 SALOMEDS::SObject_var SO = myStudy->FindObjectID(myTable->GetObjectEntry());
426 SALOMEDS::StudyBuilder_var Builder = myStudy->NewBuilder();
427 SALOMEDS::GenericAttribute_var anAttr;
428 SALOMEDS::AttributeTableOfInteger_var anInt;
429 SALOMEDS::AttributeTableOfReal_var aReal;
430 if ( !SO->_is_nil() ) {
431 if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfInteger" ) ) {
432 anInt = SALOMEDS::AttributeTableOfInteger::_narrow( anAttr );
433 SALOMEDS::StringSeq_var rowUnits = anInt->GetRowUnits();
434 if ( rowUnits->length() > 0 && myHRow > 0 && myHRow <= anInt->GetNbRows() )
435 units = strdup( rowUnits[ myHRow-1 ] );
437 else if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfReal" ) ) {
438 aReal = SALOMEDS::AttributeTableOfReal::_narrow( anAttr );
439 SALOMEDS::StringSeq_var rowUnits = aReal->GetRowUnits();
440 if ( rowUnits->length() > 0 && myHRow > 0 && myHRow <= aReal->GetNbRows() )
441 units = strdup( rowUnits[ myHRow-1 ] );
447 Returns ver.axis units
449 string VISU::Curve_i::GetVerUnits()
452 // getting table SObject by it's entry
453 SALOMEDS::SObject_var SO = myStudy->FindObjectID(myTable->GetObjectEntry());
454 SALOMEDS::StudyBuilder_var Builder = myStudy->NewBuilder();
455 SALOMEDS::GenericAttribute_var anAttr;
456 SALOMEDS::AttributeTableOfInteger_var anInt;
457 SALOMEDS::AttributeTableOfReal_var aReal;
458 if ( !SO->_is_nil() ) {
459 if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfInteger" ) ) {
460 anInt = SALOMEDS::AttributeTableOfInteger::_narrow( anAttr );
461 SALOMEDS::StringSeq_var rowUnits = anInt->GetRowUnits();
462 if ( rowUnits->length() > 0 && myVRow > 0 && myVRow <= anInt->GetNbRows() )
463 units = strdup( rowUnits[ myVRow-1] );
465 else if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfReal" ) ) {
466 aReal = SALOMEDS::AttributeTableOfReal::_narrow( anAttr );
467 SALOMEDS::StringSeq_var rowUnits = aReal->GetRowUnits();
468 if ( rowUnits->length() > 0 && myVRow > 0 && myVRow <= aReal->GetNbRows() )
469 units = strdup( rowUnits[ myVRow-1 ] );
477 int VISU::Curve_i::GetData( double*& theHorList, double*& theVerList )
479 theHorList = 0; theVerList = 0;
480 // getting table SObject by it's entry
481 SALOMEDS::SObject_var SO = myStudy->FindObjectID(myTable->GetObjectEntry());
482 SALOMEDS::StudyBuilder_var Builder = myStudy->NewBuilder();
483 SALOMEDS::GenericAttribute_var anAttr;
484 SALOMEDS::AttributeTableOfInteger_var anInt;
485 SALOMEDS::AttributeTableOfReal_var aReal;
486 if ( !SO->_is_nil() ) {
487 if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfInteger" ) ) {
488 anInt = SALOMEDS::AttributeTableOfInteger::_narrow( anAttr );
489 int nbCols = anInt->GetNbColumns() ;
490 if ( nbCols > 0 && myHRow > 0 && myHRow <= anInt->GetNbRows() && myVRow > 0 && myVRow <= anInt->GetNbRows() ) {
492 for ( int j = 1; j <= nbCols; j++ ) {
493 if ( anInt->HasValue( myHRow, j ) && anInt->HasValue( myVRow, j ) )
496 if ( nbPoints > 0 ) {
497 theHorList = new double[ nbPoints ];
498 theVerList = new double[ nbPoints ];
500 for ( int j = 1; j <= nbCols; j++ ) {
501 if ( anInt->HasValue( myHRow, j ) && anInt->HasValue( myVRow, j ) ) {
502 theHorList[k] = anInt->GetValue( myHRow, j );
503 theVerList[k] = anInt->GetValue( myVRow, j );
511 else if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfReal" ) ) {
512 aReal = SALOMEDS::AttributeTableOfReal::_narrow( anAttr );
513 int nbCols = aReal->GetNbColumns() ;
514 if ( nbCols > 0 && myHRow > 0 && myHRow <= aReal->GetNbRows() && myVRow > 0 && myVRow <= aReal->GetNbRows() ) {
516 for ( int j = 1; j <= nbCols; j++ ) {
517 if ( aReal->HasValue( myHRow, j ) && aReal->HasValue( myVRow, j ) )
520 if ( nbPoints > 0 ) {
521 theHorList = new double[ nbPoints ];
522 theVerList = new double[ nbPoints ];
524 for ( int j = 1; j <= nbCols; j++ ) {
525 if ( aReal->HasValue( myHRow, j ) && aReal->HasValue( myVRow, j ) ) {
526 theHorList[k] = aReal->GetValue( myHRow, j );
527 theVerList[k] = aReal->GetValue( myVRow, j );
539 Creates curve Plot2d presentation object
541 Plot2d_Curve* VISU::Curve_i::CreatePresentation()
543 Plot2d_Curve* crv = new Plot2d_Curve();
544 crv->setHorTitle( GetHorTitle().c_str() );
545 string tlt = GetTitle();
546 if ( tlt.length() <= 0 )
548 //crv->setVerTitle( strdup( GetVerTitle().c_str() ) );
549 //crv->setVerTitle( strdup( GetName() ) );
550 crv->setVerTitle( tlt.c_str() );
551 crv->setHorUnits( GetHorUnits().c_str() );
552 crv->setVerUnits( GetVerUnits().c_str() );
555 int nbPoints = GetData( xList, yList );
556 if ( nbPoints > 0 && xList && yList ) {
557 crv->setData( xList, yList, nbPoints );
559 //cout << "********** Number of points: " << nbPoints <<endl;
560 //for ( int i =0 ; i < nbPoints; i++ ) {
561 // cout << i<<"\t"<<xList[i] << "\t"<< yList[i] << endl;
563 crv->setLine( (Plot2d_Curve::LineType)GetLine(), GetLineWidth() );
564 crv->setMarker( (Plot2d_Curve::MarkerType)GetMarker() );
565 SALOMEDS::Color color = GetColor();
566 crv->setColor( QColor( (int)(color.R*255.), (int)(color.G*255.), (int)(color.B*255.) ) );
567 crv->setAutoAssign( IsAuto() );
568 crv->setIO(new SALOME_InteractiveObject(strdup(mySObj->GetID()),"VISU",strdup(GetName())));
570 crv->setTableIO(new SALOME_InteractiveObject(strdup(myTable->GetObjectEntry()),"VISU",strdup(myTable->GetName())));
574 Restores curve object from stream
576 VISU::Storable* VISU::Curve_i::Restore( const Storable::TRestoringMap& theMap, SALOMEDS::SObject_ptr theSO)
577 throw( std::logic_error& )
579 if(MYDEBUG) MESSAGE(GetComment());
580 mySObj = SALOMEDS::SObject::_duplicate(theSO);
581 myName = VISU::Storable::FindValue(theMap,"myName").latin1();
582 myHRow = VISU::Storable::FindValue(theMap,"myHRow").toInt();
583 myVRow = VISU::Storable::FindValue(theMap,"myVRow").toInt();
584 myColor.R = VISU::Storable::FindValue(theMap,"myColor.R").toDouble();
585 myColor.G = VISU::Storable::FindValue(theMap,"myColor.G").toDouble();
586 myColor.B = VISU::Storable::FindValue(theMap,"myColor.B").toDouble();
587 myMarker = ( VISU::Curve::MarkerType )( VISU::Storable::FindValue(theMap,"myMarker").toInt() );
588 myLine = ( VISU::Curve::LineType )( VISU::Storable::FindValue(theMap,"myLine").toInt() );
589 myLineWidth = VISU::Storable::FindValue(theMap,"myLineWidth").toInt();
590 myAuto = VISU::Storable::FindValue(theMap,"myAuto").toInt();
591 return Build( true );
594 Flushes curve data into stream
596 void VISU::Curve_i::ToStream( std::ostringstream& theStr )
598 Storable::DataToStream( theStr, "myName", myName.c_str() );
599 Storable::DataToStream( theStr, "myHRow", myHRow );
600 Storable::DataToStream( theStr, "myVRow", myVRow );
601 Storable::DataToStream( theStr, "myColor.R", myColor.R );
602 Storable::DataToStream( theStr, "myColor.G", myColor.G );
603 Storable::DataToStream( theStr, "myColor.B", myColor.B );
604 Storable::DataToStream( theStr, "myMarker", myMarker );
605 Storable::DataToStream( theStr, "myLine", myLine );
606 Storable::DataToStream( theStr, "myLineWidth", myLineWidth );
607 Storable::DataToStream( theStr, "myAuto", myAuto );
610 Gets reference table's entry
612 const char* VISU::Curve_i::GetTableID() {
613 return CORBA::string_dup(myTable->GetObjectEntry());
616 Called from engine to restore curve from the file
618 VISU::Storable* VISU::Curve_i::Restore(SALOMEDS::SObject_ptr theSObject,
619 const string& thePrefix, const Storable::TRestoringMap& theMap)
620 throw(std::logic_error&)
622 SALOMEDS::Study_var aStudy = theSObject->GetStudy();
623 VISU::Table_i* pTable = GetTable(aStudy, theSObject->GetFather());
624 if( pTable != NULL ) {
625 VISU::Curve_i* pResent = new VISU::Curve_i( aStudy, pTable, 0, 0 );
626 return pResent->Restore( theMap, theSObject);
631 //----------------------------------------------------------------
633 //----------------------------------------------------------------
634 int VISU::Container_i::myNbPresent = 0;
635 const string VISU::Container_i::myComment = "CONTAINER";
639 const char* VISU::Container_i::GenerateName()
641 return VISU::GenerateName( "Plot2DView", ++myNbPresent );
646 const char* VISU::Container_i::GetComment() const
648 return myComment.c_str();
653 VISU::Container_i::Container_i( SALOMEDS::Study_ptr theStudy )
654 : PrsObject_i( theStudy )
660 VISU::Container_i::~Container_i()
662 INFOS("Container_i::~Container_i");
666 Inserts curve into the container
668 void VISU::Container_i::AddCurve( Curve_ptr theCurve )
670 if ( myStudy->_is_nil() )
672 SALOMEDS::SObject_var mySO = myStudy->FindObjectID( GetEntry() );
673 if ( mySO->_is_nil() )
675 PortableServer::POA_ptr aPOA = GetPOA();
676 Curve_i* pCurve = dynamic_cast<Curve_i*>( aPOA->reference_to_servant( theCurve ) );
678 QString entry = pCurve->GetEntry();
679 SALOMEDS::SObject_var SO = myStudy->FindObjectID( entry.latin1() );
680 if ( !SO->_is_nil() && myCurves.find( entry ) == myCurves.end() ) {
681 myCurves.append( entry );
682 SALOMEDS::StudyBuilder_var Builder = myStudy->NewBuilder();
683 SALOMEDS::SObject_var newSO = Builder->NewObject( mySO );
684 Builder->Addreference( newSO, SO );
689 Removes curve from the container
691 void VISU::Container_i::RemoveCurve( Curve_ptr theCurve )
693 if ( myStudy->_is_nil() )
695 SALOMEDS::SObject_var mySO = myStudy->FindObjectID( GetEntry() );
696 if ( mySO->_is_nil() )
698 PortableServer::POA_ptr aPOA = GetPOA();
699 Curve_i* pCurve = dynamic_cast<Curve_i*>( aPOA->reference_to_servant( theCurve ) );
701 QString entry = pCurve->GetEntry();
702 if ( myCurves.find( entry ) != myCurves.end() ) {
704 myCurves.remove( entry );
705 SALOMEDS::StudyBuilder_var Builder = myStudy->NewBuilder();
706 SALOMEDS::ChildIterator_var CI = myStudy->NewChildIterator( mySO );
707 for ( ; CI->More(); CI->Next() ) {
708 SALOMEDS::SObject_var childSO = CI->Value();
709 SALOMEDS::SObject_var refSO;
710 if ( childSO->ReferencedObject( refSO ) && !refSO->_is_nil() && entry == QString( refSO->GetID() ) ) {
711 Builder->RemoveObject( childSO );
718 Gets number of curves in the container
720 CORBA::Long VISU::Container_i::GetNbCurves()
723 return myCurves.count();
728 void VISU::Container_i::Clear()
730 if ( myStudy->_is_nil() )
732 SALOMEDS::SObject_var mySO = myStudy->FindObjectID( GetEntry() );
733 if ( mySO->_is_nil() )
735 QStringList toDelete;
736 SALOMEDS::ChildIterator_var CI = myStudy->NewChildIterator( mySO );
737 for ( ; CI->More(); CI->Next() ) {
738 toDelete.append( CI->Value()->GetID() );
740 SALOMEDS::StudyBuilder_var Builder = myStudy->NewBuilder();
741 for ( int i = 0; i < toDelete.count(); i++ ) {
742 SALOMEDS::SObject_var SO = myStudy->FindObjectID( toDelete[i].latin1() );
743 Builder->RemoveObject( SO );
748 Creates container object
750 VISU::Storable* VISU::Container_i::Create()
753 myName = GenerateName();
754 // ... and build the object
755 return Build( false );
758 Builds presentation of container
760 VISU::Storable* VISU::Container_i::Build( int theRestoring )
762 if ( !theRestoring ) {
763 // looking for component
764 SALOMEDS::SComponent_var SComponent = VISU::FindOrCreateVisuComponent( myStudy );
765 // create SObject and set attributes
767 aComment.sprintf("myComment=%s;myType=%d",GetComment(),VISU::TCONTAINER);
768 string anEntry = CreateAttributes( myStudy,
780 Updates presentation of container
782 void VISU::Container_i::Update()
784 if ( myStudy->_is_nil() )
786 SALOMEDS::StudyBuilder_var Builder = myStudy->NewBuilder();
787 SALOMEDS::SObject_var mySO = myStudy->FindObjectID( GetEntry() );
788 SALOMEDS::GenericAttribute_var anAttr;
789 if ( !mySO->_is_nil() ) {
790 QStringList toDelete;
792 for ( i = 0; i < myCurves.count(); i++ ) {
793 SALOMEDS::SObject_var SO = myStudy->FindObjectID( myCurves[i].latin1() );
794 if ( !SO->_is_nil() && Builder->FindAttribute( SO, anAttr, "AttributeIOR" ) ) {
795 // if real Curve Object still exists
796 SALOMEDS::ChildIterator_var CI = myStudy->NewChildIterator( mySO );
798 for ( ; CI->More(); CI->Next() ) {
799 SALOMEDS::SObject_var childSO = CI->Value();
800 SALOMEDS::SObject_var refSO;
801 if ( childSO->ReferencedObject( refSO ) && !refSO->_is_nil() && myCurves[i] == QString( refSO->GetID() ) ) {
802 bFound = true; break;
806 // create SObject referenced to real curve object if is not yet added
807 SALOMEDS::SObject_var newSO = Builder->NewObject( mySO );
808 Builder->Addreference( newSO, SO );
812 // real Curve Object doesn't exist (might be removed)
813 toDelete.append( myCurves[i] );
816 for ( i = 0; i < toDelete.count(); i++ ) {
817 myCurves.remove( toDelete[i] );
820 SALOMEDS::ChildIterator_var CI = myStudy->NewChildIterator( mySO );
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() || !Builder->FindAttribute( refSO, anAttr, "AttributeIOR" ) ||
825 myCurves.find( refSO->GetID() ) == myCurves.end() ) ) {
826 toDelete.append( childSO->GetID() );
829 for ( i = 0; i < toDelete.count(); i++ ) {
830 SALOMEDS::ChildIterator_var CI = myStudy->NewChildIterator( mySO );
831 for ( ; CI->More(); CI->Next() ) {
832 SALOMEDS::SObject_var childSO = CI->Value();
833 if ( toDelete[i] == CI->Value()->GetID() ) {
834 Builder->RemoveObject( childSO );
841 Gets curve from container by index
842 NB : curves are numbered from 1
844 VISU::Curve_i* VISU::Container_i::GetCurve( CORBA::Long theIndex )
846 if ( theIndex > 0 && theIndex <= myCurves.count() ) {
847 SALOMEDS::StudyBuilder_var Builder = myStudy->NewBuilder();
848 SALOMEDS::GenericAttribute_var anAttr;
849 SALOMEDS::SObject_var SO = myStudy->FindObjectID( myCurves[ theIndex-1 ].latin1() );
850 CORBA::Object_var anObject = VISU::SObjectToObject( SO );
851 if( !CORBA::is_nil( anObject ) ) {
852 // if real Curve Object exists
853 CORBA::Object_ptr aCurve = VISU::Curve::_narrow( anObject );
854 if( !CORBA::is_nil( aCurve ) )
855 return dynamic_cast<VISU::Curve_i*>(VISU::GetServant(aCurve).in());
861 Restores container data from the stream
863 VISU::Storable* VISU::Container_i::Restore( const Storable::TRestoringMap& theMap )
864 throw( std::logic_error& )
866 if(MYDEBUG) MESSAGE(GetComment());
867 myName = VISU::Storable::FindValue( theMap, "myName" ).latin1();
868 QString val = VISU::Storable::FindValue( theMap, "myCurves" );
869 myCurves = QStringList::split( QString( "*" ), val, false );
870 return Build( true );
873 Flushes container data into the stream
875 void VISU::Container_i::ToStream( std::ostringstream& theStr )
877 Storable::DataToStream( theStr, "myName", myName.c_str() );
878 Storable::DataToStream( theStr, "myCurves", myCurves.join( QString( "*" ) ) );
879 // theStr<<" myName "<<myName;
880 // theStr<<" myCurves "<<myCurves.join( QString( "*" ) ).latin1()<<"* ";
883 Called from engine to restore container from the file
885 VISU::Storable* VISU::Container_i::Restore(SALOMEDS::SObject_ptr theSObject,
886 const string& thePrefix, const Storable::TRestoringMap& theMap)
887 throw(std::logic_error&)
889 SALOMEDS::Study_var aStudy = theSObject->GetStudy();
890 VISU::Container_i* pResent = new VISU::Container_i( aStudy );
891 return pResent->Restore( theMap );
894 //-------------------------------------------------------------
895 // Implementation of reading from file
896 //-------------------------------------------------------------
897 typedef double TValue;
898 typedef vector<TValue> TValues;
906 typedef vector<TRow> TRows;
910 vector<string> myColumnUnits;
911 vector<string> myColumnTitles;
914 if(myRows.empty()) return 0;
915 int iEnd = myRows[0].myValues.size();
916 if(iEnd == 0) return 0;
917 if(myColumnTitles.size() != iEnd) myColumnTitles.resize(iEnd);
918 if(myColumnUnits.size() != iEnd) myColumnUnits.resize(iEnd);
919 int jEnd = myRows.size();
920 for(int j = 0; j < jEnd; j++)
921 if(myRows[j].myValues.size() != iEnd)
925 void getColumns(TTable2D& theTable2D) const {
926 TRows& aRows = theTable2D.myRows;
928 if(myRows.empty()) return;
929 int jEnd = myRows.size();
930 //Define Titles & Units
931 theTable2D.myColumnTitles.resize(jEnd);
932 theTable2D.myColumnUnits.resize(jEnd);
933 for(int j = 0; j < jEnd; j++){
934 theTable2D.myColumnTitles[j] = myRows[j].myTitle;
935 theTable2D.myColumnUnits[j] = myRows[j].myUnit;
938 int iEnd = myRows[0].myValues.size();
939 for(int i = 0; i < iEnd; i++){
941 aNewRow.myTitle = myColumnTitles[i];
942 aNewRow.myUnit = myColumnUnits[i];
943 aNewRow.myValues.resize(jEnd);
944 for(int j = 0; j < jEnd; j++){
945 aNewRow.myValues[j] = myRows[j].myValues[i];
947 aRows.push_back(aNewRow);
952 typedef vector<TTable2D> TTableCont;
954 int getLine(ifstream& theStmIn, QString& theString){
957 while(theStmIn.get(tmp)){
959 if(tmp == '\n') break;
962 theString = aStrOut.str();
963 return !theStmIn.eof();
966 void ImportTables(const char* theFileName, TTableCont& theTableCont){
967 static int STRPRECISION = 12;
969 QFileInfo aFileInfo(theFileName);
970 if(!aFileInfo.isFile() || !aFileInfo.isReadable() || !aFileInfo.size()) return;
971 aStmIn.open(theFileName);
974 //Find beginning of Table
975 while(getLine(aStmIn,aTmp) && aTmp == "\n");
976 if(MYDEBUG) cout<<"\n There is new Table2D with Title = ";
978 while(!aStmIn.eof() && aTmp != "\n"){
979 if(aTmp.find("#TITLE:") == 0){
980 int aLen = aTmp.find(":") + 1;
982 QString aTitle = aTmp.stripWhiteSpace();
983 aTable2D.myTitle = aTitle.latin1();
984 if(MYDEBUG) cout<<aTitle<<endl;
985 }else if(aTmp.find("#COLUMN_TITLES:") == 0){
986 int aLen = aTmp.find(":") + 1;
988 QStringList aStrList = QStringList::split("|",aTmp);
989 if(MYDEBUG) cout<<"Its Column Titles : ";
990 for(int i = 0; i < aStrList.count(); i++){
991 aTmp = aStrList[i].stripWhiteSpace();
992 aTable2D.myColumnTitles.push_back(aTmp.latin1());
993 if(MYDEBUG) cout<<"\t"<<aTmp;
995 if(MYDEBUG) cout<<endl;
996 }else if(aTmp.find("#COLUMN_UNITS:") == 0){
997 int aLen = aTmp.find(":") + 1;
999 QStringList aStrList = QStringList::split(" ",aTmp);
1000 if(MYDEBUG) cout<<"Its Column Units : ";
1001 for(int i = 0; i < aStrList.count(); i++){
1002 aTmp = aStrList[i].stripWhiteSpace();
1003 aTable2D.myColumnUnits.push_back(aTmp.latin1());
1004 if(MYDEBUG) cout<<"\t"<<aTmp;
1006 if(MYDEBUG) cout<<endl;
1007 }else if(aTmp.find("#") == 0){
1009 }else if(aTmp.find("#TITLE:") > 0){
1010 QStringList aStrList = QStringList::split("#TITLE:",aTmp);
1011 QString aTitle = aStrList[1].stripWhiteSpace();
1013 aRow.myTitle = aTitle.latin1();
1014 if(MYDEBUG) cout<<aTitle<<" : ";
1015 QStringList aValList = QStringList::split(" ",aStrList[0]);
1016 for(int i = 0; i < aValList.count(); i++){
1017 float aVal = aValList[i].toFloat();
1018 aRow.myValues.push_back(aVal);
1019 if(MYDEBUG) cout<<"\t"<<aVal;
1021 aTable2D.myRows.push_back(aRow);
1022 if(MYDEBUG) cout<<endl;
1026 istrstream aStream(aTmp);
1027 aStream.precision(STRPRECISION);
1028 while(aStream>>aVal){
1029 aRow.myValues.push_back(aVal);
1030 if(MYDEBUG) cout<<"\t"<<aVal;
1032 if(aRow.myValues.size() > 0)
1033 aTable2D.myRows.push_back(aRow);
1034 if(MYDEBUG) cout<<endl;
1036 getLine(aStmIn,aTmp);
1038 if(aTable2D.Check()){
1039 if(MYDEBUG) cout<<"aTable2D checked "<<aTable2D.myTitle<<endl;
1040 theTableCont.push_back(aTable2D);
1042 }while(!aStmIn.eof());
1044 if(MYDEBUG) cout<<"After close"<<endl;
1047 SALOMEDS::SObject_var VISU::ImportTables(const char* theFileName, SALOMEDS::Study_ptr theStudy){
1048 TTableCont aTableCont;
1049 ImportTables(theFileName,aTableCont);
1050 if(aTableCont.empty()) return SALOMEDS::SObject::_nil();
1051 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
1052 SALOMEDS::SComponent_var theSComponent = VISU::FindOrCreateVisuComponent(theStudy);
1053 SALOMEDS::SObject_var aFileObject = aStudyBuilder->NewObject(theSComponent);
1054 SALOMEDS::GenericAttribute_var anAttr =
1055 aStudyBuilder->FindOrCreateAttribute(aFileObject, "AttributeName");
1056 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
1057 QFileInfo aFileInfo(theFileName);
1058 aName->SetValue(aFileInfo.fileName().latin1());
1059 int iEnd = aTableCont.size();
1060 for(int i = 0, iEnd = aTableCont.size(); i < iEnd; i++){
1061 const TTable2D& aTable2D = aTableCont[i];
1062 SALOMEDS::SObject_var aRealObject = aStudyBuilder->NewObject(aFileObject);
1063 anAttr = aStudyBuilder->FindOrCreateAttribute(aRealObject, "AttributeName");
1064 aName = SALOMEDS::AttributeName::_narrow(anAttr);
1065 if(MYDEBUG) cout<<"aTable2D.myTitle = "<<aTable2D.myTitle<<endl;
1066 if(aTable2D.myTitle != "")
1067 aName->SetValue(aTable2D.myTitle.c_str());
1070 aNewName.sprintf("Table:%d",i);
1071 aName->SetValue(aNewName.latin1());
1073 anAttr = aStudyBuilder->FindOrCreateAttribute(aRealObject, "AttributeTableOfReal");
1074 SALOMEDS::AttributeTableOfReal_var aTableOfReal = SALOMEDS::AttributeTableOfReal::_narrow(anAttr);
1075 aTableOfReal->SetTitle(aTable2D.myTitle.c_str());
1076 TTable2D aNewTable2D;
1077 aTable2D.getColumns(aNewTable2D);
1078 int kEnd = aNewTable2D.myRows[0].myValues.size();
1079 aTableOfReal->SetNbColumns(kEnd);
1080 for(int j = 0, jEnd = aNewTable2D.myRows.size(); j < jEnd; j++){
1081 if(MYDEBUG) cout<<"j = "<<j<<endl;
1082 SALOMEDS::DoubleSeq_var aDoubleSeq = new SALOMEDS::DoubleSeq();
1083 aDoubleSeq->length(kEnd);
1084 if(MYDEBUG) cout<<"kEnd = "<<kEnd<<endl;
1085 for(int k = 0; k < kEnd; k++) aDoubleSeq[k] = aNewTable2D.myRows[j].myValues[k];
1086 aTableOfReal->AddRow(aDoubleSeq.in());
1087 aTableOfReal->SetRowTitle(j+1,aNewTable2D.myRows[j].myTitle.c_str());
1088 aTableOfReal->SetRowUnit(j+1,aNewTable2D.myRows[j].myUnit.c_str());
1090 for(int k = 0; k < kEnd; k++)
1091 aTableOfReal->SetColumnTitle(k+1,aNewTable2D.myColumnTitles[k].c_str());
1096 template<class TTableAttr> bool ExportTableToFile(const TTableAttr& aTabAttr,
1097 const char* theFileName)
1099 if (!CORBA::is_nil(aTabAttr)) {
1100 QFile aFile(theFileName);
1101 aFile.open(IO_WriteOnly);
1103 /* extract the tabe info and write it into file */
1105 QString aTitle(aTabAttr->GetTitle()); /*Table title*/
1106 int aRowsNb = aTabAttr->GetNbRows();
1107 int aColNb = aTabAttr->GetNbColumns();
1109 SALOMEDS::StringSeq_var aRowTitles = aTabAttr->GetRowTitles();
1110 QString anAbscissTitle(aRowTitles[0]); /*Absciss row title (X coord)*/
1111 anAbscissTitle.stripWhiteSpace();
1113 SALOMEDS::StringSeq_var aRowUnits = aTabAttr->GetRowUnits();
1114 QString anAbscissUnit(aRowUnits[0]);
1115 anAbscissUnit.stripWhiteSpace();
1117 SALOMEDS::StringSeq_var aColumnTitles = aTabAttr->GetColumnTitles();
1118 if (aRowsNb > 2 && aTitle.length() ) aTitle = aTitle + " - ";
1121 for (int i = 2; i <= aRowsNb; i++ )
1124 QString anOrdinate(aRowTitles[i-1]), aTail;
1125 anOrdinate.stripWhiteSpace();
1127 aLine = "#TITLE: " + aTitle +
1128 ((anOrdinate.length())? anOrdinate :
1129 (aRowsNb>2)? aTail.sprintf("%d",i-1) : aTail.sprintf("") ) + "\n";
1130 aFile.writeBlock(aLine, aLine.length() );
1133 if ( anAbscissTitle.length() || anOrdinate.length() ) {
1134 aLine = "#COLUMN_TITLES: " + anAbscissTitle + " | " + anOrdinate + "\n";
1135 aFile.writeBlock(aLine, aLine.length() );
1139 aLine = anAbscissUnit + " " +aRowUnits[i-1];
1140 if (!aLine.stripWhiteSpace().isEmpty()) {
1141 aLine = "#COLUMN_UNITS: " + aLine + "\n";
1142 aFile.writeBlock(aLine, aLine.length() );
1145 /* CURVE COORDINATES */
1146 for (int j = 1; j <= aColNb; j++)
1148 if ( aTabAttr -> HasValue(i,j) && aTabAttr -> HasValue(1, j)) {
1149 aLine = aLine.sprintf("%.16g %.16g",
1150 (double)(aTabAttr->GetValue(1,j)),
1151 (double)(aTabAttr->GetValue(i,j))); /* aTabAttr->GetValue(1,j) - X coord */
1152 if ( !aLine.stripWhiteSpace().isEmpty() ) {
1153 QString aColTitle(aColumnTitles[j-1]);
1154 if ( !aColTitle.stripWhiteSpace().isEmpty() )
1155 aLine = aLine + " #TITLE: " + aColTitle ;
1156 aFile.writeBlock(aLine + "\n", aLine.length() + 1);
1160 aFile.writeBlock("\n", 1);
1168 bool VISU::ExportTableToFile(SALOMEDS::SObject_ptr theTable, const char* theFileName)
1171 SALOMEDS::GenericAttribute_var anAttr ;
1172 if (theTable->FindAttribute(anAttr, "AttributeTableOfReal"))
1174 SALOMEDS::AttributeTableOfReal_var aTabAttr = SALOMEDS::AttributeTableOfReal ::_narrow(anAttr);
1175 return ExportTableToFile ( aTabAttr , theFileName);
1178 else if (theTable->FindAttribute(anAttr, "AttributeTableOfInteger")) {
1180 SALOMEDS::AttributeTableOfInteger_var aTabAttr = SALOMEDS::AttributeTableOfInteger ::_narrow(anAttr);
1181 return ExportTableToFile ( aTabAttr , theFileName);