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