1 // File : SMESH_2smeshpy.cxx
2 // Created : Fri Nov 18 13:20:10 2005
3 // Author : Edward AGAPOV (eap)
5 #include "SMESH_2smeshpy.hxx"
7 #include "SMESH_Gen_i.hxx"
9 #include "SMESH_PythonDump.hxx"
10 #include "Resource_DataMapOfAsciiStringAsciiString.hxx"
12 IMPLEMENT_STANDARD_HANDLE (_pyObject ,Standard_Transient);
13 IMPLEMENT_STANDARD_HANDLE (_pyCommand ,Standard_Transient);
14 IMPLEMENT_STANDARD_HANDLE (_pyGen ,_pyObject);
15 IMPLEMENT_STANDARD_HANDLE (_pyMesh ,_pyObject);
16 IMPLEMENT_STANDARD_HANDLE (_pyHypothesis ,_pyObject);
17 IMPLEMENT_STANDARD_HANDLE (_pyAlgorithm ,_pyHypothesis);
18 IMPLEMENT_STANDARD_HANDLE (_pyComplexParamHypo,_pyHypothesis);
19 IMPLEMENT_STANDARD_HANDLE (_pyNumberOfSegmentsHyp,_pyHypothesis);
21 IMPLEMENT_STANDARD_RTTIEXT(_pyObject ,Standard_Transient);
22 IMPLEMENT_STANDARD_RTTIEXT(_pyCommand ,Standard_Transient);
23 IMPLEMENT_STANDARD_RTTIEXT(_pyGen ,_pyObject);
24 IMPLEMENT_STANDARD_RTTIEXT(_pyMesh ,_pyObject);
25 IMPLEMENT_STANDARD_RTTIEXT(_pyHypothesis ,_pyObject);
26 IMPLEMENT_STANDARD_RTTIEXT(_pyAlgorithm ,_pyHypothesis);
27 IMPLEMENT_STANDARD_RTTIEXT(_pyComplexParamHypo,_pyHypothesis);
28 IMPLEMENT_STANDARD_RTTIEXT(_pyNumberOfSegmentsHyp,_pyHypothesis);
31 using SMESH::TPythonDump;
34 * \brief Container of commands into which the initial script is split.
35 * It also contains data coresponding to SMESH_Gen contents
37 static Handle(_pyGen) theGen;
39 static TCollection_AsciiString theEmptyString;
41 //#define DUMP_CONVERSION
43 #if !defined(_DEBUG_) && defined(DUMP_CONVERSION)
44 #undef DUMP_CONVERSION
47 //================================================================================
49 * \brief Convert python script using commands of smesh.py
50 * \param theScript - Input script
51 * \retval TCollection_AsciiString - Convertion result
53 //================================================================================
55 TCollection_AsciiString
56 SMESH_2smeshpy::ConvertScript(const TCollection_AsciiString& theScript,
57 Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod)
59 theGen = new _pyGen( theEntry2AccessorMethod );
61 // split theScript into separate commands
62 int from = 1, end = theScript.Length(), to;
63 while ( from < end && ( to = theScript.Location( "\n", from, end )))
66 // cut out and store a command
67 theGen->AddCommand( theScript.SubString( from, to - 1 ));
72 #ifdef DUMP_CONVERSION
73 cout << endl << " ######## RESULT ######## " << endl<< endl;
75 // concat commands back into a script
76 TCollection_AsciiString aScript;
77 list< Handle(_pyCommand) >::iterator cmd = theGen->GetCommands().begin();
78 for ( ; cmd != theGen->GetCommands().end(); ++cmd ) {
79 #ifdef DUMP_CONVERSION
80 cout << "## COM " << (*cmd)->GetOrderNb() << ": "<< (*cmd)->GetString() << endl;
82 if ( !(*cmd)->IsEmpty() ) {
84 aScript += (*cmd)->GetString();
94 //================================================================================
96 * \brief _pyGen constructor
98 //================================================================================
100 _pyGen::_pyGen(Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod)
101 : _pyObject( new _pyCommand( TPythonDump::SMESHGenName(), 0 )),
102 myID2AccessorMethod( theEntry2AccessorMethod )
105 // make that GetID() to return TPythonDump::SMESHGenName()
106 GetCreationCmd()->GetString() += "=";
109 //================================================================================
111 * \brief Convert a command using a specific converter
112 * \param theCommand - the command to convert
113 * \retval bool - convertion result
115 //================================================================================
117 void _pyGen::AddCommand( const TCollection_AsciiString& theCommand)
119 // store theCommand in the sequence
120 myCommands.push_back( new _pyCommand( theCommand, ++myNbCommands ));
122 Handle(_pyCommand) aCommand = myCommands.back();
123 #ifdef DUMP_CONVERSION
124 cout << "## COM " << myNbCommands << ": "<< aCommand->GetString() << endl;
127 _pyID objID = aCommand->GetObject();
129 if ( objID.IsEmpty() )
133 if ( objID == this->GetID() ) {
134 this->Process( aCommand );
137 // SMESH_Mesh method?
138 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( objID );
139 if ( id_mesh != myMeshes.end() ) {
140 id_mesh->second->Process( aCommand );
143 // SMESH_Hypothesis method
144 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
145 for ( ; hyp != myHypos.end(); ++hyp )
146 if ( !(*hyp)->IsAlgo() && objID == (*hyp)->GetID() )
147 (*hyp)->Process( aCommand );
149 // Mesh provides SMESH_IDSource interface used in SMESH_MeshEditor.
150 // Add access to wrapped mesh
151 if ( objID == TPythonDump::MeshEditorName() ) {
152 // in all SMESH_MeshEditor's commands, a SMESH_IDSource is the first arg
153 id_mesh = myMeshes.find( aCommand->GetArg( 1 ));
154 if ( id_mesh != myMeshes.end() )
155 aCommand->SetArg( 1 , aCommand->GetArg( 1 ) + ".GetMesh()" );
159 //================================================================================
161 * \brief Convert the command or remember it for later conversion
162 * \param theCommand - The python command calling a method of SMESH_Gen
164 //================================================================================
166 void _pyGen::Process( const Handle(_pyCommand)& theCommand )
168 // there are methods to convert:
169 // CreateMesh( shape )
170 // CreateHypothesis( theHypType, theLibName )
171 // Compute( mesh, geom )
173 if ( theCommand->GetMethod() == "CreateMesh" )
175 Handle(_pyMesh) mesh = new _pyMesh( theCommand );
176 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
180 // CreateHypothesis()
181 if ( theCommand->GetMethod() == "CreateHypothesis" )
183 myHypos.push_back( _pyHypothesis::NewHypothesis( theCommand ));
187 // smeshgen.Compute( mesh, geom ) --> mesh.Compute()
188 if ( theCommand->GetMethod() == "Compute" )
190 const _pyID& meshID = theCommand->GetArg( 1 );
191 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( meshID );
192 if ( id_mesh != myMeshes.end() ) {
193 theCommand->SetObject( meshID );
194 theCommand->RemoveArgs();
195 id_mesh->second->Flush();
200 // smeshgen.Method() --> smesh.smesh.Method()
201 theCommand->SetObject( SMESH_2smeshpy::GenName() );
204 //================================================================================
206 * \brief Convert the remembered commands
208 //================================================================================
212 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.begin();
213 for ( ; id_mesh != myMeshes.end(); ++id_mesh )
214 if ( ! id_mesh->second.IsNull() )
215 id_mesh->second->Flush();
217 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
218 for ( ; hyp != myHypos.end(); ++hyp )
219 if ( !hyp->IsNull() ) {
221 // smeshgen.CreateHypothesis() --> smesh.smesh.CreateHypothesis()
222 if ( !(*hyp)->GetCreationCmd()->IsEmpty() )
223 (*hyp)->GetCreationCmd()->SetObject( SMESH_2smeshpy::GenName() );
227 //================================================================================
229 * \brief Find hypothesis by ID (entry)
230 * \param theHypID - The hypothesis ID
231 * \retval Handle(_pyHypothesis) - The found hypothesis
233 //================================================================================
235 Handle(_pyHypothesis) _pyGen::FindHyp( const _pyID& theHypID )
237 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
238 for ( ; hyp != myHypos.end(); ++hyp )
239 if ( !hyp->IsNull() && theHypID == (*hyp)->GetID() )
241 return Handle(_pyHypothesis)();
244 //================================================================================
246 * \brief Find algorithm the created algorithm
247 * \param theGeom - The shape ID the algorithm was created on
248 * \param theMesh - The mesh ID that created the algorithm
249 * \param dim - The algo dimension
250 * \retval Handle(_pyHypothesis) - The found algo
252 //================================================================================
254 Handle(_pyHypothesis) _pyGen::FindAlgo( const _pyID& theGeom, const _pyID& theMesh,
255 const TCollection_AsciiString& theAlgoType )
257 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
258 for ( ; hyp != myHypos.end(); ++hyp )
259 if ( !hyp->IsNull() &&
261 (*hyp)->GetType() == theAlgoType &&
262 (*hyp)->GetGeom() == theGeom &&
263 (*hyp)->GetMesh() == theMesh )
268 //================================================================================
270 * \brief Change order of commands in the script
271 * \param theCmd1 - One command
272 * \param theCmd2 - Another command
274 //================================================================================
276 void _pyGen::ExchangeCommands( Handle(_pyCommand) theCmd1, Handle(_pyCommand) theCmd2 )
278 list< Handle(_pyCommand) >::iterator pos1, pos2;
279 pos1 = find( myCommands.begin(), myCommands.end(), theCmd1 );
280 pos2 = find( myCommands.begin(), myCommands.end(), theCmd2 );
281 myCommands.insert( pos1, theCmd2 );
282 myCommands.insert( pos2, theCmd1 );
283 myCommands.erase( pos1 );
284 myCommands.erase( pos2 );
286 int nb1 = theCmd1->GetOrderNb();
287 theCmd1->SetOrderNb( theCmd2->GetOrderNb() );
288 theCmd2->SetOrderNb( nb1 );
290 //================================================================================
292 * \brief Set one command after the other
293 * \param theCmd - Command to move
294 * \param theAfterCmd - Command ater which to insert the first one
296 //================================================================================
298 void _pyGen::SetCommandAfter( Handle(_pyCommand) theCmd, Handle(_pyCommand) theAfterCmd )
300 list< Handle(_pyCommand) >::iterator pos;
301 pos = find( myCommands.begin(), myCommands.end(), theCmd );
302 myCommands.erase( pos );
303 pos = find( myCommands.begin(), myCommands.end(), theAfterCmd );
304 myCommands.insert( ++pos, theCmd );
307 for ( pos = myCommands.begin(); pos != myCommands.end(); ++pos)
308 (*pos)->SetOrderNb( i++ );
311 //================================================================================
313 * \brief Set method to access to object wrapped with python class
314 * \param theID - The wrapped object entry
315 * \param theMethod - The accessor method
317 //================================================================================
319 void _pyGen::SetAccessorMethod(const _pyID& theID, const char* theMethod )
321 myID2AccessorMethod.Bind( theID, (char*) theMethod );
324 //================================================================================
326 * \brief Find out type of geom group
327 * \param grpID - The geom group entry
328 * \retval int - The type
330 //================================================================================
332 static bool sameGroupType( const _pyID& grpID,
333 const TCollection_AsciiString& theType)
335 // define group type as smesh.Mesh.Group() does
337 SALOMEDS::Study_var study = SMESH_Gen_i::GetSMESHGen()->GetCurrentStudy();
338 SALOMEDS::SObject_var aSObj = study->FindObjectID( grpID.ToCString() );
339 if ( !aSObj->_is_nil() ) {
340 GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow( aSObj->GetObject() );
341 if ( !aGeomObj->_is_nil() ) {
342 switch ( aGeomObj->GetShapeType() ) {
343 case GEOM::VERTEX: type = SMESH::NODE; break;
344 case GEOM::EDGE: type = SMESH::EDGE; break;
345 case GEOM::FACE: type = SMESH::FACE; break;
347 case GEOM::SHELL: type = SMESH::VOLUME; break;
348 case GEOM::COMPOUND: {
349 GEOM::GEOM_Gen_var aGeomGen = SMESH_Gen_i::GetSMESHGen()->GetGeomEngine();
350 if ( !aGeomGen->_is_nil() ) {
351 GEOM::GEOM_IGroupOperations_var aGrpOp =
352 aGeomGen->GetIGroupOperations( study->StudyId() );
353 if ( !aGrpOp->_is_nil() ) {
354 switch ( aGrpOp->GetType( aGeomObj )) {
355 case TopAbs_VERTEX: type = SMESH::NODE; break;
356 case TopAbs_EDGE: type = SMESH::EDGE; break;
357 case TopAbs_FACE: type = SMESH::FACE; break;
358 case TopAbs_SOLID: type = SMESH::VOLUME; break;
369 MESSAGE("Type of the group " << grpID << " not found");
372 if ( theType.IsIntegerValue() )
373 return type == theType.IntegerValue();
376 case SMESH::NODE: return theType.Location( "NODE", 1, theType.Length() );
377 case SMESH::EDGE: return theType.Location( "EDGE", 1, theType.Length() );
378 case SMESH::FACE: return theType.Location( "FACE", 1, theType.Length() );
379 case SMESH::VOLUME: return theType.Location( "VOLUME", 1, theType.Length() );
385 //================================================================================
388 * \param theCreationCmd -
390 //================================================================================
392 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd): _pyObject(theCreationCmd)
394 // convert my creation command
395 Handle(_pyCommand) creationCmd = GetCreationCmd();
396 creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
397 creationCmd->SetMethod( "Mesh" );
399 theGen->SetAccessorMethod( GetID(), "GetMesh()" );
402 //================================================================================
406 * \param theCommand - Engine method called for this mesh
408 //================================================================================
410 void _pyMesh::Process( const Handle(_pyCommand)& theCommand )
412 // smesh.py wraps the following methods:
414 // 1. GetSubMesh(geom, name) + AddHypothesis(geom, algo)
415 // --> in Mesh_Algorithm.Create(mesh, geom, hypo, so)
416 // 2. AddHypothesis(geom, hyp)
417 // --> in Mesh_Algorithm.Hypothesis(hyp, args, so)
418 // 3. CreateGroupFromGEOM(type, name, grp)
419 // --> in Mesh.Group(grp, name="")
420 // 4. ExportToMED(f, opt, version)
421 // --> in Mesh.ExportToMED( f, version, opt=0 )
422 // 5. ExportMED(f, opt)
423 // --> in Mesh.ExportMED( f,opt=0 )
425 // --> in Mesh.ExportDAT( f )
427 // --> in Mesh.ExportUNV(f)
428 // 8. ExportSTL(f, ascii)
429 // --> in Mesh.ExportSTL(f, ascii=1)
431 const TCollection_AsciiString method = theCommand->GetMethod();
432 if ( method == "GetSubMesh" ) {
433 mySubmeshes.push_back( theCommand );
435 else if ( method == "AddHypothesis" ) { // mesh.AddHypothesis(geom, HYPO )
436 myAddHypCmds.push_back( theCommand );
438 const _pyID& hypID = theCommand->GetArg( 2 );
439 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
440 if ( !hyp.IsNull() && hyp->GetMesh().IsEmpty() )
441 hyp->SetMesh( this->GetID() );
443 else if ( method == "CreateGroupFromGEOM" ) {// (type, name, grp)
444 _pyID grp = theCommand->GetArg( 3 );
445 if ( sameGroupType( grp, theCommand->GetArg( 1 )) ) { // --> Group(grp)
446 theCommand->SetMethod( "Group" );
447 theCommand->RemoveArgs();
448 theCommand->SetArg( 1, grp );
451 AddMeshAccess( theCommand );
454 else if ( method == "ExportToMED" ) {//(f, opt, version)
455 // --> (f, version, opt)
456 _pyID opt = theCommand->GetArg( 2 );
457 _pyID ver = theCommand->GetArg( 3 );
458 theCommand->SetArg( 2, ver );
459 theCommand->SetArg( 3, opt );
461 else if ( method == "RemoveHypothesis" ) // (geom, hyp)
463 const _pyID & hypID = theCommand->GetArg( 2 );
465 // check if this mesh still has corresponding addition command
466 bool hasAddCmd = false;
467 list< Handle(_pyCommand) >::iterator cmd = myAddHypCmds.begin();
468 while ( cmd != myAddHypCmds.end() )
470 // AddHypothesis(geom, hyp)
471 if ( hypID == (*cmd)->GetArg( 2 )) { // erase both commands
474 cmd = myAddHypCmds.erase( cmd );
482 // access to wrapped mesh
483 AddMeshAccess( theCommand );
484 // access to wrapped algo
485 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
486 if ( !hyp.IsNull() && hyp->IsAlgo() && hyp->IsWrapped() )
487 theCommand->SetArg( 2, theCommand->GetArg( 2 ) + ".GetAlgorithm()" );
490 else { // apply theCommand to the mesh wrapped by smeshpy mesh
491 AddMeshAccess( theCommand );
495 //================================================================================
497 * \brief Convert creation and addition of all algos and hypos
499 //================================================================================
501 void _pyMesh::Flush()
503 list < Handle(_pyCommand) >::iterator cmd, cmd2;
504 map< _pyID, Handle(_pyCommand) > algo2additionCmd;
506 // try to convert algo addition like this:
507 // mesh.AddHypothesis(geom, ALGO ) --> ALGO = mesh.Algo()
508 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
510 Handle(_pyCommand) addCmd = *cmd;
511 const _pyID& algoID = addCmd->GetArg( 2 );
512 Handle(_pyHypothesis) algo = theGen->FindHyp( algoID );
513 if ( algo.IsNull() || !algo->IsAlgo() )
516 _pyID geom = addCmd->GetArg( 1 );
517 if ( algo->Addition2Creation( addCmd, this->GetID() )) // OK
519 algo2additionCmd[ algo->GetID() ] = addCmd;
521 if ( geom != GetGeom() ) // local algo
523 // mesh.AddHypothesis(geom, ALGO ) --> mesh.AlgoMethod(geom)
524 addCmd->SetArg( addCmd->GetNbArgs() + 1,
525 TCollection_AsciiString( "geom=" ) + geom );
526 // sm = mesh.GetSubMesh(geom, name) --> sm = ALGO.GetSubMesh()
527 for ( cmd2 = mySubmeshes.begin(); cmd2 != mySubmeshes.end(); ++cmd2 ) {
528 Handle(_pyCommand) subCmd = *cmd2;
529 if ( geom == subCmd->GetArg( 1 )) {
530 subCmd->SetObject( algo->GetID() );
531 subCmd->RemoveArgs();
532 if ( addCmd->GetOrderNb() > subCmd->GetOrderNb() )
533 theGen->SetCommandAfter( subCmd, addCmd );
538 else // ALGO was already created
540 // mesh.AddHypothesis(geom, ALGO ) --> mesh.GetMesh().AddHypothesis(geom, ALGO )
541 AddMeshAccess( addCmd );
542 // mesh.GetMesh().AddHypothesis(geom, ALGO ) ->
543 // mesh.GetMesh().AddHypothesis(geom, ALGO.GetAlgorithm() )
544 addCmd->SetArg( 2, addCmd->GetArg( 2 ) + ".GetAlgorithm()" );
548 // try to convert hypo addition like this:
549 // mesh.AddHypothesis(geom, HYPO ) --> HYPO = algo.Hypo()
550 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
552 Handle(_pyCommand) addCmd = *cmd;
553 const _pyID& hypID = addCmd->GetArg( 2 );
554 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
555 if ( hyp.IsNull() || hyp->IsAlgo() )
557 const _pyID& geom = addCmd->GetArg( 1 );
558 // find algo created on <geom> for this mesh
559 Handle(_pyHypothesis) algo = theGen->FindAlgo( geom, this->GetID(), hyp->GetType() );
560 //_pyID algoID = algo.IsNull() ? "" : algo->GetID();
561 if ( !algo.IsNull() && hyp->Addition2Creation( addCmd, this->GetID() )) // OK
563 addCmd->SetObject( algo->GetID() );
564 Handle(_pyCommand) algoCmd = algo2additionCmd[ algo->GetID() ];
565 if ( !algoCmd.IsNull() && algoCmd->GetOrderNb() > addCmd->GetOrderNb() )
566 // algo was created later than hyp
567 theGen->ExchangeCommands( algoCmd, addCmd );
571 AddMeshAccess( addCmd );
575 // sm = mesh.GetSubMesh(geom, name) --> sm = mesh.GetMesh().GetSubMesh(geom, name)
576 for ( cmd = mySubmeshes.begin(); cmd != mySubmeshes.end(); ++cmd ) {
577 Handle(_pyCommand) subCmd = *cmd;
578 if ( subCmd->GetNbArgs() > 0 )
579 AddMeshAccess( subCmd );
581 myAddHypCmds.clear();
585 //================================================================================
587 * \brief _pyHypothesis constructor
588 * \param theCreationCmd -
590 //================================================================================
592 _pyHypothesis::_pyHypothesis(const Handle(_pyCommand)& theCreationCmd):
593 _pyObject( theCreationCmd )
595 myDim = myIsAlgo = /*myIsLocal = */myIsWrapped = myIsConverted = false;
598 //================================================================================
600 * \brief Creates algorithm or hypothesis
601 * \param theCreationCmd - The engine command creating a hypothesis
602 * \retval Handle(_pyHypothesis) - Result _pyHypothesis
604 //================================================================================
606 Handle(_pyHypothesis) _pyHypothesis::NewHypothesis( const Handle(_pyCommand)& theCreationCmd)
608 // theCreationCmd: CreateHypothesis( "theHypType", "theLibName" )
609 ASSERT (( theCreationCmd->GetMethod() == "CreateHypothesis"));
611 Handle(_pyHypothesis) hyp, algo;
614 const TCollection_AsciiString & hypTypeWithQuotes = theCreationCmd->GetArg( 1 );
615 if ( hypTypeWithQuotes.IsEmpty() )
618 TCollection_AsciiString hypType =
619 hypTypeWithQuotes.SubString( 2, hypTypeWithQuotes.Length() - 1 );
621 algo = new _pyAlgorithm( theCreationCmd );
622 hyp = new _pyHypothesis( theCreationCmd );
624 // 1D Regular_1D ----------
625 if ( hypType == "Regular_1D" ) {
627 algo->myCreationMethod = "Segment";
629 else if ( hypType == "LocalLength" ) {
631 hyp->myCreationMethod = "LocalLength";
632 hyp->myType = "Regular_1D";
633 hyp->myArgMethods.Append( "SetLength" );
635 else if ( hypType == "NumberOfSegments" ) {
636 hyp = new _pyNumberOfSegmentsHyp( theCreationCmd );
638 hyp->myCreationMethod = "NumberOfSegments";
639 hyp->myType = "Regular_1D";
640 hyp->myArgMethods.Append( "SetNumberOfSegments" );
641 hyp->myArgMethods.Append( "SetScaleFactor" );
643 else if ( hypType == "Arithmetic1D" ) {
644 hyp = new _pyComplexParamHypo( theCreationCmd );
646 hyp->myCreationMethod = "Arithmetic1D";
647 hyp->myType = "Regular_1D";
649 else if ( hypType == "StartEndLength" ) {
650 hyp = new _pyComplexParamHypo( theCreationCmd );
652 hyp->myCreationMethod = "StartEndLength";
653 hyp->myType = "Regular_1D";
655 else if ( hypType == "Deflection1D" ) {
657 hyp->myCreationMethod = "Deflection1D";
658 hyp->myArgMethods.Append( "SetDeflection" );
659 hyp->myType = "Regular_1D";
661 else if ( hypType == "Propagation" ) {
663 hyp->myCreationMethod = "Propagation";
664 hyp->myType = "Regular_1D";
666 else if ( hypType == "AutomaticLength" ) {
668 hyp->myCreationMethod = "AutomaticLength";
669 hyp->myType = "Regular_1D";
670 hyp->myArgMethods.Append( "SetFineness");
672 // 1D Python_1D ----------
673 else if ( hypType == "Python_1D" ) {
675 algo->myCreationMethod = "Segment";
676 algo->myArgs.Append( "algo=smesh.PYTHON");
678 else if ( hypType == "PythonSplit1D" ) {
680 hyp->myCreationMethod = "PythonSplit1D";
681 hyp->myType = "Python_1D";
682 hyp->myArgMethods.Append( "SetNumberOfSegments");
683 hyp->myArgMethods.Append( "SetPythonLog10RatioFunction");
686 else if ( hypType == "MEFISTO_2D" ) {
688 algo->myCreationMethod = "Triangle";
690 else if ( hypType == "MaxElementArea" ) {
692 hyp->myCreationMethod = "MaxElementArea";
693 hyp->myType = "MEFISTO_2D";
694 hyp->myArgMethods.Append( "SetMaxElementArea");
696 else if ( hypType == "LengthFromEdges" ) {
698 hyp->myCreationMethod = "LengthFromEdges";
699 hyp->myType = "MEFISTO_2D";
701 else if ( hypType == "Quadrangle_2D" ) {
703 algo->myCreationMethod = "Quadrangle";
705 else if ( hypType == "QuadranglePreference" ) {
707 hyp->myCreationMethod = "QuadranglePreference";
708 hyp->myType = "Quadrangle_2D";
711 else if ( hypType == "NETGEN_3D") {
713 algo->myCreationMethod = "Tetrahedron";
714 algo->myArgs.Append( "algo=smesh.NETGEN" );
716 else if ( hypType == "MaxElementVolume") {
718 hyp->myCreationMethod = "MaxElementVolume";
719 hyp->myType = "NETGEN_3D";
720 hyp->myArgMethods.Append( "SetMaxElementVolume" );
722 else if ( hypType == "GHS3D_3D" ) {
724 algo->myCreationMethod = "Tetrahedron";
725 algo->myArgs.Append( "algo=smesh.GHS3D" );
727 else if ( hypType == "Hexa_3D" ) {
729 algo->myCreationMethod = "Hexahedron";
732 if ( algo->GetDim() ) {
733 algo->myType = hypType;
739 //================================================================================
741 * \brief Convert the command adding a hypothesis to mesh into a smesh command
742 * \param theCmd - The command like mesh.AddHypothesis( geom, hypo )
743 * \param theAlgo - The algo that can create this hypo
744 * \retval bool - false if the command cant be converted
746 //================================================================================
748 bool _pyHypothesis::Addition2Creation( const Handle(_pyCommand)& theCmd,
749 const _pyID& theMesh)
751 ASSERT(( theCmd->GetMethod() == "AddHypothesis" ));
753 if ( !IsWrappable( theMesh ))
760 // mesh.AddHypothesis(geom,hyp) --> hyp = theMesh.myCreationMethod(args)
761 theCmd->SetResultValue( GetID() );
762 theCmd->SetObject( theMesh );
763 theCmd->SetMethod( myCreationMethod );
765 theCmd->RemoveArgs();
766 for ( int i = 1; i <= myArgs.Length(); ++i ) {
767 if ( !myArgs( i ).IsEmpty() )
768 theCmd->SetArg( i, myArgs( i ));
770 theCmd->SetArg( i, "[]");
773 // clear commands setting arg values
774 list < Handle(_pyCommand) >::iterator argCmd = myArgCommands.begin();
775 for ( ; argCmd != myArgCommands.end(); ++argCmd )
780 // // set arg commands after hypo creation
781 // list<Handle(_pyCommand)>::iterator argCmd = myArgCommands.begin();
782 // for ( ; argCmd != myArgCommands.end(); ++argCmd )
783 // if ( !(*argCmd)->IsEmpty() && GetCommandNb() > (*argCmd)->GetOrderNb() )
784 // theGen->ExchangeCommands( GetCreationCmd(), *argCmd );
787 // set unknown arg commands after hypo creation
788 Handle(_pyCommand) afterCmd = myIsWrapped ? theCmd : GetCreationCmd();
789 list<Handle(_pyCommand)>::iterator cmd = myUnknownCommands.begin();
790 for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
791 if ( !(*cmd)->IsEmpty() && afterCmd->GetOrderNb() > (*cmd)->GetOrderNb() ) {
792 theGen->SetCommandAfter( *cmd, afterCmd );
796 myArgCommands.clear();
797 myUnknownCommands.clear();
802 //================================================================================
804 * \brief Remember hypothesis parameter values
805 * \param theCommand - The called hypothesis method
807 //================================================================================
809 void _pyHypothesis::Process( const Handle(_pyCommand)& theCommand)
813 for ( int i = 1; i <= myArgMethods.Length(); ++i ) {
814 if ( myArgMethods( i ) == theCommand->GetMethod() ) {
815 while ( myArgs.Length() < i )
816 myArgs.Append( "[]" );
817 myArgs( i ) = theCommand->GetArg( 1 ); // arg value
818 myArgCommands.push_back( theCommand );
822 myUnknownCommands.push_back( theCommand );
825 //================================================================================
827 * \brief Finish conversion
829 //================================================================================
831 void _pyHypothesis::Flush()
834 GetCreationCmd()->Clear();
837 //================================================================================
839 * \brief Remember hypothesis parameter values
840 * \param theCommand - The called hypothesis method
842 //================================================================================
844 void _pyComplexParamHypo::Process( const Handle(_pyCommand)& theCommand)
846 // ex: hyp.SetLength(start, 1)
847 // hyp.SetLength(end, 0)
848 ASSERT(( theCommand->GetMethod() == "SetLength" ));
849 ASSERT(( theCommand->GetArg( 2 ).IsIntegerValue() ));
850 int i = 2 - theCommand->GetArg( 2 ).IntegerValue();
851 while ( myArgs.Length() < i )
852 myArgs.Append( "[]" );
853 myArgs( i ) = theCommand->GetArg( 1 ); // arg value
854 myArgCommands.push_back( theCommand );
857 //================================================================================
859 * \brief additionally to Addition2Creation, clears SetDistrType() command
860 * \param theCmd - AddHypothesis() command
861 * \param theMesh - mesh to which a hypothesis is added
862 * \retval bool - convertion result
864 //================================================================================
866 bool _pyNumberOfSegmentsHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
867 const _pyID& theMesh)
869 if ( IsWrappable( theMesh ) && myArgs.Length() > 1 ) {
870 list<Handle(_pyCommand)>::iterator cmd = myUnknownCommands.begin();
871 for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
872 // clear SetDistrType()
873 if ( (*cmd)->GetString().Location( "SetDistrType", 1, (*cmd)->Length() ))
877 return _pyHypothesis::Addition2Creation( theCmd, theMesh );
880 //================================================================================
882 * \brief _pyAlgorithm constructor
883 * \param theCreationCmd - The command like "algo = smeshgen.CreateHypothesis(type,lib)"
885 //================================================================================
887 _pyAlgorithm::_pyAlgorithm(const Handle(_pyCommand)& theCreationCmd)
888 : _pyHypothesis( theCreationCmd )
893 //================================================================================
895 * \brief Convert the command adding an algorithm to mesh
896 * \param theCmd - The command like mesh.AddHypothesis( geom, algo )
897 * \param theMesh - The mesh needing this algo
898 * \retval bool - false if the command cant be converted
900 //================================================================================
902 bool _pyAlgorithm::Addition2Creation( const Handle(_pyCommand)& theCmd,
903 const _pyID& theMeshID)
905 if ( IsWrappable( theMeshID )) {
907 myGeom = theCmd->GetArg( 1 );
909 // mesh.AddHypothesis(geom,algo) --> theMeshID.myCreationMethod()
910 if ( _pyHypothesis::Addition2Creation( theCmd, theMeshID )) {
911 theGen->SetAccessorMethod( GetID(), "GetAlgorithm()" );
918 //================================================================================
920 * \brief Return starting position of a part of python command
921 * \param thePartIndex - The index of command part
922 * \retval int - Part position
924 //================================================================================
926 int _pyCommand::GetBegPos( int thePartIndex )
930 if ( myBegPos.Length() < thePartIndex )
932 return myBegPos( thePartIndex );
935 //================================================================================
937 * \brief Store starting position of a part of python command
938 * \param thePartIndex - The index of command part
939 * \param thePosition - Part position
941 //================================================================================
943 void _pyCommand::SetBegPos( int thePartIndex, int thePosition )
945 while ( myBegPos.Length() < thePartIndex )
946 myBegPos.Append( UNKNOWN );
947 myBegPos( thePartIndex ) = thePosition;
950 //================================================================================
952 * \brief Return substring of python command looking like ResultValue = Obj.Meth()
953 * \retval const TCollection_AsciiString & - ResultValue substring
955 //================================================================================
957 const TCollection_AsciiString & _pyCommand::GetResultValue()
959 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
961 int begPos = myString.Location( "=", 1, Length() );
963 myRes = GetWord( myString, begPos, false );
966 SetBegPos( RESULT_IND, begPos );
971 //================================================================================
973 * \brief Return substring of python command looking like ResVal = Object.Meth()
974 * \retval const TCollection_AsciiString & - Object substring
976 //================================================================================
978 const TCollection_AsciiString & _pyCommand::GetObject()
980 if ( GetBegPos( OBJECT_IND ) == UNKNOWN )
983 int begPos = GetBegPos( RESULT_IND ) + myRes.Length();
985 begPos = myString.Location( "=", 1, Length() ) + 1;
987 myObj = GetWord( myString, begPos, true );
988 SetBegPos( OBJECT_IND, begPos );
994 //================================================================================
996 * \brief Return substring of python command looking like ResVal = Obj.Method()
997 * \retval const TCollection_AsciiString & - Method substring
999 //================================================================================
1001 const TCollection_AsciiString & _pyCommand::GetMethod()
1003 if ( GetBegPos( METHOD_IND ) == UNKNOWN )
1006 int begPos = GetBegPos( OBJECT_IND ) + myObj.Length();
1007 bool forward = true;
1009 begPos = myString.Location( "(", 1, Length() ) - 1;
1013 myMeth = GetWord( myString, begPos, forward );
1014 SetBegPos( METHOD_IND, begPos );
1020 //================================================================================
1022 * \brief Return substring of python command looking like ResVal = Obj.Meth(Arg1,...)
1023 * \retval const TCollection_AsciiString & - Arg<index> substring
1025 //================================================================================
1027 const TCollection_AsciiString & _pyCommand::GetArg( int index )
1029 if ( GetBegPos( ARG1_IND ) == UNKNOWN )
1032 int begPos = GetBegPos( METHOD_IND ) + myMeth.Length();
1034 begPos = myString.Location( "(", 1, Length() ) + 1;
1036 int i = 0, prevLen = 0;
1037 while ( begPos != EMPTY ) {
1039 // check if we are looking at the closing parenthesis
1040 while ( begPos <= Length() && isspace( myString.Value( begPos )))
1042 if ( begPos > Length() || myString.Value( begPos ) == ')' )
1044 myArgs.Append( GetWord( myString, begPos, true, true ));
1045 SetBegPos( ARG1_IND + i, begPos );
1046 prevLen = myArgs.Last().Length();
1048 myArgs.Remove( myArgs.Length() ); // no more args
1052 if ( myArgs.Length() < index )
1053 return theEmptyString;
1054 return myArgs( index );
1057 //================================================================================
1059 * \brief Check if char is a word part
1060 * \param c - The character to check
1061 * \retval bool - The check result
1063 //================================================================================
1065 static inline bool isWord(const char c, const bool dotIsWord)
1068 !isspace(c) && c != ',' && c != '=' && c != ')' && c != '(' && ( dotIsWord || c != '.');
1071 //================================================================================
1073 * \brief Looks for a word in the string and returns word's beginning
1074 * \param theString - The input string
1075 * \param theStartPos - The position to start the search, returning word's beginning
1076 * \param theForward - The search direction
1077 * \retval TCollection_AsciiString - The found word
1079 //================================================================================
1081 TCollection_AsciiString _pyCommand::GetWord( const TCollection_AsciiString & theString,
1083 const bool theForward,
1084 const bool dotIsWord )
1086 int beg = theStartPos, end = theStartPos;
1087 theStartPos = EMPTY;
1088 if ( beg < 1 || beg > theString.Length() )
1089 return theEmptyString;
1091 if ( theForward ) { // search forward
1093 while ( beg <= theString.Length() && !isWord( theString.Value( beg ), dotIsWord))
1095 if ( beg > theString.Length() )
1096 return theEmptyString; // no word found
1099 while ( end <= theString.Length() && isWord( theString.Value( end ), dotIsWord))
1103 else { // search backward
1105 while ( end > 0 && !isWord( theString.Value( end ), dotIsWord))
1108 return theEmptyString; // no word found
1110 while ( beg > 0 && isWord( theString.Value( beg ), dotIsWord))
1115 //cout << theString << " ---- " << beg << " - " << end << endl;
1116 return theString.SubString( beg, end );
1119 //================================================================================
1121 * \brief Look for position where not space char is
1122 * \param theString - The string
1123 * \param thePos - The position to search from and which returns result
1124 * \retval bool - false if there are only space after thePos in theString
1128 //================================================================================
1130 bool _pyCommand::SkipSpaces( const TCollection_AsciiString & theString, int & thePos )
1132 if ( thePos < 1 || thePos > theString.Length() )
1135 while ( thePos <= theString.Length() && isspace( theString.Value( thePos )))
1138 return thePos <= theString.Length();
1141 //================================================================================
1143 * \brief Modify a part of the command
1144 * \param thePartIndex - The index of the part
1145 * \param thePart - The new part string
1146 * \param theOldPart - The old part
1148 //================================================================================
1150 void _pyCommand::SetPart(int thePartIndex, const TCollection_AsciiString& thePart,
1151 TCollection_AsciiString& theOldPart)
1153 int pos = GetBegPos( thePartIndex );
1154 if ( pos <= Length() && theOldPart != thePart)
1156 TCollection_AsciiString seperator;
1158 pos = GetBegPos( thePartIndex + 1 );
1159 if ( pos < 1 ) return;
1160 switch ( thePartIndex ) {
1161 case RESULT_IND: seperator = " = "; break;
1162 case OBJECT_IND: seperator = "."; break;
1163 case METHOD_IND: seperator = "()"; break;
1167 myString.Remove( pos, theOldPart.Length() );
1168 if ( !seperator.IsEmpty() )
1169 myString.Insert( pos , seperator );
1170 myString.Insert( pos, thePart );
1171 // update starting positions of the following parts
1172 int posDelta = thePart.Length() + seperator.Length() - theOldPart.Length();
1173 for ( int i = thePartIndex + 1; i <= myBegPos.Length(); ++i ) {
1174 if ( myBegPos( i ) > 0 )
1175 myBegPos( i ) += posDelta;
1177 theOldPart = thePart;
1181 //================================================================================
1183 * \brief Set agrument
1184 * \param index - The argument index, it counts from 1
1185 * \param theArg - The argument string
1187 //================================================================================
1189 void _pyCommand::SetArg( int index, const TCollection_AsciiString& theArg)
1192 int argInd = ARG1_IND + index - 1;
1193 int pos = GetBegPos( argInd );
1194 if ( pos < 1 ) // no index-th arg exist, append inexistent args
1196 // find a closing parenthesis
1198 while ( pos > 0 && myString.Value( pos ) != ')' )
1200 if ( pos == 0 ) { // no parentheses at all
1204 while ( myArgs.Length() < index ) {
1205 if ( myArgs.Length() )
1206 myString.Insert( pos++, "," );
1207 myArgs.Append("None");
1208 myString.Insert( pos, myArgs.Last() );
1209 SetBegPos( ARG1_IND + myArgs.Length() - 1, pos );
1210 pos += myArgs.Last().Length();
1213 SetPart( argInd, theArg, myArgs( index ));
1216 //================================================================================
1218 * \brief Empty arg list
1220 //================================================================================
1222 void _pyCommand::RemoveArgs()
1224 if ( int pos = myString.Location( '(', 1, Length() ))
1225 myString.Trunc( pos );
1228 if ( myBegPos.Length() >= ARG1_IND )
1229 myBegPos.Remove( ARG1_IND, myBegPos.Length() );