Salome HOME
Merge branch 'V8_4_BR'
[modules/smesh.git] / src / SMESH_I / SMESH_DumpPython.cxx
index 3cb64eedf920e84b3eae8e68ee6e28068130e036..7ca8be9be0cba1b1d31f1371a0b73d7628a58ef3 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -6,7 +6,7 @@
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 // License as published by the Free Software Foundation; either
-// version 2.1 of the License.
+// version 2.1 of the License, or (at your option) any later version.
 //
 // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -53,10 +53,10 @@ namespace SMESH
   size_t TPythonDump::myCounter = 0;
   const char theNotPublishedObjectName[] = "__NOT__Published__Object__";
 
-  TVar::TVar(CORBA::Double value):myVals(1) { myVals[0] = SMESH_Comment(value); }
-  TVar::TVar(CORBA::Long   value):myVals(1) { myVals[0] = SMESH_Comment(value); }
-  TVar::TVar(CORBA::Short  value):myVals(1) { myVals[0] = SMESH_Comment(value); }
-  TVar::TVar(const SMESH::double_array& value):myVals(value.length())
+  TVar::TVar(CORBA::Double value):myVals(1), myIsList(false) { myVals[0] = SMESH_Comment(value); }
+  TVar::TVar(CORBA::Long   value):myVals(1), myIsList(false) { myVals[0] = SMESH_Comment(value); }
+  TVar::TVar(CORBA::Short  value):myVals(1), myIsList(false) { myVals[0] = SMESH_Comment(value); }
+  TVar::TVar(const SMESH::double_array& value):myVals(value.length()), myIsList(true)
   {
     for ( size_t i = 0; i < value.length(); i++)
       myVals[i] = SMESH_Comment(value[i]);
@@ -93,12 +93,12 @@ namespace SMESH
   operator<<(const TVar& theVarValue)
   {
     const std::vector< int >& varIDs = SMESH_Gen_i::GetSMESHGen()->GetLastParamIndices();
-    if ( theVarValue.myVals.size() != 1 )
+    if ( theVarValue.myIsList )
     {
       myStream << "[ ";
       for ( size_t i = 1; i <= theVarValue.myVals.size(); ++i )
       {
-        if ( myVarsCounter < varIDs.size() && varIDs[ myVarsCounter ] >= 0 )
+        if ( myVarsCounter < (int)varIDs.size() && varIDs[ myVarsCounter ] >= 0 )
           myStream << TVar::Quote() << varIDs[ myVarsCounter ] << TVar::Quote();
         else
           myStream << theVarValue.myVals[i-1];
@@ -110,7 +110,7 @@ namespace SMESH
     }
     else
     {
-      if ( myVarsCounter < varIDs.size() && varIDs[ myVarsCounter ] >= 0 )
+      if ( myVarsCounter < (int)varIDs.size() && varIDs[ myVarsCounter ] >= 0 )
         myStream << TVar::Quote() << varIDs[ myVarsCounter ] << TVar::Quote();
       else
         myStream << theVarValue.myVals[0];
@@ -162,6 +162,13 @@ namespace SMESH
     return *this;
   }
 
+  TPythonDump&
+  TPythonDump::
+  operator<<(const std::string& theArg){
+    myStream<<theArg;
+    return *this;
+  }
+
   TPythonDump&
   TPythonDump::
   operator<<(const SMESH::ElementType& theArg)
@@ -227,6 +234,7 @@ namespace SMESH
     case Entity_TriQuad_Hexa:      myStream<<"Entity_TriQuad_Hexa";      break;
     case Entity_Penta:             myStream<<"Entity_Penta";             break;
     case Entity_Quad_Penta:        myStream<<"Entity_Quad_Penta";        break;
+    case Entity_BiQuad_Penta:      myStream<<"Entity_BiQuad_Penta";      break;
     case Entity_Hexagonal_Prism:   myStream<<"Entity_Hexagonal_Prism";   break;
     case Entity_Polyhedra:         myStream<<"Entity_Polyhedra";         break;
     case Entity_Quad_Polyhedra:    myStream<<"Entity_Quad_Polyhedra";    break;
@@ -240,13 +248,20 @@ namespace SMESH
   template<class TArray>
   void DumpArray(const TArray& theArray, TPythonDump & theStream)
   {
-    theStream << "[ ";
-    for (int i = 1; i <= theArray.length(); i++) {
-      theStream << theArray[i-1];
-      if ( i < theArray.length() )
-        theStream << ", ";
+    if ( theArray.length() == 0 )
+    {
+      theStream << "[]";
+    }
+    else
+    {
+      theStream << "[ ";
+      for (CORBA::ULong i = 1; i <= theArray.length(); i++) {
+        theStream << theArray[i-1];
+        if ( i < theArray.length() )
+          theStream << ", ";
+      }
+      theStream << " ]";
     }
-    theStream << " ]";
   }
 
   TPythonDump&
@@ -263,11 +278,18 @@ namespace SMESH
     return *this;
   }
 
+  TPythonDump&
+  TPythonDump::operator<<(const SMESH::nodes_array& theArg)
+  {
+    DumpArray( theArg, *this );
+    return *this;
+  }
+
   TPythonDump&
   TPythonDump::operator<<(const SMESH::string_array& theArray)
   {
     myStream << "[ ";
-    for (int i = 1; i <= theArray.length(); i++) {
+    for ( CORBA::ULong i = 1; i <= theArray.length(); i++ ) {
       myStream << "'" << theArray[i-1] << "'";
       if ( i < theArray.length() )
         myStream << ", ";
@@ -409,6 +431,9 @@ namespace SMESH
       case FT_MultiConnection2D:     myStream<< "aMultiConnection2D";     break;
       case FT_Length:                myStream<< "aLength";                break;
       case FT_Length2D:              myStream<< "aLength2D";              break;
+      case FT_Deflection2D:          myStream<< "aDeflection2D";          break;
+      case FT_NodeConnectivityNumber:myStream<< "aNodeConnectivityNumber";break;
+      case FT_BelongToMeshGroup:     myStream<< "aBelongToMeshGroup";     break;
       case FT_BelongToGeom:          myStream<< "aBelongToGeom";          break;
       case FT_BelongToPlane:         myStream<< "aBelongToPlane";         break;
       case FT_BelongToCylinder:      myStream<< "aBelongToCylinder";      break;
@@ -433,8 +458,8 @@ namespace SMESH
       case FT_LogicalNOT:            myStream<< "aLogicalNOT";            break;
       case FT_LogicalAND:            myStream<< "aLogicalAND";            break;
       case FT_LogicalOR:             myStream<< "aLogicalOR";             break;
-      case FT_Undefined:
-      default:                       myStream<< "anUndefined";            break;
+      case FT_Undefined:             myStream<< "anUndefined";            break;
+        //default: -- commented to have a compilation warning
       }
       myStream<<theArg;
     }
@@ -471,6 +496,17 @@ namespace SMESH
     switch (theVersion) {
     case SMESH::MED_V2_1: myStream << "SMESH.MED_V2_1"; break;
     case SMESH::MED_V2_2: myStream << "SMESH.MED_V2_2"; break;
+    case SMESH::MED_LATEST: myStream << "SMESH.MED_LATEST"; break;
+    case SMESH::MED_MINOR_0: myStream << "SMESH.MED_MINOR_0"; break;
+    case SMESH::MED_MINOR_1: myStream << "SMESH.MED_MINOR_1"; break;
+    case SMESH::MED_MINOR_2: myStream << "SMESH.MED_MINOR_2"; break;
+    case SMESH::MED_MINOR_3: myStream << "SMESH.MED_MINOR_3"; break;
+    case SMESH::MED_MINOR_4: myStream << "SMESH.MED_MINOR_4"; break;
+    case SMESH::MED_MINOR_5: myStream << "SMESH.MED_MINOR_5"; break;
+    case SMESH::MED_MINOR_6: myStream << "SMESH.MED_MINOR_6"; break;
+    case SMESH::MED_MINOR_7: myStream << "SMESH.MED_MINOR_7"; break;
+    case SMESH::MED_MINOR_8: myStream << "SMESH.MED_MINOR_8"; break;
+    case SMESH::MED_MINOR_9: myStream << "SMESH.MED_MINOR_9"; break;
     default: myStream << theVersion;
     }
     return *this;
@@ -517,11 +553,54 @@ namespace SMESH
     DumpArray( *theList, *this );
     return *this;
   }
+  TPythonDump& TPythonDump::operator<<(const GEOM::ListOfGO& theList)
+  {
+    DumpArray( theList, *this );
+    return *this;
+  }
+  TPythonDump& TPythonDump::operator<<(const GEOM::ListOfGBO& theList)
+  {
+    DumpArray( theList, *this );
+    return *this;
+  }
   TPythonDump& TPythonDump::operator<<(const SMESH::ListOfIDSources& theList)
   {
     DumpArray( theList, *this );
     return *this;
   }
+  TPythonDump& TPythonDump::operator<<(const SMESH::CoincidentFreeBorders& theCFB)
+  {
+    // dump CoincidentFreeBorders as a list of lists, each enclosed list
+    // contains node IDs of a group of coincident free borders where
+    // each consequent triple of IDs describe a free border: (n1, n2, nLast)
+    // For example [[1, 2, 10, 20, 21, 40], [11, 12, 15, 55, 54, 41]] describes
+    // two groups of coincident free borders, each group including two borders
+
+    myStream << "[";
+    for ( CORBA::ULong i = 0; i < theCFB.coincidentGroups.length(); ++i )
+    {
+      const SMESH::FreeBordersGroup& aGRP = theCFB.coincidentGroups[ i ];
+      if ( i ) myStream << ",";
+      myStream << "[";
+      for ( CORBA::ULong iP = 0; iP < aGRP.length(); ++iP )
+      {
+        const SMESH::FreeBorderPart& aPART = aGRP[ iP ];
+        if ( 0 <= aPART.border && aPART.border < (CORBA::Long)theCFB.borders.length() )
+        {
+          if ( iP ) myStream << ", ";
+          const SMESH::FreeBorder& aBRD = theCFB.borders[ aPART.border ];
+          myStream << aBRD.nodeIDs[ aPART.node1    ] << ",";
+          myStream << aBRD.nodeIDs[ aPART.node2    ] << ",";
+          myStream << aBRD.nodeIDs[ aPART.nodeLast ];
+        }
+      }
+      myStream << "]";
+    }
+    myStream << "]";
+
+    return *this;
+  }
+
   const char* TPythonDump::NotPublishedObjectName()
   {
     return theNotPublishedObjectName;
@@ -949,11 +1028,11 @@ TCollection_AsciiString SMESH_Gen_i::DumpPython_impl
   std::list< TCollection_AsciiString >::iterator linesIt;
   
   if ( isPublished )
-    lines.push_back(  tab + aSMESHGen + " = smeshBuilder.New(theStudy)" );
+    lines.push_back(  aSMESHGen + " = smeshBuilder.New(theStudy)" );
    else
-    lines.push_back(  tab + aSMESHGen + " = smeshBuilder.New(None)" );
-  lines.push_back(  tab + "aFilterManager = " + aSMESHGen + ".CreateFilterManager()" );
-  lines.push_back(  tab + "aMeasurements = "  + aSMESHGen + ".CreateMeasurements()" );
+    lines.push_back(  aSMESHGen + " = smeshBuilder.New(None)" );
+  lines.push_back( helper + "aFilterManager = " + aSMESHGen + ".CreateFilterManager()" );
+  lines.push_back( helper + "aMeasurements = "  + aSMESHGen + ".CreateMeasurements()" );
 
   // Treat dump trace of restored study
   if (theSavedTrace.Length() > 0)
@@ -969,7 +1048,7 @@ TCollection_AsciiString SMESH_Gen_i::DumpPython_impl
         lines.push_back( theSavedTrace.SubString( from, to - 1 ));
       from = to + 1;
     }
-    // For the convertion of IDL API calls -> smeshBuilder.py API, "smesh" standing for SMESH_Gen
+    // For the conversion of IDL API calls -> smeshBuilder.py API, "smesh" standing for SMESH_Gen
     // was replaces with "smeshgen" (==TPythonDump::SMESHGenName()).
     // Change "smesh" -> "smeshgen" in the trace saved before passage to smeshBuilder.py API
     bool isNewVersion =
@@ -1121,9 +1200,11 @@ TCollection_AsciiString SMESH_Gen_i::DumpPython_impl
   if ( importGeom && isMultiFile )
   {
     initPart += ("\n## import GEOM dump file ## \n"
-                 "import string, os, sys, re\n"
-                 "sys.path.insert( 0, os.path.dirname(__file__) )\n"
-                 "exec(\"from \"+re.sub(\"SMESH$\",\"GEOM\",__name__)+\" import *\")\n");
+                 "import string, os, sys, re, inspect\n"
+                 "thisFile   = inspect.getfile( inspect.currentframe() )\n"
+                 "thisModule = os.path.splitext( os.path.basename( thisFile ))[0]\n"
+                 "sys.path.insert( 0, os.path.dirname( thisFile ))\n"
+                 "exec(\"from \"+re.sub(\"SMESH$\",\"GEOM\",thisModule)+\" import *\")\n\n");
   }
   // import python files corresponding to plugins if they are used in anUpdatedScript
   {
@@ -1149,7 +1230,7 @@ TCollection_AsciiString SMESH_Gen_i::DumpPython_impl
       initPart += importStr + "\n";
   }
 
-  if( isMultiFile )
+  if ( isMultiFile )
     initPart += "def RebuildData(theStudy):";
   initPart += "\n";
 
@@ -1185,6 +1266,7 @@ TCollection_AsciiString SMESH_Gen_i::DumpPython_impl
     if ( theNames.IsBound( anEntry ))
     {
       aGUIName = theNames.Find(anEntry);
+      aGUIName.RemoveAll('\''); // remove a quote from a name (issue 22360)
       setNamePart += nt + aSMESHGen + ".SetName(" + aName;
       if ( anEntry2AccessorMethod.IsBound( anEntry ) )
         setNamePart += helper + "." + anEntry2AccessorMethod( anEntry );
@@ -1212,8 +1294,18 @@ TCollection_AsciiString SMESH_Gen_i::DumpPython_impl
 
   anUpdatedScript += removeObjPart + '\n' + setNamePart + '\n' + visualPropertiesPart;
 
-  if( isMultiFile )
-    anUpdatedScript += "\n\tpass";
+  if ( isMultiFile )
+  {
+    anUpdatedScript +=
+      "\n\tpass"
+      "\n"
+      "\nif __name__ == '__main__':"
+      "\n\tSMESH_RebuildData = RebuildData"
+      "\n\texec('import '+re.sub('SMESH$','GEOM',thisModule)+' as GEOM_dump')"
+      "\n\tGEOM_dump.RebuildData( salome.myStudy )"
+      "\n\texec('from '+re.sub('SMESH$','GEOM',thisModule)+' import * ')"
+      "\n\tSMESH_RebuildData( salome.myStudy )";
+  }
   anUpdatedScript += "\n";
 
   // no need now as we use 'tab' and 'nt' variables depending on isMultiFile
@@ -1226,7 +1318,7 @@ TCollection_AsciiString SMESH_Gen_i::DumpPython_impl
 
   TCollection_AsciiString aLongString, aFunctionType;
   int where = 1;
-  set< string > functionNameSet;
+  std::set< std::string > functionNameSet;
   while ( SMESH::TPythonDump::CutoutLongString( anUpdatedScript, where, aLongString, aFunctionType ))
   {
     // make a python string literal
@@ -1241,7 +1333,7 @@ TCollection_AsciiString SMESH_Gen_i::DumpPython_impl
     {
       // find the function name
       int functBeg = posAlready;
-      char* script = (char*) anUpdatedScript.ToCString() + posAlready - 1; // look at ":" after "def fuction()"
+      char* script = (char*) anUpdatedScript.ToCString() + posAlready - 1; // look at ":" after "def function()"
       while ( *script != ' ' ) {
         script--;
         functBeg--;