+
+
+//=======================================================================
+//function : updateStrForCSV
+//purpose : auxilary for ExportTableToFile
+//=======================================================================
+void updateStrForCSV(QString& aStr, const char aSep)
+{
+ int index = aStr.indexOf('"');
+ while(index>=0) {
+ aStr.insert(index,'"');
+ if( index+2 >= aStr.size() ) break;
+ index = aStr.indexOf('"',index+2);
+ }
+ index = aStr.indexOf(aSep);
+ if(index>=0) {
+ // current string contains separator => need to use "..."
+ aStr.insert(0,'"');
+ aStr.push_back('"');
+ }
+}
+
+
+//=======================================================================
+//function : ExportTableToFile
+//purpose :
+//=======================================================================
+template<class TTableAttr> bool ExportTableToFile(const TTableAttr& aTabAttr,
+ const char* theFileName)
+{
+ if (CORBA::is_nil(aTabAttr))
+ return false;
+
+ // Set "C" numeric locale to save numbers correctly
+ Kernel_Utils::Localizer loc;
+
+ QFile aFile(theFileName);
+ aFile.open(QIODevice::WriteOnly);
+
+ /* extract the table info and write it into file */
+
+ QString aTitle(aTabAttr->GetTitle()); /*Table title*/
+ int aRowsNb = aTabAttr->GetNbRows();
+ int aColNb = aTabAttr->GetNbColumns();
+
+ SALOMEDS::StringSeq_var aRowTitles = aTabAttr->GetRowTitles();
+ SALOMEDS::StringSeq_var aRowUnits = aTabAttr->GetRowUnits();
+ SALOMEDS::StringSeq_var aColumnTitles = aTabAttr->GetColumnTitles();
+
+ //--------------------------------------------------
+ // write as *.csv file if it is needed
+ //--------------------------------------------------
+ QString tmp(theFileName);
+ tmp = tmp.trimmed();
+ tmp = tmp.right(3).trimmed();
+ if( tmp == QString("csv") ) {
+ const char aSep = ',';
+ // write column titles
+ QString aLine(aRowTitles[0]);
+ updateStrForCSV(aLine,aSep);
+ for(int i=1; i<aRowsNb; i++) {
+ aLine += aSep;
+ QString aTmp(aRowTitles[i]);
+ updateStrForCSV(aTmp,aSep);
+ aLine += aTmp;
+ }
+ aLine += "\n";
+ aFile.write(aLine.toLatin1() );
+ // write table data
+ QString aValue;
+ for (int j = 1; j <= aColNb; j++) {
+ QString aLine = "";
+ if(aTabAttr->HasValue(j,1)) {
+ aLine = aValue.sprintf("%.16g",(double)aTabAttr->GetValue(1,j));
+ }
+ for (int i = 2; i <= aRowsNb; i++) {
+ if(aTabAttr->HasValue(i,j)) {
+ aLine += aSep + aValue.sprintf("%.16g",(double)aTabAttr->GetValue(i,j));
+ }
+ else aLine += aSep;
+ }
+ aLine += "\n";
+ aFile.write(aLine.toLatin1() );
+ }
+
+ aFile.close();
+ return true;
+ }
+ //--------------------------------------------------
+ // end of writing as *.csv file
+ //--------------------------------------------------
+
+ /* The given table is rare (some cells is empty) or not? */
+ bool isRareTable = false;
+ for (int i = 1; i <= aRowsNb; i++)
+ for (int j = 1; j <= aColNb && !isRareTable; j++)
+ isRareTable = !aTabAttr->HasValue(i,j);
+
+ QString aLine;
+ if (isRareTable) {
+ /* Separate the given table to 2D tables and write these ones to the file */
+ QString anAbscissTitle(aRowTitles[0]); /*Absciss row title (X coord)*/
+ anAbscissTitle.trimmed();
+ QString anAbscissUnit(aRowUnits[0]);
+ anAbscissUnit.trimmed();
+ if (aRowsNb > 2 && aTitle.length() ) aTitle = aTitle + " - ";
+
+ for (int i = 2; i <= aRowsNb; i++ )
+ {
+ /* TITLE */
+ QString anOrdinate(aRowTitles[i-1]), aTail;
+ anOrdinate.trimmed();
+
+ aLine = "#TITLE: " + aTitle +
+ ((anOrdinate.length())? anOrdinate :
+ (aRowsNb>2)? aTail.sprintf("%d",i-1) : aTail.sprintf("") ) + "\n";
+ aFile.write(aLine.toLatin1() );
+
+ /* COLUMN_TITLES */
+ if ( anAbscissTitle.length() || anOrdinate.length() ) {
+ aLine = "#COLUMN_TITLES: " + anAbscissTitle + " | " + anOrdinate;
+ int tmpind = aLine.indexOf("\n");
+ while(tmpind>=0) {
+ aLine.remove(tmpind,1);
+ tmpind = aLine.indexOf("\n");
+ }
+ aLine += "\n";
+ aFile.write(aLine.toLatin1() );
+ }
+
+ /* COLUMN_UNITS */
+ aLine = anAbscissUnit + " " +aRowUnits[i-1];
+ if (!aLine.trimmed().isEmpty()) {
+ aLine = "#COLUMN_UNITS: " + aLine + "\n";
+ aFile.write(aLine.toLatin1() );
+ }
+
+ /* CURVE COORDINATES */
+ for (int j = 1; j <= aColNb; j++)
+ {
+ if ( aTabAttr -> HasValue(i,j) && aTabAttr -> HasValue(1, j)) {
+ aLine = aLine.sprintf("%.16g %.16g",
+ (double)(aTabAttr->GetValue(1,j)),
+ (double)(aTabAttr->GetValue(i,j))); /* aTabAttr->GetValue(1,j) - X coord */
+ if ( !aLine.trimmed().isEmpty() ) {
+ QString aColTitle(aColumnTitles[j-1]);
+ if ( !aColTitle.trimmed().isEmpty() )
+ aLine = aLine + " #TITLE: " + aColTitle ;
+ aFile.write(QString(aLine + "\n").toLatin1() );
+ }
+ }
+ }
+ aFile.write("\n", 1);
+ }
+ }//end of if (isRareTable)
+ else {
+ /* Write the table in the file without separating */
+ /* TITLE */
+ aLine = "#TITLE: " + aTitle + "\n";
+ aFile.write(aLine.toLatin1());
+
+ /* COLUMN_TITLES and COLUMN_UNITS */
+ QString aTitlesSep = "";
+ QString aUnitsSep = "";
+ QString aTitlesStr = "#COLUMN_TITLES: ";
+ QString aUnitsStr = "#COLUMN_UNITS: ";
+ for (int i = 1; i <= aRowsNb; i++) {
+ if (!QString(aRowTitles[i-1]).trimmed().isEmpty()) {
+ aTitlesStr += (aTitlesSep + aRowTitles[i-1]);
+ if (aTitlesSep.isEmpty()) aTitlesSep = " | ";
+ }
+ if (!QString(aRowUnits[i-1]).trimmed().isEmpty()) {
+ aUnitsStr += (aUnitsSep + aRowUnits[i-1]);
+ if (aUnitsSep.isEmpty()) aUnitsSep = " ";
+ }
+ }
+ int tmpind = aTitlesStr.indexOf("\n");
+ while(tmpind>=0) {
+ aTitlesStr.remove(tmpind,1);
+ tmpind = aTitlesStr.indexOf("\n");
+ }
+ aTitlesStr += "\n";
+ aUnitsStr += "\n";
+ aFile.write(aTitlesStr.toLatin1());
+ aFile.write(aUnitsStr.toLatin1());
+
+ /* CURVE COORDINATES */
+ QString aSep, aValue, aColTitle;
+ for (int j = 1; j <= aColNb; j++) {
+ aLine = ""; aSep = "";
+ for (int i = 1; i <= aRowsNb; i++) {
+ aLine += (aSep + aValue.sprintf("%.16g", (double)(aTabAttr->GetValue(i,j))));
+ if (aSep.isEmpty()) aSep = " ";
+ }
+ if (!aLine.trimmed().isEmpty()) {
+ aColTitle = aColumnTitles[j-1];
+ if (!aColTitle.trimmed().isEmpty())
+ aLine = aLine + " #TITLE: " + aColTitle;
+ aLine += "\n";
+ aFile.write(aLine.toLatin1());
+ }
+ }
+ } //end of else
+
+ aFile.close();
+ return true;
+}
+
+bool VISU::ExportTableToFile(SALOMEDS::SObject_ptr theTable, const char* theFileName)
+{
+ //Find table
+ SALOMEDS::GenericAttribute_var anAttr ;
+ if (theTable->FindAttribute(anAttr, "AttributeTableOfReal")) {
+ SALOMEDS::AttributeTableOfReal_var aTabAttr =
+ SALOMEDS::AttributeTableOfReal ::_narrow(anAttr);
+ return ExportTableToFile ( aTabAttr , theFileName);
+ }
+ else if (theTable->FindAttribute(anAttr, "AttributeTableOfInteger")) {
+ SALOMEDS::AttributeTableOfInteger_var aTabAttr =
+ SALOMEDS::AttributeTableOfInteger ::_narrow(anAttr);
+ return ExportTableToFile ( aTabAttr , theFileName);
+ }
+ return false;
+}