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"
34 #include "VISU_Result_i.hh"
40 #include <qfileinfo.h>
47 static int MYDEBUG = 0;
49 static int MYDEBUG = 0;
51 //----------------------------------------------------------------
53 //----------------------------------------------------------------
54 int VISU::Table_i::myNbPresent = 0;
55 const string VISU::Table_i::myComment = "TABLE";
59 const char* VISU::Table_i::GenerateName()
61 return VISU::GenerateName( "Table", ++myNbPresent );
66 const char* VISU::Table_i::GetComment() const
68 return myComment.c_str();
73 VISU::Table_i::Table_i( SALOMEDS::Study_ptr theStudy, const char* theObjectEntry )
74 : PrsObject_i(theStudy)
76 mySObj = SALOMEDS::SObject::_duplicate((theStudy->FindObjectID(theObjectEntry)));
77 myOrientation = VISU::Table::HORIZONTAL;
82 VISU::Table_i::~Table_i()
84 MESSAGE("Table_i::~Table_i");
87 Gets number of rows in table
89 CORBA::Long VISU::Table_i::GetNbRows()
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();
99 else if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfReal" ) ) {
100 SALOMEDS::AttributeTableOfReal_var aReal = SALOMEDS::AttributeTableOfReal::_narrow( anAttr );
101 return aReal->GetNbRows();
107 Gets number of columns in table
109 CORBA::Long VISU::Table_i::GetNbColumns()
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();
119 else if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfReal" ) ) {
120 SALOMEDS::AttributeTableOfReal_var aReal = SALOMEDS::AttributeTableOfReal::_narrow( anAttr );
121 return aReal->GetNbColumns();
129 VISU::Storable* VISU::Table_i::Create()
132 myName = GetTableTitle();
134 // mpv (PAL 5357): if name attribute already exist at this label, use it as name of table
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());
145 if (mySObj->GetName()) myName = mySObj->GetName();
149 myName = GenerateName();
150 // ... and build the object
151 return Build( false );
154 Builds presentation of table
156 VISU::Storable* VISU::Table_i::Build( int theRestoring )
159 // look for reference SObject with table attribute
160 SALOMEDS::SObject_var SO = mySObj;
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());
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
177 aComment.sprintf("myComment=%s;myType=%d",GetComment(),VISU::TTABLE);
178 string anEntry = CreateAttributes( myStudy,
179 SO->GetID(),//SComponent->GetID(),
186 // create SObject referenced to real table object
187 mySObj = SALOMEDS::SObject::_duplicate(myStudy->FindObjectID( anEntry.c_str() ));
189 pCutLines->BuildTableOfReal(mySObj);
191 // mpv (PAL5357): reference attributes are unnecessary now
192 //SALOMEDS::SObject_var refSO = Builder->NewObject( mySObj );
193 //Builder->Addreference( refSO, SO );
200 Restores table object from stream
202 VISU::Storable* VISU::Table_i::Restore( const Storable::TRestoringMap& theMap, SALOMEDS::SObject_ptr SO)
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 );
212 Flushes table data into stream
214 void VISU::Table_i::ToStream( std::ostringstream& theStr )
216 Storable::DataToStream( theStr, "myName", myName.c_str() );
217 Storable::DataToStream( theStr, "myTitle", myTitle.c_str() );
218 Storable::DataToStream( theStr, "myOrientation", myOrientation );
221 Called from engine to restore table from the file
223 VISU::Storable* VISU::Table_i::Restore(SALOMEDS::SObject_ptr theSObject,
224 const string& thePrefix, const Storable::TRestoringMap& theMap)
226 SALOMEDS::Study_var aStudy = theSObject->GetStudy();
227 VISU::Table_i* pResent = new VISU::Table_i( aStudy, "" );
228 return pResent->Restore( theMap, theSObject);
231 Gets title for the original table object
233 const char* VISU::Table_i::GetTableTitle()
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();
245 else if ( Builder->FindAttribute( SO, anAttr, "AttributeTableOfReal" ) ) {
246 aReal = SALOMEDS::AttributeTableOfReal::_narrow( anAttr );
247 return aReal->GetTitle();
253 void VISU::Table_i::RemoveFromStudy(){
254 VISU::RemoveFromStudy(mySObj,false);
257 //----------------------------------------------------------------
259 //----------------------------------------------------------------
261 Restores table object from the stream [ static ]
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());
273 int VISU::Curve_i::myNbPresent = 0;
274 const string VISU::Curve_i::myComment = "CURVE";
278 const char* VISU::Curve_i::GenerateName()
280 return VISU::GenerateName( "Curve", ++myNbPresent );
285 const char* VISU::Curve_i::GetComment() const
287 return myComment.c_str();
291 NB : theHRow, theVRow are the indexes of rows in the Table object and numbered from the 1 to GetNbRows()
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 )
297 myLine = VISU::Curve::SOLIDLINE;
299 myMarker = VISU::Curve::CIRCLE;
300 myColor.R = 0.0; myColor.G = 0.0; myColor.B = 0.0;
305 VISU::Curve_i::~Curve_i()
307 MESSAGE("Curve_i::~Curve_i");
312 VISU::Storable* VISU::Curve_i::Create()
315 myName = GetVerTitle();
317 myName = GenerateName();
318 // ... and build the object
319 return Build( false );
322 Builds presentation of curve
324 VISU::Storable* VISU::Curve_i::Build(int theRestoring )
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
335 aComment.sprintf("myComment=%s;myType=%d",GetComment(),VISU::TCURVE);
336 string anEntry = CreateAttributes( myStudy,
337 myTable->GetObjectEntry(),
344 // create SObject referenced to real table object
345 mySObj = SALOMEDS::SObject::_duplicate(myStudy->FindObjectID(anEntry.c_str()));
354 Returns CORBA::True if curve refers to valid table data
356 CORBA::Boolean VISU::Curve_i::IsValid()
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() ) {
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() ) {
381 Returns hor.axis title
383 string VISU::Curve_i::GetHorTitle()
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 ];
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 ];
411 Returns ver.axis title
413 string VISU::Curve_i::GetVerTitle()
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 ];
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 ];
439 Returns hor.axis units
441 string VISU::Curve_i::GetHorUnits()
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 ];
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 ];
467 Returns ver.axis units
469 string VISU::Curve_i::GetVerUnits()
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];
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 ];
497 int VISU::Curve_i::GetData( double*& theHorList, double*& theVerList )
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() ) {
512 for ( int j = 1; j <= nbCols; j++ ) {
513 if ( anInt->HasValue( myHRow, j ) && anInt->HasValue( myVRow, j ) )
516 if ( nbPoints > 0 ) {
517 theHorList = new double[ nbPoints ];
518 theVerList = new double[ nbPoints ];
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 );
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() ) {
536 for ( int j = 1; j <= nbCols; j++ ) {
537 if ( aReal->HasValue( myHRow, j ) && aReal->HasValue( myVRow, j ) )
540 if ( nbPoints > 0 ) {
541 theHorList = new double[ nbPoints ];
542 theVerList = new double[ nbPoints ];
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 );
559 Creates curve Plot2d presentation object
561 Plot2d_Curve* VISU::Curve_i::CreatePresentation()
563 Plot2d_Curve* crv = new Plot2d_Curve();
564 crv->setHorTitle( GetHorTitle().c_str() );
565 string tlt = GetTitle();
566 if ( tlt.length() <= 0 )
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() );
575 int nbPoints = GetData( xList, yList );
576 if ( nbPoints > 0 && xList && yList ) {
577 crv->setData( xList, yList, nbPoints );
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;
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()));
590 crv->setTableIO(new SALOME_InteractiveObject(myTable->GetObjectEntry(),"VISU",myTable->GetName()));
594 Restores curve object from stream
596 VISU::Storable* VISU::Curve_i::Restore( const Storable::TRestoringMap& theMap, SALOMEDS::SObject_ptr theSO)
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 );
613 Flushes curve data into stream
615 void VISU::Curve_i::ToStream( std::ostringstream& theStr )
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 );
629 Gets reference table's entry
631 const char* VISU::Curve_i::GetTableID() {
632 return myTable->GetObjectEntry();
635 Called from engine to restore curve from the file
637 VISU::Storable* VISU::Curve_i::Restore(SALOMEDS::SObject_ptr theSObject,
638 const string& thePrefix, const Storable::TRestoringMap& theMap)
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);
649 void VISU::Curve_i::RemoveFromStudy(){
650 VISU::RemoveFromStudy(mySObj,false);
653 //----------------------------------------------------------------
655 //----------------------------------------------------------------
656 int VISU::Container_i::myNbPresent = 0;
657 const string VISU::Container_i::myComment = "CONTAINER";
661 const char* VISU::Container_i::GenerateName()
663 return VISU::GenerateName( "Plot2DView", ++myNbPresent );
668 const char* VISU::Container_i::GetComment() const
670 return myComment.c_str();
675 VISU::Container_i::Container_i( SALOMEDS::Study_ptr theStudy )
676 : PrsObject_i( theStudy )
682 VISU::Container_i::~Container_i()
684 MESSAGE("Container_i::~Container_i");
688 Inserts curve into the container
690 void VISU::Container_i::AddCurve( Curve_ptr theCurve )
692 if ( myStudy->_is_nil() )
694 SALOMEDS::SObject_var mySO = myStudy->FindObjectID( GetEntry() );
695 if ( mySO->_is_nil() )
697 PortableServer::POA_ptr aPOA = GetPOA();
698 Curve_i* pCurve = dynamic_cast<Curve_i*>( aPOA->reference_to_servant( theCurve ) );
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 );
711 Removes curve from the container
713 void VISU::Container_i::RemoveCurve( Curve_ptr theCurve )
715 if ( myStudy->_is_nil() )
717 SALOMEDS::SObject_var mySO = myStudy->FindObjectID( GetEntry() );
718 if ( mySO->_is_nil() )
720 PortableServer::POA_ptr aPOA = GetPOA();
721 Curve_i* pCurve = dynamic_cast<Curve_i*>( aPOA->reference_to_servant( theCurve ) );
723 QString entry = pCurve->GetEntry();
724 if ( myCurves.find( entry ) != myCurves.end() ) {
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 );
740 Gets number of curves in the container
742 CORBA::Long VISU::Container_i::GetNbCurves()
745 return myCurves.count();
750 void VISU::Container_i::Clear()
752 if ( myStudy->_is_nil() )
754 SALOMEDS::SObject_var mySO = myStudy->FindObjectID( GetEntry() );
755 if ( mySO->_is_nil() )
757 QStringList toDelete;
758 SALOMEDS::ChildIterator_var CI = myStudy->NewChildIterator( mySO );
759 for ( ; CI->More(); CI->Next() ) {
760 toDelete.append( CI->Value()->GetID() );
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 );
770 Creates container object
772 VISU::Storable* VISU::Container_i::Create()
775 myName = GenerateName();
776 // ... and build the object
777 return Build( false );
780 Builds presentation of container
782 VISU::Storable* VISU::Container_i::Build( int theRestoring )
784 if ( !theRestoring ) {
785 // looking for component
786 SALOMEDS::SComponent_var SComponent = VISU::FindOrCreateVisuComponent( myStudy );
787 // create SObject and set attributes
789 aComment.sprintf("myComment=%s;myType=%d",GetComment(),VISU::TCONTAINER);
790 string anEntry = CreateAttributes( myStudy,
798 mySObj = SALOMEDS::SObject::_duplicate(myStudy->FindObjectID(anEntry.c_str()));
803 Updates presentation of container
805 void VISU::Container_i::Update()
807 if ( myStudy->_is_nil() )
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;
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 );
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;
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 );
835 // real Curve Object doesn't exist (might be removed)
836 toDelete.append( myCurves[i] );
839 for ( i = 0; i < toDelete.count(); i++ ) {
840 myCurves.remove( toDelete[i] );
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() );
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 );
864 Gets curve from container by index
865 NB : curves are numbered from 1
867 VISU::Curve_i* VISU::Container_i::GetCurve( CORBA::Long theIndex )
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());
884 Restores container data from the stream
886 VISU::Storable* VISU::Container_i::Restore( const Storable::TRestoringMap& theMap, SALOMEDS::SObject_ptr SO )
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 );
896 Flushes container data into the stream
898 void VISU::Container_i::ToStream( std::ostringstream& theStr )
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()<<"* ";
906 Called from engine to restore container from the file
908 VISU::Storable* VISU::Container_i::Restore(SALOMEDS::SObject_ptr theSObject,
909 const string& thePrefix, const Storable::TRestoringMap& theMap)
911 SALOMEDS::Study_var aStudy = theSObject->GetStudy();
912 VISU::Container_i* pResent = new VISU::Container_i( aStudy );
913 return pResent->Restore( theMap, theSObject );
916 void VISU::Container_i::RemoveFromStudy(){
917 VISU::RemoveFromStudy(mySObj,false);
920 //-------------------------------------------------------------
921 // Implementation of reading from file
922 //-------------------------------------------------------------
923 typedef string TValue;
924 typedef vector<TValue> TValues;
932 typedef vector<TRow> TRows;
936 vector<string> myColumnUnits;
937 vector<string> myColumnTitles;
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)
951 void getColumns(TTable2D& theTable2D) const {
952 TRows& aRows = theTable2D.myRows;
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;
964 int iEnd = myRows[0].myValues.size();
965 for(int i = 0; i < iEnd; i++){
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];
973 aRows.push_back(aNewRow);
978 typedef vector<TTable2D> TTableCont;
980 int getLine(ifstream& theStmIn, QString& theString){
983 while(theStmIn.get(tmp)){
985 if(tmp == '\n') break;
988 theString = aStrOut.str();
989 return !theStmIn.eof();
992 void ImportTables(const char* theFileName, TTableCont& theTableCont){
994 QFileInfo aFileInfo( theFileName );
995 if( !aFileInfo.isFile() || !aFileInfo.isReadable() || !aFileInfo.size() )
997 aStmIn.open( theFileName );
1000 // find beginning of table (tables are separated by empty lines)
1001 while( getLine( aStmIn, aTmp ) && aTmp.stripWhiteSpace() == "");
1003 if(MYDEBUG) cout << "New table is found" << endl;
1004 while( !aStmIn.eof() && aTmp.stripWhiteSpace() != "" ){
1005 QString data = aTmp.stripWhiteSpace();
1007 QString keyword = "";
1008 // split string to data and comment (comment starts from '#' symbol)
1009 int index = aTmp.find( "#" );
1011 data = aTmp.left( index ).stripWhiteSpace();
1012 cmt = aTmp.mid( index+1 ).stripWhiteSpace();
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" ) ) {
1024 cmt = cmt.mid( index1+1 ).stripWhiteSpace();
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();
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() );
1049 if(MYDEBUG) cout << endl;
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() );
1060 if(MYDEBUG) cout << endl;
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;
1069 if(MYDEBUG) cout << "...comment: " << cmt.latin1() << endl;
1070 // simple comment processing can be here
1071 // currently it is ignored
1074 // if data is not empty, try to process it
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;
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 );
1089 if( aRow.myValues.size() > 0 )
1090 aTable2D.myRows.push_back( aRow );
1091 // ************** OLD CODE ******************
1094 istrstream aStream( data );
1095 aStream.precision( STRPRECISION );
1096 while( aStream >> aVal ) {
1097 aRow.myValues.push_back( aVal );
1099 if( aRow.myValues.size() > 0 )
1100 aTable2D.myRows.push_back( aRow );
1102 // ************** OLD CODE ******************
1104 getLine( aStmIn, aTmp );
1106 if( aTable2D.Check() ) {
1107 if(MYDEBUG) cout << "aTable2D is checked OK " << aTable2D.myTitle << endl;
1108 theTableCont.push_back( aTable2D );
1111 while( !aStmIn.eof() );
1113 if(MYDEBUG) cout << "After close" << endl;
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());
1138 aNewName.sprintf("Table:%d",i);
1139 aName->SetValue(aNewName.latin1());
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;
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);
1159 aTableOfReal->SetRowTitle(j+1,aNewTable2D.myRows[j].myTitle.c_str());
1160 aTableOfReal->SetRowUnit(j+1,aNewTable2D.myRows[j].myUnit.c_str());
1162 for(int k = 0; k < kEnd; k++)
1163 aTableOfReal->SetColumnTitle(k+1,aNewTable2D.myColumnTitles[k].c_str());
1168 template<class TTableAttr> bool ExportTableToFile(const TTableAttr& aTabAttr,
1169 const char* theFileName)
1171 if (!CORBA::is_nil(aTabAttr)) {
1172 QFile aFile(theFileName);
1173 aFile.open(IO_WriteOnly);
1175 /* extract the tabe info and write it into file */
1177 QString aTitle(aTabAttr->GetTitle()); /*Table title*/
1178 int aRowsNb = aTabAttr->GetNbRows();
1179 int aColNb = aTabAttr->GetNbColumns();
1181 SALOMEDS::StringSeq_var aRowTitles = aTabAttr->GetRowTitles();
1182 QString anAbscissTitle(aRowTitles[0]); /*Absciss row title (X coord)*/
1183 anAbscissTitle.stripWhiteSpace();
1185 SALOMEDS::StringSeq_var aRowUnits = aTabAttr->GetRowUnits();
1186 QString anAbscissUnit(aRowUnits[0]);
1187 anAbscissUnit.stripWhiteSpace();
1189 SALOMEDS::StringSeq_var aColumnTitles = aTabAttr->GetColumnTitles();
1190 if (aRowsNb > 2 && aTitle.length() ) aTitle = aTitle + " - ";
1193 for (int i = 2; i <= aRowsNb; i++ )
1196 QString anOrdinate(aRowTitles[i-1]), aTail;
1197 anOrdinate.stripWhiteSpace();
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() );
1205 if ( anAbscissTitle.length() || anOrdinate.length() ) {
1206 aLine = "#COLUMN_TITLES: " + anAbscissTitle + " | " + anOrdinate + "\n";
1207 aFile.writeBlock(aLine, aLine.length() );
1211 aLine = anAbscissUnit + " " +aRowUnits[i-1];
1212 if (!aLine.stripWhiteSpace().isEmpty()) {
1213 aLine = "#COLUMN_UNITS: " + aLine + "\n";
1214 aFile.writeBlock(aLine, aLine.length() );
1217 /* CURVE COORDINATES */
1218 for (int j = 1; j <= aColNb; j++)
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);
1232 aFile.writeBlock("\n", 1);
1240 bool VISU::ExportTableToFile(SALOMEDS::SObject_ptr theTable, const char* theFileName)
1243 SALOMEDS::GenericAttribute_var anAttr ;
1244 if (theTable->FindAttribute(anAttr, "AttributeTableOfReal"))
1246 SALOMEDS::AttributeTableOfReal_var aTabAttr = SALOMEDS::AttributeTableOfReal ::_narrow(anAttr);
1247 return ExportTableToFile ( aTabAttr , theFileName);
1250 else if (theTable->FindAttribute(anAttr, "AttributeTableOfInteger")) {
1252 SALOMEDS::AttributeTableOfInteger_var aTabAttr = SALOMEDS::AttributeTableOfInteger ::_narrow(anAttr);
1253 return ExportTableToFile ( aTabAttr , theFileName);