Salome HOME
Dump Puthon extension
[modules/smesh.git] / src / SMESH_I / SMESH_2smeshpy.cxx
index f933abfb709f24d771961ac160ccce74ddef6d9b..135a6eab8bdac909378f12bcdca7af380eb55011 100644 (file)
@@ -17,7 +17,7 @@
 //  License along with this library; if not, write to the Free Software 
 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
 // 
-// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 //
 //
@@ -34,6 +34,7 @@
 
 #include "utilities.h"
 #include "SMESH_PythonDump.hxx"
+#include "SMESH_NoteBook.hxx"
 #include "Resource_DataMapOfAsciiStringAsciiString.hxx"
 
 #include "SMESH_Gen_i.hxx"
@@ -123,20 +124,37 @@ SMESH_2smeshpy::ConvertScript(const TCollection_AsciiString& theScript,
                               Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod)
 {
   theGen = new _pyGen( theEntry2AccessorMethod );
-
-  // split theScript into separate commands
+  
+  SMESH_NoteBook * aNoteBook = new SMESH_NoteBook();
+  
   int from = 1, end = theScript.Length(), to;
   while ( from < end && ( to = theScript.Location( "\n", from, end )))
+  {
+    if ( to != from )
+        // cut out and store a command
+        aNoteBook->AddCommand( theScript.SubString( from, to - 1 ));
+      from = to + 1;
+  }
+  
+  aNoteBook->ReplaceVariables();
+
+  TCollection_AsciiString aNoteScript = aNoteBook->GetResultScript();
+  delete aNoteBook;
+  aNoteBook = 0;
+  
+  // split theScript into separate commands
+  from = 1, end = aNoteScript.Length();
+  while ( from < end && ( to = aNoteScript.Location( "\n", from, end )))
   {
     if ( to != from )
       // cut out and store a command
-      theGen->AddCommand( theScript.SubString( from, to - 1 ));
+      theGen->AddCommand( aNoteScript.SubString( from, to - 1 ));
     from = to + 1;
   }
   // finish conversion
   theGen->Flush();
 #ifdef DUMP_CONVERSION
-  cout << endl << " ######## RESULT ######## " << endl<< endl;
+  MESSAGE_BEGIN ( std::endl << " ######## RESULT ######## " << std::endl<< std::endl );
 #endif
   // reorder commands after conversion
   list< Handle(_pyCommand) >::iterator cmd;
@@ -153,7 +171,7 @@ SMESH_2smeshpy::ConvertScript(const TCollection_AsciiString& theScript,
   for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
   {
 #ifdef DUMP_CONVERSION
-    cout << "## COM " << (*cmd)->GetOrderNb() << ": "<< (*cmd)->GetString() << endl;
+    MESSAGE_ADD ( "## COM " << (*cmd)->GetOrderNb() << ": "<< (*cmd)->GetString() << std::endl );
 #endif
     if ( !(*cmd)->IsEmpty() ) {
       aScript += "\n";
@@ -208,7 +226,7 @@ Handle(_pyCommand) _pyGen::AddCommand( const TCollection_AsciiString& theCommand
 
   Handle(_pyCommand) aCommand = myCommands.back();
 #ifdef DUMP_CONVERSION
-  cout << "## COM " << myNbCommands << ": "<< aCommand->GetString() << endl;
+  MESSAGE ( "## COM " << myNbCommands << ": "<< aCommand->GetString() );
 #endif
 
   _pyID objID = aCommand->GetObject();
@@ -237,6 +255,13 @@ Handle(_pyCommand) _pyGen::AddCommand( const TCollection_AsciiString& theCommand
   map< _pyID, Handle(_pyMeshEditor) >::iterator id_editor = myMeshEditors.find( objID );
   if ( id_editor != myMeshEditors.end() ) {
     id_editor->second->Process( aCommand );
+    TCollection_AsciiString processedCommand = aCommand->GetString();
+    // some commands of SMESH_MeshEditor create meshes
+    if ( aCommand->GetMethod().Search("MakeMesh") != -1 ) {
+      Handle(_pyMesh) mesh = new _pyMesh( aCommand, aCommand->GetResultValue() );
+      aCommand->GetString() = processedCommand; // discard changes made by _pyMesh
+      myMeshes.insert( make_pair( mesh->GetID(), mesh ));
+    }
     return aCommand;
   }
   // SMESH_Hypothesis method?
@@ -289,17 +314,20 @@ void _pyGen::Process( const Handle(_pyCommand)& theCommand )
   // CreateHypothesis( theHypType, theLibName )
   // Compute( mesh, geom )
   // mesh creation
-  if ( theCommand->GetMethod() == "CreateMesh" ||
-       theCommand->GetMethod() == "CreateEmptyMesh" ||
-       theCommand->GetMethod() == "CreateMeshesFromUNV" ||
-       theCommand->GetMethod() == "CreateMeshesFromSTL")
+  TCollection_AsciiString method = theCommand->GetMethod();
+  if ( method == "CreateMesh" || method == "CreateEmptyMesh")
   {
     Handle(_pyMesh) mesh = new _pyMesh( theCommand );
     myMeshes.insert( make_pair( mesh->GetID(), mesh ));
     return;
   }
-
-  if(theCommand->GetMethod() == "CreateMeshesFromMED")
+  if ( method == "CreateMeshesFromUNV" || method == "CreateMeshesFromSTL")
+  {
+    Handle(_pyMesh) mesh = new _pyMesh( theCommand, theCommand->GetResultValue() );
+    myMeshes.insert( make_pair( mesh->GetID(), mesh ));
+    return;
+  }
+  if( method == "CreateMeshesFromMED")
   {
     for(int ind = 0;ind<theCommand->GetNbResultValues();ind++)
     {
@@ -309,14 +337,14 @@ void _pyGen::Process( const Handle(_pyCommand)& theCommand )
   }
 
   // CreateHypothesis()
-  if ( theCommand->GetMethod() == "CreateHypothesis" )
+  if ( method == "CreateHypothesis" )
   {
     myHypos.push_back( _pyHypothesis::NewHypothesis( theCommand ));
     return;
   }
 
   // smeshgen.Compute( mesh, geom ) --> mesh.Compute()
-  if ( theCommand->GetMethod() == "Compute" )
+  if ( method == "Compute" )
   {
     const _pyID& meshID = theCommand->GetArg( 1 );
     map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( meshID );
@@ -329,7 +357,7 @@ void _pyGen::Process( const Handle(_pyCommand)& theCommand )
   }
 
   // leave only one smeshgen.GetPattern() in the script
-  if ( theCommand->GetMethod() == "GetPattern" ) {
+  if ( method == "GetPattern" ) {
     if ( myHasPattern ) {
       theCommand->Clear();
       return;
@@ -338,8 +366,14 @@ void _pyGen::Process( const Handle(_pyCommand)& theCommand )
   }
 
   // Concatenate( [mesh1, ...], ... )
-  if ( theCommand->GetMethod() == "Concatenate" )
+  if ( method == "Concatenate" || method == "ConcatenateWithGroups")
   {
+    if ( method == "ConcatenateWithGroups" ) {
+      theCommand->SetMethod( "Concatenate" );
+      theCommand->SetArg( theCommand->GetNbArgs() + 1, "True" );
+    }
+    Handle(_pyMesh) mesh = new _pyMesh( theCommand, theCommand->GetResultValue() );
+    myMeshes.insert( make_pair( mesh->GetID(), mesh ));
     AddMeshAccessorMethod( theCommand );
   }
 
@@ -595,18 +629,17 @@ static bool sameGroupType( const _pyID&                   grpID,
  */
 //================================================================================
 
-_pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd):
-  _pyObject(theCreationCmd), myHasEditor(false)
+_pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd)
+  _pyObject(theCreationCmd), myHasEditor(false)
 {
   // convert my creation command
   Handle(_pyCommand) creationCmd = GetCreationCmd();
-  TCollection_AsciiString str = creationCmd->GetMethod();
-  
+  //TCollection_AsciiString str = creationCmd->GetMethod();
+//   if(str != "CreateMeshesFromUNV" &&
+//      str != "CreateMeshesFromMED" &&
+//      str != "CreateMeshesFromSTL")
   creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() ); 
-  if(str != "CreateMeshesFromUNV" &&
-     str != "CreateMeshesFromMED" &&
-     str != "CreateMeshesFromSTL")
-    creationCmd->SetMethod( "Mesh" );
+  creationCmd->SetMethod( "Mesh" );
 
   theGen->SetAccessorMethod( GetID(), "GetMesh()" );
 }
@@ -752,7 +785,8 @@ bool _pyMesh::NeedMeshAccess( const Handle(_pyCommand)& theCommand )
         "GetNodeInverseElements","GetShapeID","GetShapeIDForElem","GetElemNbNodes",
         "GetElemNode","IsMediumNode","IsMediumNodeOfAnyElem","ElemNbEdges","ElemNbFaces",
         "IsPoly","IsQuadratic","BaryCenter","GetHypothesisList", "SetAutoColor", "GetAutoColor",
-        "" }; // <- mark of end
+        "Clear"
+        ,"" }; // <- mark of end
     sameMethods.Insert( names );
   }
 
@@ -874,37 +908,71 @@ void _pyMeshEditor::Process( const Handle(_pyCommand)& theCommand)
   if ( sameMethods.empty() ) {
     const char * names[] = {
       "RemoveElements","RemoveNodes","AddNode","AddEdge","AddFace","AddPolygonalFace",
-      "AddVolume","AddPolyhedralVolume","AddPolyhedralVolumeByFaces","MoveNode",
-      "InverseDiag","DeleteDiag","Reorient","ReorientObject","SplitQuad","SplitQuadObject",
+      "AddVolume","AddPolyhedralVolume","AddPolyhedralVolumeByFaces","MoveNode", "MoveClosestNodeToPoint",
+      "InverseDiag","DeleteDiag","Reorient","ReorientObject","TriToQuad","SplitQuad","SplitQuadObject",
       "BestSplit","Smooth","SmoothObject","SmoothParametric","SmoothParametricObject",
       "ConvertToQuadratic","ConvertFromQuadratic","RenumberNodes","RenumberElements",
-      "RotationSweep","RotationSweepObject","ExtrusionSweep","AdvancedExtrusion",
-      "ExtrusionSweepObject","ExtrusionSweepObject1D","ExtrusionSweepObject2D","Mirror",
-      "MirrorObject","Translate","TranslateObject","Rotate","RotateObject",
+      "RotationSweep","RotationSweepObject","RotationSweepObject1D","RotationSweepObject2D",
+      "ExtrusionSweep","AdvancedExtrusion","ExtrusionSweepObject","ExtrusionSweepObject1D","ExtrusionSweepObject2D",
+      "ExtrusionAlongPath","ExtrusionAlongPathObject","ExtrusionAlongPathObject1D","ExtrusionAlongPathObject2D",
+      "Mirror","MirrorObject","Translate","TranslateObject","Rotate","RotateObject",
       "FindCoincidentNodes","FindCoincidentNodesOnPart","MergeNodes","FindEqualElements",
       "MergeElements","MergeEqualElements","SewFreeBorders","SewConformFreeBorders",
       "SewBorderToSide","SewSideElements","ChangeElemNodes","GetLastCreatedNodes",
       "GetLastCreatedElems",
       "MirrorMakeMesh","MirrorObjectMakeMesh","TranslateMakeMesh",
-      "TranslateObjectMakeMesh","RotateMakeMesh","RotateObjectMakeMesh",
-      "" }; // <- mark of the end
+      "TranslateObjectMakeMesh","RotateMakeMesh","RotateObjectMakeMesh"
+      ,"" }; // <- mark of the end
     sameMethods.Insert( names );
   }
-  //theGen->AddMeshAccessorMethod( theCommand ); // for *Object()
+
+  // names of SMESH_MeshEditor methods which differ from methods of class Mesh
+  // only last two arguments
+  static TStringSet diffLastTwoArgsMethods;
+  if (diffLastTwoArgsMethods.empty() ){
+    const char * names[] = {
+      "MirrorMakeGroups","MirrorObjectMakeGroups",
+      "TranslateMakeGroups","TranslateObjectMakeGroups",
+      "RotateMakeGroups","RotateObjectMakeGroups",
+      ""};// <- mark of the end
+    diffLastTwoArgsMethods.Insert( names );
+  }
 
   if ( sameMethods.Contains( theCommand->GetMethod() )) {
     theCommand->SetObject( myMesh );
 
     // meshes made by *MakeMesh() methods are not wrapped by _pyMesh,
     // so let _pyMesh care of it (TMP?)
-    if ( theCommand->GetMethod().Search("MakeMesh") != -1 )
-      _pyMesh( new _pyCommand( theCommand->GetString(), 0 )); // for theGen->SetAccessorMethod()
+//     if ( theCommand->GetMethod().Search("MakeMesh") != -1 )
+//       _pyMesh( new _pyCommand( theCommand->GetString(), 0 )); // for theGen->SetAccessorMethod()
   }
   else {
-    // editor creation command is needed only if any editor function is called
-    if ( !myCreationCmdStr.IsEmpty() ) {
-      GetCreationCmd()->GetString() = myCreationCmdStr;
-      myCreationCmdStr.Clear();
+    
+    //Replace SMESH_MeshEditor "MakeGroups" functions on the Mesh 
+    //functions with the flag "theMakeGroups = True" like:
+    //SMESH_MeshEditor.CmdMakeGroups => Mesh.Cmd(...,True)
+    int pos = theCommand->GetMethod().Search("MakeGroups");
+    if( pos != -1) {  
+      // 1. Remove "MakeGroups" from the Command
+      TCollection_AsciiString aMethod = theCommand->GetMethod();
+      int nbArgsToAdd = diffLastTwoArgsMethods.Contains(aMethod) ? 2 : 1;
+      aMethod.Trunc(pos-1);
+      theCommand->SetMethod(aMethod);
+
+      // 2. Set Mesh object instead SMESH_MeshEditor
+      theCommand->SetObject( myMesh );
+
+      // 3. And add last "True" argument
+      while(nbArgsToAdd--)
+        theCommand->SetArg(theCommand->GetNbArgs()+1,"True ");
+    }
+    else {
+      // editor creation command is needed only if any editor function is called
+      theGen->AddMeshAccessorMethod( theCommand ); // for *Object()
+      if ( !myCreationCmdStr.IsEmpty() ) {
+        GetCreationCmd()->GetString() = myCreationCmdStr;
+        myCreationCmdStr.Clear();
+      }
     }
   }
 }
@@ -1032,6 +1100,9 @@ Handle(_pyHypothesis) _pyHypothesis::NewHypothesis( const Handle(_pyCommand)& th
     hyp->SetConvMethodAndType( "QuadranglePreference", "Quadrangle_2D");
     hyp->SetConvMethodAndType( "QuadranglePreference", "NETGEN_2D_ONLY");
   }
+  else if ( hypType == "TrianglePreference" ) {
+    hyp->SetConvMethodAndType( "TrianglePreference", "Quadrangle_2D");
+  }    
   // NETGEN ----------
 //   else if ( hypType == "NETGEN_2D") { // 1D-2D
 //     algo->SetConvMethodAndType( "Triangle" , hypType.ToCString());
@@ -1662,8 +1733,22 @@ const TCollection_AsciiString & _pyCommand::GetObject()
   {
     // beginning
     int begPos = GetBegPos( RESULT_IND ) + myRes.Length();
-    if ( begPos < 1 )
+    if ( begPos < 1 ) {
       begPos = myString.Location( "=", 1, Length() ) + 1;
+      // is '=' in the string argument (for example, name) or not
+      int nb1 = 0; // number of ' character at the left of =
+      int nb2 = 0; // number of " character at the left of =
+      for ( int i = 1; i < begPos-1; i++ ) {
+       if ( IsEqual(myString.Value( i ), "'" ) )
+         nb1 += 1;
+       else if ( IsEqual( myString.Value( i ), '"' ) )
+         nb2 += 1;
+      }
+      // if number of ' or " is not divisible by 2,
+      // then get an object at the start of the command
+      if ( nb1 % 2 != 0 || nb2 % 2 != 0 )
+       begPos = 1;
+    }
     // store
     myObj = GetWord( myString, begPos, true );
     SetBegPos( OBJECT_IND, begPos );
@@ -1714,14 +1799,21 @@ const TCollection_AsciiString & _pyCommand::GetArg( int index )
     if ( begPos < 1 )
       begPos = myString.Location( "(", 1, Length() ) + 1;
 
-    int i = 0, prevLen = 0;
+    int i = 0, prevLen = 0, nbNestings = 0;
     while ( begPos != EMPTY ) {
       begPos += prevLen;
+      if( myString.Value( begPos ) == '(' )
+       nbNestings++;
       // check if we are looking at the closing parenthesis
       while ( begPos <= Length() && isspace( myString.Value( begPos )))
         ++begPos;
-      if ( begPos > Length() || myString.Value( begPos ) == ')' )
+      if ( begPos > Length() )
         break;
+      if ( myString.Value( begPos ) == ')' ) {
+       nbNestings--;
+       if( nbNestings == 0 )
+         break;
+      }
       myArgs.Append( GetWord( myString, begPos, true, true ));
       SetBegPos( ARG1_IND + i, begPos );
       prevLen = myArgs.Last().Length();
@@ -1875,7 +1967,8 @@ void _pyCommand::SetArg( int index, const TCollection_AsciiString& theArg)
   if ( pos < 1 ) // no index-th arg exist, append inexistent args
   {
     // find a closing parenthesis
-    if ( int lastArgInd = GetNbArgs() ) {
+    if ( GetNbArgs() != 0 && index <= GetNbArgs() ) {
+      int lastArgInd = GetNbArgs();
       pos = GetBegPos( ARG1_IND + lastArgInd  - 1 ) + GetArg( lastArgInd ).Length();
       while ( pos > 0 && pos <= Length() && myString.Value( pos ) != ')' )
         ++pos;