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