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
86 //================================================================================
88 * \brief Set of TCollection_AsciiString initialized by C array of C strings
90 //================================================================================
92 struct TStringSet: public set<TCollection_AsciiString>
95 * \brief Filling. The last string must be ""
97 void Insert(const char* names[]) {
98 for ( int i = 0; names[i][0] ; ++i )
99 insert( (char*) names[i] );
102 * \brief Check if a string is in
104 bool Contains(const TCollection_AsciiString& name ) {
105 return find( name ) != end();
110 //================================================================================
112 * \brief Convert python script using commands of smesh.py
113 * \param theScript - Input script
114 * \retval TCollection_AsciiString - Convertion result
116 * Class SMESH_2smeshpy declared in SMESH_PythonDump.hxx
118 //================================================================================
120 TCollection_AsciiString
121 SMESH_2smeshpy::ConvertScript(const TCollection_AsciiString& theScript,
122 Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod)
124 theGen = new _pyGen( theEntry2AccessorMethod );
126 // split theScript into separate commands
127 int from = 1, end = theScript.Length(), to;
128 while ( from < end && ( to = theScript.Location( "\n", from, end )))
131 // cut out and store a command
132 theGen->AddCommand( theScript.SubString( from, to - 1 ));
137 #ifdef DUMP_CONVERSION
138 cout << endl << " ######## RESULT ######## " << endl<< endl;
140 // reorder commands after conversion
141 list< Handle(_pyCommand) >::iterator cmd;
144 orderChanges = false;
145 for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
146 if ( (*cmd)->SetDependentCmdsAfter() )
148 } while ( orderChanges );
150 // concat commands back into a script
151 TCollection_AsciiString aScript;
152 for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
154 #ifdef DUMP_CONVERSION
155 cout << "## COM " << (*cmd)->GetOrderNb() << ": "<< (*cmd)->GetString() << endl;
157 if ( !(*cmd)->IsEmpty() ) {
159 aScript += (*cmd)->GetString();
169 //================================================================================
171 * \brief _pyGen constructor
173 //================================================================================
175 _pyGen::_pyGen(Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod)
176 : _pyObject( new _pyCommand( TPythonDump::SMESHGenName(), 0 )),
177 myID2AccessorMethod( theEntry2AccessorMethod )
180 myHasPattern = false;
181 // make that GetID() to return TPythonDump::SMESHGenName()
182 GetCreationCmd()->GetString() += "=";
185 //================================================================================
187 * \brief name of SMESH_Gen in smesh.py
189 //================================================================================
191 const char* _pyGen::AccessorMethod() const
193 return SMESH_2smeshpy::GenName();
196 //================================================================================
198 * \brief Convert a command using a specific converter
199 * \param theCommand - the command to convert
201 //================================================================================
203 Handle(_pyCommand) _pyGen::AddCommand( const TCollection_AsciiString& theCommand)
205 // store theCommand in the sequence
206 myCommands.push_back( new _pyCommand( theCommand, ++myNbCommands ));
208 Handle(_pyCommand) aCommand = myCommands.back();
209 #ifdef DUMP_CONVERSION
210 cout << "## COM " << myNbCommands << ": "<< aCommand->GetString() << endl;
213 _pyID objID = aCommand->GetObject();
215 if ( objID.IsEmpty() )
219 if ( objID == this->GetID() ) {
220 this->Process( aCommand );
223 // SMESH_Mesh method?
224 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( objID );
225 if ( id_mesh != myMeshes.end() ) {
226 if ( aCommand->GetMethod() == "GetMeshEditor" ) { // MeshEditor creation
227 _pyID editorID = aCommand->GetResultValue();
228 Handle(_pyMeshEditor) editor = new _pyMeshEditor( aCommand );
229 myMeshEditors.insert( make_pair( editorID, editor ));
232 id_mesh->second->Process( aCommand );
235 // SMESH_MeshEditor method?
236 map< _pyID, Handle(_pyMeshEditor) >::iterator id_editor = myMeshEditors.find( objID );
237 if ( id_editor != myMeshEditors.end() ) {
238 id_editor->second->Process( aCommand );
241 // SMESH_Hypothesis method?
242 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
243 for ( ; hyp != myHypos.end(); ++hyp )
244 if ( !(*hyp)->IsAlgo() && objID == (*hyp)->GetID() ) {
245 (*hyp)->Process( aCommand );
249 // Add access to a wrapped mesh
250 AddMeshAccessorMethod( aCommand );
252 // Add access to a wrapped algorithm
253 AddAlgoAccessorMethod( aCommand ); // ??? what if algo won't be wrapped at all ???
255 // PAL12227. PythonDump was not updated at proper time; result is
256 // aCriteria.append(SMESH.Filter.Criterion(17,26,0,'L1',26,25,1e-07,SMESH.EDGE,-1))
257 // TypeError: __init__() takes exactly 11 arguments (10 given)
258 char wrongCommand[] = "SMESH.Filter.Criterion(";
259 if ( int beg = theCommand.Location( wrongCommand, 1, theCommand.Length() ))
261 _pyCommand tmpCmd( theCommand.SubString( beg, theCommand.Length() ), -1);
262 // there must be 10 arguments, 5-th arg ThresholdID is missing,
263 const int wrongNbArgs = 9, missingArg = 5;
264 if ( tmpCmd.GetNbArgs() == wrongNbArgs )
266 for ( int i = wrongNbArgs; i > missingArg; --i )
267 tmpCmd.SetArg( i + 1, tmpCmd.GetArg( i ));
268 tmpCmd.SetArg( missingArg, "''");
269 aCommand->GetString().Trunc( beg - 1 );
270 aCommand->GetString() += tmpCmd.GetString();
276 //================================================================================
278 * \brief Convert the command or remember it for later conversion
279 * \param theCommand - The python command calling a method of SMESH_Gen
281 //================================================================================
283 void _pyGen::Process( const Handle(_pyCommand)& theCommand )
285 // there are methods to convert:
286 // CreateMesh( shape )
287 // Concatenate( [mesh1, ...], ... )
288 // CreateHypothesis( theHypType, theLibName )
289 // Compute( mesh, geom )
292 if ( theCommand->GetMethod() == "CreateMesh" ||
293 theCommand->GetMethod() == "CreateEmptyMesh" )
295 Handle(_pyMesh) mesh = new _pyMesh( theCommand );
296 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
300 // CreateHypothesis()
301 if ( theCommand->GetMethod() == "CreateHypothesis" )
303 myHypos.push_back( _pyHypothesis::NewHypothesis( theCommand ));
307 // smeshgen.Compute( mesh, geom ) --> mesh.Compute()
308 if ( theCommand->GetMethod() == "Compute" )
310 const _pyID& meshID = theCommand->GetArg( 1 );
311 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( meshID );
312 if ( id_mesh != myMeshes.end() ) {
313 theCommand->SetObject( meshID );
314 theCommand->RemoveArgs();
315 id_mesh->second->Flush();
320 // leave only one smeshgen.GetPattern() in the script
321 if ( theCommand->GetMethod() == "GetPattern" ) {
322 if ( myHasPattern ) {
329 // Concatenate( [mesh1, ...], ... )
330 if ( theCommand->GetMethod() == "Concatenate" )
332 AddMeshAccessorMethod( theCommand );
335 // Replace name of SMESH_Gen
337 // names of SMESH_Gen methods fully equal to methods defined in smesh.py
338 static TStringSet smeshpyMethods;
339 if ( smeshpyMethods.empty() ) {
340 const char * names[] =
341 { "SetEmbeddedMode","IsEmbeddedMode","SetCurrentStudy","GetCurrentStudy",
342 "GetPattern","GetSubShapesId",
343 "" }; // <- mark of array end
344 smeshpyMethods.Insert( names );
346 if ( smeshpyMethods.Contains( theCommand->GetMethod() ))
347 // smeshgen.Method() --> smesh.Method()
348 theCommand->SetObject( SMESH_2smeshpy::SmeshpyName() );
350 // smeshgen.Method() --> smesh.smesh.Method()
351 theCommand->SetObject( SMESH_2smeshpy::GenName() );
354 //================================================================================
356 * \brief Convert the remembered commands
358 //================================================================================
362 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.begin();
363 for ( ; id_mesh != myMeshes.end(); ++id_mesh )
364 if ( ! id_mesh->second.IsNull() )
365 id_mesh->second->Flush();
367 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
368 for ( ; hyp != myHypos.end(); ++hyp )
369 if ( !hyp->IsNull() ) {
371 // smeshgen.CreateHypothesis() --> smesh.smesh.CreateHypothesis()
372 if ( !(*hyp)->IsWrapped() )
373 (*hyp)->GetCreationCmd()->SetObject( SMESH_2smeshpy::GenName() );
377 //================================================================================
379 * \brief Add access method to mesh that is an object or an argument
380 * \param theCmd - command to add access method
381 * \retval bool - true if added
383 //================================================================================
385 bool _pyGen::AddMeshAccessorMethod( Handle(_pyCommand) theCmd ) const
388 map< _pyID, Handle(_pyMesh) >::const_iterator id_mesh = myMeshes.begin();
389 for ( ; id_mesh != myMeshes.end(); ++id_mesh ) {
390 if ( theCmd->AddAccessorMethod( id_mesh->first, id_mesh->second->AccessorMethod() ))
396 //================================================================================
398 * \brief Add access method to algo that is an object or an argument
399 * \param theCmd - command to add access method
400 * \retval bool - true if added
402 //================================================================================
404 bool _pyGen::AddAlgoAccessorMethod( Handle(_pyCommand) theCmd ) const
407 list< Handle(_pyHypothesis) >::const_iterator hyp = myHypos.begin();
408 for ( ; hyp != myHypos.end(); ++hyp ) {
409 if ( (*hyp)->IsAlgo() && /*(*hyp)->IsWrapped() &&*/
410 theCmd->AddAccessorMethod( (*hyp)->GetID(), (*hyp)->AccessorMethod() ))
416 //================================================================================
418 * \brief Find hypothesis by ID (entry)
419 * \param theHypID - The hypothesis ID
420 * \retval Handle(_pyHypothesis) - The found hypothesis
422 //================================================================================
424 Handle(_pyHypothesis) _pyGen::FindHyp( const _pyID& theHypID )
426 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
427 for ( ; hyp != myHypos.end(); ++hyp )
428 if ( !hyp->IsNull() && theHypID == (*hyp)->GetID() )
430 return Handle(_pyHypothesis)();
433 //================================================================================
435 * \brief Find algorithm the created algorithm
436 * \param theGeom - The shape ID the algorithm was created on
437 * \param theMesh - The mesh ID that created the algorithm
438 * \param dim - The algo dimension
439 * \retval Handle(_pyHypothesis) - The found algo
441 //================================================================================
443 Handle(_pyHypothesis) _pyGen::FindAlgo( const _pyID& theGeom, const _pyID& theMesh,
444 const Handle(_pyHypothesis)& theHypothesis )
446 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
447 for ( ; hyp != myHypos.end(); ++hyp )
448 if ( !hyp->IsNull() &&
450 theHypothesis->CanBeCreatedBy( (*hyp)->GetAlgoType() ) &&
451 (*hyp)->GetGeom() == theGeom &&
452 (*hyp)->GetMesh() == theMesh )
457 //================================================================================
459 * \brief Change order of commands in the script
460 * \param theCmd1 - One command
461 * \param theCmd2 - Another command
463 //================================================================================
465 void _pyGen::ExchangeCommands( Handle(_pyCommand) theCmd1, Handle(_pyCommand) theCmd2 )
467 list< Handle(_pyCommand) >::iterator pos1, pos2;
468 pos1 = find( myCommands.begin(), myCommands.end(), theCmd1 );
469 pos2 = find( myCommands.begin(), myCommands.end(), theCmd2 );
470 myCommands.insert( pos1, theCmd2 );
471 myCommands.insert( pos2, theCmd1 );
472 myCommands.erase( pos1 );
473 myCommands.erase( pos2 );
475 int nb1 = theCmd1->GetOrderNb();
476 theCmd1->SetOrderNb( theCmd2->GetOrderNb() );
477 theCmd2->SetOrderNb( nb1 );
478 // cout << "BECOME " << theCmd1->GetOrderNb() << "\t" << theCmd1->GetString() << endl
479 // << "BECOME " << theCmd2->GetOrderNb() << "\t" << theCmd2->GetString() << endl << endl;
482 //================================================================================
484 * \brief Set one command after the other
485 * \param theCmd - Command to move
486 * \param theAfterCmd - Command ater which to insert the first one
488 //================================================================================
490 void _pyGen::SetCommandAfter( Handle(_pyCommand) theCmd, Handle(_pyCommand) theAfterCmd )
493 //cout << "SET\t" << theAfterCmd->GetString() << endl << "BEFORE\t" << theCmd->GetString() << endl<<endl;
495 list< Handle(_pyCommand) >::iterator pos;
496 pos = find( myCommands.begin(), myCommands.end(), theCmd );
497 myCommands.erase( pos );
498 pos = find( myCommands.begin(), myCommands.end(), theAfterCmd );
499 myCommands.insert( ++pos, theCmd );
502 for ( pos = myCommands.begin(); pos != myCommands.end(); ++pos)
503 (*pos)->SetOrderNb( i++ );
506 //================================================================================
508 * \brief Set method to access to object wrapped with python class
509 * \param theID - The wrapped object entry
510 * \param theMethod - The accessor method
512 //================================================================================
514 void _pyGen::SetAccessorMethod(const _pyID& theID, const char* theMethod )
516 myID2AccessorMethod.Bind( theID, (char*) theMethod );
519 //================================================================================
521 * \brief Find out type of geom group
522 * \param grpID - The geom group entry
523 * \retval int - The type
525 //================================================================================
527 static bool sameGroupType( const _pyID& grpID,
528 const TCollection_AsciiString& theType)
530 // define group type as smesh.Mesh.Group() does
532 SALOMEDS::Study_var study = SMESH_Gen_i::GetSMESHGen()->GetCurrentStudy();
533 SALOMEDS::SObject_var aSObj = study->FindObjectID( grpID.ToCString() );
534 if ( !aSObj->_is_nil() ) {
535 GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow( aSObj->GetObject() );
536 if ( !aGeomObj->_is_nil() ) {
537 switch ( aGeomObj->GetShapeType() ) {
538 case GEOM::VERTEX: type = SMESH::NODE; break;
539 case GEOM::EDGE: type = SMESH::EDGE; break;
540 case GEOM::FACE: type = SMESH::FACE; break;
542 case GEOM::SHELL: type = SMESH::VOLUME; break;
543 case GEOM::COMPOUND: {
544 GEOM::GEOM_Gen_ptr aGeomGen = SMESH_Gen_i::GetSMESHGen()->GetGeomEngine();
545 if ( !aGeomGen->_is_nil() ) {
546 GEOM::GEOM_IGroupOperations_var aGrpOp =
547 aGeomGen->GetIGroupOperations( study->StudyId() );
548 if ( !aGrpOp->_is_nil() ) {
549 switch ( aGrpOp->GetType( aGeomObj )) {
550 case TopAbs_VERTEX: type = SMESH::NODE; break;
551 case TopAbs_EDGE: type = SMESH::EDGE; break;
552 case TopAbs_FACE: type = SMESH::FACE; break;
553 case TopAbs_SOLID: type = SMESH::VOLUME; break;
564 MESSAGE("Type of the group " << grpID << " not found");
567 if ( theType.IsIntegerValue() )
568 return type == theType.IntegerValue();
571 case SMESH::NODE: return theType.Location( "NODE", 1, theType.Length() );
572 case SMESH::EDGE: return theType.Location( "EDGE", 1, theType.Length() );
573 case SMESH::FACE: return theType.Location( "FACE", 1, theType.Length() );
574 case SMESH::VOLUME: return theType.Location( "VOLUME", 1, theType.Length() );
580 //================================================================================
583 * \param theCreationCmd -
585 //================================================================================
587 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd):
588 _pyObject(theCreationCmd), myHasEditor(false)
590 // convert my creation command
591 Handle(_pyCommand) creationCmd = GetCreationCmd();
592 creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
593 creationCmd->SetMethod( "Mesh" );
595 theGen->SetAccessorMethod( GetID(), "GetMesh()" );
598 //================================================================================
600 * \brief Convert a IDL API command of SMESH::Mesh to a method call of python Mesh
601 * \param theCommand - Engine method called for this mesh
603 //================================================================================
605 void _pyMesh::Process( const Handle(_pyCommand)& theCommand )
607 // some methods of SMESH_Mesh interface needs special conversion
608 // to methods of Mesh python class
610 // 1. GetSubMesh(geom, name) + AddHypothesis(geom, algo)
611 // --> in Mesh_Algorithm.Create(mesh, geom, hypo, so)
612 // 2. AddHypothesis(geom, hyp)
613 // --> in Mesh_Algorithm.Hypothesis(hyp, args, so)
614 // 3. CreateGroupFromGEOM(type, name, grp)
615 // --> in Mesh.Group(grp, name="")
616 // 4. ExportToMED(f, auto_groups, version)
617 // --> in Mesh.ExportMED( f, auto_groups, version )
620 const TCollection_AsciiString method = theCommand->GetMethod();
621 // ----------------------------------------------------------------------
622 if ( method == "GetSubMesh" ) {
623 mySubmeshes.push_back( theCommand );
625 // ----------------------------------------------------------------------
626 else if ( method == "AddHypothesis" ) { // mesh.AddHypothesis(geom, HYPO )
627 myAddHypCmds.push_back( theCommand );
629 const _pyID& hypID = theCommand->GetArg( 2 );
630 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
631 if ( !hyp.IsNull() ) {
632 myHypos.push_back( hyp );
633 if ( hyp->GetMesh().IsEmpty() )
634 hyp->SetMesh( this->GetID() );
637 // ----------------------------------------------------------------------
638 else if ( method == "CreateGroupFromGEOM" ) {// (type, name, grp)
639 _pyID grp = theCommand->GetArg( 3 );
640 if ( sameGroupType( grp, theCommand->GetArg( 1 )) ) { // --> Group(grp)
641 theCommand->SetMethod( "Group" );
642 theCommand->RemoveArgs();
643 theCommand->SetArg( 1, grp );
646 AddMeshAccess( theCommand );
649 // ----------------------------------------------------------------------
650 else if ( method == "ExportToMED" ) { // ExportToMED() --> ExportMED()
651 theCommand->SetMethod( "ExportMED" );
653 // ----------------------------------------------------------------------
654 else if ( method == "CreateGroup" ) { // CreateGroup() --> CreateEmptyGroup()
655 theCommand->SetMethod( "CreateEmptyGroup" );
657 // ----------------------------------------------------------------------
658 else if ( method == "RemoveHypothesis" ) // (geom, hyp)
660 _pyID hypID = theCommand->GetArg( 2 );
662 // check if this mesh still has corresponding addition command
663 bool hasAddCmd = false;
664 list< Handle(_pyCommand) >::iterator cmd = myAddHypCmds.begin();
665 while ( cmd != myAddHypCmds.end() )
667 // AddHypothesis(geom, hyp)
668 if ( hypID == (*cmd)->GetArg( 2 )) { // erase both (add and remove) commands
671 cmd = myAddHypCmds.erase( cmd );
678 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
679 if ( ! hasAddCmd ) { // hypo addition already wrapped
680 // RemoveHypothesis(geom, hyp) --> RemoveHypothesis( hyp, geom=0 )
681 _pyID geom = theCommand->GetArg( 1 );
682 theCommand->RemoveArgs();
683 theCommand->SetArg( 1, hypID );
684 if ( geom != GetGeom() )
685 theCommand->SetArg( 2, geom );
687 // remove hyp from myHypos
688 myHypos.remove( hyp );
690 // add accessor method if necessary
693 if ( NeedMeshAccess( theCommand ))
694 // apply theCommand to the mesh wrapped by smeshpy mesh
695 AddMeshAccess( theCommand );
699 //================================================================================
701 * \brief Return True if addition of accesor method is needed
703 //================================================================================
705 bool _pyMesh::NeedMeshAccess( const Handle(_pyCommand)& theCommand )
707 // names of SMESH_Mesh methods fully equal to methods of class Mesh, so
708 // no conversion is needed for them at all:
709 static TStringSet sameMethods;
710 if ( sameMethods.empty() ) {
711 const char * names[] =
712 { "ExportDAT","ExportUNV","ExportSTL", "RemoveGroup","RemoveGroupWithContents",
713 "GetGroups","UnionGroups","IntersectGroups","CutGroups","GetLog","GetId","ClearLog",
714 "GetStudyId","HasDuplicatedGroupNamesMED","GetMEDMesh","NbNodes","NbElements",
715 "NbEdges","NbEdgesOfOrder","NbFaces","NbFacesOfOrder","NbTriangles",
716 "NbTrianglesOfOrder","NbQuadrangles","NbQuadranglesOfOrder","NbPolygons","NbVolumes",
717 "NbVolumesOfOrder","NbTetras","NbTetrasOfOrder","NbHexas","NbHexasOfOrder",
718 "NbPyramids","NbPyramidsOfOrder","NbPrisms","NbPrismsOfOrder","NbPolyhedrons",
719 "NbSubMesh","GetElementsId","GetElementsByType","GetNodesId","GetElementType",
720 "GetSubMeshElementsId","GetSubMeshNodesId","GetSubMeshElementType","Dump","GetNodeXYZ",
721 "GetNodeInverseElements","GetShapeID","GetShapeIDForElem","GetElemNbNodes",
722 "GetElemNode","IsMediumNode","IsMediumNodeOfAnyElem","ElemNbEdges","ElemNbFaces",
723 "IsPoly","IsQuadratic","BaryCenter","GetHypothesisList",
724 "" }; // <- mark of end
725 sameMethods.Insert( names );
728 return !sameMethods.Contains( theCommand->GetMethod() );
731 //================================================================================
733 * \brief Convert creation and addition of all algos and hypos
735 //================================================================================
737 void _pyMesh::Flush()
739 list < Handle(_pyCommand) >::iterator cmd, cmd2;
741 // try to convert algo addition like this:
742 // mesh.AddHypothesis(geom, ALGO ) --> ALGO = mesh.Algo()
743 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
745 Handle(_pyCommand) addCmd = *cmd;
746 _pyID algoID = addCmd->GetArg( 2 );
747 Handle(_pyHypothesis) algo = theGen->FindHyp( algoID );
748 if ( algo.IsNull() || !algo->IsAlgo() )
751 _pyID geom = addCmd->GetArg( 1 );
752 bool isLocalAlgo = ( geom != GetGeom() );
753 if ( algo->Addition2Creation( addCmd, this->GetID() )) // OK
755 // wrapped algo is created atfer mesh creation
756 GetCreationCmd()->AddDependantCmd( addCmd );
759 // mesh.AddHypothesis(geom, ALGO ) --> mesh.AlgoMethod(geom)
760 addCmd->SetArg( addCmd->GetNbArgs() + 1,
761 TCollection_AsciiString( "geom=" ) + geom );
762 // sm = mesh.GetSubMesh(geom, name) --> sm = ALGO.GetSubMesh()
763 for ( cmd2 = mySubmeshes.begin(); cmd2 != mySubmeshes.end(); ++cmd2 ) {
764 Handle(_pyCommand) subCmd = *cmd2;
765 if ( geom == subCmd->GetArg( 1 )) {
766 subCmd->SetObject( algo->GetID() );
767 subCmd->RemoveArgs();
768 addCmd->AddDependantCmd( subCmd );
773 else // KO - ALGO was already created
775 // mesh.AddHypothesis(geom, ALGO) --> mesh.AddHypothesis(ALGO, geom=0)
776 addCmd->RemoveArgs();
777 addCmd->SetArg( 1, algoID );
779 addCmd->SetArg( 2, geom );
783 // try to convert hypo addition like this:
784 // mesh.AddHypothesis(geom, HYPO ) --> HYPO = algo.Hypo()
785 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
787 Handle(_pyCommand) addCmd = *cmd;
788 _pyID hypID = addCmd->GetArg( 2 );
789 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
790 if ( hyp.IsNull() || hyp->IsAlgo() )
792 bool converted = hyp->Addition2Creation( addCmd, this->GetID() );
794 // mesh.AddHypothesis(geom, HYP) --> mesh.AddHypothesis(HYP, geom=0)
795 _pyID geom = addCmd->GetArg( 1 );
796 addCmd->RemoveArgs();
797 addCmd->SetArg( 1, hypID );
798 if ( geom != GetGeom() )
799 addCmd->SetArg( 2, geom );
803 // sm = mesh.GetSubMesh(geom, name) --> sm = mesh.GetMesh().GetSubMesh(geom, name)
804 // for ( cmd = mySubmeshes.begin(); cmd != mySubmeshes.end(); ++cmd ) {
805 // Handle(_pyCommand) subCmd = *cmd;
806 // if ( subCmd->GetNbArgs() > 0 )
807 // AddMeshAccess( subCmd );
809 myAddHypCmds.clear();
813 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
814 for ( ; hyp != myHypos.end(); ++hyp )
818 //================================================================================
820 * \brief MeshEditor convert its commands to ones of mesh
822 //================================================================================
824 _pyMeshEditor::_pyMeshEditor(const Handle(_pyCommand)& theCreationCmd):
825 _pyObject( theCreationCmd )
827 myMesh = theCreationCmd->GetObject();
828 myCreationCmdStr = theCreationCmd->GetString();
829 theCreationCmd->Clear();
832 //================================================================================
834 * \brief convert its commands to ones of mesh
836 //================================================================================
838 void _pyMeshEditor::Process( const Handle(_pyCommand)& theCommand)
840 // names of SMESH_MeshEditor methods fully equal to methods of class Mesh, so
841 // commands calling this methods are converted to calls of methods of Mesh
842 static TStringSet sameMethods;
843 if ( sameMethods.empty() ) {
844 const char * names[] = {
845 "RemoveElements","RemoveNodes","AddNode","AddEdge","AddFace","AddPolygonalFace",
846 "AddVolume","AddPolyhedralVolume","AddPolyhedralVolumeByFaces","MoveNode",
847 "InverseDiag","DeleteDiag","Reorient","ReorientObject","SplitQuad","SplitQuadObject",
848 "BestSplit","Smooth","SmoothObject","SmoothParametric","SmoothParametricObject",
849 "ConvertToQuadratic","ConvertFromQuadratic","RenumberNodes","RenumberElements",
850 "RotationSweep","RotationSweepObject","ExtrusionSweep","AdvancedExtrusion",
851 "ExtrusionSweepObject","ExtrusionSweepObject1D","ExtrusionSweepObject2D","Mirror",
852 "MirrorObject","Translate","TranslateObject","Rotate","RotateObject",
853 "FindCoincidentNodes","FindCoincidentNodesOnPart","MergeNodes","FindEqualElements",
854 "MergeElements","MergeEqualElements","SewFreeBorders","SewConformFreeBorders",
855 "SewBorderToSide","SewSideElements","ChangeElemNodes","GetLastCreatedNodes",
856 "GetLastCreatedElems",
857 "" }; // <- mark of end
858 sameMethods.Insert( names );
861 if ( sameMethods.Contains( theCommand->GetMethod() )) {
862 theCommand->SetObject( myMesh );
865 // editor creation command is needed only if any editor function is called
866 if ( !myCreationCmdStr.IsEmpty() ) {
867 GetCreationCmd()->GetString() = myCreationCmdStr;
868 myCreationCmdStr.Clear();
873 //================================================================================
875 * \brief _pyHypothesis constructor
876 * \param theCreationCmd -
878 //================================================================================
880 _pyHypothesis::_pyHypothesis(const Handle(_pyCommand)& theCreationCmd):
881 _pyObject( theCreationCmd )
883 myIsAlgo = myIsWrapped = /*myIsConverted = myIsLocal = myDim = */false;
886 //================================================================================
888 * \brief Creates algorithm or hypothesis
889 * \param theCreationCmd - The engine command creating a hypothesis
890 * \retval Handle(_pyHypothesis) - Result _pyHypothesis
892 //================================================================================
894 Handle(_pyHypothesis) _pyHypothesis::NewHypothesis( const Handle(_pyCommand)& theCreationCmd)
896 // theCreationCmd: CreateHypothesis( "theHypType", "theLibName" )
897 ASSERT (( theCreationCmd->GetMethod() == "CreateHypothesis"));
899 Handle(_pyHypothesis) hyp, algo;
902 const TCollection_AsciiString & hypTypeQuoted = theCreationCmd->GetArg( 1 );
903 if ( hypTypeQuoted.IsEmpty() )
906 TCollection_AsciiString hypType =
907 hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
909 algo = new _pyAlgorithm( theCreationCmd );
910 hyp = new _pyHypothesis( theCreationCmd );
912 // 1D Regular_1D ----------
913 if ( hypType == "Regular_1D" ) {
914 // set mesh's method creating algo,
915 // i.e. convertion result will be "regular1d = Mesh.Segment()",
916 // and set hypType by which algo creating a hypothesis is searched for
917 algo->SetConvMethodAndType("Segment", hypType.ToCString());
919 else if ( hypType == "CompositeSegment_1D" ) {
920 algo->SetConvMethodAndType("Segment", "Regular_1D");
921 algo->myArgs.Append( "algo=smesh.COMPOSITE");
923 else if ( hypType == "LocalLength" ) {
924 // set algo's method creating hyp, and algo type
925 hyp->SetConvMethodAndType( "LocalLength", "Regular_1D");
926 // set method whose 1 arg will become the 1-st arg of hyp creation command
927 // i.e. convertion result will be "locallength = regular1d.LocalLength(<arg of SetLength()>)"
928 hyp->AddArgMethod( "SetLength" );
930 else if ( hypType == "NumberOfSegments" ) {
931 hyp = new _pyNumberOfSegmentsHyp( theCreationCmd );
932 hyp->SetConvMethodAndType( "NumberOfSegments", "Regular_1D");
933 // arg of SetNumberOfSegments() will become the 1-st arg of hyp creation command
934 hyp->AddArgMethod( "SetNumberOfSegments" );
935 // arg of SetScaleFactor() will become the 2-nd arg of hyp creation command
936 hyp->AddArgMethod( "SetScaleFactor" );
938 else if ( hypType == "Arithmetic1D" ) {
939 hyp = new _pyComplexParamHypo( theCreationCmd );
940 hyp->SetConvMethodAndType( "Arithmetic1D", "Regular_1D");
942 else if ( hypType == "StartEndLength" ) {
943 hyp = new _pyComplexParamHypo( theCreationCmd );
944 hyp->SetConvMethodAndType( "StartEndLength", "Regular_1D");
946 else if ( hypType == "Deflection1D" ) {
947 hyp->SetConvMethodAndType( "Deflection1D", "Regular_1D");
948 hyp->AddArgMethod( "SetDeflection" );
950 else if ( hypType == "Propagation" ) {
951 hyp->SetConvMethodAndType( "Propagation", "Regular_1D");
953 else if ( hypType == "QuadraticMesh" ) {
954 hyp->SetConvMethodAndType( "QuadraticMesh", "Regular_1D");
956 else if ( hypType == "AutomaticLength" ) {
957 hyp->SetConvMethodAndType( "AutomaticLength", "Regular_1D");
958 hyp->AddArgMethod( "SetFineness");
960 else if ( hypType == "SegmentLengthAroundVertex" ) {
961 hyp = new _pySegmentLengthAroundVertexHyp( theCreationCmd );
962 hyp->SetConvMethodAndType( "LengthNearVertex", "Regular_1D" );
963 hyp->AddArgMethod( "SetLength" );
965 // 1D Python_1D ----------
966 else if ( hypType == "Python_1D" ) {
967 algo->SetConvMethodAndType( "Segment", hypType.ToCString());
968 algo->myArgs.Append( "algo=smesh.PYTHON");
970 else if ( hypType == "PythonSplit1D" ) {
971 hyp->SetConvMethodAndType( "PythonSplit1D", "Python_1D");
972 hyp->AddArgMethod( "SetNumberOfSegments");
973 hyp->AddArgMethod( "SetPythonLog10RatioFunction");
975 // MEFISTO_2D ----------
976 else if ( hypType == "MEFISTO_2D" ) { // MEFISTO_2D
977 algo->SetConvMethodAndType( "Triangle", hypType.ToCString());
979 else if ( hypType == "MaxElementArea" ) {
980 hyp->SetConvMethodAndType( "MaxElementArea", "MEFISTO_2D");
981 hyp->SetConvMethodAndType( "MaxElementArea", "NETGEN_2D_ONLY");
982 hyp->AddArgMethod( "SetMaxElementArea");
984 else if ( hypType == "LengthFromEdges" ) {
985 hyp->SetConvMethodAndType( "LengthFromEdges", "MEFISTO_2D");
986 hyp->SetConvMethodAndType( "LengthFromEdges", "NETGEN_2D_ONLY");
988 // Quadrangle_2D ----------
989 else if ( hypType == "Quadrangle_2D" ) {
990 algo->SetConvMethodAndType( "Quadrangle" , hypType.ToCString());
992 else if ( hypType == "QuadranglePreference" ) {
993 hyp->SetConvMethodAndType( "QuadranglePreference", "Quadrangle_2D");
994 hyp->SetConvMethodAndType( "QuadranglePreference", "NETGEN_2D_ONLY");
997 // else if ( hypType == "NETGEN_2D") { // 1D-2D
998 // algo->SetConvMethodAndType( "Triangle" , hypType.ToCString());
999 // algo->myArgs.Append( "algo=smesh.NETGEN" );
1001 else if ( hypType == "NETGEN_2D_ONLY") { // 2D
1002 algo->SetConvMethodAndType( "Triangle" , hypType.ToCString());
1003 algo->myArgs.Append( "algo=smesh.NETGEN_2D" );
1005 else if ( hypType == "NETGEN_3D") { // 3D
1006 algo->SetConvMethodAndType( "Tetrahedron" , hypType.ToCString());
1007 algo->myArgs.Append( "algo=smesh.NETGEN" );
1009 else if ( hypType == "MaxElementVolume") {
1010 hyp->SetConvMethodAndType( "MaxElementVolume", "NETGEN_3D");
1011 hyp->AddArgMethod( "SetMaxElementVolume" );
1013 // GHS3D_3D ----------
1014 else if ( hypType == "GHS3D_3D" ) {
1015 algo->SetConvMethodAndType( "Tetrahedron", hypType.ToCString());
1016 algo->myArgs.Append( "algo=smesh.GHS3D" );
1018 // Hexa_3D ---------
1019 else if ( hypType == "Hexa_3D" ) {
1020 algo->SetConvMethodAndType( "Hexahedron", hypType.ToCString());
1022 // Repetitive Projection_1D ---------
1023 else if ( hypType == "Projection_1D" ) {
1024 algo->SetConvMethodAndType( "Projection1D", hypType.ToCString());
1026 else if ( hypType == "ProjectionSource1D" ) {
1027 hyp->SetConvMethodAndType( "SourceEdge", "Projection_1D");
1028 hyp->AddArgMethod( "SetSourceEdge");
1029 hyp->AddArgMethod( "SetSourceMesh");
1030 // 2 args of SetVertexAssociation() will become the 3-th and 4-th args of hyp creation command
1031 hyp->AddArgMethod( "SetVertexAssociation", 2 );
1033 // Projection_2D ---------
1034 else if ( hypType == "Projection_2D" ) {
1035 algo->SetConvMethodAndType( "Projection2D", hypType.ToCString());
1037 else if ( hypType == "ProjectionSource2D" ) {
1038 hyp->SetConvMethodAndType( "SourceFace", "Projection_2D");
1039 hyp->AddArgMethod( "SetSourceFace");
1040 hyp->AddArgMethod( "SetSourceMesh");
1041 hyp->AddArgMethod( "SetVertexAssociation", 4 );
1043 // Projection_3D ---------
1044 else if ( hypType == "Projection_3D" ) {
1045 algo->SetConvMethodAndType( "Projection3D", hypType.ToCString());
1047 else if ( hypType == "ProjectionSource3D" ) {
1048 hyp->SetConvMethodAndType( "SourceShape3D", "Projection_3D");
1049 hyp->AddArgMethod( "SetSource3DShape");
1050 hyp->AddArgMethod( "SetSourceMesh");
1051 hyp->AddArgMethod( "SetVertexAssociation", 4 );
1053 // Prism_3D ---------
1054 else if ( hypType == "Prism_3D" ) {
1055 algo->SetConvMethodAndType( "Prism", hypType.ToCString());
1057 // RadialPrism_3D ---------
1058 else if ( hypType == "RadialPrism_3D" ) {
1059 algo->SetConvMethodAndType( "Prism", hypType.ToCString());
1061 else if ( hypType == "NumberOfLayers" ) {
1062 hyp->SetConvMethodAndType( "NumberOfLayers", "RadialPrism_3D");
1063 hyp->AddArgMethod( "SetNumberOfLayers" );
1065 else if ( hypType == "LayerDistribution" ) {
1066 hyp = new _pyLayerDistributionHypo( theCreationCmd );
1067 hyp->SetConvMethodAndType( "LayerDistribution", "RadialPrism_3D");
1070 if ( algo->IsValid() ) {
1076 //================================================================================
1078 * \brief Convert the command adding a hypothesis to mesh into a smesh command
1079 * \param theCmd - The command like mesh.AddHypothesis( geom, hypo )
1080 * \param theAlgo - The algo that can create this hypo
1081 * \retval bool - false if the command cant be converted
1083 //================================================================================
1085 bool _pyHypothesis::Addition2Creation( const Handle(_pyCommand)& theCmd,
1086 const _pyID& theMesh)
1088 ASSERT(( theCmd->GetMethod() == "AddHypothesis" ));
1090 if ( !IsWrappable( theMesh ))
1093 myGeom = theCmd->GetArg( 1 );
1095 Handle(_pyHypothesis) algo;
1097 // find algo created on myGeom in theMesh
1098 algo = theGen->FindAlgo( myGeom, theMesh, this );
1099 if ( algo.IsNull() )
1101 algo->GetCreationCmd()->AddDependantCmd( theCmd );
1105 // mesh.AddHypothesis(geom,hyp) --> hyp = <theMesh or algo>.myCreationMethod(args)
1106 theCmd->SetResultValue( GetID() );
1107 theCmd->SetObject( IsAlgo() ? theMesh : algo->GetID());
1108 theCmd->SetMethod( IsAlgo() ? GetAlgoCreationMethod() : GetCreationMethod( algo->GetAlgoType() ));
1110 theCmd->RemoveArgs();
1111 for ( int i = 1; i <= myArgs.Length(); ++i ) {
1112 if ( !myArgs( i ).IsEmpty() )
1113 theCmd->SetArg( i, myArgs( i ));
1115 theCmd->SetArg( i, "[]");
1117 // set a new creation command
1118 GetCreationCmd()->Clear();
1119 SetCreationCmd( theCmd );
1121 // clear commands setting arg values
1122 list < Handle(_pyCommand) >::iterator argCmd = myArgCommands.begin();
1123 for ( ; argCmd != myArgCommands.end(); ++argCmd )
1126 // set unknown arg commands after hypo creation
1127 Handle(_pyCommand) afterCmd = myIsWrapped ? theCmd : GetCreationCmd();
1128 list<Handle(_pyCommand)>::iterator cmd = myUnknownCommands.begin();
1129 for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
1130 afterCmd->AddDependantCmd( *cmd );
1136 //================================================================================
1138 * \brief Remember hypothesis parameter values
1139 * \param theCommand - The called hypothesis method
1141 //================================================================================
1143 void _pyHypothesis::Process( const Handle(_pyCommand)& theCommand)
1145 ASSERT( !myIsAlgo );
1148 for ( int i = 1; i <= myArgMethods.Length(); ++i ) {
1149 if ( myArgMethods( i ) == theCommand->GetMethod() ) {
1150 while ( myArgs.Length() < nbArgs + myNbArgsByMethod( i ))
1151 myArgs.Append( "[]" );
1152 for ( int iArg = 1; iArg <= myNbArgsByMethod( i ); ++iArg )
1153 myArgs( nbArgs + iArg ) = theCommand->GetArg( iArg ); // arg value
1154 myArgCommands.push_back( theCommand );
1157 nbArgs += myNbArgsByMethod( i );
1159 myUnknownCommands.push_back( theCommand );
1162 //================================================================================
1164 * \brief Finish conversion
1166 //================================================================================
1168 void _pyHypothesis::Flush()
1170 if ( IsWrapped() ) {
1173 list < Handle(_pyCommand) >::iterator cmd = myArgCommands.begin();
1174 for ( ; cmd != myArgCommands.end(); ++cmd ) {
1175 // Add access to a wrapped mesh
1176 theGen->AddMeshAccessorMethod( *cmd );
1177 // Add access to a wrapped algorithm
1178 theGen->AddAlgoAccessorMethod( *cmd );
1180 cmd = myUnknownCommands.begin();
1181 for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
1182 // Add access to a wrapped mesh
1183 theGen->AddMeshAccessorMethod( *cmd );
1184 // Add access to a wrapped algorithm
1185 theGen->AddAlgoAccessorMethod( *cmd );
1188 // forget previous hypothesis modifications
1189 myArgCommands.clear();
1190 myUnknownCommands.clear();
1193 //================================================================================
1195 * \brief clear creation, arg and unkown commands
1197 //================================================================================
1199 void _pyHypothesis::ClearAllCommands()
1201 GetCreationCmd()->Clear();
1202 list<Handle(_pyCommand)>::iterator cmd = myArgCommands.begin();
1203 for ( ; cmd != myArgCommands.end(); ++cmd )
1205 cmd = myUnknownCommands.begin();
1206 for ( ; cmd != myUnknownCommands.end(); ++cmd )
1210 //================================================================================
1212 * \brief Remember hypothesis parameter values
1213 * \param theCommand - The called hypothesis method
1215 //================================================================================
1217 void _pyComplexParamHypo::Process( const Handle(_pyCommand)& theCommand)
1219 // ex: hyp.SetLength(start, 1)
1220 // hyp.SetLength(end, 0)
1221 ASSERT(( theCommand->GetMethod() == "SetLength" ));
1222 ASSERT(( theCommand->GetArg( 2 ).IsIntegerValue() ));
1223 int i = 2 - theCommand->GetArg( 2 ).IntegerValue();
1224 while ( myArgs.Length() < i )
1225 myArgs.Append( "[]" );
1226 myArgs( i ) = theCommand->GetArg( 1 ); // arg value
1227 myArgCommands.push_back( theCommand );
1230 //================================================================================
1232 * \brief Convert methods of 1D hypotheses to my own methods
1233 * \param theCommand - The called hypothesis method
1235 //================================================================================
1237 void _pyLayerDistributionHypo::Process( const Handle(_pyCommand)& theCommand)
1239 if ( theCommand->GetMethod() != "SetLayerDistribution" )
1242 _pyID newName; // name for 1D hyp = "HypType" + "_Distribution"
1244 const _pyID& hyp1dID = theCommand->GetArg( 1 );
1245 Handle(_pyHypothesis) hyp1d = theGen->FindHyp( hyp1dID );
1246 if ( hyp1d.IsNull() ) // apparently hypId changed at study restoration
1248 else if ( !my1dHyp.IsNull() && hyp1dID != my1dHyp->GetID() ) {
1249 // 1D hypo is already set, so distribution changes and the old
1250 // 1D hypo is thrown away
1251 my1dHyp->ClearAllCommands();
1254 if ( my1dHyp.IsNull() )
1255 return; // something wrong :(
1257 // make a new name for 1D hyp = "HypType" + "_Distribution"
1258 if ( my1dHyp->GetCreationCmd()->GetMethod() == "CreateHypothesis" ) {
1259 // not yet converted creation cmd
1260 TCollection_AsciiString hypTypeQuoted = my1dHyp->GetCreationCmd()->GetArg(1);
1261 TCollection_AsciiString hypType = hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
1262 newName = hypType + "_Distribution";
1263 my1dHyp->GetCreationCmd()->SetResultValue( newName );
1266 // already converted creation cmd
1267 newName = my1dHyp->GetCreationCmd()->GetResultValue();
1270 // as creation of 1D hyp was written later then it's edition,
1271 // we need to find all it's edition calls and process them
1272 list< Handle(_pyCommand) >& cmds = theGen->GetCommands();
1273 list< Handle(_pyCommand) >::iterator cmdIt = cmds.begin();
1274 for ( ; cmdIt != cmds.end(); ++cmdIt ) {
1275 const _pyID& objID = (*cmdIt)->GetObject();
1276 if ( objID == hyp1dID ) {
1277 my1dHyp->Process( *cmdIt );
1278 my1dHyp->GetCreationCmd()->AddDependantCmd( *cmdIt );
1279 ( *cmdIt )->SetObject( newName );
1282 if ( !myArgCommands.empty() )
1283 myArgCommands.front()->Clear();
1284 theCommand->SetArg( 1, newName );
1285 myArgCommands.push_back( theCommand );
1286 // copy hyp1d's creation method and args
1287 // myCreationMethod = hyp1d->GetCreationMethod();
1288 // myArgs = hyp1d->GetArgs();
1289 // // make them cleared at conversion
1290 // myArgCommands = hyp1d->GetArgCommands();
1292 // // to be cleared at convertion only
1293 // myArgCommands.push_back( theCommand );
1296 //================================================================================
1299 * \param theAdditionCmd -
1303 //================================================================================
1305 bool _pyLayerDistributionHypo::Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
1306 const _pyID& theMesh)
1308 myIsWrapped = false;
1310 if ( my1dHyp.IsNull() )
1313 // set "SetLayerDistribution()" after addition cmd
1314 theAdditionCmd->AddDependantCmd( myArgCommands.front() );
1316 _pyID geom = theAdditionCmd->GetArg( 1 );
1318 my1dHyp->SetMesh( theMesh );
1319 if ( !my1dHyp->Addition2Creation( theAdditionCmd, theMesh ))
1322 // clear "SetLayerDistribution()" cmd
1323 myArgCommands.front()->Clear();
1325 // Convert my creation => me = RadialPrismAlgo.Get3DHypothesis()
1327 // find RadialPrism algo created on <geom> for theMesh
1328 Handle(_pyHypothesis) algo = theGen->FindAlgo( geom, theMesh, this );
1329 if ( !algo.IsNull() ) {
1330 GetCreationCmd()->SetObject( algo->GetID() );
1331 GetCreationCmd()->SetMethod( "Get3DHypothesis" );
1332 GetCreationCmd()->RemoveArgs();
1333 theAdditionCmd->AddDependantCmd( GetCreationCmd() );
1339 //================================================================================
1343 //================================================================================
1345 void _pyLayerDistributionHypo::Flush()
1347 //my1dHyp.Nullify();
1348 //_pyHypothesis::Flush();
1351 //================================================================================
1353 * \brief additionally to Addition2Creation, clears SetDistrType() command
1354 * \param theCmd - AddHypothesis() command
1355 * \param theMesh - mesh to which a hypothesis is added
1356 * \retval bool - convertion result
1358 //================================================================================
1360 bool _pyNumberOfSegmentsHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
1361 const _pyID& theMesh)
1363 if ( IsWrappable( theMesh ) && myArgs.Length() > 1 ) {
1364 // scale factor (2-nd arg) is provided: clear SetDistrType(1) command
1365 bool scaleDistrType = false;
1366 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnknownCommands.rbegin();
1367 for ( ; cmd != myUnknownCommands.rend(); ++cmd ) {
1368 if ( (*cmd)->GetMethod() == "SetDistrType" ) {
1369 if ( (*cmd)->GetArg( 1 ) == "1" ) {
1370 scaleDistrType = true;
1373 else if ( !scaleDistrType ) {
1374 // distribution type changed: remove scale factor from args
1375 myArgs.Remove( 2, myArgs.Length() );
1381 return _pyHypothesis::Addition2Creation( theCmd, theMesh );
1384 //================================================================================
1386 * \brief remove repeated commands defining distribution
1388 //================================================================================
1390 void _pyNumberOfSegmentsHyp::Flush()
1392 // find number of the last SetDistrType() command
1393 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnknownCommands.rbegin();
1394 int distrTypeNb = 0;
1395 for ( ; !distrTypeNb && cmd != myUnknownCommands.rend(); ++cmd )
1396 if ( (*cmd)->GetMethod() == "SetDistrType" )
1397 distrTypeNb = (*cmd)->GetOrderNb();
1399 // clear commands before the last SetDistrType()
1400 list<Handle(_pyCommand)> * cmds[2] = { &myArgCommands, &myUnknownCommands };
1401 for ( int i = 0; i < 2; ++i ) {
1402 set<TCollection_AsciiString> uniqueMethods;
1403 list<Handle(_pyCommand)> & cmdList = *cmds[i];
1404 for ( cmd = cmdList.rbegin(); cmd != cmdList.rend(); ++cmd )
1406 bool clear = ( (*cmd)->GetOrderNb() < distrTypeNb );
1407 const TCollection_AsciiString& method = (*cmd)->GetMethod();
1408 if ( !clear || method == "SetNumberOfSegments" ) {
1409 bool isNewInSet = uniqueMethods.insert( method ).second;
1410 clear = !isNewInSet;
1419 //================================================================================
1421 * \brief Convert the command adding "SegmentLengthAroundVertex" to mesh
1422 * into regular1D.LengthNearVertex( length, vertex )
1423 * \param theCmd - The command like mesh.AddHypothesis( vertex, SegmentLengthAroundVertex )
1424 * \param theMesh - The mesh needing this hypo
1425 * \retval bool - false if the command cant be converted
1427 //================================================================================
1429 bool _pySegmentLengthAroundVertexHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
1430 const _pyID& theMeshID)
1432 if ( IsWrappable( theMeshID )) {
1434 _pyID vertex = theCmd->GetArg( 1 );
1436 // the problem here is that segment algo will not be found
1437 // by pyHypothesis::Addition2Creation() for <vertex>, so we try to find
1438 // geometry where segment algorithm is assigned
1439 Handle(_pyHypothesis) algo;
1440 _pyID geom = vertex;
1441 while ( algo.IsNull() && !geom.IsEmpty()) {
1442 // try to find geom as a father of <vertex>
1443 geom = FatherID( geom );
1444 algo = theGen->FindAlgo( geom, theMeshID, this );
1446 if ( algo.IsNull() )
1447 return false; // also possible to find geom as brother of veretex...
1448 // set geom instead of vertex
1449 theCmd->SetArg( 1, geom );
1451 // set vertex as a second arg
1452 if ( myArgs.Length() < 1) myArgs.Append( "1" ); // :(
1453 myArgs.Append( vertex );
1455 // mesh.AddHypothesis(vertex, SegmentLengthAroundVertex) -->
1456 // theMeshID.LengthNearVertex( length, vertex )
1457 return _pyHypothesis::Addition2Creation( theCmd, theMeshID );
1462 //================================================================================
1464 * \brief _pyAlgorithm constructor
1465 * \param theCreationCmd - The command like "algo = smeshgen.CreateHypothesis(type,lib)"
1467 //================================================================================
1469 _pyAlgorithm::_pyAlgorithm(const Handle(_pyCommand)& theCreationCmd)
1470 : _pyHypothesis( theCreationCmd )
1475 //================================================================================
1477 * \brief Convert the command adding an algorithm to mesh
1478 * \param theCmd - The command like mesh.AddHypothesis( geom, algo )
1479 * \param theMesh - The mesh needing this algo
1480 * \retval bool - false if the command cant be converted
1482 //================================================================================
1484 bool _pyAlgorithm::Addition2Creation( const Handle(_pyCommand)& theCmd,
1485 const _pyID& theMeshID)
1487 // mesh.AddHypothesis(geom,algo) --> theMeshID.myCreationMethod()
1488 if ( _pyHypothesis::Addition2Creation( theCmd, theMeshID )) {
1489 theGen->SetAccessorMethod( GetID(), "GetAlgorithm()" );
1495 //================================================================================
1497 * \brief Return starting position of a part of python command
1498 * \param thePartIndex - The index of command part
1499 * \retval int - Part position
1501 //================================================================================
1503 int _pyCommand::GetBegPos( int thePartIndex )
1507 if ( myBegPos.Length() < thePartIndex )
1509 return myBegPos( thePartIndex );
1512 //================================================================================
1514 * \brief Store starting position of a part of python command
1515 * \param thePartIndex - The index of command part
1516 * \param thePosition - Part position
1518 //================================================================================
1520 void _pyCommand::SetBegPos( int thePartIndex, int thePosition )
1522 while ( myBegPos.Length() < thePartIndex )
1523 myBegPos.Append( UNKNOWN );
1524 myBegPos( thePartIndex ) = thePosition;
1527 //================================================================================
1529 * \brief Returns whitespace symbols at the line beginning
1530 * \retval TCollection_AsciiString - result
1532 //================================================================================
1534 TCollection_AsciiString _pyCommand::GetIndentation()
1537 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
1538 GetWord( myString, end, true );
1540 end = GetBegPos( RESULT_IND );
1541 return myString.SubString( 1, end - 1 );
1544 //================================================================================
1546 * \brief Return substring of python command looking like ResultValue = Obj.Meth()
1547 * \retval const TCollection_AsciiString & - ResultValue substring
1549 //================================================================================
1551 const TCollection_AsciiString & _pyCommand::GetResultValue()
1553 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
1555 int begPos = myString.Location( "=", 1, Length() );
1557 myRes = GetWord( myString, begPos, false );
1560 SetBegPos( RESULT_IND, begPos );
1565 //================================================================================
1567 * \brief Return substring of python command looking like ResVal = Object.Meth()
1568 * \retval const TCollection_AsciiString & - Object substring
1570 //================================================================================
1572 const TCollection_AsciiString & _pyCommand::GetObject()
1574 if ( GetBegPos( OBJECT_IND ) == UNKNOWN )
1577 int begPos = GetBegPos( RESULT_IND ) + myRes.Length();
1579 begPos = myString.Location( "=", 1, Length() ) + 1;
1581 myObj = GetWord( myString, begPos, true );
1582 SetBegPos( OBJECT_IND, begPos );
1588 //================================================================================
1590 * \brief Return substring of python command looking like ResVal = Obj.Method()
1591 * \retval const TCollection_AsciiString & - Method substring
1593 //================================================================================
1595 const TCollection_AsciiString & _pyCommand::GetMethod()
1597 if ( GetBegPos( METHOD_IND ) == UNKNOWN )
1600 int begPos = GetBegPos( OBJECT_IND ) + myObj.Length();
1601 bool forward = true;
1603 begPos = myString.Location( "(", 1, Length() ) - 1;
1607 myMeth = GetWord( myString, begPos, forward );
1608 SetBegPos( METHOD_IND, begPos );
1614 //================================================================================
1616 * \brief Return substring of python command looking like ResVal = Obj.Meth(Arg1,...)
1617 * \retval const TCollection_AsciiString & - Arg<index> substring
1619 //================================================================================
1621 const TCollection_AsciiString & _pyCommand::GetArg( int index )
1623 if ( GetBegPos( ARG1_IND ) == UNKNOWN )
1626 int begPos = GetBegPos( METHOD_IND ) + myMeth.Length();
1628 begPos = myString.Location( "(", 1, Length() ) + 1;
1630 int i = 0, prevLen = 0;
1631 while ( begPos != EMPTY ) {
1633 // check if we are looking at the closing parenthesis
1634 while ( begPos <= Length() && isspace( myString.Value( begPos )))
1636 if ( begPos > Length() || myString.Value( begPos ) == ')' )
1638 myArgs.Append( GetWord( myString, begPos, true, true ));
1639 SetBegPos( ARG1_IND + i, begPos );
1640 prevLen = myArgs.Last().Length();
1642 myArgs.Remove( myArgs.Length() ); // no more args
1646 if ( myArgs.Length() < index )
1647 return theEmptyString;
1648 return myArgs( index );
1651 //================================================================================
1653 * \brief Check if char is a word part
1654 * \param c - The character to check
1655 * \retval bool - The check result
1657 //================================================================================
1659 static inline bool isWord(const char c, const bool dotIsWord)
1662 !isspace(c) && c != ',' && c != '=' && c != ')' && c != '(' && ( dotIsWord || c != '.');
1665 //================================================================================
1667 * \brief Looks for a word in the string and returns word's beginning
1668 * \param theString - The input string
1669 * \param theStartPos - The position to start the search, returning word's beginning
1670 * \param theForward - The search direction
1671 * \retval TCollection_AsciiString - The found word
1673 //================================================================================
1675 TCollection_AsciiString _pyCommand::GetWord( const TCollection_AsciiString & theString,
1677 const bool theForward,
1678 const bool dotIsWord )
1680 int beg = theStartPos, end = theStartPos;
1681 theStartPos = EMPTY;
1682 if ( beg < 1 || beg > theString.Length() )
1683 return theEmptyString;
1685 if ( theForward ) { // search forward
1687 while ( beg <= theString.Length() && !isWord( theString.Value( beg ), dotIsWord))
1689 if ( beg > theString.Length() )
1690 return theEmptyString; // no word found
1693 while ( end <= theString.Length() && isWord( theString.Value( end ), dotIsWord))
1697 else { // search backward
1699 while ( end > 0 && !isWord( theString.Value( end ), dotIsWord))
1702 return theEmptyString; // no word found
1704 while ( beg > 0 && isWord( theString.Value( beg ), dotIsWord))
1709 //cout << theString << " ---- " << beg << " - " << end << endl;
1710 return theString.SubString( beg, end );
1713 //================================================================================
1715 * \brief Look for position where not space char is
1716 * \param theString - The string
1717 * \param thePos - The position to search from and which returns result
1718 * \retval bool - false if there are only space after thePos in theString
1722 //================================================================================
1724 bool _pyCommand::SkipSpaces( const TCollection_AsciiString & theString, int & thePos )
1726 if ( thePos < 1 || thePos > theString.Length() )
1729 while ( thePos <= theString.Length() && isspace( theString.Value( thePos )))
1732 return thePos <= theString.Length();
1735 //================================================================================
1737 * \brief Modify a part of the command
1738 * \param thePartIndex - The index of the part
1739 * \param thePart - The new part string
1740 * \param theOldPart - The old part
1742 //================================================================================
1744 void _pyCommand::SetPart(int thePartIndex, const TCollection_AsciiString& thePart,
1745 TCollection_AsciiString& theOldPart)
1747 int pos = GetBegPos( thePartIndex );
1748 if ( pos <= Length() && theOldPart != thePart)
1750 TCollection_AsciiString seperator;
1752 pos = GetBegPos( thePartIndex + 1 );
1753 if ( pos < 1 ) return;
1754 switch ( thePartIndex ) {
1755 case RESULT_IND: seperator = " = "; break;
1756 case OBJECT_IND: seperator = "."; break;
1757 case METHOD_IND: seperator = "()"; break;
1761 myString.Remove( pos, theOldPart.Length() );
1762 if ( !seperator.IsEmpty() )
1763 myString.Insert( pos , seperator );
1764 myString.Insert( pos, thePart );
1765 // update starting positions of the following parts
1766 int posDelta = thePart.Length() + seperator.Length() - theOldPart.Length();
1767 for ( int i = thePartIndex + 1; i <= myBegPos.Length(); ++i ) {
1768 if ( myBegPos( i ) > 0 )
1769 myBegPos( i ) += posDelta;
1771 theOldPart = thePart;
1775 //================================================================================
1777 * \brief Set agrument
1778 * \param index - The argument index, it counts from 1
1779 * \param theArg - The argument string
1781 //================================================================================
1783 void _pyCommand::SetArg( int index, const TCollection_AsciiString& theArg)
1786 int argInd = ARG1_IND + index - 1;
1787 int pos = GetBegPos( argInd );
1788 if ( pos < 1 ) // no index-th arg exist, append inexistent args
1790 // find a closing parenthesis
1791 if ( int lastArgInd = GetNbArgs() ) {
1792 pos = GetBegPos( ARG1_IND + lastArgInd - 1 ) + GetArg( lastArgInd ).Length();
1793 while ( pos > 0 && pos <= Length() && myString.Value( pos ) != ')' )
1798 while ( pos > 0 && myString.Value( pos ) != ')' )
1801 if ( pos < 1 || myString.Value( pos ) != ')' ) { // no parentheses at all
1805 while ( myArgs.Length() < index ) {
1806 if ( myArgs.Length() )
1807 myString.Insert( pos++, "," );
1808 myArgs.Append("None");
1809 myString.Insert( pos, myArgs.Last() );
1810 SetBegPos( ARG1_IND + myArgs.Length() - 1, pos );
1811 pos += myArgs.Last().Length();
1814 SetPart( argInd, theArg, myArgs( index ));
1817 //================================================================================
1819 * \brief Empty arg list
1821 //================================================================================
1823 void _pyCommand::RemoveArgs()
1825 if ( int pos = myString.Location( '(', 1, Length() ))
1826 myString.Trunc( pos );
1829 if ( myBegPos.Length() >= ARG1_IND )
1830 myBegPos.Remove( ARG1_IND, myBegPos.Length() );
1833 //================================================================================
1835 * \brief Set dependent commands after this one
1837 //================================================================================
1839 bool _pyCommand::SetDependentCmdsAfter() const
1841 bool orderChanged = false;
1842 list< Handle(_pyCommand)>::const_reverse_iterator cmd = myDependentCmds.rbegin();
1843 for ( ; cmd != myDependentCmds.rend(); ++cmd ) {
1844 if ( (*cmd)->GetOrderNb() < GetOrderNb() ) {
1845 orderChanged = true;
1846 theGen->SetCommandAfter( *cmd, this );
1847 (*cmd)->SetDependentCmdsAfter();
1850 return orderChanged;
1852 //================================================================================
1854 * \brief Insert accessor method after theObjectID
1855 * \param theObjectID - id of the accessed object
1856 * \param theAcsMethod - name of the method giving access to the object
1857 * \retval bool - false if theObjectID is not found in the command string
1859 //================================================================================
1861 bool _pyCommand::AddAccessorMethod( _pyID theObjectID, const char* theAcsMethod )
1863 if ( !theAcsMethod )
1865 // start object search from the object, i.e. ignore result
1867 int beg = GetBegPos( OBJECT_IND );
1868 if ( beg < 1 || beg > Length() )
1871 while (( beg = myString.Location( theObjectID, beg, Length() )))
1873 // check that theObjectID is not just a part of a longer ID
1874 int afterEnd = beg + theObjectID.Length();
1875 Standard_Character c = myString.Value( afterEnd );
1876 if ( !isalnum( c ) && c != ':' ) {
1877 // check if accessor method already present
1879 myString.Location( (char*) theAcsMethod, afterEnd, Length() ) != afterEnd+1) {
1881 int oldLen = Length();
1882 myString.Insert( afterEnd, (char*) theAcsMethod );
1883 myString.Insert( afterEnd, "." );
1884 // update starting positions of the parts following the modified one
1885 int posDelta = Length() - oldLen;
1886 for ( int i = 1; i <= myBegPos.Length(); ++i ) {
1887 if ( myBegPos( i ) > afterEnd )
1888 myBegPos( i ) += posDelta;
1893 beg = afterEnd; // is a part - next search
1898 //================================================================================
1900 * \brief Return method name giving access to an interaface object wrapped by python class
1901 * \retval const char* - method name
1903 //================================================================================
1905 const char* _pyObject::AccessorMethod() const
1909 //================================================================================
1911 * \brief Return ID of a father
1913 //================================================================================
1915 _pyID _pyObject::FatherID(const _pyID & childID)
1917 int colPos = childID.SearchFromEnd(':');
1919 return childID.SubString( 1, colPos-1 );