1 // SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 // File : SMESH_2D_Algo_i.hxx
25 // Author : Paul RASCLE, EDF
29 // File : SMESH_2smeshpy.cxx
30 // Created : Fri Nov 18 13:20:10 2005
31 // Author : Edward AGAPOV (eap)
33 #include "SMESH_2smeshpy.hxx"
35 #include "utilities.h"
36 #include "SMESH_PythonDump.hxx"
37 #include "Resource_DataMapOfAsciiStringAsciiString.hxx"
39 #include "SMESH_Gen_i.hxx"
40 /* SALOME headers that include CORBA headers that include windows.h
41 * that defines GetObject symbol as GetObjectA should stand before SALOME headers
42 * that declare methods named GetObject - to apply the same rules of GetObject renaming
43 * and thus to avoid mess with GetObject symbol on Windows */
45 IMPLEMENT_STANDARD_HANDLE (_pyObject ,Standard_Transient);
46 IMPLEMENT_STANDARD_HANDLE (_pyCommand ,Standard_Transient);
47 IMPLEMENT_STANDARD_HANDLE (_pyGen ,_pyObject);
48 IMPLEMENT_STANDARD_HANDLE (_pyMesh ,_pyObject);
49 IMPLEMENT_STANDARD_HANDLE (_pyMeshEditor ,_pyObject);
50 IMPLEMENT_STANDARD_HANDLE (_pyHypothesis ,_pyObject);
51 IMPLEMENT_STANDARD_HANDLE (_pyAlgorithm ,_pyHypothesis);
52 IMPLEMENT_STANDARD_HANDLE (_pyComplexParamHypo,_pyHypothesis);
53 IMPLEMENT_STANDARD_HANDLE (_pyNumberOfSegmentsHyp,_pyHypothesis);
55 IMPLEMENT_STANDARD_RTTIEXT(_pyObject ,Standard_Transient);
56 IMPLEMENT_STANDARD_RTTIEXT(_pyCommand ,Standard_Transient);
57 IMPLEMENT_STANDARD_RTTIEXT(_pyGen ,_pyObject);
58 IMPLEMENT_STANDARD_RTTIEXT(_pyMesh ,_pyObject);
59 IMPLEMENT_STANDARD_RTTIEXT(_pyMeshEditor ,_pyObject);
60 IMPLEMENT_STANDARD_RTTIEXT(_pyHypothesis ,_pyObject);
61 IMPLEMENT_STANDARD_RTTIEXT(_pyAlgorithm ,_pyHypothesis);
62 IMPLEMENT_STANDARD_RTTIEXT(_pyComplexParamHypo,_pyHypothesis);
63 IMPLEMENT_STANDARD_RTTIEXT(_pyNumberOfSegmentsHyp,_pyHypothesis);
64 IMPLEMENT_STANDARD_RTTIEXT(_pyLayerDistributionHypo,_pyHypothesis);
65 IMPLEMENT_STANDARD_RTTIEXT(_pySegmentLengthAroundVertexHyp,_pyHypothesis);
68 using SMESH::TPythonDump;
71 * \brief Container of commands into which the initial script is split.
72 * It also contains data coresponding to SMESH_Gen contents
74 static Handle(_pyGen) theGen;
76 static TCollection_AsciiString theEmptyString;
78 //#define DUMP_CONVERSION
80 #if !defined(_DEBUG_) && defined(DUMP_CONVERSION)
81 #undef DUMP_CONVERSION
87 //================================================================================
89 * \brief Set of TCollection_AsciiString initialized by C array of C strings
91 //================================================================================
93 struct TStringSet: public set<TCollection_AsciiString>
96 * \brief Filling. The last string must be ""
98 void Insert(const char* names[]) {
99 for ( int i = 0; names[i][0] ; ++i )
100 insert( (char*) names[i] );
103 * \brief Check if a string is in
105 bool Contains(const TCollection_AsciiString& name ) {
106 return find( name ) != end();
111 //================================================================================
113 * \brief Convert python script using commands of smesh.py
114 * \param theScript - Input script
115 * \retval TCollection_AsciiString - Convertion result
117 * Class SMESH_2smeshpy declared in SMESH_PythonDump.hxx
119 //================================================================================
121 TCollection_AsciiString
122 SMESH_2smeshpy::ConvertScript(const TCollection_AsciiString& theScript,
123 Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod)
125 theGen = new _pyGen( theEntry2AccessorMethod );
127 // split theScript into separate commands
128 int from = 1, end = theScript.Length(), to;
129 while ( from < end && ( to = theScript.Location( "\n", from, end )))
132 // cut out and store a command
133 theGen->AddCommand( theScript.SubString( from, to - 1 ));
138 #ifdef DUMP_CONVERSION
139 MESSAGE_BEGIN ( std::endl << " ######## RESULT ######## " << std::endl<< std::endl );
141 // reorder commands after conversion
142 list< Handle(_pyCommand) >::iterator cmd;
145 orderChanges = false;
146 for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
147 if ( (*cmd)->SetDependentCmdsAfter() )
149 } while ( orderChanges );
151 // concat commands back into a script
152 TCollection_AsciiString aScript;
153 for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
155 #ifdef DUMP_CONVERSION
156 MESSAGE_ADD ( "## COM " << (*cmd)->GetOrderNb() << ": "<< (*cmd)->GetString() << std::endl );
158 if ( !(*cmd)->IsEmpty() ) {
160 aScript += (*cmd)->GetString();
170 //================================================================================
172 * \brief _pyGen constructor
174 //================================================================================
176 _pyGen::_pyGen(Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod)
177 : _pyObject( new _pyCommand( TPythonDump::SMESHGenName(), 0 )),
178 myID2AccessorMethod( theEntry2AccessorMethod )
181 myHasPattern = false;
182 // make that GetID() to return TPythonDump::SMESHGenName()
183 GetCreationCmd()->GetString() += "=";
186 //================================================================================
188 * \brief name of SMESH_Gen in smesh.py
190 //================================================================================
192 const char* _pyGen::AccessorMethod() const
194 return SMESH_2smeshpy::GenName();
197 //================================================================================
199 * \brief Convert a command using a specific converter
200 * \param theCommand - the command to convert
202 //================================================================================
204 Handle(_pyCommand) _pyGen::AddCommand( const TCollection_AsciiString& theCommand)
206 // store theCommand in the sequence
207 myCommands.push_back( new _pyCommand( theCommand, ++myNbCommands ));
209 Handle(_pyCommand) aCommand = myCommands.back();
210 #ifdef DUMP_CONVERSION
211 MESSAGE ( "## COM " << myNbCommands << ": "<< aCommand->GetString() );
214 _pyID objID = aCommand->GetObject();
216 if ( objID.IsEmpty() )
220 if ( objID == this->GetID() ) {
221 this->Process( aCommand );
224 // SMESH_Mesh method?
225 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( objID );
226 if ( id_mesh != myMeshes.end() ) {
227 if ( aCommand->GetMethod() == "GetMeshEditor" ) { // MeshEditor creation
228 _pyID editorID = aCommand->GetResultValue();
229 Handle(_pyMeshEditor) editor = new _pyMeshEditor( aCommand );
230 myMeshEditors.insert( make_pair( editorID, editor ));
233 id_mesh->second->Process( aCommand );
236 // SMESH_MeshEditor method?
237 map< _pyID, Handle(_pyMeshEditor) >::iterator id_editor = myMeshEditors.find( objID );
238 if ( id_editor != myMeshEditors.end() ) {
239 id_editor->second->Process( aCommand );
242 // SMESH_Hypothesis method?
243 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
244 for ( ; hyp != myHypos.end(); ++hyp )
245 if ( !(*hyp)->IsAlgo() && objID == (*hyp)->GetID() ) {
246 (*hyp)->Process( aCommand );
250 // Add access to a wrapped mesh
251 AddMeshAccessorMethod( aCommand );
253 // Add access to a wrapped algorithm
254 // AddAlgoAccessorMethod( aCommand ); // ??? what if algo won't be wrapped at all ???
256 // PAL12227. PythonDump was not updated at proper time; result is
257 // aCriteria.append(SMESH.Filter.Criterion(17,26,0,'L1',26,25,1e-07,SMESH.EDGE,-1))
258 // TypeError: __init__() takes exactly 11 arguments (10 given)
259 char wrongCommand[] = "SMESH.Filter.Criterion(";
260 if ( int beg = theCommand.Location( wrongCommand, 1, theCommand.Length() ))
262 _pyCommand tmpCmd( theCommand.SubString( beg, theCommand.Length() ), -1);
263 // there must be 10 arguments, 5-th arg ThresholdID is missing,
264 const int wrongNbArgs = 9, missingArg = 5;
265 if ( tmpCmd.GetNbArgs() == wrongNbArgs )
267 for ( int i = wrongNbArgs; i > missingArg; --i )
268 tmpCmd.SetArg( i + 1, tmpCmd.GetArg( i ));
269 tmpCmd.SetArg( missingArg, "''");
270 aCommand->GetString().Trunc( beg - 1 );
271 aCommand->GetString() += tmpCmd.GetString();
277 //================================================================================
279 * \brief Convert the command or remember it for later conversion
280 * \param theCommand - The python command calling a method of SMESH_Gen
282 //================================================================================
284 void _pyGen::Process( const Handle(_pyCommand)& theCommand )
286 // there are methods to convert:
287 // CreateMesh( shape )
288 // Concatenate( [mesh1, ...], ... )
289 // CreateHypothesis( theHypType, theLibName )
290 // Compute( mesh, geom )
292 if ( theCommand->GetMethod() == "CreateMesh" ||
293 theCommand->GetMethod() == "CreateEmptyMesh" ||
294 theCommand->GetMethod() == "CreateMeshesFromUNV" ||
295 theCommand->GetMethod() == "CreateMeshesFromSTL")
297 Handle(_pyMesh) mesh = new _pyMesh( theCommand );
298 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
302 if(theCommand->GetMethod() == "CreateMeshesFromMED")
304 for(int ind = 0;ind<theCommand->GetNbResultValues();ind++)
306 Handle(_pyMesh) mesh = new _pyMesh( theCommand, theCommand->GetResultValue(ind));
307 myMeshes.insert( make_pair( theCommand->GetResultValue(ind), mesh ));
311 // CreateHypothesis()
312 if ( theCommand->GetMethod() == "CreateHypothesis" )
314 myHypos.push_back( _pyHypothesis::NewHypothesis( theCommand ));
318 // smeshgen.Compute( mesh, geom ) --> mesh.Compute()
319 if ( theCommand->GetMethod() == "Compute" )
321 const _pyID& meshID = theCommand->GetArg( 1 );
322 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( meshID );
323 if ( id_mesh != myMeshes.end() ) {
324 theCommand->SetObject( meshID );
325 theCommand->RemoveArgs();
326 id_mesh->second->Flush();
331 // leave only one smeshgen.GetPattern() in the script
332 if ( theCommand->GetMethod() == "GetPattern" ) {
333 if ( myHasPattern ) {
340 // Concatenate( [mesh1, ...], ... )
341 if ( theCommand->GetMethod() == "Concatenate" ||
342 theCommand->GetMethod() == "ConcatenateWithGroups")
344 AddMeshAccessorMethod( theCommand );
347 // Replace name of SMESH_Gen
349 // names of SMESH_Gen methods fully equal to methods defined in smesh.py
350 static TStringSet smeshpyMethods;
351 if ( smeshpyMethods.empty() ) {
352 const char * names[] =
353 { "SetEmbeddedMode","IsEmbeddedMode","SetCurrentStudy","GetCurrentStudy",
354 "GetPattern","GetSubShapesId",
355 "" }; // <- mark of array end
356 smeshpyMethods.Insert( names );
358 if ( smeshpyMethods.Contains( theCommand->GetMethod() ))
359 // smeshgen.Method() --> smesh.Method()
360 theCommand->SetObject( SMESH_2smeshpy::SmeshpyName() );
362 // smeshgen.Method() --> smesh.smesh.Method()
363 theCommand->SetObject( SMESH_2smeshpy::GenName() );
366 //================================================================================
368 * \brief Convert the remembered commands
370 //================================================================================
374 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.begin();
375 for ( ; id_mesh != myMeshes.end(); ++id_mesh )
376 if ( ! id_mesh->second.IsNull() )
377 id_mesh->second->Flush();
379 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
380 for ( ; hyp != myHypos.end(); ++hyp )
381 if ( !hyp->IsNull() ) {
383 // smeshgen.CreateHypothesis() --> smesh.smesh.CreateHypothesis()
384 if ( !(*hyp)->IsWrapped() )
385 (*hyp)->GetCreationCmd()->SetObject( SMESH_2smeshpy::GenName() );
389 //================================================================================
391 * \brief Add access method to mesh that is an argument
392 * \param theCmd - command to add access method
393 * \retval bool - true if added
395 //================================================================================
397 bool _pyGen::AddMeshAccessorMethod( Handle(_pyCommand) theCmd ) const
400 map< _pyID, Handle(_pyMesh) >::const_iterator id_mesh = myMeshes.begin();
401 for ( ; id_mesh != myMeshes.end(); ++id_mesh ) {
402 if ( theCmd->AddAccessorMethod( id_mesh->first, id_mesh->second->AccessorMethod() ))
408 //================================================================================
410 * \brief Add access method to algo that is an object or an argument
411 * \param theCmd - command to add access method
412 * \retval bool - true if added
414 //================================================================================
416 bool _pyGen::AddAlgoAccessorMethod( Handle(_pyCommand) theCmd ) const
419 list< Handle(_pyHypothesis) >::const_iterator hyp = myHypos.begin();
420 for ( ; hyp != myHypos.end(); ++hyp ) {
421 if ( (*hyp)->IsAlgo() && /*(*hyp)->IsWrapped() &&*/
422 theCmd->AddAccessorMethod( (*hyp)->GetID(), (*hyp)->AccessorMethod() ))
428 //================================================================================
430 * \brief Find hypothesis by ID (entry)
431 * \param theHypID - The hypothesis ID
432 * \retval Handle(_pyHypothesis) - The found hypothesis
434 //================================================================================
436 Handle(_pyHypothesis) _pyGen::FindHyp( const _pyID& theHypID )
438 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
439 for ( ; hyp != myHypos.end(); ++hyp )
440 if ( !hyp->IsNull() && theHypID == (*hyp)->GetID() )
442 return Handle(_pyHypothesis)();
445 //================================================================================
447 * \brief Find algorithm the created algorithm
448 * \param theGeom - The shape ID the algorithm was created on
449 * \param theMesh - The mesh ID that created the algorithm
450 * \param dim - The algo dimension
451 * \retval Handle(_pyHypothesis) - The found algo
453 //================================================================================
455 Handle(_pyHypothesis) _pyGen::FindAlgo( const _pyID& theGeom, const _pyID& theMesh,
456 const Handle(_pyHypothesis)& theHypothesis )
458 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
459 for ( ; hyp != myHypos.end(); ++hyp )
460 if ( !hyp->IsNull() &&
462 theHypothesis->CanBeCreatedBy( (*hyp)->GetAlgoType() ) &&
463 (*hyp)->GetGeom() == theGeom &&
464 (*hyp)->GetMesh() == theMesh )
469 //================================================================================
471 * \brief Change order of commands in the script
472 * \param theCmd1 - One command
473 * \param theCmd2 - Another command
475 //================================================================================
477 void _pyGen::ExchangeCommands( Handle(_pyCommand) theCmd1, Handle(_pyCommand) theCmd2 )
479 list< Handle(_pyCommand) >::iterator pos1, pos2;
480 pos1 = find( myCommands.begin(), myCommands.end(), theCmd1 );
481 pos2 = find( myCommands.begin(), myCommands.end(), theCmd2 );
482 myCommands.insert( pos1, theCmd2 );
483 myCommands.insert( pos2, theCmd1 );
484 myCommands.erase( pos1 );
485 myCommands.erase( pos2 );
487 int nb1 = theCmd1->GetOrderNb();
488 theCmd1->SetOrderNb( theCmd2->GetOrderNb() );
489 theCmd2->SetOrderNb( nb1 );
490 // cout << "BECOME " << theCmd1->GetOrderNb() << "\t" << theCmd1->GetString() << endl
491 // << "BECOME " << theCmd2->GetOrderNb() << "\t" << theCmd2->GetString() << endl << endl;
494 //================================================================================
496 * \brief Set one command after the other
497 * \param theCmd - Command to move
498 * \param theAfterCmd - Command ater which to insert the first one
500 //================================================================================
502 void _pyGen::SetCommandAfter( Handle(_pyCommand) theCmd, Handle(_pyCommand) theAfterCmd )
505 //cout << "SET\t" << theAfterCmd->GetString() << endl << "BEFORE\t" << theCmd->GetString() << endl<<endl;
507 list< Handle(_pyCommand) >::iterator pos;
508 pos = find( myCommands.begin(), myCommands.end(), theCmd );
509 myCommands.erase( pos );
510 pos = find( myCommands.begin(), myCommands.end(), theAfterCmd );
511 myCommands.insert( ++pos, theCmd );
514 for ( pos = myCommands.begin(); pos != myCommands.end(); ++pos)
515 (*pos)->SetOrderNb( i++ );
518 //================================================================================
520 * \brief Set method to access to object wrapped with python class
521 * \param theID - The wrapped object entry
522 * \param theMethod - The accessor method
524 //================================================================================
526 void _pyGen::SetAccessorMethod(const _pyID& theID, const char* theMethod )
528 myID2AccessorMethod.Bind( theID, (char*) theMethod );
531 //================================================================================
533 * \brief Find out type of geom group
534 * \param grpID - The geom group entry
535 * \retval int - The type
537 //================================================================================
539 static bool sameGroupType( const _pyID& grpID,
540 const TCollection_AsciiString& theType)
542 // define group type as smesh.Mesh.Group() does
544 SALOMEDS::Study_var study = SMESH_Gen_i::GetSMESHGen()->GetCurrentStudy();
545 SALOMEDS::SObject_var aSObj = study->FindObjectID( grpID.ToCString() );
546 if ( !aSObj->_is_nil() ) {
547 GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow( aSObj->GetObject() );
548 if ( !aGeomObj->_is_nil() ) {
549 switch ( aGeomObj->GetShapeType() ) {
550 case GEOM::VERTEX: type = SMESH::NODE; break;
551 case GEOM::EDGE: type = SMESH::EDGE; break;
552 case GEOM::FACE: type = SMESH::FACE; break;
554 case GEOM::SHELL: type = SMESH::VOLUME; break;
555 case GEOM::COMPOUND: {
556 GEOM::GEOM_Gen_ptr aGeomGen = SMESH_Gen_i::GetSMESHGen()->GetGeomEngine();
557 if ( !aGeomGen->_is_nil() ) {
558 GEOM::GEOM_IGroupOperations_var aGrpOp =
559 aGeomGen->GetIGroupOperations( study->StudyId() );
560 if ( !aGrpOp->_is_nil() ) {
561 switch ( aGrpOp->GetType( aGeomObj )) {
562 case TopAbs_VERTEX: type = SMESH::NODE; break;
563 case TopAbs_EDGE: type = SMESH::EDGE; break;
564 case TopAbs_FACE: type = SMESH::FACE; break;
565 case TopAbs_SOLID: type = SMESH::VOLUME; break;
576 MESSAGE("Type of the group " << grpID << " not found");
579 if ( theType.IsIntegerValue() )
580 return type == theType.IntegerValue();
583 case SMESH::NODE: return theType.Location( "NODE", 1, theType.Length() );
584 case SMESH::EDGE: return theType.Location( "EDGE", 1, theType.Length() );
585 case SMESH::FACE: return theType.Location( "FACE", 1, theType.Length() );
586 case SMESH::VOLUME: return theType.Location( "VOLUME", 1, theType.Length() );
592 //================================================================================
595 * \param theCreationCmd -
597 //================================================================================
599 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd):
600 _pyObject(theCreationCmd), myHasEditor(false)
602 // convert my creation command
603 Handle(_pyCommand) creationCmd = GetCreationCmd();
604 TCollection_AsciiString str = creationCmd->GetMethod();
606 creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
607 if(str != "CreateMeshesFromUNV" &&
608 str != "CreateMeshesFromMED" &&
609 str != "CreateMeshesFromSTL")
610 creationCmd->SetMethod( "Mesh" );
612 theGen->SetAccessorMethod( GetID(), "GetMesh()" );
615 //================================================================================
618 * \param theCreationCmd -
620 //================================================================================
621 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd, const TCollection_AsciiString& id):
622 _pyObject(theCreationCmd), myHasEditor(false)
624 // convert my creation command
625 Handle(_pyCommand) creationCmd = GetCreationCmd();
626 creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
627 theGen->SetAccessorMethod( id, "GetMesh()" );
630 //================================================================================
632 * \brief Convert a IDL API command of SMESH::Mesh to a method call of python Mesh
633 * \param theCommand - Engine method called for this mesh
635 //================================================================================
637 void _pyMesh::Process( const Handle(_pyCommand)& theCommand )
639 // some methods of SMESH_Mesh interface needs special conversion
640 // to methods of Mesh python class
642 // 1. GetSubMesh(geom, name) + AddHypothesis(geom, algo)
643 // --> in Mesh_Algorithm.Create(mesh, geom, hypo, so)
644 // 2. AddHypothesis(geom, hyp)
645 // --> in Mesh_Algorithm.Hypothesis(hyp, args, so)
646 // 3. CreateGroupFromGEOM(type, name, grp)
647 // --> in Mesh.Group(grp, name="")
648 // 4. ExportToMED(f, auto_groups, version)
649 // --> in Mesh.ExportMED( f, auto_groups, version )
652 const TCollection_AsciiString method = theCommand->GetMethod();
653 // ----------------------------------------------------------------------
654 if ( method == "GetSubMesh" ) {
655 mySubmeshes.push_back( theCommand );
657 // ----------------------------------------------------------------------
658 else if ( method == "AddHypothesis" ) { // mesh.AddHypothesis(geom, HYPO )
659 myAddHypCmds.push_back( theCommand );
661 const _pyID& hypID = theCommand->GetArg( 2 );
662 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
663 if ( !hyp.IsNull() ) {
664 myHypos.push_back( hyp );
665 if ( hyp->GetMesh().IsEmpty() )
666 hyp->SetMesh( this->GetID() );
669 // ----------------------------------------------------------------------
670 else if ( method == "CreateGroupFromGEOM" ) {// (type, name, grp)
671 _pyID grp = theCommand->GetArg( 3 );
672 if ( sameGroupType( grp, theCommand->GetArg( 1 )) ) { // --> Group(grp)
673 theCommand->SetMethod( "Group" );
674 theCommand->RemoveArgs();
675 theCommand->SetArg( 1, grp );
678 AddMeshAccess( theCommand );
681 // ----------------------------------------------------------------------
682 else if ( method == "ExportToMED" ) { // ExportToMED() --> ExportMED()
683 theCommand->SetMethod( "ExportMED" );
685 // ----------------------------------------------------------------------
686 else if ( method == "CreateGroup" ) { // CreateGroup() --> CreateEmptyGroup()
687 theCommand->SetMethod( "CreateEmptyGroup" );
689 // ----------------------------------------------------------------------
690 else if ( method == "RemoveHypothesis" ) // (geom, hyp)
692 _pyID hypID = theCommand->GetArg( 2 );
694 // check if this mesh still has corresponding addition command
695 bool hasAddCmd = false;
696 list< Handle(_pyCommand) >::iterator cmd = myAddHypCmds.begin();
697 while ( cmd != myAddHypCmds.end() )
699 // AddHypothesis(geom, hyp)
700 if ( hypID == (*cmd)->GetArg( 2 )) { // erase both (add and remove) commands
703 cmd = myAddHypCmds.erase( cmd );
710 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
711 if ( ! hasAddCmd && hypID.Length() != 0 ) { // hypo addition already wrapped
712 // RemoveHypothesis(geom, hyp) --> RemoveHypothesis( hyp, geom=0 )
713 _pyID geom = theCommand->GetArg( 1 );
714 theCommand->RemoveArgs();
715 theCommand->SetArg( 1, hypID );
716 if ( geom != GetGeom() )
717 theCommand->SetArg( 2, geom );
719 // remove hyp from myHypos
720 myHypos.remove( hyp );
722 // add accessor method if necessary
725 if ( NeedMeshAccess( theCommand ))
726 // apply theCommand to the mesh wrapped by smeshpy mesh
727 AddMeshAccess( theCommand );
731 //================================================================================
733 * \brief Return True if addition of accesor method is needed
735 //================================================================================
737 bool _pyMesh::NeedMeshAccess( const Handle(_pyCommand)& theCommand )
739 // names of SMESH_Mesh methods fully equal to methods of class Mesh, so
740 // no conversion is needed for them at all:
741 static TStringSet sameMethods;
742 if ( sameMethods.empty() ) {
743 const char * names[] =
744 { "ExportDAT","ExportUNV","ExportSTL", "RemoveGroup","RemoveGroupWithContents",
745 "GetGroups","UnionGroups","IntersectGroups","CutGroups","GetLog","GetId","ClearLog",
746 "GetStudyId","HasDuplicatedGroupNamesMED","GetMEDMesh","NbNodes","NbElements",
747 "NbEdges","NbEdgesOfOrder","NbFaces","NbFacesOfOrder","NbTriangles",
748 "NbTrianglesOfOrder","NbQuadrangles","NbQuadranglesOfOrder","NbPolygons","NbVolumes",
749 "NbVolumesOfOrder","NbTetras","NbTetrasOfOrder","NbHexas","NbHexasOfOrder",
750 "NbPyramids","NbPyramidsOfOrder","NbPrisms","NbPrismsOfOrder","NbPolyhedrons",
751 "NbSubMesh","GetElementsId","GetElementsByType","GetNodesId","GetElementType",
752 "GetSubMeshElementsId","GetSubMeshNodesId","GetSubMeshElementType","Dump","GetNodeXYZ",
753 "GetNodeInverseElements","GetShapeID","GetShapeIDForElem","GetElemNbNodes",
754 "GetElemNode","IsMediumNode","IsMediumNodeOfAnyElem","ElemNbEdges","ElemNbFaces",
755 "IsPoly","IsQuadratic","BaryCenter","GetHypothesisList", "SetAutoColor", "GetAutoColor",
757 ,"" }; // <- mark of end
758 sameMethods.Insert( names );
761 return !sameMethods.Contains( theCommand->GetMethod() );
764 //================================================================================
766 * \brief Convert creation and addition of all algos and hypos
768 //================================================================================
770 void _pyMesh::Flush()
772 list < Handle(_pyCommand) >::iterator cmd, cmd2;
774 // try to convert algo addition like this:
775 // mesh.AddHypothesis(geom, ALGO ) --> ALGO = mesh.Algo()
776 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
778 Handle(_pyCommand) addCmd = *cmd;
779 _pyID algoID = addCmd->GetArg( 2 );
780 Handle(_pyHypothesis) algo = theGen->FindHyp( algoID );
781 if ( algo.IsNull() || !algo->IsAlgo() )
784 _pyID geom = addCmd->GetArg( 1 );
785 bool isLocalAlgo = ( geom != GetGeom() );
786 if ( algo->Addition2Creation( addCmd, this->GetID() )) // OK
788 // wrapped algo is created atfer mesh creation
789 GetCreationCmd()->AddDependantCmd( addCmd );
792 // mesh.AddHypothesis(geom, ALGO ) --> mesh.AlgoMethod(geom)
793 addCmd->SetArg( addCmd->GetNbArgs() + 1,
794 TCollection_AsciiString( "geom=" ) + geom );
795 // sm = mesh.GetSubMesh(geom, name) --> sm = ALGO.GetSubMesh()
796 for ( cmd2 = mySubmeshes.begin(); cmd2 != mySubmeshes.end(); ++cmd2 ) {
797 Handle(_pyCommand) subCmd = *cmd2;
798 if ( geom == subCmd->GetArg( 1 )) {
799 subCmd->SetObject( algo->GetID() );
800 subCmd->RemoveArgs();
801 addCmd->AddDependantCmd( subCmd );
806 else // KO - ALGO was already created
808 // mesh.AddHypothesis(geom, ALGO) --> mesh.AddHypothesis(ALGO, geom=0)
809 addCmd->RemoveArgs();
810 addCmd->SetArg( 1, algoID );
812 addCmd->SetArg( 2, geom );
816 // try to convert hypo addition like this:
817 // mesh.AddHypothesis(geom, HYPO ) --> HYPO = algo.Hypo()
818 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
820 Handle(_pyCommand) addCmd = *cmd;
821 _pyID hypID = addCmd->GetArg( 2 );
822 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
823 if ( hyp.IsNull() || hyp->IsAlgo() )
825 bool converted = hyp->Addition2Creation( addCmd, this->GetID() );
827 // mesh.AddHypothesis(geom, HYP) --> mesh.AddHypothesis(HYP, geom=0)
828 _pyID geom = addCmd->GetArg( 1 );
829 addCmd->RemoveArgs();
830 addCmd->SetArg( 1, hypID );
831 if ( geom != GetGeom() )
832 addCmd->SetArg( 2, geom );
836 // sm = mesh.GetSubMesh(geom, name) --> sm = mesh.GetMesh().GetSubMesh(geom, name)
837 // for ( cmd = mySubmeshes.begin(); cmd != mySubmeshes.end(); ++cmd ) {
838 // Handle(_pyCommand) subCmd = *cmd;
839 // if ( subCmd->GetNbArgs() > 0 )
840 // AddMeshAccess( subCmd );
842 myAddHypCmds.clear();
846 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
847 for ( ; hyp != myHypos.end(); ++hyp )
851 //================================================================================
853 * \brief MeshEditor convert its commands to ones of mesh
855 //================================================================================
857 _pyMeshEditor::_pyMeshEditor(const Handle(_pyCommand)& theCreationCmd):
858 _pyObject( theCreationCmd )
860 myMesh = theCreationCmd->GetObject();
861 myCreationCmdStr = theCreationCmd->GetString();
862 theCreationCmd->Clear();
865 //================================================================================
867 * \brief convert its commands to ones of mesh
869 //================================================================================
871 void _pyMeshEditor::Process( const Handle(_pyCommand)& theCommand)
873 // names of SMESH_MeshEditor methods fully equal to methods of class Mesh, so
874 // commands calling this methods are converted to calls of methods of Mesh
875 static TStringSet sameMethods;
876 if ( sameMethods.empty() ) {
877 const char * names[] = {
878 "RemoveElements","RemoveNodes","AddNode","AddEdge","AddFace","AddPolygonalFace",
879 "AddVolume","AddPolyhedralVolume","AddPolyhedralVolumeByFaces","MoveNode",
880 "InverseDiag","DeleteDiag","Reorient","ReorientObject","SplitQuad","SplitQuadObject",
881 "BestSplit","Smooth","SmoothObject","SmoothParametric","SmoothParametricObject",
882 "ConvertToQuadratic","ConvertFromQuadratic","RenumberNodes","RenumberElements",
883 "RotationSweep","RotationSweepObject","ExtrusionSweep","AdvancedExtrusion",
884 "ExtrusionSweepObject","ExtrusionSweepObject1D","ExtrusionSweepObject2D","Mirror",
885 "MirrorObject","Translate","TranslateObject","Rotate","RotateObject",
886 "FindCoincidentNodes","FindCoincidentNodesOnPart","MergeNodes","FindEqualElements",
887 "MergeElements","MergeEqualElements","SewFreeBorders","SewConformFreeBorders",
888 "SewBorderToSide","SewSideElements","ChangeElemNodes","GetLastCreatedNodes",
889 "GetLastCreatedElems",
890 "MirrorMakeMesh","MirrorObjectMakeMesh","TranslateMakeMesh",
891 "TranslateObjectMakeMesh","RotateMakeMesh","RotateObjectMakeMesh"
892 ,"" }; // <- mark of the end
893 sameMethods.Insert( names );
896 if ( sameMethods.Contains( theCommand->GetMethod() )) {
897 theCommand->SetObject( myMesh );
899 // meshes made by *MakeMesh() methods are not wrapped by _pyMesh,
900 // so let _pyMesh care of it (TMP?)
901 if ( theCommand->GetMethod().Search("MakeMesh") != -1 )
902 _pyMesh( new _pyCommand( theCommand->GetString(), 0 )); // for theGen->SetAccessorMethod()
905 // editor creation command is needed only if any editor function is called
906 theGen->AddMeshAccessorMethod( theCommand ); // for *Object()
907 if ( !myCreationCmdStr.IsEmpty() ) {
908 GetCreationCmd()->GetString() = myCreationCmdStr;
909 myCreationCmdStr.Clear();
914 //================================================================================
916 * \brief _pyHypothesis constructor
917 * \param theCreationCmd -
919 //================================================================================
921 _pyHypothesis::_pyHypothesis(const Handle(_pyCommand)& theCreationCmd):
922 _pyObject( theCreationCmd )
924 myIsAlgo = myIsWrapped = /*myIsConverted = myIsLocal = myDim = */false;
927 //================================================================================
929 * \brief Creates algorithm or hypothesis
930 * \param theCreationCmd - The engine command creating a hypothesis
931 * \retval Handle(_pyHypothesis) - Result _pyHypothesis
933 //================================================================================
935 Handle(_pyHypothesis) _pyHypothesis::NewHypothesis( const Handle(_pyCommand)& theCreationCmd)
937 // theCreationCmd: CreateHypothesis( "theHypType", "theLibName" )
938 ASSERT (( theCreationCmd->GetMethod() == "CreateHypothesis"));
940 Handle(_pyHypothesis) hyp, algo;
943 const TCollection_AsciiString & hypTypeQuoted = theCreationCmd->GetArg( 1 );
944 if ( hypTypeQuoted.IsEmpty() )
947 TCollection_AsciiString hypType =
948 hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
950 algo = new _pyAlgorithm( theCreationCmd );
951 hyp = new _pyHypothesis( theCreationCmd );
953 // 1D Regular_1D ----------
954 if ( hypType == "Regular_1D" ) {
955 // set mesh's method creating algo,
956 // i.e. convertion result will be "regular1d = Mesh.Segment()",
957 // and set hypType by which algo creating a hypothesis is searched for
958 algo->SetConvMethodAndType("Segment", hypType.ToCString());
960 else if ( hypType == "CompositeSegment_1D" ) {
961 algo->SetConvMethodAndType("Segment", "Regular_1D");
962 algo->myArgs.Append( "algo=smesh.COMPOSITE");
964 else if ( hypType == "LocalLength" ) {
965 // set algo's method creating hyp, and algo type
966 hyp->SetConvMethodAndType( "LocalLength", "Regular_1D");
967 // set method whose 1 arg will become the 1-st arg of hyp creation command
968 // i.e. convertion result will be "locallength = regular1d.LocalLength(<arg of SetLength()>)"
969 hyp->AddArgMethod( "SetLength" );
971 else if ( hypType == "NumberOfSegments" ) {
972 hyp = new _pyNumberOfSegmentsHyp( theCreationCmd );
973 hyp->SetConvMethodAndType( "NumberOfSegments", "Regular_1D");
974 // arg of SetNumberOfSegments() will become the 1-st arg of hyp creation command
975 hyp->AddArgMethod( "SetNumberOfSegments" );
976 // arg of SetScaleFactor() will become the 2-nd arg of hyp creation command
977 hyp->AddArgMethod( "SetScaleFactor" );
979 else if ( hypType == "Arithmetic1D" ) {
980 hyp = new _pyComplexParamHypo( theCreationCmd );
981 hyp->SetConvMethodAndType( "Arithmetic1D", "Regular_1D");
983 else if ( hypType == "StartEndLength" ) {
984 hyp = new _pyComplexParamHypo( theCreationCmd );
985 hyp->SetConvMethodAndType( "StartEndLength", "Regular_1D");
987 else if ( hypType == "Deflection1D" ) {
988 hyp->SetConvMethodAndType( "Deflection1D", "Regular_1D");
989 hyp->AddArgMethod( "SetDeflection" );
991 else if ( hypType == "Propagation" ) {
992 hyp->SetConvMethodAndType( "Propagation", "Regular_1D");
994 else if ( hypType == "QuadraticMesh" ) {
995 hyp->SetConvMethodAndType( "QuadraticMesh", "Regular_1D");
997 else if ( hypType == "AutomaticLength" ) {
998 hyp->SetConvMethodAndType( "AutomaticLength", "Regular_1D");
999 hyp->AddArgMethod( "SetFineness");
1001 else if ( hypType == "SegmentLengthAroundVertex" ) {
1002 hyp = new _pySegmentLengthAroundVertexHyp( theCreationCmd );
1003 hyp->SetConvMethodAndType( "LengthNearVertex", "Regular_1D" );
1004 hyp->AddArgMethod( "SetLength" );
1006 // 1D Python_1D ----------
1007 else if ( hypType == "Python_1D" ) {
1008 algo->SetConvMethodAndType( "Segment", hypType.ToCString());
1009 algo->myArgs.Append( "algo=smesh.PYTHON");
1011 else if ( hypType == "PythonSplit1D" ) {
1012 hyp->SetConvMethodAndType( "PythonSplit1D", "Python_1D");
1013 hyp->AddArgMethod( "SetNumberOfSegments");
1014 hyp->AddArgMethod( "SetPythonLog10RatioFunction");
1016 // MEFISTO_2D ----------
1017 else if ( hypType == "MEFISTO_2D" ) { // MEFISTO_2D
1018 algo->SetConvMethodAndType( "Triangle", hypType.ToCString());
1020 else if ( hypType == "MaxElementArea" ) {
1021 hyp->SetConvMethodAndType( "MaxElementArea", "MEFISTO_2D");
1022 hyp->SetConvMethodAndType( "MaxElementArea", "NETGEN_2D_ONLY");
1023 hyp->AddArgMethod( "SetMaxElementArea");
1025 else if ( hypType == "LengthFromEdges" ) {
1026 hyp->SetConvMethodAndType( "LengthFromEdges", "MEFISTO_2D");
1027 hyp->SetConvMethodAndType( "LengthFromEdges", "NETGEN_2D_ONLY");
1029 // Quadrangle_2D ----------
1030 else if ( hypType == "Quadrangle_2D" ) {
1031 algo->SetConvMethodAndType( "Quadrangle" , hypType.ToCString());
1033 else if ( hypType == "QuadranglePreference" ) {
1034 hyp->SetConvMethodAndType( "QuadranglePreference", "Quadrangle_2D");
1035 hyp->SetConvMethodAndType( "QuadranglePreference", "NETGEN_2D_ONLY");
1037 else if ( hypType == "TrianglePreference" ) {
1038 hyp->SetConvMethodAndType( "TrianglePreference", "Quadrangle_2D");
1040 // NETGEN ----------
1041 // else if ( hypType == "NETGEN_2D") { // 1D-2D
1042 // algo->SetConvMethodAndType( "Triangle" , hypType.ToCString());
1043 // algo->myArgs.Append( "algo=smesh.NETGEN" );
1045 else if ( hypType == "NETGEN_2D_ONLY") { // 2D
1046 algo->SetConvMethodAndType( "Triangle" , hypType.ToCString());
1047 algo->myArgs.Append( "algo=smesh.NETGEN_2D" );
1049 else if ( hypType == "NETGEN_3D") { // 3D
1050 algo->SetConvMethodAndType( "Tetrahedron" , hypType.ToCString());
1051 algo->myArgs.Append( "algo=smesh.NETGEN" );
1053 else if ( hypType == "MaxElementVolume") {
1054 hyp->SetConvMethodAndType( "MaxElementVolume", "NETGEN_3D");
1055 hyp->AddArgMethod( "SetMaxElementVolume" );
1057 // GHS3D_3D ----------
1058 else if ( hypType == "GHS3D_3D" ) {
1059 algo->SetConvMethodAndType( "Tetrahedron", hypType.ToCString());
1060 algo->myArgs.Append( "algo=smesh.GHS3D" );
1062 // Hexa_3D ---------
1063 else if ( hypType == "Hexa_3D" ) {
1064 algo->SetConvMethodAndType( "Hexahedron", hypType.ToCString());
1066 // Repetitive Projection_1D ---------
1067 else if ( hypType == "Projection_1D" ) {
1068 algo->SetConvMethodAndType( "Projection1D", hypType.ToCString());
1070 else if ( hypType == "ProjectionSource1D" ) {
1071 hyp->SetConvMethodAndType( "SourceEdge", "Projection_1D");
1072 hyp->AddArgMethod( "SetSourceEdge");
1073 hyp->AddArgMethod( "SetSourceMesh");
1074 // 2 args of SetVertexAssociation() will become the 3-th and 4-th args of hyp creation command
1075 hyp->AddArgMethod( "SetVertexAssociation", 2 );
1077 // Projection_2D ---------
1078 else if ( hypType == "Projection_2D" ) {
1079 algo->SetConvMethodAndType( "Projection2D", hypType.ToCString());
1081 else if ( hypType == "ProjectionSource2D" ) {
1082 hyp->SetConvMethodAndType( "SourceFace", "Projection_2D");
1083 hyp->AddArgMethod( "SetSourceFace");
1084 hyp->AddArgMethod( "SetSourceMesh");
1085 hyp->AddArgMethod( "SetVertexAssociation", 4 );
1087 // Projection_3D ---------
1088 else if ( hypType == "Projection_3D" ) {
1089 algo->SetConvMethodAndType( "Projection3D", hypType.ToCString());
1091 else if ( hypType == "ProjectionSource3D" ) {
1092 hyp->SetConvMethodAndType( "SourceShape3D", "Projection_3D");
1093 hyp->AddArgMethod( "SetSource3DShape");
1094 hyp->AddArgMethod( "SetSourceMesh");
1095 hyp->AddArgMethod( "SetVertexAssociation", 4 );
1097 // Prism_3D ---------
1098 else if ( hypType == "Prism_3D" ) {
1099 algo->SetConvMethodAndType( "Prism", hypType.ToCString());
1101 // RadialPrism_3D ---------
1102 else if ( hypType == "RadialPrism_3D" ) {
1103 algo->SetConvMethodAndType( "Prism", hypType.ToCString());
1105 else if ( hypType == "NumberOfLayers" ) {
1106 hyp->SetConvMethodAndType( "NumberOfLayers", "RadialPrism_3D");
1107 hyp->AddArgMethod( "SetNumberOfLayers" );
1109 else if ( hypType == "LayerDistribution" ) {
1110 hyp = new _pyLayerDistributionHypo( theCreationCmd );
1111 hyp->SetConvMethodAndType( "LayerDistribution", "RadialPrism_3D");
1114 if ( algo->IsValid() ) {
1120 //================================================================================
1122 * \brief Convert the command adding a hypothesis to mesh into a smesh command
1123 * \param theCmd - The command like mesh.AddHypothesis( geom, hypo )
1124 * \param theAlgo - The algo that can create this hypo
1125 * \retval bool - false if the command cant be converted
1127 //================================================================================
1129 bool _pyHypothesis::Addition2Creation( const Handle(_pyCommand)& theCmd,
1130 const _pyID& theMesh)
1132 ASSERT(( theCmd->GetMethod() == "AddHypothesis" ));
1134 if ( !IsWrappable( theMesh ))
1137 myGeom = theCmd->GetArg( 1 );
1139 Handle(_pyHypothesis) algo;
1141 // find algo created on myGeom in theMesh
1142 algo = theGen->FindAlgo( myGeom, theMesh, this );
1143 if ( algo.IsNull() )
1145 algo->GetCreationCmd()->AddDependantCmd( theCmd );
1149 // mesh.AddHypothesis(geom,hyp) --> hyp = <theMesh or algo>.myCreationMethod(args)
1150 theCmd->SetResultValue( GetID() );
1151 theCmd->SetObject( IsAlgo() ? theMesh : algo->GetID());
1152 theCmd->SetMethod( IsAlgo() ? GetAlgoCreationMethod() : GetCreationMethod( algo->GetAlgoType() ));
1154 theCmd->RemoveArgs();
1155 for ( int i = 1; i <= myArgs.Length(); ++i ) {
1156 if ( !myArgs( i ).IsEmpty() )
1157 theCmd->SetArg( i, myArgs( i ));
1159 theCmd->SetArg( i, "[]");
1161 // set a new creation command
1162 GetCreationCmd()->Clear();
1163 SetCreationCmd( theCmd );
1165 // clear commands setting arg values
1166 list < Handle(_pyCommand) >::iterator argCmd = myArgCommands.begin();
1167 for ( ; argCmd != myArgCommands.end(); ++argCmd )
1170 // set unknown arg commands after hypo creation
1171 Handle(_pyCommand) afterCmd = myIsWrapped ? theCmd : GetCreationCmd();
1172 list<Handle(_pyCommand)>::iterator cmd = myUnknownCommands.begin();
1173 for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
1174 afterCmd->AddDependantCmd( *cmd );
1180 //================================================================================
1182 * \brief Remember hypothesis parameter values
1183 * \param theCommand - The called hypothesis method
1185 //================================================================================
1187 void _pyHypothesis::Process( const Handle(_pyCommand)& theCommand)
1189 ASSERT( !myIsAlgo );
1192 for ( int i = 1; i <= myArgMethods.Length(); ++i ) {
1193 if ( myArgMethods( i ) == theCommand->GetMethod() ) {
1194 while ( myArgs.Length() < nbArgs + myNbArgsByMethod( i ))
1195 myArgs.Append( "[]" );
1196 for ( int iArg = 1; iArg <= myNbArgsByMethod( i ); ++iArg )
1197 myArgs( nbArgs + iArg ) = theCommand->GetArg( iArg ); // arg value
1198 myArgCommands.push_back( theCommand );
1201 nbArgs += myNbArgsByMethod( i );
1203 myUnknownCommands.push_back( theCommand );
1206 //================================================================================
1208 * \brief Finish conversion
1210 //================================================================================
1212 void _pyHypothesis::Flush()
1214 if ( IsWrapped() ) {
1217 list < Handle(_pyCommand) >::iterator cmd = myArgCommands.begin();
1218 for ( ; cmd != myArgCommands.end(); ++cmd ) {
1219 // Add access to a wrapped mesh
1220 theGen->AddMeshAccessorMethod( *cmd );
1221 // Add access to a wrapped algorithm
1222 theGen->AddAlgoAccessorMethod( *cmd );
1224 cmd = myUnknownCommands.begin();
1225 for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
1226 // Add access to a wrapped mesh
1227 theGen->AddMeshAccessorMethod( *cmd );
1228 // Add access to a wrapped algorithm
1229 theGen->AddAlgoAccessorMethod( *cmd );
1232 // forget previous hypothesis modifications
1233 myArgCommands.clear();
1234 myUnknownCommands.clear();
1237 //================================================================================
1239 * \brief clear creation, arg and unkown commands
1241 //================================================================================
1243 void _pyHypothesis::ClearAllCommands()
1245 GetCreationCmd()->Clear();
1246 list<Handle(_pyCommand)>::iterator cmd = myArgCommands.begin();
1247 for ( ; cmd != myArgCommands.end(); ++cmd )
1249 cmd = myUnknownCommands.begin();
1250 for ( ; cmd != myUnknownCommands.end(); ++cmd )
1254 //================================================================================
1256 * \brief Remember hypothesis parameter values
1257 * \param theCommand - The called hypothesis method
1259 //================================================================================
1261 void _pyComplexParamHypo::Process( const Handle(_pyCommand)& theCommand)
1263 // ex: hyp.SetLength(start, 1)
1264 // hyp.SetLength(end, 0)
1265 ASSERT(( theCommand->GetMethod() == "SetLength" ));
1266 ASSERT(( theCommand->GetArg( 2 ).IsIntegerValue() ));
1267 int i = 2 - theCommand->GetArg( 2 ).IntegerValue();
1268 while ( myArgs.Length() < i )
1269 myArgs.Append( "[]" );
1270 myArgs( i ) = theCommand->GetArg( 1 ); // arg value
1271 myArgCommands.push_back( theCommand );
1274 //================================================================================
1276 * \brief Convert methods of 1D hypotheses to my own methods
1277 * \param theCommand - The called hypothesis method
1279 //================================================================================
1281 void _pyLayerDistributionHypo::Process( const Handle(_pyCommand)& theCommand)
1283 if ( theCommand->GetMethod() != "SetLayerDistribution" )
1286 _pyID newName; // name for 1D hyp = "HypType" + "_Distribution"
1288 const _pyID& hyp1dID = theCommand->GetArg( 1 );
1289 Handle(_pyHypothesis) hyp1d = theGen->FindHyp( hyp1dID );
1290 if ( hyp1d.IsNull() ) // apparently hypId changed at study restoration
1292 else if ( !my1dHyp.IsNull() && hyp1dID != my1dHyp->GetID() ) {
1293 // 1D hypo is already set, so distribution changes and the old
1294 // 1D hypo is thrown away
1295 my1dHyp->ClearAllCommands();
1298 if ( my1dHyp.IsNull() )
1299 return; // something wrong :(
1301 // make a new name for 1D hyp = "HypType" + "_Distribution"
1302 if ( my1dHyp->GetCreationCmd()->GetMethod() == "CreateHypothesis" ) {
1303 // not yet converted creation cmd
1304 TCollection_AsciiString hypTypeQuoted = my1dHyp->GetCreationCmd()->GetArg(1);
1305 TCollection_AsciiString hypType = hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
1306 newName = hypType + "_Distribution";
1307 my1dHyp->GetCreationCmd()->SetResultValue( newName );
1310 // already converted creation cmd
1311 newName = my1dHyp->GetCreationCmd()->GetResultValue();
1314 // as creation of 1D hyp was written later then it's edition,
1315 // we need to find all it's edition calls and process them
1316 list< Handle(_pyCommand) >& cmds = theGen->GetCommands();
1317 list< Handle(_pyCommand) >::iterator cmdIt = cmds.begin();
1318 for ( ; cmdIt != cmds.end(); ++cmdIt ) {
1319 const _pyID& objID = (*cmdIt)->GetObject();
1320 if ( objID == hyp1dID ) {
1321 my1dHyp->Process( *cmdIt );
1322 my1dHyp->GetCreationCmd()->AddDependantCmd( *cmdIt );
1323 ( *cmdIt )->SetObject( newName );
1326 if ( !myArgCommands.empty() )
1327 myArgCommands.front()->Clear();
1328 theCommand->SetArg( 1, newName );
1329 myArgCommands.push_back( theCommand );
1330 // copy hyp1d's creation method and args
1331 // myCreationMethod = hyp1d->GetCreationMethod();
1332 // myArgs = hyp1d->GetArgs();
1333 // // make them cleared at conversion
1334 // myArgCommands = hyp1d->GetArgCommands();
1336 // // to be cleared at convertion only
1337 // myArgCommands.push_back( theCommand );
1340 //================================================================================
1343 * \param theAdditionCmd -
1347 //================================================================================
1349 bool _pyLayerDistributionHypo::Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
1350 const _pyID& theMesh)
1352 myIsWrapped = false;
1354 if ( my1dHyp.IsNull() )
1357 // set "SetLayerDistribution()" after addition cmd
1358 theAdditionCmd->AddDependantCmd( myArgCommands.front() );
1360 _pyID geom = theAdditionCmd->GetArg( 1 );
1362 my1dHyp->SetMesh( theMesh );
1363 if ( !my1dHyp->Addition2Creation( theAdditionCmd, theMesh ))
1366 // clear "SetLayerDistribution()" cmd
1367 myArgCommands.front()->Clear();
1369 // Convert my creation => me = RadialPrismAlgo.Get3DHypothesis()
1371 // find RadialPrism algo created on <geom> for theMesh
1372 Handle(_pyHypothesis) algo = theGen->FindAlgo( geom, theMesh, this );
1373 if ( !algo.IsNull() ) {
1374 GetCreationCmd()->SetObject( algo->GetID() );
1375 GetCreationCmd()->SetMethod( "Get3DHypothesis" );
1376 GetCreationCmd()->RemoveArgs();
1377 theAdditionCmd->AddDependantCmd( GetCreationCmd() );
1383 //================================================================================
1387 //================================================================================
1389 void _pyLayerDistributionHypo::Flush()
1391 //my1dHyp.Nullify();
1392 //_pyHypothesis::Flush();
1395 //================================================================================
1397 * \brief additionally to Addition2Creation, clears SetDistrType() command
1398 * \param theCmd - AddHypothesis() command
1399 * \param theMesh - mesh to which a hypothesis is added
1400 * \retval bool - convertion result
1402 //================================================================================
1404 bool _pyNumberOfSegmentsHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
1405 const _pyID& theMesh)
1407 if ( IsWrappable( theMesh ) && myArgs.Length() > 1 ) {
1408 // scale factor (2-nd arg) is provided: clear SetDistrType(1) command
1409 bool scaleDistrType = false;
1410 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnknownCommands.rbegin();
1411 for ( ; cmd != myUnknownCommands.rend(); ++cmd ) {
1412 if ( (*cmd)->GetMethod() == "SetDistrType" ) {
1413 if ( (*cmd)->GetArg( 1 ) == "1" ) {
1414 scaleDistrType = true;
1417 else if ( !scaleDistrType ) {
1418 // distribution type changed: remove scale factor from args
1419 myArgs.Remove( 2, myArgs.Length() );
1425 return _pyHypothesis::Addition2Creation( theCmd, theMesh );
1428 //================================================================================
1430 * \brief remove repeated commands defining distribution
1432 //================================================================================
1434 void _pyNumberOfSegmentsHyp::Flush()
1436 // find number of the last SetDistrType() command
1437 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnknownCommands.rbegin();
1438 int distrTypeNb = 0;
1439 for ( ; !distrTypeNb && cmd != myUnknownCommands.rend(); ++cmd )
1440 if ( (*cmd)->GetMethod() == "SetDistrType" )
1441 distrTypeNb = (*cmd)->GetOrderNb();
1443 // clear commands before the last SetDistrType()
1444 list<Handle(_pyCommand)> * cmds[2] = { &myArgCommands, &myUnknownCommands };
1445 for ( int i = 0; i < 2; ++i ) {
1446 set<TCollection_AsciiString> uniqueMethods;
1447 list<Handle(_pyCommand)> & cmdList = *cmds[i];
1448 for ( cmd = cmdList.rbegin(); cmd != cmdList.rend(); ++cmd )
1450 bool clear = ( (*cmd)->GetOrderNb() < distrTypeNb );
1451 const TCollection_AsciiString& method = (*cmd)->GetMethod();
1452 if ( !clear || method == "SetNumberOfSegments" ) {
1453 bool isNewInSet = uniqueMethods.insert( method ).second;
1454 clear = !isNewInSet;
1463 //================================================================================
1465 * \brief Convert the command adding "SegmentLengthAroundVertex" to mesh
1466 * into regular1D.LengthNearVertex( length, vertex )
1467 * \param theCmd - The command like mesh.AddHypothesis( vertex, SegmentLengthAroundVertex )
1468 * \param theMesh - The mesh needing this hypo
1469 * \retval bool - false if the command cant be converted
1471 //================================================================================
1473 bool _pySegmentLengthAroundVertexHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
1474 const _pyID& theMeshID)
1476 if ( IsWrappable( theMeshID )) {
1478 _pyID vertex = theCmd->GetArg( 1 );
1480 // the problem here is that segment algo will not be found
1481 // by pyHypothesis::Addition2Creation() for <vertex>, so we try to find
1482 // geometry where segment algorithm is assigned
1483 Handle(_pyHypothesis) algo;
1484 _pyID geom = vertex;
1485 while ( algo.IsNull() && !geom.IsEmpty()) {
1486 // try to find geom as a father of <vertex>
1487 geom = FatherID( geom );
1488 algo = theGen->FindAlgo( geom, theMeshID, this );
1490 if ( algo.IsNull() )
1491 return false; // also possible to find geom as brother of veretex...
1492 // set geom instead of vertex
1493 theCmd->SetArg( 1, geom );
1495 // set vertex as a second arg
1496 if ( myArgs.Length() < 1) myArgs.Append( "1" ); // :(
1497 myArgs.Append( vertex );
1499 // mesh.AddHypothesis(vertex, SegmentLengthAroundVertex) -->
1500 // theMeshID.LengthNearVertex( length, vertex )
1501 return _pyHypothesis::Addition2Creation( theCmd, theMeshID );
1506 //================================================================================
1508 * \brief _pyAlgorithm constructor
1509 * \param theCreationCmd - The command like "algo = smeshgen.CreateHypothesis(type,lib)"
1511 //================================================================================
1513 _pyAlgorithm::_pyAlgorithm(const Handle(_pyCommand)& theCreationCmd)
1514 : _pyHypothesis( theCreationCmd )
1519 //================================================================================
1521 * \brief Convert the command adding an algorithm to mesh
1522 * \param theCmd - The command like mesh.AddHypothesis( geom, algo )
1523 * \param theMesh - The mesh needing this algo
1524 * \retval bool - false if the command cant be converted
1526 //================================================================================
1528 bool _pyAlgorithm::Addition2Creation( const Handle(_pyCommand)& theCmd,
1529 const _pyID& theMeshID)
1531 // mesh.AddHypothesis(geom,algo) --> theMeshID.myCreationMethod()
1532 if ( _pyHypothesis::Addition2Creation( theCmd, theMeshID )) {
1533 theGen->SetAccessorMethod( GetID(), "GetAlgorithm()" );
1539 //================================================================================
1541 * \brief Return starting position of a part of python command
1542 * \param thePartIndex - The index of command part
1543 * \retval int - Part position
1545 //================================================================================
1547 int _pyCommand::GetBegPos( int thePartIndex )
1551 if ( myBegPos.Length() < thePartIndex )
1553 return myBegPos( thePartIndex );
1556 //================================================================================
1558 * \brief Store starting position of a part of python command
1559 * \param thePartIndex - The index of command part
1560 * \param thePosition - Part position
1562 //================================================================================
1564 void _pyCommand::SetBegPos( int thePartIndex, int thePosition )
1566 while ( myBegPos.Length() < thePartIndex )
1567 myBegPos.Append( UNKNOWN );
1568 myBegPos( thePartIndex ) = thePosition;
1571 //================================================================================
1573 * \brief Returns whitespace symbols at the line beginning
1574 * \retval TCollection_AsciiString - result
1576 //================================================================================
1578 TCollection_AsciiString _pyCommand::GetIndentation()
1581 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
1582 GetWord( myString, end, true );
1584 end = GetBegPos( RESULT_IND );
1585 return myString.SubString( 1, end - 1 );
1588 //================================================================================
1590 * \brief Return substring of python command looking like ResultValue = Obj.Meth()
1591 * \retval const TCollection_AsciiString & - ResultValue substring
1593 //================================================================================
1595 const TCollection_AsciiString & _pyCommand::GetResultValue()
1597 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
1599 int begPos = myString.Location( "=", 1, Length() );
1601 myRes = GetWord( myString, begPos, false );
1604 SetBegPos( RESULT_IND, begPos );
1609 //================================================================================
1611 * \brief Return number of python command result value ResultValue = Obj.Meth()
1614 //================================================================================
1616 const int _pyCommand::GetNbResultValues()
1620 int endPos = myString.Location( "=", 1, Length() );
1621 TCollection_AsciiString str = "";
1622 while ( begPos < endPos) {
1623 str = GetWord( myString, begPos, true );
1624 begPos = begPos+ str.Length();
1631 //================================================================================
1633 * \brief Return substring of python command looking like
1634 * ResultValue1 , ResultValue1,... = Obj.Meth() with res index
1635 * \retval const TCollection_AsciiString & - ResultValue with res index substring
1637 //================================================================================
1638 const TCollection_AsciiString & _pyCommand::GetResultValue(int res)
1642 int endPos = myString.Location( "=", 1, Length() );
1643 while ( begPos < endPos) {
1644 myRes = GetWord( myString, begPos, true );
1645 begPos = begPos + myRes.Length();
1648 myRes.RemoveAll('[');myRes.RemoveAll(']');
1654 return theEmptyString;
1657 //================================================================================
1659 * \brief Return substring of python command looking like ResVal = Object.Meth()
1660 * \retval const TCollection_AsciiString & - Object substring
1662 //================================================================================
1664 const TCollection_AsciiString & _pyCommand::GetObject()
1666 if ( GetBegPos( OBJECT_IND ) == UNKNOWN )
1669 int begPos = GetBegPos( RESULT_IND ) + myRes.Length();
1671 begPos = myString.Location( "=", 1, Length() ) + 1;
1673 myObj = GetWord( myString, begPos, true );
1674 SetBegPos( OBJECT_IND, begPos );
1680 //================================================================================
1682 * \brief Return substring of python command looking like ResVal = Obj.Method()
1683 * \retval const TCollection_AsciiString & - Method substring
1685 //================================================================================
1687 const TCollection_AsciiString & _pyCommand::GetMethod()
1689 if ( GetBegPos( METHOD_IND ) == UNKNOWN )
1692 int begPos = GetBegPos( OBJECT_IND ) + myObj.Length();
1693 bool forward = true;
1695 begPos = myString.Location( "(", 1, Length() ) - 1;
1699 myMeth = GetWord( myString, begPos, forward );
1700 SetBegPos( METHOD_IND, begPos );
1706 //================================================================================
1708 * \brief Return substring of python command looking like ResVal = Obj.Meth(Arg1,...)
1709 * \retval const TCollection_AsciiString & - Arg<index> substring
1711 //================================================================================
1713 const TCollection_AsciiString & _pyCommand::GetArg( int index )
1715 if ( GetBegPos( ARG1_IND ) == UNKNOWN )
1718 int begPos = GetBegPos( METHOD_IND ) + myMeth.Length();
1720 begPos = myString.Location( "(", 1, Length() ) + 1;
1722 int i = 0, prevLen = 0;
1723 while ( begPos != EMPTY ) {
1725 // check if we are looking at the closing parenthesis
1726 while ( begPos <= Length() && isspace( myString.Value( begPos )))
1728 if ( begPos > Length() || myString.Value( begPos ) == ')' )
1730 myArgs.Append( GetWord( myString, begPos, true, true ));
1731 SetBegPos( ARG1_IND + i, begPos );
1732 prevLen = myArgs.Last().Length();
1734 myArgs.Remove( myArgs.Length() ); // no more args
1738 if ( myArgs.Length() < index )
1739 return theEmptyString;
1740 return myArgs( index );
1743 //================================================================================
1745 * \brief Check if char is a word part
1746 * \param c - The character to check
1747 * \retval bool - The check result
1749 //================================================================================
1751 static inline bool isWord(const char c, const bool dotIsWord)
1754 !isspace(c) && c != ',' && c != '=' && c != ')' && c != '(' && ( dotIsWord || c != '.');
1757 //================================================================================
1759 * \brief Looks for a word in the string and returns word's beginning
1760 * \param theString - The input string
1761 * \param theStartPos - The position to start the search, returning word's beginning
1762 * \param theForward - The search direction
1763 * \retval TCollection_AsciiString - The found word
1765 //================================================================================
1767 TCollection_AsciiString _pyCommand::GetWord( const TCollection_AsciiString & theString,
1769 const bool theForward,
1770 const bool dotIsWord )
1772 int beg = theStartPos, end = theStartPos;
1773 theStartPos = EMPTY;
1774 if ( beg < 1 || beg > theString.Length() )
1775 return theEmptyString;
1777 if ( theForward ) { // search forward
1779 while ( beg <= theString.Length() && !isWord( theString.Value( beg ), dotIsWord))
1781 if ( beg > theString.Length() )
1782 return theEmptyString; // no word found
1785 while ( end <= theString.Length() && isWord( theString.Value( end ), dotIsWord))
1789 else { // search backward
1791 while ( end > 0 && !isWord( theString.Value( end ), dotIsWord))
1794 return theEmptyString; // no word found
1796 while ( beg > 0 && isWord( theString.Value( beg ), dotIsWord))
1801 //cout << theString << " ---- " << beg << " - " << end << endl;
1802 return theString.SubString( beg, end );
1805 //================================================================================
1807 * \brief Look for position where not space char is
1808 * \param theString - The string
1809 * \param thePos - The position to search from and which returns result
1810 * \retval bool - false if there are only space after thePos in theString
1814 //================================================================================
1816 bool _pyCommand::SkipSpaces( const TCollection_AsciiString & theString, int & thePos )
1818 if ( thePos < 1 || thePos > theString.Length() )
1821 while ( thePos <= theString.Length() && isspace( theString.Value( thePos )))
1824 return thePos <= theString.Length();
1827 //================================================================================
1829 * \brief Modify a part of the command
1830 * \param thePartIndex - The index of the part
1831 * \param thePart - The new part string
1832 * \param theOldPart - The old part
1834 //================================================================================
1836 void _pyCommand::SetPart(int thePartIndex, const TCollection_AsciiString& thePart,
1837 TCollection_AsciiString& theOldPart)
1839 int pos = GetBegPos( thePartIndex );
1840 if ( pos <= Length() && theOldPart != thePart)
1842 TCollection_AsciiString seperator;
1844 pos = GetBegPos( thePartIndex + 1 );
1845 if ( pos < 1 ) return;
1846 switch ( thePartIndex ) {
1847 case RESULT_IND: seperator = " = "; break;
1848 case OBJECT_IND: seperator = "."; break;
1849 case METHOD_IND: seperator = "()"; break;
1853 myString.Remove( pos, theOldPart.Length() );
1854 if ( !seperator.IsEmpty() )
1855 myString.Insert( pos , seperator );
1856 myString.Insert( pos, thePart );
1857 // update starting positions of the following parts
1858 int posDelta = thePart.Length() + seperator.Length() - theOldPart.Length();
1859 for ( int i = thePartIndex + 1; i <= myBegPos.Length(); ++i ) {
1860 if ( myBegPos( i ) > 0 )
1861 myBegPos( i ) += posDelta;
1863 theOldPart = thePart;
1867 //================================================================================
1869 * \brief Set agrument
1870 * \param index - The argument index, it counts from 1
1871 * \param theArg - The argument string
1873 //================================================================================
1875 void _pyCommand::SetArg( int index, const TCollection_AsciiString& theArg)
1878 int argInd = ARG1_IND + index - 1;
1879 int pos = GetBegPos( argInd );
1880 if ( pos < 1 ) // no index-th arg exist, append inexistent args
1882 // find a closing parenthesis
1883 if ( int lastArgInd = GetNbArgs() ) {
1884 pos = GetBegPos( ARG1_IND + lastArgInd - 1 ) + GetArg( lastArgInd ).Length();
1885 while ( pos > 0 && pos <= Length() && myString.Value( pos ) != ')' )
1890 while ( pos > 0 && myString.Value( pos ) != ')' )
1893 if ( pos < 1 || myString.Value( pos ) != ')' ) { // no parentheses at all
1897 while ( myArgs.Length() < index ) {
1898 if ( myArgs.Length() )
1899 myString.Insert( pos++, "," );
1900 myArgs.Append("None");
1901 myString.Insert( pos, myArgs.Last() );
1902 SetBegPos( ARG1_IND + myArgs.Length() - 1, pos );
1903 pos += myArgs.Last().Length();
1906 SetPart( argInd, theArg, myArgs( index ));
1909 //================================================================================
1911 * \brief Empty arg list
1913 //================================================================================
1915 void _pyCommand::RemoveArgs()
1917 if ( int pos = myString.Location( '(', 1, Length() ))
1918 myString.Trunc( pos );
1921 if ( myBegPos.Length() >= ARG1_IND )
1922 myBegPos.Remove( ARG1_IND, myBegPos.Length() );
1925 //================================================================================
1927 * \brief Set dependent commands after this one
1929 //================================================================================
1931 bool _pyCommand::SetDependentCmdsAfter() const
1933 bool orderChanged = false;
1934 list< Handle(_pyCommand)>::const_reverse_iterator cmd = myDependentCmds.rbegin();
1935 for ( ; cmd != myDependentCmds.rend(); ++cmd ) {
1936 if ( (*cmd)->GetOrderNb() < GetOrderNb() ) {
1937 orderChanged = true;
1938 theGen->SetCommandAfter( *cmd, this );
1939 (*cmd)->SetDependentCmdsAfter();
1942 return orderChanged;
1944 //================================================================================
1946 * \brief Insert accessor method after theObjectID
1947 * \param theObjectID - id of the accessed object
1948 * \param theAcsMethod - name of the method giving access to the object
1949 * \retval bool - false if theObjectID is not found in the command string
1951 //================================================================================
1953 bool _pyCommand::AddAccessorMethod( _pyID theObjectID, const char* theAcsMethod )
1955 if ( !theAcsMethod )
1957 // start object search from the object, i.e. ignore result
1959 int beg = GetBegPos( OBJECT_IND );
1960 if ( beg < 1 || beg > Length() )
1963 while (( beg = myString.Location( theObjectID, beg, Length() )))
1965 // check that theObjectID is not just a part of a longer ID
1966 int afterEnd = beg + theObjectID.Length();
1967 Standard_Character c = myString.Value( afterEnd );
1968 if ( !isalnum( c ) && c != ':' ) {
1969 // check if accessor method already present
1971 myString.Location( (char*) theAcsMethod, afterEnd, Length() ) != afterEnd+1) {
1973 int oldLen = Length();
1974 myString.Insert( afterEnd, (char*) theAcsMethod );
1975 myString.Insert( afterEnd, "." );
1976 // update starting positions of the parts following the modified one
1977 int posDelta = Length() - oldLen;
1978 for ( int i = 1; i <= myBegPos.Length(); ++i ) {
1979 if ( myBegPos( i ) > afterEnd )
1980 myBegPos( i ) += posDelta;
1985 beg = afterEnd; // is a part - next search
1990 //================================================================================
1992 * \brief Return method name giving access to an interaface object wrapped by python class
1993 * \retval const char* - method name
1995 //================================================================================
1997 const char* _pyObject::AccessorMethod() const
2001 //================================================================================
2003 * \brief Return ID of a father
2005 //================================================================================
2007 _pyID _pyObject::FatherID(const _pyID & childID)
2009 int colPos = childID.SearchFromEnd(':');
2011 return childID.SubString( 1, colPos-1 );