1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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
22 // SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
24 // File : SMESH_2smeshpy.cxx
25 // Created : Fri Nov 18 13:20:10 2005
26 // Author : Edward AGAPOV (eap)
28 #include "SMESH_2smeshpy.hxx"
30 #include "utilities.h"
31 #include "SMESH_PythonDump.hxx"
32 #include "SMESH_NoteBook.hxx"
33 #include "Resource_DataMapOfAsciiStringAsciiString.hxx"
35 #include "SMESH_Gen_i.hxx"
36 /* SALOME headers that include CORBA headers that include windows.h
37 * that defines GetObject symbol as GetObjectA should stand before SALOME headers
38 * that declare methods named GetObject - to apply the same rules of GetObject renaming
39 * and thus to avoid mess with GetObject symbol on Windows */
41 IMPLEMENT_STANDARD_HANDLE (_pyObject ,Standard_Transient);
42 IMPLEMENT_STANDARD_HANDLE (_pyCommand ,Standard_Transient);
43 IMPLEMENT_STANDARD_HANDLE (_pyGen ,_pyObject);
44 IMPLEMENT_STANDARD_HANDLE (_pyMesh ,_pyObject);
45 IMPLEMENT_STANDARD_HANDLE (_pySubMesh ,_pyObject);
46 IMPLEMENT_STANDARD_HANDLE (_pyMeshEditor ,_pyObject);
47 IMPLEMENT_STANDARD_HANDLE (_pyHypothesis ,_pyObject);
48 IMPLEMENT_STANDARD_HANDLE (_pySelfEraser ,_pyObject);
49 IMPLEMENT_STANDARD_HANDLE (_pyAlgorithm ,_pyHypothesis);
50 IMPLEMENT_STANDARD_HANDLE (_pyComplexParamHypo,_pyHypothesis);
51 IMPLEMENT_STANDARD_HANDLE (_pyNumberOfSegmentsHyp,_pyHypothesis);
53 IMPLEMENT_STANDARD_RTTIEXT(_pyObject ,Standard_Transient);
54 IMPLEMENT_STANDARD_RTTIEXT(_pyCommand ,Standard_Transient);
55 IMPLEMENT_STANDARD_RTTIEXT(_pyGen ,_pyObject);
56 IMPLEMENT_STANDARD_RTTIEXT(_pyMesh ,_pyObject);
57 IMPLEMENT_STANDARD_RTTIEXT(_pySubMesh ,_pyObject);
58 IMPLEMENT_STANDARD_RTTIEXT(_pyMeshEditor ,_pyObject);
59 IMPLEMENT_STANDARD_RTTIEXT(_pyHypothesis ,_pyObject);
60 IMPLEMENT_STANDARD_RTTIEXT(_pySelfEraser ,_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,
124 Resource_DataMapOfAsciiStringAsciiString& theObjectNames)
126 theGen = new _pyGen( theEntry2AccessorMethod, theObjectNames );
128 // split theScript into separate commands
130 SMESH_NoteBook * aNoteBook = new SMESH_NoteBook();
132 int from = 1, end = theScript.Length(), to;
133 while ( from < end && ( to = theScript.Location( "\n", from, end )))
136 // cut out and store a command
137 aNoteBook->AddCommand( theScript.SubString( from, to - 1 ));
141 aNoteBook->ReplaceVariables();
143 TCollection_AsciiString aNoteScript = aNoteBook->GetResultScript();
147 // split theScript into separate commands
148 from = 1, end = aNoteScript.Length();
149 while ( from < end && ( to = aNoteScript.Location( "\n", from, end )))
152 // cut out and store a command
153 theGen->AddCommand( aNoteScript.SubString( from, to - 1 ));
159 #ifdef DUMP_CONVERSION
160 MESSAGE_BEGIN ( std::endl << " ######## RESULT ######## " << std::endl<< std::endl );
163 // reorder commands after conversion
164 list< Handle(_pyCommand) >::iterator cmd;
167 orderChanges = false;
168 for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
169 if ( (*cmd)->SetDependentCmdsAfter() )
171 } while ( orderChanges );
173 // concat commands back into a script
174 TCollection_AsciiString aScript;
175 for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
177 #ifdef DUMP_CONVERSION
178 MESSAGE_ADD ( "## COM " << (*cmd)->GetOrderNb() << ": "<< (*cmd)->GetString() << std::endl );
180 if ( !(*cmd)->IsEmpty() ) {
182 aScript += (*cmd)->GetString();
192 //================================================================================
194 * \brief _pyGen constructor
196 //================================================================================
198 _pyGen::_pyGen(Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod,
199 Resource_DataMapOfAsciiStringAsciiString& theObjectNames)
200 : _pyObject( new _pyCommand( TPythonDump::SMESHGenName(), 0 )),
201 myID2AccessorMethod( theEntry2AccessorMethod ),
202 myObjectNames( theObjectNames )
205 // make that GetID() to return TPythonDump::SMESHGenName()
206 GetCreationCmd()->GetString() += "=";
209 //================================================================================
211 * \brief name of SMESH_Gen in smesh.py
213 //================================================================================
215 const char* _pyGen::AccessorMethod() const
217 return SMESH_2smeshpy::GenName();
220 //================================================================================
222 * \brief Convert a command using a specific converter
223 * \param theCommand - the command to convert
225 //================================================================================
227 Handle(_pyCommand) _pyGen::AddCommand( const TCollection_AsciiString& theCommand)
229 // store theCommand in the sequence
230 myCommands.push_back( new _pyCommand( theCommand, ++myNbCommands ));
232 Handle(_pyCommand) aCommand = myCommands.back();
233 #ifdef DUMP_CONVERSION
234 MESSAGE ( "## COM " << myNbCommands << ": "<< aCommand->GetString() );
237 _pyID objID = aCommand->GetObject();
239 if ( objID.IsEmpty() )
243 if ( objID == this->GetID() || objID == SMESH_2smeshpy::GenName()) {
244 this->Process( aCommand );
248 // SMESH_Mesh method?
249 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( objID );
250 if ( id_mesh != myMeshes.end() ) {
251 // check for mesh editor object
252 if ( aCommand->GetMethod() == "GetMeshEditor" ) { // MeshEditor creation
253 _pyID editorID = aCommand->GetResultValue();
254 Handle(_pyMeshEditor) editor = new _pyMeshEditor( aCommand );
255 myMeshEditors.insert( make_pair( editorID, editor ));
258 // check for SubMesh objects
259 else if ( aCommand->GetMethod() == "GetSubMesh" ) { // SubMesh creation
260 _pyID subMeshID = aCommand->GetResultValue();
261 Handle(_pySubMesh) subMesh = new _pySubMesh( aCommand );
262 myObjects.insert( make_pair( subMeshID, subMesh ));
264 id_mesh->second->Process( aCommand );
268 // SMESH_MeshEditor method?
269 map< _pyID, Handle(_pyMeshEditor) >::iterator id_editor = myMeshEditors.find( objID );
270 if ( id_editor != myMeshEditors.end() ) {
271 id_editor->second->Process( aCommand );
272 TCollection_AsciiString processedCommand = aCommand->GetString();
273 // some commands of SMESH_MeshEditor create meshes
274 if ( aCommand->GetMethod().Search("MakeMesh") != -1 ) {
275 Handle(_pyMesh) mesh = new _pyMesh( aCommand, aCommand->GetResultValue() );
276 aCommand->GetString() = processedCommand; // discard changes made by _pyMesh
277 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
281 // SMESH_Hypothesis method?
282 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
283 for ( ; hyp != myHypos.end(); ++hyp )
284 if ( !(*hyp)->IsAlgo() && objID == (*hyp)->GetID() ) {
285 (*hyp)->Process( aCommand );
289 // other object method?
290 map< _pyID, Handle(_pyObject) >::iterator id_obj = myObjects.find( objID );
291 if ( id_obj != myObjects.end() ) {
292 id_obj->second->Process( aCommand );
295 // if ( theCommand.Search( "aFilterManager" ) != -1 ) {
296 // if ( theCommand.Search( "CreateFilterManager" ) != -1 )
297 // myFilterManager = new _pySelfEraser( aCommand );
298 // else if ( !myFilterManager.IsNull() )
299 // myFilterManager->Process( aCommand );
303 // Add access to a wrapped mesh
304 AddMeshAccessorMethod( aCommand );
306 // Add access to a wrapped algorithm
307 // AddAlgoAccessorMethod( aCommand ); // ??? what if algo won't be wrapped at all ???
309 // PAL12227. PythonDump was not updated at proper time; result is
310 // aCriteria.append(SMESH.Filter.Criterion(17,26,0,'L1',26,25,1e-07,SMESH.EDGE,-1))
311 // TypeError: __init__() takes exactly 11 arguments (10 given)
312 char wrongCommand[] = "SMESH.Filter.Criterion(";
313 if ( int beg = theCommand.Location( wrongCommand, 1, theCommand.Length() ))
315 _pyCommand tmpCmd( theCommand.SubString( beg, theCommand.Length() ), -1);
316 // there must be 10 arguments, 5-th arg ThresholdID is missing,
317 const int wrongNbArgs = 9, missingArg = 5;
318 if ( tmpCmd.GetNbArgs() == wrongNbArgs )
320 for ( int i = wrongNbArgs; i > missingArg; --i )
321 tmpCmd.SetArg( i + 1, tmpCmd.GetArg( i ));
322 tmpCmd.SetArg( missingArg, "''");
323 aCommand->GetString().Trunc( beg - 1 );
324 aCommand->GetString() += tmpCmd.GetString();
330 //================================================================================
332 * \brief Convert the command or remember it for later conversion
333 * \param theCommand - The python command calling a method of SMESH_Gen
335 //================================================================================
337 void _pyGen::Process( const Handle(_pyCommand)& theCommand )
339 // there are methods to convert:
340 // CreateMesh( shape )
341 // Concatenate( [mesh1, ...], ... )
342 // CreateHypothesis( theHypType, theLibName )
343 // Compute( mesh, geom )
345 TCollection_AsciiString method = theCommand->GetMethod();
347 if ( method == "CreateMesh" || method == "CreateEmptyMesh")
349 Handle(_pyMesh) mesh = new _pyMesh( theCommand );
350 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
353 if ( method == "CreateMeshesFromUNV" || method == "CreateMeshesFromSTL")
355 Handle(_pyMesh) mesh = new _pyMesh( theCommand, theCommand->GetResultValue() );
356 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
359 if( method == "CreateMeshesFromMED")
361 for(int ind = 0;ind<theCommand->GetNbResultValues();ind++)
363 Handle(_pyMesh) mesh = new _pyMesh( theCommand, theCommand->GetResultValue(ind));
364 myMeshes.insert( make_pair( theCommand->GetResultValue(ind), mesh ));
368 // CreateHypothesis()
369 if ( method == "CreateHypothesis" )
371 // issue 199929, remove standard library name (default parameter)
372 const TCollection_AsciiString & aLibName = theCommand->GetArg( 2 );
373 if ( aLibName.Search( "StdMeshersEngine" ) != -1 ) {
374 // keep first argument
375 TCollection_AsciiString arg = theCommand->GetArg( 1 );
376 theCommand->RemoveArgs();
377 theCommand->SetArg( 1, arg );
380 myHypos.push_back( _pyHypothesis::NewHypothesis( theCommand ));
384 // smeshgen.Compute( mesh, geom ) --> mesh.Compute()
385 if ( method == "Compute" )
387 const _pyID& meshID = theCommand->GetArg( 1 );
388 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( meshID );
389 if ( id_mesh != myMeshes.end() ) {
390 theCommand->SetObject( meshID );
391 theCommand->RemoveArgs();
392 id_mesh->second->Flush();
397 // objects erasing creation command if no more it's commands invoked:
398 // SMESH_Pattern, FilterManager
399 if ( method == "GetPattern" || method == "CreateFilterManager" ) {
400 Handle(_pyObject) obj = new _pySelfEraser( theCommand );
401 if ( !myObjects.insert( make_pair( obj->GetID(), obj )).second )
402 theCommand->Clear(); // already created
405 // Concatenate( [mesh1, ...], ... )
406 if ( method == "Concatenate" || method == "ConcatenateWithGroups")
408 if ( method == "ConcatenateWithGroups" ) {
409 theCommand->SetMethod( "Concatenate" );
410 theCommand->SetArg( theCommand->GetNbArgs() + 1, "True" );
412 Handle(_pyMesh) mesh = new _pyMesh( theCommand, theCommand->GetResultValue() );
413 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
414 AddMeshAccessorMethod( theCommand );
417 // Replace name of SMESH_Gen
419 // names of SMESH_Gen methods fully equal to methods defined in smesh.py
420 static TStringSet smeshpyMethods;
421 if ( smeshpyMethods.empty() ) {
422 const char * names[] =
423 { "SetEmbeddedMode","IsEmbeddedMode","SetCurrentStudy","GetCurrentStudy",
424 "GetPattern","GetSubShapesId",
425 "" }; // <- mark of array end
426 smeshpyMethods.Insert( names );
428 if ( smeshpyMethods.Contains( theCommand->GetMethod() ))
429 // smeshgen.Method() --> smesh.Method()
430 theCommand->SetObject( SMESH_2smeshpy::SmeshpyName() );
432 // smeshgen.Method() --> smesh.smesh.Method()
433 theCommand->SetObject( SMESH_2smeshpy::GenName() );
436 //================================================================================
438 * \brief Convert the remembered commands
440 //================================================================================
444 // create empty command
445 myLastCommand = new _pyCommand();
447 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.begin();
448 for ( ; id_mesh != myMeshes.end(); ++id_mesh )
449 if ( ! id_mesh->second.IsNull() )
450 id_mesh->second->Flush();
452 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
453 for ( ; hyp != myHypos.end(); ++hyp )
454 if ( !hyp->IsNull() ) {
456 // smeshgen.CreateHypothesis() --> smesh.smesh.CreateHypothesis()
457 if ( !(*hyp)->IsWrapped() )
458 (*hyp)->GetCreationCmd()->SetObject( SMESH_2smeshpy::GenName() );
461 map< _pyID, Handle(_pyObject) >::iterator id_obj = myObjects.begin();
462 for ( ; id_obj != myObjects.end(); ++id_obj )
463 if ( ! id_obj->second.IsNull() )
464 id_obj->second->Flush();
466 myLastCommand->SetOrderNb( ++myNbCommands );
467 myCommands.push_back( myLastCommand );
470 //================================================================================
472 * \brief Add access method to mesh that is an argument
473 * \param theCmd - command to add access method
474 * \retval bool - true if added
476 //================================================================================
478 bool _pyGen::AddMeshAccessorMethod( Handle(_pyCommand) theCmd ) const
481 map< _pyID, Handle(_pyMesh) >::const_iterator id_mesh = myMeshes.begin();
482 for ( ; id_mesh != myMeshes.end(); ++id_mesh ) {
483 if ( theCmd->AddAccessorMethod( id_mesh->first, id_mesh->second->AccessorMethod() ))
489 //================================================================================
491 * \brief Add access method to algo that is an object or an argument
492 * \param theCmd - command to add access method
493 * \retval bool - true if added
495 //================================================================================
497 bool _pyGen::AddAlgoAccessorMethod( Handle(_pyCommand) theCmd ) const
500 list< Handle(_pyHypothesis) >::const_iterator hyp = myHypos.begin();
501 for ( ; hyp != myHypos.end(); ++hyp ) {
502 if ( (*hyp)->IsAlgo() && /*(*hyp)->IsWrapped() &&*/
503 theCmd->AddAccessorMethod( (*hyp)->GetID(), (*hyp)->AccessorMethod() ))
509 //================================================================================
511 * \brief Find hypothesis by ID (entry)
512 * \param theHypID - The hypothesis ID
513 * \retval Handle(_pyHypothesis) - The found hypothesis
515 //================================================================================
517 Handle(_pyHypothesis) _pyGen::FindHyp( const _pyID& theHypID )
519 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
520 for ( ; hyp != myHypos.end(); ++hyp )
521 if ( !hyp->IsNull() && theHypID == (*hyp)->GetID() )
523 return Handle(_pyHypothesis)();
526 //================================================================================
528 * \brief Find algorithm the created algorithm
529 * \param theGeom - The shape ID the algorithm was created on
530 * \param theMesh - The mesh ID that created the algorithm
531 * \param dim - The algo dimension
532 * \retval Handle(_pyHypothesis) - The found algo
534 //================================================================================
536 Handle(_pyHypothesis) _pyGen::FindAlgo( const _pyID& theGeom, const _pyID& theMesh,
537 const Handle(_pyHypothesis)& theHypothesis )
539 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
540 for ( ; hyp != myHypos.end(); ++hyp )
541 if ( !hyp->IsNull() &&
543 theHypothesis->CanBeCreatedBy( (*hyp)->GetAlgoType() ) &&
544 (*hyp)->GetGeom() == theGeom &&
545 (*hyp)->GetMesh() == theMesh )
550 //================================================================================
552 * \brief Find subMesh by ID (entry)
553 * \param theSubMeshID - The subMesh ID
554 * \retval Handle(_pySubMesh) - The found subMesh
556 //================================================================================
558 Handle(_pySubMesh) _pyGen::FindSubMesh( const _pyID& theSubMeshID )
560 map< _pyID, Handle(_pyObject) >::iterator id_subMesh = myObjects.find(theSubMeshID);
561 if ( id_subMesh != myObjects.end() )
562 return Handle(_pySubMesh)::DownCast( id_subMesh->second );
563 return Handle(_pySubMesh)();
567 //================================================================================
569 * \brief Change order of commands in the script
570 * \param theCmd1 - One command
571 * \param theCmd2 - Another command
573 //================================================================================
575 void _pyGen::ExchangeCommands( Handle(_pyCommand) theCmd1, Handle(_pyCommand) theCmd2 )
577 list< Handle(_pyCommand) >::iterator pos1, pos2;
578 pos1 = find( myCommands.begin(), myCommands.end(), theCmd1 );
579 pos2 = find( myCommands.begin(), myCommands.end(), theCmd2 );
580 myCommands.insert( pos1, theCmd2 );
581 myCommands.insert( pos2, theCmd1 );
582 myCommands.erase( pos1 );
583 myCommands.erase( pos2 );
585 int nb1 = theCmd1->GetOrderNb();
586 theCmd1->SetOrderNb( theCmd2->GetOrderNb() );
587 theCmd2->SetOrderNb( nb1 );
588 // cout << "BECOME " << theCmd1->GetOrderNb() << "\t" << theCmd1->GetString() << endl
589 // << "BECOME " << theCmd2->GetOrderNb() << "\t" << theCmd2->GetString() << endl << endl;
592 //================================================================================
594 * \brief Set one command after the other
595 * \param theCmd - Command to move
596 * \param theAfterCmd - Command ater which to insert the first one
598 //================================================================================
600 void _pyGen::SetCommandAfter( Handle(_pyCommand) theCmd, Handle(_pyCommand) theAfterCmd )
602 setNeighbourCommand( theCmd, theAfterCmd, true );
605 //================================================================================
607 * \brief Set one command before the other
608 * \param theCmd - Command to move
609 * \param theBeforeCmd - Command before which to insert the first one
611 //================================================================================
613 void _pyGen::SetCommandBefore( Handle(_pyCommand) theCmd, Handle(_pyCommand) theBeforeCmd )
615 setNeighbourCommand( theCmd, theBeforeCmd, false );
618 //================================================================================
620 * \brief Set one command before or after the other
621 * \param theCmd - Command to move
622 * \param theOtherCmd - Command ater or before which to insert the first one
624 //================================================================================
626 void _pyGen::setNeighbourCommand( Handle(_pyCommand)& theCmd,
627 Handle(_pyCommand)& theOtherCmd,
628 const bool theIsAfter )
630 list< Handle(_pyCommand) >::iterator pos;
631 pos = find( myCommands.begin(), myCommands.end(), theCmd );
632 myCommands.erase( pos );
633 pos = find( myCommands.begin(), myCommands.end(), theOtherCmd );
634 myCommands.insert( (theIsAfter ? ++pos : pos), theCmd );
637 for ( pos = myCommands.begin(); pos != myCommands.end(); ++pos)
638 (*pos)->SetOrderNb( i++ );
641 //================================================================================
643 * \brief Set command be last in list of commands
644 * \param theCmd - Command to be last
646 //================================================================================
648 Handle(_pyCommand)& _pyGen::GetLastCommand()
650 return myLastCommand;
653 //================================================================================
655 * \brief Set method to access to object wrapped with python class
656 * \param theID - The wrapped object entry
657 * \param theMethod - The accessor method
659 //================================================================================
661 void _pyGen::SetAccessorMethod(const _pyID& theID, const char* theMethod )
663 myID2AccessorMethod.Bind( theID, (char*) theMethod );
666 //================================================================================
668 * \brief Generated new ID for object and assign with existing name
669 * \param theID - ID of existing object
671 //================================================================================
673 _pyID _pyGen::GenerateNewID( const _pyID& theID )
678 aNewID = theID + _pyID( ":" ) + _pyID( index++ );
680 while ( myObjectNames.IsBound( aNewID ) );
682 myObjectNames.Bind( aNewID, myObjectNames.IsBound( theID )
683 ? (myObjectNames.Find( theID ) + _pyID( "_" ) + _pyID( index-1 ))
684 : _pyID( "A" ) + aNewID );
688 //================================================================================
690 * \brief Find out type of geom group
691 * \param grpID - The geom group entry
692 * \retval int - The type
694 //================================================================================
696 static bool sameGroupType( const _pyID& grpID,
697 const TCollection_AsciiString& theType)
699 // define group type as smesh.Mesh.Group() does
701 SALOMEDS::Study_var study = SMESH_Gen_i::GetSMESHGen()->GetCurrentStudy();
702 SALOMEDS::SObject_var aSObj = study->FindObjectID( grpID.ToCString() );
703 if ( !aSObj->_is_nil() ) {
704 GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow( aSObj->GetObject() );
705 if ( !aGeomObj->_is_nil() ) {
706 switch ( aGeomObj->GetShapeType() ) {
707 case GEOM::VERTEX: type = SMESH::NODE; break;
708 case GEOM::EDGE: type = SMESH::EDGE; break;
709 case GEOM::FACE: type = SMESH::FACE; break;
711 case GEOM::SHELL: type = SMESH::VOLUME; break;
712 case GEOM::COMPOUND: {
713 GEOM::GEOM_Gen_ptr aGeomGen = SMESH_Gen_i::GetSMESHGen()->GetGeomEngine();
714 if ( !aGeomGen->_is_nil() ) {
715 GEOM::GEOM_IGroupOperations_var aGrpOp =
716 aGeomGen->GetIGroupOperations( study->StudyId() );
717 if ( !aGrpOp->_is_nil() ) {
718 switch ( aGrpOp->GetType( aGeomObj )) {
719 case TopAbs_VERTEX: type = SMESH::NODE; break;
720 case TopAbs_EDGE: type = SMESH::EDGE; break;
721 case TopAbs_FACE: type = SMESH::FACE; break;
722 case TopAbs_SOLID: type = SMESH::VOLUME; break;
733 MESSAGE("Type of the group " << grpID << " not found");
736 if ( theType.IsIntegerValue() )
737 return type == theType.IntegerValue();
740 case SMESH::NODE: return theType.Location( "NODE", 1, theType.Length() );
741 case SMESH::EDGE: return theType.Location( "EDGE", 1, theType.Length() );
742 case SMESH::FACE: return theType.Location( "FACE", 1, theType.Length() );
743 case SMESH::VOLUME: return theType.Location( "VOLUME", 1, theType.Length() );
749 //================================================================================
752 * \param theCreationCmd -
754 //================================================================================
756 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd)
757 : _pyObject(theCreationCmd), myHasEditor(false)
759 // convert my creation command
760 Handle(_pyCommand) creationCmd = GetCreationCmd();
761 //TCollection_AsciiString str = creationCmd->GetMethod();
762 // if(str != "CreateMeshesFromUNV" &&
763 // str != "CreateMeshesFromMED" &&
764 // str != "CreateMeshesFromSTL")
765 creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
766 creationCmd->SetMethod( "Mesh" );
768 theGen->SetAccessorMethod( GetID(), "GetMesh()" );
771 //================================================================================
774 * \param theCreationCmd -
776 //================================================================================
777 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd, const TCollection_AsciiString& id):
778 _pyObject(theCreationCmd), myHasEditor(false)
780 // convert my creation command
781 Handle(_pyCommand) creationCmd = GetCreationCmd();
782 creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
783 theGen->SetAccessorMethod( id, "GetMesh()" );
786 //================================================================================
788 * \brief Convert a IDL API command of SMESH::Mesh to a method call of python Mesh
789 * \param theCommand - Engine method called for this mesh
791 //================================================================================
793 void _pyMesh::Process( const Handle(_pyCommand)& theCommand )
795 // some methods of SMESH_Mesh interface needs special conversion
796 // to methods of Mesh python class
798 // 1. GetSubMesh(geom, name) + AddHypothesis(geom, algo)
799 // --> in Mesh_Algorithm.Create(mesh, geom, hypo, so)
800 // 2. AddHypothesis(geom, hyp)
801 // --> in Mesh_Algorithm.Hypothesis(hyp, args, so)
802 // 3. CreateGroupFromGEOM(type, name, grp)
803 // --> in Mesh.Group(grp, name="")
804 // 4. ExportToMED(f, auto_groups, version)
805 // --> in Mesh.ExportMED( f, auto_groups, version )
808 const TCollection_AsciiString method = theCommand->GetMethod();
809 // ----------------------------------------------------------------------
810 if ( method == "GetSubMesh" ) {
811 Handle(_pySubMesh) subMesh = theGen->FindSubMesh( theCommand->GetResultValue() );
812 if ( !subMesh.IsNull() ) {
813 subMesh->SetCreator( this );
814 mySubmeshes.push_back( subMesh );
817 // ----------------------------------------------------------------------
818 else if ( method == "AddHypothesis" ) { // mesh.AddHypothesis(geom, HYPO )
819 myAddHypCmds.push_back( theCommand );
821 const _pyID& hypID = theCommand->GetArg( 2 );
822 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
823 if ( !hyp.IsNull() ) {
824 myHypos.push_back( hyp );
825 if ( hyp->GetMesh().IsEmpty() )
826 hyp->SetMesh( this->GetID() );
829 // ----------------------------------------------------------------------
830 else if ( method == "CreateGroupFromGEOM" ) {// (type, name, grp)
831 _pyID grp = theCommand->GetArg( 3 );
832 if ( sameGroupType( grp, theCommand->GetArg( 1 )) ) { // --> Group(grp)
833 theCommand->SetMethod( "Group" );
834 theCommand->RemoveArgs();
835 theCommand->SetArg( 1, grp );
838 _pyID type = theCommand->GetArg( 1 );
839 _pyID name = theCommand->GetArg( 2 );
840 theCommand->SetMethod( "GroupOnGeom" );
841 theCommand->RemoveArgs();
842 theCommand->SetArg( 1, grp );
843 theCommand->SetArg( 2, name );
844 theCommand->SetArg( 3, type );
847 // ----------------------------------------------------------------------
848 else if ( method == "ExportToMED" ) { // ExportToMED() --> ExportMED()
849 theCommand->SetMethod( "ExportMED" );
851 // ----------------------------------------------------------------------
852 else if ( method == "CreateGroup" ) { // CreateGroup() --> CreateEmptyGroup()
853 theCommand->SetMethod( "CreateEmptyGroup" );
855 // ----------------------------------------------------------------------
856 else if ( method == "RemoveHypothesis" ) // (geom, hyp)
858 _pyID hypID = theCommand->GetArg( 2 );
860 // check if this mesh still has corresponding addition command
861 bool hasAddCmd = false;
862 list< Handle(_pyCommand) >::iterator cmd = myAddHypCmds.begin();
863 while ( cmd != myAddHypCmds.end() )
865 // AddHypothesis(geom, hyp)
866 if ( hypID == (*cmd)->GetArg( 2 )) { // erase both (add and remove) commands
869 cmd = myAddHypCmds.erase( cmd );
876 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
877 if ( ! hasAddCmd && hypID.Length() != 0 ) { // hypo addition already wrapped
878 // RemoveHypothesis(geom, hyp) --> RemoveHypothesis( hyp, geom=0 )
879 _pyID geom = theCommand->GetArg( 1 );
880 theCommand->RemoveArgs();
881 theCommand->SetArg( 1, hypID );
882 if ( geom != GetGeom() )
883 theCommand->SetArg( 2, geom );
885 // remove hyp from myHypos
886 myHypos.remove( hyp );
888 // add accessor method if necessary
891 if ( NeedMeshAccess( theCommand ))
892 // apply theCommand to the mesh wrapped by smeshpy mesh
893 AddMeshAccess( theCommand );
897 //================================================================================
899 * \brief Return True if addition of accesor method is needed
901 //================================================================================
903 bool _pyMesh::NeedMeshAccess( const Handle(_pyCommand)& theCommand )
905 // names of SMESH_Mesh methods fully equal to methods of class Mesh, so
906 // no conversion is needed for them at all:
907 static TStringSet sameMethods;
908 if ( sameMethods.empty() ) {
909 const char * names[] =
910 { "ExportDAT","ExportUNV","ExportSTL", "RemoveGroup","RemoveGroupWithContents",
911 "GetGroups","UnionGroups","IntersectGroups","CutGroups","GetLog","GetId","ClearLog",
912 "GetStudyId","HasDuplicatedGroupNamesMED","GetMEDMesh","NbNodes","NbElements",
913 "NbEdges","NbEdgesOfOrder","NbFaces","NbFacesOfOrder","NbTriangles",
914 "NbTrianglesOfOrder","NbQuadrangles","NbQuadranglesOfOrder","NbPolygons","NbVolumes",
915 "NbVolumesOfOrder","NbTetras","NbTetrasOfOrder","NbHexas","NbHexasOfOrder",
916 "NbPyramids","NbPyramidsOfOrder","NbPrisms","NbPrismsOfOrder","NbPolyhedrons",
917 "NbSubMesh","GetElementsId","GetElementsByType","GetNodesId","GetElementType",
918 "GetSubMeshElementsId","GetSubMeshNodesId","GetSubMeshElementType","Dump","GetNodeXYZ",
919 "GetNodeInverseElements","GetShapeID","GetShapeIDForElem","GetElemNbNodes",
920 "GetElemNode","IsMediumNode","IsMediumNodeOfAnyElem","ElemNbEdges","ElemNbFaces",
921 "IsPoly","IsQuadratic","BaryCenter","GetHypothesisList", "SetAutoColor", "GetAutoColor",
922 "Clear", "ConvertToStandalone"
923 ,"" }; // <- mark of end
924 sameMethods.Insert( names );
927 return !sameMethods.Contains( theCommand->GetMethod() );
930 //================================================================================
932 * \brief Convert creation and addition of all algos and hypos
934 //================================================================================
936 void _pyMesh::Flush()
938 list < Handle(_pyCommand) >::iterator cmd;
940 // try to convert algo addition like this:
941 // mesh.AddHypothesis(geom, ALGO ) --> ALGO = mesh.Algo()
942 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
944 Handle(_pyCommand) addCmd = *cmd;
946 _pyID algoID = addCmd->GetArg( 2 );
947 Handle(_pyHypothesis) algo = theGen->FindHyp( algoID );
948 if ( algo.IsNull() || !algo->IsAlgo() )
951 // check and create new algorithm instance if it is already wrapped
952 if ( algo->IsWrapped() ) {
953 _pyID localAlgoID = theGen->GenerateNewID( algoID );
954 TCollection_AsciiString aNewCmdStr = localAlgoID +
955 TCollection_AsciiString( " = " ) + theGen->GetID() +
956 TCollection_AsciiString( ".CreateHypothesis( \"" ) + algo->GetAlgoType() +
957 TCollection_AsciiString( "\" )" );
959 Handle(_pyCommand) newCmd = theGen->AddCommand( aNewCmdStr );
960 Handle(_pyAlgorithm) newAlgo = Handle(_pyAlgorithm)::DownCast(theGen->FindHyp( localAlgoID ));
961 if ( !newAlgo.IsNull() ) {
962 newAlgo->Assign( algo, this->GetID() );
963 newAlgo->SetCreationCmd( newCmd );
965 // set algorithm creation
966 theGen->SetCommandBefore( newCmd, addCmd );
971 _pyID geom = addCmd->GetArg( 1 );
972 bool isLocalAlgo = ( geom != GetGeom() );
975 if ( algo->Addition2Creation( addCmd, this->GetID() )) // OK
977 // wrapped algo is created atfer mesh creation
978 GetCreationCmd()->AddDependantCmd( addCmd );
981 // mesh.AddHypothesis(geom, ALGO ) --> mesh.AlgoMethod(geom)
982 addCmd->SetArg( addCmd->GetNbArgs() + 1,
983 TCollection_AsciiString( "geom=" ) + geom );
984 // sm = mesh.GetSubMesh(geom, name) --> sm = ALGO.GetSubMesh()
985 list < Handle(_pySubMesh) >::iterator smIt;
986 for ( smIt = mySubmeshes.begin(); smIt != mySubmeshes.end(); ++smIt ) {
987 Handle(_pySubMesh) subMesh = *smIt;
988 Handle(_pyCommand) subCmd = subMesh->GetCreationCmd();
989 if ( geom == subCmd->GetArg( 1 )) {
990 subCmd->SetObject( algo->GetID() );
991 subCmd->RemoveArgs();
992 subMesh->SetCreator( algo );
997 else // KO - ALGO was already created
999 // mesh.AddHypothesis(geom, ALGO) --> mesh.AddHypothesis(ALGO, geom=0)
1000 addCmd->RemoveArgs();
1001 addCmd->SetArg( 1, algoID );
1003 addCmd->SetArg( 2, geom );
1007 // try to convert hypo addition like this:
1008 // mesh.AddHypothesis(geom, HYPO ) --> HYPO = algo.Hypo()
1009 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
1011 Handle(_pyCommand) addCmd = *cmd;
1012 _pyID hypID = addCmd->GetArg( 2 );
1013 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
1014 if ( hyp.IsNull() || hyp->IsAlgo() )
1016 bool converted = hyp->Addition2Creation( addCmd, this->GetID() );
1018 // mesh.AddHypothesis(geom, HYP) --> mesh.AddHypothesis(HYP, geom=0)
1019 _pyID geom = addCmd->GetArg( 1 );
1020 addCmd->RemoveArgs();
1021 addCmd->SetArg( 1, hypID );
1022 if ( geom != GetGeom() )
1023 addCmd->SetArg( 2, geom );
1027 // sm = mesh.GetSubMesh(geom, name) --> sm = mesh.GetMesh().GetSubMesh(geom, name)
1028 // for ( cmd = mySubmeshes.begin(); cmd != mySubmeshes.end(); ++cmd ) {
1029 // Handle(_pyCommand) subCmd = *cmd;
1030 // if ( subCmd->GetNbArgs() > 0 )
1031 // AddMeshAccess( subCmd );
1033 myAddHypCmds.clear();
1034 mySubmeshes.clear();
1037 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
1038 for ( ; hyp != myHypos.end(); ++hyp )
1042 //================================================================================
1044 * \brief MeshEditor convert its commands to ones of mesh
1046 //================================================================================
1048 _pyMeshEditor::_pyMeshEditor(const Handle(_pyCommand)& theCreationCmd):
1049 _pyObject( theCreationCmd )
1051 myMesh = theCreationCmd->GetObject();
1052 myCreationCmdStr = theCreationCmd->GetString();
1053 theCreationCmd->Clear();
1056 //================================================================================
1058 * \brief convert its commands to ones of mesh
1060 //================================================================================
1062 void _pyMeshEditor::Process( const Handle(_pyCommand)& theCommand)
1064 // names of SMESH_MeshEditor methods fully equal to methods of class Mesh, so
1065 // commands calling this methods are converted to calls of methods of Mesh
1066 static TStringSet sameMethods;
1067 if ( sameMethods.empty() ) {
1068 const char * names[] = {
1069 "RemoveElements","RemoveNodes","AddNode","Add0DElement","AddEdge","AddFace","AddPolygonalFace",
1070 "AddVolume","AddPolyhedralVolume","AddPolyhedralVolumeByFaces","MoveNode", "MoveClosestNodeToPoint",
1071 "InverseDiag","DeleteDiag","Reorient","ReorientObject","TriToQuad","SplitQuad","SplitQuadObject",
1072 "BestSplit","Smooth","SmoothObject","SmoothParametric","SmoothParametricObject",
1073 "ConvertToQuadratic","ConvertFromQuadratic","RenumberNodes","RenumberElements",
1074 "RotationSweep","RotationSweepObject","RotationSweepObject1D","RotationSweepObject2D",
1075 "ExtrusionSweep","AdvancedExtrusion","ExtrusionSweepObject","ExtrusionSweepObject1D","ExtrusionSweepObject2D",
1076 "ExtrusionAlongPath","ExtrusionAlongPathObject","ExtrusionAlongPathObject1D","ExtrusionAlongPathObject2D",
1077 "ExtrusionAlongPathX","ExtrusionAlongPathObjX",
1078 "Mirror","MirrorObject","Translate","TranslateObject","Rotate","RotateObject",
1079 "FindCoincidentNodes","FindCoincidentNodesOnPart","MergeNodes","FindEqualElements",
1080 "MergeElements","MergeEqualElements","SewFreeBorders","SewConformFreeBorders",
1081 "SewBorderToSide","SewSideElements","ChangeElemNodes","GetLastCreatedNodes",
1082 "GetLastCreatedElems",
1083 "MirrorMakeMesh","MirrorObjectMakeMesh","TranslateMakeMesh",
1084 "TranslateObjectMakeMesh","RotateMakeMesh","RotateObjectMakeMesh"
1085 ,"" }; // <- mark of the end
1086 sameMethods.Insert( names );
1089 // names of SMESH_MeshEditor methods which differ from methods of class Mesh
1090 // only last two arguments
1091 static TStringSet diffLastTwoArgsMethods;
1092 if (diffLastTwoArgsMethods.empty() ){
1093 const char * names[] = {
1094 "MirrorMakeGroups","MirrorObjectMakeGroups",
1095 "TranslateMakeGroups","TranslateObjectMakeGroups",
1096 "RotateMakeGroups","RotateObjectMakeGroups",
1097 ""};// <- mark of the end
1098 diffLastTwoArgsMethods.Insert( names );
1101 if ( sameMethods.Contains( theCommand->GetMethod() )) {
1102 theCommand->SetObject( myMesh );
1104 // meshes made by *MakeMesh() methods are not wrapped by _pyMesh,
1105 // so let _pyMesh care of it (TMP?)
1106 // if ( theCommand->GetMethod().Search("MakeMesh") != -1 )
1107 // _pyMesh( new _pyCommand( theCommand->GetString(), 0 )); // for theGen->SetAccessorMethod()
1111 //Replace SMESH_MeshEditor "MakeGroups" functions on the Mesh
1112 //functions with the flag "theMakeGroups = True" like:
1113 //SMESH_MeshEditor.CmdMakeGroups => Mesh.Cmd(...,True)
1114 int pos = theCommand->GetMethod().Search("MakeGroups");
1116 // 1. Remove "MakeGroups" from the Command
1117 TCollection_AsciiString aMethod = theCommand->GetMethod();
1118 int nbArgsToAdd = diffLastTwoArgsMethods.Contains(aMethod) ? 2 : 1;
1119 aMethod.Trunc(pos-1);
1120 theCommand->SetMethod(aMethod);
1122 // 2. Set Mesh object instead of SMESH_MeshEditor
1123 theCommand->SetObject( myMesh );
1125 // 3. And add last "True" argument
1126 while(nbArgsToAdd--)
1127 theCommand->SetArg(theCommand->GetNbArgs()+1,"True ");
1130 // editor creation command is needed only if any editor function is called
1131 theGen->AddMeshAccessorMethod( theCommand ); // for *Object()
1132 if ( !myCreationCmdStr.IsEmpty() ) {
1133 GetCreationCmd()->GetString() = myCreationCmdStr;
1134 myCreationCmdStr.Clear();
1140 //================================================================================
1142 * \brief _pyHypothesis constructor
1143 * \param theCreationCmd -
1145 //================================================================================
1147 _pyHypothesis::_pyHypothesis(const Handle(_pyCommand)& theCreationCmd):
1148 _pyObject( theCreationCmd )
1150 myIsAlgo = myIsWrapped = /*myIsConverted = myIsLocal = myDim = */false;
1153 //================================================================================
1155 * \brief Creates algorithm or hypothesis
1156 * \param theCreationCmd - The engine command creating a hypothesis
1157 * \retval Handle(_pyHypothesis) - Result _pyHypothesis
1159 //================================================================================
1161 Handle(_pyHypothesis) _pyHypothesis::NewHypothesis( const Handle(_pyCommand)& theCreationCmd)
1163 // theCreationCmd: CreateHypothesis( "theHypType", "theLibName" )
1164 ASSERT (( theCreationCmd->GetMethod() == "CreateHypothesis"));
1166 Handle(_pyHypothesis) hyp, algo;
1169 const TCollection_AsciiString & hypTypeQuoted = theCreationCmd->GetArg( 1 );
1170 if ( hypTypeQuoted.IsEmpty() )
1173 TCollection_AsciiString hypType =
1174 hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
1176 algo = new _pyAlgorithm( theCreationCmd );
1177 hyp = new _pyHypothesis( theCreationCmd );
1179 // 1D Regular_1D ----------
1180 if ( hypType == "Regular_1D" ) {
1181 // set mesh's method creating algo,
1182 // i.e. convertion result will be "regular1d = Mesh.Segment()",
1183 // and set hypType by which algo creating a hypothesis is searched for
1184 algo->SetConvMethodAndType("Segment", hypType.ToCString());
1186 else if ( hypType == "CompositeSegment_1D" ) {
1187 algo->SetConvMethodAndType("Segment", "Regular_1D");
1188 algo->myArgs.Append( "algo=smesh.COMPOSITE");
1190 else if ( hypType == "LocalLength" ) {
1191 // set algo's method creating hyp, and algo type
1192 hyp->SetConvMethodAndType( "LocalLength", "Regular_1D");
1193 // set method whose 1 arg will become the 1-st arg of hyp creation command
1194 // i.e. convertion result will be "locallength = regular1d.LocalLength(<arg of SetLength()>)"
1195 hyp->AddArgMethod( "SetLength" );
1197 else if ( hypType == "MaxLength" ) {
1198 // set algo's method creating hyp, and algo type
1199 hyp->SetConvMethodAndType( "MaxSize", "Regular_1D");
1200 // set method whose 1 arg will become the 1-st arg of hyp creation command
1201 // i.e. convertion result will be "maxsize = regular1d.MaxSize(<arg of SetLength()>)"
1202 hyp->AddArgMethod( "SetLength" );
1204 else if ( hypType == "NumberOfSegments" ) {
1205 hyp = new _pyNumberOfSegmentsHyp( theCreationCmd );
1206 hyp->SetConvMethodAndType( "NumberOfSegments", "Regular_1D");
1207 // arg of SetNumberOfSegments() will become the 1-st arg of hyp creation command
1208 hyp->AddArgMethod( "SetNumberOfSegments" );
1209 // arg of SetScaleFactor() will become the 2-nd arg of hyp creation command
1210 hyp->AddArgMethod( "SetScaleFactor" );
1211 hyp->AddArgMethod( "SetReversedEdges" );
1213 else if ( hypType == "Arithmetic1D" ) {
1214 hyp = new _pyComplexParamHypo( theCreationCmd );
1215 hyp->SetConvMethodAndType( "Arithmetic1D", "Regular_1D");
1216 hyp->AddArgMethod( "SetStartLength" );
1217 hyp->AddArgMethod( "SetEndLength" );
1218 hyp->AddArgMethod( "SetReversedEdges" );
1220 else if ( hypType == "StartEndLength" ) {
1221 hyp = new _pyComplexParamHypo( theCreationCmd );
1222 hyp->SetConvMethodAndType( "StartEndLength", "Regular_1D");
1223 hyp->AddArgMethod( "SetStartLength" );
1224 hyp->AddArgMethod( "SetEndLength" );
1225 hyp->AddArgMethod( "SetReversedEdges" );
1227 else if ( hypType == "Deflection1D" ) {
1228 hyp->SetConvMethodAndType( "Deflection1D", "Regular_1D");
1229 hyp->AddArgMethod( "SetDeflection" );
1231 else if ( hypType == "Propagation" ) {
1232 hyp->SetConvMethodAndType( "Propagation", "Regular_1D");
1234 else if ( hypType == "QuadraticMesh" ) {
1235 hyp->SetConvMethodAndType( "QuadraticMesh", "Regular_1D");
1237 else if ( hypType == "AutomaticLength" ) {
1238 hyp->SetConvMethodAndType( "AutomaticLength", "Regular_1D");
1239 hyp->AddArgMethod( "SetFineness");
1241 else if ( hypType == "SegmentLengthAroundVertex" ) {
1242 hyp = new _pySegmentLengthAroundVertexHyp( theCreationCmd );
1243 hyp->SetConvMethodAndType( "LengthNearVertex", "Regular_1D" );
1244 hyp->AddArgMethod( "SetLength" );
1246 // 1D Python_1D ----------
1247 else if ( hypType == "Python_1D" ) {
1248 algo->SetConvMethodAndType( "Segment", hypType.ToCString());
1249 algo->myArgs.Append( "algo=smesh.PYTHON");
1251 else if ( hypType == "PythonSplit1D" ) {
1252 hyp->SetConvMethodAndType( "PythonSplit1D", "Python_1D");
1253 hyp->AddArgMethod( "SetNumberOfSegments");
1254 hyp->AddArgMethod( "SetPythonLog10RatioFunction");
1256 // MEFISTO_2D ----------
1257 else if ( hypType == "MEFISTO_2D" ) { // MEFISTO_2D
1258 algo->SetConvMethodAndType( "Triangle", hypType.ToCString());
1260 else if ( hypType == "MaxElementArea" ) {
1261 hyp->SetConvMethodAndType( "MaxElementArea", "MEFISTO_2D");
1262 hyp->SetConvMethodAndType( "MaxElementArea", "NETGEN_2D_ONLY");
1263 hyp->AddArgMethod( "SetMaxElementArea");
1265 else if ( hypType == "LengthFromEdges" ) {
1266 hyp->SetConvMethodAndType( "LengthFromEdges", "MEFISTO_2D");
1267 hyp->SetConvMethodAndType( "LengthFromEdges", "NETGEN_2D_ONLY");
1269 // Quadrangle_2D ----------
1270 else if ( hypType == "Quadrangle_2D" ) {
1271 algo->SetConvMethodAndType( "Quadrangle" , hypType.ToCString());
1273 else if ( hypType == "QuadranglePreference" ) {
1274 hyp->SetConvMethodAndType( "QuadranglePreference", "Quadrangle_2D");
1275 hyp->SetConvMethodAndType( "SetQuadAllowed", "NETGEN_2D_ONLY");
1277 else if ( hypType == "TrianglePreference" ) {
1278 hyp->SetConvMethodAndType( "TrianglePreference", "Quadrangle_2D");
1280 // BLSURF ----------
1281 else if ( hypType == "BLSURF" ) {
1282 algo->SetConvMethodAndType( "Triangle", hypType.ToCString());
1283 algo->myArgs.Append( "algo=smesh.BLSURF" );
1285 else if ( hypType == "BLSURF_Parameters") {
1286 hyp->SetConvMethodAndType( "Parameters", "BLSURF");
1288 // NETGEN ----------
1289 else if ( hypType == "NETGEN_2D") { // 1D-2D
1290 algo->SetConvMethodAndType( "Triangle" , hypType.ToCString());
1291 algo->myArgs.Append( "algo=smesh.NETGEN" );
1293 else if ( hypType == "NETGEN_Parameters_2D") {
1294 hyp->SetConvMethodAndType( "Parameters", "NETGEN_2D");
1296 else if ( hypType == "NETGEN_SimpleParameters_2D") {
1297 hyp->SetConvMethodAndType( "Parameters", "NETGEN_2D");
1298 hyp->myArgs.Append( "which=smesh.SIMPLE" );
1300 else if ( hypType == "NETGEN_2D3D") { // 1D-2D-3D
1301 algo->SetConvMethodAndType( "Tetrahedron" , hypType.ToCString());
1302 algo->myArgs.Append( "algo=smesh.FULL_NETGEN" );
1304 else if ( hypType == "NETGEN_Parameters") {
1305 hyp->SetConvMethodAndType( "Parameters", "NETGEN_2D3D");
1307 else if ( hypType == "NETGEN_SimpleParameters_3D") {
1308 hyp->SetConvMethodAndType( "Parameters", "NETGEN_2D3D");
1309 hyp->myArgs.Append( "which=smesh.SIMPLE" );
1311 else if ( hypType == "NETGEN_2D_ONLY") { // 2D
1312 algo->SetConvMethodAndType( "Triangle" , hypType.ToCString());
1313 algo->myArgs.Append( "algo=smesh.NETGEN_2D" );
1315 else if ( hypType == "NETGEN_3D") { // 3D
1316 algo->SetConvMethodAndType( "Tetrahedron" , hypType.ToCString());
1317 algo->myArgs.Append( "algo=smesh.NETGEN" );
1319 else if ( hypType == "MaxElementVolume") {
1320 hyp->SetConvMethodAndType( "MaxElementVolume", "NETGEN_3D");
1321 hyp->AddArgMethod( "SetMaxElementVolume" );
1323 // GHS3D_3D ----------
1324 else if ( hypType == "GHS3D_3D" ) {
1325 algo->SetConvMethodAndType( "Tetrahedron", hypType.ToCString());
1326 algo->myArgs.Append( "algo=smesh.GHS3D" );
1328 else if ( hypType == "GHS3D_Parameters") {
1329 hyp->SetConvMethodAndType( "Parameters", "GHS3D_3D");
1331 // Hexa_3D ---------
1332 else if ( hypType == "BLSURF" ) {
1333 algo->SetConvMethodAndType( "Hexahedron", hypType.ToCString());
1335 // Repetitive Projection_1D ---------
1336 else if ( hypType == "Projection_1D" ) {
1337 algo->SetConvMethodAndType( "Projection1D", hypType.ToCString());
1339 else if ( hypType == "ProjectionSource1D" ) {
1340 hyp->SetConvMethodAndType( "SourceEdge", "Projection_1D");
1341 hyp->AddArgMethod( "SetSourceEdge");
1342 hyp->AddArgMethod( "SetSourceMesh");
1343 // 2 args of SetVertexAssociation() will become the 3-th and 4-th args of hyp creation command
1344 hyp->AddArgMethod( "SetVertexAssociation", 2 );
1346 // Projection_2D ---------
1347 else if ( hypType == "Projection_2D" ) {
1348 algo->SetConvMethodAndType( "Projection2D", hypType.ToCString());
1350 else if ( hypType == "ProjectionSource2D" ) {
1351 hyp->SetConvMethodAndType( "SourceFace", "Projection_2D");
1352 hyp->AddArgMethod( "SetSourceFace");
1353 hyp->AddArgMethod( "SetSourceMesh");
1354 hyp->AddArgMethod( "SetVertexAssociation", 4 );
1356 // Projection_3D ---------
1357 else if ( hypType == "Projection_3D" ) {
1358 algo->SetConvMethodAndType( "Projection3D", hypType.ToCString());
1360 else if ( hypType == "ProjectionSource3D" ) {
1361 hyp->SetConvMethodAndType( "SourceShape3D", "Projection_3D");
1362 hyp->AddArgMethod( "SetSource3DShape");
1363 hyp->AddArgMethod( "SetSourceMesh");
1364 hyp->AddArgMethod( "SetVertexAssociation", 4 );
1366 // Prism_3D ---------
1367 else if ( hypType == "Prism_3D" ) {
1368 algo->SetConvMethodAndType( "Prism", hypType.ToCString());
1370 // RadialPrism_3D ---------
1371 else if ( hypType == "RadialPrism_3D" ) {
1372 algo->SetConvMethodAndType( "Prism", hypType.ToCString());
1374 else if ( hypType == "NumberOfLayers" ) {
1375 hyp->SetConvMethodAndType( "NumberOfLayers", "RadialPrism_3D");
1376 hyp->AddArgMethod( "SetNumberOfLayers" );
1378 else if ( hypType == "LayerDistribution" ) {
1379 hyp = new _pyLayerDistributionHypo( theCreationCmd );
1380 hyp->SetConvMethodAndType( "LayerDistribution", "RadialPrism_3D");
1382 else if ( hypType == "LayerDistribution2D" ) {
1383 hyp = new _pyLayerDistributionHypo( theCreationCmd );
1384 hyp->SetConvMethodAndType( "LayerDistribution2D", "RadialQuadrangle_1D2D");
1387 if ( algo->IsValid() ) {
1393 //================================================================================
1395 * \brief Convert the command adding a hypothesis to mesh into a smesh command
1396 * \param theCmd - The command like mesh.AddHypothesis( geom, hypo )
1397 * \param theAlgo - The algo that can create this hypo
1398 * \retval bool - false if the command cant be converted
1400 //================================================================================
1402 bool _pyHypothesis::Addition2Creation( const Handle(_pyCommand)& theCmd,
1403 const _pyID& theMesh)
1405 ASSERT(( theCmd->GetMethod() == "AddHypothesis" ));
1407 if ( !IsWrappable( theMesh ))
1410 myGeom = theCmd->GetArg( 1 );
1412 Handle(_pyHypothesis) algo;
1414 // find algo created on myGeom in theMesh
1415 algo = theGen->FindAlgo( myGeom, theMesh, this );
1416 if ( algo.IsNull() )
1418 // attach hypothesis creation command to be after algo creation command
1419 // because it can be new created instance of algorithm
1420 algo->GetCreationCmd()->AddDependantCmd( theCmd );
1424 // mesh.AddHypothesis(geom,hyp) --> hyp = <theMesh or algo>.myCreationMethod(args)
1425 theCmd->SetResultValue( GetID() );
1426 theCmd->SetObject( IsAlgo() ? theMesh : algo->GetID());
1427 theCmd->SetMethod( IsAlgo() ? GetAlgoCreationMethod() : GetCreationMethod( algo->GetAlgoType() ));
1429 theCmd->RemoveArgs();
1430 for ( int i = 1; i <= myArgs.Length(); ++i ) {
1431 if ( !myArgs( i ).IsEmpty() )
1432 theCmd->SetArg( i, myArgs( i ));
1434 theCmd->SetArg( i, "[]");
1436 // set a new creation command
1437 GetCreationCmd()->Clear();
1438 // replace creation command by wrapped instance
1439 // please note, that hypothesis attaches to algo creation command (see upper)
1440 SetCreationCmd( theCmd );
1443 // clear commands setting arg values
1444 list < Handle(_pyCommand) >::iterator argCmd = myArgCommands.begin();
1445 for ( ; argCmd != myArgCommands.end(); ++argCmd )
1448 // set unknown arg commands after hypo creation
1449 Handle(_pyCommand) afterCmd = myIsWrapped ? theCmd : GetCreationCmd();
1450 list<Handle(_pyCommand)>::iterator cmd = myUnknownCommands.begin();
1451 for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
1452 afterCmd->AddDependantCmd( *cmd );
1458 //================================================================================
1460 * \brief Remember hypothesis parameter values
1461 * \param theCommand - The called hypothesis method
1463 //================================================================================
1465 void _pyHypothesis::Process( const Handle(_pyCommand)& theCommand)
1467 ASSERT( !myIsAlgo );
1470 for ( int i = 1; i <= myArgMethods.Length(); ++i ) {
1471 if ( myArgMethods( i ) == theCommand->GetMethod() ) {
1472 while ( myArgs.Length() < nbArgs + myNbArgsByMethod( i ))
1473 myArgs.Append( "[]" );
1474 for ( int iArg = 1; iArg <= myNbArgsByMethod( i ); ++iArg )
1475 myArgs( nbArgs + iArg ) = theCommand->GetArg( iArg ); // arg value
1476 myArgCommands.push_back( theCommand );
1479 nbArgs += myNbArgsByMethod( i );
1481 myUnknownCommands.push_back( theCommand );
1484 //================================================================================
1486 * \brief Finish conversion
1488 //================================================================================
1490 void _pyHypothesis::Flush()
1492 if ( IsWrapped() ) {
1495 list < Handle(_pyCommand) >::iterator cmd = myArgCommands.begin();
1496 for ( ; cmd != myArgCommands.end(); ++cmd ) {
1497 // Add access to a wrapped mesh
1498 theGen->AddMeshAccessorMethod( *cmd );
1499 // Add access to a wrapped algorithm
1500 theGen->AddAlgoAccessorMethod( *cmd );
1502 cmd = myUnknownCommands.begin();
1503 for ( ; cmd != myUnknownCommands.end(); ++cmd ) {
1504 // Add access to a wrapped mesh
1505 theGen->AddMeshAccessorMethod( *cmd );
1506 // Add access to a wrapped algorithm
1507 theGen->AddAlgoAccessorMethod( *cmd );
1510 // forget previous hypothesis modifications
1511 myArgCommands.clear();
1512 myUnknownCommands.clear();
1515 //================================================================================
1517 * \brief clear creation, arg and unkown commands
1519 //================================================================================
1521 void _pyHypothesis::ClearAllCommands()
1523 GetCreationCmd()->Clear();
1524 list<Handle(_pyCommand)>::iterator cmd = myArgCommands.begin();
1525 for ( ; cmd != myArgCommands.end(); ++cmd )
1527 cmd = myUnknownCommands.begin();
1528 for ( ; cmd != myUnknownCommands.end(); ++cmd )
1533 //================================================================================
1535 * \brief Assign fields of theOther to me except myIsWrapped
1537 //================================================================================
1539 void _pyHypothesis::Assign( const Handle(_pyHypothesis)& theOther,
1540 const _pyID& theMesh )
1542 myIsWrapped = false;
1545 // myCreationCmd = theOther->myCreationCmd;
1546 myIsAlgo = theOther->myIsAlgo;
1547 myGeom = theOther->myGeom;
1548 myType2CreationMethod = theOther->myType2CreationMethod;
1549 myArgs = theOther->myArgs;
1550 myArgMethods = theOther->myArgMethods;
1551 myNbArgsByMethod = theOther->myNbArgsByMethod;
1552 myArgCommands = theOther->myArgCommands;
1553 myUnknownCommands = theOther->myUnknownCommands;
1556 //================================================================================
1558 * \brief Remember hypothesis parameter values
1559 * \param theCommand - The called hypothesis method
1561 //================================================================================
1563 void _pyComplexParamHypo::Process( const Handle(_pyCommand)& theCommand)
1565 if( theCommand->GetMethod() == "SetLength" )
1567 // NOW it becomes OBSOLETE
1568 // ex: hyp.SetLength(start, 1)
1569 // hyp.SetLength(end, 0)
1570 ASSERT(( theCommand->GetArg( 2 ).IsIntegerValue() ));
1571 int i = 2 - theCommand->GetArg( 2 ).IntegerValue();
1572 while ( myArgs.Length() < i )
1573 myArgs.Append( "[]" );
1574 myArgs( i ) = theCommand->GetArg( 1 ); // arg value
1575 myArgCommands.push_back( theCommand );
1579 _pyHypothesis::Process( theCommand );
1582 //================================================================================
1584 * \brief Clear SetObjectEntry() as it is called by methods of Mesh_Segment
1586 //================================================================================
1588 void _pyComplexParamHypo::Flush()
1592 list < Handle(_pyCommand) >::iterator cmd = myUnknownCommands.begin();
1593 for ( ; cmd != myUnknownCommands.end(); ++cmd )
1594 if ((*cmd)->GetMethod() == "SetObjectEntry" )
1599 //================================================================================
1601 * \brief Convert methods of 1D hypotheses to my own methods
1602 * \param theCommand - The called hypothesis method
1604 //================================================================================
1606 void _pyLayerDistributionHypo::Process( const Handle(_pyCommand)& theCommand)
1608 if ( theCommand->GetMethod() != "SetLayerDistribution" )
1611 _pyID newName; // name for 1D hyp = "HypType" + "_Distribution"
1613 const _pyID& hyp1dID = theCommand->GetArg( 1 );
1614 Handle(_pyHypothesis) hyp1d = theGen->FindHyp( hyp1dID );
1615 if ( hyp1d.IsNull() ) // apparently hypId changed at study restoration
1617 else if ( !my1dHyp.IsNull() && hyp1dID != my1dHyp->GetID() ) {
1618 // 1D hypo is already set, so distribution changes and the old
1619 // 1D hypo is thrown away
1620 my1dHyp->ClearAllCommands();
1623 if ( my1dHyp.IsNull() )
1624 return; // something wrong :(
1626 // make a new name for 1D hyp = "HypType" + "_Distribution"
1627 if ( my1dHyp->GetCreationCmd()->GetMethod() == "CreateHypothesis" ) {
1628 // not yet converted creation cmd
1629 TCollection_AsciiString hypTypeQuoted = my1dHyp->GetCreationCmd()->GetArg(1);
1630 TCollection_AsciiString hypType = hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
1631 newName = hypType + "_Distribution";
1632 my1dHyp->GetCreationCmd()->SetResultValue( newName );
1635 // already converted creation cmd
1636 newName = my1dHyp->GetCreationCmd()->GetResultValue();
1639 // as creation of 1D hyp was written later then it's edition,
1640 // we need to find all it's edition calls and process them
1641 list< Handle(_pyCommand) >& cmds = theGen->GetCommands();
1642 list< Handle(_pyCommand) >::iterator cmdIt = cmds.begin();
1643 for ( ; cmdIt != cmds.end(); ++cmdIt ) {
1644 const _pyID& objID = (*cmdIt)->GetObject();
1645 if ( objID == hyp1dID ) {
1646 my1dHyp->Process( *cmdIt );
1647 my1dHyp->GetCreationCmd()->AddDependantCmd( *cmdIt );
1648 ( *cmdIt )->SetObject( newName );
1651 if ( !myArgCommands.empty() )
1652 myArgCommands.front()->Clear();
1653 theCommand->SetArg( 1, newName );
1654 myArgCommands.push_back( theCommand );
1655 // copy hyp1d's creation method and args
1656 // myCreationMethod = hyp1d->GetCreationMethod();
1657 // myArgs = hyp1d->GetArgs();
1658 // // make them cleared at conversion
1659 // myArgCommands = hyp1d->GetArgCommands();
1661 // // to be cleared at convertion only
1662 // myArgCommands.push_back( theCommand );
1665 //================================================================================
1668 * \param theAdditionCmd - command to be converted
1669 * \param theMesh - mesh instance
1670 * \retval bool - status
1672 //================================================================================
1674 bool _pyLayerDistributionHypo::Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
1675 const _pyID& theMesh)
1677 myIsWrapped = false;
1679 if ( my1dHyp.IsNull() )
1682 // set "SetLayerDistribution()" after addition cmd
1683 theAdditionCmd->AddDependantCmd( myArgCommands.front() );
1685 _pyID geom = theAdditionCmd->GetArg( 1 );
1687 my1dHyp->SetMesh( theMesh );
1688 if ( !my1dHyp->Addition2Creation( theAdditionCmd, theMesh ))
1691 // clear "SetLayerDistribution()" cmd
1692 myArgCommands.front()->Clear();
1694 // Convert my creation => me = RadialPrismAlgo.Get3DHypothesis()
1696 // find RadialPrism algo created on <geom> for theMesh
1697 Handle(_pyHypothesis) algo = theGen->FindAlgo( geom, theMesh, this );
1698 if ( !algo.IsNull() ) {
1699 GetCreationCmd()->SetObject( algo->GetID() );
1700 GetCreationCmd()->SetMethod( "Get3DHypothesis" );
1701 GetCreationCmd()->RemoveArgs();
1702 theAdditionCmd->AddDependantCmd( GetCreationCmd() );
1708 //================================================================================
1712 //================================================================================
1714 void _pyLayerDistributionHypo::Flush()
1716 //my1dHyp.Nullify();
1717 //_pyHypothesis::Flush();
1720 //================================================================================
1722 * \brief additionally to Addition2Creation, clears SetDistrType() command
1723 * \param theCmd - AddHypothesis() command
1724 * \param theMesh - mesh to which a hypothesis is added
1725 * \retval bool - convertion result
1727 //================================================================================
1729 bool _pyNumberOfSegmentsHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
1730 const _pyID& theMesh)
1732 if ( IsWrappable( theMesh ) && myArgs.Length() > 1 ) {
1733 // scale factor (2-nd arg) is provided: clear SetDistrType(1) command
1734 bool scaleDistrType = false;
1735 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnknownCommands.rbegin();
1736 for ( ; cmd != myUnknownCommands.rend(); ++cmd ) {
1737 if ( (*cmd)->GetMethod() == "SetDistrType" ) {
1738 if ( (*cmd)->GetArg( 1 ) == "1" ) {
1739 scaleDistrType = true;
1742 else if ( !scaleDistrType ) {
1743 // distribution type changed: remove scale factor from args
1744 myArgs.Remove( 2, myArgs.Length() );
1750 return _pyHypothesis::Addition2Creation( theCmd, theMesh );
1753 //================================================================================
1755 * \brief remove repeated commands defining distribution
1757 //================================================================================
1759 void _pyNumberOfSegmentsHyp::Flush()
1761 // find number of the last SetDistrType() command
1762 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnknownCommands.rbegin();
1763 int distrTypeNb = 0;
1764 for ( ; !distrTypeNb && cmd != myUnknownCommands.rend(); ++cmd )
1765 if ( (*cmd)->GetMethod() == "SetDistrType" )
1766 distrTypeNb = (*cmd)->GetOrderNb();
1767 else if (IsWrapped() && (*cmd)->GetMethod() == "SetObjectEntry" )
1770 // clear commands before the last SetDistrType()
1771 list<Handle(_pyCommand)> * cmds[2] = { &myArgCommands, &myUnknownCommands };
1772 for ( int i = 0; i < 2; ++i ) {
1773 set<TCollection_AsciiString> uniqueMethods;
1774 list<Handle(_pyCommand)> & cmdList = *cmds[i];
1775 for ( cmd = cmdList.rbegin(); cmd != cmdList.rend(); ++cmd )
1777 bool clear = ( (*cmd)->GetOrderNb() < distrTypeNb );
1778 const TCollection_AsciiString& method = (*cmd)->GetMethod();
1779 if ( !clear || method == "SetNumberOfSegments" ) {
1780 bool isNewInSet = uniqueMethods.insert( method ).second;
1781 clear = !isNewInSet;
1790 //================================================================================
1792 * \brief Convert the command adding "SegmentLengthAroundVertex" to mesh
1793 * into regular1D.LengthNearVertex( length, vertex )
1794 * \param theCmd - The command like mesh.AddHypothesis( vertex, SegmentLengthAroundVertex )
1795 * \param theMesh - The mesh needing this hypo
1796 * \retval bool - false if the command cant be converted
1798 //================================================================================
1800 bool _pySegmentLengthAroundVertexHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
1801 const _pyID& theMeshID)
1803 if ( IsWrappable( theMeshID )) {
1805 _pyID vertex = theCmd->GetArg( 1 );
1807 // the problem here is that segment algo will not be found
1808 // by pyHypothesis::Addition2Creation() for <vertex>, so we try to find
1809 // geometry where segment algorithm is assigned
1810 Handle(_pyHypothesis) algo;
1811 _pyID geom = vertex;
1812 while ( algo.IsNull() && !geom.IsEmpty()) {
1813 // try to find geom as a father of <vertex>
1814 geom = FatherID( geom );
1815 algo = theGen->FindAlgo( geom, theMeshID, this );
1817 if ( algo.IsNull() )
1818 return false; // also possible to find geom as brother of veretex...
1819 // set geom instead of vertex
1820 theCmd->SetArg( 1, geom );
1822 // set vertex as a second arg
1823 if ( myArgs.Length() < 1) myArgs.Append( "1" ); // :(
1824 myArgs.Append( vertex );
1826 // mesh.AddHypothesis(vertex, SegmentLengthAroundVertex) -->
1827 // theMeshID.LengthNearVertex( length, vertex )
1828 return _pyHypothesis::Addition2Creation( theCmd, theMeshID );
1833 //================================================================================
1835 * \brief _pyAlgorithm constructor
1836 * \param theCreationCmd - The command like "algo = smeshgen.CreateHypothesis(type,lib)"
1838 //================================================================================
1840 _pyAlgorithm::_pyAlgorithm(const Handle(_pyCommand)& theCreationCmd)
1841 : _pyHypothesis( theCreationCmd )
1846 //================================================================================
1848 * \brief Convert the command adding an algorithm to mesh
1849 * \param theCmd - The command like mesh.AddHypothesis( geom, algo )
1850 * \param theMesh - The mesh needing this algo
1851 * \retval bool - false if the command cant be converted
1853 //================================================================================
1855 bool _pyAlgorithm::Addition2Creation( const Handle(_pyCommand)& theCmd,
1856 const _pyID& theMeshID)
1858 // mesh.AddHypothesis(geom,algo) --> theMeshID.myCreationMethod()
1859 if ( _pyHypothesis::Addition2Creation( theCmd, theMeshID )) {
1860 theGen->SetAccessorMethod( GetID(), "GetAlgorithm()" );
1866 //================================================================================
1868 * \brief Return starting position of a part of python command
1869 * \param thePartIndex - The index of command part
1870 * \retval int - Part position
1872 //================================================================================
1874 int _pyCommand::GetBegPos( int thePartIndex )
1878 if ( myBegPos.Length() < thePartIndex )
1880 return myBegPos( thePartIndex );
1883 //================================================================================
1885 * \brief Store starting position of a part of python command
1886 * \param thePartIndex - The index of command part
1887 * \param thePosition - Part position
1889 //================================================================================
1891 void _pyCommand::SetBegPos( int thePartIndex, int thePosition )
1893 while ( myBegPos.Length() < thePartIndex )
1894 myBegPos.Append( UNKNOWN );
1895 myBegPos( thePartIndex ) = thePosition;
1898 //================================================================================
1900 * \brief Returns whitespace symbols at the line beginning
1901 * \retval TCollection_AsciiString - result
1903 //================================================================================
1905 TCollection_AsciiString _pyCommand::GetIndentation()
1908 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
1909 GetWord( myString, end, true );
1911 end = GetBegPos( RESULT_IND );
1912 return myString.SubString( 1, end - 1 );
1915 //================================================================================
1917 * \brief Return substring of python command looking like ResultValue = Obj.Meth()
1918 * \retval const TCollection_AsciiString & - ResultValue substring
1920 //================================================================================
1922 const TCollection_AsciiString & _pyCommand::GetResultValue()
1924 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
1926 int begPos = myString.Location( "=", 1, Length() );
1928 myRes = GetWord( myString, begPos, false );
1931 SetBegPos( RESULT_IND, begPos );
1936 //================================================================================
1938 * \brief Return number of python command result value ResultValue = Obj.Meth()
1941 //================================================================================
1943 const int _pyCommand::GetNbResultValues()
1947 int endPos = myString.Location( "=", 1, Length() );
1948 TCollection_AsciiString str = "";
1949 while ( begPos < endPos) {
1950 str = GetWord( myString, begPos, true );
1951 begPos = begPos+ str.Length();
1958 //================================================================================
1960 * \brief Return substring of python command looking like
1961 * ResultValue1 , ResultValue1,... = Obj.Meth() with res index
1962 * \retval const TCollection_AsciiString & - ResultValue with res index substring
1964 //================================================================================
1965 const TCollection_AsciiString & _pyCommand::GetResultValue(int res)
1969 int endPos = myString.Location( "=", 1, Length() );
1970 while ( begPos < endPos) {
1971 myRes = GetWord( myString, begPos, true );
1972 begPos = begPos + myRes.Length();
1975 myRes.RemoveAll('[');myRes.RemoveAll(']');
1981 return theEmptyString;
1984 //================================================================================
1986 * \brief Return substring of python command looking like ResVal = Object.Meth()
1987 * \retval const TCollection_AsciiString & - Object substring
1989 //================================================================================
1991 const TCollection_AsciiString & _pyCommand::GetObject()
1993 if ( GetBegPos( OBJECT_IND ) == UNKNOWN )
1996 int begPos = GetBegPos( RESULT_IND ) + myRes.Length();
1998 begPos = myString.Location( "=", 1, Length() ) + 1;
1999 // is '=' in the string argument (for example, name) or not
2000 int nb1 = 0; // number of ' character at the left of =
2001 int nb2 = 0; // number of " character at the left of =
2002 for ( int i = 1; i < begPos-1; i++ ) {
2003 if ( myString.Value( i )=='\'' )
2005 else if ( myString.Value( i )=='"' )
2008 // if number of ' or " is not divisible by 2,
2009 // then get an object at the start of the command
2010 if ( nb1 % 2 != 0 || nb2 % 2 != 0 )
2013 myObj = GetWord( myString, begPos, true );
2014 // check if object is complex,
2015 // so far consider case like "smesh.smesh.Method()"
2016 if ( int bracketPos = myString.Location( "(", begPos, Length() )) {
2017 //if ( bracketPos==0 ) bracketPos = Length();
2018 int dotPos = begPos+myObj.Length();
2019 while ( dotPos+1 < bracketPos ) {
2020 if ( int pos = myString.Location( ".", dotPos+1, bracketPos ))
2025 if ( dotPos > begPos+myObj.Length() )
2026 myObj = myString.SubString( begPos, dotPos-1 );
2029 SetBegPos( OBJECT_IND, begPos );
2035 //================================================================================
2037 * \brief Return substring of python command looking like ResVal = Obj.Method()
2038 * \retval const TCollection_AsciiString & - Method substring
2040 //================================================================================
2042 const TCollection_AsciiString & _pyCommand::GetMethod()
2044 if ( GetBegPos( METHOD_IND ) == UNKNOWN )
2047 int begPos = GetBegPos( OBJECT_IND ) + myObj.Length();
2048 bool forward = true;
2050 begPos = myString.Location( "(", 1, Length() ) - 1;
2054 myMeth = GetWord( myString, begPos, forward );
2055 SetBegPos( METHOD_IND, begPos );
2061 //================================================================================
2063 * \brief Return substring of python command looking like ResVal = Obj.Meth(Arg1,...)
2064 * \retval const TCollection_AsciiString & - Arg<index> substring
2066 //================================================================================
2068 const TCollection_AsciiString & _pyCommand::GetArg( int index )
2070 if ( GetBegPos( ARG1_IND ) == UNKNOWN )
2073 int begPos = GetBegPos( METHOD_IND ) + myMeth.Length();
2075 begPos = myString.Location( "(", 1, Length() ) + 1;
2077 int i = 0, prevLen = 0, nbNestings = 0;
2078 while ( begPos != EMPTY ) {
2080 if( myString.Value( begPos ) == '(' )
2082 // check if we are looking at the closing parenthesis
2083 while ( begPos <= Length() && isspace( myString.Value( begPos )))
2085 if ( begPos > Length() )
2087 if ( myString.Value( begPos ) == ')' ) {
2089 if( nbNestings == 0 )
2092 myArgs.Append( GetWord( myString, begPos, true, true ));
2093 SetBegPos( ARG1_IND + i, begPos );
2094 prevLen = myArgs.Last().Length();
2096 myArgs.Remove( myArgs.Length() ); // no more args
2100 if ( myArgs.Length() < index )
2101 return theEmptyString;
2102 return myArgs( index );
2105 //================================================================================
2107 * \brief Check if char is a word part
2108 * \param c - The character to check
2109 * \retval bool - The check result
2111 //================================================================================
2113 static inline bool isWord(const char c, const bool dotIsWord)
2116 !isspace(c) && c != ',' && c != '=' && c != ')' && c != '(' && ( dotIsWord || c != '.');
2119 //================================================================================
2121 * \brief Looks for a word in the string and returns word's beginning
2122 * \param theString - The input string
2123 * \param theStartPos - The position to start the search, returning word's beginning
2124 * \param theForward - The search direction
2125 * \retval TCollection_AsciiString - The found word
2127 //================================================================================
2129 TCollection_AsciiString _pyCommand::GetWord( const TCollection_AsciiString & theString,
2131 const bool theForward,
2132 const bool dotIsWord )
2134 int beg = theStartPos, end = theStartPos;
2135 theStartPos = EMPTY;
2136 if ( beg < 1 || beg > theString.Length() )
2137 return theEmptyString;
2139 if ( theForward ) { // search forward
2141 while ( beg <= theString.Length() && !isWord( theString.Value( beg ), dotIsWord))
2143 if ( beg > theString.Length() )
2144 return theEmptyString; // no word found
2147 char begChar = theString.Value( beg );
2148 if ( begChar == '"' || begChar == '\'' || begChar == '[') {
2149 char endChar = ( begChar == '[' ) ? ']' : begChar;
2150 // end is at the corresponding quoting mark or bracket
2151 while ( end < theString.Length() &&
2152 ( theString.Value( end ) != endChar || theString.Value( end-1 ) == '\\'))
2156 while ( end <= theString.Length() && isWord( theString.Value( end ), dotIsWord))
2161 else { // search backward
2163 while ( end > 0 && !isWord( theString.Value( end ), dotIsWord))
2166 return theEmptyString; // no word found
2168 char endChar = theString.Value( end );
2169 if ( endChar == '"' || endChar == '\'' ) {
2170 // beg is at the corresponding quoting mark
2172 ( theString.Value( beg ) != endChar || theString.Value( beg-1 ) == '\\'))
2176 while ( beg > 0 && isWord( theString.Value( beg ), dotIsWord))
2182 //cout << theString << " ---- " << beg << " - " << end << endl;
2183 return theString.SubString( beg, end );
2186 //================================================================================
2188 * \brief Look for position where not space char is
2189 * \param theString - The string
2190 * \param thePos - The position to search from and which returns result
2191 * \retval bool - false if there are only space after thePos in theString
2195 //================================================================================
2197 bool _pyCommand::SkipSpaces( const TCollection_AsciiString & theString, int & thePos )
2199 if ( thePos < 1 || thePos > theString.Length() )
2202 while ( thePos <= theString.Length() && isspace( theString.Value( thePos )))
2205 return thePos <= theString.Length();
2208 //================================================================================
2210 * \brief Modify a part of the command
2211 * \param thePartIndex - The index of the part
2212 * \param thePart - The new part string
2213 * \param theOldPart - The old part
2215 //================================================================================
2217 void _pyCommand::SetPart(int thePartIndex, const TCollection_AsciiString& thePart,
2218 TCollection_AsciiString& theOldPart)
2220 int pos = GetBegPos( thePartIndex );
2221 if ( pos <= Length() && theOldPart != thePart)
2223 TCollection_AsciiString seperator;
2225 pos = GetBegPos( thePartIndex + 1 );
2226 if ( pos < 1 ) return;
2227 switch ( thePartIndex ) {
2228 case RESULT_IND: seperator = " = "; break;
2229 case OBJECT_IND: seperator = "."; break;
2230 case METHOD_IND: seperator = "()"; break;
2234 myString.Remove( pos, theOldPart.Length() );
2235 if ( !seperator.IsEmpty() )
2236 myString.Insert( pos , seperator );
2237 myString.Insert( pos, thePart );
2238 // update starting positions of the following parts
2239 int posDelta = thePart.Length() + seperator.Length() - theOldPart.Length();
2240 for ( int i = thePartIndex + 1; i <= myBegPos.Length(); ++i ) {
2241 if ( myBegPos( i ) > 0 )
2242 myBegPos( i ) += posDelta;
2244 theOldPart = thePart;
2248 //================================================================================
2250 * \brief Set agrument
2251 * \param index - The argument index, it counts from 1
2252 * \param theArg - The argument string
2254 //================================================================================
2256 void _pyCommand::SetArg( int index, const TCollection_AsciiString& theArg)
2259 int argInd = ARG1_IND + index - 1;
2260 int pos = GetBegPos( argInd );
2261 if ( pos < 1 ) // no index-th arg exist, append inexistent args
2263 // find a closing parenthesis
2264 if ( GetNbArgs() != 0 && index <= GetNbArgs() ) {
2265 int lastArgInd = GetNbArgs();
2266 pos = GetBegPos( ARG1_IND + lastArgInd - 1 ) + GetArg( lastArgInd ).Length();
2267 while ( pos > 0 && pos <= Length() && myString.Value( pos ) != ')' )
2272 while ( pos > 0 && myString.Value( pos ) != ')' )
2275 if ( pos < 1 || myString.Value( pos ) != ')' ) { // no parentheses at all
2279 while ( myArgs.Length() < index ) {
2280 if ( myArgs.Length() )
2281 myString.Insert( pos++, "," );
2282 myArgs.Append("None");
2283 myString.Insert( pos, myArgs.Last() );
2284 SetBegPos( ARG1_IND + myArgs.Length() - 1, pos );
2285 pos += myArgs.Last().Length();
2288 SetPart( argInd, theArg, myArgs( index ));
2291 //================================================================================
2293 * \brief Empty arg list
2295 //================================================================================
2297 void _pyCommand::RemoveArgs()
2299 if ( int pos = myString.Location( '(', 1, Length() ))
2300 myString.Trunc( pos );
2303 if ( myBegPos.Length() >= ARG1_IND )
2304 myBegPos.Remove( ARG1_IND, myBegPos.Length() );
2307 //================================================================================
2309 * \brief Set dependent commands after this one
2311 //================================================================================
2313 bool _pyCommand::SetDependentCmdsAfter() const
2315 bool orderChanged = false;
2316 list< Handle(_pyCommand)>::const_reverse_iterator cmd = myDependentCmds.rbegin();
2317 for ( ; cmd != myDependentCmds.rend(); ++cmd ) {
2318 if ( (*cmd)->GetOrderNb() < GetOrderNb() ) {
2319 orderChanged = true;
2320 theGen->SetCommandAfter( *cmd, this );
2321 (*cmd)->SetDependentCmdsAfter();
2324 return orderChanged;
2326 //================================================================================
2328 * \brief Insert accessor method after theObjectID
2329 * \param theObjectID - id of the accessed object
2330 * \param theAcsMethod - name of the method giving access to the object
2331 * \retval bool - false if theObjectID is not found in the command string
2333 //================================================================================
2335 bool _pyCommand::AddAccessorMethod( _pyID theObjectID, const char* theAcsMethod )
2337 if ( !theAcsMethod )
2339 // start object search from the object, i.e. ignore result
2341 int beg = GetBegPos( OBJECT_IND );
2342 if ( beg < 1 || beg > Length() )
2345 while (( beg = myString.Location( theObjectID, beg, Length() )))
2347 // check that theObjectID is not just a part of a longer ID
2348 int afterEnd = beg + theObjectID.Length();
2349 Standard_Character c = myString.Value( afterEnd );
2350 if ( !isalnum( c ) && c != ':' ) {
2351 // check if accessor method already present
2353 myString.Location( (char*) theAcsMethod, afterEnd, Length() ) != afterEnd+1) {
2355 int oldLen = Length();
2356 myString.Insert( afterEnd, (char*) theAcsMethod );
2357 myString.Insert( afterEnd, "." );
2358 // update starting positions of the parts following the modified one
2359 int posDelta = Length() - oldLen;
2360 for ( int i = 1; i <= myBegPos.Length(); ++i ) {
2361 if ( myBegPos( i ) > afterEnd )
2362 myBegPos( i ) += posDelta;
2367 beg = afterEnd; // is a part - next search
2372 //================================================================================
2374 * \brief Return method name giving access to an interaface object wrapped by python class
2375 * \retval const char* - method name
2377 //================================================================================
2379 const char* _pyObject::AccessorMethod() const
2383 //================================================================================
2385 * \brief Return ID of a father
2387 //================================================================================
2389 _pyID _pyObject::FatherID(const _pyID & childID)
2391 int colPos = childID.SearchFromEnd(':');
2393 return childID.SubString( 1, colPos-1 );
2397 //================================================================================
2399 * \brief SelfEraser erases creation command if no more it's commands invoked
2401 //================================================================================
2403 void _pySelfEraser::Flush()
2405 if ( GetNbCalls() == 0 )
2406 GetCreationCmd()->Clear();
2409 //================================================================================
2411 * \brief count invoked commands
2413 //================================================================================
2415 void _pySubMesh::Process( const Handle(_pyCommand)& theCommand )
2417 _pyObject::Process(theCommand); // count calls of Process()
2418 GetCreationCmd()->AddDependantCmd( theCommand );
2421 //================================================================================
2423 * \brief Clear creatin command if no commands invoked
2425 //================================================================================
2427 void _pySubMesh::Flush()
2429 if ( GetNbCalls() == 0 ) // move to the end of all commands
2430 theGen->GetLastCommand()->AddDependantCmd( GetCreationCmd() );
2431 else if ( !myCreator.IsNull() )
2432 // move to be just after creator
2433 myCreator->GetCreationCmd()->AddDependantCmd( GetCreationCmd() );