Salome HOME
Join modifications from branch OCC_development_for_3_2_0a2
[modules/smesh.git] / src / SMESH_I / SMESH_2smeshpy.cxx
1 //  SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
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. 
10 // 
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. 
15 // 
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 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SMESH_2D_Algo_i.hxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SMESH
27 //  $Header$
28
29 // File      : SMESH_2smeshpy.cxx
30 // Created   : Fri Nov 18 13:20:10 2005
31 // Author    : Edward AGAPOV (eap)
32
33 #include "SMESH_2smeshpy.hxx"
34
35 #include "SMESH_Gen_i.hxx"
36 #include "utilities.h"
37 #include "SMESH_PythonDump.hxx"
38 #include "Resource_DataMapOfAsciiStringAsciiString.hxx"
39
40 IMPLEMENT_STANDARD_HANDLE (_pyObject          ,Standard_Transient);
41 IMPLEMENT_STANDARD_HANDLE (_pyCommand         ,Standard_Transient);
42 IMPLEMENT_STANDARD_HANDLE (_pyGen             ,_pyObject);
43 IMPLEMENT_STANDARD_HANDLE (_pyMesh            ,_pyObject);
44 IMPLEMENT_STANDARD_HANDLE (_pyHypothesis      ,_pyObject);
45 IMPLEMENT_STANDARD_HANDLE (_pyAlgorithm       ,_pyHypothesis);
46 IMPLEMENT_STANDARD_HANDLE (_pyComplexParamHypo,_pyHypothesis);
47 IMPLEMENT_STANDARD_HANDLE (_pyNumberOfSegmentsHyp,_pyHypothesis);
48
49 IMPLEMENT_STANDARD_RTTIEXT(_pyObject          ,Standard_Transient);
50 IMPLEMENT_STANDARD_RTTIEXT(_pyCommand         ,Standard_Transient);
51 IMPLEMENT_STANDARD_RTTIEXT(_pyGen             ,_pyObject);
52 IMPLEMENT_STANDARD_RTTIEXT(_pyMesh            ,_pyObject);
53 IMPLEMENT_STANDARD_RTTIEXT(_pyHypothesis      ,_pyObject);
54 IMPLEMENT_STANDARD_RTTIEXT(_pyAlgorithm       ,_pyHypothesis);
55 IMPLEMENT_STANDARD_RTTIEXT(_pyComplexParamHypo,_pyHypothesis);
56 IMPLEMENT_STANDARD_RTTIEXT(_pyNumberOfSegmentsHyp,_pyHypothesis);
57
58 using namespace std;
59 using SMESH::TPythonDump;
60
61 /*!
62  * \brief Container of commands into which the initial script is split.
63  *        It also contains data coresponding to SMESH_Gen contents
64  */
65 static Handle(_pyGen) theGen;
66
67 static TCollection_AsciiString theEmptyString;
68
69 //#define DUMP_CONVERSION
70
71 #if !defined(_DEBUG_) && defined(DUMP_CONVERSION)
72 #undef DUMP_CONVERSION
73 #endif
74
75 //================================================================================
76 /*!
77  * \brief Convert python script using commands of smesh.py
78   * \param theScript - Input script
79   * \retval TCollection_AsciiString - Convertion result
80  */
81 //================================================================================
82
83 TCollection_AsciiString
84 SMESH_2smeshpy::ConvertScript(const TCollection_AsciiString& theScript,
85                               Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod)
86 {
87   theGen = new _pyGen( theEntry2AccessorMethod );
88
89   // split theScript into separate commands
90   int from = 1, end = theScript.Length(), to;
91   while ( from < end && ( to = theScript.Location( "\n", from, end )))
92   {
93     if ( to != from )
94       // cut out and store a command
95       theGen->AddCommand( theScript.SubString( from, to - 1 ));
96     from = to + 1;
97   }
98   // finish conversion
99   theGen->Flush();
100 #ifdef DUMP_CONVERSION
101   cout << endl << " ######## RESULT ######## " << endl<< endl;
102 #endif
103   // reorder commands after conversion
104   list< Handle(_pyCommand) >::iterator cmd;
105   bool orderChanges;
106   do {
107     orderChanges = false;
108     for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
109       if ( (*cmd)->SetDependentCmdsAfter() )
110         orderChanges = true;
111   } while ( orderChanges );
112   
113   // concat commands back into a script
114   TCollection_AsciiString aScript;
115   for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
116   {
117 #ifdef DUMP_CONVERSION
118     cout << "## COM " << (*cmd)->GetOrderNb() << ": "<< (*cmd)->GetString() << endl;
119 #endif
120     if ( !(*cmd)->IsEmpty() ) {
121       aScript += "\n";
122       aScript += (*cmd)->GetString();
123     }
124   }
125   aScript += "\n";
126
127   theGen.Nullify();
128
129   return aScript;
130 }
131
132 //================================================================================
133 /*!
134  * \brief _pyGen constructor
135  */
136 //================================================================================
137
138 _pyGen::_pyGen(Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod)
139   : _pyObject( new _pyCommand( TPythonDump::SMESHGenName(), 0 )),
140     myID2AccessorMethod( theEntry2AccessorMethod )
141 {
142   myNbCommands = 0;
143   // make that GetID() to return TPythonDump::SMESHGenName()
144   GetCreationCmd()->GetString() += "=";
145 }
146
147 //================================================================================
148 /*!
149  * \brief Convert a command using a specific converter
150   * \param theCommand - the command to convert
151   * \retval bool - convertion result
152  */
153 //================================================================================
154
155 void _pyGen::AddCommand( const TCollection_AsciiString& theCommand)
156 {
157   // store theCommand in the sequence
158   myCommands.push_back( new _pyCommand( theCommand, ++myNbCommands ));
159
160   Handle(_pyCommand) aCommand = myCommands.back();
161 #ifdef DUMP_CONVERSION
162   cout << "## COM " << myNbCommands << ": "<< aCommand->GetString() << endl;
163 #endif
164
165   _pyID objID = aCommand->GetObject();
166
167   if ( objID.IsEmpty() )
168     return;
169
170   // SMESH_Gen method?
171   if ( objID == this->GetID() ) {
172     this->Process( aCommand );
173     return;
174   }
175   // SMESH_Mesh method?
176   map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( objID );
177   if ( id_mesh != myMeshes.end() ) {
178     id_mesh->second->Process( aCommand );
179     return;
180   }
181   // SMESH_Hypothesis method
182   list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
183   for ( ; hyp != myHypos.end(); ++hyp )
184     if ( !(*hyp)->IsAlgo() && objID == (*hyp)->GetID() )
185       (*hyp)->Process( aCommand );
186
187   // Mesh provides SMESH_IDSource interface used in SMESH_MeshEditor.
188   // Add access to wrapped mesh
189   if ( objID == TPythonDump::MeshEditorName() ) {
190     // in all SMESH_MeshEditor's commands, a SMESH_IDSource is the first arg
191     id_mesh = myMeshes.find( aCommand->GetArg( 1 ));
192     if ( id_mesh != myMeshes.end() )
193       aCommand->SetArg( 1 , aCommand->GetArg( 1 ) + ".GetMesh()" );
194   }
195 }
196
197 //================================================================================
198 /*!
199  * \brief Convert the command or remember it for later conversion 
200   * \param theCommand - The python command calling a method of SMESH_Gen
201  */
202 //================================================================================
203
204 void _pyGen::Process( const Handle(_pyCommand)& theCommand )
205 {
206   // there are methods to convert:
207   // CreateMesh( shape )
208   // CreateHypothesis( theHypType, theLibName )
209   // Compute( mesh, geom )
210
211   if ( theCommand->GetMethod() == "CreateMesh" )
212   {
213     Handle(_pyMesh) mesh = new _pyMesh( theCommand );
214     myMeshes.insert( make_pair( mesh->GetID(), mesh ));
215     return;
216   }
217
218   // CreateHypothesis()
219   if ( theCommand->GetMethod() == "CreateHypothesis" )
220   {
221     myHypos.push_back( _pyHypothesis::NewHypothesis( theCommand ));
222     return;
223   }
224
225   // smeshgen.Compute( mesh, geom ) --> mesh.Compute()
226   if ( theCommand->GetMethod() == "Compute" )
227   {
228     const _pyID& meshID = theCommand->GetArg( 1 );
229     map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( meshID );
230     if ( id_mesh != myMeshes.end() ) {
231       theCommand->SetObject( meshID );
232       theCommand->RemoveArgs();
233       id_mesh->second->Flush();
234       return;
235     }
236   }
237
238   // smeshgen.Method() --> smesh.smesh.Method()
239   theCommand->SetObject( SMESH_2smeshpy::GenName() );
240 }
241
242 //================================================================================
243 /*!
244  * \brief Convert the remembered commands
245  */
246 //================================================================================
247
248 void _pyGen::Flush()
249 {
250   map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.begin();
251   for ( ; id_mesh != myMeshes.end(); ++id_mesh )
252     if ( ! id_mesh->second.IsNull() )
253       id_mesh->second->Flush();
254
255   list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
256   for ( ; hyp != myHypos.end(); ++hyp )
257     if ( !hyp->IsNull() ) {
258       (*hyp)->Flush();
259       // smeshgen.CreateHypothesis() --> smesh.smesh.CreateHypothesis()
260       if ( !(*hyp)->IsWrapped() )
261         (*hyp)->GetCreationCmd()->SetObject( SMESH_2smeshpy::GenName() );
262     }
263 }
264
265 //================================================================================
266 /*!
267  * \brief Find hypothesis by ID (entry)
268   * \param theHypID - The hypothesis ID
269   * \retval Handle(_pyHypothesis) - The found hypothesis
270  */
271 //================================================================================
272
273 Handle(_pyHypothesis) _pyGen::FindHyp( const _pyID& theHypID )
274 {
275   list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
276   for ( ; hyp != myHypos.end(); ++hyp )
277     if ( !hyp->IsNull() && theHypID == (*hyp)->GetID() )
278       return *hyp;
279   return Handle(_pyHypothesis)();
280 }
281
282 //================================================================================
283 /*!
284  * \brief Find algorithm the created algorithm
285   * \param theGeom - The shape ID the algorithm was created on
286   * \param theMesh - The mesh ID that created the algorithm
287   * \param dim - The algo dimension
288   * \retval Handle(_pyHypothesis) - The found algo
289  */
290 //================================================================================
291
292 Handle(_pyHypothesis) _pyGen::FindAlgo( const _pyID& theGeom, const _pyID& theMesh,
293                                       const TCollection_AsciiString& theAlgoType )
294 {
295   list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
296   for ( ; hyp != myHypos.end(); ++hyp )
297     if ( !hyp->IsNull() &&
298          (*hyp)->IsAlgo() &&
299          (*hyp)->GetType() == theAlgoType &&
300          (*hyp)->GetGeom() == theGeom &&
301          (*hyp)->GetMesh() == theMesh )
302       return *hyp;
303   return 0;
304 }
305
306 //================================================================================
307 /*!
308  * \brief Change order of commands in the script
309   * \param theCmd1 - One command
310   * \param theCmd2 - Another command
311  */
312 //================================================================================
313
314 void _pyGen::ExchangeCommands( Handle(_pyCommand) theCmd1, Handle(_pyCommand) theCmd2 )
315 {
316   list< Handle(_pyCommand) >::iterator pos1, pos2;
317   pos1 = find( myCommands.begin(), myCommands.end(), theCmd1 );
318   pos2 = find( myCommands.begin(), myCommands.end(), theCmd2 );
319   myCommands.insert( pos1, theCmd2 );
320   myCommands.insert( pos2, theCmd1 );
321   myCommands.erase( pos1 );
322   myCommands.erase( pos2 );
323
324   int nb1 = theCmd1->GetOrderNb();
325   theCmd1->SetOrderNb( theCmd2->GetOrderNb() );
326   theCmd2->SetOrderNb( nb1 );
327 //   cout << "BECOME " << theCmd1->GetOrderNb() << "\t" << theCmd1->GetString() << endl
328 //        << "BECOME " << theCmd2->GetOrderNb() << "\t" << theCmd2->GetString() << endl << endl;
329 }
330
331 //================================================================================
332 /*!
333  * \brief Set one command after the other
334   * \param theCmd - Command to move
335   * \param theAfterCmd - Command ater which to insert the first one
336  */
337 //================================================================================
338
339 void _pyGen::SetCommandAfter( Handle(_pyCommand) theCmd, Handle(_pyCommand) theAfterCmd )
340 {
341 //   cout << "SET\t" << theCmd->GetString() << endl << "AFTER\t" << theAfterCmd->GetString() << endl << endl;
342   list< Handle(_pyCommand) >::iterator pos;
343   pos = find( myCommands.begin(), myCommands.end(), theCmd );
344   myCommands.erase( pos );
345   pos = find( myCommands.begin(), myCommands.end(), theAfterCmd );
346   myCommands.insert( ++pos, theCmd );
347
348   int i = 1;
349   for ( pos = myCommands.begin(); pos != myCommands.end(); ++pos)
350     (*pos)->SetOrderNb( i++ );
351 }
352
353 //================================================================================
354 /*!
355  * \brief Set method to access to object wrapped with python class
356   * \param theID - The wrapped object entry
357   * \param theMethod - The accessor method
358  */
359 //================================================================================
360
361 void _pyGen::SetAccessorMethod(const _pyID& theID, const char* theMethod )
362 {
363   myID2AccessorMethod.Bind( theID, (char*) theMethod );
364 }
365
366 //================================================================================
367 /*!
368  * \brief Find out type of geom group
369   * \param grpID - The geom group entry
370   * \retval int - The type
371  */
372 //================================================================================
373
374 static bool sameGroupType( const _pyID&                   grpID,
375                            const TCollection_AsciiString& theType)
376 {
377   // define group type as smesh.Mesh.Group() does
378   int type = -1;
379   SALOMEDS::Study_var study = SMESH_Gen_i::GetSMESHGen()->GetCurrentStudy();
380   SALOMEDS::SObject_var aSObj = study->FindObjectID( grpID.ToCString() );
381   if ( !aSObj->_is_nil() ) {
382     GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow( aSObj->GetObject() );
383     if ( !aGeomObj->_is_nil() ) {
384       switch ( aGeomObj->GetShapeType() ) {
385       case GEOM::VERTEX: type = SMESH::NODE; break;
386       case GEOM::EDGE:   type = SMESH::EDGE; break;
387       case GEOM::FACE:   type = SMESH::FACE; break;
388       case GEOM::SOLID:
389       case GEOM::SHELL:  type = SMESH::VOLUME; break;
390       case GEOM::COMPOUND: {
391         GEOM::GEOM_Gen_var aGeomGen = SMESH_Gen_i::GetSMESHGen()->GetGeomEngine();
392         if ( !aGeomGen->_is_nil() ) {
393           GEOM::GEOM_IGroupOperations_var aGrpOp =
394             aGeomGen->GetIGroupOperations( study->StudyId() );
395           if ( !aGrpOp->_is_nil() ) {
396             switch ( aGrpOp->GetType( aGeomObj )) {
397             case TopAbs_VERTEX: type = SMESH::NODE; break;
398             case TopAbs_EDGE:   type = SMESH::EDGE; break;
399             case TopAbs_FACE:   type = SMESH::FACE; break;
400             case TopAbs_SOLID:  type = SMESH::VOLUME; break;
401             default:;
402             }
403           }
404         }
405       }
406       default:;
407       }
408     }
409   }
410   if ( type < 0 ) {
411     MESSAGE("Type of the group " << grpID << " not found");
412     return false;
413   }
414   if ( theType.IsIntegerValue() )
415     return type == theType.IntegerValue();
416
417   switch ( type ) {
418   case SMESH::NODE:   return theType.Location( "NODE", 1, theType.Length() );
419   case SMESH::EDGE:   return theType.Location( "EDGE", 1, theType.Length() );
420   case SMESH::FACE:   return theType.Location( "FACE", 1, theType.Length() );
421   case SMESH::VOLUME: return theType.Location( "VOLUME", 1, theType.Length() );
422   default:;
423   }
424   return false;
425 }
426
427 //================================================================================
428 /*!
429  * \brief 
430   * \param theCreationCmd - 
431  */
432 //================================================================================
433
434 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd): _pyObject(theCreationCmd)
435 {
436   // convert my creation command
437   Handle(_pyCommand) creationCmd = GetCreationCmd();
438   creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
439   creationCmd->SetMethod( "Mesh" );
440
441   theGen->SetAccessorMethod( GetID(), "GetMesh()" );
442 }
443
444 //================================================================================
445 /*!
446  * \brief Convert a IDL API command of SMESH::Mesh to a method call of python Mesh
447   * \param theCommand - Engine method called for this mesh
448  */
449 //================================================================================
450
451 void _pyMesh::Process( const Handle(_pyCommand)& theCommand )
452 {
453   // smesh.py wraps the following methods:
454   //
455   // 1. GetSubMesh(geom, name) + AddHypothesis(geom, algo)
456   //     --> in Mesh_Algorithm.Create(mesh, geom, hypo, so)
457   // 2. AddHypothesis(geom, hyp)
458   //     --> in Mesh_Algorithm.Hypothesis(hyp, args, so)
459   // 3. CreateGroupFromGEOM(type, name, grp)
460   //     --> in Mesh.Group(grp, name="")
461   // 4. ExportToMED(f, opt, version)
462   //     --> in Mesh.ExportToMED( f, version, opt=0 )
463   // 5. ExportMED(f, opt)
464   //     --> in Mesh.ExportMED( f,opt=0 )
465   // 6. ExportDAT(f)
466   //     --> in Mesh.ExportDAT( f )
467   // 7. ExportUNV(f)
468   //     --> in Mesh.ExportUNV(f)
469   // 8. ExportSTL(f, ascii)
470   //     --> in Mesh.ExportSTL(f, ascii=1)
471
472   const TCollection_AsciiString method = theCommand->GetMethod();
473   if ( method == "GetSubMesh" ) {
474     mySubmeshes.push_back( theCommand );
475   }
476   else if ( method == "AddHypothesis" ) { // mesh.AddHypothesis(geom, HYPO )
477     myAddHypCmds.push_back( theCommand );
478     // set mesh to hypo
479     const _pyID& hypID = theCommand->GetArg( 2 );
480     Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
481     if ( !hyp.IsNull() && hyp->GetMesh().IsEmpty() )
482       hyp->SetMesh( this->GetID() );
483   }
484   else if ( method == "CreateGroupFromGEOM" ) {// (type, name, grp)
485     _pyID grp = theCommand->GetArg( 3 );
486     if ( sameGroupType( grp, theCommand->GetArg( 1 )) ) { // --> Group(grp)
487       theCommand->SetMethod( "Group" );
488       theCommand->RemoveArgs();
489       theCommand->SetArg( 1, grp );
490     }
491     else {
492       AddMeshAccess( theCommand );
493     }
494   }
495   else if ( method == "ExportToMED" ) {//(f, opt, version)
496     // --> (f, version, opt)
497     _pyID opt = theCommand->GetArg( 2 );
498     _pyID ver = theCommand->GetArg( 3 );
499     theCommand->SetArg( 2, ver );
500     theCommand->SetArg( 3, opt );
501   }
502   else if ( method == "RemoveHypothesis" ) // (geom, hyp)
503   {
504     const _pyID & hypID = theCommand->GetArg( 2 );
505
506     // check if this mesh still has corresponding addition command
507     bool hasAddCmd = false;
508     list< Handle(_pyCommand) >::iterator cmd = myAddHypCmds.begin();
509     while ( cmd != myAddHypCmds.end() )
510     {
511       // AddHypothesis(geom, hyp)
512       if ( hypID == (*cmd)->GetArg( 2 )) { // erase both commands
513         theCommand->Clear();
514         (*cmd)->Clear();
515         cmd = myAddHypCmds.erase( cmd );
516         hasAddCmd = true;
517       }
518       else {
519         ++cmd;
520       }
521     }
522     if ( ! hasAddCmd ) {
523       // access to wrapped mesh
524       AddMeshAccess( theCommand );
525       // access to wrapped algo
526       Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
527       if ( !hyp.IsNull() && hyp->IsAlgo() && hyp->IsWrapped() )
528         theCommand->SetArg( 2, theCommand->GetArg( 2 ) + ".GetAlgorithm()" );
529     }
530   }
531   else { // apply theCommand to the mesh wrapped by smeshpy mesh
532     AddMeshAccess( theCommand );
533   }
534 }
535
536 //================================================================================
537 /*!
538  * \brief Convert creation and addition of all algos and hypos
539  */
540 //================================================================================
541
542 void _pyMesh::Flush()
543 {
544   list < Handle(_pyCommand) >::iterator cmd, cmd2;
545
546   // try to convert algo addition like this:
547   // mesh.AddHypothesis(geom, ALGO ) --> ALGO = mesh.Algo()
548   for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
549   {
550     Handle(_pyCommand) addCmd = *cmd;
551     const _pyID& algoID = addCmd->GetArg( 2 );
552     Handle(_pyHypothesis) algo = theGen->FindHyp( algoID );
553     if ( algo.IsNull() || !algo->IsAlgo() )
554       continue;
555     // try to convert
556     _pyID geom = addCmd->GetArg( 1 );
557     if ( algo->Addition2Creation( addCmd, this->GetID() )) // OK
558     {
559       // wrapped algo is created atfer mesh creation
560       GetCreationCmd()->AddDependantCmd( addCmd );
561
562       if ( geom != GetGeom() ) // local algo
563       {
564         // mesh.AddHypothesis(geom, ALGO ) --> mesh.AlgoMethod(geom)
565         addCmd->SetArg( addCmd->GetNbArgs() + 1,
566                         TCollection_AsciiString( "geom=" ) + geom );
567         // sm = mesh.GetSubMesh(geom, name) --> sm = ALGO.GetSubMesh()
568         for ( cmd2 = mySubmeshes.begin(); cmd2 != mySubmeshes.end(); ++cmd2 ) {
569           Handle(_pyCommand) subCmd = *cmd2;
570           if ( geom == subCmd->GetArg( 1 )) {
571             subCmd->SetObject( algo->GetID() );
572             subCmd->RemoveArgs();
573             addCmd->AddDependantCmd( subCmd );
574           }
575         }
576       }
577     }
578     else // ALGO was already created
579     {
580       // mesh.AddHypothesis(geom, ALGO ) --> mesh.GetMesh().AddHypothesis(geom, ALGO )
581       AddMeshAccess( addCmd );
582       // mesh.GetMesh().AddHypothesis(geom, ALGO ) ->
583       // mesh.GetMesh().AddHypothesis(geom, ALGO.GetAlgorithm() )
584       addCmd->SetArg( 2, addCmd->GetArg( 2 ) + ".GetAlgorithm()" );
585     }
586   }
587
588   // try to convert hypo addition like this:
589   // mesh.AddHypothesis(geom, HYPO ) --> HYPO = algo.Hypo()
590   for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
591   {
592     Handle(_pyCommand) addCmd = *cmd;
593     const _pyID& hypID = addCmd->GetArg( 2 );
594     Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
595     if ( hyp.IsNull() || hyp->IsAlgo() )
596       continue;
597     const _pyID& geom = addCmd->GetArg( 1 );
598     // find algo created on <geom> for this mesh
599     Handle(_pyHypothesis) algo = theGen->FindAlgo( geom, this->GetID(), hyp->GetType() );
600     //_pyID algoID = algo.IsNull() ? "" : algo->GetID();
601     if ( !algo.IsNull() && hyp->Addition2Creation( addCmd, this->GetID() )) // OK
602     {
603       addCmd->SetObject( algo->GetID() );
604       algo->GetCreationCmd()->AddDependantCmd( addCmd );
605     }
606     else
607     {
608       AddMeshAccess( addCmd );
609     }
610   }
611
612   // sm = mesh.GetSubMesh(geom, name) --> sm = mesh.GetMesh().GetSubMesh(geom, name)
613   for ( cmd = mySubmeshes.begin(); cmd != mySubmeshes.end(); ++cmd ) {
614     Handle(_pyCommand) subCmd = *cmd;
615     if ( subCmd->GetNbArgs() > 0 )
616       AddMeshAccess( subCmd );
617   }
618   myAddHypCmds.clear();
619   mySubmeshes.clear();
620 }
621
622 //================================================================================
623 /*!
624  * \brief _pyHypothesis constructor
625   * \param theCreationCmd - 
626  */
627 //================================================================================
628
629 _pyHypothesis::_pyHypothesis(const Handle(_pyCommand)& theCreationCmd):
630   _pyObject( theCreationCmd )
631 {
632   myDim = myIsAlgo = /*myIsLocal = */myIsWrapped = myIsConverted = false;
633 }
634
635 //================================================================================
636 /*!
637  * \brief Creates algorithm or hypothesis
638   * \param theCreationCmd - The engine command creating a hypothesis
639   * \retval Handle(_pyHypothesis) - Result _pyHypothesis
640  */
641 //================================================================================
642
643 Handle(_pyHypothesis) _pyHypothesis::NewHypothesis( const Handle(_pyCommand)& theCreationCmd)
644 {
645   // theCreationCmd: CreateHypothesis( "theHypType", "theLibName" )
646   ASSERT (( theCreationCmd->GetMethod() == "CreateHypothesis"));
647
648   Handle(_pyHypothesis) hyp, algo;
649
650   // "theHypType"
651   const TCollection_AsciiString & hypTypeWithQuotes = theCreationCmd->GetArg( 1 );
652   if ( hypTypeWithQuotes.IsEmpty() )
653     return hyp;
654   // theHypType
655   TCollection_AsciiString  hypType =
656     hypTypeWithQuotes.SubString( 2, hypTypeWithQuotes.Length() - 1 );
657
658   algo = new _pyAlgorithm( theCreationCmd );
659   hyp  = new _pyHypothesis( theCreationCmd );
660
661   // 1D Regular_1D ----------
662   if ( hypType == "Regular_1D" ) {
663     algo->myDim = 1;
664     algo->myCreationMethod = "Segment";
665   }
666   else if ( hypType == "LocalLength" ) {
667     hyp->myDim = 1;
668     hyp->myCreationMethod = "LocalLength";
669     hyp->myType = "Regular_1D";
670     hyp->myArgMethods.Append( "SetLength" );
671   }
672   else if ( hypType == "NumberOfSegments" ) {
673     hyp = new _pyNumberOfSegmentsHyp( theCreationCmd );
674     hyp->myDim = 1;
675     hyp->myCreationMethod = "NumberOfSegments";
676     hyp->myType = "Regular_1D";
677     hyp->myArgMethods.Append( "SetNumberOfSegments" );
678     hyp->myArgMethods.Append( "SetScaleFactor" );
679   }
680   else if ( hypType == "Arithmetic1D" ) {
681     hyp = new _pyComplexParamHypo( theCreationCmd );
682     hyp->myDim = 1;
683     hyp->myCreationMethod = "Arithmetic1D";
684     hyp->myType = "Regular_1D";
685   }
686   else if ( hypType == "StartEndLength" ) {
687     hyp = new _pyComplexParamHypo( theCreationCmd );
688     hyp->myDim = 1;
689     hyp->myCreationMethod = "StartEndLength";
690     hyp->myType = "Regular_1D";
691   }
692   else if ( hypType == "Deflection1D" ) {
693     hyp->myDim = 1;
694     hyp->myCreationMethod = "Deflection1D";
695     hyp->myArgMethods.Append( "SetDeflection" );
696     hyp->myType = "Regular_1D";
697   }
698   else if ( hypType == "Propagation" ) {
699     hyp->myDim = 1;
700     hyp->myCreationMethod = "Propagation";
701     hyp->myType = "Regular_1D";
702   }
703   else if ( hypType == "QuadraticMesh" ) {
704     hyp->myDim = 1;
705     hyp->myCreationMethod = "QuadraticMesh";
706     hyp->myType = "Regular_1D";
707   }
708   else if ( hypType == "AutomaticLength" ) {
709     hyp->myDim = 1;
710     hyp->myCreationMethod = "AutomaticLength";
711     hyp->myType = "Regular_1D";
712     hyp->myArgMethods.Append( "SetFineness");
713   }
714   // 1D Python_1D ----------
715   else if ( hypType == "Python_1D" ) {
716     algo->myDim = 1;
717     algo->myCreationMethod = "Segment";
718     algo->myArgs.Append( "algo=smesh.PYTHON");
719   }
720   else if ( hypType == "PythonSplit1D" ) {
721     hyp->myDim = 1;
722     hyp->myCreationMethod = "PythonSplit1D";
723     hyp->myType = "Python_1D";
724     hyp->myArgMethods.Append( "SetNumberOfSegments");
725     hyp->myArgMethods.Append( "SetPythonLog10RatioFunction");
726   }
727   // 2D ----------
728   else if ( hypType == "MEFISTO_2D" ) {
729     algo->myDim = 2;
730     algo->myCreationMethod = "Triangle";
731   }
732   else if ( hypType == "MaxElementArea" ) {
733     hyp->myDim = 2;
734     hyp->myCreationMethod = "MaxElementArea";
735     hyp->myType = "MEFISTO_2D";
736     hyp->myArgMethods.Append( "SetMaxElementArea");
737   }
738   else if ( hypType == "LengthFromEdges" ) {
739     hyp->myDim = 2;
740     hyp->myCreationMethod = "LengthFromEdges";
741     hyp->myType = "MEFISTO_2D";
742   }
743   else if ( hypType == "Quadrangle_2D" ) {
744     algo->myDim = 2;
745     algo->myCreationMethod = "Quadrangle";
746   }
747   else if ( hypType == "QuadranglePreference" ) {
748     hyp->myDim = 2;
749     hyp->myCreationMethod = "QuadranglePreference";
750     hyp->myType = "Quadrangle_2D";
751   }
752   // 3D ----------
753   else if ( hypType == "NETGEN_3D") {
754     algo->myDim = 3;
755     algo->myCreationMethod = "Tetrahedron";
756     algo->myArgs.Append( "algo=smesh.NETGEN" );
757   }
758   else if ( hypType == "MaxElementVolume") {
759     hyp->myDim = 3;
760     hyp->myCreationMethod = "MaxElementVolume";
761     hyp->myType = "NETGEN_3D";
762     hyp->myArgMethods.Append( "SetMaxElementVolume" );
763   }
764   else if ( hypType == "GHS3D_3D" ) {
765     algo->myDim = 3;
766     algo->myCreationMethod = "Tetrahedron";
767     algo->myArgs.Append( "algo=smesh.GHS3D" );
768   }
769   else if ( hypType == "Hexa_3D" ) {
770     algo->myDim = 3;
771     algo->myCreationMethod = "Hexahedron";
772   }
773
774   if ( algo->GetDim() ) {
775     algo->myType = hypType;
776     return algo;
777   }
778   return hyp;
779 }
780
781 //================================================================================
782 /*!
783  * \brief Convert the command adding a hypothesis to mesh into a smesh command
784   * \param theCmd - The command like mesh.AddHypothesis( geom, hypo )
785   * \param theAlgo - The algo that can create this hypo
786   * \retval bool - false if the command cant be converted
787  */
788 //================================================================================
789
790 bool _pyHypothesis::Addition2Creation( const Handle(_pyCommand)& theCmd,
791                                        const _pyID&              theMesh)
792 {
793   ASSERT(( theCmd->GetMethod() == "AddHypothesis" ));
794
795   if ( !IsWrappable( theMesh ))
796     return false;
797
798   myIsWrapped = true;
799
800   if ( myIsWrapped )
801   {
802     // mesh.AddHypothesis(geom,hyp) --> hyp = theMesh.myCreationMethod(args)
803     theCmd->SetResultValue( GetID() );
804     theCmd->SetObject( theMesh );
805     theCmd->SetMethod( myCreationMethod );
806     // set args
807     theCmd->RemoveArgs();
808     for ( int i = 1; i <= myArgs.Length(); ++i ) {
809       if ( !myArgs( i ).IsEmpty() )
810         theCmd->SetArg( i, myArgs( i ));
811       else
812         theCmd->SetArg( i, "[]");
813     }
814     // set a new creation command
815     GetCreationCmd()->Clear();
816     SetCreationCmd( theCmd );
817
818     // clear commands setting arg values
819     list < Handle(_pyCommand) >::iterator argCmd = myArgCommands.begin();
820     for ( ; argCmd != myArgCommands.end(); ++argCmd )
821       (*argCmd)->Clear();
822   }
823   else
824   {
825 //     // set arg commands after hypo creation
826 //     list<Handle(_pyCommand)>::iterator argCmd = myArgCommands.begin();
827 //     for ( ; argCmd != myArgCommands.end(); ++argCmd )
828 //       if ( !(*argCmd)->IsEmpty() && GetCommandNb() > (*argCmd)->GetOrderNb() )
829 //         theGen->ExchangeCommands( GetCreationCmd(), *argCmd );
830   }
831
832   // set unknown arg commands after hypo creation
833   Handle(_pyCommand) afterCmd = myIsWrapped ? theCmd : GetCreationCmd();
834   list<Handle(_pyCommand)>::iterator cmd = myUnknownCommands.begin();
835   for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
836     afterCmd->AddDependantCmd( *cmd );
837   }
838   myArgCommands.clear();
839   myUnknownCommands.clear();
840
841   return myIsWrapped;
842 }
843
844 //================================================================================
845 /*!
846  * \brief Remember hypothesis parameter values
847  * \param theCommand - The called hypothesis method
848  */
849 //================================================================================
850
851 void _pyHypothesis::Process( const Handle(_pyCommand)& theCommand)
852 {
853   ASSERT( !myIsAlgo );
854   // set args
855   for ( int i = 1; i <= myArgMethods.Length(); ++i ) {
856     if ( myArgMethods( i ) == theCommand->GetMethod() ) {
857       while ( myArgs.Length() < i )
858         myArgs.Append( "[]" );
859       myArgs( i ) = theCommand->GetArg( 1 ); // arg value
860       myArgCommands.push_back( theCommand );
861       return;
862     }
863   }
864   myUnknownCommands.push_back( theCommand );
865 }
866
867 //================================================================================
868 /*!
869  * \brief Finish conversion
870  */
871 //================================================================================
872
873 void _pyHypothesis::Flush()
874 {
875 //   if ( IsWrapped() )
876 //     GetCreationCmd()->Clear();
877 }
878
879 //================================================================================
880 /*!
881  * \brief Remember hypothesis parameter values
882   * \param theCommand - The called hypothesis method
883  */
884 //================================================================================
885
886 void _pyComplexParamHypo::Process( const Handle(_pyCommand)& theCommand)
887 {
888   // ex: hyp.SetLength(start, 1)
889   //     hyp.SetLength(end,   0)
890   ASSERT(( theCommand->GetMethod() == "SetLength" ));
891   ASSERT(( theCommand->GetArg( 2 ).IsIntegerValue() ));
892   int i = 2 - theCommand->GetArg( 2 ).IntegerValue();
893   while ( myArgs.Length() < i )
894     myArgs.Append( "[]" );
895   myArgs( i ) = theCommand->GetArg( 1 ); // arg value
896   myArgCommands.push_back( theCommand );
897 }
898
899 //================================================================================
900 /*!
901  * \brief additionally to Addition2Creation, clears SetDistrType() command
902   * \param theCmd - AddHypothesis() command
903   * \param theMesh - mesh to which a hypothesis is added
904   * \retval bool - convertion result
905  */
906 //================================================================================
907
908 bool _pyNumberOfSegmentsHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
909                                                 const _pyID&              theMesh)
910 {
911   if ( IsWrappable( theMesh ) && myArgs.Length() > 0 ) {
912     list<Handle(_pyCommand)>::iterator cmd = myUnknownCommands.begin();
913     for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
914       // clear SetDistrType()
915       if ( (*cmd)->GetString().Location( "SetDistrType", 1, (*cmd)->Length() ))
916         (*cmd)->Clear();
917     }
918   }
919   return _pyHypothesis::Addition2Creation( theCmd, theMesh );
920 }
921
922 //================================================================================
923 /*!
924  * \brief _pyAlgorithm constructor
925   * \param theCreationCmd - The command like "algo = smeshgen.CreateHypothesis(type,lib)"
926  */
927 //================================================================================
928
929 _pyAlgorithm::_pyAlgorithm(const Handle(_pyCommand)& theCreationCmd)
930   : _pyHypothesis( theCreationCmd )
931 {
932   myIsAlgo = true;
933 }
934
935 //================================================================================
936 /*!
937  * \brief Convert the command adding an algorithm to mesh
938   * \param theCmd - The command like mesh.AddHypothesis( geom, algo )
939   * \param theMesh - The mesh needing this algo 
940   * \retval bool - false if the command cant be converted
941  */
942 //================================================================================
943   
944 bool _pyAlgorithm::Addition2Creation( const Handle(_pyCommand)& theCmd,
945                                       const _pyID&              theMeshID)
946 {
947   if ( IsWrappable( theMeshID )) {
948
949     myGeom = theCmd->GetArg( 1 );
950
951     // mesh.AddHypothesis(geom,algo) --> theMeshID.myCreationMethod()
952     if ( _pyHypothesis::Addition2Creation( theCmd, theMeshID )) {
953       theGen->SetAccessorMethod( GetID(), "GetAlgorithm()" );
954       return true;
955     }
956   }
957   return false;
958 }
959
960 //================================================================================
961 /*!
962  * \brief Return starting position of a part of python command
963   * \param thePartIndex - The index of command part
964   * \retval int - Part position
965  */
966 //================================================================================
967
968 int _pyCommand::GetBegPos( int thePartIndex )
969 {
970   if ( IsEmpty() )
971     return EMPTY;
972   if ( myBegPos.Length() < thePartIndex )
973     return UNKNOWN;
974   return myBegPos( thePartIndex );
975 }
976
977 //================================================================================
978 /*!
979  * \brief Store starting position of a part of python command
980   * \param thePartIndex - The index of command part
981   * \param thePosition - Part position
982  */
983 //================================================================================
984
985 void _pyCommand::SetBegPos( int thePartIndex, int thePosition )
986 {
987   while ( myBegPos.Length() < thePartIndex )
988     myBegPos.Append( UNKNOWN );
989   myBegPos( thePartIndex ) = thePosition;
990 }
991
992 //================================================================================
993 /*!
994  * \brief Return substring of python command looking like ResultValue = Obj.Meth()
995   * \retval const TCollection_AsciiString & - ResultValue substring
996  */
997 //================================================================================
998
999 const TCollection_AsciiString & _pyCommand::GetResultValue()
1000 {
1001   if ( GetBegPos( RESULT_IND ) == UNKNOWN )
1002   {
1003     int begPos = myString.Location( "=", 1, Length() );
1004     if ( begPos )
1005       myRes = GetWord( myString, begPos, false );
1006     else
1007       begPos = EMPTY;
1008     SetBegPos( RESULT_IND, begPos );
1009   }
1010   return myRes;
1011 }
1012
1013 //================================================================================
1014 /*!
1015  * \brief Return substring of python command looking like ResVal = Object.Meth()
1016   * \retval const TCollection_AsciiString & - Object substring
1017  */
1018 //================================================================================
1019
1020 const TCollection_AsciiString & _pyCommand::GetObject()
1021 {
1022   if ( GetBegPos( OBJECT_IND ) == UNKNOWN )
1023   {
1024     // beginning
1025     int begPos = GetBegPos( RESULT_IND ) + myRes.Length();
1026     if ( begPos < 1 )
1027       begPos = myString.Location( "=", 1, Length() ) + 1;
1028     // store
1029     myObj = GetWord( myString, begPos, true );
1030     SetBegPos( OBJECT_IND, begPos );
1031   }
1032   //SCRUTE(myObj);
1033   return myObj;
1034 }
1035
1036 //================================================================================
1037 /*!
1038  * \brief Return substring of python command looking like ResVal = Obj.Method()
1039   * \retval const TCollection_AsciiString & - Method substring
1040  */
1041 //================================================================================
1042
1043 const TCollection_AsciiString & _pyCommand::GetMethod()
1044 {
1045   if ( GetBegPos( METHOD_IND ) == UNKNOWN )
1046   {
1047     // beginning
1048     int begPos = GetBegPos( OBJECT_IND ) + myObj.Length();
1049     bool forward = true;
1050     if ( begPos < 1 ) {
1051       begPos = myString.Location( "(", 1, Length() ) - 1;
1052       forward = false;
1053     }
1054     // store
1055     myMeth = GetWord( myString, begPos, forward );
1056     SetBegPos( METHOD_IND, begPos );
1057   }
1058   //SCRUTE(myMeth);
1059   return myMeth;
1060 }
1061
1062 //================================================================================
1063 /*!
1064  * \brief Return substring of python command looking like ResVal = Obj.Meth(Arg1,...)
1065   * \retval const TCollection_AsciiString & - Arg<index> substring
1066  */
1067 //================================================================================
1068
1069 const TCollection_AsciiString & _pyCommand::GetArg( int index )
1070 {
1071   if ( GetBegPos( ARG1_IND ) == UNKNOWN )
1072   {
1073     // find all args
1074     int begPos = GetBegPos( METHOD_IND ) + myMeth.Length();
1075     if ( begPos < 1 )
1076       begPos = myString.Location( "(", 1, Length() ) + 1;
1077
1078     int i = 0, prevLen = 0;
1079     while ( begPos != EMPTY ) {
1080       begPos += prevLen;
1081       // check if we are looking at the closing parenthesis
1082       while ( begPos <= Length() && isspace( myString.Value( begPos )))
1083         ++begPos;
1084       if ( begPos > Length() || myString.Value( begPos ) == ')' )
1085         break;
1086       myArgs.Append( GetWord( myString, begPos, true, true ));
1087       SetBegPos( ARG1_IND + i, begPos );
1088       prevLen = myArgs.Last().Length();
1089       if ( prevLen == 0 )
1090         myArgs.Remove( myArgs.Length() ); // no more args
1091       i++;
1092     }
1093   }
1094   if ( myArgs.Length() < index )
1095     return theEmptyString;
1096   return myArgs( index );
1097 }
1098
1099 //================================================================================
1100 /*!
1101  * \brief Check if char is a word part
1102   * \param c - The character to check
1103   * \retval bool - The check result
1104  */
1105 //================================================================================
1106
1107 static inline bool isWord(const char c, const bool dotIsWord)
1108 {
1109   return
1110     !isspace(c) && c != ',' && c != '=' && c != ')' && c != '(' && ( dotIsWord || c != '.');
1111 }
1112
1113 //================================================================================
1114 /*!
1115  * \brief Looks for a word in the string and returns word's beginning
1116   * \param theString - The input string
1117   * \param theStartPos - The position to start the search, returning word's beginning
1118   * \param theForward - The search direction
1119   * \retval TCollection_AsciiString - The found word
1120  */
1121 //================================================================================
1122
1123 TCollection_AsciiString _pyCommand::GetWord( const TCollection_AsciiString & theString,
1124                                             int &      theStartPos,
1125                                             const bool theForward,
1126                                             const bool dotIsWord )
1127 {
1128   int beg = theStartPos, end = theStartPos;
1129   theStartPos = EMPTY;
1130   if ( beg < 1 || beg > theString.Length() )
1131     return theEmptyString;
1132
1133   if ( theForward ) { // search forward
1134     // beg
1135     while ( beg <= theString.Length() && !isWord( theString.Value( beg ), dotIsWord))
1136       ++beg;
1137     if ( beg > theString.Length() )
1138       return theEmptyString; // no word found
1139     // end
1140     end = beg + 1;
1141     while ( end <= theString.Length() && isWord( theString.Value( end ), dotIsWord))
1142       ++end;
1143     --end;
1144   }
1145   else {  // search backward
1146     // end
1147     while ( end > 0 && !isWord( theString.Value( end ), dotIsWord))
1148       --end;
1149     if ( end == 0 )
1150       return theEmptyString; // no word found
1151     beg = end - 1;
1152     while ( beg > 0 && isWord( theString.Value( beg ), dotIsWord))
1153       --beg;
1154     ++beg;
1155   }
1156   theStartPos = beg;
1157   //cout << theString << " ---- " << beg << " - " << end << endl;
1158   return theString.SubString( beg, end );
1159 }
1160
1161 //================================================================================
1162 /*!
1163  * \brief Look for position where not space char is
1164   * \param theString - The string 
1165   * \param thePos - The position to search from and which returns result
1166   * \retval bool - false if there are only space after thePos in theString
1167  * 
1168  * 
1169  */
1170 //================================================================================
1171
1172 bool _pyCommand::SkipSpaces( const TCollection_AsciiString & theString, int & thePos )
1173 {
1174   if ( thePos < 1 || thePos > theString.Length() )
1175     return false;
1176
1177   while ( thePos <= theString.Length() && isspace( theString.Value( thePos )))
1178     ++thePos;
1179
1180   return thePos <= theString.Length();
1181 }
1182
1183 //================================================================================
1184 /*!
1185  * \brief Modify a part of the command
1186   * \param thePartIndex - The index of the part
1187   * \param thePart - The new part string
1188   * \param theOldPart - The old part
1189  */
1190 //================================================================================
1191
1192 void _pyCommand::SetPart(int thePartIndex, const TCollection_AsciiString& thePart,
1193                         TCollection_AsciiString& theOldPart)
1194 {
1195   int pos = GetBegPos( thePartIndex );
1196   if ( pos <= Length() && theOldPart != thePart)
1197   {
1198     TCollection_AsciiString seperator;
1199     if ( pos < 1 ) {
1200       pos = GetBegPos( thePartIndex + 1 );
1201       if ( pos < 1 ) return;
1202       switch ( thePartIndex ) {
1203       case RESULT_IND: seperator = " = "; break;
1204       case OBJECT_IND: seperator = "."; break;
1205       case METHOD_IND: seperator = "()"; break;
1206       default:;
1207       }
1208     }      
1209     myString.Remove( pos, theOldPart.Length() );
1210     if ( !seperator.IsEmpty() )
1211       myString.Insert( pos , seperator );
1212     myString.Insert( pos, thePart );
1213     // update starting positions of the following parts
1214     int posDelta = thePart.Length() + seperator.Length() - theOldPart.Length();
1215     for ( int i = thePartIndex + 1; i <= myBegPos.Length(); ++i ) {
1216       if ( myBegPos( i ) > 0 )
1217         myBegPos( i ) += posDelta;
1218     }
1219     theOldPart = thePart;
1220   }
1221 }
1222
1223 //================================================================================
1224 /*!
1225  * \brief Set agrument
1226   * \param index - The argument index, it counts from 1
1227   * \param theArg - The argument string
1228  */
1229 //================================================================================
1230
1231 void _pyCommand::SetArg( int index, const TCollection_AsciiString& theArg)
1232 {
1233   FindAllArgs();
1234   int argInd = ARG1_IND + index - 1;
1235   int pos = GetBegPos( argInd );
1236   if ( pos < 1 ) // no index-th arg exist, append inexistent args
1237   {
1238     // find a closing parenthesis
1239     pos = Length();
1240     while ( pos > 0 && myString.Value( pos ) != ')' )
1241       --pos;
1242     if ( pos == 0 ) { // no parentheses at all
1243       myString += "()";
1244       pos = Length();
1245     }
1246     while ( myArgs.Length() < index ) {
1247       if ( myArgs.Length() )
1248         myString.Insert( pos++, "," );
1249       myArgs.Append("None");
1250       myString.Insert( pos, myArgs.Last() );
1251       SetBegPos( ARG1_IND + myArgs.Length() - 1, pos );
1252       pos += myArgs.Last().Length();
1253     }
1254   }
1255   SetPart( argInd, theArg, myArgs( index ));
1256 }
1257
1258 //================================================================================
1259 /*!
1260  * \brief Empty arg list
1261  */
1262 //================================================================================
1263
1264 void _pyCommand::RemoveArgs()
1265 {
1266   if ( int pos = myString.Location( '(', 1, Length() ))
1267     myString.Trunc( pos );
1268   myString += ")";
1269   myArgs.Clear();
1270   if ( myBegPos.Length() >= ARG1_IND )
1271     myBegPos.Remove( ARG1_IND, myBegPos.Length() );
1272 }
1273
1274 //================================================================================
1275 /*!
1276  * \brief Set dependent commands after this one
1277  */
1278 //================================================================================
1279
1280 bool _pyCommand::SetDependentCmdsAfter() const
1281 {
1282   bool orderChanged = false;
1283   list< Handle(_pyCommand)>::const_iterator cmd = myDependentCmds.begin();
1284   for ( ; cmd != myDependentCmds.end(); ++cmd ) {
1285     if ( (*cmd)->GetOrderNb() < GetOrderNb() ) {
1286       orderChanged = true;
1287       theGen->SetCommandAfter( *cmd, this );
1288       (*cmd)->SetDependentCmdsAfter();
1289     }
1290   }
1291   return orderChanged;
1292 }