1 // Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
24 // File : SMESH_2smeshpy.cxx
25 // Created : Fri Nov 18 13:20:10 2005
26 // Author : Edward AGAPOV (eap)
28 #include "SMESH_2smeshpy.hxx"
30 #include "utilities.h"
31 #include "SMESH_PythonDump.hxx"
32 #include "SMESH_NoteBook.hxx"
33 #include "SMESH_Filter_i.hxx"
35 #include <Resource_DataMapOfAsciiStringAsciiString.hxx>
37 #include "SMESH_Gen_i.hxx"
38 /* SALOME headers that include CORBA headers that include windows.h
39 * that defines GetObject symbol as GetObjectA should stand before SALOME headers
40 * that declare methods named GetObject - to apply the same rules of GetObject renaming
41 * and thus to avoid mess with GetObject symbol on Windows */
43 IMPLEMENT_STANDARD_HANDLE (_pyObject ,Standard_Transient);
44 IMPLEMENT_STANDARD_HANDLE (_pyCommand ,Standard_Transient);
45 IMPLEMENT_STANDARD_HANDLE (_pyGen ,_pyObject);
46 IMPLEMENT_STANDARD_HANDLE (_pyMesh ,_pyObject);
47 IMPLEMENT_STANDARD_HANDLE (_pySubMesh ,_pyObject);
48 IMPLEMENT_STANDARD_HANDLE (_pyMeshEditor ,_pyObject);
49 IMPLEMENT_STANDARD_HANDLE (_pyHypothesis ,_pyObject);
50 IMPLEMENT_STANDARD_HANDLE (_pySelfEraser ,_pyObject);
51 IMPLEMENT_STANDARD_HANDLE (_pyGroup ,_pyObject);
52 IMPLEMENT_STANDARD_HANDLE (_pyFilter ,_pyObject);
53 IMPLEMENT_STANDARD_HANDLE (_pyAlgorithm ,_pyHypothesis);
54 IMPLEMENT_STANDARD_HANDLE (_pyComplexParamHypo,_pyHypothesis);
55 IMPLEMENT_STANDARD_HANDLE (_pyNumberOfSegmentsHyp,_pyHypothesis);
57 IMPLEMENT_STANDARD_RTTIEXT(_pyObject ,Standard_Transient);
58 IMPLEMENT_STANDARD_RTTIEXT(_pyCommand ,Standard_Transient);
59 IMPLEMENT_STANDARD_RTTIEXT(_pyGen ,_pyObject);
60 IMPLEMENT_STANDARD_RTTIEXT(_pyMesh ,_pyObject);
61 IMPLEMENT_STANDARD_RTTIEXT(_pySubMesh ,_pyObject);
62 IMPLEMENT_STANDARD_RTTIEXT(_pyMeshEditor ,_pyObject);
63 IMPLEMENT_STANDARD_RTTIEXT(_pyHypothesis ,_pyObject);
64 IMPLEMENT_STANDARD_RTTIEXT(_pySelfEraser ,_pyObject);
65 IMPLEMENT_STANDARD_RTTIEXT(_pyGroup ,_pyObject);
66 IMPLEMENT_STANDARD_RTTIEXT(_pyFilter ,_pyObject);
67 IMPLEMENT_STANDARD_RTTIEXT(_pyAlgorithm ,_pyHypothesis);
68 IMPLEMENT_STANDARD_RTTIEXT(_pyComplexParamHypo,_pyHypothesis);
69 IMPLEMENT_STANDARD_RTTIEXT(_pyNumberOfSegmentsHyp,_pyHypothesis);
70 IMPLEMENT_STANDARD_RTTIEXT(_pyLayerDistributionHypo,_pyHypothesis);
71 IMPLEMENT_STANDARD_RTTIEXT(_pySegmentLengthAroundVertexHyp,_pyHypothesis);
74 using SMESH::TPythonDump;
77 * \brief Container of commands into which the initial script is split.
78 * It also contains data coresponding to SMESH_Gen contents
80 static Handle(_pyGen) theGen;
82 static TCollection_AsciiString theEmptyString;
84 //#define DUMP_CONVERSION
86 #if !defined(_DEBUG_) && defined(DUMP_CONVERSION)
87 #undef DUMP_CONVERSION
93 //================================================================================
95 * \brief Set of TCollection_AsciiString initialized by C array of C strings
97 //================================================================================
99 struct TStringSet: public set<TCollection_AsciiString>
102 * \brief Filling. The last string must be ""
104 void Insert(const char* names[]) {
105 for ( int i = 0; names[i][0] ; ++i )
106 insert( (char*) names[i] );
109 * \brief Check if a string is in
111 bool Contains(const TCollection_AsciiString& name ) {
112 return find( name ) != end();
117 //================================================================================
119 * \brief Convert python script using commands of smesh.py
120 * \param theScript - Input script
121 * \retval TCollection_AsciiString - Convertion result
123 * Class SMESH_2smeshpy declared in SMESH_PythonDump.hxx
125 //================================================================================
127 TCollection_AsciiString
128 SMESH_2smeshpy::ConvertScript(const TCollection_AsciiString& theScript,
129 Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod,
130 Resource_DataMapOfAsciiStringAsciiString& theObjectNames)
132 theGen = new _pyGen( theEntry2AccessorMethod, theObjectNames );
134 // split theScript into separate commands
136 SMESH_NoteBook * aNoteBook = new SMESH_NoteBook();
138 int from = 1, end = theScript.Length(), to;
139 while ( from < end && ( to = theScript.Location( "\n", from, end )))
142 // cut out and store a command
143 aNoteBook->AddCommand( theScript.SubString( from, to - 1 ));
147 aNoteBook->ReplaceVariables();
149 TCollection_AsciiString aNoteScript = aNoteBook->GetResultScript();
153 // split theScript into separate commands
154 from = 1, end = aNoteScript.Length();
155 while ( from < end && ( to = aNoteScript.Location( "\n", from, end )))
158 // cut out and store a command
159 theGen->AddCommand( aNoteScript.SubString( from, to - 1 ));
165 #ifdef DUMP_CONVERSION
166 MESSAGE_BEGIN ( std::endl << " ######## RESULT ######## " << std::endl<< std::endl );
169 // reorder commands after conversion
170 list< Handle(_pyCommand) >::iterator cmd;
173 orderChanges = false;
174 for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
175 if ( (*cmd)->SetDependentCmdsAfter() )
177 } while ( orderChanges );
179 // concat commands back into a script
180 TCollection_AsciiString aScript;
181 for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
183 #ifdef DUMP_CONVERSION
184 MESSAGE_ADD ( "## COM " << (*cmd)->GetOrderNb() << ": "<< (*cmd)->GetString() << std::endl );
186 if ( !(*cmd)->IsEmpty() ) {
188 aScript += (*cmd)->GetString();
198 //================================================================================
200 * \brief _pyGen constructor
202 //================================================================================
204 _pyGen::_pyGen(Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod,
205 Resource_DataMapOfAsciiStringAsciiString& theObjectNames)
206 : _pyObject( new _pyCommand( TPythonDump::SMESHGenName(), 0 )),
208 myID2AccessorMethod( theEntry2AccessorMethod ),
209 myObjectNames( theObjectNames ),
212 // make that GetID() to return TPythonDump::SMESHGenName()
213 GetCreationCmd()->GetString() += "=";
216 //================================================================================
218 * \brief name of SMESH_Gen in smesh.py
220 //================================================================================
222 const char* _pyGen::AccessorMethod() const
224 return SMESH_2smeshpy::GenName();
227 //================================================================================
229 * \brief Convert a command using a specific converter
230 * \param theCommand - the command to convert
232 //================================================================================
234 Handle(_pyCommand) _pyGen::AddCommand( const TCollection_AsciiString& theCommand)
236 // store theCommand in the sequence
237 myCommands.push_back( new _pyCommand( theCommand, ++myNbCommands ));
239 Handle(_pyCommand) aCommand = myCommands.back();
240 #ifdef DUMP_CONVERSION
241 MESSAGE ( "## COM " << myNbCommands << ": "<< aCommand->GetString() );
244 _pyID objID = aCommand->GetObject();
246 if ( objID.IsEmpty() )
249 // Find an object to process theCommand
252 if ( objID == this->GetID() || objID == SMESH_2smeshpy::GenName()) {
253 this->Process( aCommand );
257 // SMESH_Mesh method?
258 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( objID );
259 if ( id_mesh != myMeshes.end() )
261 // check for mesh editor object
262 if ( aCommand->GetMethod() == "GetMeshEditor" ) { // MeshEditor creation
263 _pyID editorID = aCommand->GetResultValue();
264 Handle(_pyMeshEditor) editor = new _pyMeshEditor( aCommand );
265 myMeshEditors.insert( make_pair( editorID, editor ));
268 // check for SubMesh objects
269 else if ( aCommand->GetMethod() == "GetSubMesh" ) { // SubMesh creation
270 _pyID subMeshID = aCommand->GetResultValue();
271 Handle(_pySubMesh) subMesh = new _pySubMesh( aCommand );
272 myObjects.insert( make_pair( subMeshID, subMesh ));
275 id_mesh->second->Process( aCommand );
279 // SMESH_MeshEditor method?
280 map< _pyID, Handle(_pyMeshEditor) >::iterator id_editor = myMeshEditors.find( objID );
281 if ( id_editor != myMeshEditors.end() )
283 id_editor->second->Process( aCommand );
284 TCollection_AsciiString processedCommand = aCommand->GetString();
285 // some commands of SMESH_MeshEditor create meshes
286 if ( aCommand->GetMethod().Search("MakeMesh") != -1 ) {
287 Handle(_pyMesh) mesh = new _pyMesh( aCommand, aCommand->GetResultValue() );
288 aCommand->GetString() = processedCommand; // discard changes made by _pyMesh
289 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
291 if ( aCommand->GetMethod() == "MakeBoundaryMesh") {
292 _pyID meshID = aCommand->GetResultValue(0);
293 if ( !myMeshes.count( meshID ) )
295 Handle(_pyMesh) mesh = new _pyMesh( aCommand, meshID );
296 aCommand->GetString() = processedCommand; // discard changes made by _pyMesh
297 myMeshes.insert( make_pair( meshID, mesh ));
302 // SMESH_Hypothesis method?
303 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
304 for ( ; hyp != myHypos.end(); ++hyp )
305 if ( !(*hyp)->IsAlgo() && objID == (*hyp)->GetID() ) {
306 (*hyp)->Process( aCommand );
310 // aFilterManager.CreateFilter() ?
311 if ( aCommand->GetMethod() == "CreateFilter" )
313 // Set a more human readable name to a filter
314 // aFilter0x7fbf6c71cfb0 -> aFilter_nb
315 _pyID newID, filterID = aCommand->GetResultValue();
316 int pos = filterID.Search( "0x" );
318 newID = (filterID.SubString(1,pos-1) + "_") + _pyID( ++myNbFilters );
320 Handle(_pyObject) filter( new _pyFilter( aCommand, newID ));
324 // other object method?
325 map< _pyID, Handle(_pyObject) >::iterator id_obj = myObjects.find( objID );
326 if ( id_obj != myObjects.end() ) {
327 id_obj->second->Process( aCommand );
331 // Add access to a wrapped mesh
332 AddMeshAccessorMethod( aCommand );
334 // Add access to a wrapped algorithm
335 // AddAlgoAccessorMethod( aCommand ); // ??? what if algo won't be wrapped at all ???
337 // PAL12227. PythonDump was not updated at proper time; result is
338 // aCriteria.append(SMESH.Filter.Criterion(17,26,0,'L1',26,25,1e-07,SMESH.EDGE,-1))
339 // TypeError: __init__() takes exactly 11 arguments (10 given)
340 const char wrongCommand[] = "SMESH.Filter.Criterion(";
341 if ( int beg = theCommand.Location( wrongCommand, 1, theCommand.Length() ))
343 _pyCommand tmpCmd( theCommand.SubString( beg, theCommand.Length() ), -1);
344 // there must be 10 arguments, 5-th arg ThresholdID is missing,
345 const int wrongNbArgs = 9, missingArg = 5;
346 if ( tmpCmd.GetNbArgs() == wrongNbArgs )
348 for ( int i = wrongNbArgs; i > missingArg; --i )
349 tmpCmd.SetArg( i + 1, tmpCmd.GetArg( i ));
350 tmpCmd.SetArg( missingArg, "''");
351 aCommand->GetString().Trunc( beg - 1 );
352 aCommand->GetString() += tmpCmd.GetString();
355 // set GetCriterion(elementType,CritType,Compare,Treshold,UnaryOp,BinaryOp,Tolerance)
357 // instead of "SMESH.Filter.Criterion(
358 // Type,Compare,Threshold,ThresholdStr,ThresholdID,UnaryOp,BinaryOp,Tolerance,TypeOfElement,Precision)
359 // 1 2 3 4 5 6 7 8 9 10
360 // in order to avoid the problem of type mismatch of long and FunctorType
361 const TCollection_AsciiString
362 SMESH("SMESH."), dfltFunctor = "SMESH.FT_Undefined", dftlTol = "1e-07", dftlPreci = "-1";
363 TCollection_AsciiString
364 Type = aCommand->GetArg(1), // long
365 Compare = aCommand->GetArg(2), // long
366 Threshold = aCommand->GetArg(3), // double
367 ThresholdStr = aCommand->GetArg(4), // string
368 ThresholdID = aCommand->GetArg(5), // string
369 UnaryOp = aCommand->GetArg(6), // long
370 BinaryOp = aCommand->GetArg(7), // long
371 Tolerance = aCommand->GetArg(8), // double
372 TypeOfElement = aCommand->GetArg(9), // ElementType
373 Precision = aCommand->GetArg(10); // long
374 Type = SMESH + SMESH::FunctorTypeToString( SMESH::FunctorType( Type.IntegerValue() ));
375 Compare = SMESH + SMESH::FunctorTypeToString( SMESH::FunctorType( Compare.IntegerValue() ));
376 UnaryOp = SMESH + SMESH::FunctorTypeToString( SMESH::FunctorType( UnaryOp.IntegerValue() ));
377 BinaryOp = SMESH + SMESH::FunctorTypeToString( SMESH::FunctorType( BinaryOp.IntegerValue() ));
379 aCommand->RemoveArgs();
380 aCommand->SetObject( SMESH_2smeshpy::GenName() );
381 aCommand->SetMethod( "GetCriterion" );
383 aCommand->SetArg( 1, TypeOfElement );
384 aCommand->SetArg( 2, Type );
385 aCommand->SetArg( 3, Compare );
387 if ( ThresholdStr.Length() != 2 ) // not '' or ""
388 aCommand->SetArg( 4, ThresholdStr );
389 else if ( ThresholdID.Length() != 2 )
390 aCommand->SetArg( 4, ThresholdID );
392 aCommand->SetArg( 4, Threshold );
393 // find the last not default arg
395 if ( Tolerance == dftlTol ) {
397 if ( BinaryOp == dfltFunctor ) {
399 if ( UnaryOp == dfltFunctor )
403 if ( 5 < lastDefault ) aCommand->SetArg( 5, UnaryOp );
404 if ( 6 < lastDefault ) aCommand->SetArg( 6, BinaryOp );
405 if ( 7 < lastDefault ) aCommand->SetArg( 7, Tolerance );
406 if ( Precision != dftlPreci )
408 TCollection_AsciiString crit = aCommand->GetResultValue();
409 aCommand->GetString() += "; ";
410 aCommand->GetString() += crit + ".Precision = " + Precision;
416 //================================================================================
418 * \brief Convert the command or remember it for later conversion
419 * \param theCommand - The python command calling a method of SMESH_Gen
421 //================================================================================
423 void _pyGen::Process( const Handle(_pyCommand)& theCommand )
425 // there are methods to convert:
426 // CreateMesh( shape )
427 // Concatenate( [mesh1, ...], ... )
428 // CreateHypothesis( theHypType, theLibName )
429 // Compute( mesh, geom )
430 // Evaluate( mesh, geom )
432 TCollection_AsciiString method = theCommand->GetMethod();
434 if ( method == "CreateMesh" || method == "CreateEmptyMesh")
436 Handle(_pyMesh) mesh = new _pyMesh( theCommand );
437 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
440 if ( method == "CreateMeshesFromUNV" ||
441 method == "CreateMeshesFromSTL" ||
442 method == "CreateMeshesFromCGNS" ||
443 method == "CopyMesh" )
445 Handle(_pyMesh) mesh = new _pyMesh( theCommand, theCommand->GetResultValue() );
446 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
449 if( method == "CreateMeshesFromMED" || method == "CreateMeshesFromSAUV")
451 for(int ind = 0;ind<theCommand->GetNbResultValues();ind++)
453 Handle(_pyMesh) mesh = new _pyMesh( theCommand, theCommand->GetResultValue(ind));
454 myMeshes.insert( make_pair( theCommand->GetResultValue(ind), mesh ));
458 // CreateHypothesis()
459 if ( method == "CreateHypothesis" )
461 // issue 199929, remove standard library name (default parameter)
462 const TCollection_AsciiString & aLibName = theCommand->GetArg( 2 );
463 if ( aLibName.Search( "StdMeshersEngine" ) != -1 ) {
464 // keep first argument
465 TCollection_AsciiString arg = theCommand->GetArg( 1 );
466 theCommand->RemoveArgs();
467 theCommand->SetArg( 1, arg );
470 myHypos.push_back( _pyHypothesis::NewHypothesis( theCommand ));
474 // smeshgen.Compute( mesh, geom ) --> mesh.Compute()
475 if ( method == "Compute" )
477 const _pyID& meshID = theCommand->GetArg( 1 );
478 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( meshID );
479 if ( id_mesh != myMeshes.end() ) {
480 theCommand->SetObject( meshID );
481 theCommand->RemoveArgs();
482 id_mesh->second->Flush();
487 // smeshgen.Evaluate( mesh, geom ) --> mesh.Evaluate(geom)
488 if ( method == "Evaluate" )
490 const _pyID& meshID = theCommand->GetArg( 1 );
491 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( meshID );
492 if ( id_mesh != myMeshes.end() ) {
493 theCommand->SetObject( meshID );
494 _pyID geom = theCommand->GetArg( 2 );
495 theCommand->RemoveArgs();
496 theCommand->SetArg( 1, geom );
501 // objects erasing creation command if no more it's commands invoked:
502 // SMESH_Pattern, FilterManager
503 if ( method == "GetPattern" ||
504 method == "CreateFilterManager" ||
505 method == "CreateMeasurements" ) {
506 Handle(_pyObject) obj = new _pySelfEraser( theCommand );
507 if ( !myObjects.insert( make_pair( obj->GetID(), obj )).second )
508 theCommand->Clear(); // already created
511 // Concatenate( [mesh1, ...], ... )
512 if ( method == "Concatenate" || method == "ConcatenateWithGroups")
514 if ( method == "ConcatenateWithGroups" ) {
515 theCommand->SetMethod( "Concatenate" );
516 theCommand->SetArg( theCommand->GetNbArgs() + 1, "True" );
518 Handle(_pyMesh) mesh = new _pyMesh( theCommand, theCommand->GetResultValue() );
519 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
520 AddMeshAccessorMethod( theCommand );
523 // Replace name of SMESH_Gen
525 // names of SMESH_Gen methods fully equal to methods defined in smesh.py
526 static TStringSet smeshpyMethods;
527 if ( smeshpyMethods.empty() ) {
528 const char * names[] =
529 { "SetEmbeddedMode","IsEmbeddedMode","SetCurrentStudy","GetCurrentStudy",
530 "GetPattern","GetSubShapesId",
531 "" }; // <- mark of array end
532 smeshpyMethods.Insert( names );
534 if ( smeshpyMethods.Contains( theCommand->GetMethod() ))
535 // smeshgen.Method() --> smesh.Method()
536 theCommand->SetObject( SMESH_2smeshpy::SmeshpyName() );
538 // smeshgen.Method() --> smesh.smesh.Method()
539 theCommand->SetObject( SMESH_2smeshpy::GenName() );
542 //================================================================================
544 * \brief Convert the remembered commands
546 //================================================================================
550 // create empty command
551 myLastCommand = new _pyCommand();
553 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.begin();
554 for ( ; id_mesh != myMeshes.end(); ++id_mesh )
555 if ( ! id_mesh->second.IsNull() )
556 id_mesh->second->Flush();
558 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
559 for ( ; hyp != myHypos.end(); ++hyp )
560 if ( !hyp->IsNull() ) {
562 // smeshgen.CreateHypothesis() --> smesh.smesh.CreateHypothesis()
563 if ( !(*hyp)->IsWrapped() )
564 (*hyp)->GetCreationCmd()->SetObject( SMESH_2smeshpy::GenName() );
567 map< _pyID, Handle(_pyObject) >::iterator id_obj = myObjects.begin();
568 for ( ; id_obj != myObjects.end(); ++id_obj )
569 if ( ! id_obj->second.IsNull() )
570 id_obj->second->Flush();
572 myLastCommand->SetOrderNb( ++myNbCommands );
573 myCommands.push_back( myLastCommand );
576 //================================================================================
578 * \brief Add access method to mesh that is an argument
579 * \param theCmd - command to add access method
580 * \retval bool - true if added
582 //================================================================================
584 bool _pyGen::AddMeshAccessorMethod( Handle(_pyCommand) theCmd ) const
587 map< _pyID, Handle(_pyMesh) >::const_iterator id_mesh = myMeshes.begin();
588 for ( ; id_mesh != myMeshes.end(); ++id_mesh ) {
589 if ( theCmd->AddAccessorMethod( id_mesh->first, id_mesh->second->AccessorMethod() ))
595 //================================================================================
597 * \brief Add access method to algo that is an object or an argument
598 * \param theCmd - command to add access method
599 * \retval bool - true if added
601 //================================================================================
603 bool _pyGen::AddAlgoAccessorMethod( Handle(_pyCommand) theCmd ) const
606 list< Handle(_pyHypothesis) >::const_iterator hyp = myHypos.begin();
607 for ( ; hyp != myHypos.end(); ++hyp ) {
608 if ( (*hyp)->IsAlgo() && /*(*hyp)->IsWrapped() &&*/
609 theCmd->AddAccessorMethod( (*hyp)->GetID(), (*hyp)->AccessorMethod() ))
615 //================================================================================
617 * \brief Find hypothesis by ID (entry)
618 * \param theHypID - The hypothesis ID
619 * \retval Handle(_pyHypothesis) - The found hypothesis
621 //================================================================================
623 Handle(_pyHypothesis) _pyGen::FindHyp( const _pyID& theHypID )
625 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
626 for ( ; hyp != myHypos.end(); ++hyp )
627 if ( !hyp->IsNull() && theHypID == (*hyp)->GetID() )
629 return Handle(_pyHypothesis)();
632 //================================================================================
634 * \brief Find algorithm the created algorithm
635 * \param theGeom - The shape ID the algorithm was created on
636 * \param theMesh - The mesh ID that created the algorithm
637 * \param dim - The algo dimension
638 * \retval Handle(_pyHypothesis) - The found algo
640 //================================================================================
642 Handle(_pyHypothesis) _pyGen::FindAlgo( const _pyID& theGeom, const _pyID& theMesh,
643 const Handle(_pyHypothesis)& theHypothesis )
645 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
646 for ( ; hyp != myHypos.end(); ++hyp )
647 if ( !hyp->IsNull() &&
649 theHypothesis->CanBeCreatedBy( (*hyp)->GetAlgoType() ) &&
650 (*hyp)->GetGeom() == theGeom &&
651 (*hyp)->GetMesh() == theMesh )
656 //================================================================================
658 * \brief Find subMesh by ID (entry)
659 * \param theSubMeshID - The subMesh ID
660 * \retval Handle(_pySubMesh) - The found subMesh
662 //================================================================================
664 Handle(_pySubMesh) _pyGen::FindSubMesh( const _pyID& theSubMeshID )
666 map< _pyID, Handle(_pyObject) >::iterator id_subMesh = myObjects.find(theSubMeshID);
667 if ( id_subMesh != myObjects.end() )
668 return Handle(_pySubMesh)::DownCast( id_subMesh->second );
669 return Handle(_pySubMesh)();
673 //================================================================================
675 * \brief Change order of commands in the script
676 * \param theCmd1 - One command
677 * \param theCmd2 - Another command
679 //================================================================================
681 void _pyGen::ExchangeCommands( Handle(_pyCommand) theCmd1, Handle(_pyCommand) theCmd2 )
683 list< Handle(_pyCommand) >::iterator pos1, pos2;
684 pos1 = find( myCommands.begin(), myCommands.end(), theCmd1 );
685 pos2 = find( myCommands.begin(), myCommands.end(), theCmd2 );
686 myCommands.insert( pos1, theCmd2 );
687 myCommands.insert( pos2, theCmd1 );
688 myCommands.erase( pos1 );
689 myCommands.erase( pos2 );
691 int nb1 = theCmd1->GetOrderNb();
692 theCmd1->SetOrderNb( theCmd2->GetOrderNb() );
693 theCmd2->SetOrderNb( nb1 );
694 // cout << "BECOME " << theCmd1->GetOrderNb() << "\t" << theCmd1->GetString() << endl
695 // << "BECOME " << theCmd2->GetOrderNb() << "\t" << theCmd2->GetString() << endl << endl;
698 //================================================================================
700 * \brief Set one command after the other
701 * \param theCmd - Command to move
702 * \param theAfterCmd - Command ater which to insert the first one
704 //================================================================================
706 void _pyGen::SetCommandAfter( Handle(_pyCommand) theCmd, Handle(_pyCommand) theAfterCmd )
708 setNeighbourCommand( theCmd, theAfterCmd, true );
711 //================================================================================
713 * \brief Set one command before the other
714 * \param theCmd - Command to move
715 * \param theBeforeCmd - Command before which to insert the first one
717 //================================================================================
719 void _pyGen::SetCommandBefore( Handle(_pyCommand) theCmd, Handle(_pyCommand) theBeforeCmd )
721 setNeighbourCommand( theCmd, theBeforeCmd, false );
724 //================================================================================
726 * \brief Set one command before or after the other
727 * \param theCmd - Command to move
728 * \param theOtherCmd - Command ater or before which to insert the first one
730 //================================================================================
732 void _pyGen::setNeighbourCommand( Handle(_pyCommand)& theCmd,
733 Handle(_pyCommand)& theOtherCmd,
734 const bool theIsAfter )
736 list< Handle(_pyCommand) >::iterator pos;
737 pos = find( myCommands.begin(), myCommands.end(), theCmd );
738 myCommands.erase( pos );
739 pos = find( myCommands.begin(), myCommands.end(), theOtherCmd );
740 myCommands.insert( (theIsAfter ? ++pos : pos), theCmd );
743 for ( pos = myCommands.begin(); pos != myCommands.end(); ++pos)
744 (*pos)->SetOrderNb( i++ );
747 //================================================================================
749 * \brief Set command be last in list of commands
750 * \param theCmd - Command to be last
752 //================================================================================
754 Handle(_pyCommand)& _pyGen::GetLastCommand()
756 return myLastCommand;
759 //================================================================================
761 * \brief Set method to access to object wrapped with python class
762 * \param theID - The wrapped object entry
763 * \param theMethod - The accessor method
765 //================================================================================
767 void _pyGen::SetAccessorMethod(const _pyID& theID, const char* theMethod )
769 myID2AccessorMethod.Bind( theID, (char*) theMethod );
772 //================================================================================
774 * \brief Generated new ID for object and assign with existing name
775 * \param theID - ID of existing object
777 //================================================================================
779 _pyID _pyGen::GenerateNewID( const _pyID& theID )
784 aNewID = theID + _pyID( ":" ) + _pyID( index++ );
786 while ( myObjectNames.IsBound( aNewID ) );
788 myObjectNames.Bind( aNewID, myObjectNames.IsBound( theID )
789 ? (myObjectNames.Find( theID ) + _pyID( "_" ) + _pyID( index-1 ))
790 : _pyID( "A" ) + aNewID );
794 //================================================================================
796 * \brief Stores theObj in myObjects
798 //================================================================================
800 void _pyGen::AddObject( Handle(_pyObject)& theObj )
802 myObjects.insert( make_pair( theObj->GetID(), theObj ));
805 //================================================================================
807 * \brief Finds a _pyObject by ID
809 //================================================================================
811 Handle(_pyObject) _pyGen::FindObject( const _pyID& theObjID ) const
813 std::map< _pyID, Handle(_pyObject) >::const_iterator id_obj = myObjects.find( theObjID );
814 return ( id_obj == myObjects.end() ) ? Handle(_pyObject)() : id_obj->second;
817 //================================================================================
819 * \brief Find out type of geom group
820 * \param grpID - The geom group entry
821 * \retval int - The type
823 //================================================================================
825 // static bool sameGroupType( const _pyID& grpID,
826 // const TCollection_AsciiString& theType)
828 // // define group type as smesh.Mesh.Group() does
830 // SALOMEDS::Study_var study = SMESH_Gen_i::GetSMESHGen()->GetCurrentStudy();
831 // SALOMEDS::SObject_var aSObj = study->FindObjectID( grpID.ToCString() );
832 // if ( !aSObj->_is_nil() ) {
833 // GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow( aSObj->GetObject() );
834 // if ( !aGeomObj->_is_nil() ) {
835 // switch ( aGeomObj->GetShapeType() ) {
836 // case GEOM::VERTEX: type = SMESH::NODE; break;
837 // case GEOM::EDGE: type = SMESH::EDGE; break;
838 // case GEOM::FACE: type = SMESH::FACE; break;
840 // case GEOM::SHELL: type = SMESH::VOLUME; break;
841 // case GEOM::COMPOUND: {
842 // GEOM::GEOM_Gen_ptr aGeomGen = SMESH_Gen_i::GetSMESHGen()->GetGeomEngine();
843 // if ( !aGeomGen->_is_nil() ) {
844 // GEOM::GEOM_IGroupOperations_var aGrpOp =
845 // aGeomGen->GetIGroupOperations( study->StudyId() );
846 // if ( !aGrpOp->_is_nil() ) {
847 // switch ( aGrpOp->GetType( aGeomObj )) {
848 // case TopAbs_VERTEX: type = SMESH::NODE; break;
849 // case TopAbs_EDGE: type = SMESH::EDGE; break;
850 // case TopAbs_FACE: type = SMESH::FACE; break;
851 // case TopAbs_SOLID: type = SMESH::VOLUME; break;
862 // MESSAGE("Type of the group " << grpID << " not found");
865 // if ( theType.IsIntegerValue() )
866 // return type == theType.IntegerValue();
869 // case SMESH::NODE: return theType.Location( "NODE", 1, theType.Length() );
870 // case SMESH::EDGE: return theType.Location( "EDGE", 1, theType.Length() );
871 // case SMESH::FACE: return theType.Location( "FACE", 1, theType.Length() );
872 // case SMESH::VOLUME: return theType.Location( "VOLUME", 1, theType.Length() );
878 //================================================================================
881 * \param theCreationCmd -
883 //================================================================================
885 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd)
886 : _pyObject(theCreationCmd), myHasEditor(false)
888 // convert my creation command
889 Handle(_pyCommand) creationCmd = GetCreationCmd();
890 //TCollection_AsciiString str = creationCmd->GetMethod();
891 // if(str != "CreateMeshesFromUNV" &&
892 // str != "CreateMeshesFromMED" &&
893 // str != "CreateMeshesFromSTL")
894 creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
895 creationCmd->SetMethod( "Mesh" );
897 theGen->SetAccessorMethod( GetID(), "GetMesh()" );
900 //================================================================================
903 * \param theCreationCmd -
905 //================================================================================
906 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd, const TCollection_AsciiString& id):
907 _pyObject(theCreationCmd), myHasEditor(false)
909 // convert my creation command
910 Handle(_pyCommand) creationCmd = GetCreationCmd();
911 creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
912 theGen->SetAccessorMethod( id, "GetMesh()" );
915 //================================================================================
917 * \brief Convert an IDL API command of SMESH::SMESH_Mesh to a method call of python Mesh
918 * \param theCommand - Engine method called for this mesh
920 //================================================================================
922 void _pyMesh::Process( const Handle(_pyCommand)& theCommand )
924 // some methods of SMESH_Mesh interface needs special conversion
925 // to methods of Mesh python class
927 // 1. GetSubMesh(geom, name) + AddHypothesis(geom, algo)
928 // --> in Mesh_Algorithm.Create(mesh, geom, hypo, so)
929 // 2. AddHypothesis(geom, hyp)
930 // --> in Mesh_Algorithm.Hypothesis(hyp, args, so)
931 // 3. CreateGroupFromGEOM(type, name, grp)
932 // --> in Mesh.Group(grp, name="")
933 // 4. ExportToMED(f, auto_groups, version)
934 // --> in Mesh.ExportMED( f, auto_groups, version )
937 const TCollection_AsciiString method = theCommand->GetMethod();
938 // ----------------------------------------------------------------------
939 if ( method == "GetSubMesh" ) { // collect submeshes of the mesh
940 Handle(_pySubMesh) subMesh = theGen->FindSubMesh( theCommand->GetResultValue() );
941 if ( !subMesh.IsNull() ) {
942 subMesh->SetCreator( this );
943 mySubmeshes.push_back( subMesh );
946 // ----------------------------------------------------------------------
947 else if ( method == "AddHypothesis" ) { // mesh.AddHypothesis(geom, HYPO )
948 myAddHypCmds.push_back( theCommand );
950 const _pyID& hypID = theCommand->GetArg( 2 );
951 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
952 if ( !hyp.IsNull() ) {
953 myHypos.push_back( hyp );
954 if ( hyp->GetMesh().IsEmpty() )
955 hyp->SetMesh( this->GetID() );
958 // ----------------------------------------------------------------------
959 else if ( method == "CreateGroupFromGEOM" ) {// (type, name, grp)
960 _pyID grp = theCommand->GetArg( 3 );
961 // VSR 24/12/2010. PAL21106: always use GroupOnGeom() function on dump
962 // next if(){...} section is commented
963 //if ( sameGroupType( grp, theCommand->GetArg( 1 )) ) { // --> Group(grp)
964 // theCommand->SetMethod( "Group" );
965 // theCommand->RemoveArgs();
966 // theCommand->SetArg( 1, grp );
969 // ------------------------->>>>> GroupOnGeom( grp, name, typ )
970 _pyID type = theCommand->GetArg( 1 );
971 _pyID name = theCommand->GetArg( 2 );
972 theCommand->SetMethod( "GroupOnGeom" );
973 theCommand->RemoveArgs();
974 theCommand->SetArg( 1, grp );
975 theCommand->SetArg( 2, name );
976 theCommand->SetArg( 3, type );
979 // ----------------------------------------------------------------------
980 else if ( method == "CreateGroupFromFilter" ) // --> GroupOnFilter()
982 theCommand->SetMethod( "GroupOnFilter" );
984 // ----------------------------------------------------------------------
985 else if ( method == "GetIdsFromFilter" )
987 // GetIdsFromFilter( aFilter0x4743dc0) -> GetIdsFromFilter( aFilter_1)
988 _pyID filterID = theCommand->GetArg(1);
989 Handle(_pyObject) filter = theGen->FindObject( filterID );
990 if ( !filter.IsNull() && filter->IsKind(STANDARD_TYPE(_pyFilter)))
991 filter->Process( theCommand );
993 // ----------------------------------------------------------------------
994 else if ( method == "CreateGroup" ) // CreateGroup() --> CreateEmptyGroup()
996 theCommand->SetMethod( "CreateEmptyGroup" );
997 Handle(_pyGroup) group = new _pyGroup( theCommand );
998 theGen->AddObject( group );
1000 // ----------------------------------------------------------------------
1001 else if ( method == "ExportToMED" || // ExportToMED() --> ExportMED()
1002 method == "ExportToMEDX" ) { // ExportToMEDX() --> ExportMED()
1003 theCommand->SetMethod( "ExportMED" );
1005 // ----------------------------------------------------------------------
1006 else if ( method == "ExportCGNS" )
1007 { // ExportCGNS(part, ...) -> ExportCGNS(..., part)
1008 _pyID partID = theCommand->GetArg( 1 );
1009 int nbArgs = theCommand->GetNbArgs();
1010 for ( int i = 2; i <= nbArgs; ++i )
1011 theCommand->SetArg( i-1, theCommand->GetArg( i ));
1012 theCommand->SetArg( nbArgs, partID );
1014 // ----------------------------------------------------------------------
1015 else if ( method.Location( "ExportPartTo", 1, method.Length() ) == 1 )
1016 { // ExportPartTo*(part, ...) -> Export*(..., part)
1018 // remove "PartTo" from the method
1019 TCollection_AsciiString newMethod = method;
1020 newMethod.Remove( 7, 6 );
1021 theCommand->SetMethod( newMethod );
1022 // make the 1st arg be the last one
1023 _pyID partID = theCommand->GetArg( 1 );
1024 int nbArgs = theCommand->GetNbArgs();
1025 for ( int i = 2; i <= nbArgs; ++i )
1026 theCommand->SetArg( i-1, theCommand->GetArg( i ));
1027 theCommand->SetArg( nbArgs, partID );
1029 // ----------------------------------------------------------------------
1030 else if ( method == "RemoveHypothesis" ) // (geom, hyp)
1032 _pyID hypID = theCommand->GetArg( 2 );
1034 // check if this mesh still has corresponding addition command
1035 bool hasAddCmd = false;
1036 list< Handle(_pyCommand) >::iterator cmd = myAddHypCmds.begin();
1037 while ( cmd != myAddHypCmds.end() )
1039 // AddHypothesis(geom, hyp)
1040 if ( hypID == (*cmd)->GetArg( 2 )) { // erase both (add and remove) commands
1041 theCommand->Clear();
1043 cmd = myAddHypCmds.erase( cmd );
1050 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
1051 if ( ! hasAddCmd && hypID.Length() != 0 ) { // hypo addition already wrapped
1052 // RemoveHypothesis(geom, hyp) --> RemoveHypothesis( hyp, geom=0 )
1053 _pyID geom = theCommand->GetArg( 1 );
1054 theCommand->RemoveArgs();
1055 theCommand->SetArg( 1, hypID );
1056 if ( geom != GetGeom() )
1057 theCommand->SetArg( 2, geom );
1059 // remove hyp from myHypos
1060 myHypos.remove( hyp );
1062 // check for SubMesh order commands
1063 else if ( theCommand->GetMethod() == "GetMeshOrder" ||
1064 theCommand->GetMethod() == "SetMeshOrder" )
1066 // make commands GetSubMesh() returning sub-meshes be before using sub-meshes
1067 // by GetMeshOrder() and SetMeshOrder(), since by defalut GetSubMesh()
1068 // commands are moved at the end of the script
1069 const bool isArg = theCommand->GetMethod() == "SetMeshOrder";
1070 const TCollection_AsciiString& cmdStr = theCommand->GetString();
1071 int begPos = (/*isArg ? cmdStr.Search( "(" ) :*/ cmdStr.Search( "[" )) + 1;
1072 int endPos = (isArg ? cmdStr.Search( ")" ) : cmdStr.Search( "=" )) - 1;
1073 if ( begPos != -1 && begPos < endPos && endPos <= cmdStr.Length() ) {
1074 TCollection_AsciiString aSubStr = cmdStr.SubString( begPos, endPos );
1075 Standard_Integer index = 1;
1076 TCollection_AsciiString anIDStr = aSubStr.Token("\t ,[]", index++);
1077 while ( !anIDStr.IsEmpty() ) {
1078 Handle(_pySubMesh) subMesh = theGen->FindSubMesh( anIDStr );
1079 if ( !subMesh.IsNull() )
1080 subMesh->Process( theCommand ); // it moves GetSubMesh() before theCommand
1081 anIDStr = aSubStr.Token("\t ,[]", index++);
1085 // add accessor method if necessary
1088 if ( NeedMeshAccess( theCommand ))
1089 // apply theCommand to the mesh wrapped by smeshpy mesh
1090 AddMeshAccess( theCommand );
1094 //================================================================================
1096 * \brief Return True if addition of accesor method is needed
1098 //================================================================================
1100 bool _pyMesh::NeedMeshAccess( const Handle(_pyCommand)& theCommand )
1102 // names of SMESH_Mesh methods fully equal to methods of python class Mesh,
1103 // so no conversion is needed for them at all:
1104 static TStringSet sameMethods;
1105 if ( sameMethods.empty() ) {
1106 const char * names[] =
1107 { "ExportDAT","ExportUNV","ExportSTL","ExportSAUV", "RemoveGroup","RemoveGroupWithContents",
1108 "GetGroups","UnionGroups","IntersectGroups","CutGroups","GetLog","GetId","ClearLog",
1109 "GetStudyId","HasDuplicatedGroupNamesMED","GetMEDMesh","NbNodes","NbElements",
1110 "NbEdges","NbEdgesOfOrder","NbFaces","NbFacesOfOrder","NbTriangles",
1111 "NbTrianglesOfOrder","NbQuadrangles","NbQuadranglesOfOrder","NbPolygons","NbVolumes",
1112 "NbVolumesOfOrder","NbTetras","NbTetrasOfOrder","NbHexas","NbHexasOfOrder",
1113 "NbPyramids","NbPyramidsOfOrder","NbPrisms","NbPrismsOfOrder","NbPolyhedrons",
1114 "NbSubMesh","GetElementsId","GetElementsByType","GetNodesId","GetElementType",
1115 "GetSubMeshElementsId","GetSubMeshNodesId","GetSubMeshElementType","Dump","GetNodeXYZ",
1116 "GetNodeInverseElements","GetShapeID","GetShapeIDForElem","GetElemNbNodes",
1117 "GetElemNode","IsMediumNode","IsMediumNodeOfAnyElem","ElemNbEdges","ElemNbFaces",
1118 "IsPoly","IsQuadratic","BaryCenter","GetHypothesisList", "SetAutoColor", "GetAutoColor",
1119 "Clear", "ConvertToStandalone", "GetMeshOrder", "SetMeshOrder"
1120 ,"" }; // <- mark of end
1121 sameMethods.Insert( names );
1124 return !sameMethods.Contains( theCommand->GetMethod() );
1127 //================================================================================
1129 * \brief Convert creation and addition of all algos and hypos
1131 //================================================================================
1133 void _pyMesh::Flush()
1135 list < Handle(_pyCommand) >::iterator cmd;
1137 // try to convert algo addition like this:
1138 // mesh.AddHypothesis(geom, ALGO ) --> ALGO = mesh.Algo()
1139 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
1141 Handle(_pyCommand) addCmd = *cmd;
1143 _pyID algoID = addCmd->GetArg( 2 );
1144 Handle(_pyHypothesis) algo = theGen->FindHyp( algoID );
1145 if ( algo.IsNull() || !algo->IsAlgo() )
1148 // check and create new algorithm instance if it is already wrapped
1149 if ( algo->IsWrapped() ) {
1150 _pyID localAlgoID = theGen->GenerateNewID( algoID );
1151 TCollection_AsciiString aNewCmdStr = localAlgoID +
1152 TCollection_AsciiString( " = " ) + theGen->GetID() +
1153 TCollection_AsciiString( ".CreateHypothesis( \"" ) + algo->GetAlgoType() +
1154 TCollection_AsciiString( "\" )" );
1156 Handle(_pyCommand) newCmd = theGen->AddCommand( aNewCmdStr );
1157 Handle(_pyAlgorithm) newAlgo = Handle(_pyAlgorithm)::DownCast(theGen->FindHyp( localAlgoID ));
1158 if ( !newAlgo.IsNull() ) {
1159 newAlgo->Assign( algo, this->GetID() );
1160 newAlgo->SetCreationCmd( newCmd );
1162 // set algorithm creation
1163 theGen->SetCommandBefore( newCmd, addCmd );
1168 _pyID geom = addCmd->GetArg( 1 );
1169 bool isLocalAlgo = ( geom != GetGeom() );
1172 if ( algo->Addition2Creation( addCmd, this->GetID() )) // OK
1174 // wrapped algo is created atfer mesh creation
1175 GetCreationCmd()->AddDependantCmd( addCmd );
1177 if ( isLocalAlgo ) {
1178 // mesh.AddHypothesis(geom, ALGO ) --> mesh.AlgoMethod(geom)
1179 addCmd->SetArg( addCmd->GetNbArgs() + 1,
1180 TCollection_AsciiString( "geom=" ) + geom );
1181 // sm = mesh.GetSubMesh(geom, name) --> sm = ALGO.GetSubMesh()
1182 list < Handle(_pySubMesh) >::iterator smIt;
1183 for ( smIt = mySubmeshes.begin(); smIt != mySubmeshes.end(); ++smIt ) {
1184 Handle(_pySubMesh) subMesh = *smIt;
1185 Handle(_pyCommand) subCmd = subMesh->GetCreationCmd();
1186 if ( geom == subCmd->GetArg( 1 )) {
1187 subCmd->SetObject( algo->GetID() );
1188 subCmd->RemoveArgs();
1189 subMesh->SetCreator( algo );
1194 else // KO - ALGO was already created
1196 // mesh.AddHypothesis(geom, ALGO) --> mesh.AddHypothesis(ALGO, geom=0)
1197 addCmd->RemoveArgs();
1198 addCmd->SetArg( 1, algoID );
1200 addCmd->SetArg( 2, geom );
1204 // try to convert hypo addition like this:
1205 // mesh.AddHypothesis(geom, HYPO ) --> HYPO = algo.Hypo()
1206 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
1208 Handle(_pyCommand) addCmd = *cmd;
1209 _pyID hypID = addCmd->GetArg( 2 );
1210 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
1211 if ( hyp.IsNull() || hyp->IsAlgo() )
1213 bool converted = hyp->Addition2Creation( addCmd, this->GetID() );
1215 // mesh.AddHypothesis(geom, HYP) --> mesh.AddHypothesis(HYP, geom=0)
1216 _pyID geom = addCmd->GetArg( 1 );
1217 addCmd->RemoveArgs();
1218 addCmd->SetArg( 1, hypID );
1219 if ( geom != GetGeom() )
1220 addCmd->SetArg( 2, geom );
1224 // sm = mesh.GetSubMesh(geom, name) --> sm = mesh.GetMesh().GetSubMesh(geom, name)
1225 // for ( cmd = mySubmeshes.begin(); cmd != mySubmeshes.end(); ++cmd ) {
1226 // Handle(_pyCommand) subCmd = *cmd;
1227 // if ( subCmd->GetNbArgs() > 0 )
1228 // AddMeshAccess( subCmd );
1230 myAddHypCmds.clear();
1231 mySubmeshes.clear();
1234 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
1235 for ( ; hyp != myHypos.end(); ++hyp )
1239 //================================================================================
1241 * \brief MeshEditor convert its commands to ones of mesh
1243 //================================================================================
1245 _pyMeshEditor::_pyMeshEditor(const Handle(_pyCommand)& theCreationCmd):
1246 _pyObject( theCreationCmd )
1248 myMesh = theCreationCmd->GetObject();
1249 myCreationCmdStr = theCreationCmd->GetString();
1250 theCreationCmd->Clear();
1253 //================================================================================
1255 * \brief convert its commands to ones of mesh
1257 //================================================================================
1259 void _pyMeshEditor::Process( const Handle(_pyCommand)& theCommand)
1261 // names of SMESH_MeshEditor methods fully equal to methods of python class Mesh, so
1262 // commands calling this methods are converted to calls of methods of Mesh
1263 static TStringSet sameMethods;
1264 if ( sameMethods.empty() ) {
1265 const char * names[] = {
1266 "RemoveElements","RemoveNodes","RemoveOrphanNodes","AddNode","Add0DElement","AddEdge","AddFace","AddPolygonalFace",
1267 "AddVolume","AddPolyhedralVolume","AddPolyhedralVolumeByFaces","MoveNode", "MoveClosestNodeToPoint",
1268 "InverseDiag","DeleteDiag","Reorient","ReorientObject","TriToQuad","SplitQuad","SplitQuadObject",
1269 "BestSplit","Smooth","SmoothObject","SmoothParametric","SmoothParametricObject",
1270 "ConvertToQuadratic","ConvertFromQuadratic","RenumberNodes","RenumberElements",
1271 "RotationSweep","RotationSweepObject","RotationSweepObject1D","RotationSweepObject2D",
1272 "ExtrusionSweep","AdvancedExtrusion","ExtrusionSweepObject","ExtrusionSweepObject1D","ExtrusionSweepObject2D",
1273 "ExtrusionAlongPath","ExtrusionAlongPathObject","ExtrusionAlongPathX",
1274 "ExtrusionAlongPathObject1D","ExtrusionAlongPathObject2D",
1275 "Mirror","MirrorObject","Translate","TranslateObject","Rotate","RotateObject",
1276 "FindCoincidentNodes",/*"FindCoincidentNodesOnPart",*/"MergeNodes","FindEqualElements",
1277 "MergeElements","MergeEqualElements","SewFreeBorders","SewConformFreeBorders",
1278 "SewBorderToSide","SewSideElements","ChangeElemNodes","GetLastCreatedNodes",
1279 "GetLastCreatedElems",
1280 "MirrorMakeMesh","MirrorObjectMakeMesh","TranslateMakeMesh",
1281 "TranslateObjectMakeMesh","RotateMakeMesh","RotateObjectMakeMesh","MakeBoundaryMesh"
1282 ,"" }; // <- mark of the end
1283 sameMethods.Insert( names );
1286 // names of SMESH_MeshEditor methods which differ from methods of class Mesh
1287 // only by last two arguments
1288 static TStringSet diffLastTwoArgsMethods;
1289 if (diffLastTwoArgsMethods.empty() ) {
1290 const char * names[] = {
1291 "MirrorMakeGroups","MirrorObjectMakeGroups",
1292 "TranslateMakeGroups","TranslateObjectMakeGroups",
1293 "RotateMakeGroups","RotateObjectMakeGroups",
1294 ""};// <- mark of the end
1295 diffLastTwoArgsMethods.Insert( names );
1298 const TCollection_AsciiString & method = theCommand->GetMethod();
1299 bool isPyMeshMethod = sameMethods.Contains( method );
1300 if ( !isPyMeshMethod )
1302 //Replace SMESH_MeshEditor "MakeGroups" functions by the Mesh
1303 //functions with the flag "theMakeGroups = True" like:
1304 //SMESH_MeshEditor.CmdMakeGroups => Mesh.Cmd(...,True)
1305 int pos = method.Search("MakeGroups");
1308 isPyMeshMethod = true;
1310 // 1. Remove "MakeGroups" from the Command
1311 TCollection_AsciiString aMethod = theCommand->GetMethod();
1312 int nbArgsToAdd = diffLastTwoArgsMethods.Contains(aMethod) ? 2 : 1;
1313 aMethod.Trunc(pos-1);
1314 theCommand->SetMethod(aMethod);
1316 // 2. And add last "True" argument(s)
1317 while(nbArgsToAdd--)
1318 theCommand->SetArg(theCommand->GetNbArgs()+1,"True");
1322 // set "ExtrusionAlongPathX()" instead of "ExtrusionAlongPathObjX()"
1323 if ( !isPyMeshMethod && method == "ExtrusionAlongPathObjX")
1325 isPyMeshMethod=true;
1326 theCommand->SetMethod("ExtrusionAlongPathX");
1329 // set "FindCoincidentNodesOnPart()" instead of "FindCoincidentNodesOnPartBut()"
1330 if ( !isPyMeshMethod && method == "FindCoincidentNodesOnPartBut")
1332 isPyMeshMethod=true;
1333 theCommand->SetMethod("FindCoincidentNodesOnPart");
1335 // DoubleNodeElemGroupNew() -> DoubleNodeElemGroup()
1336 // DoubleNodeGroupNew() -> DoubleNodeGroup()
1337 // DoubleNodeGroupsNew() -> DoubleNodeGroups()
1338 // DoubleNodeElemGroupsNew() -> DoubleNodeElemGroups()
1339 if ( !isPyMeshMethod && ( method == "DoubleNodeElemGroupNew" ||
1340 method == "DoubleNodeElemGroupsNew" ||
1341 method == "DoubleNodeGroupNew" ||
1342 method == "DoubleNodeGroupsNew"))
1344 isPyMeshMethod=true;
1345 theCommand->SetMethod( method.SubString( 1, method.Length()-3));
1346 theCommand->SetArg(theCommand->GetNbArgs()+1,"True");
1348 // ConvertToQuadraticObject(bool,obj) -> ConvertToQuadratic(bool,obj)
1349 // ConvertFromQuadraticObject(obj) -> ConvertFromQuadratic(obj)
1350 if ( !isPyMeshMethod && ( method == "ConvertToQuadraticObject" ||
1351 method == "ConvertFromQuadraticObject" ))
1353 isPyMeshMethod=true;
1354 theCommand->SetMethod( method.SubString( 1, method.Length()-6));
1355 // prevent moving creation of the converted sub-mesh to the end of the script
1356 bool isFromQua = ( method.Value( 8 ) == 'F' );
1357 Handle(_pySubMesh) sm = theGen->FindSubMesh( theCommand->GetArg( isFromQua ? 1 : 2 ));
1359 sm->Process( theCommand );
1361 // FindAmongElementsByPoint(meshPart, x, y, z, elementType) ->
1362 // FindElementsByPoint(x, y, z, elementType, meshPart)
1363 if ( !isPyMeshMethod && method == "FindAmongElementsByPoint" )
1365 isPyMeshMethod=true;
1366 theCommand->SetMethod( "FindElementsByPoint" );
1367 // make the 1st arg be the last one
1368 _pyID partID = theCommand->GetArg( 1 );
1369 int nbArgs = theCommand->GetNbArgs();
1370 for ( int i = 2; i <= nbArgs; ++i )
1371 theCommand->SetArg( i-1, theCommand->GetArg( i ));
1372 theCommand->SetArg( nbArgs, partID );
1375 // meshes made by *MakeMesh() methods are not wrapped by _pyMesh,
1376 // so let _pyMesh care of it (TMP?)
1377 // if ( theCommand->GetMethod().Search("MakeMesh") != -1 )
1378 // _pyMesh( new _pyCommand( theCommand->GetString(), 0 )); // for theGen->SetAccessorMethod()
1379 if ( isPyMeshMethod )
1381 theCommand->SetObject( myMesh );
1385 // editor creation command is needed only if any editor function is called
1386 theGen->AddMeshAccessorMethod( theCommand ); // for *Object()
1387 if ( !myCreationCmdStr.IsEmpty() ) {
1388 GetCreationCmd()->GetString() = myCreationCmdStr;
1389 myCreationCmdStr.Clear();
1394 //================================================================================
1396 * \brief _pyHypothesis constructor
1397 * \param theCreationCmd -
1399 //================================================================================
1401 _pyHypothesis::_pyHypothesis(const Handle(_pyCommand)& theCreationCmd):
1402 _pyObject( theCreationCmd )
1404 myIsAlgo = myIsWrapped = /*myIsConverted = myIsLocal = myDim = */false;
1407 //================================================================================
1409 * \brief Creates algorithm or hypothesis
1410 * \param theCreationCmd - The engine command creating a hypothesis
1411 * \retval Handle(_pyHypothesis) - Result _pyHypothesis
1413 //================================================================================
1415 Handle(_pyHypothesis) _pyHypothesis::NewHypothesis( const Handle(_pyCommand)& theCreationCmd)
1417 // theCreationCmd: CreateHypothesis( "theHypType", "theLibName" )
1418 ASSERT (( theCreationCmd->GetMethod() == "CreateHypothesis"));
1420 Handle(_pyHypothesis) hyp, algo;
1423 const TCollection_AsciiString & hypTypeQuoted = theCreationCmd->GetArg( 1 );
1424 if ( hypTypeQuoted.IsEmpty() )
1427 TCollection_AsciiString hypType =
1428 hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
1430 algo = new _pyAlgorithm( theCreationCmd );
1431 hyp = new _pyHypothesis( theCreationCmd );
1433 // 1D Regular_1D ----------
1434 if ( hypType == "Regular_1D" ) {
1435 // set mesh's method creating algo,
1436 // i.e. convertion result will be "regular1d = Mesh.Segment()",
1437 // and set hypType by which algo creating a hypothesis is searched for
1438 algo->SetConvMethodAndType("Segment", hypType.ToCString());
1440 else if ( hypType == "CompositeSegment_1D" ) {
1441 algo->SetConvMethodAndType("Segment", "Regular_1D");
1442 algo->myArgs.Append( "algo=smesh.COMPOSITE");
1444 else if ( hypType == "LocalLength" ) {
1445 // set algo's method creating hyp, and algo type
1446 hyp->SetConvMethodAndType( "LocalLength", "Regular_1D");
1447 // set method whose 1 arg will become the 1-st arg of hyp creation command
1448 // i.e. convertion result will be "locallength = regular1d.LocalLength(<arg of SetLength()>)"
1449 hyp->AddArgMethod( "SetLength" );
1451 else if ( hypType == "MaxLength" ) {
1452 // set algo's method creating hyp, and algo type
1453 hyp->SetConvMethodAndType( "MaxSize", "Regular_1D");
1454 // set method whose 1 arg will become the 1-st arg of hyp creation command
1455 // i.e. convertion result will be "maxsize = regular1d.MaxSize(<arg of SetLength()>)"
1456 hyp->AddArgMethod( "SetLength" );
1458 else if ( hypType == "NumberOfSegments" ) {
1459 hyp = new _pyNumberOfSegmentsHyp( theCreationCmd );
1460 hyp->SetConvMethodAndType( "NumberOfSegments", "Regular_1D");
1461 // arg of SetNumberOfSegments() will become the 1-st arg of hyp creation command
1462 hyp->AddArgMethod( "SetNumberOfSegments" );
1463 // arg of SetScaleFactor() will become the 2-nd arg of hyp creation command
1464 hyp->AddArgMethod( "SetScaleFactor" );
1465 hyp->AddArgMethod( "SetReversedEdges" );
1467 else if ( hypType == "Arithmetic1D" ) {
1468 hyp = new _pyComplexParamHypo( theCreationCmd );
1469 hyp->SetConvMethodAndType( "Arithmetic1D", "Regular_1D");
1470 hyp->AddArgMethod( "SetStartLength" );
1471 hyp->AddArgMethod( "SetEndLength" );
1472 hyp->AddArgMethod( "SetReversedEdges" );
1474 else if ( hypType == "StartEndLength" ) {
1475 hyp = new _pyComplexParamHypo( theCreationCmd );
1476 hyp->SetConvMethodAndType( "StartEndLength", "Regular_1D");
1477 hyp->AddArgMethod( "SetStartLength" );
1478 hyp->AddArgMethod( "SetEndLength" );
1479 hyp->AddArgMethod( "SetReversedEdges" );
1481 else if ( hypType == "Deflection1D" ) {
1482 hyp->SetConvMethodAndType( "Deflection1D", "Regular_1D");
1483 hyp->AddArgMethod( "SetDeflection" );
1485 else if ( hypType == "Propagation" ) {
1486 hyp->SetConvMethodAndType( "Propagation", "Regular_1D");
1488 else if ( hypType == "QuadraticMesh" ) {
1489 hyp->SetConvMethodAndType( "QuadraticMesh", "Regular_1D");
1491 else if ( hypType == "AutomaticLength" ) {
1492 hyp->SetConvMethodAndType( "AutomaticLength", "Regular_1D");
1493 hyp->AddArgMethod( "SetFineness");
1495 else if ( hypType == "SegmentLengthAroundVertex" ) {
1496 hyp = new _pySegmentLengthAroundVertexHyp( theCreationCmd );
1497 hyp->SetConvMethodAndType( "LengthNearVertex", "Regular_1D" );
1498 hyp->AddArgMethod( "SetLength" );
1500 // 1D Python_1D ----------
1501 else if ( hypType == "Python_1D" ) {
1502 algo->SetConvMethodAndType( "Segment", hypType.ToCString());
1503 algo->myArgs.Append( "algo=smesh.PYTHON");
1505 else if ( hypType == "PythonSplit1D" ) {
1506 hyp->SetConvMethodAndType( "PythonSplit1D", "Python_1D");
1507 hyp->AddArgMethod( "SetNumberOfSegments");
1508 hyp->AddArgMethod( "SetPythonLog10RatioFunction");
1510 // MEFISTO_2D ----------
1511 else if ( hypType == "MEFISTO_2D" ) { // MEFISTO_2D
1512 algo->SetConvMethodAndType( "Triangle", hypType.ToCString());
1514 else if ( hypType == "MaxElementArea" ) {
1515 hyp->SetConvMethodAndType( "MaxElementArea", "MEFISTO_2D");
1516 hyp->SetConvMethodAndType( "MaxElementArea", "NETGEN_2D_ONLY");
1517 hyp->AddArgMethod( "SetMaxElementArea");
1519 else if ( hypType == "LengthFromEdges" ) {
1520 hyp->SetConvMethodAndType( "LengthFromEdges", "MEFISTO_2D");
1521 hyp->SetConvMethodAndType( "LengthFromEdges", "NETGEN_2D_ONLY");
1523 // Quadrangle_2D ----------
1524 else if ( hypType == "Quadrangle_2D" ) {
1525 algo->SetConvMethodAndType( "Quadrangle" , hypType.ToCString());
1527 else if ( hypType == "QuadranglePreference" ) {
1528 hyp->SetConvMethodAndType( "QuadranglePreference", "Quadrangle_2D");
1529 hyp->SetConvMethodAndType( "SetQuadAllowed", "NETGEN_2D_ONLY");
1531 else if ( hypType == "TrianglePreference" ) {
1532 hyp->SetConvMethodAndType( "TrianglePreference", "Quadrangle_2D");
1534 // RadialQuadrangle_1D2D ----------
1535 else if ( hypType == "RadialQuadrangle_1D2D" ) {
1536 algo->SetConvMethodAndType( "Quadrangle" , hypType.ToCString());
1537 algo->myArgs.Append( "algo=smesh.RADIAL_QUAD" );
1539 else if ( hypType == "NumberOfLayers2D" ) {
1540 hyp->SetConvMethodAndType( "NumberOfLayers", "RadialQuadrangle_1D2D");
1541 hyp->AddArgMethod( "SetNumberOfLayers" );
1543 else if ( hypType == "LayerDistribution2D" ) {
1544 hyp = new _pyLayerDistributionHypo( theCreationCmd, "Get2DHypothesis" );
1545 hyp->SetConvMethodAndType( "LayerDistribution", "RadialQuadrangle_1D2D");
1547 // BLSURF ----------
1548 else if ( hypType == "BLSURF" ) {
1549 algo->SetConvMethodAndType( "Triangle", hypType.ToCString());
1550 algo->myArgs.Append( "algo=smesh.BLSURF" );
1552 else if ( hypType == "BLSURF_Parameters") {
1553 hyp->SetConvMethodAndType( "Parameters", "BLSURF");
1555 // NETGEN ----------
1556 else if ( hypType == "NETGEN_2D") { // 1D-2D
1557 algo->SetConvMethodAndType( "Triangle" , hypType.ToCString());
1558 algo->myArgs.Append( "algo=smesh.NETGEN" );
1560 else if ( hypType == "NETGEN_Parameters_2D") {
1561 hyp->SetConvMethodAndType( "Parameters", "NETGEN_2D");
1563 else if ( hypType == "NETGEN_SimpleParameters_2D") {
1564 hyp->SetConvMethodAndType( "Parameters", "NETGEN_2D");
1565 hyp->myArgs.Append( "which=smesh.SIMPLE" );
1567 else if ( hypType == "NETGEN_2D3D") { // 1D-2D-3D
1568 algo->SetConvMethodAndType( "Tetrahedron" , hypType.ToCString());
1569 algo->myArgs.Append( "algo=smesh.FULL_NETGEN" );
1571 else if ( hypType == "NETGEN_Parameters") {
1572 hyp->SetConvMethodAndType( "Parameters", "NETGEN_2D3D");
1574 else if ( hypType == "NETGEN_SimpleParameters_3D") {
1575 hyp->SetConvMethodAndType( "Parameters", "NETGEN_2D3D");
1576 hyp->myArgs.Append( "which=smesh.SIMPLE" );
1578 else if ( hypType == "NETGEN_2D_ONLY") { // 2D
1579 algo->SetConvMethodAndType( "Triangle" , hypType.ToCString());
1580 algo->myArgs.Append( "algo=smesh.NETGEN_2D" );
1582 else if ( hypType == "NETGEN_3D") { // 3D
1583 algo->SetConvMethodAndType( "Tetrahedron" , hypType.ToCString());
1584 algo->myArgs.Append( "algo=smesh.NETGEN" );
1586 else if ( hypType == "MaxElementVolume") {
1587 hyp->SetConvMethodAndType( "MaxElementVolume", "NETGEN_3D");
1588 hyp->AddArgMethod( "SetMaxElementVolume" );
1590 // GHS3D_3D ----------
1591 else if ( hypType == "GHS3D_3D" ) {
1592 algo->SetConvMethodAndType( "Tetrahedron", hypType.ToCString());
1593 algo->myArgs.Append( "algo=smesh.GHS3D" );
1595 else if ( hypType == "GHS3D_Parameters") {
1596 hyp->SetConvMethodAndType( "Parameters", "GHS3D_3D");
1598 // Hexa_3D ---------
1599 else if ( hypType == "BLSURF" ) {
1600 algo->SetConvMethodAndType( "Hexahedron", hypType.ToCString());
1602 // Repetitive Projection_1D ---------
1603 else if ( hypType == "Projection_1D" ) {
1604 algo->SetConvMethodAndType( "Projection1D", hypType.ToCString());
1606 else if ( hypType == "ProjectionSource1D" ) {
1607 hyp->SetConvMethodAndType( "SourceEdge", "Projection_1D");
1608 hyp->AddArgMethod( "SetSourceEdge");
1609 hyp->AddArgMethod( "SetSourceMesh");
1610 // 2 args of SetVertexAssociation() will become the 3-th and 4-th args of hyp creation command
1611 hyp->AddArgMethod( "SetVertexAssociation", 2 );
1613 // Projection_2D ---------
1614 else if ( hypType == "Projection_2D" ) {
1615 algo->SetConvMethodAndType( "Projection2D", hypType.ToCString());
1617 else if ( hypType == "Projection_1D2D" ) {
1618 algo->SetConvMethodAndType( "Projection1D2D", hypType.ToCString());
1620 else if ( hypType == "ProjectionSource2D" ) {
1621 hyp->SetConvMethodAndType( "SourceFace", "Projection_2D");
1622 hyp->AddArgMethod( "SetSourceFace");
1623 hyp->AddArgMethod( "SetSourceMesh");
1624 hyp->AddArgMethod( "SetVertexAssociation", 4 );
1626 // Projection_3D ---------
1627 else if ( hypType == "Projection_3D" ) {
1628 algo->SetConvMethodAndType( "Projection3D", hypType.ToCString());
1630 else if ( hypType == "ProjectionSource3D" ) {
1631 hyp->SetConvMethodAndType( "SourceShape3D", "Projection_3D");
1632 hyp->AddArgMethod( "SetSource3DShape");
1633 hyp->AddArgMethod( "SetSourceMesh");
1634 hyp->AddArgMethod( "SetVertexAssociation", 4 );
1636 // Prism_3D ---------
1637 else if ( hypType == "Prism_3D" ) {
1638 algo->SetConvMethodAndType( "Prism", hypType.ToCString());
1640 // RadialPrism_3D ---------
1641 else if ( hypType == "RadialPrism_3D" ) {
1642 algo->SetConvMethodAndType( "Prism", hypType.ToCString());
1644 else if ( hypType == "NumberOfLayers" ) {
1645 hyp->SetConvMethodAndType( "NumberOfLayers", "RadialPrism_3D");
1646 hyp->AddArgMethod( "SetNumberOfLayers" );
1648 else if ( hypType == "LayerDistribution" ) {
1649 hyp = new _pyLayerDistributionHypo( theCreationCmd, "Get3DHypothesis" );
1650 hyp->SetConvMethodAndType( "LayerDistribution", "RadialPrism_3D");
1653 return algo->IsValid() ? algo : hyp;
1656 //================================================================================
1658 * \brief Convert the command adding a hypothesis to mesh into a smesh command
1659 * \param theCmd - The command like mesh.AddHypothesis( geom, hypo )
1660 * \param theAlgo - The algo that can create this hypo
1661 * \retval bool - false if the command cant be converted
1663 //================================================================================
1665 bool _pyHypothesis::Addition2Creation( const Handle(_pyCommand)& theCmd,
1666 const _pyID& theMesh)
1668 ASSERT(( theCmd->GetMethod() == "AddHypothesis" ));
1670 if ( !IsWrappable( theMesh ))
1673 myGeom = theCmd->GetArg( 1 );
1675 Handle(_pyHypothesis) algo;
1677 // find algo created on myGeom in theMesh
1678 algo = theGen->FindAlgo( myGeom, theMesh, this );
1679 if ( algo.IsNull() )
1681 // attach hypothesis creation command to be after algo creation command
1682 // because it can be new created instance of algorithm
1683 algo->GetCreationCmd()->AddDependantCmd( theCmd );
1687 // mesh.AddHypothesis(geom,hyp) --> hyp = <theMesh or algo>.myCreationMethod(args)
1688 theCmd->SetResultValue( GetID() );
1689 theCmd->SetObject( IsAlgo() ? theMesh : algo->GetID());
1690 theCmd->SetMethod( IsAlgo() ? GetAlgoCreationMethod() : GetCreationMethod( algo->GetAlgoType() ));
1692 theCmd->RemoveArgs();
1693 for ( int i = 1; i <= myArgs.Length(); ++i ) {
1694 if ( !myArgs( i ).IsEmpty() )
1695 theCmd->SetArg( i, myArgs( i ));
1697 theCmd->SetArg( i, "[]");
1699 // set a new creation command
1700 GetCreationCmd()->Clear();
1701 // replace creation command by wrapped instance
1702 // please note, that hypothesis attaches to algo creation command (see upper)
1703 SetCreationCmd( theCmd );
1706 // clear commands setting arg values
1707 list < Handle(_pyCommand) >::iterator argCmd = myArgCommands.begin();
1708 for ( ; argCmd != myArgCommands.end(); ++argCmd )
1711 // set unknown arg commands after hypo creation
1712 Handle(_pyCommand) afterCmd = myIsWrapped ? theCmd : GetCreationCmd();
1713 list<Handle(_pyCommand)>::iterator cmd = myUnknownCommands.begin();
1714 for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
1715 afterCmd->AddDependantCmd( *cmd );
1721 //================================================================================
1723 * \brief Remember hypothesis parameter values
1724 * \param theCommand - The called hypothesis method
1726 //================================================================================
1728 void _pyHypothesis::Process( const Handle(_pyCommand)& theCommand)
1730 ASSERT( !myIsAlgo );
1733 for ( int i = 1; i <= myArgMethods.Length(); ++i ) {
1734 if ( myArgMethods( i ) == theCommand->GetMethod() ) {
1735 while ( myArgs.Length() < nbArgs + myNbArgsByMethod( i ))
1736 myArgs.Append( "[]" );
1737 for ( int iArg = 1; iArg <= myNbArgsByMethod( i ); ++iArg )
1738 myArgs( nbArgs + iArg ) = theCommand->GetArg( iArg ); // arg value
1739 myArgCommands.push_back( theCommand );
1742 nbArgs += myNbArgsByMethod( i );
1744 myUnknownCommands.push_back( theCommand );
1747 //================================================================================
1749 * \brief Finish conversion
1751 //================================================================================
1753 void _pyHypothesis::Flush()
1755 if ( IsWrapped() ) {
1758 list < Handle(_pyCommand) >::iterator cmd = myArgCommands.begin();
1759 for ( ; cmd != myArgCommands.end(); ++cmd ) {
1760 // Add access to a wrapped mesh
1761 theGen->AddMeshAccessorMethod( *cmd );
1762 // Add access to a wrapped algorithm
1763 theGen->AddAlgoAccessorMethod( *cmd );
1765 cmd = myUnknownCommands.begin();
1766 for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
1767 // Add access to a wrapped mesh
1768 theGen->AddMeshAccessorMethod( *cmd );
1769 // Add access to a wrapped algorithm
1770 theGen->AddAlgoAccessorMethod( *cmd );
1773 // forget previous hypothesis modifications
1774 myArgCommands.clear();
1775 myUnknownCommands.clear();
1778 //================================================================================
1780 * \brief clear creation, arg and unkown commands
1782 //================================================================================
1784 void _pyHypothesis::ClearAllCommands()
1786 GetCreationCmd()->Clear();
1787 list<Handle(_pyCommand)>::iterator cmd = myArgCommands.begin();
1788 for ( ; cmd != myArgCommands.end(); ++cmd )
1790 cmd = myUnknownCommands.begin();
1791 for ( ; cmd != myUnknownCommands.end(); ++cmd )
1796 //================================================================================
1798 * \brief Assign fields of theOther to me except myIsWrapped
1800 //================================================================================
1802 void _pyHypothesis::Assign( const Handle(_pyHypothesis)& theOther,
1803 const _pyID& theMesh )
1805 myIsWrapped = false;
1808 // myCreationCmd = theOther->myCreationCmd;
1809 myIsAlgo = theOther->myIsAlgo;
1810 myGeom = theOther->myGeom;
1811 myType2CreationMethod = theOther->myType2CreationMethod;
1812 myArgs = theOther->myArgs;
1813 myArgMethods = theOther->myArgMethods;
1814 myNbArgsByMethod = theOther->myNbArgsByMethod;
1815 myArgCommands = theOther->myArgCommands;
1816 myUnknownCommands = theOther->myUnknownCommands;
1819 //================================================================================
1821 * \brief Remember hypothesis parameter values
1822 * \param theCommand - The called hypothesis method
1824 //================================================================================
1826 void _pyComplexParamHypo::Process( const Handle(_pyCommand)& theCommand)
1828 if( theCommand->GetMethod() == "SetLength" )
1830 // NOW it becomes OBSOLETE
1831 // ex: hyp.SetLength(start, 1)
1832 // hyp.SetLength(end, 0)
1833 ASSERT(( theCommand->GetArg( 2 ).IsIntegerValue() ));
1834 int i = 2 - theCommand->GetArg( 2 ).IntegerValue();
1835 while ( myArgs.Length() < i )
1836 myArgs.Append( "[]" );
1837 myArgs( i ) = theCommand->GetArg( 1 ); // arg value
1838 myArgCommands.push_back( theCommand );
1842 _pyHypothesis::Process( theCommand );
1845 //================================================================================
1847 * \brief Clear SetObjectEntry() as it is called by methods of Mesh_Segment
1849 //================================================================================
1851 void _pyComplexParamHypo::Flush()
1855 list < Handle(_pyCommand) >::iterator cmd = myUnknownCommands.begin();
1856 for ( ; cmd != myUnknownCommands.end(); ++cmd )
1857 if ((*cmd)->GetMethod() == "SetObjectEntry" )
1862 //================================================================================
1864 * \brief Convert methods of 1D hypotheses to my own methods
1865 * \param theCommand - The called hypothesis method
1867 //================================================================================
1869 void _pyLayerDistributionHypo::Process( const Handle(_pyCommand)& theCommand)
1871 if ( theCommand->GetMethod() != "SetLayerDistribution" )
1874 _pyID newName; // name for 1D hyp = "HypType" + "_Distribution"
1876 const _pyID& hyp1dID = theCommand->GetArg( 1 );
1877 Handle(_pyHypothesis) hyp1d = theGen->FindHyp( hyp1dID );
1878 if ( hyp1d.IsNull() ) // apparently hypId changed at study restoration
1880 else if ( !my1dHyp.IsNull() && hyp1dID != my1dHyp->GetID() ) {
1881 // 1D hypo is already set, so distribution changes and the old
1882 // 1D hypo is thrown away
1883 my1dHyp->ClearAllCommands();
1887 if ( !myArgCommands.empty() )
1888 myArgCommands.front()->Clear();
1889 myArgCommands.push_back( theCommand );
1892 //================================================================================
1895 * \param theAdditionCmd - command to be converted
1896 * \param theMesh - mesh instance
1897 * \retval bool - status
1899 //================================================================================
1901 bool _pyLayerDistributionHypo::Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
1902 const _pyID& theMesh)
1904 myIsWrapped = false;
1906 if ( my1dHyp.IsNull() )
1909 // set "SetLayerDistribution()" after addition cmd
1910 theAdditionCmd->AddDependantCmd( myArgCommands.front() );
1912 _pyID geom = theAdditionCmd->GetArg( 1 );
1914 Handle(_pyHypothesis) algo = theGen->FindAlgo( geom, theMesh, this );
1915 if ( !algo.IsNull() )
1917 my1dHyp->SetMesh( theMesh );
1918 my1dHyp->SetConvMethodAndType(my1dHyp->GetAlgoCreationMethod().ToCString(),
1919 algo->GetAlgoType().ToCString());
1920 if ( !my1dHyp->Addition2Creation( theAdditionCmd, theMesh ))
1923 // clear "SetLayerDistribution()" cmd
1924 myArgCommands.back()->Clear();
1926 // Convert my creation => me = RadialPrismAlgo.Get3DHypothesis()
1928 // find RadialPrism algo created on <geom> for theMesh
1929 GetCreationCmd()->SetObject( algo->GetID() );
1930 GetCreationCmd()->SetMethod( myAlgoMethod );
1931 GetCreationCmd()->RemoveArgs();
1932 theAdditionCmd->AddDependantCmd( GetCreationCmd() );
1938 //================================================================================
1942 //================================================================================
1944 void _pyLayerDistributionHypo::Flush()
1946 // as creation of 1D hyp was written later then it's edition,
1947 // we need to find all it's edition calls and process them
1948 if ( !my1dHyp.IsNull() )
1950 _pyID hyp1dID = my1dHyp->GetCreationCmd()->GetResultValue();
1952 // make a new name for 1D hyp = "HypType" + "_Distribution"
1954 if ( my1dHyp->IsWrapped() ) {
1955 newName = my1dHyp->GetCreationCmd()->GetMethod();
1958 TCollection_AsciiString hypTypeQuoted = my1dHyp->GetCreationCmd()->GetArg(1);
1959 newName = hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
1961 newName += "_Distribution";
1962 my1dHyp->GetCreationCmd()->SetResultValue( newName );
1964 list< Handle(_pyCommand) >& cmds = theGen->GetCommands();
1965 list< Handle(_pyCommand) >::iterator cmdIt = cmds.begin();
1966 for ( ; cmdIt != cmds.end(); ++cmdIt ) {
1967 const _pyID& objID = (*cmdIt)->GetObject();
1968 if ( objID == hyp1dID ) {
1969 my1dHyp->Process( *cmdIt );
1970 my1dHyp->GetCreationCmd()->AddDependantCmd( *cmdIt );
1971 ( *cmdIt )->SetObject( newName );
1974 // Set new hyp name to SetLayerDistribution() cmd
1975 if ( !myArgCommands.empty() && !myArgCommands.back()->IsEmpty() )
1976 myArgCommands.back()->SetArg( 1, newName );
1980 //================================================================================
1982 * \brief additionally to Addition2Creation, clears SetDistrType() command
1983 * \param theCmd - AddHypothesis() command
1984 * \param theMesh - mesh to which a hypothesis is added
1985 * \retval bool - convertion result
1987 //================================================================================
1989 bool _pyNumberOfSegmentsHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
1990 const _pyID& theMesh)
1992 if ( IsWrappable( theMesh ) && myArgs.Length() > 1 ) {
1993 // scale factor (2-nd arg) is provided: clear SetDistrType(1) command
1994 bool scaleDistrType = false;
1995 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnknownCommands.rbegin();
1996 for ( ; cmd != myUnknownCommands.rend(); ++cmd ) {
1997 if ( (*cmd)->GetMethod() == "SetDistrType" ) {
1998 if ( (*cmd)->GetArg( 1 ) == "1" ) {
1999 scaleDistrType = true;
2002 else if ( !scaleDistrType ) {
2003 // distribution type changed: remove scale factor from args
2004 myArgs.Remove( 2, myArgs.Length() );
2010 return _pyHypothesis::Addition2Creation( theCmd, theMesh );
2013 //================================================================================
2015 * \brief remove repeated commands defining distribution
2017 //================================================================================
2019 void _pyNumberOfSegmentsHyp::Flush()
2021 // find number of the last SetDistrType() command
2022 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnknownCommands.rbegin();
2023 int distrTypeNb = 0;
2024 for ( ; !distrTypeNb && cmd != myUnknownCommands.rend(); ++cmd )
2025 if ( (*cmd)->GetMethod() == "SetDistrType" )
2026 distrTypeNb = (*cmd)->GetOrderNb();
2027 else if (IsWrapped() && (*cmd)->GetMethod() == "SetObjectEntry" )
2030 // clear commands before the last SetDistrType()
2031 list<Handle(_pyCommand)> * cmds[2] = { &myArgCommands, &myUnknownCommands };
2032 for ( int i = 0; i < 2; ++i ) {
2033 set<TCollection_AsciiString> uniqueMethods;
2034 list<Handle(_pyCommand)> & cmdList = *cmds[i];
2035 for ( cmd = cmdList.rbegin(); cmd != cmdList.rend(); ++cmd )
2037 bool clear = ( (*cmd)->GetOrderNb() < distrTypeNb );
2038 const TCollection_AsciiString& method = (*cmd)->GetMethod();
2039 if ( !clear || method == "SetNumberOfSegments" ) {
2040 bool isNewInSet = uniqueMethods.insert( method ).second;
2041 clear = !isNewInSet;
2050 //================================================================================
2052 * \brief Convert the command adding "SegmentLengthAroundVertex" to mesh
2053 * into regular1D.LengthNearVertex( length, vertex )
2054 * \param theCmd - The command like mesh.AddHypothesis( vertex, SegmentLengthAroundVertex )
2055 * \param theMesh - The mesh needing this hypo
2056 * \retval bool - false if the command cant be converted
2058 //================================================================================
2060 bool _pySegmentLengthAroundVertexHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
2061 const _pyID& theMeshID)
2063 if ( IsWrappable( theMeshID )) {
2065 _pyID vertex = theCmd->GetArg( 1 );
2067 // the problem here is that segment algo will not be found
2068 // by pyHypothesis::Addition2Creation() for <vertex>, so we try to find
2069 // geometry where segment algorithm is assigned
2070 Handle(_pyHypothesis) algo;
2071 _pyID geom = vertex;
2072 while ( algo.IsNull() && !geom.IsEmpty()) {
2073 // try to find geom as a father of <vertex>
2074 geom = FatherID( geom );
2075 algo = theGen->FindAlgo( geom, theMeshID, this );
2077 if ( algo.IsNull() )
2078 return false; // also possible to find geom as brother of veretex...
2079 // set geom instead of vertex
2080 theCmd->SetArg( 1, geom );
2082 // set vertex as a second arg
2083 if ( myArgs.Length() < 1) myArgs.Append( "1" ); // :(
2084 myArgs.Append( vertex );
2086 // mesh.AddHypothesis(vertex, SegmentLengthAroundVertex) -->
2087 // theMeshID.LengthNearVertex( length, vertex )
2088 return _pyHypothesis::Addition2Creation( theCmd, theMeshID );
2093 //================================================================================
2095 * \brief _pyAlgorithm constructor
2096 * \param theCreationCmd - The command like "algo = smeshgen.CreateHypothesis(type,lib)"
2098 //================================================================================
2100 _pyAlgorithm::_pyAlgorithm(const Handle(_pyCommand)& theCreationCmd)
2101 : _pyHypothesis( theCreationCmd )
2106 //================================================================================
2108 * \brief Convert the command adding an algorithm to mesh
2109 * \param theCmd - The command like mesh.AddHypothesis( geom, algo )
2110 * \param theMesh - The mesh needing this algo
2111 * \retval bool - false if the command cant be converted
2113 //================================================================================
2115 bool _pyAlgorithm::Addition2Creation( const Handle(_pyCommand)& theCmd,
2116 const _pyID& theMeshID)
2118 // mesh.AddHypothesis(geom,algo) --> theMeshID.myCreationMethod()
2119 if ( _pyHypothesis::Addition2Creation( theCmd, theMeshID )) {
2120 theGen->SetAccessorMethod( GetID(), "GetAlgorithm()" );
2126 //================================================================================
2128 * \brief Return starting position of a part of python command
2129 * \param thePartIndex - The index of command part
2130 * \retval int - Part position
2132 //================================================================================
2134 int _pyCommand::GetBegPos( int thePartIndex )
2138 if ( myBegPos.Length() < thePartIndex )
2140 return myBegPos( thePartIndex );
2143 //================================================================================
2145 * \brief Store starting position of a part of python command
2146 * \param thePartIndex - The index of command part
2147 * \param thePosition - Part position
2149 //================================================================================
2151 void _pyCommand::SetBegPos( int thePartIndex, int thePosition )
2153 while ( myBegPos.Length() < thePartIndex )
2154 myBegPos.Append( UNKNOWN );
2155 myBegPos( thePartIndex ) = thePosition;
2158 //================================================================================
2160 * \brief Returns whitespace symbols at the line beginning
2161 * \retval TCollection_AsciiString - result
2163 //================================================================================
2165 TCollection_AsciiString _pyCommand::GetIndentation()
2168 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
2169 GetWord( myString, end, true );
2171 end = GetBegPos( RESULT_IND );
2172 return myString.SubString( 1, end - 1 );
2175 //================================================================================
2177 * \brief Return substring of python command looking like ResultValue = Obj.Meth()
2178 * \retval const TCollection_AsciiString & - ResultValue substring
2180 //================================================================================
2182 const TCollection_AsciiString & _pyCommand::GetResultValue()
2184 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
2186 int begPos = myString.Location( "=", 1, Length() );
2188 myRes = GetWord( myString, begPos, false );
2191 SetBegPos( RESULT_IND, begPos );
2196 //================================================================================
2198 * \brief Return number of python command result value ResultValue = Obj.Meth()
2201 //================================================================================
2203 const int _pyCommand::GetNbResultValues()
2207 int endPos = myString.Location( "=", 1, Length() );
2208 TCollection_AsciiString str = "";
2209 while ( begPos < endPos) {
2210 str = GetWord( myString, begPos, true );
2211 begPos = begPos+ str.Length();
2218 //================================================================================
2220 * \brief Return substring of python command looking like
2221 * ResultValue1 , ResultValue1,... = Obj.Meth() with res index
2222 * \retval const TCollection_AsciiString & - ResultValue with res index substring
2224 //================================================================================
2225 const TCollection_AsciiString & _pyCommand::GetResultValue(int res)
2229 int endPos = myString.Location( "=", 1, Length() );
2230 while ( begPos < endPos) {
2231 myRes = GetWord( myString, begPos, true );
2232 begPos = begPos + myRes.Length();
2235 myRes.RemoveAll('[');myRes.RemoveAll(']');
2241 return theEmptyString;
2244 //================================================================================
2246 * \brief Return substring of python command looking like ResVal = Object.Meth()
2247 * \retval const TCollection_AsciiString & - Object substring
2249 //================================================================================
2251 const TCollection_AsciiString & _pyCommand::GetObject()
2253 if ( GetBegPos( OBJECT_IND ) == UNKNOWN )
2256 int begPos = GetBegPos( RESULT_IND ) + myRes.Length();
2258 begPos = myString.Location( "=", 1, Length() ) + 1;
2259 // is '=' in the string argument (for example, name) or not
2260 int nb1 = 0; // number of ' character at the left of =
2261 int nb2 = 0; // number of " character at the left of =
2262 for ( int i = 1; i < begPos-1; i++ ) {
2263 if ( myString.Value( i )=='\'' )
2265 else if ( myString.Value( i )=='"' )
2268 // if number of ' or " is not divisible by 2,
2269 // then get an object at the start of the command
2270 if ( nb1 % 2 != 0 || nb2 % 2 != 0 )
2273 myObj = GetWord( myString, begPos, true );
2274 // check if object is complex,
2275 // so far consider case like "smesh.smesh.Method()"
2276 if ( int bracketPos = myString.Location( "(", begPos, Length() )) {
2277 //if ( bracketPos==0 ) bracketPos = Length();
2278 int dotPos = begPos+myObj.Length();
2279 while ( dotPos+1 < bracketPos ) {
2280 if ( int pos = myString.Location( ".", dotPos+1, bracketPos ))
2285 if ( dotPos > begPos+myObj.Length() )
2286 myObj = myString.SubString( begPos, dotPos-1 );
2289 SetBegPos( OBJECT_IND, begPos );
2295 //================================================================================
2297 * \brief Return substring of python command looking like ResVal = Obj.Method()
2298 * \retval const TCollection_AsciiString & - Method substring
2300 //================================================================================
2302 const TCollection_AsciiString & _pyCommand::GetMethod()
2304 if ( GetBegPos( METHOD_IND ) == UNKNOWN )
2307 int begPos = GetBegPos( OBJECT_IND ) + myObj.Length();
2308 bool forward = true;
2310 begPos = myString.Location( "(", 1, Length() ) - 1;
2314 myMeth = GetWord( myString, begPos, forward );
2315 SetBegPos( METHOD_IND, begPos );
2321 //================================================================================
2323 * \brief Return substring of python command looking like ResVal = Obj.Meth(Arg1,...)
2324 * \retval const TCollection_AsciiString & - Arg<index> substring
2326 //================================================================================
2328 const TCollection_AsciiString & _pyCommand::GetArg( int index )
2330 if ( GetBegPos( ARG1_IND ) == UNKNOWN )
2334 int pos = GetBegPos( METHOD_IND ) + myMeth.Length();
2336 pos = myString.Location( "(", 1, Length() );
2340 // we are at or before '(', skip it if present
2342 while ( pos <= Length() && myString.Value( pos ) != '(' ) ++pos;
2343 if ( myString.Value( pos ) != '(' )
2347 SetBegPos( ARG1_IND, 0 ); // even no '('
2348 return theEmptyString;
2352 list< TCollection_AsciiString > separatorStack( 1, ",)");
2353 bool ignoreNesting = false;
2355 while ( pos <= Length() )
2357 const char chr = myString.Value( pos );
2359 if ( separatorStack.back().Location( chr, 1, separatorStack.back().Length()))
2361 if ( separatorStack.size() == 1 ) // ',' dividing args or a terminal ')' found
2363 while ( pos-1 >= prevPos && isspace( myString.Value( prevPos )))
2365 if ( pos-1 >= prevPos ) {
2366 TCollection_AsciiString arg = myString.SubString( prevPos, pos-1 );
2367 arg.RightAdjust(); // remove spaces
2369 SetBegPos( ARG1_IND + myArgs.Length(), prevPos );
2370 myArgs.Append( arg );
2376 else // end of nesting args found
2378 separatorStack.pop_back();
2379 ignoreNesting = false;
2382 else if ( !ignoreNesting )
2385 case '(' : separatorStack.push_back(")"); break;
2386 case '[' : separatorStack.push_back("]"); break;
2387 case '\'': separatorStack.push_back("'"); ignoreNesting=true; break;
2388 case '"' : separatorStack.push_back("\""); ignoreNesting=true; break;
2395 if ( myArgs.Length() < index )
2396 return theEmptyString;
2397 return myArgs( index );
2400 //================================================================================
2402 * \brief Check if char is a word part
2403 * \param c - The character to check
2404 * \retval bool - The check result
2406 //================================================================================
2408 static inline bool isWord(const char c, const bool dotIsWord)
2411 !isspace(c) && c != ',' && c != '=' && c != ')' && c != '(' && ( dotIsWord || c != '.');
2414 //================================================================================
2416 * \brief Looks for a word in the string and returns word's beginning
2417 * \param theString - The input string
2418 * \param theStartPos - The position to start the search, returning word's beginning
2419 * \param theForward - The search direction
2420 * \retval TCollection_AsciiString - The found word
2422 //================================================================================
2424 TCollection_AsciiString _pyCommand::GetWord( const TCollection_AsciiString & theString,
2426 const bool theForward,
2427 const bool dotIsWord )
2429 int beg = theStartPos, end = theStartPos;
2430 theStartPos = EMPTY;
2431 if ( beg < 1 || beg > theString.Length() )
2432 return theEmptyString;
2434 if ( theForward ) { // search forward
2436 while ( beg <= theString.Length() && !isWord( theString.Value( beg ), dotIsWord))
2438 if ( beg > theString.Length() )
2439 return theEmptyString; // no word found
2442 char begChar = theString.Value( beg );
2443 if ( begChar == '"' || begChar == '\'' || begChar == '[') {
2444 char endChar = ( begChar == '[' ) ? ']' : begChar;
2445 // end is at the corresponding quoting mark or bracket
2446 while ( end < theString.Length() &&
2447 ( theString.Value( end ) != endChar || theString.Value( end-1 ) == '\\'))
2451 while ( end <= theString.Length() && isWord( theString.Value( end ), dotIsWord))
2456 else { // search backward
2458 while ( end > 0 && !isWord( theString.Value( end ), dotIsWord))
2461 return theEmptyString; // no word found
2463 char endChar = theString.Value( end );
2464 if ( endChar == '"' || endChar == '\'' ) {
2465 // beg is at the corresponding quoting mark
2467 ( theString.Value( beg ) != endChar || theString.Value( beg-1 ) == '\\'))
2471 while ( beg > 0 && isWord( theString.Value( beg ), dotIsWord))
2477 //cout << theString << " ---- " << beg << " - " << end << endl;
2478 return theString.SubString( beg, end );
2481 //================================================================================
2483 * \brief Look for position where not space char is
2484 * \param theString - The string
2485 * \param thePos - The position to search from and which returns result
2486 * \retval bool - false if there are only space after thePos in theString
2490 //================================================================================
2492 bool _pyCommand::SkipSpaces( const TCollection_AsciiString & theString, int & thePos )
2494 if ( thePos < 1 || thePos > theString.Length() )
2497 while ( thePos <= theString.Length() && isspace( theString.Value( thePos )))
2500 return thePos <= theString.Length();
2503 //================================================================================
2505 * \brief Modify a part of the command
2506 * \param thePartIndex - The index of the part
2507 * \param thePart - The new part string
2508 * \param theOldPart - The old part
2510 //================================================================================
2512 void _pyCommand::SetPart(int thePartIndex, const TCollection_AsciiString& thePart,
2513 TCollection_AsciiString& theOldPart)
2515 int pos = GetBegPos( thePartIndex );
2516 if ( pos <= Length() && theOldPart != thePart)
2518 TCollection_AsciiString seperator;
2520 pos = GetBegPos( thePartIndex + 1 );
2521 if ( pos < 1 ) return;
2522 switch ( thePartIndex ) {
2523 case RESULT_IND: seperator = " = "; break;
2524 case OBJECT_IND: seperator = "."; break;
2525 case METHOD_IND: seperator = "()"; break;
2529 myString.Remove( pos, theOldPart.Length() );
2530 if ( !seperator.IsEmpty() )
2531 myString.Insert( pos , seperator );
2532 myString.Insert( pos, thePart );
2533 // update starting positions of the following parts
2534 int posDelta = thePart.Length() + seperator.Length() - theOldPart.Length();
2535 for ( int i = thePartIndex + 1; i <= myBegPos.Length(); ++i ) {
2536 if ( myBegPos( i ) > 0 )
2537 myBegPos( i ) += posDelta;
2539 theOldPart = thePart;
2543 //================================================================================
2545 * \brief Set agrument
2546 * \param index - The argument index, it counts from 1
2547 * \param theArg - The argument string
2549 //================================================================================
2551 void _pyCommand::SetArg( int index, const TCollection_AsciiString& theArg)
2554 int argInd = ARG1_IND + index - 1;
2555 int pos = GetBegPos( argInd );
2556 if ( pos < 1 ) // no index-th arg exist, append inexistent args
2558 // find a closing parenthesis
2559 if ( GetNbArgs() != 0 && index <= GetNbArgs() ) {
2560 int lastArgInd = GetNbArgs();
2561 pos = GetBegPos( ARG1_IND + lastArgInd - 1 ) + GetArg( lastArgInd ).Length();
2562 while ( pos > 0 && pos <= Length() && myString.Value( pos ) != ')' )
2567 while ( pos > 0 && myString.Value( pos ) != ')' )
2570 if ( pos < 1 || myString.Value( pos ) != ')' ) { // no parentheses at all
2574 while ( myArgs.Length() < index ) {
2575 if ( myArgs.Length() )
2576 myString.Insert( pos++, "," );
2577 myArgs.Append("None");
2578 myString.Insert( pos, myArgs.Last() );
2579 SetBegPos( ARG1_IND + myArgs.Length() - 1, pos );
2580 pos += myArgs.Last().Length();
2583 SetPart( argInd, theArg, myArgs( index ));
2586 //================================================================================
2588 * \brief Empty arg list
2590 //================================================================================
2592 void _pyCommand::RemoveArgs()
2594 if ( int pos = myString.Location( '(', 1, Length() ))
2595 myString.Trunc( pos );
2598 if ( myBegPos.Length() >= ARG1_IND )
2599 myBegPos.Remove( ARG1_IND, myBegPos.Length() );
2602 //================================================================================
2604 * \brief Set dependent commands after this one
2606 //================================================================================
2608 bool _pyCommand::SetDependentCmdsAfter() const
2610 bool orderChanged = false;
2611 list< Handle(_pyCommand)>::const_reverse_iterator cmd = myDependentCmds.rbegin();
2612 for ( ; cmd != myDependentCmds.rend(); ++cmd ) {
2613 if ( (*cmd)->GetOrderNb() < GetOrderNb() ) {
2614 orderChanged = true;
2615 theGen->SetCommandAfter( *cmd, this );
2616 (*cmd)->SetDependentCmdsAfter();
2619 return orderChanged;
2621 //================================================================================
2623 * \brief Insert accessor method after theObjectID
2624 * \param theObjectID - id of the accessed object
2625 * \param theAcsMethod - name of the method giving access to the object
2626 * \retval bool - false if theObjectID is not found in the command string
2628 //================================================================================
2630 bool _pyCommand::AddAccessorMethod( _pyID theObjectID, const char* theAcsMethod )
2632 if ( !theAcsMethod )
2634 // start object search from the object, i.e. ignore result
2636 int beg = GetBegPos( OBJECT_IND );
2637 if ( beg < 1 || beg > Length() )
2640 while (( beg = myString.Location( theObjectID, beg, Length() )))
2642 // check that theObjectID is not just a part of a longer ID
2643 int afterEnd = beg + theObjectID.Length();
2644 Standard_Character c = myString.Value( afterEnd );
2645 if ( !isalnum( c ) && c != ':' ) {
2646 // check if accessor method already present
2648 myString.Location( (char*) theAcsMethod, afterEnd, Length() ) != afterEnd+1) {
2650 int oldLen = Length();
2651 myString.Insert( afterEnd, (char*) theAcsMethod );
2652 myString.Insert( afterEnd, "." );
2653 // update starting positions of the parts following the modified one
2654 int posDelta = Length() - oldLen;
2655 for ( int i = 1; i <= myBegPos.Length(); ++i ) {
2656 if ( myBegPos( i ) > afterEnd )
2657 myBegPos( i ) += posDelta;
2662 beg = afterEnd; // is a part - next search
2667 //================================================================================
2669 * \brief Return method name giving access to an interaface object wrapped by python class
2670 * \retval const char* - method name
2672 //================================================================================
2674 const char* _pyObject::AccessorMethod() const
2678 //================================================================================
2680 * \brief Return ID of a father
2682 //================================================================================
2684 _pyID _pyObject::FatherID(const _pyID & childID)
2686 int colPos = childID.SearchFromEnd(':');
2688 return childID.SubString( 1, colPos-1 );
2692 //================================================================================
2694 * \brief SelfEraser erases creation command if no more it's commands invoked
2696 //================================================================================
2698 void _pySelfEraser::Flush()
2700 if ( GetNbCalls() == 0 )
2701 GetCreationCmd()->Clear();
2704 //================================================================================
2706 * \brief count invoked commands
2708 //================================================================================
2710 void _pySubMesh::Process( const Handle(_pyCommand)& theCommand )
2712 _pyObject::Process(theCommand); // count calls of Process()
2713 GetCreationCmd()->AddDependantCmd( theCommand );
2716 //================================================================================
2718 * \brief Clear creation command if no commands invoked
2720 //================================================================================
2722 void _pySubMesh::Flush()
2724 if ( GetNbCalls() == 0 ) // move to the end of all commands
2725 theGen->GetLastCommand()->AddDependantCmd( GetCreationCmd() );
2726 else if ( !myCreator.IsNull() )
2727 // move to be just after creator
2728 myCreator->GetCreationCmd()->AddDependantCmd( GetCreationCmd() );
2731 //================================================================================
2733 * \brief To convert creation of a group by filter
2735 //================================================================================
2737 void _pyGroup::Process( const Handle(_pyCommand)& theCommand)
2739 // Convert the following set of commands into mesh.MakeGroupByFilter(groupName, theFilter)
2740 // group = mesh.CreateEmptyGroup( elemType, groupName )
2741 // aFilter.SetMesh(mesh)
2742 // nbAdd = group.AddFrom( aFilter )
2743 if ( theCommand->GetMethod() == "AddFrom" )
2745 _pyID idSource = theCommand->GetArg(1);
2746 // check if idSource is a filter
2747 Handle(_pyObject) filter = theGen->FindObject( idSource );
2748 if ( filter.IsNull() || !filter->IsKind(STANDARD_TYPE(_pyFilter)))
2750 // find aFilter.SetMesh(mesh) to clear it, it should be just before theCommand
2751 list< Handle(_pyCommand) >::reverse_iterator cmdIt = theGen->GetCommands().rbegin();
2752 while ( *cmdIt != theCommand ) ++cmdIt;
2753 while ( (*cmdIt)->GetOrderNb() != 1 )
2755 const Handle(_pyCommand)& setMeshCmd = *(++cmdIt);
2756 if ((setMeshCmd->GetObject() == idSource ||
2757 setMeshCmd->GetObject() == Handle(_pyFilter)::DownCast(filter)->GetNewID() )
2759 setMeshCmd->GetMethod() == "SetMesh")
2761 setMeshCmd->Clear();
2765 // replace 3 commands by one
2766 theCommand->Clear();
2767 const Handle(_pyCommand)& makeGroupCmd = GetCreationCmd();
2768 TCollection_AsciiString name = makeGroupCmd->GetArg( 2 );
2769 makeGroupCmd->SetMethod( "MakeGroupByFilter" );
2770 makeGroupCmd->SetArg( 1, name );
2771 makeGroupCmd->SetArg( 2, idSource );
2772 // set new name of a filter
2773 filter->Process( makeGroupCmd );
2777 //================================================================================
2779 * \brief Constructor of _pyFilter
2781 //================================================================================
2783 _pyFilter::_pyFilter(const Handle(_pyCommand)& theCreationCmd, const _pyID& newID/*=""*/)
2784 :_pyObject(theCreationCmd), myNewID( newID )
2788 //================================================================================
2790 * \brief To convert creation of a filter by criteria and
2791 * to replace an old name by a new one
2793 //================================================================================
2795 void _pyFilter::Process( const Handle(_pyCommand)& theCommand)
2797 if ( theCommand->GetObject() == GetID() )
2798 _pyObject::Process(theCommand); // count commands
2800 if ( !myNewID.IsEmpty() )
2802 if ( theCommand->GetObject() == GetID() )
2803 theCommand->SetObject( myNewID );
2804 else if ( theCommand->GetResultValue() == GetID() )
2805 theCommand->SetResultValue( myNewID );
2807 for ( int i = 1, nb = theCommand->GetNbArgs(); i <= nb; ++i )
2808 if ( theCommand->GetArg( i ) == GetID() )
2810 theCommand->SetArg( i, myNewID );
2815 // Convert the following set of commands into smesh.GetFilterFromCriteria(criteria)
2816 // aFilter0x2aaab0487080 = aFilterManager.CreateFilter()
2817 // aFilter0x2aaab0487080.SetCriteria(aCriteria)
2818 if ( GetNbCalls() == 0 && // none method was called before SetCriteria()
2819 theCommand->GetMethod() == "SetCriteria")
2821 // aFilter.SetCriteria(aCriteria) ->
2822 // aFilter = smesh.GetFilterFromCriteria(criteria)
2823 if ( myNewID.IsEmpty() )
2824 theCommand->SetResultValue( GetID() );
2826 theCommand->SetResultValue( myNewID );
2827 theCommand->SetObject( SMESH_2smeshpy::GenName() );
2828 theCommand->SetMethod( "GetFilterFromCriteria" );
2830 // Clear aFilterManager.CreateFilter()
2831 GetCreationCmd()->Clear();
2833 else if ( theCommand->GetMethod() == "SetMesh")
2835 theGen->AddMeshAccessorMethod( theCommand );
2839 //================================================================================
2841 * \brief Set new filter name to the creation command
2843 //================================================================================
2845 void _pyFilter::Flush()
2847 if ( !myNewID.IsEmpty() && !GetCreationCmd()->IsEmpty() )
2848 GetCreationCmd()->SetResultValue( myNewID );