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