1 // SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
3 // Copyright (C) 2003 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
24 // File : SMESH_2D_Algo_i.hxx
25 // Author : Paul RASCLE, EDF
29 // File : SMESH_2smeshpy.cxx
30 // Created : Fri Nov 18 13:20:10 2005
31 // Author : Edward AGAPOV (eap)
33 #include "SMESH_2smeshpy.hxx"
35 #include "utilities.h"
36 #include "SMESH_PythonDump.hxx"
37 #include "Resource_DataMapOfAsciiStringAsciiString.hxx"
39 #include "SMESH_Gen_i.hxx"
40 /* SALOME headers that include CORBA headers that include windows.h
41 * that defines GetObject symbol as GetObjectA should stand before SALOME headers
42 * that declare methods named GetObject - to apply the same rules of GetObject renaming
43 * and thus to avoid mess with GetObject symbol on Windows */
45 IMPLEMENT_STANDARD_HANDLE (_pyObject ,Standard_Transient);
46 IMPLEMENT_STANDARD_HANDLE (_pyCommand ,Standard_Transient);
47 IMPLEMENT_STANDARD_HANDLE (_pyGen ,_pyObject);
48 IMPLEMENT_STANDARD_HANDLE (_pyMesh ,_pyObject);
49 IMPLEMENT_STANDARD_HANDLE (_pyHypothesis ,_pyObject);
50 IMPLEMENT_STANDARD_HANDLE (_pyAlgorithm ,_pyHypothesis);
51 IMPLEMENT_STANDARD_HANDLE (_pyComplexParamHypo,_pyHypothesis);
52 IMPLEMENT_STANDARD_HANDLE (_pyNumberOfSegmentsHyp,_pyHypothesis);
54 IMPLEMENT_STANDARD_RTTIEXT(_pyObject ,Standard_Transient);
55 IMPLEMENT_STANDARD_RTTIEXT(_pyCommand ,Standard_Transient);
56 IMPLEMENT_STANDARD_RTTIEXT(_pyGen ,_pyObject);
57 IMPLEMENT_STANDARD_RTTIEXT(_pyMesh ,_pyObject);
58 IMPLEMENT_STANDARD_RTTIEXT(_pyHypothesis ,_pyObject);
59 IMPLEMENT_STANDARD_RTTIEXT(_pyAlgorithm ,_pyHypothesis);
60 IMPLEMENT_STANDARD_RTTIEXT(_pyComplexParamHypo,_pyHypothesis);
61 IMPLEMENT_STANDARD_RTTIEXT(_pyNumberOfSegmentsHyp,_pyHypothesis);
64 using SMESH::TPythonDump;
67 * \brief Container of commands into which the initial script is split.
68 * It also contains data coresponding to SMESH_Gen contents
70 static Handle(_pyGen) theGen;
72 static TCollection_AsciiString theEmptyString;
74 //#define DUMP_CONVERSION
76 #if !defined(_DEBUG_) && defined(DUMP_CONVERSION)
77 #undef DUMP_CONVERSION
80 //================================================================================
82 * \brief Convert python script using commands of smesh.py
83 * \param theScript - Input script
84 * \retval TCollection_AsciiString - Convertion result
86 //================================================================================
88 TCollection_AsciiString
89 SMESH_2smeshpy::ConvertScript(const TCollection_AsciiString& theScript,
90 Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod)
92 theGen = new _pyGen( theEntry2AccessorMethod );
94 // split theScript into separate commands
95 int from = 1, end = theScript.Length(), to;
96 while ( from < end && ( to = theScript.Location( "\n", from, end )))
99 // cut out and store a command
100 theGen->AddCommand( theScript.SubString( from, to - 1 ));
105 #ifdef DUMP_CONVERSION
106 cout << endl << " ######## RESULT ######## " << endl<< endl;
108 // reorder commands after conversion
109 list< Handle(_pyCommand) >::iterator cmd;
112 orderChanges = false;
113 for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
114 if ( (*cmd)->SetDependentCmdsAfter() )
116 } while ( orderChanges );
118 // concat commands back into a script
119 TCollection_AsciiString aScript;
120 for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
122 #ifdef DUMP_CONVERSION
123 cout << "## COM " << (*cmd)->GetOrderNb() << ": "<< (*cmd)->GetString() << endl;
125 if ( !(*cmd)->IsEmpty() ) {
127 aScript += (*cmd)->GetString();
137 //================================================================================
139 * \brief _pyGen constructor
141 //================================================================================
143 _pyGen::_pyGen(Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod)
144 : _pyObject( new _pyCommand( TPythonDump::SMESHGenName(), 0 )),
145 myID2AccessorMethod( theEntry2AccessorMethod )
148 myHasPattern = false;
149 // make that GetID() to return TPythonDump::SMESHGenName()
150 GetCreationCmd()->GetString() += "=";
153 //================================================================================
155 * \brief Convert a command using a specific converter
156 * \param theCommand - the command to convert
158 //================================================================================
160 void _pyGen::AddCommand( const TCollection_AsciiString& theCommand)
162 // store theCommand in the sequence
163 myCommands.push_back( new _pyCommand( theCommand, ++myNbCommands ));
165 Handle(_pyCommand) aCommand = myCommands.back();
166 #ifdef DUMP_CONVERSION
167 cout << "## COM " << myNbCommands << ": "<< aCommand->GetString() << endl;
170 _pyID objID = aCommand->GetObject();
172 if ( objID.IsEmpty() )
176 if ( objID == this->GetID() ) {
177 this->Process( aCommand );
180 // SMESH_Mesh method?
181 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( objID );
182 if ( id_mesh != myMeshes.end() ) {
183 id_mesh->second->Process( aCommand );
186 // SMESH_Hypothesis method?
187 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
188 for ( ; hyp != myHypos.end(); ++hyp )
189 if ( !(*hyp)->IsAlgo() && objID == (*hyp)->GetID() ) {
190 (*hyp)->Process( aCommand );
194 // Add access to a wrapped mesh
195 for ( id_mesh = myMeshes.begin(); id_mesh != myMeshes.end(); ++id_mesh ) {
196 if ( aCommand->AddAccessorMethod( id_mesh->first, id_mesh->second->AccessorMethod() ))
200 // Add access to a wrapped algorithm
201 for ( hyp = myHypos.begin(); hyp != myHypos.end(); ++hyp ) {
202 if ( (*hyp)->IsAlgo() &&
203 aCommand->AddAccessorMethod( (*hyp)->GetID(), (*hyp)->AccessorMethod() ))
207 // PAL12227. PythonDump was not updated at proper time; result is
208 // aCriteria.append(SMESH.Filter.Criterion(17,26,0,'L1',26,25,1e-07,SMESH.EDGE,-1))
209 // TypeError: __init__() takes exactly 11 arguments (10 given)
210 char wrongCommand[] = "SMESH.Filter.Criterion(";
211 if ( int beg = theCommand.Location( wrongCommand, 1, theCommand.Length() ))
213 _pyCommand tmpCmd( theCommand.SubString( beg, theCommand.Length() ), -1);
214 // there must be 10 arguments, 5-th arg ThresholdID is missing,
215 const int wrongNbArgs = 9, missingArg = 5;
216 if ( tmpCmd.GetNbArgs() == wrongNbArgs )
218 for ( int i = wrongNbArgs; i > missingArg; --i )
219 tmpCmd.SetArg( i + 1, tmpCmd.GetArg( i ));
220 tmpCmd.SetArg( missingArg, "''");
221 aCommand->GetString().Trunc( beg - 1 );
222 aCommand->GetString() += tmpCmd.GetString();
227 //================================================================================
229 * \brief Convert the command or remember it for later conversion
230 * \param theCommand - The python command calling a method of SMESH_Gen
232 //================================================================================
234 void _pyGen::Process( const Handle(_pyCommand)& theCommand )
236 // there are methods to convert:
237 // CreateMesh( shape )
238 // CreateHypothesis( theHypType, theLibName )
239 // Compute( mesh, geom )
241 if ( theCommand->GetMethod() == "CreateMesh" )
243 Handle(_pyMesh) mesh = new _pyMesh( theCommand );
244 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
248 // CreateHypothesis()
249 if ( theCommand->GetMethod() == "CreateHypothesis" )
251 myHypos.push_back( _pyHypothesis::NewHypothesis( theCommand ));
255 // smeshgen.Compute( mesh, geom ) --> mesh.Compute()
256 if ( theCommand->GetMethod() == "Compute" )
258 const _pyID& meshID = theCommand->GetArg( 1 );
259 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( meshID );
260 if ( id_mesh != myMeshes.end() ) {
261 theCommand->SetObject( meshID );
262 theCommand->RemoveArgs();
263 id_mesh->second->Flush();
268 // leave only one smeshgen.GetPattern() in the script
269 if ( theCommand->GetMethod() == "GetPattern" ) {
270 if ( myHasPattern ) {
277 // smeshgen.Method() --> smesh.smesh.Method()
278 theCommand->SetObject( SMESH_2smeshpy::GenName() );
281 //================================================================================
283 * \brief Convert the remembered commands
285 //================================================================================
289 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.begin();
290 for ( ; id_mesh != myMeshes.end(); ++id_mesh )
291 if ( ! id_mesh->second.IsNull() )
292 id_mesh->second->Flush();
294 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
295 for ( ; hyp != myHypos.end(); ++hyp )
296 if ( !hyp->IsNull() ) {
298 // smeshgen.CreateHypothesis() --> smesh.smesh.CreateHypothesis()
299 if ( !(*hyp)->IsWrapped() )
300 (*hyp)->GetCreationCmd()->SetObject( SMESH_2smeshpy::GenName() );
304 //================================================================================
306 * \brief Find hypothesis by ID (entry)
307 * \param theHypID - The hypothesis ID
308 * \retval Handle(_pyHypothesis) - The found hypothesis
310 //================================================================================
312 Handle(_pyHypothesis) _pyGen::FindHyp( const _pyID& theHypID )
314 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
315 for ( ; hyp != myHypos.end(); ++hyp )
316 if ( !hyp->IsNull() && theHypID == (*hyp)->GetID() )
318 return Handle(_pyHypothesis)();
321 //================================================================================
323 * \brief Find algorithm the created algorithm
324 * \param theGeom - The shape ID the algorithm was created on
325 * \param theMesh - The mesh ID that created the algorithm
326 * \param dim - The algo dimension
327 * \retval Handle(_pyHypothesis) - The found algo
329 //================================================================================
331 Handle(_pyHypothesis) _pyGen::FindAlgo( const _pyID& theGeom, const _pyID& theMesh,
332 const TCollection_AsciiString& theAlgoType )
334 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
335 for ( ; hyp != myHypos.end(); ++hyp )
336 if ( !hyp->IsNull() &&
338 (*hyp)->GetType() == theAlgoType &&
339 (*hyp)->GetGeom() == theGeom &&
340 (*hyp)->GetMesh() == theMesh )
345 //================================================================================
347 * \brief Change order of commands in the script
348 * \param theCmd1 - One command
349 * \param theCmd2 - Another command
351 //================================================================================
353 void _pyGen::ExchangeCommands( Handle(_pyCommand) theCmd1, Handle(_pyCommand) theCmd2 )
355 list< Handle(_pyCommand) >::iterator pos1, pos2;
356 pos1 = find( myCommands.begin(), myCommands.end(), theCmd1 );
357 pos2 = find( myCommands.begin(), myCommands.end(), theCmd2 );
358 myCommands.insert( pos1, theCmd2 );
359 myCommands.insert( pos2, theCmd1 );
360 myCommands.erase( pos1 );
361 myCommands.erase( pos2 );
363 int nb1 = theCmd1->GetOrderNb();
364 theCmd1->SetOrderNb( theCmd2->GetOrderNb() );
365 theCmd2->SetOrderNb( nb1 );
366 // cout << "BECOME " << theCmd1->GetOrderNb() << "\t" << theCmd1->GetString() << endl
367 // << "BECOME " << theCmd2->GetOrderNb() << "\t" << theCmd2->GetString() << endl << endl;
370 //================================================================================
372 * \brief Set one command after the other
373 * \param theCmd - Command to move
374 * \param theAfterCmd - Command ater which to insert the first one
376 //================================================================================
378 void _pyGen::SetCommandAfter( Handle(_pyCommand) theCmd, Handle(_pyCommand) theAfterCmd )
380 // cout << "SET\t" << theCmd->GetString() << endl << "AFTER\t" << theAfterCmd->GetString() << endl << endl;
381 list< Handle(_pyCommand) >::iterator pos;
382 pos = find( myCommands.begin(), myCommands.end(), theCmd );
383 myCommands.erase( pos );
384 pos = find( myCommands.begin(), myCommands.end(), theAfterCmd );
385 myCommands.insert( ++pos, theCmd );
388 for ( pos = myCommands.begin(); pos != myCommands.end(); ++pos)
389 (*pos)->SetOrderNb( i++ );
392 //================================================================================
394 * \brief Set method to access to object wrapped with python class
395 * \param theID - The wrapped object entry
396 * \param theMethod - The accessor method
398 //================================================================================
400 void _pyGen::SetAccessorMethod(const _pyID& theID, const char* theMethod )
402 myID2AccessorMethod.Bind( theID, (char*) theMethod );
405 //================================================================================
407 * \brief Find out type of geom group
408 * \param grpID - The geom group entry
409 * \retval int - The type
411 //================================================================================
413 static bool sameGroupType( const _pyID& grpID,
414 const TCollection_AsciiString& theType)
416 // define group type as smesh.Mesh.Group() does
418 SALOMEDS::Study_var study = SMESH_Gen_i::GetSMESHGen()->GetCurrentStudy();
419 SALOMEDS::SObject_var aSObj = study->FindObjectID( grpID.ToCString() );
420 if ( !aSObj->_is_nil() ) {
421 GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow( aSObj->GetObject() );
422 if ( !aGeomObj->_is_nil() ) {
423 switch ( aGeomObj->GetShapeType() ) {
424 case GEOM::VERTEX: type = SMESH::NODE; break;
425 case GEOM::EDGE: type = SMESH::EDGE; break;
426 case GEOM::FACE: type = SMESH::FACE; break;
428 case GEOM::SHELL: type = SMESH::VOLUME; break;
429 case GEOM::COMPOUND: {
430 GEOM::GEOM_Gen_var aGeomGen = SMESH_Gen_i::GetSMESHGen()->GetGeomEngine();
431 if ( !aGeomGen->_is_nil() ) {
432 GEOM::GEOM_IGroupOperations_var aGrpOp =
433 aGeomGen->GetIGroupOperations( study->StudyId() );
434 if ( !aGrpOp->_is_nil() ) {
435 switch ( aGrpOp->GetType( aGeomObj )) {
436 case TopAbs_VERTEX: type = SMESH::NODE; break;
437 case TopAbs_EDGE: type = SMESH::EDGE; break;
438 case TopAbs_FACE: type = SMESH::FACE; break;
439 case TopAbs_SOLID: type = SMESH::VOLUME; break;
450 MESSAGE("Type of the group " << grpID << " not found");
453 if ( theType.IsIntegerValue() )
454 return type == theType.IntegerValue();
457 case SMESH::NODE: return theType.Location( "NODE", 1, theType.Length() );
458 case SMESH::EDGE: return theType.Location( "EDGE", 1, theType.Length() );
459 case SMESH::FACE: return theType.Location( "FACE", 1, theType.Length() );
460 case SMESH::VOLUME: return theType.Location( "VOLUME", 1, theType.Length() );
466 //================================================================================
469 * \param theCreationCmd -
471 //================================================================================
473 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd):
474 _pyObject(theCreationCmd), myHasEditor(false)
476 // convert my creation command
477 Handle(_pyCommand) creationCmd = GetCreationCmd();
478 creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
479 creationCmd->SetMethod( "Mesh" );
481 theGen->SetAccessorMethod( GetID(), "GetMesh()" );
484 //================================================================================
486 * \brief Convert a IDL API command of SMESH::Mesh to a method call of python Mesh
487 * \param theCommand - Engine method called for this mesh
489 //================================================================================
491 void _pyMesh::Process( const Handle(_pyCommand)& theCommand )
493 // smesh.py wraps the following methods:
495 // 1. GetSubMesh(geom, name) + AddHypothesis(geom, algo)
496 // --> in Mesh_Algorithm.Create(mesh, geom, hypo, so)
497 // 2. AddHypothesis(geom, hyp)
498 // --> in Mesh_Algorithm.Hypothesis(hyp, args, so)
499 // 3. CreateGroupFromGEOM(type, name, grp)
500 // --> in Mesh.Group(grp, name="")
501 // 4. ExportToMED(f, opt, version)
502 // --> in Mesh.ExportToMED( f, version, opt=0 )
503 // 5. ExportMED(f, opt)
504 // --> in Mesh.ExportMED( f,opt=0 )
506 // --> in Mesh.ExportDAT( f )
508 // --> in Mesh.ExportUNV(f)
509 // 8. ExportSTL(f, ascii)
510 // --> in Mesh.ExportSTL(f, ascii=1)
512 const TCollection_AsciiString method = theCommand->GetMethod();
513 if ( method == "GetSubMesh" ) {
514 mySubmeshes.push_back( theCommand );
516 else if ( method == "AddHypothesis" ) { // mesh.AddHypothesis(geom, HYPO )
517 myAddHypCmds.push_back( theCommand );
519 const _pyID& hypID = theCommand->GetArg( 2 );
520 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
521 if ( !hyp.IsNull() ) {
522 myHypos.push_back( hyp );
523 if ( hyp->GetMesh().IsEmpty() )
524 hyp->SetMesh( this->GetID() );
527 else if ( method == "CreateGroupFromGEOM" ) {// (type, name, grp)
528 _pyID grp = theCommand->GetArg( 3 );
529 if ( sameGroupType( grp, theCommand->GetArg( 1 )) ) { // --> Group(grp)
530 theCommand->SetMethod( "Group" );
531 theCommand->RemoveArgs();
532 theCommand->SetArg( 1, grp );
535 AddMeshAccess( theCommand );
538 else if ( method == "ExportToMED" ) {//(f, opt, version)
539 // --> (f, version, opt)
540 _pyID opt = theCommand->GetArg( 2 );
541 _pyID ver = theCommand->GetArg( 3 );
542 theCommand->SetArg( 2, ver );
543 theCommand->SetArg( 3, opt );
545 else if ( method == "RemoveHypothesis" ) // (geom, hyp)
547 const _pyID & hypID = theCommand->GetArg( 2 );
549 // check if this mesh still has corresponding addition command
550 bool hasAddCmd = false;
551 list< Handle(_pyCommand) >::iterator cmd = myAddHypCmds.begin();
552 while ( cmd != myAddHypCmds.end() )
554 // AddHypothesis(geom, hyp)
555 if ( hypID == (*cmd)->GetArg( 2 )) { // erase both (add and remove) commands
558 cmd = myAddHypCmds.erase( cmd );
565 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
566 if ( ! hasAddCmd ) { // hypo addition already wrapped
567 // access to wrapped mesh
568 AddMeshAccess( theCommand );
569 // access to wrapped algo
570 if ( !hyp.IsNull() && hyp->IsAlgo() && hyp->IsWrapped() )
571 theCommand->SetArg( 2, theCommand->GetArg( 2 ) + ".GetAlgorithm()" );
573 // remove hyp from myHypos
574 myHypos.remove( hyp );
577 // leave only one " mesh_editor_<nb> = mesh.GetMeshEditor()"
578 else if ( theCommand->GetMethod() == "GetMeshEditor")
583 AddMeshAccess( theCommand );
587 // apply theCommand to the mesh wrapped by smeshpy mesh
590 AddMeshAccess( theCommand );
594 //================================================================================
596 * \brief Convert creation and addition of all algos and hypos
598 //================================================================================
600 void _pyMesh::Flush()
602 list < Handle(_pyCommand) >::iterator cmd, cmd2;
604 // try to convert algo addition like this:
605 // mesh.AddHypothesis(geom, ALGO ) --> ALGO = mesh.Algo()
606 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
608 Handle(_pyCommand) addCmd = *cmd;
609 const _pyID& algoID = addCmd->GetArg( 2 );
610 Handle(_pyHypothesis) algo = theGen->FindHyp( algoID );
611 if ( algo.IsNull() || !algo->IsAlgo() )
614 _pyID geom = addCmd->GetArg( 1 );
615 if ( algo->Addition2Creation( addCmd, this->GetID() )) // OK
617 // wrapped algo is created atfer mesh creation
618 GetCreationCmd()->AddDependantCmd( addCmd );
620 if ( geom != GetGeom() ) // local algo
622 // mesh.AddHypothesis(geom, ALGO ) --> mesh.AlgoMethod(geom)
623 addCmd->SetArg( addCmd->GetNbArgs() + 1,
624 TCollection_AsciiString( "geom=" ) + geom );
625 // sm = mesh.GetSubMesh(geom, name) --> sm = ALGO.GetSubMesh()
626 for ( cmd2 = mySubmeshes.begin(); cmd2 != mySubmeshes.end(); ++cmd2 ) {
627 Handle(_pyCommand) subCmd = *cmd2;
628 if ( geom == subCmd->GetArg( 1 )) {
629 subCmd->SetObject( algo->GetID() );
630 subCmd->RemoveArgs();
631 addCmd->AddDependantCmd( subCmd );
636 else // ALGO was already created
638 // mesh.AddHypothesis(geom, ALGO ) --> mesh.GetMesh().AddHypothesis(geom, ALGO )
639 AddMeshAccess( addCmd );
640 // mesh.GetMesh().AddHypothesis(geom, ALGO ) ->
641 // mesh.GetMesh().AddHypothesis(geom, ALGO.GetAlgorithm() )
642 addCmd->SetArg( 2, addCmd->GetArg( 2 ) + ".GetAlgorithm()" );
646 // try to convert hypo addition like this:
647 // mesh.AddHypothesis(geom, HYPO ) --> HYPO = algo.Hypo()
648 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
650 Handle(_pyCommand) addCmd = *cmd;
651 const _pyID& hypID = addCmd->GetArg( 2 );
652 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
653 if ( hyp.IsNull() || hyp->IsAlgo() )
655 const _pyID& geom = addCmd->GetArg( 1 );
656 // find algo created on <geom> for this mesh
657 Handle(_pyHypothesis) algo = theGen->FindAlgo( geom, this->GetID(), hyp->GetType() );
658 //_pyID algoID = algo.IsNull() ? "" : algo->GetID();
659 if ( !algo.IsNull() && hyp->Addition2Creation( addCmd, this->GetID() )) // OK
661 addCmd->SetObject( algo->GetID() );
662 algo->GetCreationCmd()->AddDependantCmd( addCmd );
666 AddMeshAccess( addCmd );
670 // sm = mesh.GetSubMesh(geom, name) --> sm = mesh.GetMesh().GetSubMesh(geom, name)
671 for ( cmd = mySubmeshes.begin(); cmd != mySubmeshes.end(); ++cmd ) {
672 Handle(_pyCommand) subCmd = *cmd;
673 if ( subCmd->GetNbArgs() > 0 )
674 AddMeshAccess( subCmd );
676 myAddHypCmds.clear();
680 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
681 for ( ; hyp != myHypos.end(); ++hyp )
685 //================================================================================
687 * \brief _pyHypothesis constructor
688 * \param theCreationCmd -
690 //================================================================================
692 _pyHypothesis::_pyHypothesis(const Handle(_pyCommand)& theCreationCmd):
693 _pyObject( theCreationCmd )
695 myDim = myIsAlgo = /*myIsLocal = */myIsWrapped = myIsConverted = false;
698 //================================================================================
700 * \brief Creates algorithm or hypothesis
701 * \param theCreationCmd - The engine command creating a hypothesis
702 * \retval Handle(_pyHypothesis) - Result _pyHypothesis
704 //================================================================================
706 Handle(_pyHypothesis) _pyHypothesis::NewHypothesis( const Handle(_pyCommand)& theCreationCmd)
708 // theCreationCmd: CreateHypothesis( "theHypType", "theLibName" )
709 ASSERT (( theCreationCmd->GetMethod() == "CreateHypothesis"));
711 Handle(_pyHypothesis) hyp, algo;
714 const TCollection_AsciiString & hypTypeWithQuotes = theCreationCmd->GetArg( 1 );
715 if ( hypTypeWithQuotes.IsEmpty() )
718 TCollection_AsciiString hypType =
719 hypTypeWithQuotes.SubString( 2, hypTypeWithQuotes.Length() - 1 );
721 algo = new _pyAlgorithm( theCreationCmd );
722 hyp = new _pyHypothesis( theCreationCmd );
724 // 1D Regular_1D ----------
725 if ( hypType == "Regular_1D" ) {
727 algo->myCreationMethod = "Segment";
729 else if ( hypType == "LocalLength" ) {
731 hyp->myCreationMethod = "LocalLength";
732 hyp->myType = "Regular_1D";
733 hyp->myArgMethods.Append( "SetLength" );
735 else if ( hypType == "NumberOfSegments" ) {
736 hyp = new _pyNumberOfSegmentsHyp( theCreationCmd );
738 hyp->myCreationMethod = "NumberOfSegments";
739 hyp->myType = "Regular_1D";
740 hyp->myArgMethods.Append( "SetNumberOfSegments" );
741 hyp->myArgMethods.Append( "SetScaleFactor" );
743 else if ( hypType == "Arithmetic1D" ) {
744 hyp = new _pyComplexParamHypo( theCreationCmd );
746 hyp->myCreationMethod = "Arithmetic1D";
747 hyp->myType = "Regular_1D";
749 else if ( hypType == "StartEndLength" ) {
750 hyp = new _pyComplexParamHypo( theCreationCmd );
752 hyp->myCreationMethod = "StartEndLength";
753 hyp->myType = "Regular_1D";
755 else if ( hypType == "Deflection1D" ) {
757 hyp->myCreationMethod = "Deflection1D";
758 hyp->myArgMethods.Append( "SetDeflection" );
759 hyp->myType = "Regular_1D";
761 else if ( hypType == "Propagation" ) {
763 hyp->myCreationMethod = "Propagation";
764 hyp->myType = "Regular_1D";
766 else if ( hypType == "QuadraticMesh" ) {
768 hyp->myCreationMethod = "QuadraticMesh";
769 hyp->myType = "Regular_1D";
771 else if ( hypType == "AutomaticLength" ) {
773 hyp->myCreationMethod = "AutomaticLength";
774 hyp->myType = "Regular_1D";
775 hyp->myArgMethods.Append( "SetFineness");
777 // 1D Python_1D ----------
778 else if ( hypType == "Python_1D" ) {
780 algo->myCreationMethod = "Segment";
781 algo->myArgs.Append( "algo=smesh.PYTHON");
783 else if ( hypType == "PythonSplit1D" ) {
785 hyp->myCreationMethod = "PythonSplit1D";
786 hyp->myType = "Python_1D";
787 hyp->myArgMethods.Append( "SetNumberOfSegments");
788 hyp->myArgMethods.Append( "SetPythonLog10RatioFunction");
791 else if ( hypType == "MEFISTO_2D" ) {
793 algo->myCreationMethod = "Triangle";
795 else if ( hypType == "MaxElementArea" ) {
797 hyp->myCreationMethod = "MaxElementArea";
798 hyp->myType = "MEFISTO_2D";
799 hyp->myArgMethods.Append( "SetMaxElementArea");
801 else if ( hypType == "LengthFromEdges" ) {
803 hyp->myCreationMethod = "LengthFromEdges";
804 hyp->myType = "MEFISTO_2D";
806 else if ( hypType == "Quadrangle_2D" ) {
808 algo->myCreationMethod = "Quadrangle";
810 else if ( hypType == "QuadranglePreference" ) {
812 hyp->myCreationMethod = "QuadranglePreference";
813 hyp->myType = "Quadrangle_2D";
816 else if ( hypType == "NETGEN_3D") {
818 algo->myCreationMethod = "Tetrahedron";
819 algo->myArgs.Append( "algo=smesh.NETGEN" );
821 else if ( hypType == "MaxElementVolume") {
823 hyp->myCreationMethod = "MaxElementVolume";
824 hyp->myType = "NETGEN_3D";
825 hyp->myArgMethods.Append( "SetMaxElementVolume" );
827 else if ( hypType == "GHS3D_3D" ) {
829 algo->myCreationMethod = "Tetrahedron";
830 algo->myArgs.Append( "algo=smesh.GHS3D" );
832 else if ( hypType == "Hexa_3D" ) {
834 algo->myCreationMethod = "Hexahedron";
837 if ( algo->GetDim() ) {
838 algo->myType = hypType;
844 //================================================================================
846 * \brief Convert the command adding a hypothesis to mesh into a smesh command
847 * \param theCmd - The command like mesh.AddHypothesis( geom, hypo )
848 * \param theAlgo - The algo that can create this hypo
849 * \retval bool - false if the command cant be converted
851 //================================================================================
853 bool _pyHypothesis::Addition2Creation( const Handle(_pyCommand)& theCmd,
854 const _pyID& theMesh)
856 ASSERT(( theCmd->GetMethod() == "AddHypothesis" ));
858 if ( !IsWrappable( theMesh ))
865 // mesh.AddHypothesis(geom,hyp) --> hyp = theMesh.myCreationMethod(args)
866 theCmd->SetResultValue( GetID() );
867 theCmd->SetObject( theMesh );
868 theCmd->SetMethod( myCreationMethod );
870 theCmd->RemoveArgs();
871 for ( int i = 1; i <= myArgs.Length(); ++i ) {
872 if ( !myArgs( i ).IsEmpty() )
873 theCmd->SetArg( i, myArgs( i ));
875 theCmd->SetArg( i, "[]");
877 // set a new creation command
878 GetCreationCmd()->Clear();
879 SetCreationCmd( theCmd );
881 // clear commands setting arg values
882 list < Handle(_pyCommand) >::iterator argCmd = myArgCommands.begin();
883 for ( ; argCmd != myArgCommands.end(); ++argCmd )
888 // // set arg commands after hypo creation
889 // list<Handle(_pyCommand)>::iterator argCmd = myArgCommands.begin();
890 // for ( ; argCmd != myArgCommands.end(); ++argCmd )
891 // if ( !(*argCmd)->IsEmpty() && GetCommandNb() > (*argCmd)->GetOrderNb() )
892 // theGen->ExchangeCommands( GetCreationCmd(), *argCmd );
895 // set unknown arg commands after hypo creation
896 Handle(_pyCommand) afterCmd = myIsWrapped ? theCmd : GetCreationCmd();
897 list<Handle(_pyCommand)>::iterator cmd = myUnknownCommands.begin();
898 for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
899 afterCmd->AddDependantCmd( *cmd );
905 //================================================================================
907 * \brief Remember hypothesis parameter values
908 * \param theCommand - The called hypothesis method
910 //================================================================================
912 void _pyHypothesis::Process( const Handle(_pyCommand)& theCommand)
916 for ( int i = 1; i <= myArgMethods.Length(); ++i ) {
917 if ( myArgMethods( i ) == theCommand->GetMethod() ) {
918 while ( myArgs.Length() < i )
919 myArgs.Append( "[]" );
920 myArgs( i ) = theCommand->GetArg( 1 ); // arg value
921 myArgCommands.push_back( theCommand );
925 myUnknownCommands.push_back( theCommand );
928 //================================================================================
930 * \brief Finish conversion
932 //================================================================================
934 void _pyHypothesis::Flush()
937 // forget previous hypothesis modifications
938 myArgCommands.clear();
939 myUnknownCommands.clear();
943 //================================================================================
945 * \brief Remember hypothesis parameter values
946 * \param theCommand - The called hypothesis method
948 //================================================================================
950 void _pyComplexParamHypo::Process( const Handle(_pyCommand)& theCommand)
952 // ex: hyp.SetLength(start, 1)
953 // hyp.SetLength(end, 0)
954 ASSERT(( theCommand->GetMethod() == "SetLength" ));
955 ASSERT(( theCommand->GetArg( 2 ).IsIntegerValue() ));
956 int i = 2 - theCommand->GetArg( 2 ).IntegerValue();
957 while ( myArgs.Length() < i )
958 myArgs.Append( "[]" );
959 myArgs( i ) = theCommand->GetArg( 1 ); // arg value
960 myArgCommands.push_back( theCommand );
963 //================================================================================
965 * \brief additionally to Addition2Creation, clears SetDistrType() command
966 * \param theCmd - AddHypothesis() command
967 * \param theMesh - mesh to which a hypothesis is added
968 * \retval bool - convertion result
970 //================================================================================
972 bool _pyNumberOfSegmentsHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
973 const _pyID& theMesh)
975 if ( IsWrappable( theMesh ) && myArgs.Length() > 1 ) {
976 // scale factor (2-nd arg) is provided: clear SetDistrType(1) command
977 bool scaleDistrType = false;
978 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnknownCommands.rbegin();
979 for ( ; cmd != myUnknownCommands.rend(); ++cmd ) {
980 if ( (*cmd)->GetMethod() == "SetDistrType" ) {
981 if ( (*cmd)->GetArg( 1 ) == "1" ) {
982 scaleDistrType = true;
985 else if ( !scaleDistrType ) {
986 // distribution type changed: remove scale factor from args
987 myArgs.Remove( 2, myArgs.Length() );
993 return _pyHypothesis::Addition2Creation( theCmd, theMesh );
996 //================================================================================
998 * \brief remove repeated commands defining distribution
1000 //================================================================================
1002 void _pyNumberOfSegmentsHyp::Flush()
1004 const int nbCmdLists = 2;
1005 list<Handle(_pyCommand)> * cmds[nbCmdLists] = { &myArgCommands, &myUnknownCommands };
1006 for ( int i = 0; i < nbCmdLists; ++i ) {
1007 set<TCollection_AsciiString> uniqueMethods;
1008 list<Handle(_pyCommand)> & cmdList = *cmds[i];
1009 list<Handle(_pyCommand)>::reverse_iterator cmd = cmdList.rbegin();
1010 for ( ; cmd != cmdList.rend(); ++cmd ) {
1011 bool isNewInSet = uniqueMethods.insert( (*cmd)->GetMethod() ).second;
1019 //================================================================================
1021 * \brief _pyAlgorithm constructor
1022 * \param theCreationCmd - The command like "algo = smeshgen.CreateHypothesis(type,lib)"
1024 //================================================================================
1026 _pyAlgorithm::_pyAlgorithm(const Handle(_pyCommand)& theCreationCmd)
1027 : _pyHypothesis( theCreationCmd )
1032 //================================================================================
1034 * \brief Convert the command adding an algorithm to mesh
1035 * \param theCmd - The command like mesh.AddHypothesis( geom, algo )
1036 * \param theMesh - The mesh needing this algo
1037 * \retval bool - false if the command cant be converted
1039 //================================================================================
1041 bool _pyAlgorithm::Addition2Creation( const Handle(_pyCommand)& theCmd,
1042 const _pyID& theMeshID)
1044 if ( IsWrappable( theMeshID )) {
1046 myGeom = theCmd->GetArg( 1 );
1048 // mesh.AddHypothesis(geom,algo) --> theMeshID.myCreationMethod()
1049 if ( _pyHypothesis::Addition2Creation( theCmd, theMeshID )) {
1050 theGen->SetAccessorMethod( GetID(), "GetAlgorithm()" );
1057 //================================================================================
1059 * \brief Return starting position of a part of python command
1060 * \param thePartIndex - The index of command part
1061 * \retval int - Part position
1063 //================================================================================
1065 int _pyCommand::GetBegPos( int thePartIndex )
1069 if ( myBegPos.Length() < thePartIndex )
1071 return myBegPos( thePartIndex );
1074 //================================================================================
1076 * \brief Store starting position of a part of python command
1077 * \param thePartIndex - The index of command part
1078 * \param thePosition - Part position
1080 //================================================================================
1082 void _pyCommand::SetBegPos( int thePartIndex, int thePosition )
1084 while ( myBegPos.Length() < thePartIndex )
1085 myBegPos.Append( UNKNOWN );
1086 myBegPos( thePartIndex ) = thePosition;
1089 //================================================================================
1091 * \brief Return substring of python command looking like ResultValue = Obj.Meth()
1092 * \retval const TCollection_AsciiString & - ResultValue substring
1094 //================================================================================
1096 const TCollection_AsciiString & _pyCommand::GetResultValue()
1098 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
1100 int begPos = myString.Location( "=", 1, Length() );
1102 myRes = GetWord( myString, begPos, false );
1105 SetBegPos( RESULT_IND, begPos );
1110 //================================================================================
1112 * \brief Return substring of python command looking like ResVal = Object.Meth()
1113 * \retval const TCollection_AsciiString & - Object substring
1115 //================================================================================
1117 const TCollection_AsciiString & _pyCommand::GetObject()
1119 if ( GetBegPos( OBJECT_IND ) == UNKNOWN )
1122 int begPos = GetBegPos( RESULT_IND ) + myRes.Length();
1124 begPos = myString.Location( "=", 1, Length() ) + 1;
1126 myObj = GetWord( myString, begPos, true );
1127 SetBegPos( OBJECT_IND, begPos );
1133 //================================================================================
1135 * \brief Return substring of python command looking like ResVal = Obj.Method()
1136 * \retval const TCollection_AsciiString & - Method substring
1138 //================================================================================
1140 const TCollection_AsciiString & _pyCommand::GetMethod()
1142 if ( GetBegPos( METHOD_IND ) == UNKNOWN )
1145 int begPos = GetBegPos( OBJECT_IND ) + myObj.Length();
1146 bool forward = true;
1148 begPos = myString.Location( "(", 1, Length() ) - 1;
1152 myMeth = GetWord( myString, begPos, forward );
1153 SetBegPos( METHOD_IND, begPos );
1159 //================================================================================
1161 * \brief Return substring of python command looking like ResVal = Obj.Meth(Arg1,...)
1162 * \retval const TCollection_AsciiString & - Arg<index> substring
1164 //================================================================================
1166 const TCollection_AsciiString & _pyCommand::GetArg( int index )
1168 if ( GetBegPos( ARG1_IND ) == UNKNOWN )
1171 int begPos = GetBegPos( METHOD_IND ) + myMeth.Length();
1173 begPos = myString.Location( "(", 1, Length() ) + 1;
1175 int i = 0, prevLen = 0;
1176 while ( begPos != EMPTY ) {
1178 // check if we are looking at the closing parenthesis
1179 while ( begPos <= Length() && isspace( myString.Value( begPos )))
1181 if ( begPos > Length() || myString.Value( begPos ) == ')' )
1183 myArgs.Append( GetWord( myString, begPos, true, true ));
1184 SetBegPos( ARG1_IND + i, begPos );
1185 prevLen = myArgs.Last().Length();
1187 myArgs.Remove( myArgs.Length() ); // no more args
1191 if ( myArgs.Length() < index )
1192 return theEmptyString;
1193 return myArgs( index );
1196 //================================================================================
1198 * \brief Check if char is a word part
1199 * \param c - The character to check
1200 * \retval bool - The check result
1202 //================================================================================
1204 static inline bool isWord(const char c, const bool dotIsWord)
1207 !isspace(c) && c != ',' && c != '=' && c != ')' && c != '(' && ( dotIsWord || c != '.');
1210 //================================================================================
1212 * \brief Looks for a word in the string and returns word's beginning
1213 * \param theString - The input string
1214 * \param theStartPos - The position to start the search, returning word's beginning
1215 * \param theForward - The search direction
1216 * \retval TCollection_AsciiString - The found word
1218 //================================================================================
1220 TCollection_AsciiString _pyCommand::GetWord( const TCollection_AsciiString & theString,
1222 const bool theForward,
1223 const bool dotIsWord )
1225 int beg = theStartPos, end = theStartPos;
1226 theStartPos = EMPTY;
1227 if ( beg < 1 || beg > theString.Length() )
1228 return theEmptyString;
1230 if ( theForward ) { // search forward
1232 while ( beg <= theString.Length() && !isWord( theString.Value( beg ), dotIsWord))
1234 if ( beg > theString.Length() )
1235 return theEmptyString; // no word found
1238 while ( end <= theString.Length() && isWord( theString.Value( end ), dotIsWord))
1242 else { // search backward
1244 while ( end > 0 && !isWord( theString.Value( end ), dotIsWord))
1247 return theEmptyString; // no word found
1249 while ( beg > 0 && isWord( theString.Value( beg ), dotIsWord))
1254 //cout << theString << " ---- " << beg << " - " << end << endl;
1255 return theString.SubString( beg, end );
1258 //================================================================================
1260 * \brief Look for position where not space char is
1261 * \param theString - The string
1262 * \param thePos - The position to search from and which returns result
1263 * \retval bool - false if there are only space after thePos in theString
1267 //================================================================================
1269 bool _pyCommand::SkipSpaces( const TCollection_AsciiString & theString, int & thePos )
1271 if ( thePos < 1 || thePos > theString.Length() )
1274 while ( thePos <= theString.Length() && isspace( theString.Value( thePos )))
1277 return thePos <= theString.Length();
1280 //================================================================================
1282 * \brief Modify a part of the command
1283 * \param thePartIndex - The index of the part
1284 * \param thePart - The new part string
1285 * \param theOldPart - The old part
1287 //================================================================================
1289 void _pyCommand::SetPart(int thePartIndex, const TCollection_AsciiString& thePart,
1290 TCollection_AsciiString& theOldPart)
1292 int pos = GetBegPos( thePartIndex );
1293 if ( pos <= Length() && theOldPart != thePart)
1295 TCollection_AsciiString seperator;
1297 pos = GetBegPos( thePartIndex + 1 );
1298 if ( pos < 1 ) return;
1299 switch ( thePartIndex ) {
1300 case RESULT_IND: seperator = " = "; break;
1301 case OBJECT_IND: seperator = "."; break;
1302 case METHOD_IND: seperator = "()"; break;
1306 myString.Remove( pos, theOldPart.Length() );
1307 if ( !seperator.IsEmpty() )
1308 myString.Insert( pos , seperator );
1309 myString.Insert( pos, thePart );
1310 // update starting positions of the following parts
1311 int posDelta = thePart.Length() + seperator.Length() - theOldPart.Length();
1312 for ( int i = thePartIndex + 1; i <= myBegPos.Length(); ++i ) {
1313 if ( myBegPos( i ) > 0 )
1314 myBegPos( i ) += posDelta;
1316 theOldPart = thePart;
1320 //================================================================================
1322 * \brief Set agrument
1323 * \param index - The argument index, it counts from 1
1324 * \param theArg - The argument string
1326 //================================================================================
1328 void _pyCommand::SetArg( int index, const TCollection_AsciiString& theArg)
1331 int argInd = ARG1_IND + index - 1;
1332 int pos = GetBegPos( argInd );
1333 if ( pos < 1 ) // no index-th arg exist, append inexistent args
1335 // find a closing parenthesis
1336 if ( int lastArgInd = GetNbArgs() ) {
1337 pos = GetBegPos( ARG1_IND + lastArgInd - 1 ) + GetArg( lastArgInd ).Length();
1338 while ( pos > 0 && pos <= Length() && myString.Value( pos ) != ')' )
1343 while ( pos > 0 && myString.Value( pos ) != ')' )
1346 if ( pos < 1 || myString.Value( pos ) != ')' ) { // no parentheses at all
1350 while ( myArgs.Length() < index ) {
1351 if ( myArgs.Length() )
1352 myString.Insert( pos++, "," );
1353 myArgs.Append("None");
1354 myString.Insert( pos, myArgs.Last() );
1355 SetBegPos( ARG1_IND + myArgs.Length() - 1, pos );
1356 pos += myArgs.Last().Length();
1359 SetPart( argInd, theArg, myArgs( index ));
1362 //================================================================================
1364 * \brief Empty arg list
1366 //================================================================================
1368 void _pyCommand::RemoveArgs()
1370 if ( int pos = myString.Location( '(', 1, Length() ))
1371 myString.Trunc( pos );
1374 if ( myBegPos.Length() >= ARG1_IND )
1375 myBegPos.Remove( ARG1_IND, myBegPos.Length() );
1378 //================================================================================
1380 * \brief Set dependent commands after this one
1382 //================================================================================
1384 bool _pyCommand::SetDependentCmdsAfter() const
1386 bool orderChanged = false;
1387 list< Handle(_pyCommand)>::const_reverse_iterator cmd = myDependentCmds.rbegin();
1388 for ( ; cmd != myDependentCmds.rend(); ++cmd ) {
1389 if ( (*cmd)->GetOrderNb() < GetOrderNb() ) {
1390 orderChanged = true;
1391 theGen->SetCommandAfter( *cmd, this );
1392 (*cmd)->SetDependentCmdsAfter();
1395 return orderChanged;
1397 //================================================================================
1399 * \brief Insert accessor method after theObjectID
1400 * \param theObjectID - id of the accessed object
1401 * \param theAcsMethod - name of the method giving access to the object
1402 * \retval bool - false if theObjectID is not found in the command string
1404 //================================================================================
1406 bool _pyCommand::AddAccessorMethod( _pyID theObjectID, const char* theAcsMethod )
1408 if ( !theAcsMethod )
1410 // start object search from the object, i.e. ignore result
1412 int beg = GetBegPos( OBJECT_IND );
1413 if ( beg < 1 || beg > Length() )
1415 while (( beg = myString.Location( theObjectID, beg, Length() )))
1417 // check that theObjectID is not just a part of a longer ID
1418 int afterEnd = beg + theObjectID.Length();
1419 Standard_Character c = myString.Value( afterEnd );
1420 if ( !isalnum( c ) && c != ':' ) {
1422 int oldLen = Length();
1423 myString.Insert( afterEnd, (char*) theAcsMethod );
1424 myString.Insert( afterEnd, "." );
1425 // update starting positions of the parts following the modified one
1426 int posDelta = Length() - oldLen;
1427 for ( int i = 1; i <= myBegPos.Length(); ++i ) {
1428 if ( myBegPos( i ) > afterEnd )
1429 myBegPos( i ) += posDelta;
1433 beg = afterEnd; // is a part - next search
1438 //================================================================================
1440 * \brief Return method name giving access to an interaface object wrapped by python class
1441 * \retval const char* - method name
1443 //================================================================================
1445 const char* _pyObject::AccessorMethod() const