Salome HOME
Fix for Bug IPAL11843:
[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)> aNewCommandsList;
913     list<TCollection_AsciiString> aNewList;
914     list<TCollection_AsciiString>::iterator aNewListIter;
915     
916     list<Handle(_pyCommand)>::reverse_iterator cmd = myUnknownCommands.rbegin();
917     for ( ; cmd != myUnknownCommands.rend(); ++cmd ) {
918       aNewListIter = find(aNewList.begin(),aNewList.end(),(*cmd)->GetMethod());
919       if(aNewListIter == aNewList.end()){
920         aNewList.push_front((*cmd)->GetMethod());
921         aNewCommandsList.push_front((*cmd));
922       } else {
923         (*cmd)->Clear();
924       }
925     }
926     myUnknownCommands = aNewCommandsList;
927   }
928   return _pyHypothesis::Addition2Creation( theCmd, theMesh );
929 }
930
931 //================================================================================
932 /*!
933  * \brief _pyAlgorithm constructor
934   * \param theCreationCmd - The command like "algo = smeshgen.CreateHypothesis(type,lib)"
935  */
936 //================================================================================
937
938 _pyAlgorithm::_pyAlgorithm(const Handle(_pyCommand)& theCreationCmd)
939   : _pyHypothesis( theCreationCmd )
940 {
941   myIsAlgo = true;
942 }
943
944 //================================================================================
945 /*!
946  * \brief Convert the command adding an algorithm to mesh
947   * \param theCmd - The command like mesh.AddHypothesis( geom, algo )
948   * \param theMesh - The mesh needing this algo 
949   * \retval bool - false if the command cant be converted
950  */
951 //================================================================================
952   
953 bool _pyAlgorithm::Addition2Creation( const Handle(_pyCommand)& theCmd,
954                                       const _pyID&              theMeshID)
955 {
956   if ( IsWrappable( theMeshID )) {
957
958     myGeom = theCmd->GetArg( 1 );
959
960     // mesh.AddHypothesis(geom,algo) --> theMeshID.myCreationMethod()
961     if ( _pyHypothesis::Addition2Creation( theCmd, theMeshID )) {
962       theGen->SetAccessorMethod( GetID(), "GetAlgorithm()" );
963       return true;
964     }
965   }
966   return false;
967 }
968
969 //================================================================================
970 /*!
971  * \brief Return starting position of a part of python command
972   * \param thePartIndex - The index of command part
973   * \retval int - Part position
974  */
975 //================================================================================
976
977 int _pyCommand::GetBegPos( int thePartIndex )
978 {
979   if ( IsEmpty() )
980     return EMPTY;
981   if ( myBegPos.Length() < thePartIndex )
982     return UNKNOWN;
983   return myBegPos( thePartIndex );
984 }
985
986 //================================================================================
987 /*!
988  * \brief Store starting position of a part of python command
989   * \param thePartIndex - The index of command part
990   * \param thePosition - Part position
991  */
992 //================================================================================
993
994 void _pyCommand::SetBegPos( int thePartIndex, int thePosition )
995 {
996   while ( myBegPos.Length() < thePartIndex )
997     myBegPos.Append( UNKNOWN );
998   myBegPos( thePartIndex ) = thePosition;
999 }
1000
1001 //================================================================================
1002 /*!
1003  * \brief Return substring of python command looking like ResultValue = Obj.Meth()
1004   * \retval const TCollection_AsciiString & - ResultValue substring
1005  */
1006 //================================================================================
1007
1008 const TCollection_AsciiString & _pyCommand::GetResultValue()
1009 {
1010   if ( GetBegPos( RESULT_IND ) == UNKNOWN )
1011   {
1012     int begPos = myString.Location( "=", 1, Length() );
1013     if ( begPos )
1014       myRes = GetWord( myString, begPos, false );
1015     else
1016       begPos = EMPTY;
1017     SetBegPos( RESULT_IND, begPos );
1018   }
1019   return myRes;
1020 }
1021
1022 //================================================================================
1023 /*!
1024  * \brief Return substring of python command looking like ResVal = Object.Meth()
1025   * \retval const TCollection_AsciiString & - Object substring
1026  */
1027 //================================================================================
1028
1029 const TCollection_AsciiString & _pyCommand::GetObject()
1030 {
1031   if ( GetBegPos( OBJECT_IND ) == UNKNOWN )
1032   {
1033     // beginning
1034     int begPos = GetBegPos( RESULT_IND ) + myRes.Length();
1035     if ( begPos < 1 )
1036       begPos = myString.Location( "=", 1, Length() ) + 1;
1037     // store
1038     myObj = GetWord( myString, begPos, true );
1039     SetBegPos( OBJECT_IND, begPos );
1040   }
1041   //SCRUTE(myObj);
1042   return myObj;
1043 }
1044
1045 //================================================================================
1046 /*!
1047  * \brief Return substring of python command looking like ResVal = Obj.Method()
1048   * \retval const TCollection_AsciiString & - Method substring
1049  */
1050 //================================================================================
1051
1052 const TCollection_AsciiString & _pyCommand::GetMethod()
1053 {
1054   if ( GetBegPos( METHOD_IND ) == UNKNOWN )
1055   {
1056     // beginning
1057     int begPos = GetBegPos( OBJECT_IND ) + myObj.Length();
1058     bool forward = true;
1059     if ( begPos < 1 ) {
1060       begPos = myString.Location( "(", 1, Length() ) - 1;
1061       forward = false;
1062     }
1063     // store
1064     myMeth = GetWord( myString, begPos, forward );
1065     SetBegPos( METHOD_IND, begPos );
1066   }
1067   //SCRUTE(myMeth);
1068   return myMeth;
1069 }
1070
1071 //================================================================================
1072 /*!
1073  * \brief Return substring of python command looking like ResVal = Obj.Meth(Arg1,...)
1074   * \retval const TCollection_AsciiString & - Arg<index> substring
1075  */
1076 //================================================================================
1077
1078 const TCollection_AsciiString & _pyCommand::GetArg( int index )
1079 {
1080   if ( GetBegPos( ARG1_IND ) == UNKNOWN )
1081   {
1082     // find all args
1083     int begPos = GetBegPos( METHOD_IND ) + myMeth.Length();
1084     if ( begPos < 1 )
1085       begPos = myString.Location( "(", 1, Length() ) + 1;
1086
1087     int i = 0, prevLen = 0;
1088     while ( begPos != EMPTY ) {
1089       begPos += prevLen;
1090       // check if we are looking at the closing parenthesis
1091       while ( begPos <= Length() && isspace( myString.Value( begPos )))
1092         ++begPos;
1093       if ( begPos > Length() || myString.Value( begPos ) == ')' )
1094         break;
1095       myArgs.Append( GetWord( myString, begPos, true, true ));
1096       SetBegPos( ARG1_IND + i, begPos );
1097       prevLen = myArgs.Last().Length();
1098       if ( prevLen == 0 )
1099         myArgs.Remove( myArgs.Length() ); // no more args
1100       i++;
1101     }
1102   }
1103   if ( myArgs.Length() < index )
1104     return theEmptyString;
1105   return myArgs( index );
1106 }
1107
1108 //================================================================================
1109 /*!
1110  * \brief Check if char is a word part
1111   * \param c - The character to check
1112   * \retval bool - The check result
1113  */
1114 //================================================================================
1115
1116 static inline bool isWord(const char c, const bool dotIsWord)
1117 {
1118   return
1119     !isspace(c) && c != ',' && c != '=' && c != ')' && c != '(' && ( dotIsWord || c != '.');
1120 }
1121
1122 //================================================================================
1123 /*!
1124  * \brief Looks for a word in the string and returns word's beginning
1125   * \param theString - The input string
1126   * \param theStartPos - The position to start the search, returning word's beginning
1127   * \param theForward - The search direction
1128   * \retval TCollection_AsciiString - The found word
1129  */
1130 //================================================================================
1131
1132 TCollection_AsciiString _pyCommand::GetWord( const TCollection_AsciiString & theString,
1133                                             int &      theStartPos,
1134                                             const bool theForward,
1135                                             const bool dotIsWord )
1136 {
1137   int beg = theStartPos, end = theStartPos;
1138   theStartPos = EMPTY;
1139   if ( beg < 1 || beg > theString.Length() )
1140     return theEmptyString;
1141
1142   if ( theForward ) { // search forward
1143     // beg
1144     while ( beg <= theString.Length() && !isWord( theString.Value( beg ), dotIsWord))
1145       ++beg;
1146     if ( beg > theString.Length() )
1147       return theEmptyString; // no word found
1148     // end
1149     end = beg + 1;
1150     while ( end <= theString.Length() && isWord( theString.Value( end ), dotIsWord))
1151       ++end;
1152     --end;
1153   }
1154   else {  // search backward
1155     // end
1156     while ( end > 0 && !isWord( theString.Value( end ), dotIsWord))
1157       --end;
1158     if ( end == 0 )
1159       return theEmptyString; // no word found
1160     beg = end - 1;
1161     while ( beg > 0 && isWord( theString.Value( beg ), dotIsWord))
1162       --beg;
1163     ++beg;
1164   }
1165   theStartPos = beg;
1166   //cout << theString << " ---- " << beg << " - " << end << endl;
1167   return theString.SubString( beg, end );
1168 }
1169
1170 //================================================================================
1171 /*!
1172  * \brief Look for position where not space char is
1173   * \param theString - The string 
1174   * \param thePos - The position to search from and which returns result
1175   * \retval bool - false if there are only space after thePos in theString
1176  * 
1177  * 
1178  */
1179 //================================================================================
1180
1181 bool _pyCommand::SkipSpaces( const TCollection_AsciiString & theString, int & thePos )
1182 {
1183   if ( thePos < 1 || thePos > theString.Length() )
1184     return false;
1185
1186   while ( thePos <= theString.Length() && isspace( theString.Value( thePos )))
1187     ++thePos;
1188
1189   return thePos <= theString.Length();
1190 }
1191
1192 //================================================================================
1193 /*!
1194  * \brief Modify a part of the command
1195   * \param thePartIndex - The index of the part
1196   * \param thePart - The new part string
1197   * \param theOldPart - The old part
1198  */
1199 //================================================================================
1200
1201 void _pyCommand::SetPart(int thePartIndex, const TCollection_AsciiString& thePart,
1202                         TCollection_AsciiString& theOldPart)
1203 {
1204   int pos = GetBegPos( thePartIndex );
1205   if ( pos <= Length() && theOldPart != thePart)
1206   {
1207     TCollection_AsciiString seperator;
1208     if ( pos < 1 ) {
1209       pos = GetBegPos( thePartIndex + 1 );
1210       if ( pos < 1 ) return;
1211       switch ( thePartIndex ) {
1212       case RESULT_IND: seperator = " = "; break;
1213       case OBJECT_IND: seperator = "."; break;
1214       case METHOD_IND: seperator = "()"; break;
1215       default:;
1216       }
1217     }      
1218     myString.Remove( pos, theOldPart.Length() );
1219     if ( !seperator.IsEmpty() )
1220       myString.Insert( pos , seperator );
1221     myString.Insert( pos, thePart );
1222     // update starting positions of the following parts
1223     int posDelta = thePart.Length() + seperator.Length() - theOldPart.Length();
1224     for ( int i = thePartIndex + 1; i <= myBegPos.Length(); ++i ) {
1225       if ( myBegPos( i ) > 0 )
1226         myBegPos( i ) += posDelta;
1227     }
1228     theOldPart = thePart;
1229   }
1230 }
1231
1232 //================================================================================
1233 /*!
1234  * \brief Set agrument
1235   * \param index - The argument index, it counts from 1
1236   * \param theArg - The argument string
1237  */
1238 //================================================================================
1239
1240 void _pyCommand::SetArg( int index, const TCollection_AsciiString& theArg)
1241 {
1242   FindAllArgs();
1243   int argInd = ARG1_IND + index - 1;
1244   int pos = GetBegPos( argInd );
1245   if ( pos < 1 ) // no index-th arg exist, append inexistent args
1246   {
1247     // find a closing parenthesis
1248     pos = Length();
1249     while ( pos > 0 && myString.Value( pos ) != ')' )
1250       --pos;
1251     if ( pos == 0 ) { // no parentheses at all
1252       myString += "()";
1253       pos = Length();
1254     }
1255     while ( myArgs.Length() < index ) {
1256       if ( myArgs.Length() )
1257         myString.Insert( pos++, "," );
1258       myArgs.Append("None");
1259       myString.Insert( pos, myArgs.Last() );
1260       SetBegPos( ARG1_IND + myArgs.Length() - 1, pos );
1261       pos += myArgs.Last().Length();
1262     }
1263   }
1264   SetPart( argInd, theArg, myArgs( index ));
1265 }
1266
1267 //================================================================================
1268 /*!
1269  * \brief Empty arg list
1270  */
1271 //================================================================================
1272
1273 void _pyCommand::RemoveArgs()
1274 {
1275   if ( int pos = myString.Location( '(', 1, Length() ))
1276     myString.Trunc( pos );
1277   myString += ")";
1278   myArgs.Clear();
1279   if ( myBegPos.Length() >= ARG1_IND )
1280     myBegPos.Remove( ARG1_IND, myBegPos.Length() );
1281 }
1282
1283 //================================================================================
1284 /*!
1285  * \brief Set dependent commands after this one
1286  */
1287 //================================================================================
1288
1289 bool _pyCommand::SetDependentCmdsAfter() const
1290 {
1291   bool orderChanged = false;
1292   list< Handle(_pyCommand)>::const_reverse_iterator cmd = myDependentCmds.rbegin();
1293   for ( ; cmd != myDependentCmds.rend(); ++cmd ) {
1294     if ( (*cmd)->GetOrderNb() < GetOrderNb() ) {
1295       orderChanged = true;
1296       theGen->SetCommandAfter( *cmd, this );
1297       (*cmd)->SetDependentCmdsAfter();
1298     }
1299   }
1300   return orderChanged;
1301 }