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 (_pyMeshEditor ,_pyObject);
50 IMPLEMENT_STANDARD_HANDLE (_pyHypothesis ,_pyObject);
51 IMPLEMENT_STANDARD_HANDLE (_pyAlgorithm ,_pyHypothesis);
52 IMPLEMENT_STANDARD_HANDLE (_pyComplexParamHypo,_pyHypothesis);
53 IMPLEMENT_STANDARD_HANDLE (_pyNumberOfSegmentsHyp,_pyHypothesis);
55 IMPLEMENT_STANDARD_RTTIEXT(_pyObject ,Standard_Transient);
56 IMPLEMENT_STANDARD_RTTIEXT(_pyCommand ,Standard_Transient);
57 IMPLEMENT_STANDARD_RTTIEXT(_pyGen ,_pyObject);
58 IMPLEMENT_STANDARD_RTTIEXT(_pyMesh ,_pyObject);
59 IMPLEMENT_STANDARD_RTTIEXT(_pyMeshEditor ,_pyObject);
60 IMPLEMENT_STANDARD_RTTIEXT(_pyHypothesis ,_pyObject);
61 IMPLEMENT_STANDARD_RTTIEXT(_pyAlgorithm ,_pyHypothesis);
62 IMPLEMENT_STANDARD_RTTIEXT(_pyComplexParamHypo,_pyHypothesis);
63 IMPLEMENT_STANDARD_RTTIEXT(_pyNumberOfSegmentsHyp,_pyHypothesis);
64 IMPLEMENT_STANDARD_RTTIEXT(_pyLayerDistributionHypo,_pyHypothesis);
65 IMPLEMENT_STANDARD_RTTIEXT(_pySegmentLengthAroundVertexHyp,_pyHypothesis);
68 using SMESH::TPythonDump;
71 * \brief Container of commands into which the initial script is split.
72 * It also contains data coresponding to SMESH_Gen contents
74 static Handle(_pyGen) theGen;
76 static TCollection_AsciiString theEmptyString;
78 //#define DUMP_CONVERSION
80 #if !defined(_DEBUG_) && defined(DUMP_CONVERSION)
81 #undef DUMP_CONVERSION
87 //================================================================================
89 * \brief Set of TCollection_AsciiString initialized by C array of C strings
91 //================================================================================
93 struct TStringSet: public set<TCollection_AsciiString>
96 * \brief Filling. The last string must be ""
98 void Insert(const char* names[]) {
99 for ( int i = 0; names[i][0] ; ++i )
100 insert( (char*) names[i] );
103 * \brief Check if a string is in
105 bool Contains(const TCollection_AsciiString& name ) {
106 return find( name ) != end();
111 //================================================================================
113 * \brief Convert python script using commands of smesh.py
114 * \param theScript - Input script
115 * \retval TCollection_AsciiString - Convertion result
117 * Class SMESH_2smeshpy declared in SMESH_PythonDump.hxx
119 //================================================================================
121 TCollection_AsciiString
122 SMESH_2smeshpy::ConvertScript(const TCollection_AsciiString& theScript,
123 Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod)
125 theGen = new _pyGen( theEntry2AccessorMethod );
127 // split theScript into separate commands
128 int from = 1, end = theScript.Length(), to;
129 while ( from < end && ( to = theScript.Location( "\n", from, end )))
132 // cut out and store a command
133 theGen->AddCommand( theScript.SubString( from, to - 1 ));
138 #ifdef DUMP_CONVERSION
139 cout << endl << " ######## RESULT ######## " << endl<< endl;
141 // reorder commands after conversion
142 list< Handle(_pyCommand) >::iterator cmd;
145 orderChanges = false;
146 for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
147 if ( (*cmd)->SetDependentCmdsAfter() )
149 } while ( orderChanges );
151 // concat commands back into a script
152 TCollection_AsciiString aScript;
153 for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
155 #ifdef DUMP_CONVERSION
156 cout << "## COM " << (*cmd)->GetOrderNb() << ": "<< (*cmd)->GetString() << endl;
158 if ( !(*cmd)->IsEmpty() ) {
160 aScript += (*cmd)->GetString();
170 //================================================================================
172 * \brief _pyGen constructor
174 //================================================================================
176 _pyGen::_pyGen(Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod)
177 : _pyObject( new _pyCommand( TPythonDump::SMESHGenName(), 0 )),
178 myID2AccessorMethod( theEntry2AccessorMethod )
181 myHasPattern = false;
182 // make that GetID() to return TPythonDump::SMESHGenName()
183 GetCreationCmd()->GetString() += "=";
186 //================================================================================
188 * \brief name of SMESH_Gen in smesh.py
190 //================================================================================
192 const char* _pyGen::AccessorMethod() const
194 return SMESH_2smeshpy::GenName();
197 //================================================================================
199 * \brief Convert a command using a specific converter
200 * \param theCommand - the command to convert
202 //================================================================================
204 Handle(_pyCommand) _pyGen::AddCommand( const TCollection_AsciiString& theCommand)
206 // store theCommand in the sequence
207 myCommands.push_back( new _pyCommand( theCommand, ++myNbCommands ));
209 Handle(_pyCommand) aCommand = myCommands.back();
210 #ifdef DUMP_CONVERSION
211 cout << "## COM " << myNbCommands << ": "<< aCommand->GetString() << endl;
214 _pyID objID = aCommand->GetObject();
216 if ( objID.IsEmpty() )
220 if ( objID == this->GetID() ) {
221 this->Process( aCommand );
224 // SMESH_Mesh method?
225 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( objID );
226 if ( id_mesh != myMeshes.end() ) {
227 if ( aCommand->GetMethod() == "GetMeshEditor" ) { // MeshEditor creation
228 _pyID editorID = aCommand->GetResultValue();
229 Handle(_pyMeshEditor) editor = new _pyMeshEditor( aCommand );
230 myMeshEditors.insert( make_pair( editorID, editor ));
233 id_mesh->second->Process( aCommand );
236 // SMESH_MeshEditor method?
237 map< _pyID, Handle(_pyMeshEditor) >::iterator id_editor = myMeshEditors.find( objID );
238 if ( id_editor != myMeshEditors.end() ) {
239 id_editor->second->Process( aCommand );
242 // SMESH_Hypothesis method?
243 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
244 for ( ; hyp != myHypos.end(); ++hyp )
245 if ( !(*hyp)->IsAlgo() && objID == (*hyp)->GetID() ) {
246 (*hyp)->Process( aCommand );
250 // Add access to a wrapped mesh
251 AddMeshAccessorMethod( aCommand );
253 // Add access to a wrapped algorithm
254 // AddAlgoAccessorMethod( aCommand ); // ??? what if algo won't be wrapped at all ???
256 // PAL12227. PythonDump was not updated at proper time; result is
257 // aCriteria.append(SMESH.Filter.Criterion(17,26,0,'L1',26,25,1e-07,SMESH.EDGE,-1))
258 // TypeError: __init__() takes exactly 11 arguments (10 given)
259 char wrongCommand[] = "SMESH.Filter.Criterion(";
260 if ( int beg = theCommand.Location( wrongCommand, 1, theCommand.Length() ))
262 _pyCommand tmpCmd( theCommand.SubString( beg, theCommand.Length() ), -1);
263 // there must be 10 arguments, 5-th arg ThresholdID is missing,
264 const int wrongNbArgs = 9, missingArg = 5;
265 if ( tmpCmd.GetNbArgs() == wrongNbArgs )
267 for ( int i = wrongNbArgs; i > missingArg; --i )
268 tmpCmd.SetArg( i + 1, tmpCmd.GetArg( i ));
269 tmpCmd.SetArg( missingArg, "''");
270 aCommand->GetString().Trunc( beg - 1 );
271 aCommand->GetString() += tmpCmd.GetString();
277 //================================================================================
279 * \brief Convert the command or remember it for later conversion
280 * \param theCommand - The python command calling a method of SMESH_Gen
282 //================================================================================
284 void _pyGen::Process( const Handle(_pyCommand)& theCommand )
286 // there are methods to convert:
287 // CreateMesh( shape )
288 // Concatenate( [mesh1, ...], ... )
289 // CreateHypothesis( theHypType, theLibName )
290 // Compute( mesh, geom )
292 if ( theCommand->GetMethod() == "CreateMesh" ||
293 theCommand->GetMethod() == "CreateEmptyMesh" ||
294 theCommand->GetMethod() == "CreateMeshesFromUNV" ||
295 theCommand->GetMethod() == "CreateMeshesFromSTL")
297 Handle(_pyMesh) mesh = new _pyMesh( theCommand );
298 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
302 if(theCommand->GetMethod() == "CreateMeshesFromMED")
304 for(int ind = 0;ind<theCommand->GetNbResultValues();ind++)
306 Handle(_pyMesh) mesh = new _pyMesh( theCommand, theCommand->GetResultValue(ind));
307 myMeshes.insert( make_pair( theCommand->GetResultValue(ind), mesh ));
311 // CreateHypothesis()
312 if ( theCommand->GetMethod() == "CreateHypothesis" )
314 myHypos.push_back( _pyHypothesis::NewHypothesis( theCommand ));
318 // smeshgen.Compute( mesh, geom ) --> mesh.Compute()
319 if ( theCommand->GetMethod() == "Compute" )
321 const _pyID& meshID = theCommand->GetArg( 1 );
322 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( meshID );
323 if ( id_mesh != myMeshes.end() ) {
324 theCommand->SetObject( meshID );
325 theCommand->RemoveArgs();
326 id_mesh->second->Flush();
331 // leave only one smeshgen.GetPattern() in the script
332 if ( theCommand->GetMethod() == "GetPattern" ) {
333 if ( myHasPattern ) {
340 // Concatenate( [mesh1, ...], ... )
341 if ( theCommand->GetMethod() == "Concatenate" )
343 AddMeshAccessorMethod( theCommand );
346 // Replace name of SMESH_Gen
348 // names of SMESH_Gen methods fully equal to methods defined in smesh.py
349 static TStringSet smeshpyMethods;
350 if ( smeshpyMethods.empty() ) {
351 const char * names[] =
352 { "SetEmbeddedMode","IsEmbeddedMode","SetCurrentStudy","GetCurrentStudy",
353 "GetPattern","GetSubShapesId",
354 "" }; // <- mark of array end
355 smeshpyMethods.Insert( names );
357 if ( smeshpyMethods.Contains( theCommand->GetMethod() ))
358 // smeshgen.Method() --> smesh.Method()
359 theCommand->SetObject( SMESH_2smeshpy::SmeshpyName() );
361 // smeshgen.Method() --> smesh.smesh.Method()
362 theCommand->SetObject( SMESH_2smeshpy::GenName() );
365 //================================================================================
367 * \brief Convert the remembered commands
369 //================================================================================
373 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.begin();
374 for ( ; id_mesh != myMeshes.end(); ++id_mesh )
375 if ( ! id_mesh->second.IsNull() )
376 id_mesh->second->Flush();
378 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
379 for ( ; hyp != myHypos.end(); ++hyp )
380 if ( !hyp->IsNull() ) {
382 // smeshgen.CreateHypothesis() --> smesh.smesh.CreateHypothesis()
383 if ( !(*hyp)->IsWrapped() )
384 (*hyp)->GetCreationCmd()->SetObject( SMESH_2smeshpy::GenName() );
388 //================================================================================
390 * \brief Add access method to mesh that is an argument
391 * \param theCmd - command to add access method
392 * \retval bool - true if added
394 //================================================================================
396 bool _pyGen::AddMeshAccessorMethod( Handle(_pyCommand) theCmd ) const
399 map< _pyID, Handle(_pyMesh) >::const_iterator id_mesh = myMeshes.begin();
400 for ( ; id_mesh != myMeshes.end(); ++id_mesh ) {
401 if ( theCmd->AddAccessorMethod( id_mesh->first, id_mesh->second->AccessorMethod() ))
407 //================================================================================
409 * \brief Add access method to algo that is an object or an argument
410 * \param theCmd - command to add access method
411 * \retval bool - true if added
413 //================================================================================
415 bool _pyGen::AddAlgoAccessorMethod( Handle(_pyCommand) theCmd ) const
418 list< Handle(_pyHypothesis) >::const_iterator hyp = myHypos.begin();
419 for ( ; hyp != myHypos.end(); ++hyp ) {
420 if ( (*hyp)->IsAlgo() && /*(*hyp)->IsWrapped() &&*/
421 theCmd->AddAccessorMethod( (*hyp)->GetID(), (*hyp)->AccessorMethod() ))
427 //================================================================================
429 * \brief Find hypothesis by ID (entry)
430 * \param theHypID - The hypothesis ID
431 * \retval Handle(_pyHypothesis) - The found hypothesis
433 //================================================================================
435 Handle(_pyHypothesis) _pyGen::FindHyp( const _pyID& theHypID )
437 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
438 for ( ; hyp != myHypos.end(); ++hyp )
439 if ( !hyp->IsNull() && theHypID == (*hyp)->GetID() )
441 return Handle(_pyHypothesis)();
444 //================================================================================
446 * \brief Find algorithm the created algorithm
447 * \param theGeom - The shape ID the algorithm was created on
448 * \param theMesh - The mesh ID that created the algorithm
449 * \param dim - The algo dimension
450 * \retval Handle(_pyHypothesis) - The found algo
452 //================================================================================
454 Handle(_pyHypothesis) _pyGen::FindAlgo( const _pyID& theGeom, const _pyID& theMesh,
455 const Handle(_pyHypothesis)& theHypothesis )
457 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
458 for ( ; hyp != myHypos.end(); ++hyp )
459 if ( !hyp->IsNull() &&
461 theHypothesis->CanBeCreatedBy( (*hyp)->GetAlgoType() ) &&
462 (*hyp)->GetGeom() == theGeom &&
463 (*hyp)->GetMesh() == theMesh )
468 //================================================================================
470 * \brief Change order of commands in the script
471 * \param theCmd1 - One command
472 * \param theCmd2 - Another command
474 //================================================================================
476 void _pyGen::ExchangeCommands( Handle(_pyCommand) theCmd1, Handle(_pyCommand) theCmd2 )
478 list< Handle(_pyCommand) >::iterator pos1, pos2;
479 pos1 = find( myCommands.begin(), myCommands.end(), theCmd1 );
480 pos2 = find( myCommands.begin(), myCommands.end(), theCmd2 );
481 myCommands.insert( pos1, theCmd2 );
482 myCommands.insert( pos2, theCmd1 );
483 myCommands.erase( pos1 );
484 myCommands.erase( pos2 );
486 int nb1 = theCmd1->GetOrderNb();
487 theCmd1->SetOrderNb( theCmd2->GetOrderNb() );
488 theCmd2->SetOrderNb( nb1 );
489 // cout << "BECOME " << theCmd1->GetOrderNb() << "\t" << theCmd1->GetString() << endl
490 // << "BECOME " << theCmd2->GetOrderNb() << "\t" << theCmd2->GetString() << endl << endl;
493 //================================================================================
495 * \brief Set one command after the other
496 * \param theCmd - Command to move
497 * \param theAfterCmd - Command ater which to insert the first one
499 //================================================================================
501 void _pyGen::SetCommandAfter( Handle(_pyCommand) theCmd, Handle(_pyCommand) theAfterCmd )
504 //cout << "SET\t" << theAfterCmd->GetString() << endl << "BEFORE\t" << theCmd->GetString() << endl<<endl;
506 list< Handle(_pyCommand) >::iterator pos;
507 pos = find( myCommands.begin(), myCommands.end(), theCmd );
508 myCommands.erase( pos );
509 pos = find( myCommands.begin(), myCommands.end(), theAfterCmd );
510 myCommands.insert( ++pos, theCmd );
513 for ( pos = myCommands.begin(); pos != myCommands.end(); ++pos)
514 (*pos)->SetOrderNb( i++ );
517 //================================================================================
519 * \brief Set method to access to object wrapped with python class
520 * \param theID - The wrapped object entry
521 * \param theMethod - The accessor method
523 //================================================================================
525 void _pyGen::SetAccessorMethod(const _pyID& theID, const char* theMethod )
527 myID2AccessorMethod.Bind( theID, (char*) theMethod );
530 //================================================================================
532 * \brief Find out type of geom group
533 * \param grpID - The geom group entry
534 * \retval int - The type
536 //================================================================================
538 static bool sameGroupType( const _pyID& grpID,
539 const TCollection_AsciiString& theType)
541 // define group type as smesh.Mesh.Group() does
543 SALOMEDS::Study_var study = SMESH_Gen_i::GetSMESHGen()->GetCurrentStudy();
544 SALOMEDS::SObject_var aSObj = study->FindObjectID( grpID.ToCString() );
545 if ( !aSObj->_is_nil() ) {
546 GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow( aSObj->GetObject() );
547 if ( !aGeomObj->_is_nil() ) {
548 switch ( aGeomObj->GetShapeType() ) {
549 case GEOM::VERTEX: type = SMESH::NODE; break;
550 case GEOM::EDGE: type = SMESH::EDGE; break;
551 case GEOM::FACE: type = SMESH::FACE; break;
553 case GEOM::SHELL: type = SMESH::VOLUME; break;
554 case GEOM::COMPOUND: {
555 GEOM::GEOM_Gen_ptr aGeomGen = SMESH_Gen_i::GetSMESHGen()->GetGeomEngine();
556 if ( !aGeomGen->_is_nil() ) {
557 GEOM::GEOM_IGroupOperations_var aGrpOp =
558 aGeomGen->GetIGroupOperations( study->StudyId() );
559 if ( !aGrpOp->_is_nil() ) {
560 switch ( aGrpOp->GetType( aGeomObj )) {
561 case TopAbs_VERTEX: type = SMESH::NODE; break;
562 case TopAbs_EDGE: type = SMESH::EDGE; break;
563 case TopAbs_FACE: type = SMESH::FACE; break;
564 case TopAbs_SOLID: type = SMESH::VOLUME; break;
575 MESSAGE("Type of the group " << grpID << " not found");
578 if ( theType.IsIntegerValue() )
579 return type == theType.IntegerValue();
582 case SMESH::NODE: return theType.Location( "NODE", 1, theType.Length() );
583 case SMESH::EDGE: return theType.Location( "EDGE", 1, theType.Length() );
584 case SMESH::FACE: return theType.Location( "FACE", 1, theType.Length() );
585 case SMESH::VOLUME: return theType.Location( "VOLUME", 1, theType.Length() );
591 //================================================================================
594 * \param theCreationCmd -
596 //================================================================================
598 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd):
599 _pyObject(theCreationCmd), myHasEditor(false)
601 // convert my creation command
602 Handle(_pyCommand) creationCmd = GetCreationCmd();
603 TCollection_AsciiString str = creationCmd->GetMethod();
605 creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
606 if(str != "CreateMeshesFromUNV" &&
607 str != "CreateMeshesFromMED" &&
608 str != "CreateMeshesFromSTL")
609 creationCmd->SetMethod( "Mesh" );
611 theGen->SetAccessorMethod( GetID(), "GetMesh()" );
614 //================================================================================
617 * \param theCreationCmd -
619 //================================================================================
620 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd, const TCollection_AsciiString& id):
621 _pyObject(theCreationCmd), myHasEditor(false)
623 // convert my creation command
624 Handle(_pyCommand) creationCmd = GetCreationCmd();
625 creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
626 theGen->SetAccessorMethod( id, "GetMesh()" );
629 //================================================================================
631 * \brief Convert a IDL API command of SMESH::Mesh to a method call of python Mesh
632 * \param theCommand - Engine method called for this mesh
634 //================================================================================
636 void _pyMesh::Process( const Handle(_pyCommand)& theCommand )
638 // some methods of SMESH_Mesh interface needs special conversion
639 // to methods of Mesh python class
641 // 1. GetSubMesh(geom, name) + AddHypothesis(geom, algo)
642 // --> in Mesh_Algorithm.Create(mesh, geom, hypo, so)
643 // 2. AddHypothesis(geom, hyp)
644 // --> in Mesh_Algorithm.Hypothesis(hyp, args, so)
645 // 3. CreateGroupFromGEOM(type, name, grp)
646 // --> in Mesh.Group(grp, name="")
647 // 4. ExportToMED(f, auto_groups, version)
648 // --> in Mesh.ExportMED( f, auto_groups, version )
651 const TCollection_AsciiString method = theCommand->GetMethod();
652 // ----------------------------------------------------------------------
653 if ( method == "GetSubMesh" ) {
654 mySubmeshes.push_back( theCommand );
656 // ----------------------------------------------------------------------
657 else if ( method == "AddHypothesis" ) { // mesh.AddHypothesis(geom, HYPO )
658 myAddHypCmds.push_back( theCommand );
660 const _pyID& hypID = theCommand->GetArg( 2 );
661 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
662 if ( !hyp.IsNull() ) {
663 myHypos.push_back( hyp );
664 if ( hyp->GetMesh().IsEmpty() )
665 hyp->SetMesh( this->GetID() );
668 // ----------------------------------------------------------------------
669 else if ( method == "CreateGroupFromGEOM" ) {// (type, name, grp)
670 _pyID grp = theCommand->GetArg( 3 );
671 if ( sameGroupType( grp, theCommand->GetArg( 1 )) ) { // --> Group(grp)
672 theCommand->SetMethod( "Group" );
673 theCommand->RemoveArgs();
674 theCommand->SetArg( 1, grp );
677 AddMeshAccess( theCommand );
680 // ----------------------------------------------------------------------
681 else if ( method == "ExportToMED" ) { // ExportToMED() --> ExportMED()
682 theCommand->SetMethod( "ExportMED" );
684 // ----------------------------------------------------------------------
685 else if ( method == "CreateGroup" ) { // CreateGroup() --> CreateEmptyGroup()
686 theCommand->SetMethod( "CreateEmptyGroup" );
688 // ----------------------------------------------------------------------
689 else if ( method == "RemoveHypothesis" ) // (geom, hyp)
691 _pyID hypID = theCommand->GetArg( 2 );
693 // check if this mesh still has corresponding addition command
694 bool hasAddCmd = false;
695 list< Handle(_pyCommand) >::iterator cmd = myAddHypCmds.begin();
696 while ( cmd != myAddHypCmds.end() )
698 // AddHypothesis(geom, hyp)
699 if ( hypID == (*cmd)->GetArg( 2 )) { // erase both (add and remove) commands
702 cmd = myAddHypCmds.erase( cmd );
709 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
710 if ( ! hasAddCmd && hypID.Length() != 0 ) { // hypo addition already wrapped
711 // RemoveHypothesis(geom, hyp) --> RemoveHypothesis( hyp, geom=0 )
712 _pyID geom = theCommand->GetArg( 1 );
713 theCommand->RemoveArgs();
714 theCommand->SetArg( 1, hypID );
715 if ( geom != GetGeom() )
716 theCommand->SetArg( 2, geom );
718 // remove hyp from myHypos
719 myHypos.remove( hyp );
721 // add accessor method if necessary
724 if ( NeedMeshAccess( theCommand ))
725 // apply theCommand to the mesh wrapped by smeshpy mesh
726 AddMeshAccess( theCommand );
730 //================================================================================
732 * \brief Return True if addition of accesor method is needed
734 //================================================================================
736 bool _pyMesh::NeedMeshAccess( const Handle(_pyCommand)& theCommand )
738 // names of SMESH_Mesh methods fully equal to methods of class Mesh, so
739 // no conversion is needed for them at all:
740 static TStringSet sameMethods;
741 if ( sameMethods.empty() ) {
742 const char * names[] =
743 { "ExportDAT","ExportUNV","ExportSTL", "RemoveGroup","RemoveGroupWithContents",
744 "GetGroups","UnionGroups","IntersectGroups","CutGroups","GetLog","GetId","ClearLog",
745 "GetStudyId","HasDuplicatedGroupNamesMED","GetMEDMesh","NbNodes","NbElements",
746 "NbEdges","NbEdgesOfOrder","NbFaces","NbFacesOfOrder","NbTriangles",
747 "NbTrianglesOfOrder","NbQuadrangles","NbQuadranglesOfOrder","NbPolygons","NbVolumes",
748 "NbVolumesOfOrder","NbTetras","NbTetrasOfOrder","NbHexas","NbHexasOfOrder",
749 "NbPyramids","NbPyramidsOfOrder","NbPrisms","NbPrismsOfOrder","NbPolyhedrons",
750 "NbSubMesh","GetElementsId","GetElementsByType","GetNodesId","GetElementType",
751 "GetSubMeshElementsId","GetSubMeshNodesId","GetSubMeshElementType","Dump","GetNodeXYZ",
752 "GetNodeInverseElements","GetShapeID","GetShapeIDForElem","GetElemNbNodes",
753 "GetElemNode","IsMediumNode","IsMediumNodeOfAnyElem","ElemNbEdges","ElemNbFaces",
754 "IsPoly","IsQuadratic","BaryCenter","GetHypothesisList", "SetAutoColor", "GetAutoColor",
755 "" }; // <- mark of end
756 sameMethods.Insert( names );
759 return !sameMethods.Contains( theCommand->GetMethod() );
762 //================================================================================
764 * \brief Convert creation and addition of all algos and hypos
766 //================================================================================
768 void _pyMesh::Flush()
770 list < Handle(_pyCommand) >::iterator cmd, cmd2;
772 // try to convert algo addition like this:
773 // mesh.AddHypothesis(geom, ALGO ) --> ALGO = mesh.Algo()
774 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
776 Handle(_pyCommand) addCmd = *cmd;
777 _pyID algoID = addCmd->GetArg( 2 );
778 Handle(_pyHypothesis) algo = theGen->FindHyp( algoID );
779 if ( algo.IsNull() || !algo->IsAlgo() )
782 _pyID geom = addCmd->GetArg( 1 );
783 bool isLocalAlgo = ( geom != GetGeom() );
784 if ( algo->Addition2Creation( addCmd, this->GetID() )) // OK
786 // wrapped algo is created atfer mesh creation
787 GetCreationCmd()->AddDependantCmd( addCmd );
790 // mesh.AddHypothesis(geom, ALGO ) --> mesh.AlgoMethod(geom)
791 addCmd->SetArg( addCmd->GetNbArgs() + 1,
792 TCollection_AsciiString( "geom=" ) + geom );
793 // sm = mesh.GetSubMesh(geom, name) --> sm = ALGO.GetSubMesh()
794 for ( cmd2 = mySubmeshes.begin(); cmd2 != mySubmeshes.end(); ++cmd2 ) {
795 Handle(_pyCommand) subCmd = *cmd2;
796 if ( geom == subCmd->GetArg( 1 )) {
797 subCmd->SetObject( algo->GetID() );
798 subCmd->RemoveArgs();
799 addCmd->AddDependantCmd( subCmd );
804 else // KO - ALGO was already created
806 // mesh.AddHypothesis(geom, ALGO) --> mesh.AddHypothesis(ALGO, geom=0)
807 addCmd->RemoveArgs();
808 addCmd->SetArg( 1, algoID );
810 addCmd->SetArg( 2, geom );
814 // try to convert hypo addition like this:
815 // mesh.AddHypothesis(geom, HYPO ) --> HYPO = algo.Hypo()
816 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
818 Handle(_pyCommand) addCmd = *cmd;
819 _pyID hypID = addCmd->GetArg( 2 );
820 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
821 if ( hyp.IsNull() || hyp->IsAlgo() )
823 bool converted = hyp->Addition2Creation( addCmd, this->GetID() );
825 // mesh.AddHypothesis(geom, HYP) --> mesh.AddHypothesis(HYP, geom=0)
826 _pyID geom = addCmd->GetArg( 1 );
827 addCmd->RemoveArgs();
828 addCmd->SetArg( 1, hypID );
829 if ( geom != GetGeom() )
830 addCmd->SetArg( 2, geom );
834 // sm = mesh.GetSubMesh(geom, name) --> sm = mesh.GetMesh().GetSubMesh(geom, name)
835 // for ( cmd = mySubmeshes.begin(); cmd != mySubmeshes.end(); ++cmd ) {
836 // Handle(_pyCommand) subCmd = *cmd;
837 // if ( subCmd->GetNbArgs() > 0 )
838 // AddMeshAccess( subCmd );
840 myAddHypCmds.clear();
844 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
845 for ( ; hyp != myHypos.end(); ++hyp )
849 //================================================================================
851 * \brief MeshEditor convert its commands to ones of mesh
853 //================================================================================
855 _pyMeshEditor::_pyMeshEditor(const Handle(_pyCommand)& theCreationCmd):
856 _pyObject( theCreationCmd )
858 myMesh = theCreationCmd->GetObject();
859 myCreationCmdStr = theCreationCmd->GetString();
860 theCreationCmd->Clear();
863 //================================================================================
865 * \brief convert its commands to ones of mesh
867 //================================================================================
869 void _pyMeshEditor::Process( const Handle(_pyCommand)& theCommand)
871 // names of SMESH_MeshEditor methods fully equal to methods of class Mesh, so
872 // commands calling this methods are converted to calls of methods of Mesh
873 static TStringSet sameMethods;
874 if ( sameMethods.empty() ) {
875 const char * names[] = {
876 "RemoveElements","RemoveNodes","AddNode","AddEdge","AddFace","AddPolygonalFace",
877 "AddVolume","AddPolyhedralVolume","AddPolyhedralVolumeByFaces","MoveNode",
878 "InverseDiag","DeleteDiag","Reorient","ReorientObject","SplitQuad","SplitQuadObject",
879 "BestSplit","Smooth","SmoothObject","SmoothParametric","SmoothParametricObject",
880 "ConvertToQuadratic","ConvertFromQuadratic","RenumberNodes","RenumberElements",
881 "RotationSweep","RotationSweepObject","ExtrusionSweep","AdvancedExtrusion",
882 "ExtrusionSweepObject","ExtrusionSweepObject1D","ExtrusionSweepObject2D","Mirror",
883 "MirrorObject","Translate","TranslateObject","Rotate","RotateObject",
884 "FindCoincidentNodes","FindCoincidentNodesOnPart","MergeNodes","FindEqualElements",
885 "MergeElements","MergeEqualElements","SewFreeBorders","SewConformFreeBorders",
886 "SewBorderToSide","SewSideElements","ChangeElemNodes","GetLastCreatedNodes",
887 "GetLastCreatedElems",
888 "MirrorMakeMesh","MirrorObjectMakeMesh","TranslateMakeMesh",
889 "TranslateObjectMakeMesh","RotateMakeMesh","RotateObjectMakeMesh",
890 "" }; // <- mark of the end
891 sameMethods.Insert( names );
893 //theGen->AddMeshAccessorMethod( theCommand ); // for *Object()
895 if ( sameMethods.Contains( theCommand->GetMethod() )) {
896 theCommand->SetObject( myMesh );
898 // meshes made by *MakeMesh() methods are not wrapped by _pyMesh,
899 // so let _pyMesh care of it (TMP?)
900 if ( theCommand->GetMethod().Search("MakeMesh") != -1 )
901 _pyMesh( new _pyCommand( theCommand->GetString(), 0 )); // for theGen->SetAccessorMethod()
904 // editor creation command is needed only if any editor function is called
905 if ( !myCreationCmdStr.IsEmpty() ) {
906 GetCreationCmd()->GetString() = myCreationCmdStr;
907 myCreationCmdStr.Clear();
912 //================================================================================
914 * \brief _pyHypothesis constructor
915 * \param theCreationCmd -
917 //================================================================================
919 _pyHypothesis::_pyHypothesis(const Handle(_pyCommand)& theCreationCmd):
920 _pyObject( theCreationCmd )
922 myIsAlgo = myIsWrapped = /*myIsConverted = myIsLocal = myDim = */false;
925 //================================================================================
927 * \brief Creates algorithm or hypothesis
928 * \param theCreationCmd - The engine command creating a hypothesis
929 * \retval Handle(_pyHypothesis) - Result _pyHypothesis
931 //================================================================================
933 Handle(_pyHypothesis) _pyHypothesis::NewHypothesis( const Handle(_pyCommand)& theCreationCmd)
935 // theCreationCmd: CreateHypothesis( "theHypType", "theLibName" )
936 ASSERT (( theCreationCmd->GetMethod() == "CreateHypothesis"));
938 Handle(_pyHypothesis) hyp, algo;
941 const TCollection_AsciiString & hypTypeQuoted = theCreationCmd->GetArg( 1 );
942 if ( hypTypeQuoted.IsEmpty() )
945 TCollection_AsciiString hypType =
946 hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
948 algo = new _pyAlgorithm( theCreationCmd );
949 hyp = new _pyHypothesis( theCreationCmd );
951 // 1D Regular_1D ----------
952 if ( hypType == "Regular_1D" ) {
953 // set mesh's method creating algo,
954 // i.e. convertion result will be "regular1d = Mesh.Segment()",
955 // and set hypType by which algo creating a hypothesis is searched for
956 algo->SetConvMethodAndType("Segment", hypType.ToCString());
958 else if ( hypType == "CompositeSegment_1D" ) {
959 algo->SetConvMethodAndType("Segment", "Regular_1D");
960 algo->myArgs.Append( "algo=smesh.COMPOSITE");
962 else if ( hypType == "LocalLength" ) {
963 // set algo's method creating hyp, and algo type
964 hyp->SetConvMethodAndType( "LocalLength", "Regular_1D");
965 // set method whose 1 arg will become the 1-st arg of hyp creation command
966 // i.e. convertion result will be "locallength = regular1d.LocalLength(<arg of SetLength()>)"
967 hyp->AddArgMethod( "SetLength" );
969 else if ( hypType == "NumberOfSegments" ) {
970 hyp = new _pyNumberOfSegmentsHyp( theCreationCmd );
971 hyp->SetConvMethodAndType( "NumberOfSegments", "Regular_1D");
972 // arg of SetNumberOfSegments() will become the 1-st arg of hyp creation command
973 hyp->AddArgMethod( "SetNumberOfSegments" );
974 // arg of SetScaleFactor() will become the 2-nd arg of hyp creation command
975 hyp->AddArgMethod( "SetScaleFactor" );
977 else if ( hypType == "Arithmetic1D" ) {
978 hyp = new _pyComplexParamHypo( theCreationCmd );
979 hyp->SetConvMethodAndType( "Arithmetic1D", "Regular_1D");
981 else if ( hypType == "StartEndLength" ) {
982 hyp = new _pyComplexParamHypo( theCreationCmd );
983 hyp->SetConvMethodAndType( "StartEndLength", "Regular_1D");
985 else if ( hypType == "Deflection1D" ) {
986 hyp->SetConvMethodAndType( "Deflection1D", "Regular_1D");
987 hyp->AddArgMethod( "SetDeflection" );
989 else if ( hypType == "Propagation" ) {
990 hyp->SetConvMethodAndType( "Propagation", "Regular_1D");
992 else if ( hypType == "QuadraticMesh" ) {
993 hyp->SetConvMethodAndType( "QuadraticMesh", "Regular_1D");
995 else if ( hypType == "AutomaticLength" ) {
996 hyp->SetConvMethodAndType( "AutomaticLength", "Regular_1D");
997 hyp->AddArgMethod( "SetFineness");
999 else if ( hypType == "SegmentLengthAroundVertex" ) {
1000 hyp = new _pySegmentLengthAroundVertexHyp( theCreationCmd );
1001 hyp->SetConvMethodAndType( "LengthNearVertex", "Regular_1D" );
1002 hyp->AddArgMethod( "SetLength" );
1004 // 1D Python_1D ----------
1005 else if ( hypType == "Python_1D" ) {
1006 algo->SetConvMethodAndType( "Segment", hypType.ToCString());
1007 algo->myArgs.Append( "algo=smesh.PYTHON");
1009 else if ( hypType == "PythonSplit1D" ) {
1010 hyp->SetConvMethodAndType( "PythonSplit1D", "Python_1D");
1011 hyp->AddArgMethod( "SetNumberOfSegments");
1012 hyp->AddArgMethod( "SetPythonLog10RatioFunction");
1014 // MEFISTO_2D ----------
1015 else if ( hypType == "MEFISTO_2D" ) { // MEFISTO_2D
1016 algo->SetConvMethodAndType( "Triangle", hypType.ToCString());
1018 else if ( hypType == "MaxElementArea" ) {
1019 hyp->SetConvMethodAndType( "MaxElementArea", "MEFISTO_2D");
1020 hyp->SetConvMethodAndType( "MaxElementArea", "NETGEN_2D_ONLY");
1021 hyp->AddArgMethod( "SetMaxElementArea");
1023 else if ( hypType == "LengthFromEdges" ) {
1024 hyp->SetConvMethodAndType( "LengthFromEdges", "MEFISTO_2D");
1025 hyp->SetConvMethodAndType( "LengthFromEdges", "NETGEN_2D_ONLY");
1027 // Quadrangle_2D ----------
1028 else if ( hypType == "Quadrangle_2D" ) {
1029 algo->SetConvMethodAndType( "Quadrangle" , hypType.ToCString());
1031 else if ( hypType == "QuadranglePreference" ) {
1032 hyp->SetConvMethodAndType( "QuadranglePreference", "Quadrangle_2D");
1033 hyp->SetConvMethodAndType( "QuadranglePreference", "NETGEN_2D_ONLY");
1035 // NETGEN ----------
1036 // else if ( hypType == "NETGEN_2D") { // 1D-2D
1037 // algo->SetConvMethodAndType( "Triangle" , hypType.ToCString());
1038 // algo->myArgs.Append( "algo=smesh.NETGEN" );
1040 else if ( hypType == "NETGEN_2D_ONLY") { // 2D
1041 algo->SetConvMethodAndType( "Triangle" , hypType.ToCString());
1042 algo->myArgs.Append( "algo=smesh.NETGEN_2D" );
1044 else if ( hypType == "NETGEN_3D") { // 3D
1045 algo->SetConvMethodAndType( "Tetrahedron" , hypType.ToCString());
1046 algo->myArgs.Append( "algo=smesh.NETGEN" );
1048 else if ( hypType == "MaxElementVolume") {
1049 hyp->SetConvMethodAndType( "MaxElementVolume", "NETGEN_3D");
1050 hyp->AddArgMethod( "SetMaxElementVolume" );
1052 // GHS3D_3D ----------
1053 else if ( hypType == "GHS3D_3D" ) {
1054 algo->SetConvMethodAndType( "Tetrahedron", hypType.ToCString());
1055 algo->myArgs.Append( "algo=smesh.GHS3D" );
1057 // Hexa_3D ---------
1058 else if ( hypType == "Hexa_3D" ) {
1059 algo->SetConvMethodAndType( "Hexahedron", hypType.ToCString());
1061 // Repetitive Projection_1D ---------
1062 else if ( hypType == "Projection_1D" ) {
1063 algo->SetConvMethodAndType( "Projection1D", hypType.ToCString());
1065 else if ( hypType == "ProjectionSource1D" ) {
1066 hyp->SetConvMethodAndType( "SourceEdge", "Projection_1D");
1067 hyp->AddArgMethod( "SetSourceEdge");
1068 hyp->AddArgMethod( "SetSourceMesh");
1069 // 2 args of SetVertexAssociation() will become the 3-th and 4-th args of hyp creation command
1070 hyp->AddArgMethod( "SetVertexAssociation", 2 );
1072 // Projection_2D ---------
1073 else if ( hypType == "Projection_2D" ) {
1074 algo->SetConvMethodAndType( "Projection2D", hypType.ToCString());
1076 else if ( hypType == "ProjectionSource2D" ) {
1077 hyp->SetConvMethodAndType( "SourceFace", "Projection_2D");
1078 hyp->AddArgMethod( "SetSourceFace");
1079 hyp->AddArgMethod( "SetSourceMesh");
1080 hyp->AddArgMethod( "SetVertexAssociation", 4 );
1082 // Projection_3D ---------
1083 else if ( hypType == "Projection_3D" ) {
1084 algo->SetConvMethodAndType( "Projection3D", hypType.ToCString());
1086 else if ( hypType == "ProjectionSource3D" ) {
1087 hyp->SetConvMethodAndType( "SourceShape3D", "Projection_3D");
1088 hyp->AddArgMethod( "SetSource3DShape");
1089 hyp->AddArgMethod( "SetSourceMesh");
1090 hyp->AddArgMethod( "SetVertexAssociation", 4 );
1092 // Prism_3D ---------
1093 else if ( hypType == "Prism_3D" ) {
1094 algo->SetConvMethodAndType( "Prism", hypType.ToCString());
1096 // RadialPrism_3D ---------
1097 else if ( hypType == "RadialPrism_3D" ) {
1098 algo->SetConvMethodAndType( "Prism", hypType.ToCString());
1100 else if ( hypType == "NumberOfLayers" ) {
1101 hyp->SetConvMethodAndType( "NumberOfLayers", "RadialPrism_3D");
1102 hyp->AddArgMethod( "SetNumberOfLayers" );
1104 else if ( hypType == "LayerDistribution" ) {
1105 hyp = new _pyLayerDistributionHypo( theCreationCmd );
1106 hyp->SetConvMethodAndType( "LayerDistribution", "RadialPrism_3D");
1109 if ( algo->IsValid() ) {
1115 //================================================================================
1117 * \brief Convert the command adding a hypothesis to mesh into a smesh command
1118 * \param theCmd - The command like mesh.AddHypothesis( geom, hypo )
1119 * \param theAlgo - The algo that can create this hypo
1120 * \retval bool - false if the command cant be converted
1122 //================================================================================
1124 bool _pyHypothesis::Addition2Creation( const Handle(_pyCommand)& theCmd,
1125 const _pyID& theMesh)
1127 ASSERT(( theCmd->GetMethod() == "AddHypothesis" ));
1129 if ( !IsWrappable( theMesh ))
1132 myGeom = theCmd->GetArg( 1 );
1134 Handle(_pyHypothesis) algo;
1136 // find algo created on myGeom in theMesh
1137 algo = theGen->FindAlgo( myGeom, theMesh, this );
1138 if ( algo.IsNull() )
1140 algo->GetCreationCmd()->AddDependantCmd( theCmd );
1144 // mesh.AddHypothesis(geom,hyp) --> hyp = <theMesh or algo>.myCreationMethod(args)
1145 theCmd->SetResultValue( GetID() );
1146 theCmd->SetObject( IsAlgo() ? theMesh : algo->GetID());
1147 theCmd->SetMethod( IsAlgo() ? GetAlgoCreationMethod() : GetCreationMethod( algo->GetAlgoType() ));
1149 theCmd->RemoveArgs();
1150 for ( int i = 1; i <= myArgs.Length(); ++i ) {
1151 if ( !myArgs( i ).IsEmpty() )
1152 theCmd->SetArg( i, myArgs( i ));
1154 theCmd->SetArg( i, "[]");
1156 // set a new creation command
1157 GetCreationCmd()->Clear();
1158 SetCreationCmd( theCmd );
1160 // clear commands setting arg values
1161 list < Handle(_pyCommand) >::iterator argCmd = myArgCommands.begin();
1162 for ( ; argCmd != myArgCommands.end(); ++argCmd )
1165 // set unknown arg commands after hypo creation
1166 Handle(_pyCommand) afterCmd = myIsWrapped ? theCmd : GetCreationCmd();
1167 list<Handle(_pyCommand)>::iterator cmd = myUnknownCommands.begin();
1168 for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
1169 afterCmd->AddDependantCmd( *cmd );
1175 //================================================================================
1177 * \brief Remember hypothesis parameter values
1178 * \param theCommand - The called hypothesis method
1180 //================================================================================
1182 void _pyHypothesis::Process( const Handle(_pyCommand)& theCommand)
1184 ASSERT( !myIsAlgo );
1187 for ( int i = 1; i <= myArgMethods.Length(); ++i ) {
1188 if ( myArgMethods( i ) == theCommand->GetMethod() ) {
1189 while ( myArgs.Length() < nbArgs + myNbArgsByMethod( i ))
1190 myArgs.Append( "[]" );
1191 for ( int iArg = 1; iArg <= myNbArgsByMethod( i ); ++iArg )
1192 myArgs( nbArgs + iArg ) = theCommand->GetArg( iArg ); // arg value
1193 myArgCommands.push_back( theCommand );
1196 nbArgs += myNbArgsByMethod( i );
1198 myUnknownCommands.push_back( theCommand );
1201 //================================================================================
1203 * \brief Finish conversion
1205 //================================================================================
1207 void _pyHypothesis::Flush()
1209 if ( IsWrapped() ) {
1212 list < Handle(_pyCommand) >::iterator cmd = myArgCommands.begin();
1213 for ( ; cmd != myArgCommands.end(); ++cmd ) {
1214 // Add access to a wrapped mesh
1215 theGen->AddMeshAccessorMethod( *cmd );
1216 // Add access to a wrapped algorithm
1217 theGen->AddAlgoAccessorMethod( *cmd );
1219 cmd = myUnknownCommands.begin();
1220 for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
1221 // Add access to a wrapped mesh
1222 theGen->AddMeshAccessorMethod( *cmd );
1223 // Add access to a wrapped algorithm
1224 theGen->AddAlgoAccessorMethod( *cmd );
1227 // forget previous hypothesis modifications
1228 myArgCommands.clear();
1229 myUnknownCommands.clear();
1232 //================================================================================
1234 * \brief clear creation, arg and unkown commands
1236 //================================================================================
1238 void _pyHypothesis::ClearAllCommands()
1240 GetCreationCmd()->Clear();
1241 list<Handle(_pyCommand)>::iterator cmd = myArgCommands.begin();
1242 for ( ; cmd != myArgCommands.end(); ++cmd )
1244 cmd = myUnknownCommands.begin();
1245 for ( ; cmd != myUnknownCommands.end(); ++cmd )
1249 //================================================================================
1251 * \brief Remember hypothesis parameter values
1252 * \param theCommand - The called hypothesis method
1254 //================================================================================
1256 void _pyComplexParamHypo::Process( const Handle(_pyCommand)& theCommand)
1258 // ex: hyp.SetLength(start, 1)
1259 // hyp.SetLength(end, 0)
1260 ASSERT(( theCommand->GetMethod() == "SetLength" ));
1261 ASSERT(( theCommand->GetArg( 2 ).IsIntegerValue() ));
1262 int i = 2 - theCommand->GetArg( 2 ).IntegerValue();
1263 while ( myArgs.Length() < i )
1264 myArgs.Append( "[]" );
1265 myArgs( i ) = theCommand->GetArg( 1 ); // arg value
1266 myArgCommands.push_back( theCommand );
1269 //================================================================================
1271 * \brief Convert methods of 1D hypotheses to my own methods
1272 * \param theCommand - The called hypothesis method
1274 //================================================================================
1276 void _pyLayerDistributionHypo::Process( const Handle(_pyCommand)& theCommand)
1278 if ( theCommand->GetMethod() != "SetLayerDistribution" )
1281 _pyID newName; // name for 1D hyp = "HypType" + "_Distribution"
1283 const _pyID& hyp1dID = theCommand->GetArg( 1 );
1284 Handle(_pyHypothesis) hyp1d = theGen->FindHyp( hyp1dID );
1285 if ( hyp1d.IsNull() ) // apparently hypId changed at study restoration
1287 else if ( !my1dHyp.IsNull() && hyp1dID != my1dHyp->GetID() ) {
1288 // 1D hypo is already set, so distribution changes and the old
1289 // 1D hypo is thrown away
1290 my1dHyp->ClearAllCommands();
1293 if ( my1dHyp.IsNull() )
1294 return; // something wrong :(
1296 // make a new name for 1D hyp = "HypType" + "_Distribution"
1297 if ( my1dHyp->GetCreationCmd()->GetMethod() == "CreateHypothesis" ) {
1298 // not yet converted creation cmd
1299 TCollection_AsciiString hypTypeQuoted = my1dHyp->GetCreationCmd()->GetArg(1);
1300 TCollection_AsciiString hypType = hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
1301 newName = hypType + "_Distribution";
1302 my1dHyp->GetCreationCmd()->SetResultValue( newName );
1305 // already converted creation cmd
1306 newName = my1dHyp->GetCreationCmd()->GetResultValue();
1309 // as creation of 1D hyp was written later then it's edition,
1310 // we need to find all it's edition calls and process them
1311 list< Handle(_pyCommand) >& cmds = theGen->GetCommands();
1312 list< Handle(_pyCommand) >::iterator cmdIt = cmds.begin();
1313 for ( ; cmdIt != cmds.end(); ++cmdIt ) {
1314 const _pyID& objID = (*cmdIt)->GetObject();
1315 if ( objID == hyp1dID ) {
1316 my1dHyp->Process( *cmdIt );
1317 my1dHyp->GetCreationCmd()->AddDependantCmd( *cmdIt );
1318 ( *cmdIt )->SetObject( newName );
1321 if ( !myArgCommands.empty() )
1322 myArgCommands.front()->Clear();
1323 theCommand->SetArg( 1, newName );
1324 myArgCommands.push_back( theCommand );
1325 // copy hyp1d's creation method and args
1326 // myCreationMethod = hyp1d->GetCreationMethod();
1327 // myArgs = hyp1d->GetArgs();
1328 // // make them cleared at conversion
1329 // myArgCommands = hyp1d->GetArgCommands();
1331 // // to be cleared at convertion only
1332 // myArgCommands.push_back( theCommand );
1335 //================================================================================
1338 * \param theAdditionCmd -
1342 //================================================================================
1344 bool _pyLayerDistributionHypo::Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
1345 const _pyID& theMesh)
1347 myIsWrapped = false;
1349 if ( my1dHyp.IsNull() )
1352 // set "SetLayerDistribution()" after addition cmd
1353 theAdditionCmd->AddDependantCmd( myArgCommands.front() );
1355 _pyID geom = theAdditionCmd->GetArg( 1 );
1357 my1dHyp->SetMesh( theMesh );
1358 if ( !my1dHyp->Addition2Creation( theAdditionCmd, theMesh ))
1361 // clear "SetLayerDistribution()" cmd
1362 myArgCommands.front()->Clear();
1364 // Convert my creation => me = RadialPrismAlgo.Get3DHypothesis()
1366 // find RadialPrism algo created on <geom> for theMesh
1367 Handle(_pyHypothesis) algo = theGen->FindAlgo( geom, theMesh, this );
1368 if ( !algo.IsNull() ) {
1369 GetCreationCmd()->SetObject( algo->GetID() );
1370 GetCreationCmd()->SetMethod( "Get3DHypothesis" );
1371 GetCreationCmd()->RemoveArgs();
1372 theAdditionCmd->AddDependantCmd( GetCreationCmd() );
1378 //================================================================================
1382 //================================================================================
1384 void _pyLayerDistributionHypo::Flush()
1386 //my1dHyp.Nullify();
1387 //_pyHypothesis::Flush();
1390 //================================================================================
1392 * \brief additionally to Addition2Creation, clears SetDistrType() command
1393 * \param theCmd - AddHypothesis() command
1394 * \param theMesh - mesh to which a hypothesis is added
1395 * \retval bool - convertion result
1397 //================================================================================
1399 bool _pyNumberOfSegmentsHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
1400 const _pyID& theMesh)
1402 if ( IsWrappable( theMesh ) && myArgs.Length() > 1 ) {
1403 // scale factor (2-nd arg) is provided: clear SetDistrType(1) command
1404 bool scaleDistrType = false;
1405 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnknownCommands.rbegin();
1406 for ( ; cmd != myUnknownCommands.rend(); ++cmd ) {
1407 if ( (*cmd)->GetMethod() == "SetDistrType" ) {
1408 if ( (*cmd)->GetArg( 1 ) == "1" ) {
1409 scaleDistrType = true;
1412 else if ( !scaleDistrType ) {
1413 // distribution type changed: remove scale factor from args
1414 myArgs.Remove( 2, myArgs.Length() );
1420 return _pyHypothesis::Addition2Creation( theCmd, theMesh );
1423 //================================================================================
1425 * \brief remove repeated commands defining distribution
1427 //================================================================================
1429 void _pyNumberOfSegmentsHyp::Flush()
1431 // find number of the last SetDistrType() command
1432 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnknownCommands.rbegin();
1433 int distrTypeNb = 0;
1434 for ( ; !distrTypeNb && cmd != myUnknownCommands.rend(); ++cmd )
1435 if ( (*cmd)->GetMethod() == "SetDistrType" )
1436 distrTypeNb = (*cmd)->GetOrderNb();
1438 // clear commands before the last SetDistrType()
1439 list<Handle(_pyCommand)> * cmds[2] = { &myArgCommands, &myUnknownCommands };
1440 for ( int i = 0; i < 2; ++i ) {
1441 set<TCollection_AsciiString> uniqueMethods;
1442 list<Handle(_pyCommand)> & cmdList = *cmds[i];
1443 for ( cmd = cmdList.rbegin(); cmd != cmdList.rend(); ++cmd )
1445 bool clear = ( (*cmd)->GetOrderNb() < distrTypeNb );
1446 const TCollection_AsciiString& method = (*cmd)->GetMethod();
1447 if ( !clear || method == "SetNumberOfSegments" ) {
1448 bool isNewInSet = uniqueMethods.insert( method ).second;
1449 clear = !isNewInSet;
1458 //================================================================================
1460 * \brief Convert the command adding "SegmentLengthAroundVertex" to mesh
1461 * into regular1D.LengthNearVertex( length, vertex )
1462 * \param theCmd - The command like mesh.AddHypothesis( vertex, SegmentLengthAroundVertex )
1463 * \param theMesh - The mesh needing this hypo
1464 * \retval bool - false if the command cant be converted
1466 //================================================================================
1468 bool _pySegmentLengthAroundVertexHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
1469 const _pyID& theMeshID)
1471 if ( IsWrappable( theMeshID )) {
1473 _pyID vertex = theCmd->GetArg( 1 );
1475 // the problem here is that segment algo will not be found
1476 // by pyHypothesis::Addition2Creation() for <vertex>, so we try to find
1477 // geometry where segment algorithm is assigned
1478 Handle(_pyHypothesis) algo;
1479 _pyID geom = vertex;
1480 while ( algo.IsNull() && !geom.IsEmpty()) {
1481 // try to find geom as a father of <vertex>
1482 geom = FatherID( geom );
1483 algo = theGen->FindAlgo( geom, theMeshID, this );
1485 if ( algo.IsNull() )
1486 return false; // also possible to find geom as brother of veretex...
1487 // set geom instead of vertex
1488 theCmd->SetArg( 1, geom );
1490 // set vertex as a second arg
1491 if ( myArgs.Length() < 1) myArgs.Append( "1" ); // :(
1492 myArgs.Append( vertex );
1494 // mesh.AddHypothesis(vertex, SegmentLengthAroundVertex) -->
1495 // theMeshID.LengthNearVertex( length, vertex )
1496 return _pyHypothesis::Addition2Creation( theCmd, theMeshID );
1501 //================================================================================
1503 * \brief _pyAlgorithm constructor
1504 * \param theCreationCmd - The command like "algo = smeshgen.CreateHypothesis(type,lib)"
1506 //================================================================================
1508 _pyAlgorithm::_pyAlgorithm(const Handle(_pyCommand)& theCreationCmd)
1509 : _pyHypothesis( theCreationCmd )
1514 //================================================================================
1516 * \brief Convert the command adding an algorithm to mesh
1517 * \param theCmd - The command like mesh.AddHypothesis( geom, algo )
1518 * \param theMesh - The mesh needing this algo
1519 * \retval bool - false if the command cant be converted
1521 //================================================================================
1523 bool _pyAlgorithm::Addition2Creation( const Handle(_pyCommand)& theCmd,
1524 const _pyID& theMeshID)
1526 // mesh.AddHypothesis(geom,algo) --> theMeshID.myCreationMethod()
1527 if ( _pyHypothesis::Addition2Creation( theCmd, theMeshID )) {
1528 theGen->SetAccessorMethod( GetID(), "GetAlgorithm()" );
1534 //================================================================================
1536 * \brief Return starting position of a part of python command
1537 * \param thePartIndex - The index of command part
1538 * \retval int - Part position
1540 //================================================================================
1542 int _pyCommand::GetBegPos( int thePartIndex )
1546 if ( myBegPos.Length() < thePartIndex )
1548 return myBegPos( thePartIndex );
1551 //================================================================================
1553 * \brief Store starting position of a part of python command
1554 * \param thePartIndex - The index of command part
1555 * \param thePosition - Part position
1557 //================================================================================
1559 void _pyCommand::SetBegPos( int thePartIndex, int thePosition )
1561 while ( myBegPos.Length() < thePartIndex )
1562 myBegPos.Append( UNKNOWN );
1563 myBegPos( thePartIndex ) = thePosition;
1566 //================================================================================
1568 * \brief Returns whitespace symbols at the line beginning
1569 * \retval TCollection_AsciiString - result
1571 //================================================================================
1573 TCollection_AsciiString _pyCommand::GetIndentation()
1576 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
1577 GetWord( myString, end, true );
1579 end = GetBegPos( RESULT_IND );
1580 return myString.SubString( 1, end - 1 );
1583 //================================================================================
1585 * \brief Return substring of python command looking like ResultValue = Obj.Meth()
1586 * \retval const TCollection_AsciiString & - ResultValue substring
1588 //================================================================================
1590 const TCollection_AsciiString & _pyCommand::GetResultValue()
1592 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
1594 int begPos = myString.Location( "=", 1, Length() );
1596 myRes = GetWord( myString, begPos, false );
1599 SetBegPos( RESULT_IND, begPos );
1604 //================================================================================
1606 * \brief Return number of python command result value ResultValue = Obj.Meth()
1609 //================================================================================
1611 const int _pyCommand::GetNbResultValues()
1615 int endPos = myString.Location( "=", 1, Length() );
1616 TCollection_AsciiString str = "";
1617 while ( begPos < endPos) {
1618 str = GetWord( myString, begPos, true );
1619 begPos = begPos+ str.Length();
1626 //================================================================================
1628 * \brief Return substring of python command looking like
1629 * ResultValue1 , ResultValue1,... = Obj.Meth() with res index
1630 * \retval const TCollection_AsciiString & - ResultValue with res index substring
1632 //================================================================================
1633 const TCollection_AsciiString & _pyCommand::GetResultValue(int res)
1637 int endPos = myString.Location( "=", 1, Length() );
1638 while ( begPos < endPos) {
1639 myRes = GetWord( myString, begPos, true );
1640 begPos = begPos + myRes.Length();
1643 myRes.RemoveAll('[');myRes.RemoveAll(']');
1649 return theEmptyString;
1652 //================================================================================
1654 * \brief Return substring of python command looking like ResVal = Object.Meth()
1655 * \retval const TCollection_AsciiString & - Object substring
1657 //================================================================================
1659 const TCollection_AsciiString & _pyCommand::GetObject()
1661 if ( GetBegPos( OBJECT_IND ) == UNKNOWN )
1664 int begPos = GetBegPos( RESULT_IND ) + myRes.Length();
1666 begPos = myString.Location( "=", 1, Length() ) + 1;
1668 myObj = GetWord( myString, begPos, true );
1669 SetBegPos( OBJECT_IND, begPos );
1675 //================================================================================
1677 * \brief Return substring of python command looking like ResVal = Obj.Method()
1678 * \retval const TCollection_AsciiString & - Method substring
1680 //================================================================================
1682 const TCollection_AsciiString & _pyCommand::GetMethod()
1684 if ( GetBegPos( METHOD_IND ) == UNKNOWN )
1687 int begPos = GetBegPos( OBJECT_IND ) + myObj.Length();
1688 bool forward = true;
1690 begPos = myString.Location( "(", 1, Length() ) - 1;
1694 myMeth = GetWord( myString, begPos, forward );
1695 SetBegPos( METHOD_IND, begPos );
1701 //================================================================================
1703 * \brief Return substring of python command looking like ResVal = Obj.Meth(Arg1,...)
1704 * \retval const TCollection_AsciiString & - Arg<index> substring
1706 //================================================================================
1708 const TCollection_AsciiString & _pyCommand::GetArg( int index )
1710 if ( GetBegPos( ARG1_IND ) == UNKNOWN )
1713 int begPos = GetBegPos( METHOD_IND ) + myMeth.Length();
1715 begPos = myString.Location( "(", 1, Length() ) + 1;
1717 int i = 0, prevLen = 0;
1718 while ( begPos != EMPTY ) {
1720 // check if we are looking at the closing parenthesis
1721 while ( begPos <= Length() && isspace( myString.Value( begPos )))
1723 if ( begPos > Length() || myString.Value( begPos ) == ')' )
1725 myArgs.Append( GetWord( myString, begPos, true, true ));
1726 SetBegPos( ARG1_IND + i, begPos );
1727 prevLen = myArgs.Last().Length();
1729 myArgs.Remove( myArgs.Length() ); // no more args
1733 if ( myArgs.Length() < index )
1734 return theEmptyString;
1735 return myArgs( index );
1738 //================================================================================
1740 * \brief Check if char is a word part
1741 * \param c - The character to check
1742 * \retval bool - The check result
1744 //================================================================================
1746 static inline bool isWord(const char c, const bool dotIsWord)
1749 !isspace(c) && c != ',' && c != '=' && c != ')' && c != '(' && ( dotIsWord || c != '.');
1752 //================================================================================
1754 * \brief Looks for a word in the string and returns word's beginning
1755 * \param theString - The input string
1756 * \param theStartPos - The position to start the search, returning word's beginning
1757 * \param theForward - The search direction
1758 * \retval TCollection_AsciiString - The found word
1760 //================================================================================
1762 TCollection_AsciiString _pyCommand::GetWord( const TCollection_AsciiString & theString,
1764 const bool theForward,
1765 const bool dotIsWord )
1767 int beg = theStartPos, end = theStartPos;
1768 theStartPos = EMPTY;
1769 if ( beg < 1 || beg > theString.Length() )
1770 return theEmptyString;
1772 if ( theForward ) { // search forward
1774 while ( beg <= theString.Length() && !isWord( theString.Value( beg ), dotIsWord))
1776 if ( beg > theString.Length() )
1777 return theEmptyString; // no word found
1780 while ( end <= theString.Length() && isWord( theString.Value( end ), dotIsWord))
1784 else { // search backward
1786 while ( end > 0 && !isWord( theString.Value( end ), dotIsWord))
1789 return theEmptyString; // no word found
1791 while ( beg > 0 && isWord( theString.Value( beg ), dotIsWord))
1796 //cout << theString << " ---- " << beg << " - " << end << endl;
1797 return theString.SubString( beg, end );
1800 //================================================================================
1802 * \brief Look for position where not space char is
1803 * \param theString - The string
1804 * \param thePos - The position to search from and which returns result
1805 * \retval bool - false if there are only space after thePos in theString
1809 //================================================================================
1811 bool _pyCommand::SkipSpaces( const TCollection_AsciiString & theString, int & thePos )
1813 if ( thePos < 1 || thePos > theString.Length() )
1816 while ( thePos <= theString.Length() && isspace( theString.Value( thePos )))
1819 return thePos <= theString.Length();
1822 //================================================================================
1824 * \brief Modify a part of the command
1825 * \param thePartIndex - The index of the part
1826 * \param thePart - The new part string
1827 * \param theOldPart - The old part
1829 //================================================================================
1831 void _pyCommand::SetPart(int thePartIndex, const TCollection_AsciiString& thePart,
1832 TCollection_AsciiString& theOldPart)
1834 int pos = GetBegPos( thePartIndex );
1835 if ( pos <= Length() && theOldPart != thePart)
1837 TCollection_AsciiString seperator;
1839 pos = GetBegPos( thePartIndex + 1 );
1840 if ( pos < 1 ) return;
1841 switch ( thePartIndex ) {
1842 case RESULT_IND: seperator = " = "; break;
1843 case OBJECT_IND: seperator = "."; break;
1844 case METHOD_IND: seperator = "()"; break;
1848 myString.Remove( pos, theOldPart.Length() );
1849 if ( !seperator.IsEmpty() )
1850 myString.Insert( pos , seperator );
1851 myString.Insert( pos, thePart );
1852 // update starting positions of the following parts
1853 int posDelta = thePart.Length() + seperator.Length() - theOldPart.Length();
1854 for ( int i = thePartIndex + 1; i <= myBegPos.Length(); ++i ) {
1855 if ( myBegPos( i ) > 0 )
1856 myBegPos( i ) += posDelta;
1858 theOldPart = thePart;
1862 //================================================================================
1864 * \brief Set agrument
1865 * \param index - The argument index, it counts from 1
1866 * \param theArg - The argument string
1868 //================================================================================
1870 void _pyCommand::SetArg( int index, const TCollection_AsciiString& theArg)
1873 int argInd = ARG1_IND + index - 1;
1874 int pos = GetBegPos( argInd );
1875 if ( pos < 1 ) // no index-th arg exist, append inexistent args
1877 // find a closing parenthesis
1878 if ( int lastArgInd = GetNbArgs() ) {
1879 pos = GetBegPos( ARG1_IND + lastArgInd - 1 ) + GetArg( lastArgInd ).Length();
1880 while ( pos > 0 && pos <= Length() && myString.Value( pos ) != ')' )
1885 while ( pos > 0 && myString.Value( pos ) != ')' )
1888 if ( pos < 1 || myString.Value( pos ) != ')' ) { // no parentheses at all
1892 while ( myArgs.Length() < index ) {
1893 if ( myArgs.Length() )
1894 myString.Insert( pos++, "," );
1895 myArgs.Append("None");
1896 myString.Insert( pos, myArgs.Last() );
1897 SetBegPos( ARG1_IND + myArgs.Length() - 1, pos );
1898 pos += myArgs.Last().Length();
1901 SetPart( argInd, theArg, myArgs( index ));
1904 //================================================================================
1906 * \brief Empty arg list
1908 //================================================================================
1910 void _pyCommand::RemoveArgs()
1912 if ( int pos = myString.Location( '(', 1, Length() ))
1913 myString.Trunc( pos );
1916 if ( myBegPos.Length() >= ARG1_IND )
1917 myBegPos.Remove( ARG1_IND, myBegPos.Length() );
1920 //================================================================================
1922 * \brief Set dependent commands after this one
1924 //================================================================================
1926 bool _pyCommand::SetDependentCmdsAfter() const
1928 bool orderChanged = false;
1929 list< Handle(_pyCommand)>::const_reverse_iterator cmd = myDependentCmds.rbegin();
1930 for ( ; cmd != myDependentCmds.rend(); ++cmd ) {
1931 if ( (*cmd)->GetOrderNb() < GetOrderNb() ) {
1932 orderChanged = true;
1933 theGen->SetCommandAfter( *cmd, this );
1934 (*cmd)->SetDependentCmdsAfter();
1937 return orderChanged;
1939 //================================================================================
1941 * \brief Insert accessor method after theObjectID
1942 * \param theObjectID - id of the accessed object
1943 * \param theAcsMethod - name of the method giving access to the object
1944 * \retval bool - false if theObjectID is not found in the command string
1946 //================================================================================
1948 bool _pyCommand::AddAccessorMethod( _pyID theObjectID, const char* theAcsMethod )
1950 if ( !theAcsMethod )
1952 // start object search from the object, i.e. ignore result
1954 int beg = GetBegPos( OBJECT_IND );
1955 if ( beg < 1 || beg > Length() )
1958 while (( beg = myString.Location( theObjectID, beg, Length() )))
1960 // check that theObjectID is not just a part of a longer ID
1961 int afterEnd = beg + theObjectID.Length();
1962 Standard_Character c = myString.Value( afterEnd );
1963 if ( !isalnum( c ) && c != ':' ) {
1964 // check if accessor method already present
1966 myString.Location( (char*) theAcsMethod, afterEnd, Length() ) != afterEnd+1) {
1968 int oldLen = Length();
1969 myString.Insert( afterEnd, (char*) theAcsMethod );
1970 myString.Insert( afterEnd, "." );
1971 // update starting positions of the parts following the modified one
1972 int posDelta = Length() - oldLen;
1973 for ( int i = 1; i <= myBegPos.Length(); ++i ) {
1974 if ( myBegPos( i ) > afterEnd )
1975 myBegPos( i ) += posDelta;
1980 beg = afterEnd; // is a part - next search
1985 //================================================================================
1987 * \brief Return method name giving access to an interaface object wrapped by python class
1988 * \retval const char* - method name
1990 //================================================================================
1992 const char* _pyObject::AccessorMethod() const
1996 //================================================================================
1998 * \brief Return ID of a father
2000 //================================================================================
2002 _pyID _pyObject::FatherID(const _pyID & childID)
2004 int colPos = childID.SearchFromEnd(':');
2006 return childID.SubString( 1, colPos-1 );