1 // File : SMESH_2smeshpy.cxx
2 // Created : Fri Nov 18 13:20:10 2005
3 // Author : Edward AGAPOV (eap)
5 #include "SMESH_2smeshpy.hxx"
8 #include "SMESH_PythonDump.hxx"
9 #include "Resource_DataMapOfAsciiStringAsciiString.hxx"
11 #include "SMESH_Gen_i.hxx"
12 /* SALOME headers that include CORBA headers that include windows.h
13 * that defines GetObject symbol as GetObjectA should stand before SALOME headers
14 * that declare methods named GetObject - to apply the same rules of GetObject renaming
15 * and thus to avoid mess with GetObject symbol on Windows */
17 IMPLEMENT_STANDARD_HANDLE (_pyObject ,Standard_Transient);
18 IMPLEMENT_STANDARD_HANDLE (_pyCommand ,Standard_Transient);
19 IMPLEMENT_STANDARD_HANDLE (_pyGen ,_pyObject);
20 IMPLEMENT_STANDARD_HANDLE (_pyMesh ,_pyObject);
21 IMPLEMENT_STANDARD_HANDLE (_pyHypothesis ,_pyObject);
22 IMPLEMENT_STANDARD_HANDLE (_pyAlgorithm ,_pyHypothesis);
23 IMPLEMENT_STANDARD_HANDLE (_pyComplexParamHypo,_pyHypothesis);
24 IMPLEMENT_STANDARD_HANDLE (_pyNumberOfSegmentsHyp,_pyHypothesis);
26 IMPLEMENT_STANDARD_RTTIEXT(_pyObject ,Standard_Transient);
27 IMPLEMENT_STANDARD_RTTIEXT(_pyCommand ,Standard_Transient);
28 IMPLEMENT_STANDARD_RTTIEXT(_pyGen ,_pyObject);
29 IMPLEMENT_STANDARD_RTTIEXT(_pyMesh ,_pyObject);
30 IMPLEMENT_STANDARD_RTTIEXT(_pyHypothesis ,_pyObject);
31 IMPLEMENT_STANDARD_RTTIEXT(_pyAlgorithm ,_pyHypothesis);
32 IMPLEMENT_STANDARD_RTTIEXT(_pyComplexParamHypo,_pyHypothesis);
33 IMPLEMENT_STANDARD_RTTIEXT(_pyNumberOfSegmentsHyp,_pyHypothesis);
36 using SMESH::TPythonDump;
39 * \brief Container of commands into which the initial script is split.
40 * It also contains data coresponding to SMESH_Gen contents
42 static Handle(_pyGen) theGen;
44 static TCollection_AsciiString theEmptyString;
46 //#define DUMP_CONVERSION
48 #if !defined(_DEBUG_) && defined(DUMP_CONVERSION)
49 #undef DUMP_CONVERSION
52 //================================================================================
54 * \brief Convert python script using commands of smesh.py
55 * \param theScript - Input script
56 * \retval TCollection_AsciiString - Convertion result
58 //================================================================================
60 TCollection_AsciiString
61 SMESH_2smeshpy::ConvertScript(const TCollection_AsciiString& theScript,
62 Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod)
64 theGen = new _pyGen( theEntry2AccessorMethod );
66 // split theScript into separate commands
67 int from = 1, end = theScript.Length(), to;
68 while ( from < end && ( to = theScript.Location( "\n", from, end )))
71 // cut out and store a command
72 theGen->AddCommand( theScript.SubString( from, to - 1 ));
77 #ifdef DUMP_CONVERSION
78 cout << endl << " ######## RESULT ######## " << endl<< endl;
80 // concat commands back into a script
81 TCollection_AsciiString aScript;
82 list< Handle(_pyCommand) >::iterator cmd = theGen->GetCommands().begin();
83 for ( ; cmd != theGen->GetCommands().end(); ++cmd ) {
84 #ifdef DUMP_CONVERSION
85 cout << "## COM " << (*cmd)->GetOrderNb() << ": "<< (*cmd)->GetString() << endl;
87 if ( !(*cmd)->IsEmpty() ) {
89 aScript += (*cmd)->GetString();
99 //================================================================================
101 * \brief _pyGen constructor
103 //================================================================================
105 _pyGen::_pyGen(Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod)
106 : _pyObject( new _pyCommand( TPythonDump::SMESHGenName(), 0 )),
107 myID2AccessorMethod( theEntry2AccessorMethod )
110 // make that GetID() to return TPythonDump::SMESHGenName()
111 GetCreationCmd()->GetString() += "=";
114 //================================================================================
116 * \brief Convert a command using a specific converter
117 * \param theCommand - the command to convert
118 * \retval bool - convertion result
120 //================================================================================
122 void _pyGen::AddCommand( const TCollection_AsciiString& theCommand)
124 // store theCommand in the sequence
125 myCommands.push_back( new _pyCommand( theCommand, ++myNbCommands ));
127 Handle(_pyCommand) aCommand = myCommands.back();
128 #ifdef DUMP_CONVERSION
129 cout << "## COM " << myNbCommands << ": "<< aCommand->GetString() << endl;
132 _pyID objID = aCommand->GetObject();
134 if ( objID.IsEmpty() )
138 if ( objID == this->GetID() ) {
139 this->Process( aCommand );
142 // SMESH_Mesh method?
143 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( objID );
144 if ( id_mesh != myMeshes.end() ) {
145 id_mesh->second->Process( aCommand );
148 // SMESH_Hypothesis method
149 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
150 for ( ; hyp != myHypos.end(); ++hyp )
151 if ( !(*hyp)->IsAlgo() && objID == (*hyp)->GetID() )
152 (*hyp)->Process( aCommand );
154 // Mesh provides SMESH_IDSource interface used in SMESH_MeshEditor.
155 // Add access to wrapped mesh
156 if ( objID == TPythonDump::MeshEditorName() ) {
157 // in all SMESH_MeshEditor's commands, a SMESH_IDSource is the first arg
158 id_mesh = myMeshes.find( aCommand->GetArg( 1 ));
159 if ( id_mesh != myMeshes.end() )
160 aCommand->SetArg( 1 , aCommand->GetArg( 1 ) + ".GetMesh()" );
164 //================================================================================
166 * \brief Convert the command or remember it for later conversion
167 * \param theCommand - The python command calling a method of SMESH_Gen
169 //================================================================================
171 void _pyGen::Process( const Handle(_pyCommand)& theCommand )
173 // there are methods to convert:
174 // CreateMesh( shape )
175 // CreateHypothesis( theHypType, theLibName )
176 // Compute( mesh, geom )
178 if ( theCommand->GetMethod() == "CreateMesh" )
180 Handle(_pyMesh) mesh = new _pyMesh( theCommand );
181 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
185 // CreateHypothesis()
186 if ( theCommand->GetMethod() == "CreateHypothesis" )
188 myHypos.push_back( _pyHypothesis::NewHypothesis( theCommand ));
192 // smeshgen.Compute( mesh, geom ) --> mesh.Compute()
193 if ( theCommand->GetMethod() == "Compute" )
195 const _pyID& meshID = theCommand->GetArg( 1 );
196 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( meshID );
197 if ( id_mesh != myMeshes.end() ) {
198 theCommand->SetObject( meshID );
199 theCommand->RemoveArgs();
200 id_mesh->second->Flush();
205 // smeshgen.Method() --> smesh.smesh.Method()
206 theCommand->SetObject( SMESH_2smeshpy::GenName() );
209 //================================================================================
211 * \brief Convert the remembered commands
213 //================================================================================
217 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.begin();
218 for ( ; id_mesh != myMeshes.end(); ++id_mesh )
219 if ( ! id_mesh->second.IsNull() )
220 id_mesh->second->Flush();
222 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
223 for ( ; hyp != myHypos.end(); ++hyp )
224 if ( !hyp->IsNull() ) {
226 // smeshgen.CreateHypothesis() --> smesh.smesh.CreateHypothesis()
227 if ( !(*hyp)->GetCreationCmd()->IsEmpty() )
228 (*hyp)->GetCreationCmd()->SetObject( SMESH_2smeshpy::GenName() );
232 //================================================================================
234 * \brief Find hypothesis by ID (entry)
235 * \param theHypID - The hypothesis ID
236 * \retval Handle(_pyHypothesis) - The found hypothesis
238 //================================================================================
240 Handle(_pyHypothesis) _pyGen::FindHyp( const _pyID& theHypID )
242 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
243 for ( ; hyp != myHypos.end(); ++hyp )
244 if ( !hyp->IsNull() && theHypID == (*hyp)->GetID() )
246 return Handle(_pyHypothesis)();
249 //================================================================================
251 * \brief Find algorithm the created algorithm
252 * \param theGeom - The shape ID the algorithm was created on
253 * \param theMesh - The mesh ID that created the algorithm
254 * \param dim - The algo dimension
255 * \retval Handle(_pyHypothesis) - The found algo
257 //================================================================================
259 Handle(_pyHypothesis) _pyGen::FindAlgo( const _pyID& theGeom, const _pyID& theMesh,
260 const TCollection_AsciiString& theAlgoType )
262 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
263 for ( ; hyp != myHypos.end(); ++hyp )
264 if ( !hyp->IsNull() &&
266 (*hyp)->GetType() == theAlgoType &&
267 (*hyp)->GetGeom() == theGeom &&
268 (*hyp)->GetMesh() == theMesh )
273 //================================================================================
275 * \brief Change order of commands in the script
276 * \param theCmd1 - One command
277 * \param theCmd2 - Another command
279 //================================================================================
281 void _pyGen::ExchangeCommands( Handle(_pyCommand) theCmd1, Handle(_pyCommand) theCmd2 )
283 list< Handle(_pyCommand) >::iterator pos1, pos2;
284 pos1 = find( myCommands.begin(), myCommands.end(), theCmd1 );
285 pos2 = find( myCommands.begin(), myCommands.end(), theCmd2 );
286 myCommands.insert( pos1, theCmd2 );
287 myCommands.insert( pos2, theCmd1 );
288 myCommands.erase( pos1 );
289 myCommands.erase( pos2 );
291 int nb1 = theCmd1->GetOrderNb();
292 theCmd1->SetOrderNb( theCmd2->GetOrderNb() );
293 theCmd2->SetOrderNb( nb1 );
295 //================================================================================
297 * \brief Set one command after the other
298 * \param theCmd - Command to move
299 * \param theAfterCmd - Command ater which to insert the first one
301 //================================================================================
303 void _pyGen::SetCommandAfter( Handle(_pyCommand) theCmd, Handle(_pyCommand) theAfterCmd )
305 list< Handle(_pyCommand) >::iterator pos;
306 pos = find( myCommands.begin(), myCommands.end(), theCmd );
307 myCommands.erase( pos );
308 pos = find( myCommands.begin(), myCommands.end(), theAfterCmd );
309 myCommands.insert( ++pos, theCmd );
312 for ( pos = myCommands.begin(); pos != myCommands.end(); ++pos)
313 (*pos)->SetOrderNb( i++ );
316 //================================================================================
318 * \brief Set method to access to object wrapped with python class
319 * \param theID - The wrapped object entry
320 * \param theMethod - The accessor method
322 //================================================================================
324 void _pyGen::SetAccessorMethod(const _pyID& theID, const char* theMethod )
326 myID2AccessorMethod.Bind( theID, (char*) theMethod );
329 //================================================================================
331 * \brief Find out type of geom group
332 * \param grpID - The geom group entry
333 * \retval int - The type
335 //================================================================================
337 static bool sameGroupType( const _pyID& grpID,
338 const TCollection_AsciiString& theType)
340 // define group type as smesh.Mesh.Group() does
342 SALOMEDS::Study_var study = SMESH_Gen_i::GetSMESHGen()->GetCurrentStudy();
343 SALOMEDS::SObject_var aSObj = study->FindObjectID( grpID.ToCString() );
344 if ( !aSObj->_is_nil() ) {
345 GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow( aSObj->GetObject() );
346 if ( !aGeomObj->_is_nil() ) {
347 switch ( aGeomObj->GetShapeType() ) {
348 case GEOM::VERTEX: type = SMESH::NODE; break;
349 case GEOM::EDGE: type = SMESH::EDGE; break;
350 case GEOM::FACE: type = SMESH::FACE; break;
352 case GEOM::SHELL: type = SMESH::VOLUME; break;
353 case GEOM::COMPOUND: {
354 GEOM::GEOM_Gen_var aGeomGen = SMESH_Gen_i::GetSMESHGen()->GetGeomEngine();
355 if ( !aGeomGen->_is_nil() ) {
356 GEOM::GEOM_IGroupOperations_var aGrpOp =
357 aGeomGen->GetIGroupOperations( study->StudyId() );
358 if ( !aGrpOp->_is_nil() ) {
359 switch ( aGrpOp->GetType( aGeomObj )) {
360 case TopAbs_VERTEX: type = SMESH::NODE; break;
361 case TopAbs_EDGE: type = SMESH::EDGE; break;
362 case TopAbs_FACE: type = SMESH::FACE; break;
363 case TopAbs_SOLID: type = SMESH::VOLUME; break;
374 MESSAGE("Type of the group " << grpID << " not found");
377 if ( theType.IsIntegerValue() )
378 return type == theType.IntegerValue();
381 case SMESH::NODE: return theType.Location( "NODE", 1, theType.Length() );
382 case SMESH::EDGE: return theType.Location( "EDGE", 1, theType.Length() );
383 case SMESH::FACE: return theType.Location( "FACE", 1, theType.Length() );
384 case SMESH::VOLUME: return theType.Location( "VOLUME", 1, theType.Length() );
390 //================================================================================
393 * \param theCreationCmd -
395 //================================================================================
397 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd): _pyObject(theCreationCmd)
399 // convert my creation command
400 Handle(_pyCommand) creationCmd = GetCreationCmd();
401 creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
402 creationCmd->SetMethod( "Mesh" );
404 theGen->SetAccessorMethod( GetID(), "GetMesh()" );
407 //================================================================================
411 * \param theCommand - Engine method called for this mesh
413 //================================================================================
415 void _pyMesh::Process( const Handle(_pyCommand)& theCommand )
417 // smesh.py wraps the following methods:
419 // 1. GetSubMesh(geom, name) + AddHypothesis(geom, algo)
420 // --> in Mesh_Algorithm.Create(mesh, geom, hypo, so)
421 // 2. AddHypothesis(geom, hyp)
422 // --> in Mesh_Algorithm.Hypothesis(hyp, args, so)
423 // 3. CreateGroupFromGEOM(type, name, grp)
424 // --> in Mesh.Group(grp, name="")
425 // 4. ExportToMED(f, opt, version)
426 // --> in Mesh.ExportToMED( f, version, opt=0 )
427 // 5. ExportMED(f, opt)
428 // --> in Mesh.ExportMED( f,opt=0 )
430 // --> in Mesh.ExportDAT( f )
432 // --> in Mesh.ExportUNV(f)
433 // 8. ExportSTL(f, ascii)
434 // --> in Mesh.ExportSTL(f, ascii=1)
436 const TCollection_AsciiString method = theCommand->GetMethod();
437 if ( method == "GetSubMesh" ) {
438 mySubmeshes.push_back( theCommand );
440 else if ( method == "AddHypothesis" ) { // mesh.AddHypothesis(geom, HYPO )
441 myAddHypCmds.push_back( theCommand );
443 const _pyID& hypID = theCommand->GetArg( 2 );
444 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
445 if ( !hyp.IsNull() && hyp->GetMesh().IsEmpty() )
446 hyp->SetMesh( this->GetID() );
448 else if ( method == "CreateGroupFromGEOM" ) {// (type, name, grp)
449 _pyID grp = theCommand->GetArg( 3 );
450 if ( sameGroupType( grp, theCommand->GetArg( 1 )) ) { // --> Group(grp)
451 theCommand->SetMethod( "Group" );
452 theCommand->RemoveArgs();
453 theCommand->SetArg( 1, grp );
456 AddMeshAccess( theCommand );
459 else if ( method == "ExportToMED" ) {//(f, opt, version)
460 // --> (f, version, opt)
461 _pyID opt = theCommand->GetArg( 2 );
462 _pyID ver = theCommand->GetArg( 3 );
463 theCommand->SetArg( 2, ver );
464 theCommand->SetArg( 3, opt );
466 else if ( method == "RemoveHypothesis" ) // (geom, hyp)
468 const _pyID & hypID = theCommand->GetArg( 2 );
470 // check if this mesh still has corresponding addition command
471 bool hasAddCmd = false;
472 list< Handle(_pyCommand) >::iterator cmd = myAddHypCmds.begin();
473 while ( cmd != myAddHypCmds.end() )
475 // AddHypothesis(geom, hyp)
476 if ( hypID == (*cmd)->GetArg( 2 )) { // erase both commands
479 cmd = myAddHypCmds.erase( cmd );
487 // access to wrapped mesh
488 AddMeshAccess( theCommand );
489 // access to wrapped algo
490 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
491 if ( !hyp.IsNull() && hyp->IsAlgo() && hyp->IsWrapped() )
492 theCommand->SetArg( 2, theCommand->GetArg( 2 ) + ".GetAlgorithm()" );
495 else { // apply theCommand to the mesh wrapped by smeshpy mesh
496 AddMeshAccess( theCommand );
500 //================================================================================
502 * \brief Convert creation and addition of all algos and hypos
504 //================================================================================
506 void _pyMesh::Flush()
508 list < Handle(_pyCommand) >::iterator cmd, cmd2;
509 map< _pyID, Handle(_pyCommand) > algo2additionCmd;
511 // try to convert algo addition like this:
512 // mesh.AddHypothesis(geom, ALGO ) --> ALGO = mesh.Algo()
513 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
515 Handle(_pyCommand) addCmd = *cmd;
516 const _pyID& algoID = addCmd->GetArg( 2 );
517 Handle(_pyHypothesis) algo = theGen->FindHyp( algoID );
518 if ( algo.IsNull() || !algo->IsAlgo() )
521 _pyID geom = addCmd->GetArg( 1 );
522 if ( algo->Addition2Creation( addCmd, this->GetID() )) // OK
524 algo2additionCmd[ algo->GetID() ] = addCmd;
526 if ( geom != GetGeom() ) // local algo
528 // mesh.AddHypothesis(geom, ALGO ) --> mesh.AlgoMethod(geom)
529 addCmd->SetArg( addCmd->GetNbArgs() + 1,
530 TCollection_AsciiString( "geom=" ) + geom );
531 // sm = mesh.GetSubMesh(geom, name) --> sm = ALGO.GetSubMesh()
532 for ( cmd2 = mySubmeshes.begin(); cmd2 != mySubmeshes.end(); ++cmd2 ) {
533 Handle(_pyCommand) subCmd = *cmd2;
534 if ( geom == subCmd->GetArg( 1 )) {
535 subCmd->SetObject( algo->GetID() );
536 subCmd->RemoveArgs();
537 if ( addCmd->GetOrderNb() > subCmd->GetOrderNb() )
538 theGen->SetCommandAfter( subCmd, addCmd );
543 else // ALGO was already created
545 // mesh.AddHypothesis(geom, ALGO ) --> mesh.GetMesh().AddHypothesis(geom, ALGO )
546 AddMeshAccess( addCmd );
547 // mesh.GetMesh().AddHypothesis(geom, ALGO ) ->
548 // mesh.GetMesh().AddHypothesis(geom, ALGO.GetAlgorithm() )
549 addCmd->SetArg( 2, addCmd->GetArg( 2 ) + ".GetAlgorithm()" );
553 // try to convert hypo addition like this:
554 // mesh.AddHypothesis(geom, HYPO ) --> HYPO = algo.Hypo()
555 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
557 Handle(_pyCommand) addCmd = *cmd;
558 const _pyID& hypID = addCmd->GetArg( 2 );
559 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
560 if ( hyp.IsNull() || hyp->IsAlgo() )
562 const _pyID& geom = addCmd->GetArg( 1 );
563 // find algo created on <geom> for this mesh
564 Handle(_pyHypothesis) algo = theGen->FindAlgo( geom, this->GetID(), hyp->GetType() );
565 //_pyID algoID = algo.IsNull() ? "" : algo->GetID();
566 if ( !algo.IsNull() && hyp->Addition2Creation( addCmd, this->GetID() )) // OK
568 addCmd->SetObject( algo->GetID() );
569 Handle(_pyCommand) algoCmd = algo2additionCmd[ algo->GetID() ];
570 if ( !algoCmd.IsNull() && algoCmd->GetOrderNb() > addCmd->GetOrderNb() )
571 // algo was created later than hyp
572 theGen->ExchangeCommands( algoCmd, addCmd );
576 AddMeshAccess( addCmd );
580 // sm = mesh.GetSubMesh(geom, name) --> sm = mesh.GetMesh().GetSubMesh(geom, name)
581 for ( cmd = mySubmeshes.begin(); cmd != mySubmeshes.end(); ++cmd ) {
582 Handle(_pyCommand) subCmd = *cmd;
583 if ( subCmd->GetNbArgs() > 0 )
584 AddMeshAccess( subCmd );
586 myAddHypCmds.clear();
590 //================================================================================
592 * \brief _pyHypothesis constructor
593 * \param theCreationCmd -
595 //================================================================================
597 _pyHypothesis::_pyHypothesis(const Handle(_pyCommand)& theCreationCmd):
598 _pyObject( theCreationCmd )
600 myDim = myIsAlgo = /*myIsLocal = */myIsWrapped = myIsConverted = false;
603 //================================================================================
605 * \brief Creates algorithm or hypothesis
606 * \param theCreationCmd - The engine command creating a hypothesis
607 * \retval Handle(_pyHypothesis) - Result _pyHypothesis
609 //================================================================================
611 Handle(_pyHypothesis) _pyHypothesis::NewHypothesis( const Handle(_pyCommand)& theCreationCmd)
613 // theCreationCmd: CreateHypothesis( "theHypType", "theLibName" )
614 ASSERT (( theCreationCmd->GetMethod() == "CreateHypothesis"));
616 Handle(_pyHypothesis) hyp, algo;
619 const TCollection_AsciiString & hypTypeWithQuotes = theCreationCmd->GetArg( 1 );
620 if ( hypTypeWithQuotes.IsEmpty() )
623 TCollection_AsciiString hypType =
624 hypTypeWithQuotes.SubString( 2, hypTypeWithQuotes.Length() - 1 );
626 algo = new _pyAlgorithm( theCreationCmd );
627 hyp = new _pyHypothesis( theCreationCmd );
629 // 1D Regular_1D ----------
630 if ( hypType == "Regular_1D" ) {
632 algo->myCreationMethod = "Segment";
634 else if ( hypType == "LocalLength" ) {
636 hyp->myCreationMethod = "LocalLength";
637 hyp->myType = "Regular_1D";
638 hyp->myArgMethods.Append( "SetLength" );
640 else if ( hypType == "NumberOfSegments" ) {
641 hyp = new _pyNumberOfSegmentsHyp( theCreationCmd );
643 hyp->myCreationMethod = "NumberOfSegments";
644 hyp->myType = "Regular_1D";
645 hyp->myArgMethods.Append( "SetNumberOfSegments" );
646 hyp->myArgMethods.Append( "SetScaleFactor" );
648 else if ( hypType == "Arithmetic1D" ) {
649 hyp = new _pyComplexParamHypo( theCreationCmd );
651 hyp->myCreationMethod = "Arithmetic1D";
652 hyp->myType = "Regular_1D";
654 else if ( hypType == "StartEndLength" ) {
655 hyp = new _pyComplexParamHypo( theCreationCmd );
657 hyp->myCreationMethod = "StartEndLength";
658 hyp->myType = "Regular_1D";
660 else if ( hypType == "Deflection1D" ) {
662 hyp->myCreationMethod = "Deflection1D";
663 hyp->myArgMethods.Append( "SetDeflection" );
664 hyp->myType = "Regular_1D";
666 else if ( hypType == "Propagation" ) {
668 hyp->myCreationMethod = "Propagation";
669 hyp->myType = "Regular_1D";
671 else if ( hypType == "AutomaticLength" ) {
673 hyp->myCreationMethod = "AutomaticLength";
674 hyp->myType = "Regular_1D";
675 hyp->myArgMethods.Append( "SetFineness");
677 // 1D Python_1D ----------
678 else if ( hypType == "Python_1D" ) {
680 algo->myCreationMethod = "Segment";
681 algo->myArgs.Append( "algo=smesh.PYTHON");
683 else if ( hypType == "PythonSplit1D" ) {
685 hyp->myCreationMethod = "PythonSplit1D";
686 hyp->myType = "Python_1D";
687 hyp->myArgMethods.Append( "SetNumberOfSegments");
688 hyp->myArgMethods.Append( "SetPythonLog10RatioFunction");
691 else if ( hypType == "MEFISTO_2D" ) {
693 algo->myCreationMethod = "Triangle";
695 else if ( hypType == "MaxElementArea" ) {
697 hyp->myCreationMethod = "MaxElementArea";
698 hyp->myType = "MEFISTO_2D";
699 hyp->myArgMethods.Append( "SetMaxElementArea");
701 else if ( hypType == "LengthFromEdges" ) {
703 hyp->myCreationMethod = "LengthFromEdges";
704 hyp->myType = "MEFISTO_2D";
706 else if ( hypType == "Quadrangle_2D" ) {
708 algo->myCreationMethod = "Quadrangle";
710 else if ( hypType == "QuadranglePreference" ) {
712 hyp->myCreationMethod = "QuadranglePreference";
713 hyp->myType = "Quadrangle_2D";
716 else if ( hypType == "NETGEN_3D") {
718 algo->myCreationMethod = "Tetrahedron";
719 algo->myArgs.Append( "algo=smesh.NETGEN" );
721 else if ( hypType == "MaxElementVolume") {
723 hyp->myCreationMethod = "MaxElementVolume";
724 hyp->myType = "NETGEN_3D";
725 hyp->myArgMethods.Append( "SetMaxElementVolume" );
727 else if ( hypType == "GHS3D_3D" ) {
729 algo->myCreationMethod = "Tetrahedron";
730 algo->myArgs.Append( "algo=smesh.GHS3D" );
732 else if ( hypType == "Hexa_3D" ) {
734 algo->myCreationMethod = "Hexahedron";
737 if ( algo->GetDim() ) {
738 algo->myType = hypType;
744 //================================================================================
746 * \brief Convert the command adding a hypothesis to mesh into a smesh command
747 * \param theCmd - The command like mesh.AddHypothesis( geom, hypo )
748 * \param theAlgo - The algo that can create this hypo
749 * \retval bool - false if the command cant be converted
751 //================================================================================
753 bool _pyHypothesis::Addition2Creation( const Handle(_pyCommand)& theCmd,
754 const _pyID& theMesh)
756 ASSERT(( theCmd->GetMethod() == "AddHypothesis" ));
758 if ( !IsWrappable( theMesh ))
765 // mesh.AddHypothesis(geom,hyp) --> hyp = theMesh.myCreationMethod(args)
766 theCmd->SetResultValue( GetID() );
767 theCmd->SetObject( theMesh );
768 theCmd->SetMethod( myCreationMethod );
770 theCmd->RemoveArgs();
771 for ( int i = 1; i <= myArgs.Length(); ++i ) {
772 if ( !myArgs( i ).IsEmpty() )
773 theCmd->SetArg( i, myArgs( i ));
775 theCmd->SetArg( i, "[]");
778 // clear commands setting arg values
779 list < Handle(_pyCommand) >::iterator argCmd = myArgCommands.begin();
780 for ( ; argCmd != myArgCommands.end(); ++argCmd )
785 // // set arg commands after hypo creation
786 // list<Handle(_pyCommand)>::iterator argCmd = myArgCommands.begin();
787 // for ( ; argCmd != myArgCommands.end(); ++argCmd )
788 // if ( !(*argCmd)->IsEmpty() && GetCommandNb() > (*argCmd)->GetOrderNb() )
789 // theGen->ExchangeCommands( GetCreationCmd(), *argCmd );
792 // set unknown arg commands after hypo creation
793 Handle(_pyCommand) afterCmd = myIsWrapped ? theCmd : GetCreationCmd();
794 list<Handle(_pyCommand)>::iterator cmd = myUnknownCommands.begin();
795 for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
796 if ( !(*cmd)->IsEmpty() && afterCmd->GetOrderNb() > (*cmd)->GetOrderNb() ) {
797 theGen->SetCommandAfter( *cmd, afterCmd );
801 myArgCommands.clear();
802 myUnknownCommands.clear();
807 //================================================================================
809 * \brief Remember hypothesis parameter values
810 * \param theCommand - The called hypothesis method
812 //================================================================================
814 void _pyHypothesis::Process( const Handle(_pyCommand)& theCommand)
818 for ( int i = 1; i <= myArgMethods.Length(); ++i ) {
819 if ( myArgMethods( i ) == theCommand->GetMethod() ) {
820 while ( myArgs.Length() < i )
821 myArgs.Append( "[]" );
822 myArgs( i ) = theCommand->GetArg( 1 ); // arg value
823 myArgCommands.push_back( theCommand );
827 myUnknownCommands.push_back( theCommand );
830 //================================================================================
832 * \brief Finish conversion
834 //================================================================================
836 void _pyHypothesis::Flush()
839 GetCreationCmd()->Clear();
842 //================================================================================
844 * \brief Remember hypothesis parameter values
845 * \param theCommand - The called hypothesis method
847 //================================================================================
849 void _pyComplexParamHypo::Process( const Handle(_pyCommand)& theCommand)
851 // ex: hyp.SetLength(start, 1)
852 // hyp.SetLength(end, 0)
853 ASSERT(( theCommand->GetMethod() == "SetLength" ));
854 ASSERT(( theCommand->GetArg( 2 ).IsIntegerValue() ));
855 int i = 2 - theCommand->GetArg( 2 ).IntegerValue();
856 while ( myArgs.Length() < i )
857 myArgs.Append( "[]" );
858 myArgs( i ) = theCommand->GetArg( 1 ); // arg value
859 myArgCommands.push_back( theCommand );
862 //================================================================================
864 * \brief additionally to Addition2Creation, clears SetDistrType() command
865 * \param theCmd - AddHypothesis() command
866 * \param theMesh - mesh to which a hypothesis is added
867 * \retval bool - convertion result
869 //================================================================================
871 bool _pyNumberOfSegmentsHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
872 const _pyID& theMesh)
874 if ( IsWrappable( theMesh ) && myArgs.Length() > 1 ) {
875 list<Handle(_pyCommand)>::iterator cmd = myUnknownCommands.begin();
876 for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
877 // clear SetDistrType()
878 if ( (*cmd)->GetString().Location( "SetDistrType", 1, (*cmd)->Length() ))
882 return _pyHypothesis::Addition2Creation( theCmd, theMesh );
885 //================================================================================
887 * \brief _pyAlgorithm constructor
888 * \param theCreationCmd - The command like "algo = smeshgen.CreateHypothesis(type,lib)"
890 //================================================================================
892 _pyAlgorithm::_pyAlgorithm(const Handle(_pyCommand)& theCreationCmd)
893 : _pyHypothesis( theCreationCmd )
898 //================================================================================
900 * \brief Convert the command adding an algorithm to mesh
901 * \param theCmd - The command like mesh.AddHypothesis( geom, algo )
902 * \param theMesh - The mesh needing this algo
903 * \retval bool - false if the command cant be converted
905 //================================================================================
907 bool _pyAlgorithm::Addition2Creation( const Handle(_pyCommand)& theCmd,
908 const _pyID& theMeshID)
910 if ( IsWrappable( theMeshID )) {
912 myGeom = theCmd->GetArg( 1 );
914 // mesh.AddHypothesis(geom,algo) --> theMeshID.myCreationMethod()
915 if ( _pyHypothesis::Addition2Creation( theCmd, theMeshID )) {
916 theGen->SetAccessorMethod( GetID(), "GetAlgorithm()" );
923 //================================================================================
925 * \brief Return starting position of a part of python command
926 * \param thePartIndex - The index of command part
927 * \retval int - Part position
929 //================================================================================
931 int _pyCommand::GetBegPos( int thePartIndex )
935 if ( myBegPos.Length() < thePartIndex )
937 return myBegPos( thePartIndex );
940 //================================================================================
942 * \brief Store starting position of a part of python command
943 * \param thePartIndex - The index of command part
944 * \param thePosition - Part position
946 //================================================================================
948 void _pyCommand::SetBegPos( int thePartIndex, int thePosition )
950 while ( myBegPos.Length() < thePartIndex )
951 myBegPos.Append( UNKNOWN );
952 myBegPos( thePartIndex ) = thePosition;
955 //================================================================================
957 * \brief Return substring of python command looking like ResultValue = Obj.Meth()
958 * \retval const TCollection_AsciiString & - ResultValue substring
960 //================================================================================
962 const TCollection_AsciiString & _pyCommand::GetResultValue()
964 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
966 int begPos = myString.Location( "=", 1, Length() );
968 myRes = GetWord( myString, begPos, false );
971 SetBegPos( RESULT_IND, begPos );
976 //================================================================================
978 * \brief Return substring of python command looking like ResVal = Object.Meth()
979 * \retval const TCollection_AsciiString & - Object substring
981 //================================================================================
983 const TCollection_AsciiString & _pyCommand::GetObject()
985 if ( GetBegPos( OBJECT_IND ) == UNKNOWN )
988 int begPos = GetBegPos( RESULT_IND ) + myRes.Length();
990 begPos = myString.Location( "=", 1, Length() ) + 1;
992 myObj = GetWord( myString, begPos, true );
993 SetBegPos( OBJECT_IND, begPos );
999 //================================================================================
1001 * \brief Return substring of python command looking like ResVal = Obj.Method()
1002 * \retval const TCollection_AsciiString & - Method substring
1004 //================================================================================
1006 const TCollection_AsciiString & _pyCommand::GetMethod()
1008 if ( GetBegPos( METHOD_IND ) == UNKNOWN )
1011 int begPos = GetBegPos( OBJECT_IND ) + myObj.Length();
1012 bool forward = true;
1014 begPos = myString.Location( "(", 1, Length() ) - 1;
1018 myMeth = GetWord( myString, begPos, forward );
1019 SetBegPos( METHOD_IND, begPos );
1025 //================================================================================
1027 * \brief Return substring of python command looking like ResVal = Obj.Meth(Arg1,...)
1028 * \retval const TCollection_AsciiString & - Arg<index> substring
1030 //================================================================================
1032 const TCollection_AsciiString & _pyCommand::GetArg( int index )
1034 if ( GetBegPos( ARG1_IND ) == UNKNOWN )
1037 int begPos = GetBegPos( METHOD_IND ) + myMeth.Length();
1039 begPos = myString.Location( "(", 1, Length() ) + 1;
1041 int i = 0, prevLen = 0;
1042 while ( begPos != EMPTY ) {
1044 // check if we are looking at the closing parenthesis
1045 while ( begPos <= Length() && isspace( myString.Value( begPos )))
1047 if ( begPos > Length() || myString.Value( begPos ) == ')' )
1049 myArgs.Append( GetWord( myString, begPos, true, true ));
1050 SetBegPos( ARG1_IND + i, begPos );
1051 prevLen = myArgs.Last().Length();
1053 myArgs.Remove( myArgs.Length() ); // no more args
1057 if ( myArgs.Length() < index )
1058 return theEmptyString;
1059 return myArgs( index );
1062 //================================================================================
1064 * \brief Check if char is a word part
1065 * \param c - The character to check
1066 * \retval bool - The check result
1068 //================================================================================
1070 static inline bool isWord(const char c, const bool dotIsWord)
1073 !isspace(c) && c != ',' && c != '=' && c != ')' && c != '(' && ( dotIsWord || c != '.');
1076 //================================================================================
1078 * \brief Looks for a word in the string and returns word's beginning
1079 * \param theString - The input string
1080 * \param theStartPos - The position to start the search, returning word's beginning
1081 * \param theForward - The search direction
1082 * \retval TCollection_AsciiString - The found word
1084 //================================================================================
1086 TCollection_AsciiString _pyCommand::GetWord( const TCollection_AsciiString & theString,
1088 const bool theForward,
1089 const bool dotIsWord )
1091 int beg = theStartPos, end = theStartPos;
1092 theStartPos = EMPTY;
1093 if ( beg < 1 || beg > theString.Length() )
1094 return theEmptyString;
1096 if ( theForward ) { // search forward
1098 while ( beg <= theString.Length() && !isWord( theString.Value( beg ), dotIsWord))
1100 if ( beg > theString.Length() )
1101 return theEmptyString; // no word found
1104 while ( end <= theString.Length() && isWord( theString.Value( end ), dotIsWord))
1108 else { // search backward
1110 while ( end > 0 && !isWord( theString.Value( end ), dotIsWord))
1113 return theEmptyString; // no word found
1115 while ( beg > 0 && isWord( theString.Value( beg ), dotIsWord))
1120 //cout << theString << " ---- " << beg << " - " << end << endl;
1121 return theString.SubString( beg, end );
1124 //================================================================================
1126 * \brief Look for position where not space char is
1127 * \param theString - The string
1128 * \param thePos - The position to search from and which returns result
1129 * \retval bool - false if there are only space after thePos in theString
1133 //================================================================================
1135 bool _pyCommand::SkipSpaces( const TCollection_AsciiString & theString, int & thePos )
1137 if ( thePos < 1 || thePos > theString.Length() )
1140 while ( thePos <= theString.Length() && isspace( theString.Value( thePos )))
1143 return thePos <= theString.Length();
1146 //================================================================================
1148 * \brief Modify a part of the command
1149 * \param thePartIndex - The index of the part
1150 * \param thePart - The new part string
1151 * \param theOldPart - The old part
1153 //================================================================================
1155 void _pyCommand::SetPart(int thePartIndex, const TCollection_AsciiString& thePart,
1156 TCollection_AsciiString& theOldPart)
1158 int pos = GetBegPos( thePartIndex );
1159 if ( pos <= Length() && theOldPart != thePart)
1161 TCollection_AsciiString seperator;
1163 pos = GetBegPos( thePartIndex + 1 );
1164 if ( pos < 1 ) return;
1165 switch ( thePartIndex ) {
1166 case RESULT_IND: seperator = " = "; break;
1167 case OBJECT_IND: seperator = "."; break;
1168 case METHOD_IND: seperator = "()"; break;
1172 myString.Remove( pos, theOldPart.Length() );
1173 if ( !seperator.IsEmpty() )
1174 myString.Insert( pos , seperator );
1175 myString.Insert( pos, thePart );
1176 // update starting positions of the following parts
1177 int posDelta = thePart.Length() + seperator.Length() - theOldPart.Length();
1178 for ( int i = thePartIndex + 1; i <= myBegPos.Length(); ++i ) {
1179 if ( myBegPos( i ) > 0 )
1180 myBegPos( i ) += posDelta;
1182 theOldPart = thePart;
1186 //================================================================================
1188 * \brief Set agrument
1189 * \param index - The argument index, it counts from 1
1190 * \param theArg - The argument string
1192 //================================================================================
1194 void _pyCommand::SetArg( int index, const TCollection_AsciiString& theArg)
1197 int argInd = ARG1_IND + index - 1;
1198 int pos = GetBegPos( argInd );
1199 if ( pos < 1 ) // no index-th arg exist, append inexistent args
1201 // find a closing parenthesis
1203 while ( pos > 0 && myString.Value( pos ) != ')' )
1205 if ( pos == 0 ) { // no parentheses at all
1209 while ( myArgs.Length() < index ) {
1210 if ( myArgs.Length() )
1211 myString.Insert( pos++, "," );
1212 myArgs.Append("None");
1213 myString.Insert( pos, myArgs.Last() );
1214 SetBegPos( ARG1_IND + myArgs.Length() - 1, pos );
1215 pos += myArgs.Last().Length();
1218 SetPart( argInd, theArg, myArgs( index ));
1221 //================================================================================
1223 * \brief Empty arg list
1225 //================================================================================
1227 void _pyCommand::RemoveArgs()
1229 if ( int pos = myString.Location( '(', 1, Length() ))
1230 myString.Trunc( pos );
1233 if ( myBegPos.Length() >= ARG1_IND )
1234 myBegPos.Remove( ARG1_IND, myBegPos.Length() );