1 // Copyright (C) 2007-2011 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
23 // 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 "SMESH_Filter_i.hxx"
35 #include <Resource_DataMapOfAsciiStringAsciiString.hxx>
36 #include <Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString.hxx>
38 #include "SMESH_Gen_i.hxx"
39 /* SALOME headers that include CORBA headers that include windows.h
40 * that defines GetObject symbol as GetObjectA should stand before SALOME headers
41 * that declare methods named GetObject - to apply the same rules of GetObject renaming
42 * and thus to avoid mess with GetObject symbol on Windows */
44 #include <LDOMParser.hxx>
53 IMPLEMENT_STANDARD_HANDLE (_pyObject ,Standard_Transient);
54 IMPLEMENT_STANDARD_HANDLE (_pyCommand ,Standard_Transient);
55 IMPLEMENT_STANDARD_HANDLE (_pyHypothesisReader,Standard_Transient);
56 IMPLEMENT_STANDARD_HANDLE (_pyGen ,_pyObject);
57 IMPLEMENT_STANDARD_HANDLE (_pyMesh ,_pyObject);
58 IMPLEMENT_STANDARD_HANDLE (_pySubMesh ,_pyObject);
59 IMPLEMENT_STANDARD_HANDLE (_pyMeshEditor ,_pyObject);
60 IMPLEMENT_STANDARD_HANDLE (_pyHypothesis ,_pyObject);
61 IMPLEMENT_STANDARD_HANDLE (_pySelfEraser ,_pyObject);
62 IMPLEMENT_STANDARD_HANDLE (_pyGroup ,_pyObject);
63 IMPLEMENT_STANDARD_HANDLE (_pyFilter ,_pyObject);
64 IMPLEMENT_STANDARD_HANDLE (_pyAlgorithm ,_pyHypothesis);
65 IMPLEMENT_STANDARD_HANDLE (_pyComplexParamHypo,_pyHypothesis);
66 IMPLEMENT_STANDARD_HANDLE (_pyNumberOfSegmentsHyp,_pyHypothesis);
68 IMPLEMENT_STANDARD_RTTIEXT(_pyObject ,Standard_Transient);
69 IMPLEMENT_STANDARD_RTTIEXT(_pyCommand ,Standard_Transient);
70 IMPLEMENT_STANDARD_RTTIEXT(_pyHypothesisReader,Standard_Transient);
71 IMPLEMENT_STANDARD_RTTIEXT(_pyGen ,_pyObject);
72 IMPLEMENT_STANDARD_RTTIEXT(_pyMesh ,_pyObject);
73 IMPLEMENT_STANDARD_RTTIEXT(_pySubMesh ,_pyObject);
74 IMPLEMENT_STANDARD_RTTIEXT(_pyMeshEditor ,_pyObject);
75 IMPLEMENT_STANDARD_RTTIEXT(_pyHypothesis ,_pyObject);
76 IMPLEMENT_STANDARD_RTTIEXT(_pySelfEraser ,_pyObject);
77 IMPLEMENT_STANDARD_RTTIEXT(_pyGroup ,_pyObject);
78 IMPLEMENT_STANDARD_RTTIEXT(_pyFilter ,_pyObject);
79 IMPLEMENT_STANDARD_RTTIEXT(_pyAlgorithm ,_pyHypothesis);
80 IMPLEMENT_STANDARD_RTTIEXT(_pyComplexParamHypo,_pyHypothesis);
81 IMPLEMENT_STANDARD_RTTIEXT(_pyNumberOfSegmentsHyp,_pyHypothesis);
82 IMPLEMENT_STANDARD_RTTIEXT(_pyLayerDistributionHypo,_pyHypothesis);
83 IMPLEMENT_STANDARD_RTTIEXT(_pySegmentLengthAroundVertexHyp,_pyHypothesis);
86 using SMESH::TPythonDump;
89 * \brief Container of commands into which the initial script is split.
90 * It also contains data coresponding to SMESH_Gen contents
92 static Handle(_pyGen) theGen;
94 static TCollection_AsciiString theEmptyString;
96 //#define DUMP_CONVERSION
98 #if !defined(_DEBUG_) && defined(DUMP_CONVERSION)
99 #undef DUMP_CONVERSION
105 //================================================================================
107 * \brief Set of TCollection_AsciiString initialized by C array of C strings
109 //================================================================================
111 struct TStringSet: public set<TCollection_AsciiString>
114 * \brief Filling. The last string must be ""
116 void Insert(const char* names[]) {
117 for ( int i = 0; names[i][0] ; ++i )
118 insert( (char*) names[i] );
121 * \brief Check if a string is in
123 bool Contains(const TCollection_AsciiString& name ) {
124 return find( name ) != end();
128 //================================================================================
130 * \brief Returns a mesh by object
132 //================================================================================
134 Handle(_pyMesh) ObjectToMesh( const Handle( _pyObject )& obj )
138 if ( obj->IsKind( STANDARD_TYPE( _pyMesh )))
139 return Handle(_pyMesh)::DownCast( obj );
140 else if ( obj->IsKind( STANDARD_TYPE( _pySubMesh )))
141 return Handle(_pySubMesh)::DownCast( obj )->GetMesh();
142 else if ( obj->IsKind( STANDARD_TYPE( _pyGroup )))
143 return Handle(_pyGroup)::DownCast( obj )->GetMesh();
145 return Handle(_pyMesh)();
148 //================================================================================
150 * \brief Check if objects used as args have been created by previous commands
152 //================================================================================
154 void CheckObjectPresence( const Handle(_pyCommand)& cmd, set<_pyID> & presentObjects)
156 for ( int iArg = cmd->GetNbArgs(); iArg; --iArg )
158 const _pyID& arg = cmd->GetArg( iArg );
159 if ( arg.IsEmpty() || arg.Value( 1 ) == '"' || arg.Value( 1 ) == '\'' )
161 list< _pyID > idList = cmd->GetStudyEntries( arg );
162 list< _pyID >::iterator id = idList.begin();
163 for ( ; id != idList.end(); ++id )
164 if ( !theGen->IsGeomObject( *id ) && !presentObjects.count( *id ))
167 cmd->GetString() += " ### " ;
168 cmd->GetString() += *id + " has not been yet created";
172 const _pyID& obj = cmd->GetObject();
173 if ( !obj.IsEmpty() && cmd->IsStudyEntry( obj ) && !presentObjects.count( obj ))
176 cmd->GetString() += " ### not created object" ;
178 const _pyID& result = cmd->GetResultValue();
179 if ( result.IsEmpty() || result.Value( 1 ) == '"' || result.Value( 1 ) == '\'' )
181 list< _pyID > idList = cmd->GetStudyEntries( result );
182 list< _pyID >::iterator id = idList.begin();
183 for ( ; id != idList.end(); ++id )
184 presentObjects.insert( *id );
188 //================================================================================
190 * \brief Convert python script using commands of smesh.py
191 * \param theScript - Input script
192 * \retval TCollection_AsciiString - Convertion result
193 * \param theToKeepAllCommands - to keep all commands or
194 * to exclude commands relating to objects removed from study
196 * Class SMESH_2smeshpy declared in SMESH_PythonDump.hxx
198 //================================================================================
200 TCollection_AsciiString
201 SMESH_2smeshpy::ConvertScript(const TCollection_AsciiString& theScript,
202 Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod,
203 Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
204 SALOMEDS::Study_ptr& theStudy,
205 const bool theToKeepAllCommands)
207 theGen = new _pyGen( theEntry2AccessorMethod, theObjectNames, theStudy, theToKeepAllCommands );
209 // split theScript into separate commands
211 SMESH_NoteBook * aNoteBook = new SMESH_NoteBook();
213 int from = 1, end = theScript.Length(), to;
214 while ( from < end && ( to = theScript.Location( "\n", from, end )))
217 // cut out and store a command
218 aNoteBook->AddCommand( theScript.SubString( from, to - 1 ));
222 aNoteBook->ReplaceVariables();
224 TCollection_AsciiString aNoteScript = aNoteBook->GetResultScript();
228 // split theScript into separate commands
229 from = 1, end = aNoteScript.Length();
230 while ( from < end && ( to = aNoteScript.Location( "\n", from, end )))
233 // cut out and store a command
234 theGen->AddCommand( aNoteScript.SubString( from, to - 1 ));
240 #ifdef DUMP_CONVERSION
241 MESSAGE_BEGIN ( std::endl << " ######## RESULT ######## " << std::endl<< std::endl );
244 // clean commmands of removed objects depending on myIsPublished flag
245 theGen->ClearCommands();
247 // reorder commands after conversion
248 list< Handle(_pyCommand) >::iterator cmd;
251 orderChanges = false;
252 for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
253 if ( (*cmd)->SetDependentCmdsAfter() )
255 } while ( orderChanges );
257 // concat commands back into a script
258 TCollection_AsciiString aScript, aPrevCmd;
259 set<_pyID> createdObjects;
260 for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
262 #ifdef DUMP_CONVERSION
263 MESSAGE_ADD ( "## COM " << (*cmd)->GetOrderNb() << ": "<< (*cmd)->GetString() << std::endl );
265 if ( !(*cmd)->IsEmpty() && aPrevCmd != (*cmd)->GetString()) {
266 CheckObjectPresence( *cmd, createdObjects );
267 aPrevCmd = (*cmd)->GetString();
280 //================================================================================
282 * \brief _pyGen constructor
284 //================================================================================
286 _pyGen::_pyGen(Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod,
287 Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
288 SALOMEDS::Study_ptr& theStudy,
289 const bool theToKeepAllCommands)
290 : _pyObject( new _pyCommand( "", 0 )),
292 myID2AccessorMethod( theEntry2AccessorMethod ),
293 myObjectNames( theObjectNames ),
295 myToKeepAllCommands( theToKeepAllCommands ),
296 myStudy( SALOMEDS::Study::_duplicate( theStudy )),
297 myGeomIDNb(0), myGeomIDIndex(-1)
299 // make that GetID() to return TPythonDump::SMESHGenName()
300 GetCreationCmd()->Clear();
301 GetCreationCmd()->GetString() = TPythonDump::SMESHGenName();
302 GetCreationCmd()->GetString() += "=";
304 // Find 1st digit of study entry by which a GEOM object differs from a SMESH object
305 if ( !theObjectNames.IsEmpty() && !CORBA::is_nil( theStudy ))
309 SALOMEDS::SComponent_var geomComp = theStudy->FindComponent("GEOM");
310 if ( geomComp->_is_nil() ) return;
311 CORBA::String_var entry = geomComp->GetID();
314 // find a SMESH entry
316 Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString e2n( theObjectNames );
317 for ( ; e2n.More() && smeshID.IsEmpty(); e2n.Next() )
318 if ( _pyCommand::IsStudyEntry( e2n.Key() ))
321 // find 1st difference between smeshID and geomID
322 if ( !geomID.IsEmpty() && !smeshID.IsEmpty() )
323 for ( int i = 1; i <= geomID.Length() && i <= smeshID.Length(); ++i )
324 if ( geomID.Value( i ) != smeshID.Value( i ))
326 myGeomIDNb = geomID.Value( i );
332 //================================================================================
334 * \brief name of SMESH_Gen in smesh.py
336 //================================================================================
338 const char* _pyGen::AccessorMethod() const
340 return SMESH_2smeshpy::GenName();
343 //================================================================================
345 * \brief Convert a command using a specific converter
346 * \param theCommand - the command to convert
348 //================================================================================
350 Handle(_pyCommand) _pyGen::AddCommand( const TCollection_AsciiString& theCommand)
352 // store theCommand in the sequence
353 myCommands.push_back( new _pyCommand( theCommand, ++myNbCommands ));
355 Handle(_pyCommand) aCommand = myCommands.back();
356 #ifdef DUMP_CONVERSION
357 MESSAGE ( "## COM " << myNbCommands << ": "<< aCommand->GetString() );
360 const _pyID& objID = aCommand->GetObject();
362 if ( objID.IsEmpty() )
365 // Find an object to process theCommand
368 if ( objID == this->GetID() || objID == SMESH_2smeshpy::GenName())
370 this->Process( aCommand );
374 // SMESH_Mesh method?
375 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( objID );
376 if ( id_mesh != myMeshes.end() )
378 //id_mesh->second->AddProcessedCmd( aCommand );
380 // check for mesh editor object
381 if ( aCommand->GetMethod() == "GetMeshEditor" ) { // MeshEditor creation
382 _pyID editorID = aCommand->GetResultValue();
383 Handle(_pyMeshEditor) editor = new _pyMeshEditor( aCommand );
384 myMeshEditors.insert( make_pair( editorID, editor ));
387 // check for SubMesh objects
388 else if ( aCommand->GetMethod() == "GetSubMesh" ) { // SubMesh creation
389 _pyID subMeshID = aCommand->GetResultValue();
390 Handle(_pySubMesh) subMesh = new _pySubMesh( aCommand );
391 myObjects.insert( make_pair( subMeshID, subMesh ));
394 id_mesh->second->Process( aCommand );
395 id_mesh->second->AddProcessedCmd( aCommand );
399 // SMESH_MeshEditor method?
400 map< _pyID, Handle(_pyMeshEditor) >::iterator id_editor = myMeshEditors.find( objID );
401 if ( id_editor != myMeshEditors.end() )
403 const TCollection_AsciiString& method = aCommand->GetMethod();
405 // some commands of SMESH_MeshEditor create meshes and groups
406 _pyID meshID, groups;
407 if ( method.Search("MakeMesh") != -1 )
408 meshID = aCommand->GetResultValue();
409 else if ( method == "MakeBoundaryMesh")
410 meshID = aCommand->GetResultValue(1);
411 else if ( method == "MakeBoundaryElements")
412 meshID = aCommand->GetResultValue(2);
414 if ( method.Search("MakeGroups") != -1 ||
415 method == "ExtrusionAlongPathX" ||
416 method == "ExtrusionAlongPathObjX" ||
417 method == "DoubleNodeGroupNew" ||
418 method == "DoubleNodeGroupsNew" ||
419 method == "DoubleNodeElemGroupNew" ||
420 method == "DoubleNodeElemGroupsNew" )
421 groups = aCommand->GetResultValue();
422 else if ( method == "MakeBoundaryMesh" )
423 groups = aCommand->GetResultValue(2);
424 else if ( method == "MakeBoundaryElements")
425 groups = aCommand->GetResultValue(3);
427 id_editor->second->Process( aCommand );
428 id_editor->second->AddProcessedCmd( aCommand );
430 if ( !meshID.IsEmpty() &&
431 !myMeshes.count( meshID ) &&
432 aCommand->IsStudyEntry( meshID ))
434 TCollection_AsciiString processedCommand = aCommand->GetString();
435 Handle(_pyMesh) mesh = new _pyMesh( aCommand, meshID );
436 myMeshes.insert( make_pair( meshID, mesh ));
438 aCommand->GetString() = processedCommand; // discard changes made by _pyMesh
440 if ( !groups.IsEmpty() )
442 if ( !aCommand->IsStudyEntry( meshID ))
443 meshID = id_editor->second->GetMesh();
444 Handle(_pyMesh) mesh = myMeshes[ meshID ];
446 list< _pyID > idList = aCommand->GetStudyEntries( groups );
447 list< _pyID >::iterator grID = idList.begin();
448 for ( ; grID != idList.end(); ++grID )
449 if ( !myObjects.count( *grID ))
451 Handle(_pyGroup) group = new _pyGroup( aCommand, *grID );
453 if ( !mesh.IsNull() ) mesh->AddGroup( group );
457 } // SMESH_MeshEditor methods
459 // SMESH_Hypothesis method?
460 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
461 for ( ; hyp != myHypos.end(); ++hyp )
462 if ( !(*hyp)->IsAlgo() && objID == (*hyp)->GetID() ) {
463 (*hyp)->Process( aCommand );
464 (*hyp)->AddProcessedCmd( aCommand );
468 // aFilterManager.CreateFilter() ?
469 if ( aCommand->GetMethod() == "CreateFilter" )
471 // Set a more human readable name to a filter
472 // aFilter0x7fbf6c71cfb0 -> aFilter_nb
473 _pyID newID, filterID = aCommand->GetResultValue();
474 int pos = filterID.Search( "0x" );
476 newID = (filterID.SubString(1,pos-1) + "_") + _pyID( ++myNbFilters );
478 Handle(_pyObject) filter( new _pyFilter( aCommand, newID ));
482 // other object method?
483 map< _pyID, Handle(_pyObject) >::iterator id_obj = myObjects.find( objID );
484 if ( id_obj != myObjects.end() ) {
485 id_obj->second->Process( aCommand );
486 id_obj->second->AddProcessedCmd( aCommand );
490 // Add access to a wrapped mesh
491 AddMeshAccessorMethod( aCommand );
493 // Add access to a wrapped algorithm
494 // AddAlgoAccessorMethod( aCommand ); // ??? what if algo won't be wrapped at all ???
496 // PAL12227. PythonDump was not updated at proper time; result is
497 // aCriteria.append(SMESH.Filter.Criterion(17,26,0,'L1',26,25,1e-07,SMESH.EDGE,-1))
498 // TypeError: __init__() takes exactly 11 arguments (10 given)
499 const char wrongCommand[] = "SMESH.Filter.Criterion(";
500 if ( int beg = theCommand.Location( wrongCommand, 1, theCommand.Length() ))
502 _pyCommand tmpCmd( theCommand.SubString( beg, theCommand.Length() ), -1);
503 // there must be 10 arguments, 5-th arg ThresholdID is missing,
504 const int wrongNbArgs = 9, missingArg = 5;
505 if ( tmpCmd.GetNbArgs() == wrongNbArgs )
507 for ( int i = wrongNbArgs; i > missingArg; --i )
508 tmpCmd.SetArg( i + 1, tmpCmd.GetArg( i ));
509 tmpCmd.SetArg( missingArg, "''");
510 aCommand->GetString().Trunc( beg - 1 );
511 aCommand->GetString() += tmpCmd.GetString();
514 // set GetCriterion(elementType,CritType,Compare,Treshold,UnaryOp,BinaryOp,Tolerance)
516 // instead of "SMESH.Filter.Criterion(
517 // Type,Compare,Threshold,ThresholdStr,ThresholdID,UnaryOp,BinaryOp,Tolerance,TypeOfElement,Precision)
518 // 1 2 3 4 5 6 7 8 9 10
519 // in order to avoid the problem of type mismatch of long and FunctorType
520 const TCollection_AsciiString
521 SMESH("SMESH."), dfltFunctor = "SMESH.FT_Undefined", dftlTol = "1e-07", dftlPreci = "-1";
522 TCollection_AsciiString
523 Type = aCommand->GetArg(1), // long
524 Compare = aCommand->GetArg(2), // long
525 Threshold = aCommand->GetArg(3), // double
526 ThresholdStr = aCommand->GetArg(4), // string
527 ThresholdID = aCommand->GetArg(5), // string
528 UnaryOp = aCommand->GetArg(6), // long
529 BinaryOp = aCommand->GetArg(7), // long
530 Tolerance = aCommand->GetArg(8), // double
531 TypeOfElement = aCommand->GetArg(9), // ElementType
532 Precision = aCommand->GetArg(10); // long
533 Type = SMESH + SMESH::FunctorTypeToString( SMESH::FunctorType( Type.IntegerValue() ));
534 Compare = SMESH + SMESH::FunctorTypeToString( SMESH::FunctorType( Compare.IntegerValue() ));
535 UnaryOp = SMESH + SMESH::FunctorTypeToString( SMESH::FunctorType( UnaryOp.IntegerValue() ));
536 BinaryOp = SMESH + SMESH::FunctorTypeToString( SMESH::FunctorType( BinaryOp.IntegerValue() ));
538 aCommand->RemoveArgs();
539 aCommand->SetObject( SMESH_2smeshpy::GenName() );
540 aCommand->SetMethod( "GetCriterion" );
542 aCommand->SetArg( 1, TypeOfElement );
543 aCommand->SetArg( 2, Type );
544 aCommand->SetArg( 3, Compare );
546 if ( Type == "SMESH.FT_ElemGeomType" && Threshold.IsIntegerValue() )
548 // set SMESH.GeometryType instead of a numerical Threshold
549 const char* types[SMESH::Geom_POLYHEDRA+1] = {
550 "Geom_POINT", "Geom_EDGE", "Geom_TRIANGLE", "Geom_QUADRANGLE", "Geom_POLYGON",
551 "Geom_TETRA", "Geom_PYRAMID", "Geom_HEXA", "Geom_PENTA", "Geom_HEXAGONAL_PRISM",
554 int iGeom = Threshold.IntegerValue();
555 if ( -1 < iGeom && iGeom < SMESH::Geom_POLYHEDRA+1 )
556 Threshold = SMESH + types[ iGeom ];
558 if ( ThresholdID.Length() != 2 && ThresholdStr.Length() != 2) // not '' or ""
559 aCommand->SetArg( 4, ThresholdID.SubString( 2, ThresholdID.Length()-1 )); // shape entry
560 else if ( ThresholdStr.Length() != 2 )
561 aCommand->SetArg( 4, ThresholdStr );
562 else if ( ThresholdID.Length() != 2 )
563 aCommand->SetArg( 4, ThresholdID );
565 aCommand->SetArg( 4, Threshold );
566 // find the last not default arg
568 if ( Tolerance == dftlTol ) {
570 if ( BinaryOp == dfltFunctor ) {
572 if ( UnaryOp == dfltFunctor )
576 if ( 5 < lastDefault ) aCommand->SetArg( 5, UnaryOp );
577 if ( 6 < lastDefault ) aCommand->SetArg( 6, BinaryOp );
578 if ( 7 < lastDefault ) aCommand->SetArg( 7, Tolerance );
579 if ( Precision != dftlPreci )
581 TCollection_AsciiString crit = aCommand->GetResultValue();
582 aCommand->GetString() += "; ";
583 aCommand->GetString() += crit + ".Precision = " + Precision;
589 //================================================================================
591 * \brief Convert the command or remember it for later conversion
592 * \param theCommand - The python command calling a method of SMESH_Gen
594 //================================================================================
596 void _pyGen::Process( const Handle(_pyCommand)& theCommand )
598 // there are methods to convert:
599 // CreateMesh( shape )
600 // Concatenate( [mesh1, ...], ... )
601 // CreateHypothesis( theHypType, theLibName )
602 // Compute( mesh, geom )
603 // Evaluate( mesh, geom )
605 TCollection_AsciiString method = theCommand->GetMethod();
607 if ( method == "CreateMesh" || method == "CreateEmptyMesh")
609 Handle(_pyMesh) mesh = new _pyMesh( theCommand );
610 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
613 if ( method == "CreateMeshesFromUNV" ||
614 method == "CreateMeshesFromSTL" ||
615 method == "CreateMeshesFromCGNS" ||
616 method == "CopyMesh" )
618 Handle(_pyMesh) mesh = new _pyMesh( theCommand, theCommand->GetResultValue() );
619 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
622 if( method == "CreateMeshesFromMED" || method == "CreateMeshesFromSAUV")
624 for(int ind = 0;ind<theCommand->GetNbResultValues();ind++)
626 const _pyID& meshID = theCommand->GetResultValue(ind+1);
627 if ( !theCommand->IsStudyEntry( meshID ) ) continue;
628 Handle(_pyMesh) mesh = new _pyMesh( theCommand, theCommand->GetResultValue(ind+1));
629 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
633 // CreateHypothesis()
634 if ( method == "CreateHypothesis" )
636 // issue 199929, remove standard library name (default parameter)
637 const TCollection_AsciiString & aLibName = theCommand->GetArg( 2 );
638 if ( aLibName.Search( "StdMeshersEngine" ) != -1 ) {
639 // keep first argument
640 TCollection_AsciiString arg = theCommand->GetArg( 1 );
641 theCommand->RemoveArgs();
642 theCommand->SetArg( 1, arg );
645 myHypos.push_back( _pyHypothesis::NewHypothesis( theCommand ));
649 // smeshgen.Compute( mesh, geom ) --> mesh.Compute()
650 if ( method == "Compute" )
652 const _pyID& meshID = theCommand->GetArg( 1 );
653 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( meshID );
654 if ( id_mesh != myMeshes.end() ) {
655 theCommand->SetObject( meshID );
656 theCommand->RemoveArgs();
657 id_mesh->second->Process( theCommand );
658 id_mesh->second->AddProcessedCmd( theCommand );
663 // smeshgen.Evaluate( mesh, geom ) --> mesh.Evaluate(geom)
664 if ( method == "Evaluate" )
666 const _pyID& meshID = theCommand->GetArg( 1 );
667 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( meshID );
668 if ( id_mesh != myMeshes.end() ) {
669 theCommand->SetObject( meshID );
670 _pyID geom = theCommand->GetArg( 2 );
671 theCommand->RemoveArgs();
672 theCommand->SetArg( 1, geom );
673 id_mesh->second->AddProcessedCmd( theCommand );
678 // objects erasing creation command if no more it's commands invoked:
679 // SMESH_Pattern, FilterManager
680 if ( method == "GetPattern" ||
681 method == "CreateFilterManager" ||
682 method == "CreateMeasurements" ) {
683 Handle(_pyObject) obj = new _pySelfEraser( theCommand );
684 if ( !myObjects.insert( make_pair( obj->GetID(), obj )).second )
685 theCommand->Clear(); // already created
687 // Concatenate( [mesh1, ...], ... )
688 else if ( method == "Concatenate" || method == "ConcatenateWithGroups")
690 if ( method == "ConcatenateWithGroups" ) {
691 theCommand->SetMethod( "Concatenate" );
692 theCommand->SetArg( theCommand->GetNbArgs() + 1, "True" );
694 Handle(_pyMesh) mesh = new _pyMesh( theCommand, theCommand->GetResultValue() );
695 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
696 AddMeshAccessorMethod( theCommand );
698 else if ( method == "SetName" ) // SetName(obj,name)
700 // store theCommand as one of object commands to erase it along with the object
701 const _pyID& objID = theCommand->GetArg( 1 );
702 Handle(_pyObject) obj = FindObject( objID );
704 obj->AddProcessedCmd( theCommand );
707 // Replace name of SMESH_Gen
709 // names of SMESH_Gen methods fully equal to methods defined in smesh.py
710 static TStringSet smeshpyMethods;
711 if ( smeshpyMethods.empty() ) {
712 const char * names[] =
713 { "SetEmbeddedMode","IsEmbeddedMode","SetCurrentStudy","GetCurrentStudy",
714 "GetPattern","GetSubShapesId",
715 "" }; // <- mark of array end
716 smeshpyMethods.Insert( names );
718 if ( smeshpyMethods.Contains( theCommand->GetMethod() ))
719 // smeshgen.Method() --> smesh.Method()
720 theCommand->SetObject( SMESH_2smeshpy::SmeshpyName() );
722 // smeshgen.Method() --> smesh.smesh.Method()
723 theCommand->SetObject( SMESH_2smeshpy::GenName() );
726 //================================================================================
728 * \brief Convert the remembered commands
730 //================================================================================
734 // create an empty command
735 myLastCommand = new _pyCommand();
737 map< _pyID, Handle(_pyMesh) >::iterator id_mesh;
738 map< _pyID, Handle(_pyObject) >::iterator id_obj;
739 list< Handle(_pyHypothesis) >::iterator hyp;
741 if ( IsToKeepAllCommands() ) // historical dump
743 // set myIsPublished = true to all objects
744 for ( id_mesh = myMeshes.begin(); id_mesh != myMeshes.end(); ++id_mesh )
745 id_mesh->second->SetRemovedFromStudy( false );
746 for ( hyp = myHypos.begin(); hyp != myHypos.end(); ++hyp )
747 (*hyp)->SetRemovedFromStudy( false );
748 for ( id_obj = myObjects.begin(); id_obj != myObjects.end(); ++id_obj )
749 id_obj->second->SetRemovedFromStudy( false );
753 // let hypotheses find referred objects in order to prevent clearing
754 // not published referred hyps (it's needed for hyps like "LayerDistribution")
755 list< Handle(_pyMesh) > fatherMeshes;
756 for ( hyp = myHypos.begin(); hyp != myHypos.end(); ++hyp )
757 if ( !hyp->IsNull() )
758 (*hyp)->GetReferredMeshesAndGeom( fatherMeshes );
760 // set myIsPublished = false to all objects depending on
761 // meshes built on a removed geometry
762 for ( id_mesh = myMeshes.begin(); id_mesh != myMeshes.end(); ++id_mesh )
763 if ( id_mesh->second->IsNotGeomPublished() )
764 id_mesh->second->SetRemovedFromStudy( true );
767 for ( id_mesh = myMeshes.begin(); id_mesh != myMeshes.end(); ++id_mesh )
768 if ( ! id_mesh->second.IsNull() )
769 id_mesh->second->Flush();
772 for ( hyp = myHypos.begin(); hyp != myHypos.end(); ++hyp )
773 if ( !hyp->IsNull() ) {
775 // smeshgen.CreateHypothesis() --> smesh.smesh.CreateHypothesis()
776 if ( !(*hyp)->IsWrapped() )
777 (*hyp)->GetCreationCmd()->SetObject( SMESH_2smeshpy::GenName() );
780 // Flush other objects
781 for ( id_obj = myObjects.begin(); id_obj != myObjects.end(); ++id_obj )
782 if ( ! id_obj->second.IsNull() )
783 id_obj->second->Flush();
785 myLastCommand->SetOrderNb( ++myNbCommands );
786 myCommands.push_back( myLastCommand );
789 //================================================================================
791 * \brief Clean commmands of removed objects depending on myIsPublished flag
793 //================================================================================
795 void _pyGen::ClearCommands()
797 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.begin();
798 for ( ; id_mesh != myMeshes.end(); ++id_mesh )
799 id_mesh->second->ClearCommands();
801 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
802 for ( ; hyp != myHypos.end(); ++hyp )
803 if ( !hyp->IsNull() )
804 (*hyp)->ClearCommands();
806 map< _pyID, Handle(_pyObject) >::iterator id_obj = myObjects.begin();
807 for ( ; id_obj != myObjects.end(); ++id_obj )
808 id_obj->second->ClearCommands();
811 //================================================================================
813 * \brief Release mutual handles of objects
815 //================================================================================
819 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.begin();
820 for ( ; id_mesh != myMeshes.end(); ++id_mesh )
821 id_mesh->second->Free();
824 map< _pyID, Handle(_pyMeshEditor) >::iterator id_ed = myMeshEditors.begin();
825 for ( ; id_ed != myMeshEditors.end(); ++id_ed )
826 id_ed->second->Free();
827 myMeshEditors.clear();
829 map< _pyID, Handle(_pyObject) >::iterator id_obj = myObjects.begin();
830 for ( ; id_obj != myObjects.end(); ++id_obj )
831 id_obj->second->Free();
834 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
835 for ( ; hyp != myHypos.end(); ++hyp )
836 if ( !hyp->IsNull() )
840 myFile2ExportedMesh.clear();
843 //================================================================================
845 * \brief Add access method to mesh that is an argument
846 * \param theCmd - command to add access method
847 * \retval bool - true if added
849 //================================================================================
851 bool _pyGen::AddMeshAccessorMethod( Handle(_pyCommand) theCmd ) const
854 map< _pyID, Handle(_pyMesh) >::const_iterator id_mesh = myMeshes.begin();
855 for ( ; id_mesh != myMeshes.end(); ++id_mesh ) {
856 if ( theCmd->AddAccessorMethod( id_mesh->first, id_mesh->second->AccessorMethod() ))
862 //================================================================================
864 * \brief Add access method to algo that is an object or an argument
865 * \param theCmd - command to add access method
866 * \retval bool - true if added
868 //================================================================================
870 bool _pyGen::AddAlgoAccessorMethod( Handle(_pyCommand) theCmd ) const
873 list< Handle(_pyHypothesis) >::const_iterator hyp = myHypos.begin();
874 for ( ; hyp != myHypos.end(); ++hyp ) {
875 if ( (*hyp)->IsAlgo() && /*(*hyp)->IsWrapped() &&*/
876 theCmd->AddAccessorMethod( (*hyp)->GetID(), (*hyp)->AccessorMethod() ))
882 //================================================================================
884 * \brief Find hypothesis by ID (entry)
885 * \param theHypID - The hypothesis ID
886 * \retval Handle(_pyHypothesis) - The found hypothesis
888 //================================================================================
890 Handle(_pyHypothesis) _pyGen::FindHyp( const _pyID& theHypID )
892 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
893 for ( ; hyp != myHypos.end(); ++hyp )
894 if ( !hyp->IsNull() && theHypID == (*hyp)->GetID() )
896 return Handle(_pyHypothesis)();
899 //================================================================================
901 * \brief Find algorithm the created algorithm
902 * \param theGeom - The shape ID the algorithm was created on
903 * \param theMesh - The mesh ID that created the algorithm
904 * \param dim - The algo dimension
905 * \retval Handle(_pyHypothesis) - The found algo
907 //================================================================================
909 Handle(_pyHypothesis) _pyGen::FindAlgo( const _pyID& theGeom, const _pyID& theMesh,
910 const Handle(_pyHypothesis)& theHypothesis )
912 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
913 for ( ; hyp != myHypos.end(); ++hyp )
914 if ( !hyp->IsNull() &&
916 theHypothesis->CanBeCreatedBy( (*hyp)->GetAlgoType() ) &&
917 (*hyp)->GetGeom() == theGeom &&
918 (*hyp)->GetMesh() == theMesh )
923 //================================================================================
925 * \brief Find subMesh by ID (entry)
926 * \param theSubMeshID - The subMesh ID
927 * \retval Handle(_pySubMesh) - The found subMesh
929 //================================================================================
931 Handle(_pySubMesh) _pyGen::FindSubMesh( const _pyID& theSubMeshID )
933 map< _pyID, Handle(_pyObject) >::iterator id_subMesh = myObjects.find(theSubMeshID);
934 if ( id_subMesh != myObjects.end() )
935 return Handle(_pySubMesh)::DownCast( id_subMesh->second );
936 return Handle(_pySubMesh)();
940 //================================================================================
942 * \brief Change order of commands in the script
943 * \param theCmd1 - One command
944 * \param theCmd2 - Another command
946 //================================================================================
948 void _pyGen::ExchangeCommands( Handle(_pyCommand) theCmd1, Handle(_pyCommand) theCmd2 )
950 list< Handle(_pyCommand) >::iterator pos1, pos2;
951 pos1 = find( myCommands.begin(), myCommands.end(), theCmd1 );
952 pos2 = find( myCommands.begin(), myCommands.end(), theCmd2 );
953 myCommands.insert( pos1, theCmd2 );
954 myCommands.insert( pos2, theCmd1 );
955 myCommands.erase( pos1 );
956 myCommands.erase( pos2 );
958 int nb1 = theCmd1->GetOrderNb();
959 theCmd1->SetOrderNb( theCmd2->GetOrderNb() );
960 theCmd2->SetOrderNb( nb1 );
961 // cout << "BECOME " << theCmd1->GetOrderNb() << "\t" << theCmd1->GetString() << endl
962 // << "BECOME " << theCmd2->GetOrderNb() << "\t" << theCmd2->GetString() << endl << endl;
965 //================================================================================
967 * \brief Set one command after the other
968 * \param theCmd - Command to move
969 * \param theAfterCmd - Command ater which to insert the first one
971 //================================================================================
973 void _pyGen::SetCommandAfter( Handle(_pyCommand) theCmd, Handle(_pyCommand) theAfterCmd )
975 setNeighbourCommand( theCmd, theAfterCmd, true );
978 //================================================================================
980 * \brief Set one command before the other
981 * \param theCmd - Command to move
982 * \param theBeforeCmd - Command before which to insert the first one
984 //================================================================================
986 void _pyGen::SetCommandBefore( Handle(_pyCommand) theCmd, Handle(_pyCommand) theBeforeCmd )
988 setNeighbourCommand( theCmd, theBeforeCmd, false );
991 //================================================================================
993 * \brief Set one command before or after the other
994 * \param theCmd - Command to move
995 * \param theOtherCmd - Command ater or before which to insert the first one
997 //================================================================================
999 void _pyGen::setNeighbourCommand( Handle(_pyCommand)& theCmd,
1000 Handle(_pyCommand)& theOtherCmd,
1001 const bool theIsAfter )
1003 list< Handle(_pyCommand) >::iterator pos;
1004 pos = find( myCommands.begin(), myCommands.end(), theCmd );
1005 myCommands.erase( pos );
1006 pos = find( myCommands.begin(), myCommands.end(), theOtherCmd );
1007 myCommands.insert( (theIsAfter ? ++pos : pos), theCmd );
1010 for ( pos = myCommands.begin(); pos != myCommands.end(); ++pos)
1011 (*pos)->SetOrderNb( i++ );
1014 //================================================================================
1016 * \brief Set command be last in list of commands
1017 * \param theCmd - Command to be last
1019 //================================================================================
1021 Handle(_pyCommand)& _pyGen::GetLastCommand()
1023 return myLastCommand;
1026 //================================================================================
1028 * \brief Set method to access to object wrapped with python class
1029 * \param theID - The wrapped object entry
1030 * \param theMethod - The accessor method
1032 //================================================================================
1034 void _pyGen::SetAccessorMethod(const _pyID& theID, const char* theMethod )
1036 myID2AccessorMethod.Bind( theID, (char*) theMethod );
1039 //================================================================================
1041 * \brief Generated new ID for object and assign with existing name
1042 * \param theID - ID of existing object
1044 //================================================================================
1046 _pyID _pyGen::GenerateNewID( const _pyID& theID )
1051 aNewID = theID + _pyID( ":" ) + _pyID( index++ );
1053 while ( myObjectNames.IsBound( aNewID ) );
1055 myObjectNames.Bind( aNewID, myObjectNames.IsBound( theID )
1056 ? (myObjectNames.Find( theID ) + _pyID( "_" ) + _pyID( index-1 ))
1057 : _pyID( "A" ) + aNewID );
1061 //================================================================================
1063 * \brief Stores theObj in myObjects
1065 //================================================================================
1067 void _pyGen::AddObject( Handle(_pyObject)& theObj )
1069 if ( theObj.IsNull() ) return;
1071 if ( theObj->IsKind( STANDARD_TYPE( _pyMesh )))
1072 myMeshes.insert( make_pair( theObj->GetID(), Handle(_pyMesh)::DownCast( theObj )));
1074 else if ( theObj->IsKind( STANDARD_TYPE( _pyMeshEditor )))
1075 myMeshEditors.insert( make_pair( theObj->GetID(), Handle(_pyMeshEditor)::DownCast( theObj )));
1078 myObjects.insert( make_pair( theObj->GetID(), theObj ));
1081 //================================================================================
1083 * \brief Finds a _pyObject by ID
1085 //================================================================================
1087 Handle(_pyObject) _pyGen::FindObject( const _pyID& theObjID ) const
1090 map< _pyID, Handle(_pyObject) >::const_iterator id_obj = myObjects.find( theObjID );
1091 if ( id_obj != myObjects.end() )
1092 return id_obj->second;
1095 map< _pyID, Handle(_pyMesh) >::const_iterator id_obj = myMeshes.find( theObjID );
1096 if ( id_obj != myMeshes.end() )
1097 return id_obj->second;
1100 // map< _pyID, Handle(_pyMeshEditor) >::const_iterator id_obj = myMeshEditors.find( theObjID );
1101 // if ( id_obj != myMeshEditors.end() )
1102 // return id_obj->second;
1104 return Handle(_pyObject)();
1107 //================================================================================
1109 * \brief Check if a study entry is under GEOM component
1111 //================================================================================
1113 bool _pyGen::IsGeomObject(const _pyID& theObjID) const
1117 return ( myGeomIDIndex <= theObjID.Length() &&
1118 int( theObjID.Value( myGeomIDIndex )) == myGeomIDNb);
1123 //================================================================================
1125 * \brief Returns true if an object is not present in a study
1127 //================================================================================
1129 bool _pyGen::IsNotPublished(const _pyID& theObjID) const
1131 if ( theObjID.IsEmpty() ) return false;
1133 if ( myObjectNames.IsBound( theObjID ))
1134 return false; // SMESH object is in study
1136 // either the SMESH object is not in study or it is a GEOM object
1137 if ( IsGeomObject( theObjID ))
1139 SALOMEDS::SObject_var so = myStudy->FindObjectID( theObjID.ToCString() );
1140 if ( so->_is_nil() ) return true;
1141 CORBA::Object_var obj = so->GetObject();
1142 return CORBA::is_nil( obj );
1144 return true; // SMESH object not in study
1147 //================================================================================
1149 * \brief Return reader of hypotheses of plugins
1151 //================================================================================
1153 Handle( _pyHypothesisReader ) _pyGen::GetHypothesisReader() const
1155 if (myHypReader.IsNull() )
1156 ((_pyGen*) this)->myHypReader = new _pyHypothesisReader;
1162 //================================================================================
1164 * \brief Mesh created by SMESH_Gen
1166 //================================================================================
1168 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd)
1169 : _pyObject( theCreationCmd ), myGeomNotInStudy( false )
1171 if ( theCreationCmd->GetMethod() == "CreateMesh" && theGen->IsNotPublished( GetGeom() ))
1172 myGeomNotInStudy = true;
1174 // convert my creation command --> smeshpy.Mesh(...)
1175 Handle(_pyCommand) creationCmd = GetCreationCmd();
1176 creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
1177 creationCmd->SetMethod( "Mesh" );
1178 theGen->SetAccessorMethod( GetID(), _pyMesh::AccessorMethod() );
1181 //================================================================================
1183 * \brief Mesh created by SMESH_MeshEditor
1185 //================================================================================
1187 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd, const _pyID& meshId):
1188 _pyObject(theCreationCmd,meshId), myGeomNotInStudy(false )
1190 if ( theCreationCmd->MethodStartsFrom( "CreateMeshesFrom" ))
1192 // this mesh depends on the exported mesh
1193 const TCollection_AsciiString& file = theCreationCmd->GetArg( 1 );
1194 if ( !file.IsEmpty() )
1196 ExportedMeshData& exportData = theGen->FindExportedMesh( file );
1197 addFatherMesh( exportData.myMesh );
1198 if ( !exportData.myLastComputeCmd.IsNull() )
1200 // restore cleared Compute() by which the exported mesh was generated
1201 exportData.myLastComputeCmd->GetString() = exportData.myLastComputeCmdString;
1202 // protect that Compute() cmd from clearing
1203 if ( exportData.myMesh->myLastComputeCmd == exportData.myLastComputeCmd )
1204 exportData.myMesh->myLastComputeCmd.Nullify();
1208 else if ( theCreationCmd->MethodStartsFrom( "Concatenate" ))
1210 // this mesh depends on concatenated meshes
1211 const TCollection_AsciiString& meshIDs = theCreationCmd->GetArg( 1 );
1212 list< _pyID > idList = theCreationCmd->GetStudyEntries( meshIDs );
1213 list< _pyID >::iterator meshID = idList.begin();
1214 for ( ; meshID != idList.end(); ++meshID )
1215 addFatherMesh( *meshID );
1217 else if ( theCreationCmd->GetMethod() == "CopyMesh" )
1219 // this mesh depends on a copied IdSource
1220 const _pyID& objID = theCreationCmd->GetArg( 1 );
1221 addFatherMesh( objID );
1223 else if ( theCreationCmd->GetMethod().Search("MakeMesh") != -1 ||
1224 theCreationCmd->GetMethod() == "MakeBoundaryMesh" ||
1225 theCreationCmd->GetMethod() == "MakeBoundaryElements" )
1227 // this mesh depends on a source mesh
1228 // (theCreationCmd is already Process()ed by _pyMeshEditor)
1229 const _pyID& meshID = theCreationCmd->GetObject();
1230 addFatherMesh( meshID );
1233 // convert my creation command
1234 Handle(_pyCommand) creationCmd = GetCreationCmd();
1235 creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
1236 theGen->SetAccessorMethod( meshId, _pyMesh::AccessorMethod() );
1239 //================================================================================
1241 * \brief Convert an IDL API command of SMESH::SMESH_Mesh to a method call of python Mesh
1242 * \param theCommand - Engine method called for this mesh
1244 //================================================================================
1246 void _pyMesh::Process( const Handle(_pyCommand)& theCommand )
1248 // some methods of SMESH_Mesh interface needs special conversion
1249 // to methods of Mesh python class
1251 // 1. GetSubMesh(geom, name) + AddHypothesis(geom, algo)
1252 // --> in Mesh_Algorithm.Create(mesh, geom, hypo, so)
1253 // 2. AddHypothesis(geom, hyp)
1254 // --> in Mesh_Algorithm.Hypothesis(hyp, args, so)
1255 // 3. CreateGroupFromGEOM(type, name, grp)
1256 // --> in Mesh.Group(grp, name="")
1257 // 4. ExportToMED(f, auto_groups, version)
1258 // --> in Mesh.ExportMED( f, auto_groups, version )
1261 const TCollection_AsciiString& method = theCommand->GetMethod();
1262 // ----------------------------------------------------------------------
1263 if ( method == "Compute" ) // in snapshot mode, clear the previous Compute()
1265 if ( !theGen->IsToKeepAllCommands() ) // !historical
1267 list< Handle(_pyHypothesis) >::iterator hyp;
1268 if ( !myLastComputeCmd.IsNull() )
1270 for ( hyp = myHypos.begin(); hyp != myHypos.end(); ++hyp )
1271 (*hyp)->ComputeDiscarded( myLastComputeCmd );
1273 myLastComputeCmd->Clear();
1275 myLastComputeCmd = theCommand;
1277 for ( hyp = myHypos.begin(); hyp != myHypos.end(); ++hyp )
1278 (*hyp)->MeshComputed( myLastComputeCmd );
1282 // ----------------------------------------------------------------------
1283 else if ( method == "Clear" ) // in snapshot mode, clear all previous commands
1285 if ( !theGen->IsToKeepAllCommands() ) // !historical
1288 myChildMeshes.empty() ? 0 : myChildMeshes.back()->GetCreationCmd()->GetOrderNb();
1289 // list< Handle(_pyCommand) >::reverse_iterator cmd = myProcessedCmds.rbegin();
1290 // for ( ; cmd != myProcessedCmds.rend() && (*cmd)->GetOrderNb() > untilCmdNb; ++cmd )
1292 if ( !myLastComputeCmd.IsNull() )
1294 list< Handle(_pyHypothesis) >::iterator hyp;
1295 for ( hyp = myHypos.begin(); hyp != myHypos.end(); ++hyp )
1296 (*hyp)->ComputeDiscarded( myLastComputeCmd );
1298 myLastComputeCmd->Clear();
1301 list< Handle(_pyMeshEditor)>::iterator e = myEditors.begin();
1302 for ( ; e != myEditors.end(); ++e )
1304 list< Handle(_pyCommand)>& cmds = (*e)->GetProcessedCmds();
1305 list< Handle(_pyCommand) >::reverse_iterator cmd = cmds.rbegin();
1306 for ( ; cmd != cmds.rend() && (*cmd)->GetOrderNb() > untilCmdNb; ++cmd )
1307 if ( !(*cmd)->IsEmpty() )
1309 if ( (*cmd)->GetStudyEntries( (*cmd)->GetResultValue() ).empty() ) // no object created
1313 myLastComputeCmd = theCommand; // to clear Clear() the same way as Compute()
1316 // ----------------------------------------------------------------------
1317 else if ( method == "GetSubMesh" ) { // collect submeshes of the mesh
1318 Handle(_pySubMesh) subMesh = theGen->FindSubMesh( theCommand->GetResultValue() );
1319 if ( !subMesh.IsNull() ) {
1320 subMesh->SetCreator( this );
1321 mySubmeshes.push_back( subMesh );
1324 else if ( method == "RemoveSubMesh" ) { // move submesh creation before its removal
1325 Handle(_pySubMesh) subMesh = theGen->FindSubMesh( theCommand->GetArg(1) );
1326 if ( !subMesh.IsNull() )
1327 subMesh->Process( theCommand );
1328 AddMeshAccess( theCommand );
1330 // ----------------------------------------------------------------------
1331 else if ( method == "AddHypothesis" ) { // mesh.AddHypothesis(geom, HYPO )
1332 myAddHypCmds.push_back( theCommand );
1334 const _pyID& hypID = theCommand->GetArg( 2 );
1335 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
1336 if ( !hyp.IsNull() ) {
1337 myHypos.push_back( hyp );
1338 if ( hyp->GetMesh().IsEmpty() )
1339 hyp->SetMesh( this->GetID() );
1342 // ----------------------------------------------------------------------
1343 else if ( method == "CreateGroup" ) // CreateGroup() --> CreateEmptyGroup()
1345 theCommand->SetMethod( "CreateEmptyGroup" );
1346 Handle(_pyGroup) group = new _pyGroup( theCommand );
1347 myGroups.push_back( group );
1348 theGen->AddObject( group );
1350 // ----------------------------------------------------------------------
1351 else if ( method == "CreateGroupFromGEOM" ) {// (type, name, grp)
1352 _pyID grp = theCommand->GetArg( 3 );
1353 // VSR 24/12/2010. PAL21106: always use GroupOnGeom() function on dump
1354 // next if(){...} section is commented
1355 //if ( sameGroupType( grp, theCommand->GetArg( 1 )) ) { // --> Group(grp)
1356 // theCommand->SetMethod( "Group" );
1357 // theCommand->RemoveArgs();
1358 // theCommand->SetArg( 1, grp );
1361 // ------------------------->>>>> GroupOnGeom( grp, name, typ )
1362 _pyID type = theCommand->GetArg( 1 );
1363 _pyID name = theCommand->GetArg( 2 );
1364 theCommand->SetMethod( "GroupOnGeom" );
1365 theCommand->RemoveArgs();
1366 theCommand->SetArg( 1, grp );
1367 theCommand->SetArg( 2, name );
1368 theCommand->SetArg( 3, type );
1370 Handle(_pyGroup) group = new _pyGroup( theCommand );
1371 myGroups.push_back( group );
1372 theGen->AddObject( group );
1374 // ----------------------------------------------------------------------
1375 else if ( method == "CreateGroupFromFilter" ) // --> GroupOnFilter()
1377 theCommand->SetMethod( "GroupOnFilter" );
1378 Handle(_pyGroup) group = new _pyGroup( theCommand );
1379 myGroups.push_back( group );
1380 theGen->AddObject( group );
1382 // GroupOnFilter(typ, name, aFilter0x4743dc0 -> aFilter_1)
1383 _pyID filterID = theCommand->GetArg(3);
1384 Handle(_pyFilter) filter = Handle(_pyFilter)::DownCast( theGen->FindObject( filterID ));
1385 if ( !filter.IsNull())
1387 filter->Process( theCommand );
1388 filter->AddUser( group );
1391 // ----------------------------------------------------------------------
1392 else if ( theCommand->MethodStartsFrom( "Export" ))
1394 if ( method == "ExportToMED" || // ExportToMED() --> ExportMED()
1395 method == "ExportToMEDX" ) { // ExportToMEDX() --> ExportMED()
1396 theCommand->SetMethod( "ExportMED" );
1398 else if ( method == "ExportCGNS" )
1399 { // ExportCGNS(part, ...) -> ExportCGNS(..., part)
1400 _pyID partID = theCommand->GetArg( 1 );
1401 int nbArgs = theCommand->GetNbArgs();
1402 for ( int i = 2; i <= nbArgs; ++i )
1403 theCommand->SetArg( i-1, theCommand->GetArg( i ));
1404 theCommand->SetArg( nbArgs, partID );
1406 else if ( theCommand->MethodStartsFrom( "ExportPartTo" ))
1407 { // ExportPartTo*(part, ...) -> Export*(..., part)
1409 // remove "PartTo" from the method
1410 TCollection_AsciiString newMethod = method;
1411 newMethod.Remove( 7, 6 );
1412 theCommand->SetMethod( newMethod );
1413 // make the 1st arg be the last one
1414 _pyID partID = theCommand->GetArg( 1 );
1415 int nbArgs = theCommand->GetNbArgs();
1416 for ( int i = 2; i <= nbArgs; ++i )
1417 theCommand->SetArg( i-1, theCommand->GetArg( i ));
1418 theCommand->SetArg( nbArgs, partID );
1420 // remember file name
1421 theGen->AddExportedMesh( theCommand->GetArg( 1 ),
1422 ExportedMeshData( this, myLastComputeCmd ));
1424 // ----------------------------------------------------------------------
1425 else if ( method == "RemoveHypothesis" ) // (geom, hyp)
1427 _pyID hypID = theCommand->GetArg( 2 );
1429 // check if this mesh still has corresponding addition command
1430 bool hasAddCmd = false;
1431 list< Handle(_pyCommand) >::iterator cmd = myAddHypCmds.begin();
1432 while ( cmd != myAddHypCmds.end() )
1434 // AddHypothesis(geom, hyp)
1435 if ( hypID == (*cmd)->GetArg( 2 )) { // erase both (add and remove) commands
1436 theCommand->Clear();
1438 cmd = myAddHypCmds.erase( cmd );
1445 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
1446 if ( ! hasAddCmd && hypID.Length() != 0 ) { // hypo addition already wrapped
1447 // RemoveHypothesis(geom, hyp) --> RemoveHypothesis( hyp, geom=0 )
1448 _pyID geom = theCommand->GetArg( 1 );
1449 theCommand->RemoveArgs();
1450 theCommand->SetArg( 1, hypID );
1451 if ( geom != GetGeom() )
1452 theCommand->SetArg( 2, geom );
1454 // remove hyp from myHypos
1455 myHypos.remove( hyp );
1457 // check for SubMesh order commands
1458 else if ( method == "GetMeshOrder" || method == "SetMeshOrder" )
1460 // make commands GetSubMesh() returning sub-meshes be before using sub-meshes
1461 // by GetMeshOrder() and SetMeshOrder(), since by defalut GetSubMesh()
1462 // commands are moved at the end of the script
1463 TCollection_AsciiString subIDs =
1464 ( method == "SetMeshOrder" ) ? theCommand->GetArg(1) : theCommand->GetResultValue();
1465 list< _pyID > idList = theCommand->GetStudyEntries( subIDs );
1466 list< _pyID >::iterator subID = idList.begin();
1467 for ( ; subID != idList.end(); ++subID )
1469 Handle(_pySubMesh) subMesh = theGen->FindSubMesh( *subID );
1470 if ( !subMesh.IsNull() )
1471 subMesh->Process( theCommand ); // it moves GetSubMesh() before theCommand
1474 // update list of groups
1475 else if ( method == "GetGroups" )
1477 TCollection_AsciiString grIDs = theCommand->GetResultValue();
1478 list< _pyID > idList = theCommand->GetStudyEntries( grIDs );
1479 list< _pyID >::iterator grID = idList.begin();
1480 for ( ; grID != idList.end(); ++grID )
1482 Handle(_pyObject) obj = theGen->FindObject( *grID );
1485 Handle(_pyGroup) group = new _pyGroup( theCommand, *grID );
1486 theGen->AddObject( group );
1487 myGroups.push_back( group );
1491 // add accessor method if necessary
1494 if ( NeedMeshAccess( theCommand ))
1495 // apply theCommand to the mesh wrapped by smeshpy mesh
1496 AddMeshAccess( theCommand );
1500 //================================================================================
1502 * \brief Return True if addition of accesor method is needed
1504 //================================================================================
1506 bool _pyMesh::NeedMeshAccess( const Handle(_pyCommand)& theCommand )
1508 // names of SMESH_Mesh methods fully equal to methods of python class Mesh,
1509 // so no conversion is needed for them at all:
1510 static TStringSet sameMethods;
1511 if ( sameMethods.empty() ) {
1512 const char * names[] =
1513 { "ExportDAT","ExportUNV","ExportSTL","ExportSAUV", "RemoveGroup","RemoveGroupWithContents",
1514 "GetGroups","UnionGroups","IntersectGroups","CutGroups","GetLog","GetId","ClearLog",
1515 "GetStudyId","HasDuplicatedGroupNamesMED","GetMEDMesh","NbNodes","NbElements",
1516 "NbEdges","NbEdgesOfOrder","NbFaces","NbFacesOfOrder","NbTriangles",
1517 "NbTrianglesOfOrder","NbQuadrangles","NbQuadranglesOfOrder","NbPolygons","NbVolumes",
1518 "NbVolumesOfOrder","NbTetras","NbTetrasOfOrder","NbHexas","NbHexasOfOrder",
1519 "NbPyramids","NbPyramidsOfOrder","NbPrisms","NbPrismsOfOrder","NbPolyhedrons",
1520 "NbSubMesh","GetElementsId","GetElementsByType","GetNodesId","GetElementType",
1521 "GetSubMeshElementsId","GetSubMeshNodesId","GetSubMeshElementType","Dump","GetNodeXYZ",
1522 "GetNodeInverseElements","GetShapeID","GetShapeIDForElem","GetElemNbNodes",
1523 "GetElemNode","IsMediumNode","IsMediumNodeOfAnyElem","ElemNbEdges","ElemNbFaces",
1524 "IsPoly","IsQuadratic","BaryCenter","GetHypothesisList", "SetAutoColor", "GetAutoColor",
1525 "Clear", "ConvertToStandalone", "GetMeshOrder", "SetMeshOrder"
1526 ,"" }; // <- mark of end
1527 sameMethods.Insert( names );
1530 return !sameMethods.Contains( theCommand->GetMethod() );
1533 //================================================================================
1535 * \brief Convert creation and addition of all algos and hypos
1537 //================================================================================
1539 void _pyMesh::Flush()
1542 // get the meshes this mesh depends on via hypotheses
1543 list< Handle(_pyMesh) > fatherMeshes;
1544 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
1545 for ( ; hyp != myHypos.end(); ++hyp )
1546 if ( ! (*hyp)->GetReferredMeshesAndGeom( fatherMeshes ))
1547 myGeomNotInStudy = true;
1549 list< Handle(_pyMesh) >::iterator m = fatherMeshes.begin();
1550 for ( ; m != fatherMeshes.end(); ++m )
1551 addFatherMesh( *m );
1552 // if ( removedGeom )
1553 // SetRemovedFromStudy(); // as reffered geometry not in study
1555 if ( myGeomNotInStudy )
1558 list < Handle(_pyCommand) >::iterator cmd;
1560 // try to convert algo addition like this:
1561 // mesh.AddHypothesis(geom, ALGO ) --> ALGO = mesh.Algo()
1562 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
1564 Handle(_pyCommand) addCmd = *cmd;
1566 _pyID algoID = addCmd->GetArg( 2 );
1567 Handle(_pyHypothesis) algo = theGen->FindHyp( algoID );
1568 if ( algo.IsNull() || !algo->IsAlgo() )
1571 // check and create new algorithm instance if it is already wrapped
1572 if ( algo->IsWrapped() ) {
1573 _pyID localAlgoID = theGen->GenerateNewID( algoID );
1574 TCollection_AsciiString aNewCmdStr = addCmd->GetIndentation() + localAlgoID +
1575 TCollection_AsciiString( " = " ) + theGen->GetID() +
1576 TCollection_AsciiString( ".CreateHypothesis( \"" ) + algo->GetAlgoType() +
1577 TCollection_AsciiString( "\" )" );
1579 Handle(_pyCommand) newCmd = theGen->AddCommand( aNewCmdStr );
1580 Handle(_pyAlgorithm) newAlgo = Handle(_pyAlgorithm)::DownCast(theGen->FindHyp( localAlgoID ));
1581 if ( !newAlgo.IsNull() ) {
1582 newAlgo->Assign( algo, this->GetID() );
1583 newAlgo->SetCreationCmd( newCmd );
1585 // set algorithm creation
1586 theGen->SetCommandBefore( newCmd, addCmd );
1587 myHypos.push_back( newAlgo );
1588 if ( !myLastComputeCmd.IsNull() &&
1589 newCmd->GetOrderNb() == myLastComputeCmd->GetOrderNb() + 1)
1590 newAlgo->MeshComputed( myLastComputeCmd );
1595 _pyID geom = addCmd->GetArg( 1 );
1596 bool isLocalAlgo = ( geom != GetGeom() );
1599 if ( algo->Addition2Creation( addCmd, this->GetID() )) // OK
1601 // wrapped algo is created after mesh creation
1602 GetCreationCmd()->AddDependantCmd( addCmd );
1604 if ( isLocalAlgo ) {
1605 // mesh.AddHypothesis(geom, ALGO ) --> mesh.AlgoMethod(geom)
1606 addCmd->SetArg( addCmd->GetNbArgs() + 1,
1607 TCollection_AsciiString( "geom=" ) + geom );
1608 // sm = mesh.GetSubMesh(geom, name) --> sm = ALGO.GetSubMesh()
1609 list < Handle(_pySubMesh) >::iterator smIt;
1610 for ( smIt = mySubmeshes.begin(); smIt != mySubmeshes.end(); ++smIt ) {
1611 Handle(_pySubMesh) subMesh = *smIt;
1612 Handle(_pyCommand) subCmd = subMesh->GetCreationCmd();
1613 if ( geom == subCmd->GetArg( 1 )) {
1614 subCmd->SetObject( algo->GetID() );
1615 subCmd->RemoveArgs();
1616 subMesh->SetCreator( algo );
1621 else // KO - ALGO was already created
1623 // mesh.AddHypothesis(geom, ALGO) --> mesh.AddHypothesis(ALGO, geom=0)
1624 addCmd->RemoveArgs();
1625 addCmd->SetArg( 1, algoID );
1627 addCmd->SetArg( 2, geom );
1631 // try to convert hypo addition like this:
1632 // mesh.AddHypothesis(geom, HYPO ) --> HYPO = algo.Hypo()
1633 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
1635 Handle(_pyCommand) addCmd = *cmd;
1636 _pyID hypID = addCmd->GetArg( 2 );
1637 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
1638 if ( hyp.IsNull() || hyp->IsAlgo() )
1640 bool converted = hyp->Addition2Creation( addCmd, this->GetID() );
1642 // mesh.AddHypothesis(geom, HYP) --> mesh.AddHypothesis(HYP, geom=0)
1643 _pyID geom = addCmd->GetArg( 1 );
1644 addCmd->RemoveArgs();
1645 addCmd->SetArg( 1, hypID );
1646 if ( geom != GetGeom() )
1647 addCmd->SetArg( 2, geom );
1651 myAddHypCmds.clear();
1652 mySubmeshes.clear();
1655 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
1656 for ( hyp = myHypos.begin(); hyp != myHypos.end(); ++hyp )
1660 //================================================================================
1662 * \brief Sets myIsPublished of me and of all objects depending on me.
1664 //================================================================================
1666 void _pyMesh::SetRemovedFromStudy(const bool isRemoved)
1668 _pyObject::SetRemovedFromStudy(isRemoved);
1670 list< Handle(_pySubMesh) >::iterator sm = mySubmeshes.begin();
1671 for ( ; sm != mySubmeshes.end(); ++sm )
1672 (*sm)->SetRemovedFromStudy(isRemoved);
1674 list< Handle(_pyGroup) >::iterator gr = myGroups.begin();
1675 for ( ; gr != myGroups.end(); ++gr )
1676 (*gr)->SetRemovedFromStudy(isRemoved);
1678 list< Handle(_pyMesh) >::iterator m = myChildMeshes.begin();
1679 for ( ; m != myChildMeshes.end(); ++m )
1680 (*m)->SetRemovedFromStudy(isRemoved);
1682 list< Handle(_pyMeshEditor)>::iterator e = myEditors.begin();
1683 for ( ; e != myEditors.end(); ++e )
1684 (*e)->SetRemovedFromStudy(isRemoved);
1687 //================================================================================
1689 * \brief Return true if none of myChildMeshes is in study
1691 //================================================================================
1693 bool _pyMesh::CanClear()
1698 list< Handle(_pyMesh) >::iterator m = myChildMeshes.begin();
1699 for ( ; m != myChildMeshes.end(); ++m )
1700 if ( !(*m)->CanClear() )
1706 //================================================================================
1708 * \brief Clear my commands and commands of mesh editor
1710 //================================================================================
1712 void _pyMesh::ClearCommands()
1718 // mark all sub-objects as not removed, except child meshes
1719 list< Handle(_pyMesh) > children;
1720 children.swap( myChildMeshes );
1721 SetRemovedFromStudy( false );
1722 children.swap( myChildMeshes );
1726 _pyObject::ClearCommands();
1728 list< Handle(_pySubMesh) >::iterator sm = mySubmeshes.begin();
1729 for ( ; sm != mySubmeshes.end(); ++sm )
1730 (*sm)->ClearCommands();
1732 list< Handle(_pyGroup) >::iterator gr = myGroups.begin();
1733 for ( ; gr != myGroups.end(); ++gr )
1734 (*gr)->ClearCommands();
1736 list< Handle(_pyMeshEditor)>::iterator e = myEditors.begin();
1737 for ( ; e != myEditors.end(); ++e )
1738 (*e)->ClearCommands();
1741 //================================================================================
1743 * \brief Add a father mesh by ID
1745 //================================================================================
1747 void _pyMesh::addFatherMesh( const _pyID& meshID )
1749 if ( !meshID.IsEmpty() )
1750 addFatherMesh( Handle(_pyMesh)::DownCast( theGen->FindObject( meshID )));
1753 //================================================================================
1755 * \brief Add a father mesh
1757 //================================================================================
1759 void _pyMesh::addFatherMesh( const Handle(_pyMesh)& mesh )
1761 if ( !mesh.IsNull() )
1763 //myFatherMeshes.push_back( mesh );
1764 mesh->myChildMeshes.push_back( this );
1766 // protect last Compute() from clearing by the next Compute()
1767 mesh->myLastComputeCmd.Nullify();
1771 //================================================================================
1773 * \brief MeshEditor convert its commands to ones of mesh
1775 //================================================================================
1777 _pyMeshEditor::_pyMeshEditor(const Handle(_pyCommand)& theCreationCmd):
1778 _pyObject( theCreationCmd )
1780 myMesh = theCreationCmd->GetObject();
1781 myCreationCmdStr = theCreationCmd->GetString();
1782 theCreationCmd->Clear();
1784 Handle(_pyMesh) mesh = ObjectToMesh( theGen->FindObject( myMesh ));
1785 if ( !mesh.IsNull() )
1786 mesh->AddEditor( this );
1789 //================================================================================
1791 * \brief convert its commands to ones of mesh
1793 //================================================================================
1795 void _pyMeshEditor::Process( const Handle(_pyCommand)& theCommand)
1797 // names of SMESH_MeshEditor methods fully equal to methods of python class Mesh, so
1798 // commands calling this methods are converted to calls of methods of Mesh
1799 static TStringSet sameMethods;
1800 if ( sameMethods.empty() ) {
1801 const char * names[] = {
1802 "RemoveElements","RemoveNodes","RemoveOrphanNodes","AddNode","Add0DElement","AddEdge","AddFace","AddPolygonalFace",
1803 "AddVolume","AddPolyhedralVolume","AddPolyhedralVolumeByFaces","MoveNode", "MoveClosestNodeToPoint",
1804 "InverseDiag","DeleteDiag","Reorient","ReorientObject","TriToQuad","SplitQuad","SplitQuadObject",
1805 "BestSplit","Smooth","SmoothObject","SmoothParametric","SmoothParametricObject",
1806 "ConvertToQuadratic","ConvertFromQuadratic","RenumberNodes","RenumberElements",
1807 "RotationSweep","RotationSweepObject","RotationSweepObject1D","RotationSweepObject2D",
1808 "ExtrusionSweep","AdvancedExtrusion","ExtrusionSweepObject","ExtrusionSweepObject1D","ExtrusionSweepObject2D",
1809 "ExtrusionAlongPath","ExtrusionAlongPathObject","ExtrusionAlongPathX",
1810 "ExtrusionAlongPathObject1D","ExtrusionAlongPathObject2D",
1811 "Mirror","MirrorObject","Translate","TranslateObject","Rotate","RotateObject",
1812 "FindCoincidentNodes",/*"FindCoincidentNodesOnPart",*/"MergeNodes","FindEqualElements",
1813 "MergeElements","MergeEqualElements","SewFreeBorders","SewConformFreeBorders",
1814 "SewBorderToSide","SewSideElements","ChangeElemNodes","GetLastCreatedNodes",
1815 "GetLastCreatedElems",
1816 "MirrorMakeMesh","MirrorObjectMakeMesh","TranslateMakeMesh",
1817 "TranslateObjectMakeMesh","RotateMakeMesh","RotateObjectMakeMesh","MakeBoundaryMesh",
1818 "MakeBoundaryElements"
1819 ,"" }; // <- mark of the end
1820 sameMethods.Insert( names );
1823 // names of SMESH_MeshEditor methods which differ from methods of class Mesh
1824 // only by last two arguments
1825 static TStringSet diffLastTwoArgsMethods;
1826 if (diffLastTwoArgsMethods.empty() ) {
1827 const char * names[] = {
1828 "MirrorMakeGroups","MirrorObjectMakeGroups",
1829 "TranslateMakeGroups","TranslateObjectMakeGroups",
1830 "RotateMakeGroups","RotateObjectMakeGroups",
1831 ""};// <- mark of the end
1832 diffLastTwoArgsMethods.Insert( names );
1835 const TCollection_AsciiString & method = theCommand->GetMethod();
1836 bool isPyMeshMethod = sameMethods.Contains( method );
1837 if ( !isPyMeshMethod )
1839 //Replace SMESH_MeshEditor "MakeGroups" functions by the Mesh
1840 //functions with the flag "theMakeGroups = True" like:
1841 //SMESH_MeshEditor.CmdMakeGroups => Mesh.Cmd(...,True)
1842 int pos = method.Search("MakeGroups");
1845 isPyMeshMethod = true;
1846 bool is0DmethId = ( method == "ExtrusionSweepMakeGroups0D" );
1847 bool is0DmethObj = ( method == "ExtrusionSweepObject0DMakeGroups");
1849 // 1. Remove "MakeGroups" from the Command
1850 TCollection_AsciiString aMethod = theCommand->GetMethod();
1851 int nbArgsToAdd = diffLastTwoArgsMethods.Contains(aMethod) ? 2 : 1;
1854 pos = pos-2; //Remove "0D" from the Command too
1855 aMethod.Trunc(pos-1);
1856 theCommand->SetMethod(aMethod);
1858 // 2. And add last "True" argument(s)
1859 while(nbArgsToAdd--)
1860 theCommand->SetArg(theCommand->GetNbArgs()+1,"True");
1861 if( is0DmethId || is0DmethObj )
1862 theCommand->SetArg(theCommand->GetNbArgs()+1,"True");
1866 // ExtrusionSweep0D() -> ExtrusionSweep()
1867 // ExtrusionSweepObject0D() -> ExtrusionSweepObject()
1868 if ( !isPyMeshMethod && ( method == "ExtrusionSweep0D" ||
1869 method == "ExtrusionSweepObject0D" ))
1871 isPyMeshMethod=true;
1872 theCommand->SetMethod( method.SubString( 1, method.Length()-2));
1873 theCommand->SetArg(theCommand->GetNbArgs()+1,"False"); //sets flag "MakeGroups = False"
1874 theCommand->SetArg(theCommand->GetNbArgs()+1,"True"); //sets flag "IsNode = True"
1876 // set "ExtrusionAlongPathX()" instead of "ExtrusionAlongPathObjX()"
1877 if ( !isPyMeshMethod && method == "ExtrusionAlongPathObjX")
1879 isPyMeshMethod=true;
1880 theCommand->SetMethod("ExtrusionAlongPathX");
1883 // set "FindCoincidentNodesOnPart()" instead of "FindCoincidentNodesOnPartBut()"
1884 if ( !isPyMeshMethod && method == "FindCoincidentNodesOnPartBut")
1886 isPyMeshMethod=true;
1887 theCommand->SetMethod("FindCoincidentNodesOnPart");
1889 // DoubleNodeElemGroupNew() -> DoubleNodeElemGroup()
1890 // DoubleNodeGroupNew() -> DoubleNodeGroup()
1891 // DoubleNodeGroupsNew() -> DoubleNodeGroups()
1892 // DoubleNodeElemGroupsNew() -> DoubleNodeElemGroups()
1893 if ( !isPyMeshMethod && ( method == "DoubleNodeElemGroupNew" ||
1894 method == "DoubleNodeElemGroupsNew" ||
1895 method == "DoubleNodeGroupNew" ||
1896 method == "DoubleNodeGroupsNew"))
1898 isPyMeshMethod=true;
1899 theCommand->SetMethod( method.SubString( 1, method.Length()-3));
1900 theCommand->SetArg(theCommand->GetNbArgs()+1,"True");
1902 // ConvertToQuadraticObject(bool,obj) -> ConvertToQuadratic(bool,obj)
1903 // ConvertFromQuadraticObject(obj) -> ConvertFromQuadratic(obj)
1904 if ( !isPyMeshMethod && ( method == "ConvertToQuadraticObject" ||
1905 method == "ConvertFromQuadraticObject" ))
1907 isPyMeshMethod=true;
1908 theCommand->SetMethod( method.SubString( 1, method.Length()-6));
1909 // prevent moving creation of the converted sub-mesh to the end of the script
1910 bool isFromQua = ( method.Value( 8 ) == 'F' );
1911 Handle(_pySubMesh) sm = theGen->FindSubMesh( theCommand->GetArg( isFromQua ? 1 : 2 ));
1913 sm->Process( theCommand );
1915 // FindAmongElementsByPoint(meshPart, x, y, z, elementType) ->
1916 // FindElementsByPoint(x, y, z, elementType, meshPart)
1917 if ( !isPyMeshMethod && method == "FindAmongElementsByPoint" )
1919 isPyMeshMethod=true;
1920 theCommand->SetMethod( "FindElementsByPoint" );
1921 // make the 1st arg be the last one
1922 _pyID partID = theCommand->GetArg( 1 );
1923 int nbArgs = theCommand->GetNbArgs();
1924 for ( int i = 2; i <= nbArgs; ++i )
1925 theCommand->SetArg( i-1, theCommand->GetArg( i ));
1926 theCommand->SetArg( nbArgs, partID );
1929 // meshes made by *MakeMesh() methods are not wrapped by _pyMesh,
1930 // so let _pyMesh care of it (TMP?)
1931 // if ( theCommand->GetMethod().Search("MakeMesh") != -1 )
1932 // _pyMesh( new _pyCommand( theCommand->GetString(), 0 )); // for theGen->SetAccessorMethod()
1933 if ( isPyMeshMethod )
1935 theCommand->SetObject( myMesh );
1939 // editor creation command is needed only if any editor function is called
1940 theGen->AddMeshAccessorMethod( theCommand ); // for *Object()
1941 if ( !myCreationCmdStr.IsEmpty() ) {
1942 GetCreationCmd()->GetString() = myCreationCmdStr;
1943 myCreationCmdStr.Clear();
1948 //================================================================================
1950 * \brief Return true if my mesh can be removed
1952 //================================================================================
1954 bool _pyMeshEditor::CanClear()
1956 Handle(_pyMesh) mesh = ObjectToMesh( theGen->FindObject( myMesh ));
1957 return mesh.IsNull() ? true : mesh->CanClear();
1960 //================================================================================
1962 * \brief _pyHypothesis constructor
1963 * \param theCreationCmd -
1965 //================================================================================
1967 _pyHypothesis::_pyHypothesis(const Handle(_pyCommand)& theCreationCmd):
1968 _pyObject( theCreationCmd ), myCurCrMethod(0)
1970 myIsAlgo = myIsWrapped = /*myIsConverted = myIsLocal = myDim = */false;
1973 //================================================================================
1975 * \brief Creates algorithm or hypothesis
1976 * \param theCreationCmd - The engine command creating a hypothesis
1977 * \retval Handle(_pyHypothesis) - Result _pyHypothesis
1979 //================================================================================
1981 Handle(_pyHypothesis) _pyHypothesis::NewHypothesis( const Handle(_pyCommand)& theCreationCmd)
1983 // theCreationCmd: CreateHypothesis( "theHypType", "theLibName" )
1984 ASSERT (( theCreationCmd->GetMethod() == "CreateHypothesis"));
1986 Handle(_pyHypothesis) hyp, algo;
1989 const TCollection_AsciiString & hypTypeQuoted = theCreationCmd->GetArg( 1 );
1990 if ( hypTypeQuoted.IsEmpty() )
1993 TCollection_AsciiString hypType =
1994 hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
1996 algo = new _pyAlgorithm( theCreationCmd );
1997 hyp = new _pyHypothesis( theCreationCmd );
1999 if ( hypType == "NumberOfSegments" ) {
2000 hyp = new _pyNumberOfSegmentsHyp( theCreationCmd );
2001 hyp->SetConvMethodAndType( "NumberOfSegments", "Regular_1D");
2002 // arg of SetNumberOfSegments() will become the 1-st arg of hyp creation command
2003 hyp->AddArgMethod( "SetNumberOfSegments" );
2004 // arg of SetScaleFactor() will become the 2-nd arg of hyp creation command
2005 hyp->AddArgMethod( "SetScaleFactor" );
2006 hyp->AddArgMethod( "SetReversedEdges" );
2007 // same for ""CompositeSegment_1D:
2008 hyp->SetConvMethodAndType( "NumberOfSegments", "CompositeSegment_1D");
2009 hyp->AddArgMethod( "SetNumberOfSegments" );
2010 hyp->AddArgMethod( "SetScaleFactor" );
2011 hyp->AddArgMethod( "SetReversedEdges" );
2013 else if ( hypType == "SegmentLengthAroundVertex" ) {
2014 hyp = new _pySegmentLengthAroundVertexHyp( theCreationCmd );
2015 hyp->SetConvMethodAndType( "LengthNearVertex", "Regular_1D" );
2016 hyp->AddArgMethod( "SetLength" );
2017 // same for ""CompositeSegment_1D:
2018 hyp->SetConvMethodAndType( "LengthNearVertex", "CompositeSegment_1D");
2019 hyp->AddArgMethod( "SetLength" );
2021 else if ( hypType == "LayerDistribution2D" ) {
2022 hyp = new _pyLayerDistributionHypo( theCreationCmd, "Get2DHypothesis" );
2023 hyp->SetConvMethodAndType( "LayerDistribution", "RadialQuadrangle_1D2D");
2025 else if ( hypType == "LayerDistribution" ) {
2026 hyp = new _pyLayerDistributionHypo( theCreationCmd, "Get3DHypothesis" );
2027 hyp->SetConvMethodAndType( "LayerDistribution", "RadialPrism_3D");
2029 else if ( hypType == "CartesianParameters3D" ) {
2030 hyp = new _pyComplexParamHypo( theCreationCmd );
2031 hyp->SetConvMethodAndType( "SetGrid", "Cartesian_3D");
2032 for ( int iArg = 0; iArg < 4; ++iArg )
2033 hyp->setCreationArg( iArg+1, "[]");
2037 hyp = theGen->GetHypothesisReader()->GetHypothesis( hypType, theCreationCmd );
2040 return algo->IsValid() ? algo : hyp;
2043 //================================================================================
2045 * \brief Returns true if addition of this hypothesis to a given mesh can be
2046 * wrapped into hypothesis creation
2048 //================================================================================
2050 bool _pyHypothesis::IsWrappable(const _pyID& theMesh) const
2052 if ( !myIsWrapped && myMesh == theMesh && IsInStudy() )
2054 Handle(_pyObject) pyMesh = theGen->FindObject( myMesh );
2055 if ( !pyMesh.IsNull() && pyMesh->IsInStudy() )
2061 //================================================================================
2063 * \brief Convert the command adding a hypothesis to mesh into a smesh command
2064 * \param theCmd - The command like mesh.AddHypothesis( geom, hypo )
2065 * \param theAlgo - The algo that can create this hypo
2066 * \retval bool - false if the command cant be converted
2068 //================================================================================
2070 bool _pyHypothesis::Addition2Creation( const Handle(_pyCommand)& theCmd,
2071 const _pyID& theMesh)
2073 ASSERT(( theCmd->GetMethod() == "AddHypothesis" ));
2075 if ( !IsWrappable( theMesh ))
2078 myGeom = theCmd->GetArg( 1 );
2080 Handle(_pyHypothesis) algo;
2082 // find algo created on myGeom in theMesh
2083 algo = theGen->FindAlgo( myGeom, theMesh, this );
2084 if ( algo.IsNull() )
2086 // attach hypothesis creation command to be after algo creation command
2087 // because it can be new created instance of algorithm
2088 algo->GetCreationCmd()->AddDependantCmd( theCmd );
2092 // mesh.AddHypothesis(geom,hyp) --> hyp = <theMesh or algo>.myCreationMethod(args)
2093 theCmd->SetResultValue( GetID() );
2094 theCmd->SetObject( IsAlgo() ? theMesh : algo->GetID());
2095 theCmd->SetMethod( IsAlgo() ? GetAlgoCreationMethod() : GetCreationMethod( algo->GetAlgoType() ));
2096 // set args (geom will be set by _pyMesh calling this method)
2097 theCmd->RemoveArgs();
2098 for ( size_t i = 0; i < myCurCrMethod->myArgs.size(); ++i ) {
2099 if ( !myCurCrMethod->myArgs[ i ].IsEmpty() )
2100 theCmd->SetArg( i+1, myCurCrMethod->myArgs[ i ]);
2102 theCmd->SetArg( i+1, "[]");
2104 // set a new creation command
2105 GetCreationCmd()->Clear();
2106 // replace creation command by wrapped instance
2107 // please note, that hypothesis attaches to algo creation command (see upper)
2108 SetCreationCmd( theCmd );
2111 // clear commands setting arg values
2112 list < Handle(_pyCommand) >::iterator argCmd = myArgCommands.begin();
2113 for ( ; argCmd != myArgCommands.end(); ++argCmd )
2116 // set unknown arg commands after hypo creation
2117 Handle(_pyCommand) afterCmd = myIsWrapped ? theCmd : GetCreationCmd();
2118 list<Handle(_pyCommand)>::iterator cmd = myUnusedCommands.begin();
2119 for ( ; cmd != myUnusedCommands.end(); ++cmd ) {
2120 afterCmd->AddDependantCmd( *cmd );
2126 //================================================================================
2128 * \brief Remember hypothesis parameter values
2129 * \param theCommand - The called hypothesis method
2131 //================================================================================
2133 void _pyHypothesis::Process( const Handle(_pyCommand)& theCommand)
2135 ASSERT( !myIsAlgo );
2136 if ( !theGen->IsToKeepAllCommands() )
2137 rememberCmdOfParameter( theCommand );
2139 bool usedCommand = false;
2140 TType2CrMethod::iterator type2meth = myAlgoType2CreationMethod.begin();
2141 for ( ; type2meth != myAlgoType2CreationMethod.end(); ++type2meth )
2143 CreationMethod& crMethod = type2meth->second;
2144 for ( size_t i = 0; i < crMethod.myArgMethods.size(); ++i ) {
2145 if ( crMethod.myArgMethods[ i ] == theCommand->GetMethod() ) {
2147 myArgCommands.push_back( theCommand );
2149 while ( crMethod.myArgs.size() < i+1 )
2150 crMethod.myArgs.push_back( "[]" );
2151 crMethod.myArgs[ i ] = theCommand->GetArg( crMethod.myArgNb[i] );
2156 myUnusedCommands.push_back( theCommand );
2159 //================================================================================
2161 * \brief Finish conversion
2163 //================================================================================
2165 void _pyHypothesis::Flush()
2169 list < Handle(_pyCommand) >::iterator cmd = myArgCommands.begin();
2170 for ( ; cmd != myArgCommands.end(); ++cmd ) {
2171 // Add access to a wrapped mesh
2172 theGen->AddMeshAccessorMethod( *cmd );
2173 // Add access to a wrapped algorithm
2174 theGen->AddAlgoAccessorMethod( *cmd );
2176 cmd = myUnusedCommands.begin();
2177 for ( ; cmd != myUnusedCommands.end(); ++cmd ) {
2178 // Add access to a wrapped mesh
2179 theGen->AddMeshAccessorMethod( *cmd );
2180 // Add access to a wrapped algorithm
2181 theGen->AddAlgoAccessorMethod( *cmd );
2184 // forget previous hypothesis modifications
2185 myArgCommands.clear();
2186 myUnusedCommands.clear();
2189 //================================================================================
2191 * \brief clear creation, arg and unkown commands
2193 //================================================================================
2195 void _pyHypothesis::ClearAllCommands()
2197 GetCreationCmd()->Clear();
2198 list<Handle(_pyCommand)>::iterator cmd = myArgCommands.begin();
2199 for ( ; cmd != myArgCommands.end(); ++cmd )
2201 cmd = myUnusedCommands.begin();
2202 for ( ; cmd != myUnusedCommands.end(); ++cmd )
2207 //================================================================================
2209 * \brief Assign fields of theOther to me except myIsWrapped
2211 //================================================================================
2213 void _pyHypothesis::Assign( const Handle(_pyHypothesis)& theOther,
2214 const _pyID& theMesh )
2216 // myCreationCmd = theOther->myCreationCmd;
2217 myIsAlgo = theOther->myIsAlgo;
2218 myIsWrapped = false;
2219 myGeom = theOther->myGeom;
2221 myAlgoType2CreationMethod = theOther->myAlgoType2CreationMethod;
2222 //myArgCommands = theOther->myArgCommands;
2223 //myUnusedCommands = theOther->myUnusedCommands;
2224 // init myCurCrMethod
2225 GetCreationMethod( theOther->GetAlgoType() );
2228 //================================================================================
2230 * \brief Analyze my erasability depending on myReferredObjs
2232 //================================================================================
2234 bool _pyHypothesis::CanClear()
2238 list< Handle(_pyObject) >::iterator obj = myReferredObjs.begin();
2239 for ( ; obj != myReferredObjs.end(); ++obj )
2240 if ( (*obj)->CanClear() )
2247 //================================================================================
2249 * \brief Clear my commands depending on usage by meshes
2251 //================================================================================
2253 void _pyHypothesis::ClearCommands()
2255 // if ( !theGen->IsToKeepAllCommands() )
2257 // bool isUsed = false;
2258 // int lastComputeOrder = 0;
2259 // list<Handle(_pyCommand) >::iterator cmd = myComputeCmds.begin();
2260 // for ( ; cmd != myComputeCmds.end(); ++cmd )
2261 // if ( ! (*cmd)->IsEmpty() )
2264 // if ( (*cmd)->GetOrderNb() > lastComputeOrder )
2265 // lastComputeOrder = (*cmd)->GetOrderNb();
2269 // SetRemovedFromStudy( true );
2273 // // clear my commands invoked after lastComputeOrder
2274 // // map<TCollection_AsciiString, list< Handle(_pyCommand) > >::iterator m2c;
2275 // // for ( m2c = myMeth2Commands.begin(); m2c != myMeth2Commands.end(); ++m2c )
2277 // // list< Handle(_pyCommand)> & cmds = m2c->second;
2278 // // if ( !cmds.empty() && cmds.back()->GetOrderNb() > lastComputeOrder )
2279 // // cmds.back()->Clear();
2283 _pyObject::ClearCommands();
2286 //================================================================================
2288 * \brief Find arguments that are objects like mesh, group, geometry
2289 * \param meshes - referred meshes (directly or indirrectly)
2290 * \retval bool - false if a referred geometry is not in the study
2292 //================================================================================
2294 bool _pyHypothesis::GetReferredMeshesAndGeom( list< Handle(_pyMesh) >& meshes )
2296 if ( IsAlgo() ) return true;
2298 bool geomPublished = true;
2299 vector< _AString > args;
2300 TType2CrMethod::iterator type2meth = myAlgoType2CreationMethod.begin();
2301 for ( ; type2meth != myAlgoType2CreationMethod.end(); ++type2meth )
2303 CreationMethod& crMethod = type2meth->second;
2304 args.insert( args.end(), crMethod.myArgs.begin(), crMethod.myArgs.end());
2306 list<Handle(_pyCommand)>::iterator cmd = myUnusedCommands.begin();
2307 for ( ; cmd != myUnusedCommands.end(); ++cmd ) {
2308 for ( int nb = (*cmd)->GetNbArgs(); nb; --nb )
2309 args.push_back( (*cmd)->GetArg( nb ));
2312 for ( size_t i = 0; i < args.size(); ++i )
2314 list< _pyID > idList = _pyCommand::GetStudyEntries( args[ i ]);
2315 if ( idList.empty() && !args[ i ].IsEmpty() )
2316 idList.push_back( args[ i ]);
2317 list< _pyID >::iterator id = idList.begin();
2318 for ( ; id != idList.end(); ++id )
2320 Handle(_pyObject) obj = theGen->FindObject( *id );
2321 if ( obj.IsNull() ) obj = theGen->FindHyp( *id );
2324 if ( theGen->IsGeomObject( *id ) && theGen->IsNotPublished( *id ))
2325 geomPublished = false;
2329 myReferredObjs.push_back( obj );
2330 Handle(_pyMesh) mesh = ObjectToMesh( obj );
2331 if ( !mesh.IsNull() )
2332 meshes.push_back( mesh );
2333 // prevent clearing not published hyps referred e.g. by "LayerDistribution"
2334 else if ( obj->IsKind( STANDARD_TYPE( _pyHypothesis )) && this->IsInStudy() )
2335 obj->SetRemovedFromStudy( false );
2339 return geomPublished;
2342 //================================================================================
2344 * \brief Remember theCommand setting a parameter
2346 //================================================================================
2348 void _pyHypothesis::rememberCmdOfParameter( const Handle(_pyCommand) & theCommand )
2350 // parameters are discriminated by method name
2351 TCollection_AsciiString method = theCommand->GetMethod();
2353 // discriminate commands setting different parameters via one method
2354 // by passing parameter names like e.g. SetOption("size", "0.2")
2355 if ( theCommand->GetString().FirstLocationInSet( "'\"", 1, theCommand->Length() ) &&
2356 theCommand->GetNbArgs() > 1 )
2358 // mangle method by appending a 1st textual arg
2359 for ( int iArg = 1; iArg <= theCommand->GetNbArgs(); ++iArg )
2361 const TCollection_AsciiString& arg = theCommand->GetArg( iArg );
2362 if ( arg.Value(1) != '\"' && arg.Value(1) != '\'' ) continue;
2363 if ( !isalpha( arg.Value(2))) continue;
2368 // parameters are discriminated by method name
2369 list< Handle(_pyCommand)>& cmds = myMeth2Commands[ theCommand->GetMethod() ];
2370 if ( !cmds.empty() && !isCmdUsedForCompute( cmds.back() ))
2372 cmds.back()->Clear(); // previous parameter value has not been used
2373 cmds.back() = theCommand;
2377 cmds.push_back( theCommand );
2381 //================================================================================
2383 * \brief Return true if a setting parameter command ha been used to compute mesh
2385 //================================================================================
2387 bool _pyHypothesis::isCmdUsedForCompute( const Handle(_pyCommand) & cmd,
2388 _pyCommand::TAddr avoidComputeAddr ) const
2390 bool isUsed = false;
2391 map< _pyCommand::TAddr, list<Handle(_pyCommand) > >::const_iterator addr2cmds =
2392 myComputeAddr2Cmds.begin();
2393 for ( ; addr2cmds != myComputeAddr2Cmds.end() && !isUsed; ++addr2cmds )
2395 if ( addr2cmds->first == avoidComputeAddr ) continue;
2396 const list<Handle(_pyCommand)> & cmds = addr2cmds->second;
2397 isUsed = ( std::find( cmds.begin(), cmds.end(), cmd ) != cmds.end() );
2402 //================================================================================
2404 * \brief Save commands setting parameters as they are used for a mesh computation
2406 //================================================================================
2408 void _pyHypothesis::MeshComputed( const Handle(_pyCommand)& theComputeCmd )
2410 myComputeCmds.push_back( theComputeCmd );
2411 list<Handle(_pyCommand)>& savedCmds = myComputeAddr2Cmds[ theComputeCmd->GetAddress() ];
2413 map<TCollection_AsciiString, list< Handle(_pyCommand) > >::iterator m2c;
2414 for ( m2c = myMeth2Commands.begin(); m2c != myMeth2Commands.end(); ++m2c )
2415 savedCmds.push_back( m2c->second.back() );
2418 //================================================================================
2420 * \brief Clear commands setting parameters as a mesh computed using them is cleared
2422 //================================================================================
2424 void _pyHypothesis::ComputeDiscarded( const Handle(_pyCommand)& theComputeCmd )
2426 list<Handle(_pyCommand)>& savedCmds = myComputeAddr2Cmds[ theComputeCmd->GetAddress() ];
2428 list<Handle(_pyCommand)>::iterator cmd = savedCmds.begin();
2429 for ( ; cmd != savedCmds.end(); ++cmd )
2431 // check if a cmd has been used to compute another mesh
2432 if ( isCmdUsedForCompute( *cmd, theComputeCmd->GetAddress() ))
2434 // check if a cmd is a sole command setting its parameter;
2435 // don't use method name for search as it can change
2436 map<TCollection_AsciiString, list<Handle(_pyCommand)> >::iterator
2437 m2cmds = myMeth2Commands.begin();
2438 for ( ; m2cmds != myMeth2Commands.end(); ++m2cmds )
2440 list< Handle(_pyCommand)>& cmds = m2cmds->second;
2441 list< Handle(_pyCommand)>::iterator cmdIt = std::find( cmds.begin(), cmds.end(), *cmd );
2442 if ( cmdIt != cmds.end() )
2444 if ( cmds.back() != *cmd )
2446 cmds.erase( cmdIt );
2453 myComputeAddr2Cmds.erase( theComputeCmd->GetAddress() );
2456 //================================================================================
2458 * \brief Sets an argNb-th argument of current creation command
2459 * \param argNb - argument index countered from 1
2461 //================================================================================
2463 void _pyHypothesis::setCreationArg( const int argNb, const _AString& arg )
2465 if ( myCurCrMethod )
2467 while ( myCurCrMethod->myArgs.size() < argNb )
2468 myCurCrMethod->myArgs.push_back( "None" );
2469 if ( arg.IsEmpty() )
2470 myCurCrMethod->myArgs[ argNb-1 ] = "None";
2472 myCurCrMethod->myArgs[ argNb-1 ] = arg;
2477 //================================================================================
2479 * \brief Remember hypothesis parameter values
2480 * \param theCommand - The called hypothesis method
2482 //================================================================================
2484 void _pyComplexParamHypo::Process( const Handle(_pyCommand)& theCommand)
2486 if ( GetAlgoType() == "Cartesian_3D" )
2488 // CartesianParameters3D hyp
2490 if ( theCommand->GetMethod() == "SetSizeThreshold" )
2492 setCreationArg( 4, theCommand->GetArg( 1 ));
2493 myArgCommands.push_back( theCommand );
2496 if ( theCommand->GetMethod() == "SetGrid" ||
2497 theCommand->GetMethod() == "SetGridSpacing" )
2499 TCollection_AsciiString axis = theCommand->GetArg( theCommand->GetNbArgs() );
2500 int iArg = axis.Value(1) - '0';
2501 if ( theCommand->GetMethod() == "SetGrid" )
2503 setCreationArg( 1+iArg, theCommand->GetArg( 1 ));
2507 myCurCrMethod->myArgs[ iArg ] = "[ ";
2508 myCurCrMethod->myArgs[ iArg ] += theCommand->GetArg( 1 );
2509 myCurCrMethod->myArgs[ iArg ] += ", ";
2510 myCurCrMethod->myArgs[ iArg ] += theCommand->GetArg( 2 );
2511 myCurCrMethod->myArgs[ iArg ] += "]";
2513 myArgCommands.push_back( theCommand );
2514 rememberCmdOfParameter( theCommand );
2519 if( theCommand->GetMethod() == "SetLength" )
2521 // NOW it is OBSOLETE
2522 // ex: hyp.SetLength(start, 1)
2523 // hyp.SetLength(end, 0)
2524 ASSERT(( theCommand->GetArg( 2 ).IsIntegerValue() ));
2525 int i = 1 - theCommand->GetArg( 2 ).IntegerValue();
2526 TType2CrMethod::iterator type2meth = myAlgoType2CreationMethod.begin();
2527 for ( ; type2meth != myAlgoType2CreationMethod.end(); ++type2meth )
2529 CreationMethod& crMethod = type2meth->second;
2530 while ( crMethod.myArgs.size() < i+1 )
2531 crMethod.myArgs.push_back( "[]" );
2532 crMethod.myArgs[ i ] = theCommand->GetArg( 1 ); // arg value
2534 myArgCommands.push_back( theCommand );
2538 _pyHypothesis::Process( theCommand );
2541 //================================================================================
2543 * \brief Clear SetObjectEntry() as it is called by methods of Mesh_Segment
2545 //================================================================================
2547 void _pyComplexParamHypo::Flush()
2551 list < Handle(_pyCommand) >::iterator cmd = myUnusedCommands.begin();
2552 for ( ; cmd != myUnusedCommands.end(); ++cmd )
2553 if ((*cmd)->GetMethod() == "SetObjectEntry" )
2558 //================================================================================
2560 * \brief Convert methods of 1D hypotheses to my own methods
2561 * \param theCommand - The called hypothesis method
2563 //================================================================================
2565 void _pyLayerDistributionHypo::Process( const Handle(_pyCommand)& theCommand)
2567 if ( theCommand->GetMethod() != "SetLayerDistribution" )
2570 const _pyID& hyp1dID = theCommand->GetArg( 1 );
2571 // Handle(_pyHypothesis) hyp1d = theGen->FindHyp( hyp1dID );
2572 // if ( hyp1d.IsNull() && ! my1dHyp.IsNull()) // apparently hypId changed at study restoration
2574 // TCollection_AsciiString cmd =
2575 // my1dHyp->GetCreationCmd()->GetIndentation() + hyp1dID + " = " + my1dHyp->GetID();
2576 // Handle(_pyCommand) newCmd = theGen->AddCommand( cmd );
2577 // theGen->SetCommandAfter( newCmd, my1dHyp->GetCreationCmd() );
2580 // else if ( !my1dHyp.IsNull() && hyp1dID != my1dHyp->GetID() )
2582 // // 1D hypo is already set, so distribution changes and the old
2583 // // 1D hypo is thrown away
2584 // my1dHyp->ClearAllCommands();
2587 // //my1dHyp->SetRemovedFromStudy( false );
2589 // if ( !myArgCommands.empty() )
2590 // myArgCommands.back()->Clear();
2591 myCurCrMethod->myArgs.push_back( hyp1dID );
2592 myArgCommands.push_back( theCommand );
2595 //================================================================================
2598 * \param theAdditionCmd - command to be converted
2599 * \param theMesh - mesh instance
2600 * \retval bool - status
2602 //================================================================================
2604 bool _pyLayerDistributionHypo::Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
2605 const _pyID& theMesh)
2607 myIsWrapped = false;
2609 if ( my1dHyp.IsNull() )
2612 // set "SetLayerDistribution()" after addition cmd
2613 theAdditionCmd->AddDependantCmd( myArgCommands.front() );
2615 _pyID geom = theAdditionCmd->GetArg( 1 );
2617 Handle(_pyHypothesis) algo = theGen->FindAlgo( geom, theMesh, this );
2618 if ( !algo.IsNull() )
2620 my1dHyp->SetMesh( theMesh );
2621 my1dHyp->SetConvMethodAndType(my1dHyp->GetAlgoCreationMethod().ToCString(),
2622 algo->GetAlgoType().ToCString());
2623 if ( !my1dHyp->Addition2Creation( theAdditionCmd, theMesh ))
2626 // clear "SetLayerDistribution()" cmd
2627 myArgCommands.back()->Clear();
2629 // Convert my creation => me = RadialPrismAlgo.Get3DHypothesis()
2631 // find RadialPrism algo created on <geom> for theMesh
2632 GetCreationCmd()->SetObject( algo->GetID() );
2633 GetCreationCmd()->SetMethod( myAlgoMethod );
2634 GetCreationCmd()->RemoveArgs();
2635 theAdditionCmd->AddDependantCmd( GetCreationCmd() );
2641 //================================================================================
2645 //================================================================================
2647 void _pyLayerDistributionHypo::Flush()
2649 // as creation of 1D hyp was written later then it's edition,
2650 // we need to find all it's edition calls and process them
2651 list< Handle(_pyCommand) >::iterator cmd = myArgCommands.begin();
2653 for ( cmd = myArgCommands.begin(); cmd != myArgCommands.end(); ++cmd )
2655 const _pyID& hyp1dID = (*cmd)->GetArg( 1 );
2656 if ( hyp1dID.IsEmpty() ) continue;
2658 Handle(_pyHypothesis) hyp1d = theGen->FindHyp( hyp1dID );
2660 // make a new name for 1D hyp = "HypType" + "_Distribution"
2662 if ( hyp1d.IsNull() ) // apparently hypId changed at study restoration
2664 if ( prevNewName.IsEmpty() ) continue;
2665 newName = prevNewName;
2669 if ( hyp1d->IsWrapped() ) {
2670 newName = hyp1d->GetCreationCmd()->GetMethod();
2673 TCollection_AsciiString hypTypeQuoted = hyp1d->GetCreationCmd()->GetArg(1);
2674 newName = hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
2676 newName += "_Distribution";
2677 prevNewName = newName;
2679 hyp1d->GetCreationCmd()->SetResultValue( newName );
2681 list< Handle(_pyCommand) >& cmds = theGen->GetCommands();
2682 list< Handle(_pyCommand) >::iterator cmdIt = cmds.begin();
2683 for ( ; cmdIt != cmds.end(); ++cmdIt ) {
2684 const _pyID& objID = (*cmdIt)->GetObject();
2685 if ( objID == hyp1dID ) {
2686 if ( !hyp1d.IsNull() )
2688 hyp1d->Process( *cmdIt );
2689 hyp1d->GetCreationCmd()->AddDependantCmd( *cmdIt );
2691 ( *cmdIt )->SetObject( newName );
2694 // Set new hyp name to SetLayerDistribution(hyp1dID) cmd
2695 (*cmd)->SetArg( 1, newName );
2699 //================================================================================
2701 * \brief additionally to Addition2Creation, clears SetDistrType() command
2702 * \param theCmd - AddHypothesis() command
2703 * \param theMesh - mesh to which a hypothesis is added
2704 * \retval bool - convertion result
2706 //================================================================================
2708 bool _pyNumberOfSegmentsHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
2709 const _pyID& theMesh)
2711 if ( IsWrappable( theMesh ) && myCurCrMethod->myArgs.size() > 1 ) {
2712 // scale factor (2-nd arg) is provided: clear SetDistrType(1) command
2713 bool scaleDistrType = false;
2714 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnusedCommands.rbegin();
2715 for ( ; cmd != myUnusedCommands.rend(); ++cmd ) {
2716 if ( (*cmd)->GetMethod() == "SetDistrType" ) {
2717 if ( (*cmd)->GetArg( 1 ) == "1" ) {
2718 scaleDistrType = true;
2721 else if ( !scaleDistrType ) {
2722 // distribution type changed: remove scale factor from args
2723 TType2CrMethod::iterator type2meth = myAlgoType2CreationMethod.begin();
2724 for ( ; type2meth != myAlgoType2CreationMethod.end(); ++type2meth )
2726 CreationMethod& crMethod = type2meth->second;
2727 if ( crMethod.myArgs.size() == 2 )
2728 crMethod.myArgs.pop_back();
2735 return _pyHypothesis::Addition2Creation( theCmd, theMesh );
2738 //================================================================================
2740 * \brief remove repeated commands defining distribution
2742 //================================================================================
2744 void _pyNumberOfSegmentsHyp::Flush()
2746 // find number of the last SetDistrType() command
2747 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnusedCommands.rbegin();
2748 int distrTypeNb = 0;
2749 for ( ; !distrTypeNb && cmd != myUnusedCommands.rend(); ++cmd )
2750 if ( (*cmd)->GetMethod() == "SetDistrType" )
2751 distrTypeNb = (*cmd)->GetOrderNb();
2752 else if (IsWrapped() && (*cmd)->GetMethod() == "SetObjectEntry" )
2755 // clear commands before the last SetDistrType()
2756 list<Handle(_pyCommand)> * cmds[2] = { &myArgCommands, &myUnusedCommands };
2757 for ( int i = 0; i < 2; ++i ) {
2758 set<TCollection_AsciiString> uniqueMethods;
2759 list<Handle(_pyCommand)> & cmdList = *cmds[i];
2760 for ( cmd = cmdList.rbegin(); cmd != cmdList.rend(); ++cmd )
2762 bool clear = ( (*cmd)->GetOrderNb() < distrTypeNb );
2763 const TCollection_AsciiString& method = (*cmd)->GetMethod();
2764 if ( !clear || method == "SetNumberOfSegments" ) {
2765 bool isNewInSet = uniqueMethods.insert( method ).second;
2766 clear = !isNewInSet;
2775 //================================================================================
2777 * \brief Convert the command adding "SegmentLengthAroundVertex" to mesh
2778 * into regular1D.LengthNearVertex( length, vertex )
2779 * \param theCmd - The command like mesh.AddHypothesis( vertex, SegmentLengthAroundVertex )
2780 * \param theMesh - The mesh needing this hypo
2781 * \retval bool - false if the command cant be converted
2783 //================================================================================
2785 bool _pySegmentLengthAroundVertexHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
2786 const _pyID& theMeshID)
2788 if ( IsWrappable( theMeshID )) {
2790 _pyID vertex = theCmd->GetArg( 1 );
2792 // the problem here is that segment algo will not be found
2793 // by pyHypothesis::Addition2Creation() for <vertex>, so we try to find
2794 // geometry where segment algorithm is assigned
2795 Handle(_pyHypothesis) algo;
2796 _pyID geom = vertex;
2797 while ( algo.IsNull() && !geom.IsEmpty()) {
2798 // try to find geom as a father of <vertex>
2799 geom = FatherID( geom );
2800 algo = theGen->FindAlgo( geom, theMeshID, this );
2802 if ( algo.IsNull() )
2803 return false; // also possible to find geom as brother of veretex...
2804 // set geom instead of vertex
2805 theCmd->SetArg( 1, geom );
2807 // set vertex as a second arg
2808 if ( myCurCrMethod->myArgs.size() < 1) setCreationArg( 1, "1" ); // :(
2809 setCreationArg( 2, vertex );
2811 // mesh.AddHypothesis(vertex, SegmentLengthAroundVertex) -->
2812 // theMeshID.LengthNearVertex( length, vertex )
2813 return _pyHypothesis::Addition2Creation( theCmd, theMeshID );
2818 //================================================================================
2820 * \brief _pyAlgorithm constructor
2821 * \param theCreationCmd - The command like "algo = smeshgen.CreateHypothesis(type,lib)"
2823 //================================================================================
2825 _pyAlgorithm::_pyAlgorithm(const Handle(_pyCommand)& theCreationCmd)
2826 : _pyHypothesis( theCreationCmd )
2831 //================================================================================
2833 * \brief Convert the command adding an algorithm to mesh
2834 * \param theCmd - The command like mesh.AddHypothesis( geom, algo )
2835 * \param theMesh - The mesh needing this algo
2836 * \retval bool - false if the command cant be converted
2838 //================================================================================
2840 bool _pyAlgorithm::Addition2Creation( const Handle(_pyCommand)& theCmd,
2841 const _pyID& theMeshID)
2843 // mesh.AddHypothesis(geom,algo) --> theMeshID.myCreationMethod()
2844 if ( _pyHypothesis::Addition2Creation( theCmd, theMeshID )) {
2845 theGen->SetAccessorMethod( GetID(), "GetAlgorithm()" );
2851 //================================================================================
2853 * \brief Return starting position of a part of python command
2854 * \param thePartIndex - The index of command part
2855 * \retval int - Part position
2857 //================================================================================
2859 int _pyCommand::GetBegPos( int thePartIndex )
2863 if ( myBegPos.Length() < thePartIndex )
2865 return myBegPos( thePartIndex );
2868 //================================================================================
2870 * \brief Store starting position of a part of python command
2871 * \param thePartIndex - The index of command part
2872 * \param thePosition - Part position
2874 //================================================================================
2876 void _pyCommand::SetBegPos( int thePartIndex, int thePosition )
2878 while ( myBegPos.Length() < thePartIndex )
2879 myBegPos.Append( UNKNOWN );
2880 myBegPos( thePartIndex ) = thePosition;
2883 //================================================================================
2885 * \brief Returns whitespace symbols at the line beginning
2886 * \retval TCollection_AsciiString - result
2888 //================================================================================
2890 TCollection_AsciiString _pyCommand::GetIndentation()
2893 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
2894 GetWord( myString, end, true );
2896 end = GetBegPos( RESULT_IND );
2897 return myString.SubString( 1, end - 1 );
2900 //================================================================================
2902 * \brief Return substring of python command looking like ResultValue = Obj.Meth()
2903 * \retval const TCollection_AsciiString & - ResultValue substring
2905 //================================================================================
2907 const TCollection_AsciiString & _pyCommand::GetResultValue()
2909 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
2911 SetBegPos( RESULT_IND, EMPTY );
2912 int begPos, endPos = myString.Location( "=", 1, Length() );
2916 while ( begPos < endPos && isspace( myString.Value( begPos ))) ++begPos;
2917 if ( begPos < endPos )
2919 SetBegPos( RESULT_IND, begPos );
2921 while ( begPos < endPos && isspace( myString.Value( endPos ))) --endPos;
2922 myRes = myString.SubString( begPos, endPos );
2929 //================================================================================
2931 * \brief Return number of python command result value ResultValue = Obj.Meth()
2934 //================================================================================
2936 const int _pyCommand::GetNbResultValues()
2940 int endPos = myString.Location( "=", 1, Length() );
2941 TCollection_AsciiString str = "";
2942 while ( begPos < endPos) {
2943 str = GetWord( myString, begPos, true );
2944 begPos = begPos+ str.Length();
2951 //================================================================================
2953 * \brief Return substring of python command looking like
2954 * ResultValue1 , ResultValue1,... = Obj.Meth() with res index
2955 * \retval const TCollection_AsciiString & - ResultValue with res index substring
2957 //================================================================================
2958 const TCollection_AsciiString & _pyCommand::GetResultValue(int res)
2962 int endPos = myString.Location( "=", 1, Length() );
2963 while ( begPos < endPos) {
2964 myRes = GetWord( myString, begPos, true );
2965 begPos = begPos + myRes.Length();
2968 myRes.RemoveAll('[');myRes.RemoveAll(']');
2974 return theEmptyString;
2977 //================================================================================
2979 * \brief Return substring of python command looking like ResVal = Object.Meth()
2980 * \retval const TCollection_AsciiString & - Object substring
2982 //================================================================================
2984 const TCollection_AsciiString & _pyCommand::GetObject()
2986 if ( GetBegPos( OBJECT_IND ) == UNKNOWN )
2989 int begPos = GetBegPos( RESULT_IND ) + myRes.Length();
2991 begPos = myString.Location( "=", 1, Length() ) + 1;
2992 // is '=' in the string argument (for example, name) or not
2993 int nb1 = 0; // number of ' character at the left of =
2994 int nb2 = 0; // number of " character at the left of =
2995 for ( int i = 1; i < begPos-1; i++ ) {
2996 if ( myString.Value( i )=='\'' )
2998 else if ( myString.Value( i )=='"' )
3001 // if number of ' or " is not divisible by 2,
3002 // then get an object at the start of the command
3003 if ( nb1 % 2 != 0 || nb2 % 2 != 0 )
3006 myObj = GetWord( myString, begPos, true );
3007 // check if object is complex,
3008 // so far consider case like "smesh.smesh.Method()"
3009 if ( int bracketPos = myString.Location( "(", begPos, Length() )) {
3010 //if ( bracketPos==0 ) bracketPos = Length();
3011 int dotPos = begPos+myObj.Length();
3012 while ( dotPos+1 < bracketPos ) {
3013 if ( int pos = myString.Location( ".", dotPos+1, bracketPos ))
3018 if ( dotPos > begPos+myObj.Length() )
3019 myObj = myString.SubString( begPos, dotPos-1 );
3021 // 1st word after '=' is an object
3022 // else // no method -> no object
3028 SetBegPos( OBJECT_IND, begPos );
3034 //================================================================================
3036 * \brief Return substring of python command looking like ResVal = Obj.Method()
3037 * \retval const TCollection_AsciiString & - Method substring
3039 //================================================================================
3041 const TCollection_AsciiString & _pyCommand::GetMethod()
3043 if ( GetBegPos( METHOD_IND ) == UNKNOWN )
3046 int begPos = GetBegPos( OBJECT_IND ) + myObj.Length();
3047 bool forward = true;
3049 begPos = myString.Location( "(", 1, Length() ) - 1;
3053 myMeth = GetWord( myString, begPos, forward );
3054 SetBegPos( METHOD_IND, begPos );
3060 //================================================================================
3062 * \brief Return substring of python command looking like ResVal = Obj.Meth(Arg1,...)
3063 * \retval const TCollection_AsciiString & - Arg<index> substring
3065 //================================================================================
3067 const TCollection_AsciiString & _pyCommand::GetArg( int index )
3069 if ( GetBegPos( ARG1_IND ) == UNKNOWN )
3073 int pos = GetBegPos( METHOD_IND ) + myMeth.Length();
3075 pos = myString.Location( "(", 1, Length() );
3079 // we are at or before '(', skip it if present
3081 while ( pos <= Length() && myString.Value( pos ) != '(' ) ++pos;
3082 if ( pos > Length() )
3086 SetBegPos( ARG1_IND, 0 ); // even no '('
3087 return theEmptyString;
3091 list< TCollection_AsciiString > separatorStack( 1, ",)");
3092 bool ignoreNesting = false;
3094 while ( pos <= Length() )
3096 const char chr = myString.Value( pos );
3098 if ( separatorStack.back().Location( chr, 1, separatorStack.back().Length()))
3100 if ( separatorStack.size() == 1 ) // a comma dividing args or a terminal ')' found
3102 while ( pos-1 >= prevPos && isspace( myString.Value( prevPos )))
3104 TCollection_AsciiString arg;
3105 if ( pos-1 >= prevPos ) {
3106 arg = myString.SubString( prevPos, pos-1 );
3107 arg.RightAdjust(); // remove spaces
3110 if ( !arg.IsEmpty() || chr == ',' )
3112 SetBegPos( ARG1_IND + myArgs.Length(), prevPos );
3113 myArgs.Append( arg );
3119 else // end of nesting args found
3121 separatorStack.pop_back();
3122 ignoreNesting = false;
3125 else if ( !ignoreNesting )
3128 case '(' : separatorStack.push_back(")"); break;
3129 case '[' : separatorStack.push_back("]"); break;
3130 case '\'': separatorStack.push_back("'"); ignoreNesting=true; break;
3131 case '"' : separatorStack.push_back("\""); ignoreNesting=true; break;
3138 if ( myArgs.Length() < index )
3139 return theEmptyString;
3140 return myArgs( index );
3143 //================================================================================
3145 * \brief Check if char is a word part
3146 * \param c - The character to check
3147 * \retval bool - The check result
3149 //================================================================================
3151 static inline bool isWord(const char c, const bool dotIsWord)
3154 !isspace(c) && c != ',' && c != '=' && c != ')' && c != '(' && ( dotIsWord || c != '.');
3157 //================================================================================
3159 * \brief Looks for a word in the string and returns word's beginning
3160 * \param theString - The input string
3161 * \param theStartPos - The position to start the search, returning word's beginning
3162 * \param theForward - The search direction
3163 * \retval TCollection_AsciiString - The found word
3165 //================================================================================
3167 TCollection_AsciiString _pyCommand::GetWord( const TCollection_AsciiString & theString,
3169 const bool theForward,
3170 const bool dotIsWord )
3172 int beg = theStartPos, end = theStartPos;
3173 theStartPos = EMPTY;
3174 if ( beg < 1 || beg > theString.Length() )
3175 return theEmptyString;
3177 if ( theForward ) { // search forward
3179 while ( beg <= theString.Length() && !isWord( theString.Value( beg ), dotIsWord))
3181 if ( beg > theString.Length() )
3182 return theEmptyString; // no word found
3185 char begChar = theString.Value( beg );
3186 if ( begChar == '"' || begChar == '\'' || begChar == '[') {
3187 char endChar = ( begChar == '[' ) ? ']' : begChar;
3188 // end is at the corresponding quoting mark or bracket
3189 while ( end < theString.Length() &&
3190 ( theString.Value( end ) != endChar || theString.Value( end-1 ) == '\\'))
3194 while ( end <= theString.Length() && isWord( theString.Value( end ), dotIsWord))
3199 else { // search backward
3201 while ( end > 0 && !isWord( theString.Value( end ), dotIsWord))
3204 return theEmptyString; // no word found
3206 char endChar = theString.Value( end );
3207 if ( endChar == '"' || endChar == '\'' || endChar == ']') {
3208 char begChar = ( endChar == ']' ) ? '[' : endChar;
3209 // beg is at the corresponding quoting mark
3211 ( theString.Value( beg ) != begChar || theString.Value( beg-1 ) == '\\'))
3215 while ( beg > 0 && isWord( theString.Value( beg ), dotIsWord))
3221 //cout << theString << " ---- " << beg << " - " << end << endl;
3222 return theString.SubString( beg, end );
3225 //================================================================================
3227 * \brief Returns true if the string looks like a study entry
3229 //================================================================================
3231 bool _pyCommand::IsStudyEntry( const TCollection_AsciiString& str )
3233 if ( str.Length() < 5 ) return false;
3235 int nbColons = 0, isColon;
3236 for ( int i = 1; i <= str.Length(); ++i )
3238 char c = str.Value(i);
3239 if (!( isColon = (c == ':')) && ( c < '0' || c > '9' ))
3241 nbColons += isColon;
3243 return nbColons > 2 && str.Length()-nbColons > 2;
3246 //================================================================================
3248 * \brief Finds entries in a sting
3250 //================================================================================
3252 std::list< _pyID > _pyCommand::GetStudyEntries( const TCollection_AsciiString& str )
3254 std::list< _pyID > resList;
3256 while ( ++pos <= str.Length() )
3258 if ( !isdigit( str.Value( pos ))) continue;
3259 if ( pos != 1 && ( isalpha( str.Value( pos-1 ) || str.Value( pos-1 ) == ':'))) continue;
3262 while ( ++end <= str.Length() && ( isdigit( str.Value( end )) || str.Value( end ) == ':' ));
3263 _pyID entry = str.SubString( pos, end-1 );
3265 if ( IsStudyEntry( entry ))
3266 resList.push_back( entry );
3271 //================================================================================
3273 * \brief Look for position where not space char is
3274 * \param theString - The string
3275 * \param thePos - The position to search from and which returns result
3276 * \retval bool - false if there are only space after thePos in theString
3280 //================================================================================
3282 bool _pyCommand::SkipSpaces( const TCollection_AsciiString & theString, int & thePos )
3284 if ( thePos < 1 || thePos > theString.Length() )
3287 while ( thePos <= theString.Length() && isspace( theString.Value( thePos )))
3290 return thePos <= theString.Length();
3293 //================================================================================
3295 * \brief Modify a part of the command
3296 * \param thePartIndex - The index of the part
3297 * \param thePart - The new part string
3298 * \param theOldPart - The old part
3300 //================================================================================
3302 void _pyCommand::SetPart(int thePartIndex, const TCollection_AsciiString& thePart,
3303 TCollection_AsciiString& theOldPart)
3305 int pos = GetBegPos( thePartIndex );
3306 if ( pos <= Length() && theOldPart != thePart)
3308 TCollection_AsciiString seperator;
3310 pos = GetBegPos( thePartIndex + 1 );
3311 if ( pos < 1 ) return;
3312 switch ( thePartIndex ) {
3313 case RESULT_IND: seperator = " = "; break;
3314 case OBJECT_IND: seperator = "."; break;
3315 case METHOD_IND: seperator = "()"; break;
3319 myString.Remove( pos, theOldPart.Length() );
3320 if ( !seperator.IsEmpty() )
3321 myString.Insert( pos , seperator );
3322 myString.Insert( pos, thePart );
3323 // update starting positions of the following parts
3324 int posDelta = thePart.Length() + seperator.Length() - theOldPart.Length();
3325 for ( int i = thePartIndex + 1; i <= myBegPos.Length(); ++i ) {
3326 if ( myBegPos( i ) > 0 )
3327 myBegPos( i ) += posDelta;
3329 theOldPart = thePart;
3333 //================================================================================
3335 * \brief Set agrument
3336 * \param index - The argument index, it counts from 1
3337 * \param theArg - The argument string
3339 //================================================================================
3341 void _pyCommand::SetArg( int index, const TCollection_AsciiString& theArg)
3344 int argInd = ARG1_IND + index - 1;
3345 int pos = GetBegPos( argInd );
3346 if ( pos < 1 ) // no index-th arg exist, append inexistent args
3348 // find a closing parenthesis
3349 if ( GetNbArgs() != 0 && index <= GetNbArgs() ) {
3350 int lastArgInd = GetNbArgs();
3351 pos = GetBegPos( ARG1_IND + lastArgInd - 1 ) + GetArg( lastArgInd ).Length();
3352 while ( pos > 0 && pos <= Length() && myString.Value( pos ) != ')' )
3357 while ( pos > 0 && myString.Value( pos ) != ')' )
3360 if ( pos < 1 || myString.Value( pos ) != ')' ) { // no parentheses at all
3364 while ( myArgs.Length() < index ) {
3365 if ( myArgs.Length() )
3366 myString.Insert( pos++, "," );
3367 myArgs.Append("None");
3368 myString.Insert( pos, myArgs.Last() );
3369 SetBegPos( ARG1_IND + myArgs.Length() - 1, pos );
3370 pos += myArgs.Last().Length();
3373 SetPart( argInd, theArg, myArgs( index ));
3376 //================================================================================
3378 * \brief Empty arg list
3380 //================================================================================
3382 void _pyCommand::RemoveArgs()
3384 if ( int pos = myString.Location( '(', 1, Length() ))
3385 myString.Trunc( pos );
3388 if ( myBegPos.Length() >= ARG1_IND )
3389 myBegPos.Remove( ARG1_IND, myBegPos.Length() );
3392 //================================================================================
3394 * \brief Comment a python command
3396 //================================================================================
3398 void _pyCommand::Comment()
3400 if ( IsEmpty() ) return;
3403 while ( i <= Length() && isspace( myString.Value(i) )) ++i;
3404 if ( i <= Length() )
3406 myString.Insert( i, "#" );
3407 for ( int iPart = 0; iPart < myBegPos.Length(); ++iPart )
3409 int begPos = GetBegPos( iPart );
3410 if ( begPos != UNKNOWN )
3411 SetBegPos( iPart, begPos + 1 );
3416 //================================================================================
3418 * \brief Set dependent commands after this one
3420 //================================================================================
3422 bool _pyCommand::SetDependentCmdsAfter() const
3424 bool orderChanged = false;
3425 list< Handle(_pyCommand)>::const_reverse_iterator cmd = myDependentCmds.rbegin();
3426 for ( ; cmd != myDependentCmds.rend(); ++cmd ) {
3427 if ( (*cmd)->GetOrderNb() < GetOrderNb() ) {
3428 orderChanged = true;
3429 theGen->SetCommandAfter( *cmd, this );
3430 (*cmd)->SetDependentCmdsAfter();
3433 return orderChanged;
3435 //================================================================================
3437 * \brief Insert accessor method after theObjectID
3438 * \param theObjectID - id of the accessed object
3439 * \param theAcsMethod - name of the method giving access to the object
3440 * \retval bool - false if theObjectID is not found in the command string
3442 //================================================================================
3444 bool _pyCommand::AddAccessorMethod( _pyID theObjectID, const char* theAcsMethod )
3446 if ( !theAcsMethod )
3448 // start object search from the object, i.e. ignore result
3450 int beg = GetBegPos( OBJECT_IND );
3451 if ( beg < 1 || beg > Length() )
3454 while (( beg = myString.Location( theObjectID, beg, Length() )))
3456 // check that theObjectID is not just a part of a longer ID
3457 int afterEnd = beg + theObjectID.Length();
3458 Standard_Character c = myString.Value( afterEnd );
3459 if ( !isalnum( c ) && c != ':' ) {
3460 // check if accessor method already present
3462 myString.Location( (char*) theAcsMethod, afterEnd, Length() ) != afterEnd+1) {
3464 int oldLen = Length();
3465 myString.Insert( afterEnd, (char*) theAcsMethod );
3466 myString.Insert( afterEnd, "." );
3467 // update starting positions of the parts following the modified one
3468 int posDelta = Length() - oldLen;
3469 for ( int i = 1; i <= myBegPos.Length(); ++i ) {
3470 if ( myBegPos( i ) > afterEnd )
3471 myBegPos( i ) += posDelta;
3476 beg = afterEnd; // is a part - next search
3481 //================================================================================
3483 * \brief Creates pyObject
3485 //================================================================================
3487 _pyObject::_pyObject(const Handle(_pyCommand)& theCreationCmd, const _pyID& theID)
3488 : myID(theID), myCreationCmd(theCreationCmd), myIsPublished(false)
3493 //================================================================================
3495 * \brief Set up myID and myIsPublished
3497 //================================================================================
3499 void _pyObject::setID(const _pyID& theID)
3502 myIsPublished = !theGen->IsNotPublished( GetID() );
3505 //================================================================================
3507 * \brief Clear myCreationCmd and myProcessedCmds
3509 //================================================================================
3511 void _pyObject::ClearCommands()
3516 if ( !myCreationCmd.IsNull() )
3517 myCreationCmd->Clear();
3519 list< Handle(_pyCommand) >::iterator cmd = myProcessedCmds.begin();
3520 for ( ; cmd != myProcessedCmds.end(); ++cmd )
3524 //================================================================================
3526 * \brief Return method name giving access to an interaface object wrapped by python class
3527 * \retval const char* - method name
3529 //================================================================================
3531 const char* _pyObject::AccessorMethod() const
3535 //================================================================================
3537 * \brief Return ID of a father
3539 //================================================================================
3541 _pyID _pyObject::FatherID(const _pyID & childID)
3543 int colPos = childID.SearchFromEnd(':');
3545 return childID.SubString( 1, colPos-1 );
3549 //================================================================================
3551 * \brief SelfEraser erases creation command if no more it's commands invoked
3553 //================================================================================
3555 void _pySelfEraser::Flush()
3557 if ( GetNbCalls() == 0 )
3558 GetCreationCmd()->Clear();
3561 //================================================================================
3563 * \brief _pySubMesh constructor
3565 //================================================================================
3567 _pySubMesh::_pySubMesh(const Handle(_pyCommand)& theCreationCmd):
3568 _pyObject(theCreationCmd)
3570 myMesh = ObjectToMesh( theGen->FindObject( theCreationCmd->GetObject() ));
3573 //================================================================================
3575 * \brief count invoked commands
3577 //================================================================================
3579 void _pySubMesh::Process( const Handle(_pyCommand)& theCommand )
3581 _pyObject::Process(theCommand); // count calls of Process()
3582 GetCreationCmd()->AddDependantCmd( theCommand );
3585 //================================================================================
3587 * \brief Move creation command depending on invoked commands
3589 //================================================================================
3591 void _pySubMesh::Flush()
3593 if ( GetNbCalls() == 0 ) // move to the end of all commands
3594 theGen->GetLastCommand()->AddDependantCmd( GetCreationCmd() );
3595 else if ( !myCreator.IsNull() )
3596 // move to be just after creator
3597 myCreator->GetCreationCmd()->AddDependantCmd( GetCreationCmd() );
3600 //================================================================================
3602 * \brief To convert creation of a group by filter
3604 //================================================================================
3606 void _pyGroup::Process( const Handle(_pyCommand)& theCommand)
3608 // Convert the following set of commands into mesh.MakeGroupByFilter(groupName, theFilter)
3609 // group = mesh.CreateEmptyGroup( elemType, groupName )
3610 // aFilter.SetMesh(mesh)
3611 // nbAdd = group.AddFrom( aFilter )
3612 Handle(_pyFilter) filter;
3613 if ( theCommand->GetMethod() == "AddFrom" )
3615 _pyID idSource = theCommand->GetArg(1);
3616 // check if idSource is a filter
3617 filter = Handle(_pyFilter)::DownCast( theGen->FindObject( idSource ));
3618 if ( !filter.IsNull() )
3620 // find aFilter.SetMesh(mesh) to clear it, it should be just before theCommand
3621 list< Handle(_pyCommand) >::reverse_iterator cmdIt = theGen->GetCommands().rbegin();
3622 while ( *cmdIt != theCommand ) ++cmdIt;
3623 while ( (*cmdIt)->GetOrderNb() != 1 )
3625 const Handle(_pyCommand)& setMeshCmd = *(++cmdIt);
3626 if ((setMeshCmd->GetObject() == idSource ||
3627 setMeshCmd->GetObject() == filter->GetNewID() )
3629 setMeshCmd->GetMethod() == "SetMesh")
3631 setMeshCmd->Clear();
3635 // replace 3 commands by one
3636 theCommand->Clear();
3637 const Handle(_pyCommand)& makeGroupCmd = GetCreationCmd();
3638 TCollection_AsciiString name = makeGroupCmd->GetArg( 2 );
3639 makeGroupCmd->SetMethod( "MakeGroupByFilter" );
3640 makeGroupCmd->SetArg( 1, name );
3641 makeGroupCmd->SetArg( 2, idSource );
3642 // set new name of a filter
3643 filter->Process( makeGroupCmd );
3646 else if ( theCommand->GetMethod() == "SetFilter" )
3648 // set new name of a filter
3649 _pyID filterID = theCommand->GetArg(1);
3650 filter = Handle(_pyFilter)::DownCast( theGen->FindObject( filterID ));
3651 if ( !filter.IsNull() )
3652 filter->Process( theCommand );
3655 if ( !filter.IsNull() )
3656 filter->AddUser( this );
3658 theGen->AddMeshAccessorMethod( theCommand );
3661 //================================================================================
3663 * \brief Constructor of _pyFilter
3665 //================================================================================
3667 _pyFilter::_pyFilter(const Handle(_pyCommand)& theCreationCmd, const _pyID& newID/*=""*/)
3668 :_pyObject(theCreationCmd), myNewID( newID )
3672 //================================================================================
3674 * \brief To convert creation of a filter by criteria and
3675 * to replace an old name by a new one
3677 //================================================================================
3679 void _pyFilter::Process( const Handle(_pyCommand)& theCommand)
3681 if ( theCommand->GetObject() == GetID() )
3682 _pyObject::Process(theCommand); // count commands
3684 if ( !myNewID.IsEmpty() )
3686 if ( theCommand->GetObject() == GetID() )
3687 theCommand->SetObject( myNewID );
3688 else if ( theCommand->GetResultValue() == GetID() )
3689 theCommand->SetResultValue( myNewID );
3691 for ( int i = 1, nb = theCommand->GetNbArgs(); i <= nb; ++i )
3692 if ( theCommand->GetArg( i ) == GetID() )
3694 theCommand->SetArg( i, myNewID );
3699 // Convert the following set of commands into smesh.GetFilterFromCriteria(criteria)
3700 // aFilter0x2aaab0487080 = aFilterManager.CreateFilter()
3701 // aFilter0x2aaab0487080.SetCriteria(aCriteria)
3702 if ( GetNbCalls() == 1 && // none method was called before this SetCriteria() call
3703 theCommand->GetMethod() == "SetCriteria")
3705 // aFilter.SetCriteria(aCriteria) ->
3706 // aFilter = smesh.GetFilterFromCriteria(criteria)
3707 if ( myNewID.IsEmpty() )
3708 theCommand->SetResultValue( GetID() );
3710 theCommand->SetResultValue( myNewID );
3711 theCommand->SetObject( SMESH_2smeshpy::GenName() );
3712 theCommand->SetMethod( "GetFilterFromCriteria" );
3714 // Clear aFilterManager.CreateFilter()
3715 GetCreationCmd()->Clear();
3717 else if ( theCommand->GetMethod() == "SetMesh")
3719 theGen->AddMeshAccessorMethod( theCommand );
3723 //================================================================================
3725 * \brief Set new filter name to the creation command
3727 //================================================================================
3729 void _pyFilter::Flush()
3731 if ( !myNewID.IsEmpty() && !GetCreationCmd()->IsEmpty() )
3732 GetCreationCmd()->SetResultValue( myNewID );
3735 //================================================================================
3737 * \brief Return true if all my users can be cleared
3739 //================================================================================
3741 bool _pyFilter::CanClear()
3743 list< Handle(_pyObject) >::iterator obj = myUsers.begin();
3744 for ( ; obj != myUsers.end(); ++obj )
3745 if ( !(*obj)->CanClear() )
3751 //================================================================================
3753 * \brief Reads _pyHypothesis'es from resource files of mesher Plugins
3755 //================================================================================
3757 _pyHypothesisReader::_pyHypothesisReader()
3759 // Get paths to xml files of plugins
3760 vector< string > xmlPaths;
3762 if ( const char* meshersList = getenv("SMESH_MeshersList") )
3764 string meshers = meshersList, plugin;
3765 string::size_type from = 0, pos;
3766 while ( from < meshers.size() )
3768 // cut off plugin name
3769 pos = meshers.find( ':', from );
3770 if ( pos != string::npos )
3771 plugin = meshers.substr( from, pos-from );
3773 plugin = meshers.substr( from ), pos = meshers.size();
3776 // get PLUGIN_ROOT_DIR path
3777 string rootDirVar, pluginSubDir = plugin;
3778 if ( plugin == "StdMeshers" )
3779 rootDirVar = "SMESH", pluginSubDir = "smesh";
3781 for ( pos = 0; pos < plugin.size(); ++pos )
3782 rootDirVar += toupper( plugin[pos] );
3783 rootDirVar += "_ROOT_DIR";
3785 const char* rootDir = getenv( rootDirVar.c_str() );
3786 if ( !rootDir || strlen(rootDir) == 0 ) continue;
3788 // get a separator from rootDir
3789 for ( pos = strlen( rootDir )-1; pos >= 0 && sep.empty(); --pos )
3790 if ( rootDir[pos] == '/' || rootDir[pos] == '\\' )
3795 if (sep.empty() ) sep = "/";
3797 // get a path to resource file
3798 string xmlPath = rootDir;
3799 if ( xmlPath[ xmlPath.size()-1 ] != sep[0] )
3801 xmlPath += "share" + sep + "salome" + sep + "resources" + sep;
3802 for ( pos = 0; pos < pluginSubDir.size(); ++pos )
3803 xmlPath += tolower( pluginSubDir[pos] );
3804 xmlPath += sep + plugin + ".xml";
3807 fileOK = (GetFileAttributes(xmlPath.c_str()) != INVALID_FILE_ATTRIBUTES);
3809 fileOK = (access(xmlPath.c_str(), F_OK) == 0);
3812 xmlPaths.push_back( xmlPath );
3817 LDOMParser xmlParser;
3818 for ( size_t i = 0; i < xmlPaths.size(); ++i )
3820 bool error = xmlParser.parse( xmlPaths[i].c_str() );
3824 INFOS( xmlParser.GetError(data) );
3827 // <algorithm type="Regular_1D"
3828 // label-id="Wire discretisation"
3831 // <algo>Regular_1D=Segment()</algo>
3832 // <hypo>LocalLength=LocalLength(SetLength(1),,SetPrecision(1))</hypo>
3834 LDOM_Document xmlDoc = xmlParser.getDocument();
3835 LDOM_NodeList algoNodeList = xmlDoc.getElementsByTagName( "algorithm" );
3836 for ( int i = 0; i < algoNodeList.getLength(); ++i )
3838 LDOM_Node algoNode = algoNodeList.item( i );
3839 LDOM_Element& algoElem = (LDOM_Element&) algoNode;
3840 LDOM_NodeList pyAlgoNodeList = algoElem.getElementsByTagName( "algo" );
3841 if ( pyAlgoNodeList.getLength() < 1 ) continue;
3843 _AString text, algoType, method, arg;
3844 for ( int iA = 0; iA < pyAlgoNodeList.getLength(); ++iA )
3846 LDOM_Node pyAlgoNode = pyAlgoNodeList.item( iA );
3847 LDOM_Node textNode = pyAlgoNode.getFirstChild();
3848 text = textNode.getNodeValue();
3849 Handle(_pyCommand) algoCmd = new _pyCommand( text );
3850 algoType = algoCmd->GetResultValue();
3851 method = algoCmd->GetMethod();
3852 arg = algoCmd->GetArg(1);
3853 if ( !algoType.IsEmpty() && !method.IsEmpty() )
3855 Handle(_pyAlgorithm) algo = new _pyAlgorithm( algoCmd );
3856 algo->SetConvMethodAndType( method, algoType );
3857 if ( !arg.IsEmpty() )
3858 algo->setCreationArg( 1, arg );
3860 myType2Hyp[ algoType ] = algo;
3864 if ( algoType.IsEmpty() ) continue;
3866 LDOM_NodeList pyHypoNodeList = algoElem.getElementsByTagName( "hypo" );
3868 Handle( _pyHypothesis ) hyp;
3869 for ( int iH = 0; iH < pyHypoNodeList.getLength(); ++iH )
3871 LDOM_Node pyHypoNode = pyHypoNodeList.item( iH );
3872 LDOM_Node textNode = pyHypoNode.getFirstChild();
3873 text = textNode.getNodeValue();
3874 Handle(_pyCommand) hypoCmd = new _pyCommand( text );
3875 hypType = hypoCmd->GetResultValue();
3876 method = hypoCmd->GetMethod();
3877 if ( !hypType.IsEmpty() && !method.IsEmpty() )
3879 map<_AString, Handle(_pyHypothesis)>::iterator type2hyp = myType2Hyp.find( hypType );
3880 if ( type2hyp == myType2Hyp.end() )
3881 hyp = new _pyHypothesis( hypoCmd );
3883 hyp = type2hyp->second;
3884 hyp->SetConvMethodAndType( method, algoType );
3885 for ( int iArg = 1; iArg <= hypoCmd->GetNbArgs(); ++iArg )
3887 _pyCommand argCmd( hypoCmd->GetArg( iArg ));
3888 _AString argMethod = argCmd.GetMethod();
3889 _AString argNbText = argCmd.GetArg( 1 );
3890 if ( argMethod.IsEmpty() && !argCmd.IsEmpty() )
3891 hyp->setCreationArg( 1, argCmd.GetString() ); // e.g. Parameters(smesh.SIMPLE)
3893 hyp->AddArgMethod( argMethod,
3894 argNbText.IsIntegerValue() ? argNbText.IntegerValue() : 1 );
3896 myType2Hyp[ hypType ] = hyp;
3903 //================================================================================
3905 * \brief Returns a new hypothesis initialized according to the read information
3907 //================================================================================
3909 Handle(_pyHypothesis)
3910 _pyHypothesisReader::GetHypothesis(const _AString& hypType,
3911 const Handle(_pyCommand)& creationCmd) const
3913 Handle(_pyHypothesis) resHyp, sampleHyp;
3915 map<_AString, Handle(_pyHypothesis)>::const_iterator type2hyp = myType2Hyp.find( hypType );
3916 if ( type2hyp != myType2Hyp.end() )
3917 sampleHyp = type2hyp->second;
3919 if ( sampleHyp.IsNull() )
3921 resHyp = new _pyHypothesis(creationCmd);
3925 if ( sampleHyp->IsAlgo() )
3926 resHyp = new _pyAlgorithm( creationCmd );
3928 resHyp = new _pyHypothesis(creationCmd);
3929 resHyp->Assign( sampleHyp, _pyID() );