Salome HOME
SALOME Forum bug: http://www.salome-platform.org/forum/forum_10/967838025
[modules/smesh.git] / src / SMESH_I / SMESH_DumpPython.cxx
index 34c1c32ccc0306c62fa7bc348745419eca1d13d1..3a5af32a34f7fb84e8440e8ecc89c176e988e979 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2013  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
@@ -36,6 +36,7 @@
 
 #include <TColStd_HSequenceOfInteger.hxx>
 #include <TCollection_AsciiString.hxx>
+#include <LDOMParser.hxx>
 
 #ifdef _DEBUG_
 static int MYDEBUG = 0;
@@ -43,15 +44,11 @@ static int MYDEBUG = 0;
 static int MYDEBUG = 0;
 #endif
 
-static TCollection_AsciiString NotPublishedObjectName()
-{
-  return "__NOT__Published__Object__";
-}
-
 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); }
@@ -75,26 +72,31 @@ namespace SMESH
       std::string aString = myStream.str();
       TCollection_AsciiString aCollection(Standard_CString(aString.c_str()));
       SALOMEDS::Study_var aStudy = aSMESHGen->GetCurrentStudy();
-      if(!aStudy->_is_nil() && !aCollection.IsEmpty()){
+      if(!aStudy->_is_nil() && !aCollection.IsEmpty())
+      {
+        const std::string & objEntry = SMESH_Gen_i::GetSMESHGen()->GetLastObjEntry();
+        if ( !objEntry.empty() )
+          aCollection += (TVar::ObjPrefix() + objEntry ).c_str();
         aSMESHGen->AddToPythonScript(aStudy->StudyId(),aCollection);
         if(MYDEBUG) MESSAGE(aString);
-        aSMESHGen->UpdateParameters(""); // prevent misuse of already treated variables
+        // prevent misuse of already treated variables
+        aSMESHGen->UpdateParameters(CORBA::Object_var().in(),"");
       }
     }
   }
 
-  TPythonDump& //!< to store a variable value
+  TPythonDump& //!< store a variable value. Write either a value or '$varID$'
   TPythonDump::
   operator<<(const TVar& theVarValue)
   {
-    const std::vector< std::string >& varNames = SMESH_Gen_i::GetSMESHGen()->GetLastParameters();
+    const std::vector< int >& varIDs = SMESH_Gen_i::GetSMESHGen()->GetLastParamIndices();
     if ( theVarValue.myVals.size() != 1 )
     {
       myStream << "[ ";
       for ( size_t i = 1; i <= theVarValue.myVals.size(); ++i )
       {
-        if ( myVarsCounter < varNames.size() && !varNames[ myVarsCounter ].empty() )
-          myStream << TVar::Quote() << varNames[ myVarsCounter ] << TVar::Quote();
+        if ( myVarsCounter < varIDs.size() && varIDs[ myVarsCounter ] >= 0 )
+          myStream << TVar::Quote() << varIDs[ myVarsCounter ] << TVar::Quote();
         else
           myStream << theVarValue.myVals[i-1];
         if ( i < theVarValue.myVals.size() )
@@ -105,8 +107,8 @@ namespace SMESH
     }
     else
     {
-      if ( myVarsCounter < varNames.size() && !varNames[ myVarsCounter ].empty() )
-        myStream << TVar::Quote() << varNames[ myVarsCounter ] << TVar::Quote();
+      if ( myVarsCounter < varIDs.size() && varIDs[ myVarsCounter ] >= 0 )
+        myStream << TVar::Quote() << varIDs[ myVarsCounter ] << TVar::Quote();
       else
         myStream << theVarValue.myVals[0];
       ++myVarsCounter;
@@ -207,6 +209,7 @@ namespace SMESH
     case Entity_Quad_Edge:         myStream<<"Entity_Quad_Edge";         break;
     case Entity_Triangle:          myStream<<"Entity_Triangle";          break;
     case Entity_Quad_Triangle:     myStream<<"Entity_Quad_Triangle";     break;
+    case Entity_BiQuad_Triangle:   myStream<<"Entity_BiQuad_Triangle";   break;
     case Entity_Quadrangle:        myStream<<"Entity_Quadrangle";        break;
     case Entity_Quad_Quadrangle:   myStream<<"Entity_Quad_Quadrangle";   break;
     case Entity_BiQuad_Quadrangle: myStream<<"Entity_BiQuad_Quadrangle"; break;
@@ -279,7 +282,7 @@ namespace SMESH
       myStream << entry.in();
     }
     else {
-      myStream << NotPublishedObjectName();
+      myStream << theNotPublishedObjectName;
     }
     return *this;
   }
@@ -298,7 +301,7 @@ namespace SMESH
       if ( aSMESHGen->CanPublishInStudy( theArg )) // not published SMESH object
         myStream << "smeshObj_" << size_t(theArg);
       else
-        myStream << NotPublishedObjectName();
+        myStream << theNotPublishedObjectName;
     }
     else
       myStream << "None";
@@ -314,7 +317,7 @@ namespace SMESH
     if(aSObject->_is_nil() && !CORBA::is_nil(theArg))
       myStream << "hyp_" << theArg->GetId();
     else
-      *this << CORBA::Object_ptr( theArg );
+      *this << aSObject;
     return *this;
   }
 
@@ -343,7 +346,7 @@ namespace SMESH
       SMESH::ElementType type = types->length() ? types[0] : SMESH::ALL;
       return *this << mesh << ".GetIDSource(" << anElementsId << ", " << type << ")";
     }
-    return *this;
+    return *this << theNotPublishedObjectName;
   }
 
   TPythonDump&
@@ -376,9 +379,9 @@ namespace SMESH
   {
     if ( theArg ) {
       FunctorType aFunctorType = theArg->GetFunctorType();
-      switch(aFunctorType){
-      case FT_AspectRatio:           myStream<< "anAspectRatio";          break;
-      case FT_AspectRatio3D:         myStream<< "anAspectRatio3D";        break;
+      switch(aFunctorType) {
+      case FT_AspectRatio:           myStream<< "aAspectRatio";           break;
+      case FT_AspectRatio3D:         myStream<< "aAspectRatio3D";         break;
       case FT_Warping:               myStream<< "aWarping";               break;
       case FT_MinimumAngle:          myStream<< "aMinimumAngle";          break;
       case FT_Taper:                 myStream<< "aTaper";                 break;
@@ -391,6 +394,10 @@ namespace SMESH
       case FT_FreeEdges:             myStream<< "aFreeEdges";             break;
       case FT_FreeNodes:             myStream<< "aFreeNodes";             break;
       case FT_FreeFaces:             myStream<< "aFreeFaces";             break;
+      case FT_EqualNodes:            myStream<< "aEqualNodes";            break;
+      case FT_EqualEdges:            myStream<< "aEqualEdges";            break;
+      case FT_EqualFaces:            myStream<< "aEqualFaces";            break;
+      case FT_EqualVolumes:          myStream<< "aEqualVolumes";          break;
       case FT_MultiConnection:       myStream<< "aMultiConnection";       break;
       case FT_MultiConnection2D:     myStream<< "aMultiConnection2D";     break;
       case FT_Length:                myStream<< "aLength";                break;
@@ -400,7 +407,6 @@ namespace SMESH
       case FT_BelongToCylinder:      myStream<< "aBelongToCylinder";      break;
       case FT_BelongToGenSurface:    myStream<< "aBelongToGenSurface";    break;
       case FT_LyingOnGeom:           myStream<< "aLyingOnGeom";           break;
-      case FT_CoplanarFaces:         myStream<< "aCoplanarFaces";         break;
       case FT_RangeOfIds:            myStream<< "aRangeOfIds";            break;
       case FT_BadOrientedVolume:     myStream<< "aBadOrientedVolume";     break;
       case FT_BareBorderVolume:      myStream<< "aBareBorderVolume";      break;
@@ -409,10 +415,14 @@ namespace SMESH
       case FT_OverConstrainedFace:   myStream<< "aOverConstrainedFace";   break;
       case FT_LinearOrQuadratic:     myStream<< "aLinearOrQuadratic";     break;
       case FT_GroupColor:            myStream<< "aGroupColor";            break;
-      case FT_ElemGeomType:          myStream<< "anElemGeomType";         break;
+      case FT_ElemGeomType:          myStream<< "aElemGeomType";          break;
+      case FT_EntityType:            myStream<< "aEntityType";            break;
+      case FT_CoplanarFaces:         myStream<< "aCoplanarFaces";         break;
+      case FT_BallDiameter:          myStream<< "aBallDiameter";          break;
+      case FT_ConnectedElements:     myStream<< "aConnectedElements";     break;
       case FT_LessThan:              myStream<< "aLessThan";              break;
       case FT_MoreThan:              myStream<< "aMoreThan";              break;
-      case FT_EqualTo:               myStream<< "anEqualTo";              break;
+      case FT_EqualTo:               myStream<< "aEqualTo";               break;
       case FT_LogicalNOT:            myStream<< "aLogicalNOT";            break;
       case FT_LogicalAND:            myStream<< "aLogicalAND";            break;
       case FT_LogicalOR:             myStream<< "aLogicalOR";             break;
@@ -505,6 +515,10 @@ namespace SMESH
     DumpArray( theList, *this );
     return *this;
   }
+  const char* TPythonDump::NotPublishedObjectName()
+  {
+    return theNotPublishedObjectName;
+  }
 
   TCollection_AsciiString myLongStringStart( "TPythonDump::LongStringStart" );
   TCollection_AsciiString myLongStringEnd  ( "TPythonDump::LongStringEnd" );
@@ -657,7 +671,6 @@ Engines::TMPFile* SMESH_Gen_i::DumpPython (CORBA::Object_ptr theStudy,
   }
 
   // Get trace of restored study
-  //SALOMEDS::SObject_wrap aSO = SMESH_Gen_i::ObjectToSObject(theStudy, _this());
   SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
   SALOMEDS::GenericAttribute_wrap anAttr =
     aStudyBuilder->FindOrCreateAttribute(aSO, "AttributePythonObject");
@@ -679,7 +692,7 @@ Engines::TMPFile* SMESH_Gen_i::DumpPython (CORBA::Object_ptr theStudy,
   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)aBuffer;
   Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aLen+1, aLen+1, anOctetBuf, 1);
 
-  bool hasNotPublishedObjects = aScript.Location( NotPublishedObjectName(), 1, aLen);
+  bool hasNotPublishedObjects = aScript.Location( SMESH::theNotPublishedObjectName, 1, aLen);
   isValidScript = isValidScript && !hasNotPublishedObjects;
 
   return aStreamFile._retn();
@@ -834,6 +847,45 @@ namespace {
     }
     return isValidName;
   }
+
+  //================================================================================
+  /*!
+   * \brief Return Python module names of available plug-ins.
+   */
+  //================================================================================
+
+  std::vector<std::string> getPluginNames()
+  {
+    std::vector<std::string> pluginNames;
+    std::vector< std::string > xmlPaths = SMESH_Gen::GetPluginXMLPaths();
+    LDOMParser xmlParser;
+    for ( size_t i = 0; i < xmlPaths.size(); ++i )
+    {
+      bool error = xmlParser.parse( xmlPaths[i].c_str() );
+      if ( error )
+      {
+        TCollection_AsciiString data;
+        INFOS( xmlParser.GetError(data) );
+        continue;
+      }
+      // <meshers-group name="Standard Meshers"
+      //                resources="StdMeshers"
+      //                idl-module="StdMeshers"
+      //                server-lib="StdMeshersEngine"
+      //                gui-lib="StdMeshersGUI">
+      LDOM_Document xmlDoc   = xmlParser.getDocument();
+      LDOM_NodeList nodeList = xmlDoc.getElementsByTagName( "meshers-group" );
+      for ( int i = 0; i < nodeList.getLength(); ++i )
+      {
+        LDOM_Node       node = nodeList.item( i );
+        LDOM_Element&   elem = (LDOM_Element&) node;
+        LDOMString idlModule = elem.getAttribute( "idl-module" );
+        if ( strlen( idlModule.GetString() ) > 0 )
+          pluginNames.push_back( idlModule.GetString() );
+      }
+    }
+    return pluginNames;
+  }
 }
 
 //=============================================================================
@@ -861,29 +913,20 @@ TCollection_AsciiString SMESH_Gen_i::DumpPython_impl
   TCollection_AsciiString aScript;
   if( isMultiFile )
     aScript += "def RebuildData(theStudy):";
+
   aScript += "\n\t";
-  aScript += helper + "aFilterManager = " + aSMESHGen + ".CreateFilterManager()\n\t";
-  aScript += helper + "aMeasurements = " + aSMESHGen + ".CreateMeasurements()\n\t";
   if ( isPublished )
-    aScript += aSMESHGen + ".SetCurrentStudy(theStudy)";
+    aScript += aSMESHGen + " = smeshBuilder.New(theStudy)\n\t";
   else
-    aScript += aSMESHGen + ".SetCurrentStudy(None)";
-
-  // import python files corresponding to plugins
-  set<string> moduleNameSet;
-  map<string, GenericHypothesisCreator_i*>::iterator hyp_creator = myHypCreatorMap.begin();
-  for ( ; hyp_creator != myHypCreatorMap.end(); ++hyp_creator ) {
-    string moduleName = hyp_creator->second->GetModuleName();
-    bool newModule = moduleNameSet.insert( moduleName ).second;
-    if ( newModule )
-      aScript += helper + "\n\t" + "import " + (char*) moduleName.c_str();
-  }
+    aScript += aSMESHGen + " = smeshBuilder.New(None)\n\t";
+  aScript += helper + "aFilterManager = " + aSMESHGen + ".CreateFilterManager()\n\t";
+  aScript += helper + "aMeasurements = " + aSMESHGen + ".CreateMeasurements()\n\t";
 
   // Dump trace of restored study
   if (theSavedTrace.Length() > 0) {
-    // For the convertion of IDL API calls -> smesh.py API, "smesh" standing for SMESH_Gen
+    // For the convertion 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 smesh.py API
+    // Change "smesh" -> "smeshgen" in the trace saved before passage to smeshBuilder.py API
     bool isNewVersion =
       theSavedTrace.Location( anOldGen + ".", 1, theSavedTrace.Length() );
     if ( !isNewVersion ) {
@@ -911,13 +954,39 @@ TCollection_AsciiString SMESH_Gen_i::DumpPython_impl
     aScript += helper + "\n" + aNewLines;
   }
 
-  // Convert IDL API calls into smesh.py API.
+  // import python files corresponding to plugins if they are used in aScript
+  {
+    TCollection_AsciiString importStr;
+    std::vector<std::string> pluginNames = getPluginNames();
+    for ( size_t i = 0; i < pluginNames.size(); ++i )
+    {
+      // Convert access to plugin members:
+      // e.g. StdMeshers.QUAD_REDUCED -> StdMeshersBuilder.QUAD_REDUCED
+      TCollection_AsciiString pluginAccess = (pluginNames[i] + ".").c_str() ;
+      int iFrom = 1, iPos;
+      while (( iPos = aScript.Location( pluginAccess, iFrom, aScript.Length() )))
+      {
+        aScript.Insert( iPos + pluginNames[i].size(), "Builder" );
+        iFrom = iPos + pluginNames[i].size() + 8;
+      }
+      // if any plugin member is used, import the plugin
+      if ( iFrom > 1 )
+        importStr += ( helper + "\n\t" + "from salome." + (char*) pluginNames[i].c_str() +
+                       " import " + (char*) pluginNames[i].c_str() +"Builder" );
+    }
+    if ( !importStr.IsEmpty() )
+      aScript.Insert( 1, importStr + "\n\t" );
+  }
+
+  // Convert IDL API calls into smeshBuilder.py API.
   // Some objects are wrapped with python classes and
   // Resource_DataMapOfAsciiStringAsciiString holds methods returning wrapped objects
   Resource_DataMapOfAsciiStringAsciiString anEntry2AccessorMethod;
+  std::set< TCollection_AsciiString >      aRemovedObjIDs;
   if ( !getenv("NO_2smeshpy_conversion"))
     aScript = SMESH_2smeshpy::ConvertScript( aScript, anEntry2AccessorMethod,
-                                             theObjectNames, theStudy, isHistoricalDump );
+                                             theObjectNames, aRemovedObjIDs,
+                                             theStudy, isHistoricalDump );
 
   // Replace characters used instead of quote marks to quote notebook variables
   {
@@ -979,7 +1048,8 @@ TCollection_AsciiString SMESH_Gen_i::DumpPython_impl
         do {
           aName = aBaseName + (++objectCounter);
         } while (theObjectNames.IsBound(aName));
-        seqRemoved.Append(aName);
+        if ( !aRemovedObjIDs.count( anEntry ))
+          seqRemoved.Append(aName);
         mapRemoved.Bind(anEntry, "1");
         theObjectNames.Bind(anEntry, aName);
       }
@@ -997,7 +1067,8 @@ TCollection_AsciiString SMESH_Gen_i::DumpPython_impl
   TCollection_AsciiString initPart = "import ";
   if ( isMultiFile )
     initPart += helper + "salome, ";
-  initPart += aSmeshpy + ", SMESH, SALOMEDS\n";
+  initPart += " SMESH, SALOMEDS\n";
+  initPart += "from salome.smesh import smeshBuilder\n";
   if ( importGeom && isMultiFile )
   {
     initPart += ("\n## import GEOM dump file ## \n"
@@ -1019,7 +1090,7 @@ TCollection_AsciiString SMESH_Gen_i::DumpPython_impl
   for (int ir = 1; ir <= seqRemoved.Length(); ir++) {
     anUpdatedScript += "\n\tSO = theStudy.FindObjectIOR(theStudy.ConvertObjectToIOR(";
     anUpdatedScript += seqRemoved.Value(ir);
-    // for object wrapped by class of smesh.py
+    // for object wrapped by class of smeshBuilder.py
     anEntry = theObjectNames( seqRemoved.Value(ir) );
     if ( anEntry2AccessorMethod.IsBound( anEntry ) )
       anUpdatedScript += helper + "." + anEntry2AccessorMethod( anEntry );
@@ -1027,35 +1098,33 @@ TCollection_AsciiString SMESH_Gen_i::DumpPython_impl
   }
 
   // Set object names
-  anUpdatedScript += "\n\t## set object names";
-//   anUpdatedScript += "\n\t\tsmeshgui = salome.ImportComponentGUI(\"SMESH\")";
-//   anUpdatedScript += "\n\t\tsmeshgui.Init(theStudy._get_StudyId())";
-//   anUpdatedScript += "\n";
 
-  TCollection_AsciiString aGUIName;
+  TCollection_AsciiString aGUIName, aSetNameScriptPart;
   Resource_DataMapOfAsciiStringAsciiString mapEntries;
   for (Standard_Integer i = 1; i <= aLen; i += 2)
   {
     anEntry = aScript.SubString(aSeq->Value(i), aSeq->Value(i + 1));
-    aName = geom->GetDumpName( anEntry.ToCString() );
+    aName   = geom->GetDumpName( anEntry.ToCString() );
     if (aName.IsEmpty() && // Not a GEOM object
         theNames.IsBound(anEntry) &&
+        !aRemovedObjIDs.count(anEntry) && // A command creating anEntry was erased
         !mapEntries.IsBound(anEntry) && // Not yet processed
         !mapRemoved.IsBound(anEntry)) // Was not removed
     {
-      aName = theObjectNames.Find(anEntry);
+      aName    = theObjectNames.Find(anEntry);
       aGUIName = theNames.Find(anEntry);
       mapEntries.Bind(anEntry, aName);
-      anUpdatedScript += helper + "\n\t" + aSMESHGen + ".SetName(" + aName;
+      aSetNameScriptPart += helper + "\n\t" + aSMESHGen + ".SetName(" + aName;
       if ( anEntry2AccessorMethod.IsBound( anEntry ) )
-        anUpdatedScript += helper + "." + anEntry2AccessorMethod( anEntry );
-      anUpdatedScript += helper + ", '" + aGUIName + "')";
+        aSetNameScriptPart += helper + "." + anEntry2AccessorMethod( anEntry );
+      aSetNameScriptPart += helper + ", '" + aGUIName + "')";
     }
   }
-
-  // Issue 0021249: removed (a similar block is dumped by SALOMEDSImpl_Study)
-  //anUpdatedScript += "\n\tif salome.sg.hasDesktop():";
-  //anUpdatedScript += "\n\t\tsalome.sg.updateObjBrowser(0)";
+  if ( !aSetNameScriptPart.IsEmpty() )
+  {
+    anUpdatedScript += "\n\t## set object names";
+    anUpdatedScript += aSetNameScriptPart;
+  }
 
   // -----------------------------------------------------------------
   // store visual properties of displayed objects