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 "SMESH_Gen_i.hxx"
36 #include "utilities.h"
37 #include "SMESH_PythonDump.hxx"
38 #include "Resource_DataMapOfAsciiStringAsciiString.hxx"
40 IMPLEMENT_STANDARD_HANDLE (_pyObject ,Standard_Transient);
41 IMPLEMENT_STANDARD_HANDLE (_pyCommand ,Standard_Transient);
42 IMPLEMENT_STANDARD_HANDLE (_pyGen ,_pyObject);
43 IMPLEMENT_STANDARD_HANDLE (_pyMesh ,_pyObject);
44 IMPLEMENT_STANDARD_HANDLE (_pyMeshEditor ,_pyObject);
45 IMPLEMENT_STANDARD_HANDLE (_pyHypothesis ,_pyObject);
46 IMPLEMENT_STANDARD_HANDLE (_pyAlgorithm ,_pyHypothesis);
47 IMPLEMENT_STANDARD_HANDLE (_pyComplexParamHypo,_pyHypothesis);
48 IMPLEMENT_STANDARD_HANDLE (_pyNumberOfSegmentsHyp,_pyHypothesis);
50 IMPLEMENT_STANDARD_RTTIEXT(_pyObject ,Standard_Transient);
51 IMPLEMENT_STANDARD_RTTIEXT(_pyCommand ,Standard_Transient);
52 IMPLEMENT_STANDARD_RTTIEXT(_pyGen ,_pyObject);
53 IMPLEMENT_STANDARD_RTTIEXT(_pyMesh ,_pyObject);
54 IMPLEMENT_STANDARD_RTTIEXT(_pyMeshEditor ,_pyObject);
55 IMPLEMENT_STANDARD_RTTIEXT(_pyHypothesis ,_pyObject);
56 IMPLEMENT_STANDARD_RTTIEXT(_pyAlgorithm ,_pyHypothesis);
57 IMPLEMENT_STANDARD_RTTIEXT(_pyComplexParamHypo,_pyHypothesis);
58 IMPLEMENT_STANDARD_RTTIEXT(_pyNumberOfSegmentsHyp,_pyHypothesis);
59 IMPLEMENT_STANDARD_RTTIEXT(_pyLayerDistributionHypo,_pyHypothesis);
60 IMPLEMENT_STANDARD_RTTIEXT(_pySegmentLengthAroundVertexHyp,_pyHypothesis);
63 using SMESH::TPythonDump;
66 * \brief Container of commands into which the initial script is split.
67 * It also contains data coresponding to SMESH_Gen contents
69 static Handle(_pyGen) theGen;
71 static TCollection_AsciiString theEmptyString;
73 //#define DUMP_CONVERSION
75 #if !defined(_DEBUG_) && defined(DUMP_CONVERSION)
76 #undef DUMP_CONVERSION
81 //================================================================================
83 * \brief Set of TCollection_AsciiString initialized by C array of C strings
85 //================================================================================
87 struct TStringSet: public set<TCollection_AsciiString>
90 * \brief Filling. The last string must be ""
92 void Insert(const char* names[]) {
93 for ( int i = 0; names[i][0] ; ++i )
94 insert( (char*) names[i] );
97 * \brief Check if a string is in
99 bool Contains(const TCollection_AsciiString& name ) {
100 return find( name ) != end();
105 //================================================================================
107 * \brief Convert python script using commands of smesh.py
108 * \param theScript - Input script
109 * \retval TCollection_AsciiString - Convertion result
111 * Class SMESH_2smeshpy declared in SMESH_PythonDump.hxx
113 //================================================================================
115 TCollection_AsciiString
116 SMESH_2smeshpy::ConvertScript(const TCollection_AsciiString& theScript,
117 Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod)
119 theGen = new _pyGen( theEntry2AccessorMethod );
121 // split theScript into separate commands
122 int from = 1, end = theScript.Length(), to;
123 while ( from < end && ( to = theScript.Location( "\n", from, end )))
126 // cut out and store a command
127 theGen->AddCommand( theScript.SubString( from, to - 1 ));
132 #ifdef DUMP_CONVERSION
133 cout << endl << " ######## RESULT ######## " << endl<< endl;
135 // reorder commands after conversion
136 list< Handle(_pyCommand) >::iterator cmd;
139 orderChanges = false;
140 for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
141 if ( (*cmd)->SetDependentCmdsAfter() )
143 } while ( orderChanges );
145 // concat commands back into a script
146 TCollection_AsciiString aScript;
147 for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
149 #ifdef DUMP_CONVERSION
150 cout << "## COM " << (*cmd)->GetOrderNb() << ": "<< (*cmd)->GetString() << endl;
152 if ( !(*cmd)->IsEmpty() ) {
154 aScript += (*cmd)->GetString();
164 //================================================================================
166 * \brief _pyGen constructor
168 //================================================================================
170 _pyGen::_pyGen(Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod)
171 : _pyObject( new _pyCommand( TPythonDump::SMESHGenName(), 0 )),
172 myID2AccessorMethod( theEntry2AccessorMethod )
175 myHasPattern = false;
176 // make that GetID() to return TPythonDump::SMESHGenName()
177 GetCreationCmd()->GetString() += "=";
180 //================================================================================
182 * \brief name of SMESH_Gen in smesh.py
184 //================================================================================
186 const char* _pyGen::AccessorMethod() const
188 return SMESH_2smeshpy::GenName();
191 //================================================================================
193 * \brief Convert a command using a specific converter
194 * \param theCommand - the command to convert
196 //================================================================================
198 Handle(_pyCommand) _pyGen::AddCommand( const TCollection_AsciiString& theCommand)
200 // store theCommand in the sequence
201 myCommands.push_back( new _pyCommand( theCommand, ++myNbCommands ));
203 Handle(_pyCommand) aCommand = myCommands.back();
204 #ifdef DUMP_CONVERSION
205 cout << "## COM " << myNbCommands << ": "<< aCommand->GetString() << endl;
208 _pyID objID = aCommand->GetObject();
210 if ( objID.IsEmpty() )
214 if ( objID == this->GetID() ) {
215 this->Process( aCommand );
218 // SMESH_Mesh method?
219 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( objID );
220 if ( id_mesh != myMeshes.end() ) {
221 if ( aCommand->GetMethod() == "GetMeshEditor" ) { // MeshEditor creation
222 _pyID editorID = aCommand->GetResultValue();
223 Handle(_pyMeshEditor) editor = new _pyMeshEditor( aCommand );
224 myMeshEditors.insert( make_pair( editorID, editor ));
227 id_mesh->second->Process( aCommand );
230 // SMESH_MeshEditor method?
231 map< _pyID, Handle(_pyMeshEditor) >::iterator id_editor = myMeshEditors.find( objID );
232 if ( id_editor != myMeshEditors.end() ) {
233 id_editor->second->Process( aCommand );
236 // SMESH_Hypothesis method?
237 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
238 for ( ; hyp != myHypos.end(); ++hyp )
239 if ( !(*hyp)->IsAlgo() && objID == (*hyp)->GetID() ) {
240 (*hyp)->Process( aCommand );
244 // Add access to a wrapped mesh
245 AddMeshAccessorMethod( aCommand );
247 // Add access to a wrapped algorithm
248 AddAlgoAccessorMethod( aCommand ); // ??? what if algo won't be wrapped at all ???
250 // PAL12227. PythonDump was not updated at proper time; result is
251 // aCriteria.append(SMESH.Filter.Criterion(17,26,0,'L1',26,25,1e-07,SMESH.EDGE,-1))
252 // TypeError: __init__() takes exactly 11 arguments (10 given)
253 char wrongCommand[] = "SMESH.Filter.Criterion(";
254 if ( int beg = theCommand.Location( wrongCommand, 1, theCommand.Length() ))
256 _pyCommand tmpCmd( theCommand.SubString( beg, theCommand.Length() ), -1);
257 // there must be 10 arguments, 5-th arg ThresholdID is missing,
258 const int wrongNbArgs = 9, missingArg = 5;
259 if ( tmpCmd.GetNbArgs() == wrongNbArgs )
261 for ( int i = wrongNbArgs; i > missingArg; --i )
262 tmpCmd.SetArg( i + 1, tmpCmd.GetArg( i ));
263 tmpCmd.SetArg( missingArg, "''");
264 aCommand->GetString().Trunc( beg - 1 );
265 aCommand->GetString() += tmpCmd.GetString();
271 //================================================================================
273 * \brief Convert the command or remember it for later conversion
274 * \param theCommand - The python command calling a method of SMESH_Gen
276 //================================================================================
278 void _pyGen::Process( const Handle(_pyCommand)& theCommand )
280 // there are methods to convert:
281 // CreateMesh( shape )
282 // Concatenate( [mesh1, ...], ... )
283 // CreateHypothesis( theHypType, theLibName )
284 // Compute( mesh, geom )
287 if ( theCommand->GetMethod() == "CreateMesh" ||
288 theCommand->GetMethod() == "CreateEmptyMesh" )
290 Handle(_pyMesh) mesh = new _pyMesh( theCommand );
291 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
295 // CreateHypothesis()
296 if ( theCommand->GetMethod() == "CreateHypothesis" )
298 myHypos.push_back( _pyHypothesis::NewHypothesis( theCommand ));
302 // smeshgen.Compute( mesh, geom ) --> mesh.Compute()
303 if ( theCommand->GetMethod() == "Compute" )
305 const _pyID& meshID = theCommand->GetArg( 1 );
306 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( meshID );
307 if ( id_mesh != myMeshes.end() ) {
308 theCommand->SetObject( meshID );
309 theCommand->RemoveArgs();
310 id_mesh->second->Flush();
315 // leave only one smeshgen.GetPattern() in the script
316 if ( theCommand->GetMethod() == "GetPattern" ) {
317 if ( myHasPattern ) {
324 // Concatenate( [mesh1, ...], ... )
325 if ( theCommand->GetMethod() == "Concatenate" )
327 AddMeshAccessorMethod( theCommand );
330 // Replace name of SMESH_Gen
332 // names of SMESH_Gen methods fully equal to methods defined in smesh.py
333 static TStringSet smeshpyMethods;
334 if ( smeshpyMethods.empty() ) {
335 const char * names[] =
336 { "SetEmbeddedMode","IsEmbeddedMode","SetCurrentStudy","GetCurrentStudy",
337 "GetPattern","GetSubShapesId",
338 "" }; // <- mark of array end
339 smeshpyMethods.Insert( names );
341 if ( smeshpyMethods.Contains( theCommand->GetMethod() ))
342 // smeshgen.Method() --> smesh.Method()
343 theCommand->SetObject( SMESH_2smeshpy::SmeshpyName() );
345 // smeshgen.Method() --> smesh.smesh.Method()
346 theCommand->SetObject( SMESH_2smeshpy::GenName() );
349 //================================================================================
351 * \brief Convert the remembered commands
353 //================================================================================
357 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.begin();
358 for ( ; id_mesh != myMeshes.end(); ++id_mesh )
359 if ( ! id_mesh->second.IsNull() )
360 id_mesh->second->Flush();
362 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
363 for ( ; hyp != myHypos.end(); ++hyp )
364 if ( !hyp->IsNull() ) {
366 // smeshgen.CreateHypothesis() --> smesh.smesh.CreateHypothesis()
367 if ( !(*hyp)->IsWrapped() )
368 (*hyp)->GetCreationCmd()->SetObject( SMESH_2smeshpy::GenName() );
372 //================================================================================
374 * \brief Add access method to mesh that is an object or an argument
375 * \param theCmd - command to add access method
376 * \retval bool - true if added
378 //================================================================================
380 bool _pyGen::AddMeshAccessorMethod( Handle(_pyCommand) theCmd ) const
383 map< _pyID, Handle(_pyMesh) >::const_iterator id_mesh = myMeshes.begin();
384 for ( ; id_mesh != myMeshes.end(); ++id_mesh ) {
385 if ( theCmd->AddAccessorMethod( id_mesh->first, id_mesh->second->AccessorMethod() ))
391 //================================================================================
393 * \brief Add access method to algo that is an object or an argument
394 * \param theCmd - command to add access method
395 * \retval bool - true if added
397 //================================================================================
399 bool _pyGen::AddAlgoAccessorMethod( Handle(_pyCommand) theCmd ) const
402 list< Handle(_pyHypothesis) >::const_iterator hyp = myHypos.begin();
403 for ( ; hyp != myHypos.end(); ++hyp ) {
404 if ( (*hyp)->IsAlgo() && /*(*hyp)->IsWrapped() &&*/
405 theCmd->AddAccessorMethod( (*hyp)->GetID(), (*hyp)->AccessorMethod() ))
411 //================================================================================
413 * \brief Find hypothesis by ID (entry)
414 * \param theHypID - The hypothesis ID
415 * \retval Handle(_pyHypothesis) - The found hypothesis
417 //================================================================================
419 Handle(_pyHypothesis) _pyGen::FindHyp( const _pyID& theHypID )
421 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
422 for ( ; hyp != myHypos.end(); ++hyp )
423 if ( !hyp->IsNull() && theHypID == (*hyp)->GetID() )
425 return Handle(_pyHypothesis)();
428 //================================================================================
430 * \brief Find algorithm the created algorithm
431 * \param theGeom - The shape ID the algorithm was created on
432 * \param theMesh - The mesh ID that created the algorithm
433 * \param dim - The algo dimension
434 * \retval Handle(_pyHypothesis) - The found algo
436 //================================================================================
438 Handle(_pyHypothesis) _pyGen::FindAlgo( const _pyID& theGeom, const _pyID& theMesh,
439 const Handle(_pyHypothesis)& theHypothesis )
441 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
442 for ( ; hyp != myHypos.end(); ++hyp )
443 if ( !hyp->IsNull() &&
445 theHypothesis->CanBeCreatedBy( (*hyp)->GetAlgoType() ) &&
446 (*hyp)->GetGeom() == theGeom &&
447 (*hyp)->GetMesh() == theMesh )
452 //================================================================================
454 * \brief Change order of commands in the script
455 * \param theCmd1 - One command
456 * \param theCmd2 - Another command
458 //================================================================================
460 void _pyGen::ExchangeCommands( Handle(_pyCommand) theCmd1, Handle(_pyCommand) theCmd2 )
462 list< Handle(_pyCommand) >::iterator pos1, pos2;
463 pos1 = find( myCommands.begin(), myCommands.end(), theCmd1 );
464 pos2 = find( myCommands.begin(), myCommands.end(), theCmd2 );
465 myCommands.insert( pos1, theCmd2 );
466 myCommands.insert( pos2, theCmd1 );
467 myCommands.erase( pos1 );
468 myCommands.erase( pos2 );
470 int nb1 = theCmd1->GetOrderNb();
471 theCmd1->SetOrderNb( theCmd2->GetOrderNb() );
472 theCmd2->SetOrderNb( nb1 );
473 // cout << "BECOME " << theCmd1->GetOrderNb() << "\t" << theCmd1->GetString() << endl
474 // << "BECOME " << theCmd2->GetOrderNb() << "\t" << theCmd2->GetString() << endl << endl;
477 //================================================================================
479 * \brief Set one command after the other
480 * \param theCmd - Command to move
481 * \param theAfterCmd - Command ater which to insert the first one
483 //================================================================================
485 void _pyGen::SetCommandAfter( Handle(_pyCommand) theCmd, Handle(_pyCommand) theAfterCmd )
488 //cout << "SET\t" << theAfterCmd->GetString() << endl << "BEFORE\t" << theCmd->GetString() << endl<<endl;
490 list< Handle(_pyCommand) >::iterator pos;
491 pos = find( myCommands.begin(), myCommands.end(), theCmd );
492 myCommands.erase( pos );
493 pos = find( myCommands.begin(), myCommands.end(), theAfterCmd );
494 myCommands.insert( ++pos, theCmd );
497 for ( pos = myCommands.begin(); pos != myCommands.end(); ++pos)
498 (*pos)->SetOrderNb( i++ );
501 //================================================================================
503 * \brief Set method to access to object wrapped with python class
504 * \param theID - The wrapped object entry
505 * \param theMethod - The accessor method
507 //================================================================================
509 void _pyGen::SetAccessorMethod(const _pyID& theID, const char* theMethod )
511 myID2AccessorMethod.Bind( theID, (char*) theMethod );
514 //================================================================================
516 * \brief Find out type of geom group
517 * \param grpID - The geom group entry
518 * \retval int - The type
520 //================================================================================
522 static bool sameGroupType( const _pyID& grpID,
523 const TCollection_AsciiString& theType)
525 // define group type as smesh.Mesh.Group() does
527 SALOMEDS::Study_var study = SMESH_Gen_i::GetSMESHGen()->GetCurrentStudy();
528 SALOMEDS::SObject_var aSObj = study->FindObjectID( grpID.ToCString() );
529 if ( !aSObj->_is_nil() ) {
530 GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow( aSObj->GetObject() );
531 if ( !aGeomObj->_is_nil() ) {
532 switch ( aGeomObj->GetShapeType() ) {
533 case GEOM::VERTEX: type = SMESH::NODE; break;
534 case GEOM::EDGE: type = SMESH::EDGE; break;
535 case GEOM::FACE: type = SMESH::FACE; break;
537 case GEOM::SHELL: type = SMESH::VOLUME; break;
538 case GEOM::COMPOUND: {
539 GEOM::GEOM_Gen_ptr aGeomGen = SMESH_Gen_i::GetSMESHGen()->GetGeomEngine();
540 if ( !aGeomGen->_is_nil() ) {
541 GEOM::GEOM_IGroupOperations_var aGrpOp =
542 aGeomGen->GetIGroupOperations( study->StudyId() );
543 if ( !aGrpOp->_is_nil() ) {
544 switch ( aGrpOp->GetType( aGeomObj )) {
545 case TopAbs_VERTEX: type = SMESH::NODE; break;
546 case TopAbs_EDGE: type = SMESH::EDGE; break;
547 case TopAbs_FACE: type = SMESH::FACE; break;
548 case TopAbs_SOLID: type = SMESH::VOLUME; break;
559 MESSAGE("Type of the group " << grpID << " not found");
562 if ( theType.IsIntegerValue() )
563 return type == theType.IntegerValue();
566 case SMESH::NODE: return theType.Location( "NODE", 1, theType.Length() );
567 case SMESH::EDGE: return theType.Location( "EDGE", 1, theType.Length() );
568 case SMESH::FACE: return theType.Location( "FACE", 1, theType.Length() );
569 case SMESH::VOLUME: return theType.Location( "VOLUME", 1, theType.Length() );
575 //================================================================================
578 * \param theCreationCmd -
580 //================================================================================
582 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd):
583 _pyObject(theCreationCmd), myHasEditor(false)
585 // convert my creation command
586 Handle(_pyCommand) creationCmd = GetCreationCmd();
587 creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
588 creationCmd->SetMethod( "Mesh" );
590 theGen->SetAccessorMethod( GetID(), "GetMesh()" );
593 //================================================================================
595 * \brief Convert a IDL API command of SMESH::Mesh to a method call of python Mesh
596 * \param theCommand - Engine method called for this mesh
598 //================================================================================
600 void _pyMesh::Process( const Handle(_pyCommand)& theCommand )
602 // some methods of SMESH_Mesh interface needs special conversion
603 // to methods of Mesh python class
605 // 1. GetSubMesh(geom, name) + AddHypothesis(geom, algo)
606 // --> in Mesh_Algorithm.Create(mesh, geom, hypo, so)
607 // 2. AddHypothesis(geom, hyp)
608 // --> in Mesh_Algorithm.Hypothesis(hyp, args, so)
609 // 3. CreateGroupFromGEOM(type, name, grp)
610 // --> in Mesh.Group(grp, name="")
611 // 4. ExportToMED(f, auto_groups, version)
612 // --> in Mesh.ExportMED( f, auto_groups, version )
615 const TCollection_AsciiString method = theCommand->GetMethod();
616 // ----------------------------------------------------------------------
617 if ( method == "GetSubMesh" ) {
618 mySubmeshes.push_back( theCommand );
620 // ----------------------------------------------------------------------
621 else if ( method == "AddHypothesis" ) { // mesh.AddHypothesis(geom, HYPO )
622 myAddHypCmds.push_back( theCommand );
624 const _pyID& hypID = theCommand->GetArg( 2 );
625 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
626 if ( !hyp.IsNull() ) {
627 myHypos.push_back( hyp );
628 if ( hyp->GetMesh().IsEmpty() )
629 hyp->SetMesh( this->GetID() );
632 // ----------------------------------------------------------------------
633 else if ( method == "CreateGroupFromGEOM" ) {// (type, name, grp)
634 _pyID grp = theCommand->GetArg( 3 );
635 if ( sameGroupType( grp, theCommand->GetArg( 1 )) ) { // --> Group(grp)
636 theCommand->SetMethod( "Group" );
637 theCommand->RemoveArgs();
638 theCommand->SetArg( 1, grp );
641 AddMeshAccess( theCommand );
644 // ----------------------------------------------------------------------
645 else if ( method == "ExportToMED" ) { // ExportToMED() --> ExportMED()
646 theCommand->SetMethod( "ExportMED" );
648 // ----------------------------------------------------------------------
649 else if ( method == "CreateGroup" ) { // CreateGroup() --> CreateEmptyGroup()
650 theCommand->SetMethod( "CreateEmptyGroup" );
652 // ----------------------------------------------------------------------
653 else if ( method == "RemoveHypothesis" ) // (geom, hyp)
655 _pyID hypID = theCommand->GetArg( 2 );
657 // check if this mesh still has corresponding addition command
658 bool hasAddCmd = false;
659 list< Handle(_pyCommand) >::iterator cmd = myAddHypCmds.begin();
660 while ( cmd != myAddHypCmds.end() )
662 // AddHypothesis(geom, hyp)
663 if ( hypID == (*cmd)->GetArg( 2 )) { // erase both (add and remove) commands
666 cmd = myAddHypCmds.erase( cmd );
673 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
674 if ( ! hasAddCmd ) { // hypo addition already wrapped
675 // RemoveHypothesis(geom, hyp) --> RemoveHypothesis( hyp, geom=0 )
676 _pyID geom = theCommand->GetArg( 1 );
677 theCommand->RemoveArgs();
678 theCommand->SetArg( 1, hypID );
679 if ( geom != GetGeom() )
680 theCommand->SetArg( 2, geom );
682 // remove hyp from myHypos
683 myHypos.remove( hyp );
685 // add accessor method if necessary
688 if ( NeedMeshAccess( theCommand ))
689 // apply theCommand to the mesh wrapped by smeshpy mesh
690 AddMeshAccess( theCommand );
694 //================================================================================
696 * \brief Return True if addition of accesor method is needed
698 //================================================================================
700 bool _pyMesh::NeedMeshAccess( const Handle(_pyCommand)& theCommand )
702 // names of SMESH_Mesh methods fully equal to methods of class Mesh, so
703 // no conversion is needed for them at all:
704 static TStringSet sameMethods;
705 if ( sameMethods.empty() ) {
706 const char * names[] =
707 { "ExportDAT","ExportUNV","ExportSTL", "RemoveGroup","RemoveGroupWithContents",
708 "GetGroups","UnionGroups","IntersectGroups","CutGroups","GetLog","GetId","ClearLog",
709 "GetStudyId","HasDuplicatedGroupNamesMED","GetMEDMesh","NbNodes","NbElements",
710 "NbEdges","NbEdgesOfOrder","NbFaces","NbFacesOfOrder","NbTriangles",
711 "NbTrianglesOfOrder","NbQuadrangles","NbQuadranglesOfOrder","NbPolygons","NbVolumes",
712 "NbVolumesOfOrder","NbTetras","NbTetrasOfOrder","NbHexas","NbHexasOfOrder",
713 "NbPyramids","NbPyramidsOfOrder","NbPrisms","NbPrismsOfOrder","NbPolyhedrons",
714 "NbSubMesh","GetElementsId","GetElementsByType","GetNodesId","GetElementType",
715 "GetSubMeshElementsId","GetSubMeshNodesId","GetSubMeshElementType","Dump","GetNodeXYZ",
716 "GetNodeInverseElements","GetShapeID","GetShapeIDForElem","GetElemNbNodes",
717 "GetElemNode","IsMediumNode","IsMediumNodeOfAnyElem","ElemNbEdges","ElemNbFaces",
718 "IsPoly","IsQuadratic","BaryCenter","GetHypothesisList",
719 "" }; // <- mark of end
720 sameMethods.Insert( names );
723 return !sameMethods.Contains( theCommand->GetMethod() );
726 //================================================================================
728 * \brief Convert creation and addition of all algos and hypos
730 //================================================================================
732 void _pyMesh::Flush()
734 list < Handle(_pyCommand) >::iterator cmd, cmd2;
736 // try to convert algo addition like this:
737 // mesh.AddHypothesis(geom, ALGO ) --> ALGO = mesh.Algo()
738 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
740 Handle(_pyCommand) addCmd = *cmd;
741 _pyID algoID = addCmd->GetArg( 2 );
742 Handle(_pyHypothesis) algo = theGen->FindHyp( algoID );
743 if ( algo.IsNull() || !algo->IsAlgo() )
746 _pyID geom = addCmd->GetArg( 1 );
747 bool isLocalAlgo = ( geom != GetGeom() );
748 if ( algo->Addition2Creation( addCmd, this->GetID() )) // OK
750 // wrapped algo is created atfer mesh creation
751 GetCreationCmd()->AddDependantCmd( addCmd );
754 // mesh.AddHypothesis(geom, ALGO ) --> mesh.AlgoMethod(geom)
755 addCmd->SetArg( addCmd->GetNbArgs() + 1,
756 TCollection_AsciiString( "geom=" ) + geom );
757 // sm = mesh.GetSubMesh(geom, name) --> sm = ALGO.GetSubMesh()
758 for ( cmd2 = mySubmeshes.begin(); cmd2 != mySubmeshes.end(); ++cmd2 ) {
759 Handle(_pyCommand) subCmd = *cmd2;
760 if ( geom == subCmd->GetArg( 1 )) {
761 subCmd->SetObject( algo->GetID() );
762 subCmd->RemoveArgs();
763 addCmd->AddDependantCmd( subCmd );
768 else // KO - ALGO was already created
770 // mesh.AddHypothesis(geom, ALGO) --> mesh.AddHypothesis(ALGO, geom=0)
771 addCmd->RemoveArgs();
772 addCmd->SetArg( 1, algoID );
774 addCmd->SetArg( 2, geom );
778 // try to convert hypo addition like this:
779 // mesh.AddHypothesis(geom, HYPO ) --> HYPO = algo.Hypo()
780 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
782 Handle(_pyCommand) addCmd = *cmd;
783 _pyID hypID = addCmd->GetArg( 2 );
784 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
785 if ( hyp.IsNull() || hyp->IsAlgo() )
787 bool converted = hyp->Addition2Creation( addCmd, this->GetID() );
789 // mesh.AddHypothesis(geom, HYP) --> mesh.AddHypothesis(HYP, geom=0)
790 _pyID geom = addCmd->GetArg( 1 );
791 addCmd->RemoveArgs();
792 addCmd->SetArg( 1, hypID );
793 if ( geom != GetGeom() )
794 addCmd->SetArg( 2, geom );
798 // sm = mesh.GetSubMesh(geom, name) --> sm = mesh.GetMesh().GetSubMesh(geom, name)
799 // for ( cmd = mySubmeshes.begin(); cmd != mySubmeshes.end(); ++cmd ) {
800 // Handle(_pyCommand) subCmd = *cmd;
801 // if ( subCmd->GetNbArgs() > 0 )
802 // AddMeshAccess( subCmd );
804 myAddHypCmds.clear();
808 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
809 for ( ; hyp != myHypos.end(); ++hyp )
813 //================================================================================
815 * \brief MeshEditor convert its commands to ones of mesh
817 //================================================================================
819 _pyMeshEditor::_pyMeshEditor(const Handle(_pyCommand)& theCreationCmd):
820 _pyObject( theCreationCmd )
822 myMesh = theCreationCmd->GetObject();
823 myCreationCmdStr = theCreationCmd->GetString();
824 theCreationCmd->Clear();
827 //================================================================================
829 * \brief convert its commands to ones of mesh
831 //================================================================================
833 void _pyMeshEditor::Process( const Handle(_pyCommand)& theCommand)
835 // names of SMESH_MeshEditor methods fully equal to methods of class Mesh, so
836 // commands calling this methods are converted to calls of methods of Mesh
837 static TStringSet sameMethods;
838 if ( sameMethods.empty() ) {
839 const char * names[] = {
840 "RemoveElements","RemoveNodes","AddNode","AddEdge","AddFace","AddPolygonalFace",
841 "AddVolume","AddPolyhedralVolume","AddPolyhedralVolumeByFaces","MoveNode",
842 "InverseDiag","DeleteDiag","Reorient","ReorientObject","SplitQuad","SplitQuadObject",
843 "BestSplit","Smooth","SmoothObject","SmoothParametric","SmoothParametricObject",
844 "ConvertToQuadratic","ConvertFromQuadratic","RenumberNodes","RenumberElements",
845 "RotationSweep","RotationSweepObject","ExtrusionSweep","AdvancedExtrusion",
846 "ExtrusionSweepObject","ExtrusionSweepObject1D","ExtrusionSweepObject2D","Mirror",
847 "MirrorObject","Translate","TranslateObject","Rotate","RotateObject",
848 "FindCoincidentNodes","FindCoincidentNodesOnPart","MergeNodes","FindEqualElements",
849 "MergeElements","MergeEqualElements","SewFreeBorders","SewConformFreeBorders",
850 "SewBorderToSide","SewSideElements","ChangeElemNodes","GetLastCreatedNodes",
851 "GetLastCreatedElems",
852 "" }; // <- mark of end
853 sameMethods.Insert( names );
856 if ( sameMethods.Contains( theCommand->GetMethod() )) {
857 theCommand->SetObject( myMesh );
860 // editor creation command is needed only if any editor function is called
861 if ( !myCreationCmdStr.IsEmpty() ) {
862 GetCreationCmd()->GetString() = myCreationCmdStr;
863 myCreationCmdStr.Clear();
868 //================================================================================
870 * \brief _pyHypothesis constructor
871 * \param theCreationCmd -
873 //================================================================================
875 _pyHypothesis::_pyHypothesis(const Handle(_pyCommand)& theCreationCmd):
876 _pyObject( theCreationCmd )
878 myIsAlgo = myIsWrapped = /*myIsConverted = myIsLocal = myDim = */false;
881 //================================================================================
883 * \brief Creates algorithm or hypothesis
884 * \param theCreationCmd - The engine command creating a hypothesis
885 * \retval Handle(_pyHypothesis) - Result _pyHypothesis
887 //================================================================================
889 Handle(_pyHypothesis) _pyHypothesis::NewHypothesis( const Handle(_pyCommand)& theCreationCmd)
891 // theCreationCmd: CreateHypothesis( "theHypType", "theLibName" )
892 ASSERT (( theCreationCmd->GetMethod() == "CreateHypothesis"));
894 Handle(_pyHypothesis) hyp, algo;
897 const TCollection_AsciiString & hypTypeQuoted = theCreationCmd->GetArg( 1 );
898 if ( hypTypeQuoted.IsEmpty() )
901 TCollection_AsciiString hypType =
902 hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
904 algo = new _pyAlgorithm( theCreationCmd );
905 hyp = new _pyHypothesis( theCreationCmd );
907 // 1D Regular_1D ----------
908 if ( hypType == "Regular_1D" ) {
909 // set mesh's method creating algo,
910 // i.e. convertion result will be "regular1d = Mesh.Segment()",
911 // and set hypType by which algo creating a hypothesis is searched for
912 algo->SetConvMethodAndType("Segment", hypType.ToCString());
914 else if ( hypType == "CompositeSegment_1D" ) {
915 algo->SetConvMethodAndType("Segment", "Regular_1D");
916 algo->myArgs.Append( "algo=smesh.COMPOSITE");
918 else if ( hypType == "LocalLength" ) {
919 // set algo's method creating hyp, and algo type
920 hyp->SetConvMethodAndType( "LocalLength", "Regular_1D");
921 // set method whose 1 arg will become the 1-st arg of hyp creation command
922 // i.e. convertion result will be "locallength = regular1d.LocalLength(<arg of SetLength()>)"
923 hyp->AddArgMethod( "SetLength" );
925 else if ( hypType == "NumberOfSegments" ) {
926 hyp = new _pyNumberOfSegmentsHyp( theCreationCmd );
927 hyp->SetConvMethodAndType( "NumberOfSegments", "Regular_1D");
928 // arg of SetNumberOfSegments() will become the 1-st arg of hyp creation command
929 hyp->AddArgMethod( "SetNumberOfSegments" );
930 // arg of SetScaleFactor() will become the 2-nd arg of hyp creation command
931 hyp->AddArgMethod( "SetScaleFactor" );
933 else if ( hypType == "Arithmetic1D" ) {
934 hyp = new _pyComplexParamHypo( theCreationCmd );
935 hyp->SetConvMethodAndType( "Arithmetic1D", "Regular_1D");
937 else if ( hypType == "StartEndLength" ) {
938 hyp = new _pyComplexParamHypo( theCreationCmd );
939 hyp->SetConvMethodAndType( "StartEndLength", "Regular_1D");
941 else if ( hypType == "Deflection1D" ) {
942 hyp->SetConvMethodAndType( "Deflection1D", "Regular_1D");
943 hyp->AddArgMethod( "SetDeflection" );
945 else if ( hypType == "Propagation" ) {
946 hyp->SetConvMethodAndType( "Propagation", "Regular_1D");
948 else if ( hypType == "QuadraticMesh" ) {
949 hyp->SetConvMethodAndType( "QuadraticMesh", "Regular_1D");
951 else if ( hypType == "AutomaticLength" ) {
952 hyp->SetConvMethodAndType( "AutomaticLength", "Regular_1D");
953 hyp->AddArgMethod( "SetFineness");
955 else if ( hypType == "SegmentLengthAroundVertex" ) {
956 hyp = new _pySegmentLengthAroundVertexHyp( theCreationCmd );
957 hyp->SetConvMethodAndType( "LengthNearVertex", "Regular_1D" );
958 hyp->AddArgMethod( "SetLength" );
960 // 1D Python_1D ----------
961 else if ( hypType == "Python_1D" ) {
962 algo->SetConvMethodAndType( "Segment", hypType.ToCString());
963 algo->myArgs.Append( "algo=smesh.PYTHON");
965 else if ( hypType == "PythonSplit1D" ) {
966 hyp->SetConvMethodAndType( "PythonSplit1D", "Python_1D");
967 hyp->AddArgMethod( "SetNumberOfSegments");
968 hyp->AddArgMethod( "SetPythonLog10RatioFunction");
970 // MEFISTO_2D ----------
971 else if ( hypType == "MEFISTO_2D" ) { // MEFISTO_2D
972 algo->SetConvMethodAndType( "Triangle", hypType.ToCString());
974 else if ( hypType == "MaxElementArea" ) {
975 hyp->SetConvMethodAndType( "MaxElementArea", "MEFISTO_2D");
976 hyp->SetConvMethodAndType( "MaxElementArea", "NETGEN_2D_ONLY");
977 hyp->AddArgMethod( "SetMaxElementArea");
979 else if ( hypType == "LengthFromEdges" ) {
980 hyp->SetConvMethodAndType( "LengthFromEdges", "MEFISTO_2D");
981 hyp->SetConvMethodAndType( "LengthFromEdges", "NETGEN_2D_ONLY");
983 // Quadrangle_2D ----------
984 else if ( hypType == "Quadrangle_2D" ) {
985 algo->SetConvMethodAndType( "Quadrangle" , hypType.ToCString());
987 else if ( hypType == "QuadranglePreference" ) {
988 hyp->SetConvMethodAndType( "QuadranglePreference", "Quadrangle_2D");
989 hyp->SetConvMethodAndType( "QuadranglePreference", "NETGEN_2D_ONLY");
992 // else if ( hypType == "NETGEN_2D") { // 1D-2D
993 // algo->SetConvMethodAndType( "Triangle" , hypType.ToCString());
994 // algo->myArgs.Append( "algo=smesh.NETGEN" );
996 else if ( hypType == "NETGEN_2D_ONLY") { // 2D
997 algo->SetConvMethodAndType( "Triangle" , hypType.ToCString());
998 algo->myArgs.Append( "algo=smesh.NETGEN_2D" );
1000 else if ( hypType == "NETGEN_3D") { // 3D
1001 algo->SetConvMethodAndType( "Tetrahedron" , hypType.ToCString());
1002 algo->myArgs.Append( "algo=smesh.NETGEN" );
1004 else if ( hypType == "MaxElementVolume") {
1005 hyp->SetConvMethodAndType( "MaxElementVolume", "NETGEN_3D");
1006 hyp->AddArgMethod( "SetMaxElementVolume" );
1008 // GHS3D_3D ----------
1009 else if ( hypType == "GHS3D_3D" ) {
1010 algo->SetConvMethodAndType( "Tetrahedron", hypType.ToCString());
1011 algo->myArgs.Append( "algo=smesh.GHS3D" );
1013 // Hexa_3D ---------
1014 else if ( hypType == "Hexa_3D" ) {
1015 algo->SetConvMethodAndType( "Hexahedron", hypType.ToCString());
1017 // Repetitive Projection_1D ---------
1018 else if ( hypType == "Projection_1D" ) {
1019 algo->SetConvMethodAndType( "Projection1D", hypType.ToCString());
1021 else if ( hypType == "ProjectionSource1D" ) {
1022 hyp->SetConvMethodAndType( "SourceEdge", "Projection_1D");
1023 hyp->AddArgMethod( "SetSourceEdge");
1024 hyp->AddArgMethod( "SetSourceMesh");
1025 // 2 args of SetVertexAssociation() will become the 3-th and 4-th args of hyp creation command
1026 hyp->AddArgMethod( "SetVertexAssociation", 2 );
1028 // Projection_2D ---------
1029 else if ( hypType == "Projection_2D" ) {
1030 algo->SetConvMethodAndType( "Projection2D", hypType.ToCString());
1032 else if ( hypType == "ProjectionSource2D" ) {
1033 hyp->SetConvMethodAndType( "SourceFace", "Projection_2D");
1034 hyp->AddArgMethod( "SetSourceFace");
1035 hyp->AddArgMethod( "SetSourceMesh");
1036 hyp->AddArgMethod( "SetVertexAssociation", 4 );
1038 // Projection_3D ---------
1039 else if ( hypType == "Projection_3D" ) {
1040 algo->SetConvMethodAndType( "Projection3D", hypType.ToCString());
1042 else if ( hypType == "ProjectionSource3D" ) {
1043 hyp->SetConvMethodAndType( "SourceShape3D", "Projection_3D");
1044 hyp->AddArgMethod( "SetSource3DShape");
1045 hyp->AddArgMethod( "SetSourceMesh");
1046 hyp->AddArgMethod( "SetVertexAssociation", 4 );
1048 // Prism_3D ---------
1049 else if ( hypType == "Prism_3D" ) {
1050 algo->SetConvMethodAndType( "Prism", hypType.ToCString());
1052 // RadialPrism_3D ---------
1053 else if ( hypType == "RadialPrism_3D" ) {
1054 algo->SetConvMethodAndType( "Prism", hypType.ToCString());
1056 else if ( hypType == "NumberOfLayers" ) {
1057 hyp->SetConvMethodAndType( "NumberOfLayers", "RadialPrism_3D");
1058 hyp->AddArgMethod( "SetNumberOfLayers" );
1060 else if ( hypType == "LayerDistribution" ) {
1061 hyp = new _pyLayerDistributionHypo( theCreationCmd );
1062 hyp->SetConvMethodAndType( "LayerDistribution", "RadialPrism_3D");
1065 if ( algo->IsValid() ) {
1071 //================================================================================
1073 * \brief Convert the command adding a hypothesis to mesh into a smesh command
1074 * \param theCmd - The command like mesh.AddHypothesis( geom, hypo )
1075 * \param theAlgo - The algo that can create this hypo
1076 * \retval bool - false if the command cant be converted
1078 //================================================================================
1080 bool _pyHypothesis::Addition2Creation( const Handle(_pyCommand)& theCmd,
1081 const _pyID& theMesh)
1083 ASSERT(( theCmd->GetMethod() == "AddHypothesis" ));
1085 if ( !IsWrappable( theMesh ))
1088 myGeom = theCmd->GetArg( 1 );
1090 Handle(_pyHypothesis) algo;
1092 // find algo created on myGeom in theMesh
1093 algo = theGen->FindAlgo( myGeom, theMesh, this );
1094 if ( algo.IsNull() )
1096 algo->GetCreationCmd()->AddDependantCmd( theCmd );
1100 // mesh.AddHypothesis(geom,hyp) --> hyp = <theMesh or algo>.myCreationMethod(args)
1101 theCmd->SetResultValue( GetID() );
1102 theCmd->SetObject( IsAlgo() ? theMesh : algo->GetID());
1103 theCmd->SetMethod( IsAlgo() ? GetAlgoCreationMethod() : GetCreationMethod( algo->GetAlgoType() ));
1105 theCmd->RemoveArgs();
1106 for ( int i = 1; i <= myArgs.Length(); ++i ) {
1107 if ( !myArgs( i ).IsEmpty() )
1108 theCmd->SetArg( i, myArgs( i ));
1110 theCmd->SetArg( i, "[]");
1112 // set a new creation command
1113 GetCreationCmd()->Clear();
1114 SetCreationCmd( theCmd );
1116 // clear commands setting arg values
1117 list < Handle(_pyCommand) >::iterator argCmd = myArgCommands.begin();
1118 for ( ; argCmd != myArgCommands.end(); ++argCmd )
1121 // set unknown arg commands after hypo creation
1122 Handle(_pyCommand) afterCmd = myIsWrapped ? theCmd : GetCreationCmd();
1123 list<Handle(_pyCommand)>::iterator cmd = myUnknownCommands.begin();
1124 for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
1125 afterCmd->AddDependantCmd( *cmd );
1131 //================================================================================
1133 * \brief Remember hypothesis parameter values
1134 * \param theCommand - The called hypothesis method
1136 //================================================================================
1138 void _pyHypothesis::Process( const Handle(_pyCommand)& theCommand)
1140 ASSERT( !myIsAlgo );
1143 for ( int i = 1; i <= myArgMethods.Length(); ++i ) {
1144 if ( myArgMethods( i ) == theCommand->GetMethod() ) {
1145 while ( myArgs.Length() < nbArgs + myNbArgsByMethod( i ))
1146 myArgs.Append( "[]" );
1147 for ( int iArg = 1; iArg <= myNbArgsByMethod( i ); ++iArg )
1148 myArgs( nbArgs + iArg ) = theCommand->GetArg( iArg ); // arg value
1149 myArgCommands.push_back( theCommand );
1152 nbArgs += myNbArgsByMethod( i );
1154 myUnknownCommands.push_back( theCommand );
1157 //================================================================================
1159 * \brief Finish conversion
1161 //================================================================================
1163 void _pyHypothesis::Flush()
1165 if ( IsWrapped() ) {
1168 list < Handle(_pyCommand) >::iterator cmd = myArgCommands.begin();
1169 for ( ; cmd != myArgCommands.end(); ++cmd ) {
1170 // Add access to a wrapped mesh
1171 theGen->AddMeshAccessorMethod( *cmd );
1172 // Add access to a wrapped algorithm
1173 theGen->AddAlgoAccessorMethod( *cmd );
1175 cmd = myUnknownCommands.begin();
1176 for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
1177 // Add access to a wrapped mesh
1178 theGen->AddMeshAccessorMethod( *cmd );
1179 // Add access to a wrapped algorithm
1180 theGen->AddAlgoAccessorMethod( *cmd );
1183 // forget previous hypothesis modifications
1184 myArgCommands.clear();
1185 myUnknownCommands.clear();
1188 //================================================================================
1190 * \brief clear creation, arg and unkown commands
1192 //================================================================================
1194 void _pyHypothesis::ClearAllCommands()
1196 GetCreationCmd()->Clear();
1197 list<Handle(_pyCommand)>::iterator cmd = myArgCommands.begin();
1198 for ( ; cmd != myArgCommands.end(); ++cmd )
1200 cmd = myUnknownCommands.begin();
1201 for ( ; cmd != myUnknownCommands.end(); ++cmd )
1205 //================================================================================
1207 * \brief Remember hypothesis parameter values
1208 * \param theCommand - The called hypothesis method
1210 //================================================================================
1212 void _pyComplexParamHypo::Process( const Handle(_pyCommand)& theCommand)
1214 // ex: hyp.SetLength(start, 1)
1215 // hyp.SetLength(end, 0)
1216 ASSERT(( theCommand->GetMethod() == "SetLength" ));
1217 ASSERT(( theCommand->GetArg( 2 ).IsIntegerValue() ));
1218 int i = 2 - theCommand->GetArg( 2 ).IntegerValue();
1219 while ( myArgs.Length() < i )
1220 myArgs.Append( "[]" );
1221 myArgs( i ) = theCommand->GetArg( 1 ); // arg value
1222 myArgCommands.push_back( theCommand );
1225 //================================================================================
1227 * \brief Convert methods of 1D hypotheses to my own methods
1228 * \param theCommand - The called hypothesis method
1230 //================================================================================
1232 void _pyLayerDistributionHypo::Process( const Handle(_pyCommand)& theCommand)
1234 if ( theCommand->GetMethod() != "SetLayerDistribution" )
1237 _pyID newName; // name for 1D hyp = "HypType" + "_Distribution"
1239 const _pyID& hyp1dID = theCommand->GetArg( 1 );
1240 Handle(_pyHypothesis) hyp1d = theGen->FindHyp( hyp1dID );
1241 if ( hyp1d.IsNull() ) // apparently hypId changed at study restoration
1243 else if ( !my1dHyp.IsNull() && hyp1dID != my1dHyp->GetID() ) {
1244 // 1D hypo is already set, so distribution changes and the old
1245 // 1D hypo is thrown away
1246 my1dHyp->ClearAllCommands();
1249 if ( my1dHyp.IsNull() )
1250 return; // something wrong :(
1252 // make a new name for 1D hyp = "HypType" + "_Distribution"
1253 if ( my1dHyp->GetCreationCmd()->GetMethod() == "CreateHypothesis" ) {
1254 // not yet converted creation cmd
1255 TCollection_AsciiString hypTypeQuoted = my1dHyp->GetCreationCmd()->GetArg(1);
1256 TCollection_AsciiString hypType = hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
1257 newName = hypType + "_Distribution";
1258 my1dHyp->GetCreationCmd()->SetResultValue( newName );
1261 // already converted creation cmd
1262 newName = my1dHyp->GetCreationCmd()->GetResultValue();
1265 // as creation of 1D hyp was written later then it's edition,
1266 // we need to find all it's edition calls and process them
1267 list< Handle(_pyCommand) >& cmds = theGen->GetCommands();
1268 list< Handle(_pyCommand) >::iterator cmdIt = cmds.begin();
1269 for ( ; cmdIt != cmds.end(); ++cmdIt ) {
1270 const _pyID& objID = (*cmdIt)->GetObject();
1271 if ( objID == hyp1dID ) {
1272 my1dHyp->Process( *cmdIt );
1273 my1dHyp->GetCreationCmd()->AddDependantCmd( *cmdIt );
1274 ( *cmdIt )->SetObject( newName );
1277 if ( !myArgCommands.empty() )
1278 myArgCommands.front()->Clear();
1279 theCommand->SetArg( 1, newName );
1280 myArgCommands.push_back( theCommand );
1281 // copy hyp1d's creation method and args
1282 // myCreationMethod = hyp1d->GetCreationMethod();
1283 // myArgs = hyp1d->GetArgs();
1284 // // make them cleared at conversion
1285 // myArgCommands = hyp1d->GetArgCommands();
1287 // // to be cleared at convertion only
1288 // myArgCommands.push_back( theCommand );
1291 //================================================================================
1294 * \param theAdditionCmd -
1298 //================================================================================
1300 bool _pyLayerDistributionHypo::Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
1301 const _pyID& theMesh)
1303 myIsWrapped = false;
1305 if ( my1dHyp.IsNull() )
1308 // set "SetLayerDistribution()" after addition cmd
1309 theAdditionCmd->AddDependantCmd( myArgCommands.front() );
1311 _pyID geom = theAdditionCmd->GetArg( 1 );
1313 my1dHyp->SetMesh( theMesh );
1314 if ( !my1dHyp->Addition2Creation( theAdditionCmd, theMesh ))
1317 // clear "SetLayerDistribution()" cmd
1318 myArgCommands.front()->Clear();
1320 // Convert my creation => me = RadialPrismAlgo.Get3DHypothesis()
1322 // find RadialPrism algo created on <geom> for theMesh
1323 Handle(_pyHypothesis) algo = theGen->FindAlgo( geom, theMesh, this );
1324 if ( !algo.IsNull() ) {
1325 GetCreationCmd()->SetObject( algo->GetID() );
1326 GetCreationCmd()->SetMethod( "Get3DHypothesis" );
1327 GetCreationCmd()->RemoveArgs();
1328 theAdditionCmd->AddDependantCmd( GetCreationCmd() );
1334 //================================================================================
1338 //================================================================================
1340 void _pyLayerDistributionHypo::Flush()
1342 //my1dHyp.Nullify();
1343 //_pyHypothesis::Flush();
1346 //================================================================================
1348 * \brief additionally to Addition2Creation, clears SetDistrType() command
1349 * \param theCmd - AddHypothesis() command
1350 * \param theMesh - mesh to which a hypothesis is added
1351 * \retval bool - convertion result
1353 //================================================================================
1355 bool _pyNumberOfSegmentsHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
1356 const _pyID& theMesh)
1358 if ( IsWrappable( theMesh ) && myArgs.Length() > 1 ) {
1359 // scale factor (2-nd arg) is provided: clear SetDistrType(1) command
1360 bool scaleDistrType = false;
1361 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnknownCommands.rbegin();
1362 for ( ; cmd != myUnknownCommands.rend(); ++cmd ) {
1363 if ( (*cmd)->GetMethod() == "SetDistrType" ) {
1364 if ( (*cmd)->GetArg( 1 ) == "1" ) {
1365 scaleDistrType = true;
1368 else if ( !scaleDistrType ) {
1369 // distribution type changed: remove scale factor from args
1370 myArgs.Remove( 2, myArgs.Length() );
1376 return _pyHypothesis::Addition2Creation( theCmd, theMesh );
1379 //================================================================================
1381 * \brief remove repeated commands defining distribution
1383 //================================================================================
1385 void _pyNumberOfSegmentsHyp::Flush()
1387 // find number of the last SetDistrType() command
1388 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnknownCommands.rbegin();
1389 int distrTypeNb = 0;
1390 for ( ; !distrTypeNb && cmd != myUnknownCommands.rend(); ++cmd )
1391 if ( (*cmd)->GetMethod() == "SetDistrType" )
1392 distrTypeNb = (*cmd)->GetOrderNb();
1394 // clear commands before the last SetDistrType()
1395 list<Handle(_pyCommand)> * cmds[2] = { &myArgCommands, &myUnknownCommands };
1396 for ( int i = 0; i < 2; ++i ) {
1397 set<TCollection_AsciiString> uniqueMethods;
1398 list<Handle(_pyCommand)> & cmdList = *cmds[i];
1399 for ( cmd = cmdList.rbegin(); cmd != cmdList.rend(); ++cmd )
1401 bool clear = ( (*cmd)->GetOrderNb() < distrTypeNb );
1402 const TCollection_AsciiString& method = (*cmd)->GetMethod();
1403 if ( !clear || method == "SetNumberOfSegments" ) {
1404 bool isNewInSet = uniqueMethods.insert( method ).second;
1405 clear = !isNewInSet;
1414 //================================================================================
1416 * \brief Convert the command adding "SegmentLengthAroundVertex" to mesh
1417 * into regular1D.LengthNearVertex( length, vertex )
1418 * \param theCmd - The command like mesh.AddHypothesis( vertex, SegmentLengthAroundVertex )
1419 * \param theMesh - The mesh needing this hypo
1420 * \retval bool - false if the command cant be converted
1422 //================================================================================
1424 bool _pySegmentLengthAroundVertexHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
1425 const _pyID& theMeshID)
1427 if ( IsWrappable( theMeshID )) {
1429 _pyID vertex = theCmd->GetArg( 1 );
1431 // the problem here is that segment algo will not be found
1432 // by pyHypothesis::Addition2Creation() for <vertex>, so we try to find
1433 // geometry where segment algorithm is assigned
1434 Handle(_pyHypothesis) algo;
1435 _pyID geom = vertex;
1436 while ( algo.IsNull() && !geom.IsEmpty()) {
1437 // try to find geom as a father of <vertex>
1438 geom = FatherID( geom );
1439 algo = theGen->FindAlgo( geom, theMeshID, this );
1441 if ( algo.IsNull() )
1442 return false; // also possible to find geom as brother of veretex...
1443 // set geom instead of vertex
1444 theCmd->SetArg( 1, geom );
1446 // set vertex as a second arg
1447 if ( myArgs.Length() < 1) myArgs.Append( "1" ); // :(
1448 myArgs.Append( vertex );
1450 // mesh.AddHypothesis(vertex, SegmentLengthAroundVertex) -->
1451 // theMeshID.LengthNearVertex( length, vertex )
1452 return _pyHypothesis::Addition2Creation( theCmd, theMeshID );
1457 //================================================================================
1459 * \brief _pyAlgorithm constructor
1460 * \param theCreationCmd - The command like "algo = smeshgen.CreateHypothesis(type,lib)"
1462 //================================================================================
1464 _pyAlgorithm::_pyAlgorithm(const Handle(_pyCommand)& theCreationCmd)
1465 : _pyHypothesis( theCreationCmd )
1470 //================================================================================
1472 * \brief Convert the command adding an algorithm to mesh
1473 * \param theCmd - The command like mesh.AddHypothesis( geom, algo )
1474 * \param theMesh - The mesh needing this algo
1475 * \retval bool - false if the command cant be converted
1477 //================================================================================
1479 bool _pyAlgorithm::Addition2Creation( const Handle(_pyCommand)& theCmd,
1480 const _pyID& theMeshID)
1482 // mesh.AddHypothesis(geom,algo) --> theMeshID.myCreationMethod()
1483 if ( _pyHypothesis::Addition2Creation( theCmd, theMeshID )) {
1484 theGen->SetAccessorMethod( GetID(), "GetAlgorithm()" );
1490 //================================================================================
1492 * \brief Return starting position of a part of python command
1493 * \param thePartIndex - The index of command part
1494 * \retval int - Part position
1496 //================================================================================
1498 int _pyCommand::GetBegPos( int thePartIndex )
1502 if ( myBegPos.Length() < thePartIndex )
1504 return myBegPos( thePartIndex );
1507 //================================================================================
1509 * \brief Store starting position of a part of python command
1510 * \param thePartIndex - The index of command part
1511 * \param thePosition - Part position
1513 //================================================================================
1515 void _pyCommand::SetBegPos( int thePartIndex, int thePosition )
1517 while ( myBegPos.Length() < thePartIndex )
1518 myBegPos.Append( UNKNOWN );
1519 myBegPos( thePartIndex ) = thePosition;
1522 //================================================================================
1524 * \brief Returns whitespace symbols at the line beginning
1525 * \retval TCollection_AsciiString - result
1527 //================================================================================
1529 TCollection_AsciiString _pyCommand::GetIndentation()
1532 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
1533 GetWord( myString, end, true );
1535 end = GetBegPos( RESULT_IND );
1536 return myString.SubString( 1, end - 1 );
1539 //================================================================================
1541 * \brief Return substring of python command looking like ResultValue = Obj.Meth()
1542 * \retval const TCollection_AsciiString & - ResultValue substring
1544 //================================================================================
1546 const TCollection_AsciiString & _pyCommand::GetResultValue()
1548 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
1550 int begPos = myString.Location( "=", 1, Length() );
1552 myRes = GetWord( myString, begPos, false );
1555 SetBegPos( RESULT_IND, begPos );
1560 //================================================================================
1562 * \brief Return substring of python command looking like ResVal = Object.Meth()
1563 * \retval const TCollection_AsciiString & - Object substring
1565 //================================================================================
1567 const TCollection_AsciiString & _pyCommand::GetObject()
1569 if ( GetBegPos( OBJECT_IND ) == UNKNOWN )
1572 int begPos = GetBegPos( RESULT_IND ) + myRes.Length();
1574 begPos = myString.Location( "=", 1, Length() ) + 1;
1576 myObj = GetWord( myString, begPos, true );
1577 SetBegPos( OBJECT_IND, begPos );
1583 //================================================================================
1585 * \brief Return substring of python command looking like ResVal = Obj.Method()
1586 * \retval const TCollection_AsciiString & - Method substring
1588 //================================================================================
1590 const TCollection_AsciiString & _pyCommand::GetMethod()
1592 if ( GetBegPos( METHOD_IND ) == UNKNOWN )
1595 int begPos = GetBegPos( OBJECT_IND ) + myObj.Length();
1596 bool forward = true;
1598 begPos = myString.Location( "(", 1, Length() ) - 1;
1602 myMeth = GetWord( myString, begPos, forward );
1603 SetBegPos( METHOD_IND, begPos );
1609 //================================================================================
1611 * \brief Return substring of python command looking like ResVal = Obj.Meth(Arg1,...)
1612 * \retval const TCollection_AsciiString & - Arg<index> substring
1614 //================================================================================
1616 const TCollection_AsciiString & _pyCommand::GetArg( int index )
1618 if ( GetBegPos( ARG1_IND ) == UNKNOWN )
1621 int begPos = GetBegPos( METHOD_IND ) + myMeth.Length();
1623 begPos = myString.Location( "(", 1, Length() ) + 1;
1625 int i = 0, prevLen = 0;
1626 while ( begPos != EMPTY ) {
1628 // check if we are looking at the closing parenthesis
1629 while ( begPos <= Length() && isspace( myString.Value( begPos )))
1631 if ( begPos > Length() || myString.Value( begPos ) == ')' )
1633 myArgs.Append( GetWord( myString, begPos, true, true ));
1634 SetBegPos( ARG1_IND + i, begPos );
1635 prevLen = myArgs.Last().Length();
1637 myArgs.Remove( myArgs.Length() ); // no more args
1641 if ( myArgs.Length() < index )
1642 return theEmptyString;
1643 return myArgs( index );
1646 //================================================================================
1648 * \brief Check if char is a word part
1649 * \param c - The character to check
1650 * \retval bool - The check result
1652 //================================================================================
1654 static inline bool isWord(const char c, const bool dotIsWord)
1657 !isspace(c) && c != ',' && c != '=' && c != ')' && c != '(' && ( dotIsWord || c != '.');
1660 //================================================================================
1662 * \brief Looks for a word in the string and returns word's beginning
1663 * \param theString - The input string
1664 * \param theStartPos - The position to start the search, returning word's beginning
1665 * \param theForward - The search direction
1666 * \retval TCollection_AsciiString - The found word
1668 //================================================================================
1670 TCollection_AsciiString _pyCommand::GetWord( const TCollection_AsciiString & theString,
1672 const bool theForward,
1673 const bool dotIsWord )
1675 int beg = theStartPos, end = theStartPos;
1676 theStartPos = EMPTY;
1677 if ( beg < 1 || beg > theString.Length() )
1678 return theEmptyString;
1680 if ( theForward ) { // search forward
1682 while ( beg <= theString.Length() && !isWord( theString.Value( beg ), dotIsWord))
1684 if ( beg > theString.Length() )
1685 return theEmptyString; // no word found
1688 while ( end <= theString.Length() && isWord( theString.Value( end ), dotIsWord))
1692 else { // search backward
1694 while ( end > 0 && !isWord( theString.Value( end ), dotIsWord))
1697 return theEmptyString; // no word found
1699 while ( beg > 0 && isWord( theString.Value( beg ), dotIsWord))
1704 //cout << theString << " ---- " << beg << " - " << end << endl;
1705 return theString.SubString( beg, end );
1708 //================================================================================
1710 * \brief Look for position where not space char is
1711 * \param theString - The string
1712 * \param thePos - The position to search from and which returns result
1713 * \retval bool - false if there are only space after thePos in theString
1717 //================================================================================
1719 bool _pyCommand::SkipSpaces( const TCollection_AsciiString & theString, int & thePos )
1721 if ( thePos < 1 || thePos > theString.Length() )
1724 while ( thePos <= theString.Length() && isspace( theString.Value( thePos )))
1727 return thePos <= theString.Length();
1730 //================================================================================
1732 * \brief Modify a part of the command
1733 * \param thePartIndex - The index of the part
1734 * \param thePart - The new part string
1735 * \param theOldPart - The old part
1737 //================================================================================
1739 void _pyCommand::SetPart(int thePartIndex, const TCollection_AsciiString& thePart,
1740 TCollection_AsciiString& theOldPart)
1742 int pos = GetBegPos( thePartIndex );
1743 if ( pos <= Length() && theOldPart != thePart)
1745 TCollection_AsciiString seperator;
1747 pos = GetBegPos( thePartIndex + 1 );
1748 if ( pos < 1 ) return;
1749 switch ( thePartIndex ) {
1750 case RESULT_IND: seperator = " = "; break;
1751 case OBJECT_IND: seperator = "."; break;
1752 case METHOD_IND: seperator = "()"; break;
1756 myString.Remove( pos, theOldPart.Length() );
1757 if ( !seperator.IsEmpty() )
1758 myString.Insert( pos , seperator );
1759 myString.Insert( pos, thePart );
1760 // update starting positions of the following parts
1761 int posDelta = thePart.Length() + seperator.Length() - theOldPart.Length();
1762 for ( int i = thePartIndex + 1; i <= myBegPos.Length(); ++i ) {
1763 if ( myBegPos( i ) > 0 )
1764 myBegPos( i ) += posDelta;
1766 theOldPart = thePart;
1770 //================================================================================
1772 * \brief Set agrument
1773 * \param index - The argument index, it counts from 1
1774 * \param theArg - The argument string
1776 //================================================================================
1778 void _pyCommand::SetArg( int index, const TCollection_AsciiString& theArg)
1781 int argInd = ARG1_IND + index - 1;
1782 int pos = GetBegPos( argInd );
1783 if ( pos < 1 ) // no index-th arg exist, append inexistent args
1785 // find a closing parenthesis
1786 if ( int lastArgInd = GetNbArgs() ) {
1787 pos = GetBegPos( ARG1_IND + lastArgInd - 1 ) + GetArg( lastArgInd ).Length();
1788 while ( pos > 0 && pos <= Length() && myString.Value( pos ) != ')' )
1793 while ( pos > 0 && myString.Value( pos ) != ')' )
1796 if ( pos < 1 || myString.Value( pos ) != ')' ) { // no parentheses at all
1800 while ( myArgs.Length() < index ) {
1801 if ( myArgs.Length() )
1802 myString.Insert( pos++, "," );
1803 myArgs.Append("None");
1804 myString.Insert( pos, myArgs.Last() );
1805 SetBegPos( ARG1_IND + myArgs.Length() - 1, pos );
1806 pos += myArgs.Last().Length();
1809 SetPart( argInd, theArg, myArgs( index ));
1812 //================================================================================
1814 * \brief Empty arg list
1816 //================================================================================
1818 void _pyCommand::RemoveArgs()
1820 if ( int pos = myString.Location( '(', 1, Length() ))
1821 myString.Trunc( pos );
1824 if ( myBegPos.Length() >= ARG1_IND )
1825 myBegPos.Remove( ARG1_IND, myBegPos.Length() );
1828 //================================================================================
1830 * \brief Set dependent commands after this one
1832 //================================================================================
1834 bool _pyCommand::SetDependentCmdsAfter() const
1836 bool orderChanged = false;
1837 list< Handle(_pyCommand)>::const_reverse_iterator cmd = myDependentCmds.rbegin();
1838 for ( ; cmd != myDependentCmds.rend(); ++cmd ) {
1839 if ( (*cmd)->GetOrderNb() < GetOrderNb() ) {
1840 orderChanged = true;
1841 theGen->SetCommandAfter( *cmd, this );
1842 (*cmd)->SetDependentCmdsAfter();
1845 return orderChanged;
1847 //================================================================================
1849 * \brief Insert accessor method after theObjectID
1850 * \param theObjectID - id of the accessed object
1851 * \param theAcsMethod - name of the method giving access to the object
1852 * \retval bool - false if theObjectID is not found in the command string
1854 //================================================================================
1856 bool _pyCommand::AddAccessorMethod( _pyID theObjectID, const char* theAcsMethod )
1858 if ( !theAcsMethod )
1860 // start object search from the object, i.e. ignore result
1862 int beg = GetBegPos( OBJECT_IND );
1863 if ( beg < 1 || beg > Length() )
1866 while (( beg = myString.Location( theObjectID, beg, Length() )))
1868 // check that theObjectID is not just a part of a longer ID
1869 int afterEnd = beg + theObjectID.Length();
1870 Standard_Character c = myString.Value( afterEnd );
1871 if ( !isalnum( c ) && c != ':' ) {
1872 // check if accessor method already present
1874 myString.Location( (char*) theAcsMethod, afterEnd, Length() ) != afterEnd+1) {
1876 int oldLen = Length();
1877 myString.Insert( afterEnd, (char*) theAcsMethod );
1878 myString.Insert( afterEnd, "." );
1879 // update starting positions of the parts following the modified one
1880 int posDelta = Length() - oldLen;
1881 for ( int i = 1; i <= myBegPos.Length(); ++i ) {
1882 if ( myBegPos( i ) > afterEnd )
1883 myBegPos( i ) += posDelta;
1888 beg = afterEnd; // is a part - next search
1893 //================================================================================
1895 * \brief Return method name giving access to an interaface object wrapped by python class
1896 * \retval const char* - method name
1898 //================================================================================
1900 const char* _pyObject::AccessorMethod() const
1904 //================================================================================
1906 * \brief Return ID of a father
1908 //================================================================================
1910 _pyID _pyObject::FatherID(const _pyID & childID)
1912 int colPos = childID.SearchFromEnd(':');
1914 return childID.SubString( 1, colPos-1 );