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