Salome HOME
PAL17694 (New Tool About Hexahedral Meshing)
[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.salome-platform.org/ or email : webmaster.salome@opencascade.com
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 (_pyMeshEditor      ,_pyObject);
45 IMPLEMENT_STANDARD_HANDLE (_pyHypothesis      ,_pyObject);
46 IMPLEMENT_STANDARD_HANDLE (_pyAlgorithm       ,_pyHypothesis);
47 IMPLEMENT_STANDARD_HANDLE (_pyComplexParamHypo,_pyHypothesis);
48 IMPLEMENT_STANDARD_HANDLE (_pyNumberOfSegmentsHyp,_pyHypothesis);
49
50 IMPLEMENT_STANDARD_RTTIEXT(_pyObject          ,Standard_Transient);
51 IMPLEMENT_STANDARD_RTTIEXT(_pyCommand         ,Standard_Transient);
52 IMPLEMENT_STANDARD_RTTIEXT(_pyGen             ,_pyObject);
53 IMPLEMENT_STANDARD_RTTIEXT(_pyMesh            ,_pyObject);
54 IMPLEMENT_STANDARD_RTTIEXT(_pyMeshEditor      ,_pyObject);
55 IMPLEMENT_STANDARD_RTTIEXT(_pyHypothesis      ,_pyObject);
56 IMPLEMENT_STANDARD_RTTIEXT(_pyAlgorithm       ,_pyHypothesis);
57 IMPLEMENT_STANDARD_RTTIEXT(_pyComplexParamHypo,_pyHypothesis);
58 IMPLEMENT_STANDARD_RTTIEXT(_pyNumberOfSegmentsHyp,_pyHypothesis);
59 IMPLEMENT_STANDARD_RTTIEXT(_pyLayerDistributionHypo,_pyHypothesis);
60 IMPLEMENT_STANDARD_RTTIEXT(_pySegmentLengthAroundVertexHyp,_pyHypothesis);
61
62 using namespace std;
63 using SMESH::TPythonDump;
64
65 /*!
66  * \brief Container of commands into which the initial script is split.
67  *        It also contains data coresponding to SMESH_Gen contents
68  */
69 static Handle(_pyGen) theGen;
70
71 static TCollection_AsciiString theEmptyString;
72
73 //#define DUMP_CONVERSION
74
75 #if !defined(_DEBUG_) && defined(DUMP_CONVERSION)
76 #undef DUMP_CONVERSION
77 #endif
78
79 namespace {
80
81   //================================================================================
82   /*!
83    * \brief Set of TCollection_AsciiString initialized by C array of C strings
84    */
85   //================================================================================
86
87   struct TStringSet: public set<TCollection_AsciiString>
88   {
89     /*!
90      * \brief Filling. The last string must be ""
91      */
92     void Insert(const char* names[]) {
93       for ( int i = 0; names[i][0] ; ++i )
94         insert( (char*) names[i] );
95     }
96     /*!
97      * \brief Check if a string is in
98      */
99     bool Contains(const TCollection_AsciiString& name ) {
100       return find( name ) != end();
101     }
102   };
103 }
104
105 //================================================================================
106 /*!
107  * \brief Convert python script using commands of smesh.py
108   * \param theScript - Input script
109   * \retval TCollection_AsciiString - Convertion result
110   *
111   * Class SMESH_2smeshpy declared in SMESH_PythonDump.hxx
112  */
113 //================================================================================
114
115 TCollection_AsciiString
116 SMESH_2smeshpy::ConvertScript(const TCollection_AsciiString& theScript,
117                               Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod)
118 {
119   theGen = new _pyGen( theEntry2AccessorMethod );
120
121   // split theScript into separate commands
122   int from = 1, end = theScript.Length(), to;
123   while ( from < end && ( to = theScript.Location( "\n", from, end )))
124   {
125     if ( to != from )
126       // cut out and store a command
127       theGen->AddCommand( theScript.SubString( from, to - 1 ));
128     from = to + 1;
129   }
130   // finish conversion
131   theGen->Flush();
132 #ifdef DUMP_CONVERSION
133   cout << endl << " ######## RESULT ######## " << endl<< endl;
134 #endif
135   // reorder commands after conversion
136   list< Handle(_pyCommand) >::iterator cmd;
137   bool orderChanges;
138   do {
139     orderChanges = false;
140     for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
141       if ( (*cmd)->SetDependentCmdsAfter() )
142         orderChanges = true;
143   } while ( orderChanges );
144   
145   // concat commands back into a script
146   TCollection_AsciiString aScript;
147   for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
148   {
149 #ifdef DUMP_CONVERSION
150     cout << "## COM " << (*cmd)->GetOrderNb() << ": "<< (*cmd)->GetString() << endl;
151 #endif
152     if ( !(*cmd)->IsEmpty() ) {
153       aScript += "\n";
154       aScript += (*cmd)->GetString();
155     }
156   }
157   aScript += "\n";
158
159   theGen.Nullify();
160
161   return aScript;
162 }
163
164 //================================================================================
165 /*!
166  * \brief _pyGen constructor
167  */
168 //================================================================================
169
170 _pyGen::_pyGen(Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod)
171   : _pyObject( new _pyCommand( TPythonDump::SMESHGenName(), 0 )),
172     myID2AccessorMethod( theEntry2AccessorMethod )
173 {
174   myNbCommands = 0;
175   myHasPattern = false;
176   // make that GetID() to return TPythonDump::SMESHGenName()
177   GetCreationCmd()->GetString() += "=";
178 }
179
180 //================================================================================
181 /*!
182  * \brief name of SMESH_Gen in smesh.py
183  */
184 //================================================================================
185
186 const char* _pyGen::AccessorMethod() const
187 {
188   return SMESH_2smeshpy::GenName();
189 }
190
191 //================================================================================
192 /*!
193  * \brief Convert a command using a specific converter
194   * \param theCommand - the command to convert
195  */
196 //================================================================================
197
198 Handle(_pyCommand) _pyGen::AddCommand( const TCollection_AsciiString& theCommand)
199 {
200   // store theCommand in the sequence
201   myCommands.push_back( new _pyCommand( theCommand, ++myNbCommands ));
202
203   Handle(_pyCommand) aCommand = myCommands.back();
204 #ifdef DUMP_CONVERSION
205   cout << "## COM " << myNbCommands << ": "<< aCommand->GetString() << endl;
206 #endif
207
208   _pyID objID = aCommand->GetObject();
209
210   if ( objID.IsEmpty() )
211     return aCommand;
212
213   // SMESH_Gen method?
214   if ( objID == this->GetID() ) {
215     this->Process( aCommand );
216     return aCommand;
217   }
218   // SMESH_Mesh method?
219   map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( objID );
220   if ( id_mesh != myMeshes.end() ) {
221     if ( aCommand->GetMethod() == "GetMeshEditor" ) { // MeshEditor creation
222       _pyID editorID = aCommand->GetResultValue();
223       Handle(_pyMeshEditor) editor = new _pyMeshEditor( aCommand );
224       myMeshEditors.insert( make_pair( editorID, editor ));
225       return aCommand;
226     }
227     id_mesh->second->Process( aCommand );
228     return aCommand;
229   }
230   // SMESH_MeshEditor method?
231   map< _pyID, Handle(_pyMeshEditor) >::iterator id_editor = myMeshEditors.find( objID );
232   if ( id_editor != myMeshEditors.end() ) {
233     id_editor->second->Process( aCommand );
234     return aCommand;
235   }
236   // SMESH_Hypothesis method?
237   list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
238   for ( ; hyp != myHypos.end(); ++hyp )
239     if ( !(*hyp)->IsAlgo() && objID == (*hyp)->GetID() ) {
240       (*hyp)->Process( aCommand );
241       return aCommand;
242     }
243
244   // Add access to a wrapped mesh
245   AddMeshAccessorMethod( aCommand );
246
247   // Add access to a wrapped algorithm
248   AddAlgoAccessorMethod( aCommand ); // ??? what if algo won't be wrapped at all ???
249
250   // PAL12227. PythonDump was not updated at proper time; result is
251   //     aCriteria.append(SMESH.Filter.Criterion(17,26,0,'L1',26,25,1e-07,SMESH.EDGE,-1))
252   // TypeError: __init__() takes exactly 11 arguments (10 given)
253   char wrongCommand[] = "SMESH.Filter.Criterion(";
254   if ( int beg = theCommand.Location( wrongCommand, 1, theCommand.Length() ))
255   {
256     _pyCommand tmpCmd( theCommand.SubString( beg, theCommand.Length() ), -1);
257     // there must be 10 arguments, 5-th arg ThresholdID is missing,
258     const int wrongNbArgs = 9, missingArg = 5;
259     if ( tmpCmd.GetNbArgs() == wrongNbArgs )
260     {
261       for ( int i = wrongNbArgs; i > missingArg; --i )
262         tmpCmd.SetArg( i + 1, tmpCmd.GetArg( i ));
263       tmpCmd.SetArg(  missingArg, "''");
264       aCommand->GetString().Trunc( beg - 1 );
265       aCommand->GetString() += tmpCmd.GetString();
266     }
267   }
268   return aCommand;
269 }
270
271 //================================================================================
272 /*!
273  * \brief Convert the command or remember it for later conversion 
274   * \param theCommand - The python command calling a method of SMESH_Gen
275  */
276 //================================================================================
277
278 void _pyGen::Process( const Handle(_pyCommand)& theCommand )
279 {
280   // there are methods to convert:
281   // CreateMesh( shape )
282   // Concatenate( [mesh1, ...], ... )
283   // CreateHypothesis( theHypType, theLibName )
284   // Compute( mesh, geom )
285
286   // mesh creation
287   if ( theCommand->GetMethod() == "CreateMesh" ||
288        theCommand->GetMethod() == "CreateEmptyMesh" )
289   {
290     Handle(_pyMesh) mesh = new _pyMesh( theCommand );
291     myMeshes.insert( make_pair( mesh->GetID(), mesh ));
292     return;
293   }
294
295   // CreateHypothesis()
296   if ( theCommand->GetMethod() == "CreateHypothesis" )
297   {
298     myHypos.push_back( _pyHypothesis::NewHypothesis( theCommand ));
299     return;
300   }
301
302   // smeshgen.Compute( mesh, geom ) --> mesh.Compute()
303   if ( theCommand->GetMethod() == "Compute" )
304   {
305     const _pyID& meshID = theCommand->GetArg( 1 );
306     map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( meshID );
307     if ( id_mesh != myMeshes.end() ) {
308       theCommand->SetObject( meshID );
309       theCommand->RemoveArgs();
310       id_mesh->second->Flush();
311       return;
312     }
313   }
314
315   // leave only one smeshgen.GetPattern() in the script
316   if ( theCommand->GetMethod() == "GetPattern" ) {
317     if ( myHasPattern ) {
318       theCommand->Clear();
319       return;
320     }
321     myHasPattern = true;
322   }
323
324   // Concatenate( [mesh1, ...], ... )
325   if ( theCommand->GetMethod() == "Concatenate" )
326   {
327     AddMeshAccessorMethod( theCommand );
328   }
329
330   // Replace name of SMESH_Gen
331
332   // names of SMESH_Gen methods fully equal to methods defined in smesh.py
333   static TStringSet smeshpyMethods;
334   if ( smeshpyMethods.empty() ) {
335     const char * names[] =
336       { "SetEmbeddedMode","IsEmbeddedMode","SetCurrentStudy","GetCurrentStudy",
337         "GetPattern","GetSubShapesId",
338         "" }; // <- mark of array end
339     smeshpyMethods.Insert( names );
340   }
341   if ( smeshpyMethods.Contains( theCommand->GetMethod() ))
342     // smeshgen.Method() --> smesh.Method()
343     theCommand->SetObject( SMESH_2smeshpy::SmeshpyName() );
344   else
345     // smeshgen.Method() --> smesh.smesh.Method()
346     theCommand->SetObject( SMESH_2smeshpy::GenName() );
347 }
348
349 //================================================================================
350 /*!
351  * \brief Convert the remembered commands
352  */
353 //================================================================================
354
355 void _pyGen::Flush()
356 {
357   map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.begin();
358   for ( ; id_mesh != myMeshes.end(); ++id_mesh )
359     if ( ! id_mesh->second.IsNull() )
360       id_mesh->second->Flush();
361
362   list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
363   for ( ; hyp != myHypos.end(); ++hyp )
364     if ( !hyp->IsNull() ) {
365       (*hyp)->Flush();
366       // smeshgen.CreateHypothesis() --> smesh.smesh.CreateHypothesis()
367       if ( !(*hyp)->IsWrapped() )
368         (*hyp)->GetCreationCmd()->SetObject( SMESH_2smeshpy::GenName() );
369     }
370 }
371
372 //================================================================================
373 /*!
374  * \brief Add access method to mesh that is an object or an argument
375   * \param theCmd - command to add access method
376   * \retval bool - true if added
377  */
378 //================================================================================
379
380 bool _pyGen::AddMeshAccessorMethod( Handle(_pyCommand) theCmd ) const
381 {
382   bool added = false;
383   map< _pyID, Handle(_pyMesh) >::const_iterator id_mesh = myMeshes.begin();
384   for ( ; id_mesh != myMeshes.end(); ++id_mesh ) {
385     if ( theCmd->AddAccessorMethod( id_mesh->first, id_mesh->second->AccessorMethod() ))
386       added = true;
387   }
388   return added;
389 }
390
391 //================================================================================
392 /*!
393  * \brief Add access method to algo that is an object or an argument
394   * \param theCmd - command to add access method
395   * \retval bool - true if added
396  */
397 //================================================================================
398
399 bool _pyGen::AddAlgoAccessorMethod( Handle(_pyCommand) theCmd ) const
400 {
401   bool added = false;
402   list< Handle(_pyHypothesis) >::const_iterator hyp = myHypos.begin();
403   for ( ; hyp != myHypos.end(); ++hyp ) {
404     if ( (*hyp)->IsAlgo() && /*(*hyp)->IsWrapped() &&*/
405          theCmd->AddAccessorMethod( (*hyp)->GetID(), (*hyp)->AccessorMethod() ))
406       added = true;
407   }
408   return added;
409 }
410
411 //================================================================================
412 /*!
413  * \brief Find hypothesis by ID (entry)
414   * \param theHypID - The hypothesis ID
415   * \retval Handle(_pyHypothesis) - The found hypothesis
416  */
417 //================================================================================
418
419 Handle(_pyHypothesis) _pyGen::FindHyp( const _pyID& theHypID )
420 {
421   list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
422   for ( ; hyp != myHypos.end(); ++hyp )
423     if ( !hyp->IsNull() && theHypID == (*hyp)->GetID() )
424       return *hyp;
425   return Handle(_pyHypothesis)();
426 }
427
428 //================================================================================
429 /*!
430  * \brief Find algorithm the created algorithm
431   * \param theGeom - The shape ID the algorithm was created on
432   * \param theMesh - The mesh ID that created the algorithm
433   * \param dim - The algo dimension
434   * \retval Handle(_pyHypothesis) - The found algo
435  */
436 //================================================================================
437
438 Handle(_pyHypothesis) _pyGen::FindAlgo( const _pyID& theGeom, const _pyID& theMesh,
439                                         const Handle(_pyHypothesis)& theHypothesis )
440 {
441   list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
442   for ( ; hyp != myHypos.end(); ++hyp )
443     if ( !hyp->IsNull() &&
444          (*hyp)->IsAlgo() &&
445          theHypothesis->CanBeCreatedBy( (*hyp)->GetAlgoType() ) &&
446          (*hyp)->GetGeom() == theGeom &&
447          (*hyp)->GetMesh() == theMesh )
448       return *hyp;
449   return 0;
450 }
451
452 //================================================================================
453 /*!
454  * \brief Change order of commands in the script
455   * \param theCmd1 - One command
456   * \param theCmd2 - Another command
457  */
458 //================================================================================
459
460 void _pyGen::ExchangeCommands( Handle(_pyCommand) theCmd1, Handle(_pyCommand) theCmd2 )
461 {
462   list< Handle(_pyCommand) >::iterator pos1, pos2;
463   pos1 = find( myCommands.begin(), myCommands.end(), theCmd1 );
464   pos2 = find( myCommands.begin(), myCommands.end(), theCmd2 );
465   myCommands.insert( pos1, theCmd2 );
466   myCommands.insert( pos2, theCmd1 );
467   myCommands.erase( pos1 );
468   myCommands.erase( pos2 );
469
470   int nb1 = theCmd1->GetOrderNb();
471   theCmd1->SetOrderNb( theCmd2->GetOrderNb() );
472   theCmd2->SetOrderNb( nb1 );
473 //   cout << "BECOME " << theCmd1->GetOrderNb() << "\t" << theCmd1->GetString() << endl
474 //        << "BECOME " << theCmd2->GetOrderNb() << "\t" << theCmd2->GetString() << endl << endl;
475 }
476
477 //================================================================================
478 /*!
479  * \brief Set one command after the other
480   * \param theCmd - Command to move
481   * \param theAfterCmd - Command ater which to insert the first one
482  */
483 //================================================================================
484
485 void _pyGen::SetCommandAfter( Handle(_pyCommand) theCmd, Handle(_pyCommand) theAfterCmd )
486 {
487 #ifdef _DEBUG_
488 //cout << "SET\t" << theAfterCmd->GetString() << endl << "BEFORE\t" << theCmd->GetString() << endl<<endl;
489 #endif
490   list< Handle(_pyCommand) >::iterator pos;
491   pos = find( myCommands.begin(), myCommands.end(), theCmd );
492   myCommands.erase( pos );
493   pos = find( myCommands.begin(), myCommands.end(), theAfterCmd );
494   myCommands.insert( ++pos, theCmd );
495
496   int i = 1;
497   for ( pos = myCommands.begin(); pos != myCommands.end(); ++pos)
498     (*pos)->SetOrderNb( i++ );
499 }
500
501 //================================================================================
502 /*!
503  * \brief Set method to access to object wrapped with python class
504   * \param theID - The wrapped object entry
505   * \param theMethod - The accessor method
506  */
507 //================================================================================
508
509 void _pyGen::SetAccessorMethod(const _pyID& theID, const char* theMethod )
510 {
511   myID2AccessorMethod.Bind( theID, (char*) theMethod );
512 }
513
514 //================================================================================
515 /*!
516  * \brief Find out type of geom group
517   * \param grpID - The geom group entry
518   * \retval int - The type
519  */
520 //================================================================================
521
522 static bool sameGroupType( const _pyID&                   grpID,
523                            const TCollection_AsciiString& theType)
524 {
525   // define group type as smesh.Mesh.Group() does
526   int type = -1;
527   SALOMEDS::Study_var study = SMESH_Gen_i::GetSMESHGen()->GetCurrentStudy();
528   SALOMEDS::SObject_var aSObj = study->FindObjectID( grpID.ToCString() );
529   if ( !aSObj->_is_nil() ) {
530     GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow( aSObj->GetObject() );
531     if ( !aGeomObj->_is_nil() ) {
532       switch ( aGeomObj->GetShapeType() ) {
533       case GEOM::VERTEX: type = SMESH::NODE; break;
534       case GEOM::EDGE:   type = SMESH::EDGE; break;
535       case GEOM::FACE:   type = SMESH::FACE; break;
536       case GEOM::SOLID:
537       case GEOM::SHELL:  type = SMESH::VOLUME; break;
538       case GEOM::COMPOUND: {
539         GEOM::GEOM_Gen_ptr aGeomGen = SMESH_Gen_i::GetSMESHGen()->GetGeomEngine();
540         if ( !aGeomGen->_is_nil() ) {
541           GEOM::GEOM_IGroupOperations_var aGrpOp =
542             aGeomGen->GetIGroupOperations( study->StudyId() );
543           if ( !aGrpOp->_is_nil() ) {
544             switch ( aGrpOp->GetType( aGeomObj )) {
545             case TopAbs_VERTEX: type = SMESH::NODE; break;
546             case TopAbs_EDGE:   type = SMESH::EDGE; break;
547             case TopAbs_FACE:   type = SMESH::FACE; break;
548             case TopAbs_SOLID:  type = SMESH::VOLUME; break;
549             default:;
550             }
551           }
552         }
553       }
554       default:;
555       }
556     }
557   }
558   if ( type < 0 ) {
559     MESSAGE("Type of the group " << grpID << " not found");
560     return false;
561   }
562   if ( theType.IsIntegerValue() )
563     return type == theType.IntegerValue();
564
565   switch ( type ) {
566   case SMESH::NODE:   return theType.Location( "NODE", 1, theType.Length() );
567   case SMESH::EDGE:   return theType.Location( "EDGE", 1, theType.Length() );
568   case SMESH::FACE:   return theType.Location( "FACE", 1, theType.Length() );
569   case SMESH::VOLUME: return theType.Location( "VOLUME", 1, theType.Length() );
570   default:;
571   }
572   return false;
573 }
574
575 //================================================================================
576 /*!
577  * \brief 
578   * \param theCreationCmd - 
579  */
580 //================================================================================
581
582 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd):
583   _pyObject(theCreationCmd), myHasEditor(false)
584 {
585   // convert my creation command
586   Handle(_pyCommand) creationCmd = GetCreationCmd();
587   creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
588   creationCmd->SetMethod( "Mesh" );
589
590   theGen->SetAccessorMethod( GetID(), "GetMesh()" );
591 }
592
593 //================================================================================
594 /*!
595  * \brief Convert a IDL API command of SMESH::Mesh to a method call of python Mesh
596   * \param theCommand - Engine method called for this mesh
597  */
598 //================================================================================
599
600 void _pyMesh::Process( const Handle(_pyCommand)& theCommand )
601 {
602   // some methods of SMESH_Mesh interface needs special conversion
603   // to methods of Mesh python class
604   //
605   // 1. GetSubMesh(geom, name) + AddHypothesis(geom, algo)
606   //     --> in Mesh_Algorithm.Create(mesh, geom, hypo, so)
607   // 2. AddHypothesis(geom, hyp)
608   //     --> in Mesh_Algorithm.Hypothesis(hyp, args, so)
609   // 3. CreateGroupFromGEOM(type, name, grp)
610   //     --> in Mesh.Group(grp, name="")
611   // 4. ExportToMED(f, auto_groups, version)
612   //     --> in Mesh.ExportMED( f, auto_groups, version )
613   // 5. etc
614
615   const TCollection_AsciiString method = theCommand->GetMethod();
616   // ----------------------------------------------------------------------
617   if ( method == "GetSubMesh" ) {
618     mySubmeshes.push_back( theCommand );
619   }
620   // ----------------------------------------------------------------------
621   else if ( method == "AddHypothesis" ) { // mesh.AddHypothesis(geom, HYPO )
622     myAddHypCmds.push_back( theCommand );
623     // set mesh to hypo
624     const _pyID& hypID = theCommand->GetArg( 2 );
625     Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
626     if ( !hyp.IsNull() ) {
627       myHypos.push_back( hyp );
628       if ( hyp->GetMesh().IsEmpty() )
629         hyp->SetMesh( this->GetID() );
630     }
631   }
632   // ----------------------------------------------------------------------
633   else if ( method == "CreateGroupFromGEOM" ) {// (type, name, grp)
634     _pyID grp = theCommand->GetArg( 3 );
635     if ( sameGroupType( grp, theCommand->GetArg( 1 )) ) { // --> Group(grp)
636       theCommand->SetMethod( "Group" );
637       theCommand->RemoveArgs();
638       theCommand->SetArg( 1, grp );
639     }
640     else {
641       AddMeshAccess( theCommand );
642     }
643   }
644   // ----------------------------------------------------------------------
645   else if ( method == "ExportToMED" ) { // ExportToMED() --> ExportMED()
646     theCommand->SetMethod( "ExportMED" );
647   }
648   // ----------------------------------------------------------------------
649   else if ( method == "CreateGroup" ) { // CreateGroup() --> CreateEmptyGroup()
650     theCommand->SetMethod( "CreateEmptyGroup" );
651   }
652   // ----------------------------------------------------------------------
653   else if ( method == "RemoveHypothesis" ) // (geom, hyp)
654   {
655     _pyID hypID = theCommand->GetArg( 2 );
656
657     // check if this mesh still has corresponding addition command
658     bool hasAddCmd = false;
659     list< Handle(_pyCommand) >::iterator cmd = myAddHypCmds.begin();
660     while ( cmd != myAddHypCmds.end() )
661     {
662       // AddHypothesis(geom, hyp)
663       if ( hypID == (*cmd)->GetArg( 2 )) { // erase both (add and remove) commands
664         theCommand->Clear();
665         (*cmd)->Clear();
666         cmd = myAddHypCmds.erase( cmd );
667         hasAddCmd = true;
668       }
669       else {
670         ++cmd;
671       }
672     }
673     Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
674     if ( ! hasAddCmd ) { // hypo addition already wrapped
675       // RemoveHypothesis(geom, hyp) --> RemoveHypothesis( hyp, geom=0 )
676       _pyID geom = theCommand->GetArg( 1 );
677       theCommand->RemoveArgs();
678       theCommand->SetArg( 1, hypID );
679       if ( geom != GetGeom() )
680         theCommand->SetArg( 2, geom );
681     }
682     // remove hyp from myHypos
683     myHypos.remove( hyp );
684   }
685   // add accessor method if necessary
686   else
687   {
688     if ( NeedMeshAccess( theCommand ))
689       // apply theCommand to the mesh wrapped by smeshpy mesh
690       AddMeshAccess( theCommand );
691   }
692 }
693
694 //================================================================================
695 /*!
696  * \brief Return True if addition of accesor method is needed
697  */
698 //================================================================================
699
700 bool _pyMesh::NeedMeshAccess( const Handle(_pyCommand)& theCommand )
701 {
702   // names of SMESH_Mesh methods fully equal to methods of class Mesh, so
703   // no conversion is needed for them at all:
704   static TStringSet sameMethods;
705   if ( sameMethods.empty() ) {
706     const char * names[] =
707       { "ExportDAT","ExportUNV","ExportSTL", "RemoveGroup","RemoveGroupWithContents",
708         "GetGroups","UnionGroups","IntersectGroups","CutGroups","GetLog","GetId","ClearLog",
709         "GetStudyId","HasDuplicatedGroupNamesMED","GetMEDMesh","NbNodes","NbElements",
710         "NbEdges","NbEdgesOfOrder","NbFaces","NbFacesOfOrder","NbTriangles",
711         "NbTrianglesOfOrder","NbQuadrangles","NbQuadranglesOfOrder","NbPolygons","NbVolumes",
712         "NbVolumesOfOrder","NbTetras","NbTetrasOfOrder","NbHexas","NbHexasOfOrder",
713         "NbPyramids","NbPyramidsOfOrder","NbPrisms","NbPrismsOfOrder","NbPolyhedrons",
714         "NbSubMesh","GetElementsId","GetElementsByType","GetNodesId","GetElementType",
715         "GetSubMeshElementsId","GetSubMeshNodesId","GetSubMeshElementType","Dump","GetNodeXYZ",
716         "GetNodeInverseElements","GetShapeID","GetShapeIDForElem","GetElemNbNodes",
717         "GetElemNode","IsMediumNode","IsMediumNodeOfAnyElem","ElemNbEdges","ElemNbFaces",
718         "IsPoly","IsQuadratic","BaryCenter","GetHypothesisList",
719         "" }; // <- mark of end
720     sameMethods.Insert( names );
721   }
722
723   return !sameMethods.Contains( theCommand->GetMethod() );
724 }
725
726 //================================================================================
727 /*!
728  * \brief Convert creation and addition of all algos and hypos
729  */
730 //================================================================================
731
732 void _pyMesh::Flush()
733 {
734   list < Handle(_pyCommand) >::iterator cmd, cmd2;
735
736   // try to convert algo addition like this:
737   // mesh.AddHypothesis(geom, ALGO ) --> ALGO = mesh.Algo()
738   for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
739   {
740     Handle(_pyCommand) addCmd = *cmd;
741     _pyID algoID = addCmd->GetArg( 2 );
742     Handle(_pyHypothesis) algo = theGen->FindHyp( algoID );
743     if ( algo.IsNull() || !algo->IsAlgo() )
744       continue;
745     // try to convert
746     _pyID geom = addCmd->GetArg( 1 );
747     bool isLocalAlgo = ( geom != GetGeom() );
748     if ( algo->Addition2Creation( addCmd, this->GetID() )) // OK
749     {
750       // wrapped algo is created atfer mesh creation
751       GetCreationCmd()->AddDependantCmd( addCmd );
752
753       if ( isLocalAlgo ) {
754         // mesh.AddHypothesis(geom, ALGO ) --> mesh.AlgoMethod(geom)
755         addCmd->SetArg( addCmd->GetNbArgs() + 1,
756                         TCollection_AsciiString( "geom=" ) + geom );
757         // sm = mesh.GetSubMesh(geom, name) --> sm = ALGO.GetSubMesh()
758         for ( cmd2 = mySubmeshes.begin(); cmd2 != mySubmeshes.end(); ++cmd2 ) {
759           Handle(_pyCommand) subCmd = *cmd2;
760           if ( geom == subCmd->GetArg( 1 )) {
761             subCmd->SetObject( algo->GetID() );
762             subCmd->RemoveArgs();
763             addCmd->AddDependantCmd( subCmd );
764           }
765         }
766       }
767     }
768     else // KO - ALGO was already created
769     {
770       // mesh.AddHypothesis(geom, ALGO) --> mesh.AddHypothesis(ALGO, geom=0)
771       addCmd->RemoveArgs();
772       addCmd->SetArg( 1, algoID );
773       if ( isLocalAlgo )
774         addCmd->SetArg( 2, geom );
775     }
776   }
777
778   // try to convert hypo addition like this:
779   // mesh.AddHypothesis(geom, HYPO ) --> HYPO = algo.Hypo()
780   for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
781   {
782     Handle(_pyCommand) addCmd = *cmd;
783     _pyID hypID = addCmd->GetArg( 2 );
784     Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
785     if ( hyp.IsNull() || hyp->IsAlgo() )
786       continue;
787     bool converted = hyp->Addition2Creation( addCmd, this->GetID() );
788     if ( !converted ) {
789       // mesh.AddHypothesis(geom, HYP) --> mesh.AddHypothesis(HYP, geom=0)
790       _pyID geom = addCmd->GetArg( 1 );
791       addCmd->RemoveArgs();
792       addCmd->SetArg( 1, hypID );
793       if ( geom != GetGeom() )
794         addCmd->SetArg( 2, geom );
795     }
796   }
797
798   // sm = mesh.GetSubMesh(geom, name) --> sm = mesh.GetMesh().GetSubMesh(geom, name)
799 //   for ( cmd = mySubmeshes.begin(); cmd != mySubmeshes.end(); ++cmd ) {
800 //     Handle(_pyCommand) subCmd = *cmd;
801 //     if ( subCmd->GetNbArgs() > 0 )
802 //       AddMeshAccess( subCmd );
803 //   }
804   myAddHypCmds.clear();
805   mySubmeshes.clear();
806
807   // flush hypotheses
808   list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
809   for ( ; hyp != myHypos.end(); ++hyp )
810     (*hyp)->Flush();
811 }
812
813 //================================================================================
814 /*!
815  * \brief MeshEditor convert its commands to ones of mesh
816  */
817 //================================================================================
818
819 _pyMeshEditor::_pyMeshEditor(const Handle(_pyCommand)& theCreationCmd):
820   _pyObject( theCreationCmd )
821 {
822   myMesh = theCreationCmd->GetObject();
823   myCreationCmdStr = theCreationCmd->GetString();
824   theCreationCmd->Clear();
825 }
826
827 //================================================================================
828 /*!
829  * \brief convert its commands to ones of mesh
830  */
831 //================================================================================
832
833 void _pyMeshEditor::Process( const Handle(_pyCommand)& theCommand)
834 {
835   // names of SMESH_MeshEditor methods fully equal to methods of class Mesh, so
836   // commands calling this methods are converted to calls of methods of Mesh
837   static TStringSet sameMethods;
838   if ( sameMethods.empty() ) {
839     const char * names[] = {
840       "RemoveElements","RemoveNodes","AddNode","AddEdge","AddFace","AddPolygonalFace",
841       "AddVolume","AddPolyhedralVolume","AddPolyhedralVolumeByFaces","MoveNode",
842       "InverseDiag","DeleteDiag","Reorient","ReorientObject","SplitQuad","SplitQuadObject",
843       "BestSplit","Smooth","SmoothObject","SmoothParametric","SmoothParametricObject",
844       "ConvertToQuadratic","ConvertFromQuadratic","RenumberNodes","RenumberElements",
845       "RotationSweep","RotationSweepObject","ExtrusionSweep","AdvancedExtrusion",
846       "ExtrusionSweepObject","ExtrusionSweepObject1D","ExtrusionSweepObject2D","Mirror",
847       "MirrorObject","Translate","TranslateObject","Rotate","RotateObject",
848       "FindCoincidentNodes","FindCoincidentNodesOnPart","MergeNodes","FindEqualElements",
849       "MergeElements","MergeEqualElements","SewFreeBorders","SewConformFreeBorders",
850       "SewBorderToSide","SewSideElements","ChangeElemNodes","GetLastCreatedNodes",
851       "GetLastCreatedElems",
852       "" }; // <- mark of end
853     sameMethods.Insert( names );
854   }
855
856   if ( sameMethods.Contains( theCommand->GetMethod() )) {
857     theCommand->SetObject( myMesh );
858   }
859   else {
860     // editor creation command is needed only if any editor function is called
861     if ( !myCreationCmdStr.IsEmpty() ) {
862       GetCreationCmd()->GetString() = myCreationCmdStr;
863       myCreationCmdStr.Clear();
864     }
865   }
866 }
867
868 //================================================================================
869 /*!
870  * \brief _pyHypothesis constructor
871   * \param theCreationCmd - 
872  */
873 //================================================================================
874
875 _pyHypothesis::_pyHypothesis(const Handle(_pyCommand)& theCreationCmd):
876   _pyObject( theCreationCmd )
877 {
878   myIsAlgo = myIsWrapped = /*myIsConverted = myIsLocal = myDim = */false;
879 }
880
881 //================================================================================
882 /*!
883  * \brief Creates algorithm or hypothesis
884   * \param theCreationCmd - The engine command creating a hypothesis
885   * \retval Handle(_pyHypothesis) - Result _pyHypothesis
886  */
887 //================================================================================
888
889 Handle(_pyHypothesis) _pyHypothesis::NewHypothesis( const Handle(_pyCommand)& theCreationCmd)
890 {
891   // theCreationCmd: CreateHypothesis( "theHypType", "theLibName" )
892   ASSERT (( theCreationCmd->GetMethod() == "CreateHypothesis"));
893
894   Handle(_pyHypothesis) hyp, algo;
895
896   // "theHypType"
897   const TCollection_AsciiString & hypTypeQuoted = theCreationCmd->GetArg( 1 );
898   if ( hypTypeQuoted.IsEmpty() )
899     return hyp;
900   // theHypType
901   TCollection_AsciiString  hypType =
902     hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
903
904   algo = new _pyAlgorithm( theCreationCmd );
905   hyp  = new _pyHypothesis( theCreationCmd );
906
907   // 1D Regular_1D ----------
908   if ( hypType == "Regular_1D" ) {
909     // set mesh's method creating algo,
910     // i.e. convertion result will be "regular1d = Mesh.Segment()",
911     // and set hypType by which algo creating a hypothesis is searched for
912     algo->SetConvMethodAndType("Segment", hypType.ToCString());
913   }
914   else if ( hypType == "CompositeSegment_1D" ) {
915     algo->SetConvMethodAndType("Segment", "Regular_1D");
916     algo->myArgs.Append( "algo=smesh.COMPOSITE");
917   }
918   else if ( hypType == "LocalLength" ) {
919     // set algo's method creating hyp, and algo type
920     hyp->SetConvMethodAndType( "LocalLength", "Regular_1D");
921     // set method whose 1 arg will become the 1-st arg of hyp creation command
922     // i.e. convertion result will be "locallength = regular1d.LocalLength(<arg of SetLength()>)"
923     hyp->AddArgMethod( "SetLength" );
924   }
925   else if ( hypType == "NumberOfSegments" ) {
926     hyp = new _pyNumberOfSegmentsHyp( theCreationCmd );
927     hyp->SetConvMethodAndType( "NumberOfSegments", "Regular_1D");
928     // arg of SetNumberOfSegments() will become the 1-st arg of hyp creation command
929     hyp->AddArgMethod( "SetNumberOfSegments" );
930     // arg of SetScaleFactor() will become the 2-nd arg of hyp creation command
931     hyp->AddArgMethod( "SetScaleFactor" );
932   }
933   else if ( hypType == "Arithmetic1D" ) {
934     hyp = new _pyComplexParamHypo( theCreationCmd );
935     hyp->SetConvMethodAndType( "Arithmetic1D", "Regular_1D");
936   }
937   else if ( hypType == "StartEndLength" ) {
938     hyp = new _pyComplexParamHypo( theCreationCmd );
939     hyp->SetConvMethodAndType( "StartEndLength", "Regular_1D");
940   }
941   else if ( hypType == "Deflection1D" ) {
942     hyp->SetConvMethodAndType( "Deflection1D", "Regular_1D");
943     hyp->AddArgMethod( "SetDeflection" );
944   }
945   else if ( hypType == "Propagation" ) {
946     hyp->SetConvMethodAndType( "Propagation", "Regular_1D");
947   }
948   else if ( hypType == "QuadraticMesh" ) {
949     hyp->SetConvMethodAndType( "QuadraticMesh", "Regular_1D");
950   }
951   else if ( hypType == "AutomaticLength" ) {
952     hyp->SetConvMethodAndType( "AutomaticLength", "Regular_1D");
953     hyp->AddArgMethod( "SetFineness");
954   }
955   else if ( hypType == "SegmentLengthAroundVertex" ) {
956     hyp = new _pySegmentLengthAroundVertexHyp( theCreationCmd );
957     hyp->SetConvMethodAndType( "LengthNearVertex", "Regular_1D" );
958     hyp->AddArgMethod( "SetLength" );
959   }
960   // 1D Python_1D ----------
961   else if ( hypType == "Python_1D" ) {
962     algo->SetConvMethodAndType( "Segment", hypType.ToCString());
963     algo->myArgs.Append( "algo=smesh.PYTHON");
964   }
965   else if ( hypType == "PythonSplit1D" ) {
966     hyp->SetConvMethodAndType( "PythonSplit1D", "Python_1D");
967     hyp->AddArgMethod( "SetNumberOfSegments");
968     hyp->AddArgMethod( "SetPythonLog10RatioFunction");
969   }
970   // MEFISTO_2D ----------
971   else if ( hypType == "MEFISTO_2D" ) { // MEFISTO_2D
972     algo->SetConvMethodAndType( "Triangle", hypType.ToCString());
973   }
974   else if ( hypType == "MaxElementArea" ) {
975     hyp->SetConvMethodAndType( "MaxElementArea", "MEFISTO_2D");
976     hyp->SetConvMethodAndType( "MaxElementArea", "NETGEN_2D_ONLY");
977     hyp->AddArgMethod( "SetMaxElementArea");
978   }
979   else if ( hypType == "LengthFromEdges" ) {
980     hyp->SetConvMethodAndType( "LengthFromEdges", "MEFISTO_2D");
981     hyp->SetConvMethodAndType( "LengthFromEdges", "NETGEN_2D_ONLY");
982   }
983   // Quadrangle_2D ----------
984   else if ( hypType == "Quadrangle_2D" ) {
985     algo->SetConvMethodAndType( "Quadrangle" , hypType.ToCString());
986   }
987   else if ( hypType == "QuadranglePreference" ) {
988     hyp->SetConvMethodAndType( "QuadranglePreference", "Quadrangle_2D");
989     hyp->SetConvMethodAndType( "QuadranglePreference", "NETGEN_2D_ONLY");
990   }
991   // NETGEN ----------
992 //   else if ( hypType == "NETGEN_2D") { // 1D-2D
993 //     algo->SetConvMethodAndType( "Triangle" , hypType.ToCString());
994 //     algo->myArgs.Append( "algo=smesh.NETGEN" );
995 //   }
996   else if ( hypType == "NETGEN_2D_ONLY") { // 2D
997     algo->SetConvMethodAndType( "Triangle" , hypType.ToCString());
998     algo->myArgs.Append( "algo=smesh.NETGEN_2D" );
999   }
1000   else if ( hypType == "NETGEN_3D") { // 3D
1001     algo->SetConvMethodAndType( "Tetrahedron" , hypType.ToCString());
1002     algo->myArgs.Append( "algo=smesh.NETGEN" );
1003   }
1004   else if ( hypType == "MaxElementVolume") {
1005     hyp->SetConvMethodAndType( "MaxElementVolume", "NETGEN_3D");
1006     hyp->AddArgMethod( "SetMaxElementVolume" );
1007   }
1008   // GHS3D_3D ----------
1009   else if ( hypType == "GHS3D_3D" ) {
1010     algo->SetConvMethodAndType( "Tetrahedron", hypType.ToCString());
1011     algo->myArgs.Append( "algo=smesh.GHS3D" );
1012   }
1013   // Hexa_3D ---------
1014   else if ( hypType == "Hexa_3D" ) {
1015     algo->SetConvMethodAndType( "Hexahedron", hypType.ToCString());
1016   }
1017   // Repetitive Projection_1D ---------
1018   else if ( hypType == "Projection_1D" ) {
1019     algo->SetConvMethodAndType( "Projection1D", hypType.ToCString());
1020   }
1021   else if ( hypType == "ProjectionSource1D" ) {
1022     hyp->SetConvMethodAndType( "SourceEdge", "Projection_1D");
1023     hyp->AddArgMethod( "SetSourceEdge");
1024     hyp->AddArgMethod( "SetSourceMesh");
1025     // 2 args of SetVertexAssociation() will become the 3-th and 4-th args of hyp creation command
1026     hyp->AddArgMethod( "SetVertexAssociation", 2 );
1027   }
1028   // Projection_2D ---------
1029   else if ( hypType == "Projection_2D" ) {
1030     algo->SetConvMethodAndType( "Projection2D", hypType.ToCString());
1031   }
1032   else if ( hypType == "ProjectionSource2D" ) {
1033     hyp->SetConvMethodAndType( "SourceFace", "Projection_2D");
1034     hyp->AddArgMethod( "SetSourceFace");
1035     hyp->AddArgMethod( "SetSourceMesh");
1036     hyp->AddArgMethod( "SetVertexAssociation", 4 );
1037   }
1038   // Projection_3D ---------
1039   else if ( hypType == "Projection_3D" ) {
1040     algo->SetConvMethodAndType( "Projection3D", hypType.ToCString());
1041   }
1042   else if ( hypType == "ProjectionSource3D" ) {
1043     hyp->SetConvMethodAndType( "SourceShape3D", "Projection_3D");
1044     hyp->AddArgMethod( "SetSource3DShape");
1045     hyp->AddArgMethod( "SetSourceMesh");
1046     hyp->AddArgMethod( "SetVertexAssociation", 4 );
1047   }
1048   // Prism_3D ---------
1049   else if ( hypType == "Prism_3D" ) {
1050     algo->SetConvMethodAndType( "Prism", hypType.ToCString());
1051   }
1052   // RadialPrism_3D ---------
1053   else if ( hypType == "RadialPrism_3D" ) {
1054     algo->SetConvMethodAndType( "Prism", hypType.ToCString());
1055   }
1056   else if ( hypType == "NumberOfLayers" ) {
1057     hyp->SetConvMethodAndType( "NumberOfLayers", "RadialPrism_3D");
1058     hyp->AddArgMethod( "SetNumberOfLayers" );
1059   }
1060   else if ( hypType == "LayerDistribution" ) {
1061     hyp = new _pyLayerDistributionHypo( theCreationCmd );
1062     hyp->SetConvMethodAndType( "LayerDistribution", "RadialPrism_3D");
1063   }
1064
1065   if ( algo->IsValid() ) {
1066     return algo;
1067   }
1068   return hyp;
1069 }
1070
1071 //================================================================================
1072 /*!
1073  * \brief Convert the command adding a hypothesis to mesh into a smesh command
1074   * \param theCmd - The command like mesh.AddHypothesis( geom, hypo )
1075   * \param theAlgo - The algo that can create this hypo
1076   * \retval bool - false if the command cant be converted
1077  */
1078 //================================================================================
1079
1080 bool _pyHypothesis::Addition2Creation( const Handle(_pyCommand)& theCmd,
1081                                        const _pyID&              theMesh)
1082 {
1083   ASSERT(( theCmd->GetMethod() == "AddHypothesis" ));
1084
1085   if ( !IsWrappable( theMesh ))
1086     return false;
1087
1088   myGeom = theCmd->GetArg( 1 );
1089
1090   Handle(_pyHypothesis) algo;
1091   if ( !IsAlgo() ) {
1092     // find algo created on myGeom in theMesh
1093     algo = theGen->FindAlgo( myGeom, theMesh, this );
1094     if ( algo.IsNull() )
1095       return false;
1096     algo->GetCreationCmd()->AddDependantCmd( theCmd );
1097   }
1098   myIsWrapped = true;
1099
1100   // mesh.AddHypothesis(geom,hyp) --> hyp = <theMesh or algo>.myCreationMethod(args)
1101   theCmd->SetResultValue( GetID() );
1102   theCmd->SetObject( IsAlgo() ? theMesh : algo->GetID());
1103   theCmd->SetMethod( IsAlgo() ? GetAlgoCreationMethod() : GetCreationMethod( algo->GetAlgoType() ));
1104   // set args
1105   theCmd->RemoveArgs();
1106   for ( int i = 1; i <= myArgs.Length(); ++i ) {
1107     if ( !myArgs( i ).IsEmpty() )
1108       theCmd->SetArg( i, myArgs( i ));
1109     else
1110       theCmd->SetArg( i, "[]");
1111   }
1112   // set a new creation command
1113   GetCreationCmd()->Clear();
1114   SetCreationCmd( theCmd );
1115
1116   // clear commands setting arg values
1117   list < Handle(_pyCommand) >::iterator argCmd = myArgCommands.begin();
1118   for ( ; argCmd != myArgCommands.end(); ++argCmd )
1119     (*argCmd)->Clear();
1120
1121   // set unknown arg commands after hypo creation
1122   Handle(_pyCommand) afterCmd = myIsWrapped ? theCmd : GetCreationCmd();
1123   list<Handle(_pyCommand)>::iterator cmd = myUnknownCommands.begin();
1124   for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
1125     afterCmd->AddDependantCmd( *cmd );
1126   }
1127
1128   return myIsWrapped;
1129 }
1130
1131 //================================================================================
1132 /*!
1133  * \brief Remember hypothesis parameter values
1134  * \param theCommand - The called hypothesis method
1135  */
1136 //================================================================================
1137
1138 void _pyHypothesis::Process( const Handle(_pyCommand)& theCommand)
1139 {
1140   ASSERT( !myIsAlgo );
1141   // set args
1142   int nbArgs = 0;
1143   for ( int i = 1; i <= myArgMethods.Length(); ++i ) {
1144     if ( myArgMethods( i ) == theCommand->GetMethod() ) {
1145       while ( myArgs.Length() < nbArgs + myNbArgsByMethod( i ))
1146         myArgs.Append( "[]" );
1147       for ( int iArg = 1; iArg <= myNbArgsByMethod( i ); ++iArg )
1148         myArgs( nbArgs + iArg ) = theCommand->GetArg( iArg ); // arg value
1149       myArgCommands.push_back( theCommand );
1150       return;
1151     }
1152     nbArgs += myNbArgsByMethod( i );
1153   }
1154   myUnknownCommands.push_back( theCommand );
1155 }
1156
1157 //================================================================================
1158 /*!
1159  * \brief Finish conversion
1160  */
1161 //================================================================================
1162
1163 void _pyHypothesis::Flush()
1164 {
1165   if ( IsWrapped() ) {
1166   }
1167   else {
1168     list < Handle(_pyCommand) >::iterator cmd = myArgCommands.begin();
1169     for ( ; cmd != myArgCommands.end(); ++cmd ) {
1170       // Add access to a wrapped mesh
1171       theGen->AddMeshAccessorMethod( *cmd );
1172       // Add access to a wrapped algorithm
1173       theGen->AddAlgoAccessorMethod( *cmd );
1174     }
1175     cmd = myUnknownCommands.begin();
1176     for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
1177       // Add access to a wrapped mesh
1178       theGen->AddMeshAccessorMethod( *cmd );
1179       // Add access to a wrapped algorithm
1180       theGen->AddAlgoAccessorMethod( *cmd );
1181     }
1182   }
1183   // forget previous hypothesis modifications
1184   myArgCommands.clear();
1185   myUnknownCommands.clear();
1186 }
1187
1188 //================================================================================
1189 /*!
1190  * \brief clear creation, arg and unkown commands
1191  */
1192 //================================================================================
1193
1194 void _pyHypothesis::ClearAllCommands()
1195 {
1196   GetCreationCmd()->Clear();
1197   list<Handle(_pyCommand)>::iterator cmd = myArgCommands.begin();
1198   for ( ; cmd != myArgCommands.end(); ++cmd )
1199     ( *cmd )->Clear();
1200   cmd = myUnknownCommands.begin();
1201   for ( ; cmd != myUnknownCommands.end(); ++cmd )
1202     ( *cmd )->Clear();
1203 }
1204
1205 //================================================================================
1206 /*!
1207  * \brief Remember hypothesis parameter values
1208   * \param theCommand - The called hypothesis method
1209  */
1210 //================================================================================
1211
1212 void _pyComplexParamHypo::Process( const Handle(_pyCommand)& theCommand)
1213 {
1214   // ex: hyp.SetLength(start, 1)
1215   //     hyp.SetLength(end,   0)
1216   ASSERT(( theCommand->GetMethod() == "SetLength" ));
1217   ASSERT(( theCommand->GetArg( 2 ).IsIntegerValue() ));
1218   int i = 2 - theCommand->GetArg( 2 ).IntegerValue();
1219   while ( myArgs.Length() < i )
1220     myArgs.Append( "[]" );
1221   myArgs( i ) = theCommand->GetArg( 1 ); // arg value
1222   myArgCommands.push_back( theCommand );
1223 }
1224
1225 //================================================================================
1226 /*!
1227  * \brief Convert methods of 1D hypotheses to my own methods
1228   * \param theCommand - The called hypothesis method
1229  */
1230 //================================================================================
1231
1232 void _pyLayerDistributionHypo::Process( const Handle(_pyCommand)& theCommand)
1233 {
1234   if ( theCommand->GetMethod() != "SetLayerDistribution" )
1235     return;
1236
1237   _pyID newName; // name for 1D hyp = "HypType" + "_Distribution"
1238
1239   const _pyID& hyp1dID = theCommand->GetArg( 1 );
1240   Handle(_pyHypothesis) hyp1d = theGen->FindHyp( hyp1dID );
1241   if ( hyp1d.IsNull() ) // apparently hypId changed at study restoration
1242     hyp1d = my1dHyp;
1243   else if ( !my1dHyp.IsNull() && hyp1dID != my1dHyp->GetID() ) {
1244     // 1D hypo is already set, so distribution changes and the old
1245     // 1D hypo is thrown away
1246     my1dHyp->ClearAllCommands();
1247   }
1248   my1dHyp = hyp1d;
1249   if ( my1dHyp.IsNull() )
1250     return; // something wrong :(
1251
1252   // make a new name for 1D hyp = "HypType" + "_Distribution"
1253   if ( my1dHyp->GetCreationCmd()->GetMethod() == "CreateHypothesis" ) {
1254     // not yet converted creation cmd
1255     TCollection_AsciiString hypTypeQuoted = my1dHyp->GetCreationCmd()->GetArg(1);
1256     TCollection_AsciiString hypType = hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
1257     newName = hypType + "_Distribution";
1258     my1dHyp->GetCreationCmd()->SetResultValue( newName );
1259   }
1260   else {
1261     // already converted creation cmd
1262     newName = my1dHyp->GetCreationCmd()->GetResultValue();
1263   }
1264
1265   // as creation of 1D hyp was written later then it's edition,
1266   // we need to find all it's edition calls and process them
1267   list< Handle(_pyCommand) >& cmds = theGen->GetCommands();
1268   list< Handle(_pyCommand) >::iterator cmdIt = cmds.begin();
1269   for ( ; cmdIt != cmds.end(); ++cmdIt ) {
1270     const _pyID& objID = (*cmdIt)->GetObject();
1271     if ( objID == hyp1dID ) {
1272       my1dHyp->Process( *cmdIt );
1273       my1dHyp->GetCreationCmd()->AddDependantCmd( *cmdIt );
1274       ( *cmdIt )->SetObject( newName );
1275     }
1276   }
1277   if ( !myArgCommands.empty() )
1278     myArgCommands.front()->Clear();
1279   theCommand->SetArg( 1, newName );
1280   myArgCommands.push_back( theCommand );
1281   // copy hyp1d's creation method and args
1282 //   myCreationMethod = hyp1d->GetCreationMethod();
1283 //   myArgs           = hyp1d->GetArgs();
1284 //   // make them cleared at conversion
1285 //   myArgCommands = hyp1d->GetArgCommands();
1286
1287 //   // to be cleared at convertion only
1288 //   myArgCommands.push_back( theCommand );
1289 }
1290
1291 //================================================================================
1292 /*!
1293  * \brief 
1294   * \param theAdditionCmd - 
1295   * \param theMesh - 
1296   * \retval bool - 
1297  */
1298 //================================================================================
1299
1300 bool _pyLayerDistributionHypo::Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
1301                                                   const _pyID&              theMesh)
1302 {
1303   myIsWrapped = false;
1304
1305   if ( my1dHyp.IsNull() )
1306     return false;
1307
1308   // set "SetLayerDistribution()" after addition cmd
1309   theAdditionCmd->AddDependantCmd( myArgCommands.front() );
1310
1311   _pyID geom = theAdditionCmd->GetArg( 1 );
1312
1313   my1dHyp->SetMesh( theMesh );
1314   if ( !my1dHyp->Addition2Creation( theAdditionCmd, theMesh ))
1315     return false;
1316
1317   // clear "SetLayerDistribution()" cmd
1318   myArgCommands.front()->Clear();
1319
1320   // Convert my creation => me = RadialPrismAlgo.Get3DHypothesis()
1321
1322   // find RadialPrism algo created on <geom> for theMesh
1323   Handle(_pyHypothesis) algo = theGen->FindAlgo( geom, theMesh, this );
1324   if ( !algo.IsNull() ) {
1325     GetCreationCmd()->SetObject( algo->GetID() );
1326     GetCreationCmd()->SetMethod( "Get3DHypothesis" );
1327     GetCreationCmd()->RemoveArgs();
1328     theAdditionCmd->AddDependantCmd( GetCreationCmd() );
1329     myIsWrapped = true;
1330   }
1331   return myIsWrapped;
1332 }
1333
1334 //================================================================================
1335 /*!
1336  * \brief 
1337  */
1338 //================================================================================
1339
1340 void _pyLayerDistributionHypo::Flush()
1341 {
1342   //my1dHyp.Nullify();
1343   //_pyHypothesis::Flush();
1344 }
1345
1346 //================================================================================
1347 /*!
1348  * \brief additionally to Addition2Creation, clears SetDistrType() command
1349   * \param theCmd - AddHypothesis() command
1350   * \param theMesh - mesh to which a hypothesis is added
1351   * \retval bool - convertion result
1352  */
1353 //================================================================================
1354
1355 bool _pyNumberOfSegmentsHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
1356                                                 const _pyID&              theMesh)
1357 {
1358   if ( IsWrappable( theMesh ) && myArgs.Length() > 1 ) {
1359     // scale factor (2-nd arg) is provided: clear SetDistrType(1) command
1360     bool scaleDistrType = false;
1361     list<Handle(_pyCommand)>::reverse_iterator cmd = myUnknownCommands.rbegin();
1362     for ( ; cmd != myUnknownCommands.rend(); ++cmd ) {
1363       if ( (*cmd)->GetMethod() == "SetDistrType" ) {
1364         if ( (*cmd)->GetArg( 1 ) == "1" ) {
1365           scaleDistrType = true;
1366           (*cmd)->Clear();
1367         }
1368         else if ( !scaleDistrType ) {
1369           // distribution type changed: remove scale factor from args
1370           myArgs.Remove( 2, myArgs.Length() );
1371           break;
1372         }
1373       }
1374     }
1375   }
1376   return _pyHypothesis::Addition2Creation( theCmd, theMesh );
1377 }
1378
1379 //================================================================================
1380 /*!
1381  * \brief remove repeated commands defining distribution
1382  */
1383 //================================================================================
1384
1385 void _pyNumberOfSegmentsHyp::Flush()
1386 {
1387   // find number of the last SetDistrType() command
1388   list<Handle(_pyCommand)>::reverse_iterator cmd = myUnknownCommands.rbegin();
1389   int distrTypeNb = 0;
1390   for ( ; !distrTypeNb && cmd != myUnknownCommands.rend(); ++cmd )
1391     if ( (*cmd)->GetMethod() == "SetDistrType" )
1392       distrTypeNb = (*cmd)->GetOrderNb();
1393
1394   // clear commands before the last SetDistrType()
1395   list<Handle(_pyCommand)> * cmds[2] = { &myArgCommands, &myUnknownCommands };
1396   for ( int i = 0; i < 2; ++i ) {
1397     set<TCollection_AsciiString> uniqueMethods;
1398     list<Handle(_pyCommand)> & cmdList = *cmds[i];
1399     for ( cmd = cmdList.rbegin(); cmd != cmdList.rend(); ++cmd )
1400     {
1401       bool clear = ( (*cmd)->GetOrderNb() < distrTypeNb );
1402       const TCollection_AsciiString& method = (*cmd)->GetMethod();
1403       if ( !clear || method == "SetNumberOfSegments" ) {
1404         bool isNewInSet = uniqueMethods.insert( method ).second;
1405         clear = !isNewInSet;
1406       }
1407       if ( clear )
1408         (*cmd)->Clear();
1409     }
1410     cmdList.clear();
1411   }
1412 }
1413
1414 //================================================================================
1415 /*!
1416  * \brief Convert the command adding "SegmentLengthAroundVertex" to mesh
1417  * into regular1D.LengthNearVertex( length, vertex )
1418   * \param theCmd - The command like mesh.AddHypothesis( vertex, SegmentLengthAroundVertex )
1419   * \param theMesh - The mesh needing this hypo
1420   * \retval bool - false if the command cant be converted
1421  */
1422 //================================================================================
1423   
1424 bool _pySegmentLengthAroundVertexHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
1425                                                          const _pyID&              theMeshID)
1426 {
1427   if ( IsWrappable( theMeshID )) {
1428
1429     _pyID vertex = theCmd->GetArg( 1 );
1430
1431     // the problem here is that segment algo will not be found
1432     // by pyHypothesis::Addition2Creation() for <vertex>, so we try to find
1433     // geometry where segment algorithm is assigned
1434     Handle(_pyHypothesis) algo;
1435     _pyID geom = vertex;
1436     while ( algo.IsNull() && !geom.IsEmpty()) {
1437       // try to find geom as a father of <vertex>
1438       geom = FatherID( geom );
1439       algo = theGen->FindAlgo( geom, theMeshID, this );
1440     }
1441     if ( algo.IsNull() )
1442       return false; // also possible to find geom as brother of veretex...
1443     // set geom instead of vertex
1444     theCmd->SetArg( 1, geom );
1445
1446     // set vertex as a second arg
1447     if ( myArgs.Length() < 1) myArgs.Append( "1" ); // :(
1448     myArgs.Append( vertex );
1449
1450     // mesh.AddHypothesis(vertex, SegmentLengthAroundVertex) -->
1451     // theMeshID.LengthNearVertex( length, vertex )
1452     return _pyHypothesis::Addition2Creation( theCmd, theMeshID );
1453   }
1454   return false;
1455 }
1456
1457 //================================================================================
1458 /*!
1459  * \brief _pyAlgorithm constructor
1460  * \param theCreationCmd - The command like "algo = smeshgen.CreateHypothesis(type,lib)"
1461  */
1462 //================================================================================
1463
1464 _pyAlgorithm::_pyAlgorithm(const Handle(_pyCommand)& theCreationCmd)
1465   : _pyHypothesis( theCreationCmd )
1466 {
1467   myIsAlgo = true;
1468 }
1469
1470 //================================================================================
1471 /*!
1472  * \brief Convert the command adding an algorithm to mesh
1473   * \param theCmd - The command like mesh.AddHypothesis( geom, algo )
1474   * \param theMesh - The mesh needing this algo 
1475   * \retval bool - false if the command cant be converted
1476  */
1477 //================================================================================
1478   
1479 bool _pyAlgorithm::Addition2Creation( const Handle(_pyCommand)& theCmd,
1480                                       const _pyID&              theMeshID)
1481 {
1482   // mesh.AddHypothesis(geom,algo) --> theMeshID.myCreationMethod()
1483   if ( _pyHypothesis::Addition2Creation( theCmd, theMeshID )) {
1484     theGen->SetAccessorMethod( GetID(), "GetAlgorithm()" );
1485     return true;
1486   }
1487   return false;
1488 }
1489
1490 //================================================================================
1491 /*!
1492  * \brief Return starting position of a part of python command
1493   * \param thePartIndex - The index of command part
1494   * \retval int - Part position
1495  */
1496 //================================================================================
1497
1498 int _pyCommand::GetBegPos( int thePartIndex )
1499 {
1500   if ( IsEmpty() )
1501     return EMPTY;
1502   if ( myBegPos.Length() < thePartIndex )
1503     return UNKNOWN;
1504   return myBegPos( thePartIndex );
1505 }
1506
1507 //================================================================================
1508 /*!
1509  * \brief Store starting position of a part of python command
1510   * \param thePartIndex - The index of command part
1511   * \param thePosition - Part position
1512  */
1513 //================================================================================
1514
1515 void _pyCommand::SetBegPos( int thePartIndex, int thePosition )
1516 {
1517   while ( myBegPos.Length() < thePartIndex )
1518     myBegPos.Append( UNKNOWN );
1519   myBegPos( thePartIndex ) = thePosition;
1520 }
1521
1522 //================================================================================
1523 /*!
1524  * \brief Returns whitespace symbols at the line beginning
1525   * \retval TCollection_AsciiString - result
1526  */
1527 //================================================================================
1528
1529 TCollection_AsciiString _pyCommand::GetIndentation()
1530 {
1531   int end = 1;
1532   if ( GetBegPos( RESULT_IND ) == UNKNOWN )
1533     GetWord( myString, end, true );
1534   else
1535     end = GetBegPos( RESULT_IND );
1536   return myString.SubString( 1, end - 1 );
1537 }
1538
1539 //================================================================================
1540 /*!
1541  * \brief Return substring of python command looking like ResultValue = Obj.Meth()
1542   * \retval const TCollection_AsciiString & - ResultValue substring
1543  */
1544 //================================================================================
1545
1546 const TCollection_AsciiString & _pyCommand::GetResultValue()
1547 {
1548   if ( GetBegPos( RESULT_IND ) == UNKNOWN )
1549   {
1550     int begPos = myString.Location( "=", 1, Length() );
1551     if ( begPos )
1552       myRes = GetWord( myString, begPos, false );
1553     else
1554       begPos = EMPTY;
1555     SetBegPos( RESULT_IND, begPos );
1556   }
1557   return myRes;
1558 }
1559
1560 //================================================================================
1561 /*!
1562  * \brief Return substring of python command looking like ResVal = Object.Meth()
1563   * \retval const TCollection_AsciiString & - Object substring
1564  */
1565 //================================================================================
1566
1567 const TCollection_AsciiString & _pyCommand::GetObject()
1568 {
1569   if ( GetBegPos( OBJECT_IND ) == UNKNOWN )
1570   {
1571     // beginning
1572     int begPos = GetBegPos( RESULT_IND ) + myRes.Length();
1573     if ( begPos < 1 )
1574       begPos = myString.Location( "=", 1, Length() ) + 1;
1575     // store
1576     myObj = GetWord( myString, begPos, true );
1577     SetBegPos( OBJECT_IND, begPos );
1578   }
1579   //SCRUTE(myObj);
1580   return myObj;
1581 }
1582
1583 //================================================================================
1584 /*!
1585  * \brief Return substring of python command looking like ResVal = Obj.Method()
1586   * \retval const TCollection_AsciiString & - Method substring
1587  */
1588 //================================================================================
1589
1590 const TCollection_AsciiString & _pyCommand::GetMethod()
1591 {
1592   if ( GetBegPos( METHOD_IND ) == UNKNOWN )
1593   {
1594     // beginning
1595     int begPos = GetBegPos( OBJECT_IND ) + myObj.Length();
1596     bool forward = true;
1597     if ( begPos < 1 ) {
1598       begPos = myString.Location( "(", 1, Length() ) - 1;
1599       forward = false;
1600     }
1601     // store
1602     myMeth = GetWord( myString, begPos, forward );
1603     SetBegPos( METHOD_IND, begPos );
1604   }
1605   //SCRUTE(myMeth);
1606   return myMeth;
1607 }
1608
1609 //================================================================================
1610 /*!
1611  * \brief Return substring of python command looking like ResVal = Obj.Meth(Arg1,...)
1612   * \retval const TCollection_AsciiString & - Arg<index> substring
1613  */
1614 //================================================================================
1615
1616 const TCollection_AsciiString & _pyCommand::GetArg( int index )
1617 {
1618   if ( GetBegPos( ARG1_IND ) == UNKNOWN )
1619   {
1620     // find all args
1621     int begPos = GetBegPos( METHOD_IND ) + myMeth.Length();
1622     if ( begPos < 1 )
1623       begPos = myString.Location( "(", 1, Length() ) + 1;
1624
1625     int i = 0, prevLen = 0;
1626     while ( begPos != EMPTY ) {
1627       begPos += prevLen;
1628       // check if we are looking at the closing parenthesis
1629       while ( begPos <= Length() && isspace( myString.Value( begPos )))
1630         ++begPos;
1631       if ( begPos > Length() || myString.Value( begPos ) == ')' )
1632         break;
1633       myArgs.Append( GetWord( myString, begPos, true, true ));
1634       SetBegPos( ARG1_IND + i, begPos );
1635       prevLen = myArgs.Last().Length();
1636       if ( prevLen == 0 )
1637         myArgs.Remove( myArgs.Length() ); // no more args
1638       i++;
1639     }
1640   }
1641   if ( myArgs.Length() < index )
1642     return theEmptyString;
1643   return myArgs( index );
1644 }
1645
1646 //================================================================================
1647 /*!
1648  * \brief Check if char is a word part
1649   * \param c - The character to check
1650   * \retval bool - The check result
1651  */
1652 //================================================================================
1653
1654 static inline bool isWord(const char c, const bool dotIsWord)
1655 {
1656   return
1657     !isspace(c) && c != ',' && c != '=' && c != ')' && c != '(' && ( dotIsWord || c != '.');
1658 }
1659
1660 //================================================================================
1661 /*!
1662  * \brief Looks for a word in the string and returns word's beginning
1663   * \param theString - The input string
1664   * \param theStartPos - The position to start the search, returning word's beginning
1665   * \param theForward - The search direction
1666   * \retval TCollection_AsciiString - The found word
1667  */
1668 //================================================================================
1669
1670 TCollection_AsciiString _pyCommand::GetWord( const TCollection_AsciiString & theString,
1671                                             int &      theStartPos,
1672                                             const bool theForward,
1673                                             const bool dotIsWord )
1674 {
1675   int beg = theStartPos, end = theStartPos;
1676   theStartPos = EMPTY;
1677   if ( beg < 1 || beg > theString.Length() )
1678     return theEmptyString;
1679
1680   if ( theForward ) { // search forward
1681     // beg
1682     while ( beg <= theString.Length() && !isWord( theString.Value( beg ), dotIsWord))
1683       ++beg;
1684     if ( beg > theString.Length() )
1685       return theEmptyString; // no word found
1686     // end
1687     end = beg + 1;
1688     while ( end <= theString.Length() && isWord( theString.Value( end ), dotIsWord))
1689       ++end;
1690     --end;
1691   }
1692   else {  // search backward
1693     // end
1694     while ( end > 0 && !isWord( theString.Value( end ), dotIsWord))
1695       --end;
1696     if ( end == 0 )
1697       return theEmptyString; // no word found
1698     beg = end - 1;
1699     while ( beg > 0 && isWord( theString.Value( beg ), dotIsWord))
1700       --beg;
1701     ++beg;
1702   }
1703   theStartPos = beg;
1704   //cout << theString << " ---- " << beg << " - " << end << endl;
1705   return theString.SubString( beg, end );
1706 }
1707
1708 //================================================================================
1709 /*!
1710  * \brief Look for position where not space char is
1711   * \param theString - The string 
1712   * \param thePos - The position to search from and which returns result
1713   * \retval bool - false if there are only space after thePos in theString
1714  * 
1715  * 
1716  */
1717 //================================================================================
1718
1719 bool _pyCommand::SkipSpaces( const TCollection_AsciiString & theString, int & thePos )
1720 {
1721   if ( thePos < 1 || thePos > theString.Length() )
1722     return false;
1723
1724   while ( thePos <= theString.Length() && isspace( theString.Value( thePos )))
1725     ++thePos;
1726
1727   return thePos <= theString.Length();
1728 }
1729
1730 //================================================================================
1731 /*!
1732  * \brief Modify a part of the command
1733   * \param thePartIndex - The index of the part
1734   * \param thePart - The new part string
1735   * \param theOldPart - The old part
1736  */
1737 //================================================================================
1738
1739 void _pyCommand::SetPart(int thePartIndex, const TCollection_AsciiString& thePart,
1740                         TCollection_AsciiString& theOldPart)
1741 {
1742   int pos = GetBegPos( thePartIndex );
1743   if ( pos <= Length() && theOldPart != thePart)
1744   {
1745     TCollection_AsciiString seperator;
1746     if ( pos < 1 ) {
1747       pos = GetBegPos( thePartIndex + 1 );
1748       if ( pos < 1 ) return;
1749       switch ( thePartIndex ) {
1750       case RESULT_IND: seperator = " = "; break;
1751       case OBJECT_IND: seperator = "."; break;
1752       case METHOD_IND: seperator = "()"; break;
1753       default:;
1754       }
1755     }      
1756     myString.Remove( pos, theOldPart.Length() );
1757     if ( !seperator.IsEmpty() )
1758       myString.Insert( pos , seperator );
1759     myString.Insert( pos, thePart );
1760     // update starting positions of the following parts
1761     int posDelta = thePart.Length() + seperator.Length() - theOldPart.Length();
1762     for ( int i = thePartIndex + 1; i <= myBegPos.Length(); ++i ) {
1763       if ( myBegPos( i ) > 0 )
1764         myBegPos( i ) += posDelta;
1765     }
1766     theOldPart = thePart;
1767   }
1768 }
1769
1770 //================================================================================
1771 /*!
1772  * \brief Set agrument
1773   * \param index - The argument index, it counts from 1
1774   * \param theArg - The argument string
1775  */
1776 //================================================================================
1777
1778 void _pyCommand::SetArg( int index, const TCollection_AsciiString& theArg)
1779 {
1780   FindAllArgs();
1781   int argInd = ARG1_IND + index - 1;
1782   int pos = GetBegPos( argInd );
1783   if ( pos < 1 ) // no index-th arg exist, append inexistent args
1784   {
1785     // find a closing parenthesis
1786     if ( int lastArgInd = GetNbArgs() ) {
1787       pos = GetBegPos( ARG1_IND + lastArgInd  - 1 ) + GetArg( lastArgInd ).Length();
1788       while ( pos > 0 && pos <= Length() && myString.Value( pos ) != ')' )
1789         ++pos;
1790     }
1791     else {
1792       pos = Length();
1793       while ( pos > 0 && myString.Value( pos ) != ')' )
1794         --pos;
1795     }
1796     if ( pos < 1 || myString.Value( pos ) != ')' ) { // no parentheses at all
1797       myString += "()";
1798       pos = Length();
1799     }
1800     while ( myArgs.Length() < index ) {
1801       if ( myArgs.Length() )
1802         myString.Insert( pos++, "," );
1803       myArgs.Append("None");
1804       myString.Insert( pos, myArgs.Last() );
1805       SetBegPos( ARG1_IND + myArgs.Length() - 1, pos );
1806       pos += myArgs.Last().Length();
1807     }
1808   }
1809   SetPart( argInd, theArg, myArgs( index ));
1810 }
1811
1812 //================================================================================
1813 /*!
1814  * \brief Empty arg list
1815  */
1816 //================================================================================
1817
1818 void _pyCommand::RemoveArgs()
1819 {
1820   if ( int pos = myString.Location( '(', 1, Length() ))
1821     myString.Trunc( pos );
1822   myString += ")";
1823   myArgs.Clear();
1824   if ( myBegPos.Length() >= ARG1_IND )
1825     myBegPos.Remove( ARG1_IND, myBegPos.Length() );
1826 }
1827
1828 //================================================================================
1829 /*!
1830  * \brief Set dependent commands after this one
1831  */
1832 //================================================================================
1833
1834 bool _pyCommand::SetDependentCmdsAfter() const
1835 {
1836   bool orderChanged = false;
1837   list< Handle(_pyCommand)>::const_reverse_iterator cmd = myDependentCmds.rbegin();
1838   for ( ; cmd != myDependentCmds.rend(); ++cmd ) {
1839     if ( (*cmd)->GetOrderNb() < GetOrderNb() ) {
1840       orderChanged = true;
1841       theGen->SetCommandAfter( *cmd, this );
1842       (*cmd)->SetDependentCmdsAfter();
1843     }
1844   }
1845   return orderChanged;
1846 }
1847 //================================================================================
1848 /*!
1849  * \brief Insert accessor method after theObjectID
1850   * \param theObjectID - id of the accessed object
1851   * \param theAcsMethod - name of the method giving access to the object
1852   * \retval bool - false if theObjectID is not found in the command string
1853  */
1854 //================================================================================
1855
1856 bool _pyCommand::AddAccessorMethod( _pyID theObjectID, const char* theAcsMethod )
1857 {
1858   if ( !theAcsMethod )
1859     return false;
1860   // start object search from the object, i.e. ignore result
1861   GetObject();
1862   int beg = GetBegPos( OBJECT_IND );
1863   if ( beg < 1 || beg > Length() )
1864     return false;
1865   bool added = false;
1866   while (( beg = myString.Location( theObjectID, beg, Length() )))
1867   {
1868     // check that theObjectID is not just a part of a longer ID
1869     int afterEnd = beg + theObjectID.Length();
1870     Standard_Character c = myString.Value( afterEnd );
1871     if ( !isalnum( c ) && c != ':' ) {
1872       // check if accessor method already present
1873       if ( c != '.' ||
1874            myString.Location( (char*) theAcsMethod, afterEnd, Length() ) != afterEnd+1) {
1875         // insertion
1876         int oldLen = Length();
1877         myString.Insert( afterEnd, (char*) theAcsMethod );
1878         myString.Insert( afterEnd, "." );
1879         // update starting positions of the parts following the modified one
1880         int posDelta = Length() - oldLen;
1881         for ( int i = 1; i <= myBegPos.Length(); ++i ) {
1882           if ( myBegPos( i ) > afterEnd )
1883             myBegPos( i ) += posDelta;
1884         }
1885         added = true;
1886       }
1887     }
1888     beg = afterEnd; // is a part - next search
1889   }
1890   return added;
1891 }
1892
1893 //================================================================================
1894 /*!
1895  * \brief Return method name giving access to an interaface object wrapped by python class
1896   * \retval const char* - method name
1897  */
1898 //================================================================================
1899
1900 const char* _pyObject::AccessorMethod() const
1901 {
1902   return 0;
1903 }
1904 //================================================================================
1905 /*!
1906  * \brief Return ID of a father
1907  */
1908 //================================================================================
1909
1910 _pyID _pyObject::FatherID(const _pyID & childID)
1911 {
1912   int colPos = childID.SearchFromEnd(':');
1913   if ( colPos > 0 )
1914     return childID.SubString( 1, colPos-1 );
1915   return "";
1916 }