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