1 // Copyright (C) 2007-2012 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 // File : SMESH_2smeshpy.cxx
24 // Created : Fri Nov 18 13:20:10 2005
25 // Author : Edward AGAPOV (eap)
27 #include "SMESH_2smeshpy.hxx"
29 #include "utilities.h"
30 #include "SMESH_PythonDump.hxx"
31 #include "SMESH_NoteBook.hxx"
32 #include "SMESH_Filter_i.hxx"
34 #include <Resource_DataMapOfAsciiStringAsciiString.hxx>
35 #include <Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString.hxx>
37 #include "SMESH_Gen_i.hxx"
38 /* SALOME headers that include CORBA headers that include windows.h
39 * that defines GetObject symbol as GetObjectA should stand before SALOME headers
40 * that declare methods named GetObject - to apply the same rules of GetObject renaming
41 * and thus to avoid mess with GetObject symbol on Windows */
43 #include <LDOMParser.hxx>
52 IMPLEMENT_STANDARD_HANDLE (_pyObject ,Standard_Transient);
53 IMPLEMENT_STANDARD_HANDLE (_pyCommand ,Standard_Transient);
54 IMPLEMENT_STANDARD_HANDLE (_pyHypothesisReader,Standard_Transient);
55 IMPLEMENT_STANDARD_HANDLE (_pyGen ,_pyObject);
56 IMPLEMENT_STANDARD_HANDLE (_pyMesh ,_pyObject);
57 IMPLEMENT_STANDARD_HANDLE (_pySubMesh ,_pyObject);
58 IMPLEMENT_STANDARD_HANDLE (_pyMeshEditor ,_pyObject);
59 IMPLEMENT_STANDARD_HANDLE (_pyHypothesis ,_pyObject);
60 IMPLEMENT_STANDARD_HANDLE (_pySelfEraser ,_pyObject);
61 IMPLEMENT_STANDARD_HANDLE (_pyGroup ,_pyObject);
62 IMPLEMENT_STANDARD_HANDLE (_pyFilter ,_pyObject);
63 IMPLEMENT_STANDARD_HANDLE (_pyAlgorithm ,_pyHypothesis);
64 IMPLEMENT_STANDARD_HANDLE (_pyComplexParamHypo,_pyHypothesis);
65 IMPLEMENT_STANDARD_HANDLE (_pyNumberOfSegmentsHyp,_pyHypothesis);
67 IMPLEMENT_STANDARD_RTTIEXT(_pyObject ,Standard_Transient);
68 IMPLEMENT_STANDARD_RTTIEXT(_pyCommand ,Standard_Transient);
69 IMPLEMENT_STANDARD_RTTIEXT(_pyHypothesisReader,Standard_Transient);
70 IMPLEMENT_STANDARD_RTTIEXT(_pyGen ,_pyObject);
71 IMPLEMENT_STANDARD_RTTIEXT(_pyMesh ,_pyObject);
72 IMPLEMENT_STANDARD_RTTIEXT(_pySubMesh ,_pyObject);
73 IMPLEMENT_STANDARD_RTTIEXT(_pyMeshEditor ,_pyObject);
74 IMPLEMENT_STANDARD_RTTIEXT(_pyHypothesis ,_pyObject);
75 IMPLEMENT_STANDARD_RTTIEXT(_pySelfEraser ,_pyObject);
76 IMPLEMENT_STANDARD_RTTIEXT(_pyGroup ,_pyObject);
77 IMPLEMENT_STANDARD_RTTIEXT(_pyFilter ,_pyObject);
78 IMPLEMENT_STANDARD_RTTIEXT(_pyAlgorithm ,_pyHypothesis);
79 IMPLEMENT_STANDARD_RTTIEXT(_pyComplexParamHypo,_pyHypothesis);
80 IMPLEMENT_STANDARD_RTTIEXT(_pyNumberOfSegmentsHyp,_pyHypothesis);
81 IMPLEMENT_STANDARD_RTTIEXT(_pyLayerDistributionHypo,_pyHypothesis);
82 IMPLEMENT_STANDARD_RTTIEXT(_pySegmentLengthAroundVertexHyp,_pyHypothesis);
85 using SMESH::TPythonDump;
88 * \brief Container of commands into which the initial script is split.
89 * It also contains data coresponding to SMESH_Gen contents
91 static Handle(_pyGen) theGen;
93 static TCollection_AsciiString theEmptyString;
95 //#define DUMP_CONVERSION
97 #if !defined(_DEBUG_) && defined(DUMP_CONVERSION)
98 #undef DUMP_CONVERSION
104 //================================================================================
106 * \brief Set of TCollection_AsciiString initialized by C array of C strings
108 //================================================================================
110 struct TStringSet: public set<TCollection_AsciiString>
113 * \brief Filling. The last string must be ""
115 void Insert(const char* names[]) {
116 for ( int i = 0; names[i][0] ; ++i )
117 insert( (char*) names[i] );
120 * \brief Check if a string is in
122 bool Contains(const TCollection_AsciiString& name ) {
123 return find( name ) != end();
127 //================================================================================
129 * \brief Returns a mesh by object
131 //================================================================================
133 Handle(_pyMesh) ObjectToMesh( const Handle( _pyObject )& obj )
137 if ( obj->IsKind( STANDARD_TYPE( _pyMesh )))
138 return Handle(_pyMesh)::DownCast( obj );
139 else if ( obj->IsKind( STANDARD_TYPE( _pySubMesh )))
140 return Handle(_pySubMesh)::DownCast( obj )->GetMesh();
141 else if ( obj->IsKind( STANDARD_TYPE( _pyGroup )))
142 return Handle(_pyGroup)::DownCast( obj )->GetMesh();
144 return Handle(_pyMesh)();
147 //================================================================================
149 * \brief Check if objects used as args have been created by previous commands
151 //================================================================================
153 void CheckObjectPresence( const Handle(_pyCommand)& cmd, set<_pyID> & presentObjects)
155 for ( int iArg = cmd->GetNbArgs(); iArg; --iArg )
157 const _pyID& arg = cmd->GetArg( iArg );
158 if ( arg.IsEmpty() || arg.Value( 1 ) == '"' || arg.Value( 1 ) == '\'' )
160 list< _pyID > idList = cmd->GetStudyEntries( arg );
161 list< _pyID >::iterator id = idList.begin();
162 for ( ; id != idList.end(); ++id )
163 if ( !theGen->IsGeomObject( *id ) && !presentObjects.count( *id ))
166 cmd->GetString() += " ### " ;
167 cmd->GetString() += *id + " has not been yet created";
171 const _pyID& obj = cmd->GetObject();
172 if ( !obj.IsEmpty() && cmd->IsStudyEntry( obj ) && !presentObjects.count( obj ))
175 cmd->GetString() += " ### not created object" ;
177 const _pyID& result = cmd->GetResultValue();
178 if ( result.IsEmpty() || result.Value( 1 ) == '"' || result.Value( 1 ) == '\'' )
180 list< _pyID > idList = cmd->GetStudyEntries( result );
181 list< _pyID >::iterator id = idList.begin();
182 for ( ; id != idList.end(); ++id )
183 presentObjects.insert( *id );
186 //================================================================================
188 * \brief Fix SMESH::FunctorType arguments of SMESH::Filter::Criterion()
190 //================================================================================
192 void fixFunctorType( TCollection_AsciiString& Type,
193 TCollection_AsciiString& Compare,
194 TCollection_AsciiString& UnaryOp,
195 TCollection_AsciiString& BinaryOp )
197 // The problem is that dumps of old studies created using filters becomes invalid
198 // when new items are inserted in the enum SMESH::FunctorType since values
199 // of this enum are dumped as integer values.
200 // This function corrects enum values of old studies given as args (Type,Compare,...)
201 // We can find out how to correct them by value of BinaryOp which can have only two
202 // values: FT_Undefined or FT_LogicalNOT.
203 // Hereafter is the history of the enum SMESH::FunctorType since v3.0.0
204 // where PythonDump appeared
205 // v 3.0.0: FT_Undefined == 25
206 // v 3.1.0: FT_Undefined == 26, new items:
208 // v 4.1.2: FT_Undefined == 27, new items:
209 // - FT_BelongToGenSurface = 17
210 // v 5.1.1: FT_Undefined == 32, new items:
211 // - FT_FreeNodes = 10
212 // - FT_FreeFaces = 11
213 // - FT_LinearOrQuadratic = 23
214 // - FT_GroupColor = 24
215 // - FT_ElemGeomType = 25
216 // v 5.1.5: FT_Undefined == 33, new items:
217 // - FT_CoplanarFaces = 26
218 // v 6.2.0: FT_Undefined == 39, new items:
219 // - FT_MaxElementLength2D = 8
220 // - FT_MaxElementLength3D = 9
221 // - FT_BareBorderVolume = 25
222 // - FT_BareBorderFace = 26
223 // - FT_OverConstrainedVolume = 27
224 // - FT_OverConstrainedFace = 28
225 // v 6.5.0: FT_Undefined == 43, new items:
226 // - FT_EqualNodes = 14
227 // - FT_EqualEdges = 15
228 // - FT_EqualFaces = 16
229 // - FT_EqualVolumes = 17
230 // v 6.6.0: FT_Undefined == 44, new items:
231 // - FT_BallDiameter = 37
233 // It's necessary to continue recording this history and to fill
234 // undef2newItems (see below) accordingly.
236 typedef map< int, vector< int > > TUndef2newItems;
237 static TUndef2newItems undef2newItems;
238 if ( undef2newItems.empty() )
240 undef2newItems[ 26 ].push_back( 7 );
241 undef2newItems[ 27 ].push_back( 17 );
242 { int items[] = { 10, 11, 23, 24, 25 };
243 undef2newItems[ 32 ].assign( items, items+5 ); }
244 undef2newItems[ 33 ].push_back( 26 );
245 { int items[] = { 8, 9, 25, 26, 27, 28 };
246 undef2newItems[ 39 ].assign( items, items+6 ); }
247 { int items[] = { 14, 15, 16, 17 };
248 undef2newItems[ 43 ].assign( items, items+4 ); }
249 { int items[] = { 37 };
250 undef2newItems[ 44 ].assign( items, items+1 ); }
253 int iType = Type.IntegerValue();
254 int iCompare = Compare.IntegerValue();
255 int iUnaryOp = UnaryOp.IntegerValue();
256 int iBinaryOp = BinaryOp.IntegerValue();
258 // find out integer value of FT_Undefined at the moment of dump
259 int oldUndefined = iBinaryOp;
260 if ( iBinaryOp < iUnaryOp ) // BinaryOp was FT_LogicalNOT
263 // apply history to args
264 TUndef2newItems::const_iterator undef_items =
265 undef2newItems.upper_bound( oldUndefined );
266 if ( undef_items != undef2newItems.end() )
268 int* pArg[4] = { &iType, &iCompare, &iUnaryOp, &iBinaryOp };
269 for ( ; undef_items != undef2newItems.end(); ++undef_items )
271 const vector< int > & addedItems = undef_items->second;
272 for ( size_t i = 0; i < addedItems.size(); ++i )
273 for ( int iArg = 0; iArg < 4; ++iArg )
275 int& arg = *pArg[iArg];
276 if ( arg >= addedItems[i] )
280 Type = TCollection_AsciiString( iType );
281 Compare = TCollection_AsciiString( iCompare );
282 UnaryOp = TCollection_AsciiString( iUnaryOp );
283 BinaryOp = TCollection_AsciiString( iBinaryOp );
288 //================================================================================
290 * \brief Convert python script using commands of smesh.py
291 * \param theScript - Input script
292 * \retval TCollection_AsciiString - Convertion result
293 * \param theToKeepAllCommands - to keep all commands or
294 * to exclude commands relating to objects removed from study
296 * Class SMESH_2smeshpy declared in SMESH_PythonDump.hxx
298 //================================================================================
300 TCollection_AsciiString
301 SMESH_2smeshpy::ConvertScript(const TCollection_AsciiString& theScript,
302 Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod,
303 Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
304 SALOMEDS::Study_ptr& theStudy,
305 const bool theToKeepAllCommands)
307 theGen = new _pyGen( theEntry2AccessorMethod, theObjectNames, theStudy, theToKeepAllCommands );
309 // split theScript into separate commands
311 SMESH_NoteBook * aNoteBook = new SMESH_NoteBook();
313 int from = 1, end = theScript.Length(), to;
314 while ( from < end && ( to = theScript.Location( "\n", from, end )))
317 // cut out and store a command
318 aNoteBook->AddCommand( theScript.SubString( from, to - 1 ));
322 aNoteBook->ReplaceVariables();
324 TCollection_AsciiString aNoteScript = aNoteBook->GetResultScript();
328 // split theScript into separate commands
329 from = 1, end = aNoteScript.Length();
330 while ( from < end && ( to = aNoteScript.Location( "\n", from, end )))
333 // cut out and store a command
334 theGen->AddCommand( aNoteScript.SubString( from, to - 1 ));
340 #ifdef DUMP_CONVERSION
341 MESSAGE_BEGIN ( std::endl << " ######## RESULT ######## " << std::endl<< std::endl );
344 // clean commmands of removed objects depending on myIsPublished flag
345 theGen->ClearCommands();
347 // reorder commands after conversion
348 list< Handle(_pyCommand) >::iterator cmd;
351 orderChanges = false;
352 for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
353 if ( (*cmd)->SetDependentCmdsAfter() )
355 } while ( orderChanges );
357 // concat commands back into a script
358 TCollection_AsciiString aScript, aPrevCmd;
359 set<_pyID> createdObjects;
360 for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
362 #ifdef DUMP_CONVERSION
363 MESSAGE_ADD ( "## COM " << (*cmd)->GetOrderNb() << ": "<< (*cmd)->GetString() << std::endl );
365 if ( !(*cmd)->IsEmpty() && aPrevCmd != (*cmd)->GetString()) {
366 CheckObjectPresence( *cmd, createdObjects );
367 aPrevCmd = (*cmd)->GetString();
380 //================================================================================
382 * \brief _pyGen constructor
384 //================================================================================
386 _pyGen::_pyGen(Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod,
387 Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
388 SALOMEDS::Study_ptr& theStudy,
389 const bool theToKeepAllCommands)
390 : _pyObject( new _pyCommand( "", 0 )),
392 myID2AccessorMethod( theEntry2AccessorMethod ),
393 myObjectNames( theObjectNames ),
395 myToKeepAllCommands( theToKeepAllCommands ),
396 myStudy( SALOMEDS::Study::_duplicate( theStudy )),
397 myGeomIDNb(0), myGeomIDIndex(-1)
399 // make that GetID() to return TPythonDump::SMESHGenName()
400 GetCreationCmd()->Clear();
401 GetCreationCmd()->GetString() = TPythonDump::SMESHGenName();
402 GetCreationCmd()->GetString() += "=";
404 // Find 1st digit of study entry by which a GEOM object differs from a SMESH object
405 if ( !theObjectNames.IsEmpty() && !CORBA::is_nil( theStudy ))
409 SALOMEDS::SComponent_var geomComp = theStudy->FindComponent("GEOM");
410 if ( geomComp->_is_nil() ) return;
411 CORBA::String_var entry = geomComp->GetID();
414 // find a SMESH entry
416 Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString e2n( theObjectNames );
417 for ( ; e2n.More() && smeshID.IsEmpty(); e2n.Next() )
418 if ( _pyCommand::IsStudyEntry( e2n.Key() ))
421 // find 1st difference between smeshID and geomID
422 if ( !geomID.IsEmpty() && !smeshID.IsEmpty() )
423 for ( int i = 1; i <= geomID.Length() && i <= smeshID.Length(); ++i )
424 if ( geomID.Value( i ) != smeshID.Value( i ))
426 myGeomIDNb = geomID.Value( i );
432 //================================================================================
434 * \brief name of SMESH_Gen in smesh.py
436 //================================================================================
438 const char* _pyGen::AccessorMethod() const
440 return SMESH_2smeshpy::GenName();
443 //================================================================================
445 * \brief Convert a command using a specific converter
446 * \param theCommand - the command to convert
448 //================================================================================
450 Handle(_pyCommand) _pyGen::AddCommand( const TCollection_AsciiString& theCommand)
452 // store theCommand in the sequence
453 myCommands.push_back( new _pyCommand( theCommand, ++myNbCommands ));
455 Handle(_pyCommand) aCommand = myCommands.back();
456 #ifdef DUMP_CONVERSION
457 MESSAGE ( "## COM " << myNbCommands << ": "<< aCommand->GetString() );
460 const _pyID& objID = aCommand->GetObject();
462 if ( objID.IsEmpty() )
465 // Prevent moving a command creating a sub-mesh to the end of the script
466 // if the sub-mesh is used in theCommand as argument
467 if ( _pySubMesh::CanBeArgOfMethod( aCommand->GetMethod() ))
469 PlaceSubmeshAfterItsCreation( aCommand );
472 // Find an object to process theCommand
475 if ( objID == this->GetID() || objID == SMESH_2smeshpy::GenName())
477 this->Process( aCommand );
481 // SMESH_Mesh method?
482 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( objID );
483 if ( id_mesh != myMeshes.end() )
485 //id_mesh->second->AddProcessedCmd( aCommand );
487 // check for mesh editor object
488 if ( aCommand->GetMethod() == "GetMeshEditor" ) { // MeshEditor creation
489 _pyID editorID = aCommand->GetResultValue();
490 Handle(_pyMeshEditor) editor = new _pyMeshEditor( aCommand );
491 myMeshEditors.insert( make_pair( editorID, editor ));
494 // check for SubMesh objects
495 else if ( aCommand->GetMethod() == "GetSubMesh" ) { // SubMesh creation
496 _pyID subMeshID = aCommand->GetResultValue();
497 Handle(_pySubMesh) subMesh = new _pySubMesh( aCommand );
498 myObjects.insert( make_pair( subMeshID, subMesh ));
501 id_mesh->second->Process( aCommand );
502 id_mesh->second->AddProcessedCmd( aCommand );
506 // SMESH_MeshEditor method?
507 map< _pyID, Handle(_pyMeshEditor) >::iterator id_editor = myMeshEditors.find( objID );
508 if ( id_editor != myMeshEditors.end() )
510 const TCollection_AsciiString& method = aCommand->GetMethod();
512 // some commands of SMESH_MeshEditor create meshes and groups
513 _pyID meshID, groups;
514 if ( method.Search("MakeMesh") != -1 )
515 meshID = aCommand->GetResultValue();
516 else if ( method == "MakeBoundaryMesh")
517 meshID = aCommand->GetResultValue(1);
518 else if ( method == "MakeBoundaryElements")
519 meshID = aCommand->GetResultValue(2);
521 if ( method.Search("MakeGroups") != -1 ||
522 method == "ExtrusionAlongPathX" ||
523 method == "ExtrusionAlongPathObjX" ||
524 method == "DoubleNodeGroupNew" ||
525 method == "DoubleNodeGroupsNew" ||
526 method == "DoubleNodeElemGroupNew" ||
527 method == "DoubleNodeElemGroupsNew"||
528 method == "DoubleNodeElemGroup2New"||
529 method == "DoubleNodeElemGroups2New"
531 groups = aCommand->GetResultValue();
532 else if ( method == "MakeBoundaryMesh" )
533 groups = aCommand->GetResultValue(2);
534 else if ( method == "MakeBoundaryElements")
535 groups = aCommand->GetResultValue(3);
537 id_editor->second->Process( aCommand );
538 id_editor->second->AddProcessedCmd( aCommand );
540 if ( !meshID.IsEmpty() &&
541 !myMeshes.count( meshID ) &&
542 aCommand->IsStudyEntry( meshID ))
544 TCollection_AsciiString processedCommand = aCommand->GetString();
545 Handle(_pyMesh) mesh = new _pyMesh( aCommand, meshID );
546 myMeshes.insert( make_pair( meshID, mesh ));
548 aCommand->GetString() = processedCommand; // discard changes made by _pyMesh
550 if ( !groups.IsEmpty() )
552 if ( !aCommand->IsStudyEntry( meshID ))
553 meshID = id_editor->second->GetMesh();
554 Handle(_pyMesh) mesh = myMeshes[ meshID ];
556 list< _pyID > idList = aCommand->GetStudyEntries( groups );
557 list< _pyID >::iterator grID = idList.begin();
558 for ( ; grID != idList.end(); ++grID )
559 if ( !myObjects.count( *grID ))
561 Handle(_pyGroup) group = new _pyGroup( aCommand, *grID );
563 if ( !mesh.IsNull() ) mesh->AddGroup( group );
567 } // SMESH_MeshEditor methods
569 // SMESH_Hypothesis method?
570 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
571 for ( ; hyp != myHypos.end(); ++hyp )
572 if ( !(*hyp)->IsAlgo() && objID == (*hyp)->GetID() ) {
573 (*hyp)->Process( aCommand );
574 (*hyp)->AddProcessedCmd( aCommand );
578 // aFilterManager.CreateFilter() ?
579 if ( aCommand->GetMethod() == "CreateFilter" )
581 // Set a more human readable name to a filter
582 // aFilter0x7fbf6c71cfb0 -> aFilter_nb
583 _pyID newID, filterID = aCommand->GetResultValue();
584 int pos = filterID.Search( "0x" );
586 newID = (filterID.SubString(1,pos-1) + "_") + _pyID( ++myNbFilters );
588 Handle(_pyObject) filter( new _pyFilter( aCommand, newID ));
592 // other object method?
593 map< _pyID, Handle(_pyObject) >::iterator id_obj = myObjects.find( objID );
594 if ( id_obj != myObjects.end() ) {
595 id_obj->second->Process( aCommand );
596 id_obj->second->AddProcessedCmd( aCommand );
600 // Add access to a wrapped mesh
601 AddMeshAccessorMethod( aCommand );
603 // Add access to a wrapped algorithm
604 // AddAlgoAccessorMethod( aCommand ); // ??? what if algo won't be wrapped at all ???
606 // PAL12227. PythonDump was not updated at proper time; result is
607 // aCriteria.append(SMESH.Filter.Criterion(17,26,0,'L1',26,25,1e-07,SMESH.EDGE,-1))
608 // TypeError: __init__() takes exactly 11 arguments (10 given)
609 const char wrongCommand[] = "SMESH.Filter.Criterion(";
610 if ( int beg = theCommand.Location( wrongCommand, 1, theCommand.Length() ))
612 _pyCommand tmpCmd( theCommand.SubString( beg, theCommand.Length() ), -1);
613 // there must be 10 arguments, 5-th arg ThresholdID is missing,
614 const int wrongNbArgs = 9, missingArg = 5;
615 if ( tmpCmd.GetNbArgs() == wrongNbArgs )
617 for ( int i = wrongNbArgs; i > missingArg; --i )
618 tmpCmd.SetArg( i + 1, tmpCmd.GetArg( i ));
619 tmpCmd.SetArg( missingArg, "''");
620 aCommand->GetString().Trunc( beg - 1 );
621 aCommand->GetString() += tmpCmd.GetString();
624 // set GetCriterion(elementType,CritType,Compare,Treshold,UnaryOp,BinaryOp,Tolerance)
626 // instead of "SMESH.Filter.Criterion(
627 // Type,Compare,Threshold,ThresholdStr,ThresholdID,UnaryOp,BinaryOp,Tolerance,TypeOfElement,Precision)
628 // 1 2 3 4 5 6 7 8 9 10
629 // in order to avoid the problem of type mismatch of long and FunctorType
630 const TCollection_AsciiString
631 SMESH("SMESH."), dfltFunctor = "SMESH.FT_Undefined", dftlTol = "1e-07", dftlPreci = "-1";
632 TCollection_AsciiString
633 Type = aCommand->GetArg(1), // long
634 Compare = aCommand->GetArg(2), // long
635 Threshold = aCommand->GetArg(3), // double
636 ThresholdStr = aCommand->GetArg(4), // string
637 ThresholdID = aCommand->GetArg(5), // string
638 UnaryOp = aCommand->GetArg(6), // long
639 BinaryOp = aCommand->GetArg(7), // long
640 Tolerance = aCommand->GetArg(8), // double
641 TypeOfElement = aCommand->GetArg(9), // ElementType
642 Precision = aCommand->GetArg(10); // long
643 fixFunctorType( Type, Compare, UnaryOp, BinaryOp );
644 Type = SMESH + SMESH::FunctorTypeToString( SMESH::FunctorType( Type.IntegerValue() ));
645 Compare = SMESH + SMESH::FunctorTypeToString( SMESH::FunctorType( Compare.IntegerValue() ));
646 UnaryOp = SMESH + SMESH::FunctorTypeToString( SMESH::FunctorType( UnaryOp.IntegerValue() ));
647 BinaryOp = SMESH + SMESH::FunctorTypeToString( SMESH::FunctorType( BinaryOp.IntegerValue() ));
649 aCommand->RemoveArgs();
650 aCommand->SetObject( SMESH_2smeshpy::GenName() );
651 aCommand->SetMethod( "GetCriterion" );
653 aCommand->SetArg( 1, TypeOfElement );
654 aCommand->SetArg( 2, Type );
655 aCommand->SetArg( 3, Compare );
657 if ( Type == "SMESH.FT_ElemGeomType" && Threshold.IsIntegerValue() )
659 // set SMESH.GeometryType instead of a numerical Threshold
660 const char* types[SMESH::Geom_BALL+1] = {
661 "Geom_POINT", "Geom_EDGE", "Geom_TRIANGLE", "Geom_QUADRANGLE", "Geom_POLYGON",
662 "Geom_TETRA", "Geom_PYRAMID", "Geom_HEXA", "Geom_PENTA", "Geom_HEXAGONAL_PRISM",
663 "Geom_POLYHEDRA", "Geom_BALL"
665 int iGeom = Threshold.IntegerValue();
666 if ( -1 < iGeom && iGeom < SMESH::Geom_POLYHEDRA+1 )
667 Threshold = SMESH + types[ iGeom ];
669 if ( ThresholdID.Length() != 2 && ThresholdStr.Length() != 2) // not '' or ""
670 aCommand->SetArg( 4, ThresholdID.SubString( 2, ThresholdID.Length()-1 )); // shape entry
671 else if ( ThresholdStr.Length() != 2 )
672 aCommand->SetArg( 4, ThresholdStr );
673 else if ( ThresholdID.Length() != 2 )
674 aCommand->SetArg( 4, ThresholdID );
676 aCommand->SetArg( 4, Threshold );
677 // find the last not default arg
679 if ( Tolerance == dftlTol ) {
681 if ( BinaryOp == dfltFunctor ) {
683 if ( UnaryOp == dfltFunctor )
687 if ( 5 < lastDefault ) aCommand->SetArg( 5, UnaryOp );
688 if ( 6 < lastDefault ) aCommand->SetArg( 6, BinaryOp );
689 if ( 7 < lastDefault ) aCommand->SetArg( 7, Tolerance );
690 if ( Precision != dftlPreci )
692 TCollection_AsciiString crit = aCommand->GetResultValue();
693 aCommand->GetString() += "; ";
694 aCommand->GetString() += crit + ".Precision = " + Precision;
700 //================================================================================
702 * \brief Convert the command or remember it for later conversion
703 * \param theCommand - The python command calling a method of SMESH_Gen
705 //================================================================================
707 void _pyGen::Process( const Handle(_pyCommand)& theCommand )
709 // there are methods to convert:
710 // CreateMesh( shape )
711 // Concatenate( [mesh1, ...], ... )
712 // CreateHypothesis( theHypType, theLibName )
713 // Compute( mesh, geom )
714 // Evaluate( mesh, geom )
716 TCollection_AsciiString method = theCommand->GetMethod();
718 if ( method == "CreateMesh" || method == "CreateEmptyMesh")
720 Handle(_pyMesh) mesh = new _pyMesh( theCommand );
721 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
724 if ( method == "CreateMeshesFromUNV" ||
725 method == "CreateMeshesFromSTL" ||
726 method == "CreateMeshesFromCGNS" ||
727 method == "CopyMesh" )
729 Handle(_pyMesh) mesh = new _pyMesh( theCommand, theCommand->GetResultValue() );
730 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
733 if( method == "CreateMeshesFromMED" || method == "CreateMeshesFromSAUV")
735 for(int ind = 0;ind<theCommand->GetNbResultValues();ind++)
737 _pyID meshID = theCommand->GetResultValue(ind+1);
738 if ( !theCommand->IsStudyEntry( meshID ) ) continue;
739 Handle(_pyMesh) mesh = new _pyMesh( theCommand, theCommand->GetResultValue(ind+1));
740 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
744 // CreateHypothesis()
745 if ( method == "CreateHypothesis" )
747 // issue 199929, remove standard library name (default parameter)
748 const TCollection_AsciiString & aLibName = theCommand->GetArg( 2 );
749 if ( aLibName.Search( "StdMeshersEngine" ) != -1 ) {
750 // keep first argument
751 TCollection_AsciiString arg = theCommand->GetArg( 1 );
752 theCommand->RemoveArgs();
753 theCommand->SetArg( 1, arg );
756 myHypos.push_back( _pyHypothesis::NewHypothesis( theCommand ));
760 // smeshgen.Compute( mesh, geom ) --> mesh.Compute()
761 if ( method == "Compute" )
763 const _pyID& meshID = theCommand->GetArg( 1 );
764 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( meshID );
765 if ( id_mesh != myMeshes.end() ) {
766 theCommand->SetObject( meshID );
767 theCommand->RemoveArgs();
768 id_mesh->second->Process( theCommand );
769 id_mesh->second->AddProcessedCmd( theCommand );
774 // smeshgen.Evaluate( mesh, geom ) --> mesh.Evaluate(geom)
775 if ( method == "Evaluate" )
777 const _pyID& meshID = theCommand->GetArg( 1 );
778 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( meshID );
779 if ( id_mesh != myMeshes.end() ) {
780 theCommand->SetObject( meshID );
781 _pyID geom = theCommand->GetArg( 2 );
782 theCommand->RemoveArgs();
783 theCommand->SetArg( 1, geom );
784 id_mesh->second->AddProcessedCmd( theCommand );
789 // objects erasing creation command if no more it's commands invoked:
790 // SMESH_Pattern, FilterManager
791 if ( method == "GetPattern" ||
792 method == "CreateFilterManager" ||
793 method == "CreateMeasurements" ) {
794 Handle(_pyObject) obj = new _pySelfEraser( theCommand );
795 if ( !myObjects.insert( make_pair( obj->GetID(), obj )).second )
796 theCommand->Clear(); // already created
798 // Concatenate( [mesh1, ...], ... )
799 else if ( method == "Concatenate" || method == "ConcatenateWithGroups")
801 if ( method == "ConcatenateWithGroups" ) {
802 theCommand->SetMethod( "Concatenate" );
803 theCommand->SetArg( theCommand->GetNbArgs() + 1, "True" );
805 Handle(_pyMesh) mesh = new _pyMesh( theCommand, theCommand->GetResultValue() );
806 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
807 AddMeshAccessorMethod( theCommand );
809 else if ( method == "SetName" ) // SetName(obj,name)
811 // store theCommand as one of object commands to erase it along with the object
812 const _pyID& objID = theCommand->GetArg( 1 );
813 Handle(_pyObject) obj = FindObject( objID );
815 obj->AddProcessedCmd( theCommand );
818 // Replace name of SMESH_Gen
820 // names of SMESH_Gen methods fully equal to methods defined in smesh.py
821 static TStringSet smeshpyMethods;
822 if ( smeshpyMethods.empty() ) {
823 const char * names[] =
824 { "SetEmbeddedMode","IsEmbeddedMode","SetCurrentStudy","GetCurrentStudy",
825 "GetPattern","GetSubShapesId",
826 "" }; // <- mark of array end
827 smeshpyMethods.Insert( names );
829 if ( smeshpyMethods.Contains( theCommand->GetMethod() ))
830 // smeshgen.Method() --> smesh.Method()
831 theCommand->SetObject( SMESH_2smeshpy::SmeshpyName() );
833 // smeshgen.Method() --> smesh.smesh.Method()
834 theCommand->SetObject( SMESH_2smeshpy::GenName() );
837 //================================================================================
839 * \brief Convert the remembered commands
841 //================================================================================
845 // create an empty command
846 myLastCommand = new _pyCommand();
848 map< _pyID, Handle(_pyMesh) >::iterator id_mesh;
849 map< _pyID, Handle(_pyObject) >::iterator id_obj;
850 list< Handle(_pyHypothesis) >::iterator hyp;
852 if ( IsToKeepAllCommands() ) // historical dump
854 // set myIsPublished = true to all objects
855 for ( id_mesh = myMeshes.begin(); id_mesh != myMeshes.end(); ++id_mesh )
856 id_mesh->second->SetRemovedFromStudy( false );
857 for ( hyp = myHypos.begin(); hyp != myHypos.end(); ++hyp )
858 (*hyp)->SetRemovedFromStudy( false );
859 for ( id_obj = myObjects.begin(); id_obj != myObjects.end(); ++id_obj )
860 id_obj->second->SetRemovedFromStudy( false );
864 // let hypotheses find referred objects in order to prevent clearing
865 // not published referred hyps (it's needed for hyps like "LayerDistribution")
866 list< Handle(_pyMesh) > fatherMeshes;
867 for ( hyp = myHypos.begin(); hyp != myHypos.end(); ++hyp )
868 if ( !hyp->IsNull() )
869 (*hyp)->GetReferredMeshesAndGeom( fatherMeshes );
871 // set myIsPublished = false to all objects depending on
872 // meshes built on a removed geometry
873 for ( id_mesh = myMeshes.begin(); id_mesh != myMeshes.end(); ++id_mesh )
874 if ( id_mesh->second->IsNotGeomPublished() )
875 id_mesh->second->SetRemovedFromStudy( true );
878 for ( id_mesh = myMeshes.begin(); id_mesh != myMeshes.end(); ++id_mesh )
879 if ( ! id_mesh->second.IsNull() )
880 id_mesh->second->Flush();
883 for ( hyp = myHypos.begin(); hyp != myHypos.end(); ++hyp )
884 if ( !hyp->IsNull() ) {
886 // smeshgen.CreateHypothesis() --> smesh.smesh.CreateHypothesis()
887 if ( !(*hyp)->IsWrapped() )
888 (*hyp)->GetCreationCmd()->SetObject( SMESH_2smeshpy::GenName() );
891 // Flush other objects
892 for ( id_obj = myObjects.begin(); id_obj != myObjects.end(); ++id_obj )
893 if ( ! id_obj->second.IsNull() )
894 id_obj->second->Flush();
896 myLastCommand->SetOrderNb( ++myNbCommands );
897 myCommands.push_back( myLastCommand );
900 //================================================================================
902 * \brief Prevent moving a command creating a sub-mesh to the end of the script
903 * if the sub-mesh is used in theCmdUsingSubmesh as argument
905 //================================================================================
907 void _pyGen::PlaceSubmeshAfterItsCreation( Handle(_pyCommand) theCmdUsingSubmesh ) const
909 map< _pyID, Handle(_pyObject) >::const_iterator id_obj = myObjects.begin();
910 for ( ; id_obj != myObjects.end(); ++id_obj )
912 if ( !id_obj->second->IsKind( STANDARD_TYPE( _pySubMesh ))) continue;
913 for ( int iArg = theCmdUsingSubmesh->GetNbArgs(); iArg; --iArg )
915 const _pyID& arg = theCmdUsingSubmesh->GetArg( iArg );
916 if ( arg.IsEmpty() || arg.Value( 1 ) == '"' || arg.Value( 1 ) == '\'' )
918 list< _pyID > idList = theCmdUsingSubmesh->GetStudyEntries( arg );
919 list< _pyID >::iterator id = idList.begin();
920 for ( ; id != idList.end(); ++id )
921 if ( id_obj->first == *id )
922 // _pySubMesh::Process() does what we need
923 Handle(_pySubMesh)::DownCast( id_obj->second )->Process( theCmdUsingSubmesh );
928 //================================================================================
930 * \brief Clean commmands of removed objects depending on myIsPublished flag
932 //================================================================================
934 void _pyGen::ClearCommands()
936 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.begin();
937 for ( ; id_mesh != myMeshes.end(); ++id_mesh )
938 id_mesh->second->ClearCommands();
940 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
941 for ( ; hyp != myHypos.end(); ++hyp )
942 if ( !hyp->IsNull() )
943 (*hyp)->ClearCommands();
945 map< _pyID, Handle(_pyObject) >::iterator id_obj = myObjects.begin();
946 for ( ; id_obj != myObjects.end(); ++id_obj )
947 id_obj->second->ClearCommands();
950 //================================================================================
952 * \brief Release mutual handles of objects
954 //================================================================================
958 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.begin();
959 for ( ; id_mesh != myMeshes.end(); ++id_mesh )
960 id_mesh->second->Free();
963 map< _pyID, Handle(_pyMeshEditor) >::iterator id_ed = myMeshEditors.begin();
964 for ( ; id_ed != myMeshEditors.end(); ++id_ed )
965 id_ed->second->Free();
966 myMeshEditors.clear();
968 map< _pyID, Handle(_pyObject) >::iterator id_obj = myObjects.begin();
969 for ( ; id_obj != myObjects.end(); ++id_obj )
970 id_obj->second->Free();
973 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
974 for ( ; hyp != myHypos.end(); ++hyp )
975 if ( !hyp->IsNull() )
979 myFile2ExportedMesh.clear();
982 //================================================================================
984 * \brief Add access method to mesh that is an argument
985 * \param theCmd - command to add access method
986 * \retval bool - true if added
988 //================================================================================
990 bool _pyGen::AddMeshAccessorMethod( Handle(_pyCommand) theCmd ) const
993 map< _pyID, Handle(_pyMesh) >::const_iterator id_mesh = myMeshes.begin();
994 for ( ; id_mesh != myMeshes.end(); ++id_mesh ) {
995 if ( theCmd->AddAccessorMethod( id_mesh->first, id_mesh->second->AccessorMethod() ))
1001 //================================================================================
1003 * \brief Add access method to algo that is an object or an argument
1004 * \param theCmd - command to add access method
1005 * \retval bool - true if added
1007 //================================================================================
1009 bool _pyGen::AddAlgoAccessorMethod( Handle(_pyCommand) theCmd ) const
1012 list< Handle(_pyHypothesis) >::const_iterator hyp = myHypos.begin();
1013 for ( ; hyp != myHypos.end(); ++hyp ) {
1014 if ( (*hyp)->IsAlgo() && /*(*hyp)->IsWrapped() &&*/
1015 theCmd->AddAccessorMethod( (*hyp)->GetID(), (*hyp)->AccessorMethod() ))
1021 //================================================================================
1023 * \brief Find hypothesis by ID (entry)
1024 * \param theHypID - The hypothesis ID
1025 * \retval Handle(_pyHypothesis) - The found hypothesis
1027 //================================================================================
1029 Handle(_pyHypothesis) _pyGen::FindHyp( const _pyID& theHypID )
1031 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
1032 for ( ; hyp != myHypos.end(); ++hyp )
1033 if ( !hyp->IsNull() && theHypID == (*hyp)->GetID() )
1035 return Handle(_pyHypothesis)();
1038 //================================================================================
1040 * \brief Find algorithm the created algorithm
1041 * \param theGeom - The shape ID the algorithm was created on
1042 * \param theMesh - The mesh ID that created the algorithm
1043 * \param dim - The algo dimension
1044 * \retval Handle(_pyHypothesis) - The found algo
1046 //================================================================================
1048 Handle(_pyHypothesis) _pyGen::FindAlgo( const _pyID& theGeom, const _pyID& theMesh,
1049 const Handle(_pyHypothesis)& theHypothesis )
1051 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
1052 for ( ; hyp != myHypos.end(); ++hyp )
1053 if ( !hyp->IsNull() &&
1055 theHypothesis->CanBeCreatedBy( (*hyp)->GetAlgoType() ) &&
1056 (*hyp)->GetGeom() == theGeom &&
1057 (*hyp)->GetMesh() == theMesh )
1062 //================================================================================
1064 * \brief Find subMesh by ID (entry)
1065 * \param theSubMeshID - The subMesh ID
1066 * \retval Handle(_pySubMesh) - The found subMesh
1068 //================================================================================
1070 Handle(_pySubMesh) _pyGen::FindSubMesh( const _pyID& theSubMeshID )
1072 map< _pyID, Handle(_pyObject) >::iterator id_subMesh = myObjects.find(theSubMeshID);
1073 if ( id_subMesh != myObjects.end() )
1074 return Handle(_pySubMesh)::DownCast( id_subMesh->second );
1075 return Handle(_pySubMesh)();
1079 //================================================================================
1081 * \brief Change order of commands in the script
1082 * \param theCmd1 - One command
1083 * \param theCmd2 - Another command
1085 //================================================================================
1087 void _pyGen::ExchangeCommands( Handle(_pyCommand) theCmd1, Handle(_pyCommand) theCmd2 )
1089 list< Handle(_pyCommand) >::iterator pos1, pos2;
1090 pos1 = find( myCommands.begin(), myCommands.end(), theCmd1 );
1091 pos2 = find( myCommands.begin(), myCommands.end(), theCmd2 );
1092 myCommands.insert( pos1, theCmd2 );
1093 myCommands.insert( pos2, theCmd1 );
1094 myCommands.erase( pos1 );
1095 myCommands.erase( pos2 );
1097 int nb1 = theCmd1->GetOrderNb();
1098 theCmd1->SetOrderNb( theCmd2->GetOrderNb() );
1099 theCmd2->SetOrderNb( nb1 );
1100 // cout << "BECOME " << theCmd1->GetOrderNb() << "\t" << theCmd1->GetString() << endl
1101 // << "BECOME " << theCmd2->GetOrderNb() << "\t" << theCmd2->GetString() << endl << endl;
1104 //================================================================================
1106 * \brief Set one command after the other
1107 * \param theCmd - Command to move
1108 * \param theAfterCmd - Command ater which to insert the first one
1110 //================================================================================
1112 void _pyGen::SetCommandAfter( Handle(_pyCommand) theCmd, Handle(_pyCommand) theAfterCmd )
1114 setNeighbourCommand( theCmd, theAfterCmd, true );
1117 //================================================================================
1119 * \brief Set one command before the other
1120 * \param theCmd - Command to move
1121 * \param theBeforeCmd - Command before which to insert the first one
1123 //================================================================================
1125 void _pyGen::SetCommandBefore( Handle(_pyCommand) theCmd, Handle(_pyCommand) theBeforeCmd )
1127 setNeighbourCommand( theCmd, theBeforeCmd, false );
1130 //================================================================================
1132 * \brief Set one command before or after the other
1133 * \param theCmd - Command to move
1134 * \param theOtherCmd - Command ater or before which to insert the first one
1136 //================================================================================
1138 void _pyGen::setNeighbourCommand( Handle(_pyCommand)& theCmd,
1139 Handle(_pyCommand)& theOtherCmd,
1140 const bool theIsAfter )
1142 list< Handle(_pyCommand) >::iterator pos;
1143 pos = find( myCommands.begin(), myCommands.end(), theCmd );
1144 myCommands.erase( pos );
1145 pos = find( myCommands.begin(), myCommands.end(), theOtherCmd );
1146 myCommands.insert( (theIsAfter ? ++pos : pos), theCmd );
1149 for ( pos = myCommands.begin(); pos != myCommands.end(); ++pos)
1150 (*pos)->SetOrderNb( i++ );
1153 //================================================================================
1155 * \brief Set command be last in list of commands
1156 * \param theCmd - Command to be last
1158 //================================================================================
1160 Handle(_pyCommand)& _pyGen::GetLastCommand()
1162 return myLastCommand;
1165 //================================================================================
1167 * \brief Set method to access to object wrapped with python class
1168 * \param theID - The wrapped object entry
1169 * \param theMethod - The accessor method
1171 //================================================================================
1173 void _pyGen::SetAccessorMethod(const _pyID& theID, const char* theMethod )
1175 myID2AccessorMethod.Bind( theID, (char*) theMethod );
1178 //================================================================================
1180 * \brief Generated new ID for object and assign with existing name
1181 * \param theID - ID of existing object
1183 //================================================================================
1185 _pyID _pyGen::GenerateNewID( const _pyID& theID )
1190 aNewID = theID + _pyID( ":" ) + _pyID( index++ );
1192 while ( myObjectNames.IsBound( aNewID ) );
1194 myObjectNames.Bind( aNewID, myObjectNames.IsBound( theID )
1195 ? (myObjectNames.Find( theID ) + _pyID( "_" ) + _pyID( index-1 ))
1196 : _pyID( "A" ) + aNewID );
1200 //================================================================================
1202 * \brief Stores theObj in myObjects
1204 //================================================================================
1206 void _pyGen::AddObject( Handle(_pyObject)& theObj )
1208 if ( theObj.IsNull() ) return;
1210 if ( theObj->IsKind( STANDARD_TYPE( _pyMesh )))
1211 myMeshes.insert( make_pair( theObj->GetID(), Handle(_pyMesh)::DownCast( theObj )));
1213 else if ( theObj->IsKind( STANDARD_TYPE( _pyMeshEditor )))
1214 myMeshEditors.insert( make_pair( theObj->GetID(), Handle(_pyMeshEditor)::DownCast( theObj )));
1217 myObjects.insert( make_pair( theObj->GetID(), theObj ));
1220 //================================================================================
1222 * \brief Re-register an object with other ID to make it Process() commands of
1223 * other object having this ID
1225 //================================================================================
1227 void _pyGen::SetProxyObject( const _pyID& theID, Handle(_pyObject)& theObj )
1229 if ( theObj.IsNull() ) return;
1231 if ( theObj->IsKind( STANDARD_TYPE( _pyMesh )))
1232 myMeshes.insert( make_pair( theID, Handle(_pyMesh)::DownCast( theObj )));
1234 else if ( theObj->IsKind( STANDARD_TYPE( _pyMeshEditor )))
1235 myMeshEditors.insert( make_pair( theID, Handle(_pyMeshEditor)::DownCast( theObj )));
1238 myObjects.insert( make_pair( theID, theObj ));
1241 //================================================================================
1243 * \brief Finds a _pyObject by ID
1245 //================================================================================
1247 Handle(_pyObject) _pyGen::FindObject( const _pyID& theObjID ) const
1250 map< _pyID, Handle(_pyObject) >::const_iterator id_obj = myObjects.find( theObjID );
1251 if ( id_obj != myObjects.end() )
1252 return id_obj->second;
1255 map< _pyID, Handle(_pyMesh) >::const_iterator id_obj = myMeshes.find( theObjID );
1256 if ( id_obj != myMeshes.end() )
1257 return id_obj->second;
1260 // map< _pyID, Handle(_pyMeshEditor) >::const_iterator id_obj = myMeshEditors.find( theObjID );
1261 // if ( id_obj != myMeshEditors.end() )
1262 // return id_obj->second;
1264 return Handle(_pyObject)();
1267 //================================================================================
1269 * \brief Check if a study entry is under GEOM component
1271 //================================================================================
1273 bool _pyGen::IsGeomObject(const _pyID& theObjID) const
1277 return ( myGeomIDIndex <= theObjID.Length() &&
1278 int( theObjID.Value( myGeomIDIndex )) == myGeomIDNb &&
1279 _pyCommand::IsStudyEntry( theObjID ));
1284 //================================================================================
1286 * \brief Returns true if an object is not present in a study
1288 //================================================================================
1290 bool _pyGen::IsNotPublished(const _pyID& theObjID) const
1292 if ( theObjID.IsEmpty() ) return false;
1294 if ( myObjectNames.IsBound( theObjID ))
1295 return false; // SMESH object is in study
1297 // either the SMESH object is not in study or it is a GEOM object
1298 if ( IsGeomObject( theObjID ))
1300 SALOMEDS::SObject_var so = myStudy->FindObjectID( theObjID.ToCString() );
1301 if ( so->_is_nil() ) return true;
1302 CORBA::Object_var obj = so->GetObject();
1303 return CORBA::is_nil( obj );
1305 return true; // SMESH object not in study
1308 //================================================================================
1310 * \brief Return reader of hypotheses of plugins
1312 //================================================================================
1314 Handle( _pyHypothesisReader ) _pyGen::GetHypothesisReader() const
1316 if (myHypReader.IsNull() )
1317 ((_pyGen*) this)->myHypReader = new _pyHypothesisReader;
1323 //================================================================================
1325 * \brief Mesh created by SMESH_Gen
1327 //================================================================================
1329 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd)
1330 : _pyObject( theCreationCmd ), myGeomNotInStudy( false )
1332 if ( theCreationCmd->GetMethod() == "CreateMesh" && theGen->IsNotPublished( GetGeom() ))
1333 myGeomNotInStudy = true;
1335 // convert my creation command --> smeshpy.Mesh(...)
1336 Handle(_pyCommand) creationCmd = GetCreationCmd();
1337 creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
1338 creationCmd->SetMethod( "Mesh" );
1339 theGen->SetAccessorMethod( GetID(), _pyMesh::AccessorMethod() );
1342 //================================================================================
1344 * \brief Mesh created by SMESH_MeshEditor
1346 //================================================================================
1348 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd, const _pyID& meshId):
1349 _pyObject(theCreationCmd,meshId), myGeomNotInStudy(false )
1351 if ( theCreationCmd->MethodStartsFrom( "CreateMeshesFrom" ))
1353 // this mesh depends on the exported mesh
1354 const TCollection_AsciiString& file = theCreationCmd->GetArg( 1 );
1355 if ( !file.IsEmpty() )
1357 ExportedMeshData& exportData = theGen->FindExportedMesh( file );
1358 addFatherMesh( exportData.myMesh );
1359 if ( !exportData.myLastComputeCmd.IsNull() )
1361 // restore cleared Compute() by which the exported mesh was generated
1362 exportData.myLastComputeCmd->GetString() = exportData.myLastComputeCmdString;
1363 // protect that Compute() cmd from clearing
1364 if ( exportData.myMesh->myLastComputeCmd == exportData.myLastComputeCmd )
1365 exportData.myMesh->myLastComputeCmd.Nullify();
1369 else if ( theCreationCmd->MethodStartsFrom( "Concatenate" ))
1371 // this mesh depends on concatenated meshes
1372 const TCollection_AsciiString& meshIDs = theCreationCmd->GetArg( 1 );
1373 list< _pyID > idList = theCreationCmd->GetStudyEntries( meshIDs );
1374 list< _pyID >::iterator meshID = idList.begin();
1375 for ( ; meshID != idList.end(); ++meshID )
1376 addFatherMesh( *meshID );
1378 else if ( theCreationCmd->GetMethod() == "CopyMesh" )
1380 // this mesh depends on a copied IdSource
1381 const _pyID& objID = theCreationCmd->GetArg( 1 );
1382 addFatherMesh( objID );
1384 else if ( theCreationCmd->GetMethod().Search("MakeMesh") != -1 ||
1385 theCreationCmd->GetMethod() == "MakeBoundaryMesh" ||
1386 theCreationCmd->GetMethod() == "MakeBoundaryElements" )
1388 // this mesh depends on a source mesh
1389 // (theCreationCmd is already Process()ed by _pyMeshEditor)
1390 const _pyID& meshID = theCreationCmd->GetObject();
1391 addFatherMesh( meshID );
1394 // convert my creation command
1395 Handle(_pyCommand) creationCmd = GetCreationCmd();
1396 creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
1397 theGen->SetAccessorMethod( meshId, _pyMesh::AccessorMethod() );
1400 //================================================================================
1402 * \brief Convert an IDL API command of SMESH::SMESH_Mesh to a method call of python Mesh
1403 * \param theCommand - Engine method called for this mesh
1405 //================================================================================
1407 void _pyMesh::Process( const Handle(_pyCommand)& theCommand )
1409 // some methods of SMESH_Mesh interface needs special conversion
1410 // to methods of Mesh python class
1412 // 1. GetSubMesh(geom, name) + AddHypothesis(geom, algo)
1413 // --> in Mesh_Algorithm.Create(mesh, geom, hypo, so)
1414 // 2. AddHypothesis(geom, hyp)
1415 // --> in Mesh_Algorithm.Hypothesis(hyp, args, so)
1416 // 3. CreateGroupFromGEOM(type, name, grp)
1417 // --> in Mesh.Group(grp, name="")
1418 // 4. ExportToMED(f, auto_groups, version)
1419 // --> in Mesh.ExportMED( f, auto_groups, version )
1422 const TCollection_AsciiString& method = theCommand->GetMethod();
1423 // ----------------------------------------------------------------------
1424 if ( method == "Compute" ) // in snapshot mode, clear the previous Compute()
1426 if ( !theGen->IsToKeepAllCommands() ) // !historical
1428 list< Handle(_pyHypothesis) >::iterator hyp;
1429 if ( !myLastComputeCmd.IsNull() )
1431 for ( hyp = myHypos.begin(); hyp != myHypos.end(); ++hyp )
1432 (*hyp)->ComputeDiscarded( myLastComputeCmd );
1434 myLastComputeCmd->Clear();
1436 myLastComputeCmd = theCommand;
1438 for ( hyp = myHypos.begin(); hyp != myHypos.end(); ++hyp )
1439 (*hyp)->MeshComputed( myLastComputeCmd );
1443 // ----------------------------------------------------------------------
1444 else if ( method == "Clear" ) // in snapshot mode, clear all previous commands
1446 if ( !theGen->IsToKeepAllCommands() ) // !historical
1449 myChildMeshes.empty() ? 0 : myChildMeshes.back()->GetCreationCmd()->GetOrderNb();
1450 // list< Handle(_pyCommand) >::reverse_iterator cmd = myProcessedCmds.rbegin();
1451 // for ( ; cmd != myProcessedCmds.rend() && (*cmd)->GetOrderNb() > untilCmdNb; ++cmd )
1453 if ( !myLastComputeCmd.IsNull() )
1455 list< Handle(_pyHypothesis) >::iterator hyp;
1456 for ( hyp = myHypos.begin(); hyp != myHypos.end(); ++hyp )
1457 (*hyp)->ComputeDiscarded( myLastComputeCmd );
1459 myLastComputeCmd->Clear();
1462 list< Handle(_pyMeshEditor)>::iterator e = myEditors.begin();
1463 for ( ; e != myEditors.end(); ++e )
1465 list< Handle(_pyCommand)>& cmds = (*e)->GetProcessedCmds();
1466 list< Handle(_pyCommand) >::reverse_iterator cmd = cmds.rbegin();
1467 for ( ; cmd != cmds.rend() && (*cmd)->GetOrderNb() > untilCmdNb; ++cmd )
1468 if ( !(*cmd)->IsEmpty() )
1470 if ( (*cmd)->GetStudyEntries( (*cmd)->GetResultValue() ).empty() ) // no object created
1474 myLastComputeCmd = theCommand; // to clear Clear() the same way as Compute()
1477 // ----------------------------------------------------------------------
1478 else if ( method == "GetSubMesh" ) { // collect submeshes of the mesh
1479 Handle(_pySubMesh) subMesh = theGen->FindSubMesh( theCommand->GetResultValue() );
1480 if ( !subMesh.IsNull() ) {
1481 subMesh->SetCreator( this );
1482 mySubmeshes.push_back( subMesh );
1485 // ----------------------------------------------------------------------
1486 else if ( method == "AddHypothesis" ) { // mesh.AddHypothesis(geom, HYPO )
1487 myAddHypCmds.push_back( theCommand );
1489 const _pyID& hypID = theCommand->GetArg( 2 );
1490 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
1491 if ( !hyp.IsNull() ) {
1492 myHypos.push_back( hyp );
1493 if ( hyp->GetMesh().IsEmpty() )
1494 hyp->SetMesh( this->GetID() );
1497 // ----------------------------------------------------------------------
1498 else if ( method == "CreateGroup" ||
1499 method == "CreateGroupFromGEOM" ||
1500 method == "CreateGroupFromFilter" )
1502 Handle(_pyGroup) group = new _pyGroup( theCommand );
1503 myGroups.push_back( group );
1504 theGen->AddObject( group );
1506 // ----------------------------------------------------------------------
1507 else if ( theCommand->MethodStartsFrom( "Export" ))
1509 if ( method == "ExportToMED" || // ExportToMED() --> ExportMED()
1510 method == "ExportToMEDX" ) { // ExportToMEDX() --> ExportMED()
1511 theCommand->SetMethod( "ExportMED" );
1513 else if ( method == "ExportCGNS" )
1514 { // ExportCGNS(part, ...) -> ExportCGNS(..., part)
1515 _pyID partID = theCommand->GetArg( 1 );
1516 int nbArgs = theCommand->GetNbArgs();
1517 for ( int i = 2; i <= nbArgs; ++i )
1518 theCommand->SetArg( i-1, theCommand->GetArg( i ));
1519 theCommand->SetArg( nbArgs, partID );
1521 else if ( theCommand->MethodStartsFrom( "ExportPartTo" ))
1522 { // ExportPartTo*(part, ...) -> Export*(..., part)
1524 // remove "PartTo" from the method
1525 TCollection_AsciiString newMethod = method;
1526 newMethod.Remove( 7, 6 );
1527 theCommand->SetMethod( newMethod );
1528 // make the 1st arg be the last one
1529 _pyID partID = theCommand->GetArg( 1 );
1530 int nbArgs = theCommand->GetNbArgs();
1531 for ( int i = 2; i <= nbArgs; ++i )
1532 theCommand->SetArg( i-1, theCommand->GetArg( i ));
1533 theCommand->SetArg( nbArgs, partID );
1535 // remember file name
1536 theGen->AddExportedMesh( theCommand->GetArg( 1 ),
1537 ExportedMeshData( this, myLastComputeCmd ));
1539 // ----------------------------------------------------------------------
1540 else if ( method == "RemoveHypothesis" ) // (geom, hyp)
1542 _pyID hypID = theCommand->GetArg( 2 );
1543 _pyID geomID = theCommand->GetArg( 1 );
1545 // check if this mesh still has corresponding addition command
1546 bool hasAddCmd = false;
1547 list< Handle(_pyCommand) >::iterator cmd = myAddHypCmds.begin();
1548 while ( cmd != myAddHypCmds.end() )
1550 // AddHypothesis(geom, hyp)
1551 if ( hypID == (*cmd)->GetArg( 2 ) &&
1552 geomID == (*cmd)->GetArg( 1 )) { // erase both (add and remove) commands
1553 theCommand->Clear();
1555 cmd = myAddHypCmds.erase( cmd );
1562 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
1563 if ( ! hasAddCmd && hypID.Length() != 0 ) { // hypo addition already wrapped
1564 // RemoveHypothesis(geom, hyp) --> RemoveHypothesis( hyp, geom=0 )
1565 _pyID geom = theCommand->GetArg( 1 );
1566 theCommand->RemoveArgs();
1567 theCommand->SetArg( 1, hypID );
1568 if ( geom != GetGeom() )
1569 theCommand->SetArg( 2, geom );
1571 // remove hyp from myHypos
1572 myHypos.remove( hyp );
1574 // check for SubMesh order commands
1575 else if ( method == "GetMeshOrder" || method == "SetMeshOrder" )
1577 // make commands GetSubMesh() returning sub-meshes be before using sub-meshes
1578 // by GetMeshOrder() and SetMeshOrder(), since by defalut GetSubMesh()
1579 // commands are moved at the end of the script
1580 TCollection_AsciiString subIDs =
1581 ( method == "SetMeshOrder" ) ? theCommand->GetArg(1) : theCommand->GetResultValue();
1582 list< _pyID > idList = theCommand->GetStudyEntries( subIDs );
1583 list< _pyID >::iterator subID = idList.begin();
1584 for ( ; subID != idList.end(); ++subID )
1586 Handle(_pySubMesh) subMesh = theGen->FindSubMesh( *subID );
1587 if ( !subMesh.IsNull() )
1588 subMesh->Process( theCommand ); // it moves GetSubMesh() before theCommand
1591 // update list of groups
1592 else if ( method == "GetGroups" )
1594 TCollection_AsciiString grIDs = theCommand->GetResultValue();
1595 list< _pyID > idList = theCommand->GetStudyEntries( grIDs );
1596 list< _pyID >::iterator grID = idList.begin();
1597 for ( ; grID != idList.end(); ++grID )
1599 Handle(_pyObject) obj = theGen->FindObject( *grID );
1602 Handle(_pyGroup) group = new _pyGroup( theCommand, *grID );
1603 theGen->AddObject( group );
1604 myGroups.push_back( group );
1608 // add accessor method if necessary
1611 if ( NeedMeshAccess( theCommand ))
1612 // apply theCommand to the mesh wrapped by smeshpy mesh
1613 AddMeshAccess( theCommand );
1617 //================================================================================
1619 * \brief Return True if addition of accesor method is needed
1621 //================================================================================
1623 bool _pyMesh::NeedMeshAccess( const Handle(_pyCommand)& theCommand )
1625 // names of SMESH_Mesh methods fully equal to methods of python class Mesh,
1626 // so no conversion is needed for them at all:
1627 static TStringSet sameMethods;
1628 if ( sameMethods.empty() ) {
1629 const char * names[] =
1630 { "ExportDAT","ExportUNV","ExportSTL","ExportSAUV", "RemoveGroup","RemoveGroupWithContents",
1631 "GetGroups","UnionGroups","IntersectGroups","CutGroups","GetLog","GetId","ClearLog",
1632 "GetStudyId","HasDuplicatedGroupNamesMED","GetMEDMesh","NbNodes","NbElements",
1633 "NbEdges","NbEdgesOfOrder","NbFaces","NbFacesOfOrder","NbTriangles",
1634 "NbTrianglesOfOrder","NbQuadrangles","NbQuadranglesOfOrder","NbPolygons","NbVolumes",
1635 "NbVolumesOfOrder","NbTetras","NbTetrasOfOrder","NbHexas","NbHexasOfOrder",
1636 "NbPyramids","NbPyramidsOfOrder","NbPrisms","NbPrismsOfOrder","NbPolyhedrons",
1637 "NbSubMesh","GetElementsId","GetElementsByType","GetNodesId","GetElementType",
1638 "GetSubMeshElementsId","GetSubMeshNodesId","GetSubMeshElementType","Dump","GetNodeXYZ",
1639 "GetNodeInverseElements","GetShapeID","GetShapeIDForElem","GetElemNbNodes",
1640 "GetElemNode","IsMediumNode","IsMediumNodeOfAnyElem","ElemNbEdges","ElemNbFaces",
1641 "IsPoly","IsQuadratic","BaryCenter","GetHypothesisList", "SetAutoColor", "GetAutoColor",
1642 "Clear", "ConvertToStandalone", "GetMeshOrder", "SetMeshOrder"
1643 ,"" }; // <- mark of end
1644 sameMethods.Insert( names );
1647 return !sameMethods.Contains( theCommand->GetMethod() );
1650 //================================================================================
1652 * \brief Convert creation and addition of all algos and hypos
1654 //================================================================================
1656 void _pyMesh::Flush()
1659 // get the meshes this mesh depends on via hypotheses
1660 list< Handle(_pyMesh) > fatherMeshes;
1661 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
1662 for ( ; hyp != myHypos.end(); ++hyp )
1663 if ( ! (*hyp)->GetReferredMeshesAndGeom( fatherMeshes ))
1664 myGeomNotInStudy = true;
1666 list< Handle(_pyMesh) >::iterator m = fatherMeshes.begin();
1667 for ( ; m != fatherMeshes.end(); ++m )
1668 addFatherMesh( *m );
1669 // if ( removedGeom )
1670 // SetRemovedFromStudy(); // as reffered geometry not in study
1672 if ( myGeomNotInStudy )
1675 list < Handle(_pyCommand) >::iterator cmd;
1677 // try to convert algo addition like this:
1678 // mesh.AddHypothesis(geom, ALGO ) --> ALGO = mesh.Algo()
1679 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
1681 Handle(_pyCommand) addCmd = *cmd;
1683 _pyID algoID = addCmd->GetArg( 2 );
1684 Handle(_pyHypothesis) algo = theGen->FindHyp( algoID );
1685 if ( algo.IsNull() || !algo->IsAlgo() )
1688 // check and create new algorithm instance if it is already wrapped
1689 if ( algo->IsWrapped() ) {
1690 _pyID localAlgoID = theGen->GenerateNewID( algoID );
1691 TCollection_AsciiString aNewCmdStr = addCmd->GetIndentation() + localAlgoID +
1692 TCollection_AsciiString( " = " ) + theGen->GetID() +
1693 TCollection_AsciiString( ".CreateHypothesis( \"" ) + algo->GetAlgoType() +
1694 TCollection_AsciiString( "\" )" );
1696 Handle(_pyCommand) newCmd = theGen->AddCommand( aNewCmdStr );
1697 Handle(_pyAlgorithm) newAlgo = Handle(_pyAlgorithm)::DownCast(theGen->FindHyp( localAlgoID ));
1698 if ( !newAlgo.IsNull() ) {
1699 newAlgo->Assign( algo, this->GetID() );
1700 newAlgo->SetCreationCmd( newCmd );
1702 // set algorithm creation
1703 theGen->SetCommandBefore( newCmd, addCmd );
1704 myHypos.push_back( newAlgo );
1705 if ( !myLastComputeCmd.IsNull() &&
1706 newCmd->GetOrderNb() == myLastComputeCmd->GetOrderNb() + 1)
1707 newAlgo->MeshComputed( myLastComputeCmd );
1712 _pyID geom = addCmd->GetArg( 1 );
1713 bool isLocalAlgo = ( geom != GetGeom() );
1716 if ( algo->Addition2Creation( addCmd, this->GetID() )) // OK
1718 // wrapped algo is created after mesh creation
1719 GetCreationCmd()->AddDependantCmd( addCmd );
1721 if ( isLocalAlgo ) {
1722 // mesh.AddHypothesis(geom, ALGO ) --> mesh.AlgoMethod(geom)
1723 addCmd->SetArg( addCmd->GetNbArgs() + 1,
1724 TCollection_AsciiString( "geom=" ) + geom );
1725 // sm = mesh.GetSubMesh(geom, name) --> sm = ALGO.GetSubMesh()
1726 list < Handle(_pySubMesh) >::iterator smIt;
1727 for ( smIt = mySubmeshes.begin(); smIt != mySubmeshes.end(); ++smIt ) {
1728 Handle(_pySubMesh) subMesh = *smIt;
1729 Handle(_pyCommand) subCmd = subMesh->GetCreationCmd();
1730 if ( geom == subCmd->GetArg( 1 )) {
1731 subCmd->SetObject( algo->GetID() );
1732 subCmd->RemoveArgs();
1733 subMesh->SetCreator( algo );
1738 else // KO - ALGO was already created
1740 // mesh.AddHypothesis(geom, ALGO) --> mesh.AddHypothesis(ALGO, geom=0)
1741 addCmd->RemoveArgs();
1742 addCmd->SetArg( 1, algoID );
1744 addCmd->SetArg( 2, geom );
1748 // try to convert hypo addition like this:
1749 // mesh.AddHypothesis(geom, HYPO ) --> HYPO = algo.Hypo()
1750 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
1752 Handle(_pyCommand) addCmd = *cmd;
1753 _pyID hypID = addCmd->GetArg( 2 );
1754 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
1755 if ( hyp.IsNull() || hyp->IsAlgo() )
1757 bool converted = hyp->Addition2Creation( addCmd, this->GetID() );
1759 // mesh.AddHypothesis(geom, HYP) --> mesh.AddHypothesis(HYP, geom=0)
1760 _pyID geom = addCmd->GetArg( 1 );
1761 addCmd->RemoveArgs();
1762 addCmd->SetArg( 1, hypID );
1763 if ( geom != GetGeom() )
1764 addCmd->SetArg( 2, geom );
1768 myAddHypCmds.clear();
1769 mySubmeshes.clear();
1772 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
1773 for ( hyp = myHypos.begin(); hyp != myHypos.end(); ++hyp )
1777 //================================================================================
1779 * \brief Sets myIsPublished of me and of all objects depending on me.
1781 //================================================================================
1783 void _pyMesh::SetRemovedFromStudy(const bool isRemoved)
1785 _pyObject::SetRemovedFromStudy(isRemoved);
1787 list< Handle(_pySubMesh) >::iterator sm = mySubmeshes.begin();
1788 for ( ; sm != mySubmeshes.end(); ++sm )
1789 (*sm)->SetRemovedFromStudy(isRemoved);
1791 list< Handle(_pyGroup) >::iterator gr = myGroups.begin();
1792 for ( ; gr != myGroups.end(); ++gr )
1793 (*gr)->SetRemovedFromStudy(isRemoved);
1795 list< Handle(_pyMesh) >::iterator m = myChildMeshes.begin();
1796 for ( ; m != myChildMeshes.end(); ++m )
1797 (*m)->SetRemovedFromStudy(isRemoved);
1799 list< Handle(_pyMeshEditor)>::iterator e = myEditors.begin();
1800 for ( ; e != myEditors.end(); ++e )
1801 (*e)->SetRemovedFromStudy(isRemoved);
1804 //================================================================================
1806 * \brief Return true if none of myChildMeshes is in study
1808 //================================================================================
1810 bool _pyMesh::CanClear()
1815 list< Handle(_pyMesh) >::iterator m = myChildMeshes.begin();
1816 for ( ; m != myChildMeshes.end(); ++m )
1817 if ( !(*m)->CanClear() )
1823 //================================================================================
1825 * \brief Clear my commands and commands of mesh editor
1827 //================================================================================
1829 void _pyMesh::ClearCommands()
1835 // mark all sub-objects as not removed, except child meshes
1836 list< Handle(_pyMesh) > children;
1837 children.swap( myChildMeshes );
1838 SetRemovedFromStudy( false );
1839 children.swap( myChildMeshes );
1843 _pyObject::ClearCommands();
1845 list< Handle(_pySubMesh) >::iterator sm = mySubmeshes.begin();
1846 for ( ; sm != mySubmeshes.end(); ++sm )
1847 (*sm)->ClearCommands();
1849 list< Handle(_pyGroup) >::iterator gr = myGroups.begin();
1850 for ( ; gr != myGroups.end(); ++gr )
1851 (*gr)->ClearCommands();
1853 list< Handle(_pyMeshEditor)>::iterator e = myEditors.begin();
1854 for ( ; e != myEditors.end(); ++e )
1855 (*e)->ClearCommands();
1858 //================================================================================
1860 * \brief Add a father mesh by ID
1862 //================================================================================
1864 void _pyMesh::addFatherMesh( const _pyID& meshID )
1866 if ( !meshID.IsEmpty() )
1867 addFatherMesh( Handle(_pyMesh)::DownCast( theGen->FindObject( meshID )));
1870 //================================================================================
1872 * \brief Add a father mesh
1874 //================================================================================
1876 void _pyMesh::addFatherMesh( const Handle(_pyMesh)& mesh )
1878 if ( !mesh.IsNull() )
1880 //myFatherMeshes.push_back( mesh );
1881 mesh->myChildMeshes.push_back( this );
1883 // protect last Compute() from clearing by the next Compute()
1884 mesh->myLastComputeCmd.Nullify();
1888 //================================================================================
1890 * \brief MeshEditor convert its commands to ones of mesh
1892 //================================================================================
1894 _pyMeshEditor::_pyMeshEditor(const Handle(_pyCommand)& theCreationCmd):
1895 _pyObject( theCreationCmd )
1897 myMesh = theCreationCmd->GetObject();
1898 myCreationCmdStr = theCreationCmd->GetString();
1899 theCreationCmd->Clear();
1901 Handle(_pyMesh) mesh = ObjectToMesh( theGen->FindObject( myMesh ));
1902 if ( !mesh.IsNull() )
1903 mesh->AddEditor( this );
1906 //================================================================================
1908 * \brief convert its commands to ones of mesh
1910 //================================================================================
1912 void _pyMeshEditor::Process( const Handle(_pyCommand)& theCommand)
1914 // names of SMESH_MeshEditor methods fully equal to methods of python class Mesh, so
1915 // commands calling this methods are converted to calls of methods of Mesh
1916 static TStringSet sameMethods;
1917 if ( sameMethods.empty() ) {
1918 const char * names[] = {
1919 "RemoveElements","RemoveNodes","RemoveOrphanNodes","AddNode","Add0DElement","AddEdge","AddFace","AddPolygonalFace","AddBall",
1920 "AddVolume","AddPolyhedralVolume","AddPolyhedralVolumeByFaces","MoveNode", "MoveClosestNodeToPoint",
1921 "InverseDiag","DeleteDiag","Reorient","ReorientObject","TriToQuad","SplitQuad","SplitQuadObject",
1922 "BestSplit","Smooth","SmoothObject","SmoothParametric","SmoothParametricObject",
1923 "ConvertToQuadratic","ConvertFromQuadratic","RenumberNodes","RenumberElements",
1924 "RotationSweep","RotationSweepObject","RotationSweepObject1D","RotationSweepObject2D",
1925 "ExtrusionSweep","AdvancedExtrusion","ExtrusionSweepObject","ExtrusionSweepObject1D","ExtrusionSweepObject2D",
1926 "ExtrusionAlongPath","ExtrusionAlongPathObject","ExtrusionAlongPathX",
1927 "ExtrusionAlongPathObject1D","ExtrusionAlongPathObject2D",
1928 "Mirror","MirrorObject","Translate","TranslateObject","Rotate","RotateObject",
1929 "FindCoincidentNodes",/*"FindCoincidentNodesOnPart",*/"MergeNodes","FindEqualElements",
1930 "MergeElements","MergeEqualElements","SewFreeBorders","SewConformFreeBorders",
1931 "SewBorderToSide","SewSideElements","ChangeElemNodes","GetLastCreatedNodes",
1932 "GetLastCreatedElems",
1933 "MirrorMakeMesh","MirrorObjectMakeMesh","TranslateMakeMesh",
1934 "TranslateObjectMakeMesh","RotateMakeMesh","RotateObjectMakeMesh","MakeBoundaryMesh",
1935 "MakeBoundaryElements", "SplitVolumesIntoTetra"
1936 ,"" }; // <- mark of the end
1937 sameMethods.Insert( names );
1940 // names of SMESH_MeshEditor methods which differ from methods of class Mesh
1941 // only by last two arguments
1942 static TStringSet diffLastTwoArgsMethods;
1943 if (diffLastTwoArgsMethods.empty() ) {
1944 const char * names[] = {
1945 "MirrorMakeGroups","MirrorObjectMakeGroups",
1946 "TranslateMakeGroups","TranslateObjectMakeGroups",
1947 "RotateMakeGroups","RotateObjectMakeGroups",
1948 ""};// <- mark of the end
1949 diffLastTwoArgsMethods.Insert( names );
1952 const TCollection_AsciiString & method = theCommand->GetMethod();
1953 bool isPyMeshMethod = sameMethods.Contains( method );
1954 if ( !isPyMeshMethod )
1956 //Replace SMESH_MeshEditor "MakeGroups" functions by the Mesh
1957 //functions with the flag "theMakeGroups = True" like:
1958 //SMESH_MeshEditor.CmdMakeGroups => Mesh.Cmd(...,True)
1959 int pos = method.Search("MakeGroups");
1962 isPyMeshMethod = true;
1963 bool is0DmethId = ( method == "ExtrusionSweepMakeGroups0D" );
1964 bool is0DmethObj = ( method == "ExtrusionSweepObject0DMakeGroups");
1966 // 1. Remove "MakeGroups" from the Command
1967 TCollection_AsciiString aMethod = theCommand->GetMethod();
1968 int nbArgsToAdd = diffLastTwoArgsMethods.Contains(aMethod) ? 2 : 1;
1971 pos = pos-2; //Remove "0D" from the Command too
1972 aMethod.Trunc(pos-1);
1973 theCommand->SetMethod(aMethod);
1975 // 2. And add last "True" argument(s)
1976 while(nbArgsToAdd--)
1977 theCommand->SetArg(theCommand->GetNbArgs()+1,"True");
1978 if( is0DmethId || is0DmethObj )
1979 theCommand->SetArg(theCommand->GetNbArgs()+1,"True");
1983 // ExtrusionSweep0D() -> ExtrusionSweep()
1984 // ExtrusionSweepObject0D() -> ExtrusionSweepObject()
1985 if ( !isPyMeshMethod && ( method == "ExtrusionSweep0D" ||
1986 method == "ExtrusionSweepObject0D" ))
1988 isPyMeshMethod = true;
1989 theCommand->SetMethod( method.SubString( 1, method.Length()-2));
1990 theCommand->SetArg(theCommand->GetNbArgs()+1,"False"); //sets flag "MakeGroups = False"
1991 theCommand->SetArg(theCommand->GetNbArgs()+1,"True"); //sets flag "IsNode = True"
1993 // set "ExtrusionAlongPathX()" instead of "ExtrusionAlongPathObjX()"
1994 if ( !isPyMeshMethod && method == "ExtrusionAlongPathObjX")
1996 isPyMeshMethod = true;
1997 theCommand->SetMethod("ExtrusionAlongPathX");
2000 // set "FindCoincidentNodesOnPart()" instead of "FindCoincidentNodesOnPartBut()"
2001 if ( !isPyMeshMethod && method == "FindCoincidentNodesOnPartBut")
2003 isPyMeshMethod = true;
2004 theCommand->SetMethod("FindCoincidentNodesOnPart");
2006 // DoubleNode...New(...) -> DoubleNode...(...,True)
2007 if ( !isPyMeshMethod && ( method == "DoubleNodeElemGroupNew" ||
2008 method == "DoubleNodeElemGroupsNew" ||
2009 method == "DoubleNodeGroupNew" ||
2010 method == "DoubleNodeGroupsNew" ||
2011 method == "DoubleNodeElemGroup2New" ||
2012 method == "DoubleNodeElemGroups2New"))
2014 isPyMeshMethod = true;
2015 const int excessLen = 3 + int( method.Value( method.Length()-3 ) == '2' );
2016 theCommand->SetMethod( method.SubString( 1, method.Length()-excessLen));
2017 if ( excessLen == 3 )
2019 theCommand->SetArg(theCommand->GetNbArgs()+1,"True");
2021 else if ( theCommand->GetArg(4) == "0" ||
2022 theCommand->GetArg(5) == "0" )
2024 // [ nothing, Group ] = DoubleNodeGroup2New(,,,False, True) ->
2025 // Group = DoubleNodeGroup2New(,,,False, True)
2026 _pyID groupID = theCommand->GetResultValue( 1 + int( theCommand->GetArg(4) == "0"));
2027 theCommand->SetResultValue( groupID );
2030 // ConvertToQuadraticObject(bool,obj) -> ConvertToQuadratic(bool,obj)
2031 // ConvertFromQuadraticObject(obj) -> ConvertFromQuadratic(obj)
2032 if ( !isPyMeshMethod && ( method == "ConvertToQuadraticObject" ||
2033 method == "ConvertFromQuadraticObject" ))
2035 isPyMeshMethod = true;
2036 theCommand->SetMethod( method.SubString( 1, method.Length()-6));
2038 // FindAmongElementsByPoint(meshPart, x, y, z, elementType) ->
2039 // FindElementsByPoint(x, y, z, elementType, meshPart)
2040 if ( !isPyMeshMethod && method == "FindAmongElementsByPoint" )
2042 isPyMeshMethod = true;
2043 theCommand->SetMethod( "FindElementsByPoint" );
2044 // make the 1st arg be the last one
2045 _pyID partID = theCommand->GetArg( 1 );
2046 int nbArgs = theCommand->GetNbArgs();
2047 for ( int i = 2; i <= nbArgs; ++i )
2048 theCommand->SetArg( i-1, theCommand->GetArg( i ));
2049 theCommand->SetArg( nbArgs, partID );
2051 // Reorient2D( mesh, dir, face, point ) -> Reorient2D( mesh, dir, faceORpoint )
2052 if ( !isPyMeshMethod && method == "Reorient2D" )
2054 isPyMeshMethod = true;
2055 _AString mesh = theCommand->GetArg( 1 );
2056 _AString dir = theCommand->GetArg( 2 );
2057 _AString face = theCommand->GetArg( 3 );
2058 _AString point = theCommand->GetArg( 4 );
2059 theCommand->RemoveArgs();
2060 theCommand->SetArg( 1, mesh );
2061 theCommand->SetArg( 2, dir );
2062 if ( face.Value(1) == '-' || face.Value(1) == '0' ) // invalid: face <= 0
2063 theCommand->SetArg( 3, point );
2065 theCommand->SetArg( 3, face );
2068 // meshes made by *MakeMesh() methods are not wrapped by _pyMesh,
2069 // so let _pyMesh care of it (TMP?)
2070 // if ( theCommand->GetMethod().Search("MakeMesh") != -1 )
2071 // _pyMesh( new _pyCommand( theCommand->GetString(), 0 )); // for theGen->SetAccessorMethod()
2072 if ( isPyMeshMethod )
2074 theCommand->SetObject( myMesh );
2078 // editor creation command is needed only if any editor function is called
2079 theGen->AddMeshAccessorMethod( theCommand ); // for *Object()
2080 if ( !myCreationCmdStr.IsEmpty() ) {
2081 GetCreationCmd()->GetString() = myCreationCmdStr;
2082 myCreationCmdStr.Clear();
2087 //================================================================================
2089 * \brief Return true if my mesh can be removed
2091 //================================================================================
2093 bool _pyMeshEditor::CanClear()
2095 Handle(_pyMesh) mesh = ObjectToMesh( theGen->FindObject( myMesh ));
2096 return mesh.IsNull() ? true : mesh->CanClear();
2099 //================================================================================
2101 * \brief _pyHypothesis constructor
2102 * \param theCreationCmd -
2104 //================================================================================
2106 _pyHypothesis::_pyHypothesis(const Handle(_pyCommand)& theCreationCmd):
2107 _pyObject( theCreationCmd ), myCurCrMethod(0)
2109 myIsAlgo = myIsWrapped = /*myIsConverted = myIsLocal = myDim = */false;
2112 //================================================================================
2114 * \brief Creates algorithm or hypothesis
2115 * \param theCreationCmd - The engine command creating a hypothesis
2116 * \retval Handle(_pyHypothesis) - Result _pyHypothesis
2118 //================================================================================
2120 Handle(_pyHypothesis) _pyHypothesis::NewHypothesis( const Handle(_pyCommand)& theCreationCmd)
2122 // theCreationCmd: CreateHypothesis( "theHypType", "theLibName" )
2123 ASSERT (( theCreationCmd->GetMethod() == "CreateHypothesis"));
2125 Handle(_pyHypothesis) hyp, algo;
2128 const TCollection_AsciiString & hypTypeQuoted = theCreationCmd->GetArg( 1 );
2129 if ( hypTypeQuoted.IsEmpty() )
2132 TCollection_AsciiString hypType =
2133 hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
2135 algo = new _pyAlgorithm( theCreationCmd );
2136 hyp = new _pyHypothesis( theCreationCmd );
2138 if ( hypType == "NumberOfSegments" ) {
2139 hyp = new _pyNumberOfSegmentsHyp( theCreationCmd );
2140 hyp->SetConvMethodAndType( "NumberOfSegments", "Regular_1D");
2141 // arg of SetNumberOfSegments() will become the 1-st arg of hyp creation command
2142 hyp->AddArgMethod( "SetNumberOfSegments" );
2143 // arg of SetScaleFactor() will become the 2-nd arg of hyp creation command
2144 hyp->AddArgMethod( "SetScaleFactor" );
2145 hyp->AddArgMethod( "SetReversedEdges" );
2146 // same for ""CompositeSegment_1D:
2147 hyp->SetConvMethodAndType( "NumberOfSegments", "CompositeSegment_1D");
2148 hyp->AddArgMethod( "SetNumberOfSegments" );
2149 hyp->AddArgMethod( "SetScaleFactor" );
2150 hyp->AddArgMethod( "SetReversedEdges" );
2152 else if ( hypType == "SegmentLengthAroundVertex" ) {
2153 hyp = new _pySegmentLengthAroundVertexHyp( theCreationCmd );
2154 hyp->SetConvMethodAndType( "LengthNearVertex", "Regular_1D" );
2155 hyp->AddArgMethod( "SetLength" );
2156 // same for ""CompositeSegment_1D:
2157 hyp->SetConvMethodAndType( "LengthNearVertex", "CompositeSegment_1D");
2158 hyp->AddArgMethod( "SetLength" );
2160 else if ( hypType == "LayerDistribution2D" ) {
2161 hyp = new _pyLayerDistributionHypo( theCreationCmd, "Get2DHypothesis" );
2162 hyp->SetConvMethodAndType( "LayerDistribution", "RadialQuadrangle_1D2D");
2164 else if ( hypType == "LayerDistribution" ) {
2165 hyp = new _pyLayerDistributionHypo( theCreationCmd, "Get3DHypothesis" );
2166 hyp->SetConvMethodAndType( "LayerDistribution", "RadialPrism_3D");
2168 else if ( hypType == "CartesianParameters3D" ) {
2169 hyp = new _pyComplexParamHypo( theCreationCmd );
2170 hyp->SetConvMethodAndType( "SetGrid", "Cartesian_3D");
2171 for ( int iArg = 0; iArg < 4; ++iArg )
2172 hyp->setCreationArg( iArg+1, "[]");
2176 hyp = theGen->GetHypothesisReader()->GetHypothesis( hypType, theCreationCmd );
2179 return algo->IsValid() ? algo : hyp;
2182 //================================================================================
2184 * \brief Returns true if addition of this hypothesis to a given mesh can be
2185 * wrapped into hypothesis creation
2187 //================================================================================
2189 bool _pyHypothesis::IsWrappable(const _pyID& theMesh) const
2191 if ( !myIsWrapped && myMesh == theMesh && IsInStudy() )
2193 Handle(_pyObject) pyMesh = theGen->FindObject( myMesh );
2194 if ( !pyMesh.IsNull() && pyMesh->IsInStudy() )
2200 //================================================================================
2202 * \brief Convert the command adding a hypothesis to mesh into a smesh command
2203 * \param theCmd - The command like mesh.AddHypothesis( geom, hypo )
2204 * \param theAlgo - The algo that can create this hypo
2205 * \retval bool - false if the command cant be converted
2207 //================================================================================
2209 bool _pyHypothesis::Addition2Creation( const Handle(_pyCommand)& theCmd,
2210 const _pyID& theMesh)
2212 ASSERT(( theCmd->GetMethod() == "AddHypothesis" ));
2214 if ( !IsWrappable( theMesh ))
2217 myGeom = theCmd->GetArg( 1 );
2219 Handle(_pyHypothesis) algo;
2221 // find algo created on myGeom in theMesh
2222 algo = theGen->FindAlgo( myGeom, theMesh, this );
2223 if ( algo.IsNull() )
2225 // attach hypothesis creation command to be after algo creation command
2226 // because it can be new created instance of algorithm
2227 algo->GetCreationCmd()->AddDependantCmd( theCmd );
2231 // mesh.AddHypothesis(geom,hyp) --> hyp = <theMesh or algo>.myCreationMethod(args)
2232 theCmd->SetResultValue( GetID() );
2233 theCmd->SetObject( IsAlgo() ? theMesh : algo->GetID());
2234 theCmd->SetMethod( IsAlgo() ? GetAlgoCreationMethod() : GetCreationMethod( algo->GetAlgoType() ));
2235 // set args (geom will be set by _pyMesh calling this method)
2236 theCmd->RemoveArgs();
2237 for ( size_t i = 0; i < myCurCrMethod->myArgs.size(); ++i ) {
2238 if ( !myCurCrMethod->myArgs[ i ].IsEmpty() )
2239 theCmd->SetArg( i+1, myCurCrMethod->myArgs[ i ]);
2241 theCmd->SetArg( i+1, "[]");
2243 // set a new creation command
2244 GetCreationCmd()->Clear();
2245 // replace creation command by wrapped instance
2246 // please note, that hypothesis attaches to algo creation command (see upper)
2247 SetCreationCmd( theCmd );
2250 // clear commands setting arg values
2251 list < Handle(_pyCommand) >::iterator argCmd = myArgCommands.begin();
2252 for ( ; argCmd != myArgCommands.end(); ++argCmd )
2255 // set unknown arg commands after hypo creation
2256 Handle(_pyCommand) afterCmd = myIsWrapped ? theCmd : GetCreationCmd();
2257 list<Handle(_pyCommand)>::iterator cmd = myUnusedCommands.begin();
2258 for ( ; cmd != myUnusedCommands.end(); ++cmd ) {
2259 afterCmd->AddDependantCmd( *cmd );
2265 //================================================================================
2267 * \brief Remember hypothesis parameter values
2268 * \param theCommand - The called hypothesis method
2270 //================================================================================
2272 void _pyHypothesis::Process( const Handle(_pyCommand)& theCommand)
2274 ASSERT( !myIsAlgo );
2275 if ( !theGen->IsToKeepAllCommands() )
2276 rememberCmdOfParameter( theCommand );
2278 bool usedCommand = false;
2279 TType2CrMethod::iterator type2meth = myAlgoType2CreationMethod.begin();
2280 for ( ; type2meth != myAlgoType2CreationMethod.end(); ++type2meth )
2282 CreationMethod& crMethod = type2meth->second;
2283 for ( size_t i = 0; i < crMethod.myArgMethods.size(); ++i ) {
2284 if ( crMethod.myArgMethods[ i ] == theCommand->GetMethod() ) {
2286 myArgCommands.push_back( theCommand );
2288 while ( crMethod.myArgs.size() < i+1 )
2289 crMethod.myArgs.push_back( "[]" );
2290 crMethod.myArgs[ i ] = theCommand->GetArg( crMethod.myArgNb[i] );
2295 myUnusedCommands.push_back( theCommand );
2298 //================================================================================
2300 * \brief Finish conversion
2302 //================================================================================
2304 void _pyHypothesis::Flush()
2308 list < Handle(_pyCommand) >::iterator cmd = myArgCommands.begin();
2309 for ( ; cmd != myArgCommands.end(); ++cmd ) {
2310 // Add access to a wrapped mesh
2311 theGen->AddMeshAccessorMethod( *cmd );
2312 // Add access to a wrapped algorithm
2313 theGen->AddAlgoAccessorMethod( *cmd );
2315 cmd = myUnusedCommands.begin();
2316 for ( ; cmd != myUnusedCommands.end(); ++cmd ) {
2317 // Add access to a wrapped mesh
2318 theGen->AddMeshAccessorMethod( *cmd );
2319 // Add access to a wrapped algorithm
2320 theGen->AddAlgoAccessorMethod( *cmd );
2323 // forget previous hypothesis modifications
2324 myArgCommands.clear();
2325 myUnusedCommands.clear();
2328 //================================================================================
2330 * \brief clear creation, arg and unkown commands
2332 //================================================================================
2334 void _pyHypothesis::ClearAllCommands()
2336 GetCreationCmd()->Clear();
2337 list<Handle(_pyCommand)>::iterator cmd = myArgCommands.begin();
2338 for ( ; cmd != myArgCommands.end(); ++cmd )
2340 cmd = myUnusedCommands.begin();
2341 for ( ; cmd != myUnusedCommands.end(); ++cmd )
2346 //================================================================================
2348 * \brief Assign fields of theOther to me except myIsWrapped
2350 //================================================================================
2352 void _pyHypothesis::Assign( const Handle(_pyHypothesis)& theOther,
2353 const _pyID& theMesh )
2355 // myCreationCmd = theOther->myCreationCmd;
2356 myIsAlgo = theOther->myIsAlgo;
2357 myIsWrapped = false;
2358 myGeom = theOther->myGeom;
2360 myAlgoType2CreationMethod = theOther->myAlgoType2CreationMethod;
2361 myAccumulativeMethods = theOther->myAccumulativeMethods;
2362 //myUnusedCommands = theOther->myUnusedCommands;
2363 // init myCurCrMethod
2364 GetCreationMethod( theOther->GetAlgoType() );
2367 //================================================================================
2369 * \brief Analyze my erasability depending on myReferredObjs
2371 //================================================================================
2373 bool _pyHypothesis::CanClear()
2377 list< Handle(_pyObject) >::iterator obj = myReferredObjs.begin();
2378 for ( ; obj != myReferredObjs.end(); ++obj )
2379 if ( (*obj)->CanClear() )
2386 //================================================================================
2388 * \brief Clear my commands depending on usage by meshes
2390 //================================================================================
2392 void _pyHypothesis::ClearCommands()
2394 // if ( !theGen->IsToKeepAllCommands() )
2396 // bool isUsed = false;
2397 // int lastComputeOrder = 0;
2398 // list<Handle(_pyCommand) >::iterator cmd = myComputeCmds.begin();
2399 // for ( ; cmd != myComputeCmds.end(); ++cmd )
2400 // if ( ! (*cmd)->IsEmpty() )
2403 // if ( (*cmd)->GetOrderNb() > lastComputeOrder )
2404 // lastComputeOrder = (*cmd)->GetOrderNb();
2408 // SetRemovedFromStudy( true );
2412 // // clear my commands invoked after lastComputeOrder
2413 // // map<TCollection_AsciiString, list< Handle(_pyCommand) > >::iterator m2c;
2414 // // for ( m2c = myMeth2Commands.begin(); m2c != myMeth2Commands.end(); ++m2c )
2416 // // list< Handle(_pyCommand)> & cmds = m2c->second;
2417 // // if ( !cmds.empty() && cmds.back()->GetOrderNb() > lastComputeOrder )
2418 // // cmds.back()->Clear();
2422 _pyObject::ClearCommands();
2425 //================================================================================
2427 * \brief Find arguments that are objects like mesh, group, geometry
2428 * \param meshes - referred meshes (directly or indirrectly)
2429 * \retval bool - false if a referred geometry is not in the study
2431 //================================================================================
2433 bool _pyHypothesis::GetReferredMeshesAndGeom( list< Handle(_pyMesh) >& meshes )
2435 if ( IsAlgo() ) return true;
2437 bool geomPublished = true;
2438 vector< _AString > args;
2439 TType2CrMethod::iterator type2meth = myAlgoType2CreationMethod.begin();
2440 for ( ; type2meth != myAlgoType2CreationMethod.end(); ++type2meth )
2442 CreationMethod& crMethod = type2meth->second;
2443 args.insert( args.end(), crMethod.myArgs.begin(), crMethod.myArgs.end());
2445 list<Handle(_pyCommand)>::iterator cmd = myUnusedCommands.begin();
2446 for ( ; cmd != myUnusedCommands.end(); ++cmd ) {
2447 for ( int nb = (*cmd)->GetNbArgs(); nb; --nb )
2448 args.push_back( (*cmd)->GetArg( nb ));
2451 for ( size_t i = 0; i < args.size(); ++i )
2453 list< _pyID > idList = _pyCommand::GetStudyEntries( args[ i ]);
2454 if ( idList.empty() && !args[ i ].IsEmpty() )
2455 idList.push_back( args[ i ]);
2456 list< _pyID >::iterator id = idList.begin();
2457 for ( ; id != idList.end(); ++id )
2459 Handle(_pyObject) obj = theGen->FindObject( *id );
2460 if ( obj.IsNull() ) obj = theGen->FindHyp( *id );
2463 if ( theGen->IsGeomObject( *id ) && theGen->IsNotPublished( *id ))
2464 geomPublished = false;
2468 myReferredObjs.push_back( obj );
2469 Handle(_pyMesh) mesh = ObjectToMesh( obj );
2470 if ( !mesh.IsNull() )
2471 meshes.push_back( mesh );
2472 // prevent clearing not published hyps referred e.g. by "LayerDistribution"
2473 else if ( obj->IsKind( STANDARD_TYPE( _pyHypothesis )) && this->IsInStudy() )
2474 obj->SetRemovedFromStudy( false );
2478 return geomPublished;
2481 //================================================================================
2483 * \brief Remember theCommand setting a parameter
2485 //================================================================================
2487 void _pyHypothesis::rememberCmdOfParameter( const Handle(_pyCommand) & theCommand )
2489 // parameters are discriminated by method name
2490 _AString method = theCommand->GetMethod();
2491 if ( myAccumulativeMethods.count( method ))
2492 return; // this method adds values and not override the previus value
2494 // discriminate commands setting different parameters via one method
2495 // by passing parameter names like e.g. SetOption("size", "0.2")
2496 if ( theCommand->GetString().FirstLocationInSet( "'\"", 1, theCommand->Length() ) &&
2497 theCommand->GetNbArgs() > 1 )
2499 // mangle method by appending a 1st textual arg
2500 for ( int iArg = 1; iArg <= theCommand->GetNbArgs(); ++iArg )
2502 const TCollection_AsciiString& arg = theCommand->GetArg( iArg );
2503 if ( arg.Value(1) != '\"' && arg.Value(1) != '\'' ) continue;
2504 if ( !isalpha( arg.Value(2))) continue;
2509 // parameters are discriminated by method name
2510 list< Handle(_pyCommand)>& cmds = myMeth2Commands[ method /*theCommand->GetMethod()*/ ];
2511 if ( !cmds.empty() && !isCmdUsedForCompute( cmds.back() ))
2513 cmds.back()->Clear(); // previous parameter value has not been used
2514 cmds.back() = theCommand;
2518 cmds.push_back( theCommand );
2522 //================================================================================
2524 * \brief Return true if a setting parameter command ha been used to compute mesh
2526 //================================================================================
2528 bool _pyHypothesis::isCmdUsedForCompute( const Handle(_pyCommand) & cmd,
2529 _pyCommand::TAddr avoidComputeAddr ) const
2531 bool isUsed = false;
2532 map< _pyCommand::TAddr, list<Handle(_pyCommand) > >::const_iterator addr2cmds =
2533 myComputeAddr2Cmds.begin();
2534 for ( ; addr2cmds != myComputeAddr2Cmds.end() && !isUsed; ++addr2cmds )
2536 if ( addr2cmds->first == avoidComputeAddr ) continue;
2537 const list<Handle(_pyCommand)> & cmds = addr2cmds->second;
2538 isUsed = ( std::find( cmds.begin(), cmds.end(), cmd ) != cmds.end() );
2543 //================================================================================
2545 * \brief Save commands setting parameters as they are used for a mesh computation
2547 //================================================================================
2549 void _pyHypothesis::MeshComputed( const Handle(_pyCommand)& theComputeCmd )
2551 myComputeCmds.push_back( theComputeCmd );
2552 list<Handle(_pyCommand)>& savedCmds = myComputeAddr2Cmds[ theComputeCmd->GetAddress() ];
2554 map<TCollection_AsciiString, list< Handle(_pyCommand) > >::iterator m2c;
2555 for ( m2c = myMeth2Commands.begin(); m2c != myMeth2Commands.end(); ++m2c )
2556 savedCmds.push_back( m2c->second.back() );
2559 //================================================================================
2561 * \brief Clear commands setting parameters as a mesh computed using them is cleared
2563 //================================================================================
2565 void _pyHypothesis::ComputeDiscarded( const Handle(_pyCommand)& theComputeCmd )
2567 list<Handle(_pyCommand)>& savedCmds = myComputeAddr2Cmds[ theComputeCmd->GetAddress() ];
2569 list<Handle(_pyCommand)>::iterator cmd = savedCmds.begin();
2570 for ( ; cmd != savedCmds.end(); ++cmd )
2572 // check if a cmd has been used to compute another mesh
2573 if ( isCmdUsedForCompute( *cmd, theComputeCmd->GetAddress() ))
2575 // check if a cmd is a sole command setting its parameter;
2576 // don't use method name for search as it can change
2577 map<TCollection_AsciiString, list<Handle(_pyCommand)> >::iterator
2578 m2cmds = myMeth2Commands.begin();
2579 for ( ; m2cmds != myMeth2Commands.end(); ++m2cmds )
2581 list< Handle(_pyCommand)>& cmds = m2cmds->second;
2582 list< Handle(_pyCommand)>::iterator cmdIt = std::find( cmds.begin(), cmds.end(), *cmd );
2583 if ( cmdIt != cmds.end() )
2585 if ( cmds.back() != *cmd )
2587 cmds.erase( cmdIt );
2594 myComputeAddr2Cmds.erase( theComputeCmd->GetAddress() );
2597 //================================================================================
2599 * \brief Sets an argNb-th argument of current creation command
2600 * \param argNb - argument index countered from 1
2602 //================================================================================
2604 void _pyHypothesis::setCreationArg( const int argNb, const _AString& arg )
2606 if ( myCurCrMethod )
2608 while ( myCurCrMethod->myArgs.size() < argNb )
2609 myCurCrMethod->myArgs.push_back( "None" );
2610 if ( arg.IsEmpty() )
2611 myCurCrMethod->myArgs[ argNb-1 ] = "None";
2613 myCurCrMethod->myArgs[ argNb-1 ] = arg;
2618 //================================================================================
2620 * \brief Remember hypothesis parameter values
2621 * \param theCommand - The called hypothesis method
2623 //================================================================================
2625 void _pyComplexParamHypo::Process( const Handle(_pyCommand)& theCommand)
2627 if ( GetAlgoType() == "Cartesian_3D" )
2629 // CartesianParameters3D hyp
2631 if ( theCommand->GetMethod() == "SetSizeThreshold" )
2633 setCreationArg( 4, theCommand->GetArg( 1 ));
2634 myArgCommands.push_back( theCommand );
2637 if ( theCommand->GetMethod() == "SetGrid" ||
2638 theCommand->GetMethod() == "SetGridSpacing" )
2640 TCollection_AsciiString axis = theCommand->GetArg( theCommand->GetNbArgs() );
2641 int iArg = axis.Value(1) - '0';
2642 if ( theCommand->GetMethod() == "SetGrid" )
2644 setCreationArg( 1+iArg, theCommand->GetArg( 1 ));
2648 myCurCrMethod->myArgs[ iArg ] = "[ ";
2649 myCurCrMethod->myArgs[ iArg ] += theCommand->GetArg( 1 );
2650 myCurCrMethod->myArgs[ iArg ] += ", ";
2651 myCurCrMethod->myArgs[ iArg ] += theCommand->GetArg( 2 );
2652 myCurCrMethod->myArgs[ iArg ] += "]";
2654 myArgCommands.push_back( theCommand );
2655 rememberCmdOfParameter( theCommand );
2660 if( theCommand->GetMethod() == "SetLength" )
2662 // NOW it is OBSOLETE
2663 // ex: hyp.SetLength(start, 1)
2664 // hyp.SetLength(end, 0)
2665 ASSERT(( theCommand->GetArg( 2 ).IsIntegerValue() ));
2666 int i = 1 - theCommand->GetArg( 2 ).IntegerValue();
2667 TType2CrMethod::iterator type2meth = myAlgoType2CreationMethod.begin();
2668 for ( ; type2meth != myAlgoType2CreationMethod.end(); ++type2meth )
2670 CreationMethod& crMethod = type2meth->second;
2671 while ( crMethod.myArgs.size() < i+1 )
2672 crMethod.myArgs.push_back( "[]" );
2673 crMethod.myArgs[ i ] = theCommand->GetArg( 1 ); // arg value
2675 myArgCommands.push_back( theCommand );
2679 _pyHypothesis::Process( theCommand );
2682 //================================================================================
2684 * \brief Clear SetObjectEntry() as it is called by methods of Mesh_Segment
2686 //================================================================================
2688 void _pyComplexParamHypo::Flush()
2692 list < Handle(_pyCommand) >::iterator cmd = myUnusedCommands.begin();
2693 for ( ; cmd != myUnusedCommands.end(); ++cmd )
2694 if ((*cmd)->GetMethod() == "SetObjectEntry" )
2699 //================================================================================
2701 * \brief Convert methods of 1D hypotheses to my own methods
2702 * \param theCommand - The called hypothesis method
2704 //================================================================================
2706 void _pyLayerDistributionHypo::Process( const Handle(_pyCommand)& theCommand)
2708 if ( theCommand->GetMethod() != "SetLayerDistribution" )
2711 const _pyID& hyp1dID = theCommand->GetArg( 1 );
2712 // Handle(_pyHypothesis) hyp1d = theGen->FindHyp( hyp1dID );
2713 // if ( hyp1d.IsNull() && ! my1dHyp.IsNull()) // apparently hypId changed at study restoration
2715 // TCollection_AsciiString cmd =
2716 // my1dHyp->GetCreationCmd()->GetIndentation() + hyp1dID + " = " + my1dHyp->GetID();
2717 // Handle(_pyCommand) newCmd = theGen->AddCommand( cmd );
2718 // theGen->SetCommandAfter( newCmd, my1dHyp->GetCreationCmd() );
2721 // else if ( !my1dHyp.IsNull() && hyp1dID != my1dHyp->GetID() )
2723 // // 1D hypo is already set, so distribution changes and the old
2724 // // 1D hypo is thrown away
2725 // my1dHyp->ClearAllCommands();
2728 // //my1dHyp->SetRemovedFromStudy( false );
2730 // if ( !myArgCommands.empty() )
2731 // myArgCommands.back()->Clear();
2732 myCurCrMethod->myArgs.push_back( hyp1dID );
2733 myArgCommands.push_back( theCommand );
2736 //================================================================================
2739 * \param theAdditionCmd - command to be converted
2740 * \param theMesh - mesh instance
2741 * \retval bool - status
2743 //================================================================================
2745 bool _pyLayerDistributionHypo::Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
2746 const _pyID& theMesh)
2748 myIsWrapped = false;
2750 if ( my1dHyp.IsNull() )
2753 // set "SetLayerDistribution()" after addition cmd
2754 theAdditionCmd->AddDependantCmd( myArgCommands.front() );
2756 _pyID geom = theAdditionCmd->GetArg( 1 );
2758 Handle(_pyHypothesis) algo = theGen->FindAlgo( geom, theMesh, this );
2759 if ( !algo.IsNull() )
2761 my1dHyp->SetMesh( theMesh );
2762 my1dHyp->SetConvMethodAndType(my1dHyp->GetAlgoCreationMethod().ToCString(),
2763 algo->GetAlgoType().ToCString());
2764 if ( !my1dHyp->Addition2Creation( theAdditionCmd, theMesh ))
2767 // clear "SetLayerDistribution()" cmd
2768 myArgCommands.back()->Clear();
2770 // Convert my creation => me = RadialPrismAlgo.Get3DHypothesis()
2772 // find RadialPrism algo created on <geom> for theMesh
2773 GetCreationCmd()->SetObject( algo->GetID() );
2774 GetCreationCmd()->SetMethod( myAlgoMethod );
2775 GetCreationCmd()->RemoveArgs();
2776 theAdditionCmd->AddDependantCmd( GetCreationCmd() );
2782 //================================================================================
2786 //================================================================================
2788 void _pyLayerDistributionHypo::Flush()
2790 // as creation of 1D hyp was written later then it's edition,
2791 // we need to find all it's edition calls and process them
2792 list< Handle(_pyCommand) >::iterator cmd = myArgCommands.begin();
2794 for ( cmd = myArgCommands.begin(); cmd != myArgCommands.end(); ++cmd )
2796 const _pyID& hyp1dID = (*cmd)->GetArg( 1 );
2797 if ( hyp1dID.IsEmpty() ) continue;
2799 Handle(_pyHypothesis) hyp1d = theGen->FindHyp( hyp1dID );
2801 // make a new name for 1D hyp = "HypType" + "_Distribution"
2803 if ( hyp1d.IsNull() ) // apparently hypId changed at study restoration
2805 if ( prevNewName.IsEmpty() ) continue;
2806 newName = prevNewName;
2810 if ( hyp1d->IsWrapped() ) {
2811 newName = hyp1d->GetCreationCmd()->GetMethod();
2814 TCollection_AsciiString hypTypeQuoted = hyp1d->GetCreationCmd()->GetArg(1);
2815 newName = hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
2817 newName += "_Distribution";
2818 prevNewName = newName;
2820 hyp1d->GetCreationCmd()->SetResultValue( newName );
2822 list< Handle(_pyCommand) >& cmds = theGen->GetCommands();
2823 list< Handle(_pyCommand) >::iterator cmdIt = cmds.begin();
2824 for ( ; cmdIt != cmds.end(); ++cmdIt ) {
2825 const _pyID& objID = (*cmdIt)->GetObject();
2826 if ( objID == hyp1dID ) {
2827 if ( !hyp1d.IsNull() )
2829 hyp1d->Process( *cmdIt );
2830 hyp1d->GetCreationCmd()->AddDependantCmd( *cmdIt );
2832 ( *cmdIt )->SetObject( newName );
2835 // Set new hyp name to SetLayerDistribution(hyp1dID) cmd
2836 (*cmd)->SetArg( 1, newName );
2840 //================================================================================
2842 * \brief additionally to Addition2Creation, clears SetDistrType() command
2843 * \param theCmd - AddHypothesis() command
2844 * \param theMesh - mesh to which a hypothesis is added
2845 * \retval bool - convertion result
2847 //================================================================================
2849 bool _pyNumberOfSegmentsHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
2850 const _pyID& theMesh)
2852 if ( IsWrappable( theMesh ) && myCurCrMethod->myArgs.size() > 1 ) {
2853 // scale factor (2-nd arg) is provided: clear SetDistrType(1) command
2854 bool scaleDistrType = false;
2855 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnusedCommands.rbegin();
2856 for ( ; cmd != myUnusedCommands.rend(); ++cmd ) {
2857 if ( (*cmd)->GetMethod() == "SetDistrType" ) {
2858 if ( (*cmd)->GetArg( 1 ) == "1" ) {
2859 scaleDistrType = true;
2862 else if ( !scaleDistrType ) {
2863 // distribution type changed: remove scale factor from args
2864 TType2CrMethod::iterator type2meth = myAlgoType2CreationMethod.begin();
2865 for ( ; type2meth != myAlgoType2CreationMethod.end(); ++type2meth )
2867 CreationMethod& crMethod = type2meth->second;
2868 if ( crMethod.myArgs.size() == 2 )
2869 crMethod.myArgs.pop_back();
2876 return _pyHypothesis::Addition2Creation( theCmd, theMesh );
2879 //================================================================================
2881 * \brief remove repeated commands defining distribution
2883 //================================================================================
2885 void _pyNumberOfSegmentsHyp::Flush()
2887 // find number of the last SetDistrType() command
2888 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnusedCommands.rbegin();
2889 int distrTypeNb = 0;
2890 for ( ; !distrTypeNb && cmd != myUnusedCommands.rend(); ++cmd )
2891 if ( (*cmd)->GetMethod() == "SetDistrType" )
2892 distrTypeNb = (*cmd)->GetOrderNb();
2893 else if (IsWrapped() && (*cmd)->GetMethod() == "SetObjectEntry" )
2896 // clear commands before the last SetDistrType()
2897 list<Handle(_pyCommand)> * cmds[2] = { &myArgCommands, &myUnusedCommands };
2898 for ( int i = 0; i < 2; ++i ) {
2899 set<TCollection_AsciiString> uniqueMethods;
2900 list<Handle(_pyCommand)> & cmdList = *cmds[i];
2901 for ( cmd = cmdList.rbegin(); cmd != cmdList.rend(); ++cmd )
2903 bool clear = ( (*cmd)->GetOrderNb() < distrTypeNb );
2904 const TCollection_AsciiString& method = (*cmd)->GetMethod();
2905 if ( !clear || method == "SetNumberOfSegments" ) {
2906 bool isNewInSet = uniqueMethods.insert( method ).second;
2907 clear = !isNewInSet;
2916 //================================================================================
2918 * \brief Convert the command adding "SegmentLengthAroundVertex" to mesh
2919 * into regular1D.LengthNearVertex( length, vertex )
2920 * \param theCmd - The command like mesh.AddHypothesis( vertex, SegmentLengthAroundVertex )
2921 * \param theMesh - The mesh needing this hypo
2922 * \retval bool - false if the command cant be converted
2924 //================================================================================
2926 bool _pySegmentLengthAroundVertexHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
2927 const _pyID& theMeshID)
2929 if ( IsWrappable( theMeshID )) {
2931 _pyID vertex = theCmd->GetArg( 1 );
2933 // the problem here is that segment algo will not be found
2934 // by pyHypothesis::Addition2Creation() for <vertex>, so we try to find
2935 // geometry where segment algorithm is assigned
2936 Handle(_pyHypothesis) algo;
2937 _pyID geom = vertex;
2938 while ( algo.IsNull() && !geom.IsEmpty()) {
2939 // try to find geom as a father of <vertex>
2940 geom = FatherID( geom );
2941 algo = theGen->FindAlgo( geom, theMeshID, this );
2943 if ( algo.IsNull() )
2944 return false; // also possible to find geom as brother of veretex...
2945 // set geom instead of vertex
2946 theCmd->SetArg( 1, geom );
2948 // set vertex as a second arg
2949 if ( myCurCrMethod->myArgs.size() < 1) setCreationArg( 1, "1" ); // :(
2950 setCreationArg( 2, vertex );
2952 // mesh.AddHypothesis(vertex, SegmentLengthAroundVertex) -->
2953 // theMeshID.LengthNearVertex( length, vertex )
2954 return _pyHypothesis::Addition2Creation( theCmd, theMeshID );
2959 //================================================================================
2961 * \brief _pyAlgorithm constructor
2962 * \param theCreationCmd - The command like "algo = smeshgen.CreateHypothesis(type,lib)"
2964 //================================================================================
2966 _pyAlgorithm::_pyAlgorithm(const Handle(_pyCommand)& theCreationCmd)
2967 : _pyHypothesis( theCreationCmd )
2972 //================================================================================
2974 * \brief Convert the command adding an algorithm to mesh
2975 * \param theCmd - The command like mesh.AddHypothesis( geom, algo )
2976 * \param theMesh - The mesh needing this algo
2977 * \retval bool - false if the command cant be converted
2979 //================================================================================
2981 bool _pyAlgorithm::Addition2Creation( const Handle(_pyCommand)& theCmd,
2982 const _pyID& theMeshID)
2984 // mesh.AddHypothesis(geom,algo) --> theMeshID.myCreationMethod()
2985 if ( _pyHypothesis::Addition2Creation( theCmd, theMeshID )) {
2986 theGen->SetAccessorMethod( GetID(), "GetAlgorithm()" );
2992 //================================================================================
2994 * \brief Return starting position of a part of python command
2995 * \param thePartIndex - The index of command part
2996 * \retval int - Part position
2998 //================================================================================
3000 int _pyCommand::GetBegPos( int thePartIndex )
3004 if ( myBegPos.Length() < thePartIndex )
3006 return myBegPos( thePartIndex );
3009 //================================================================================
3011 * \brief Store starting position of a part of python command
3012 * \param thePartIndex - The index of command part
3013 * \param thePosition - Part position
3015 //================================================================================
3017 void _pyCommand::SetBegPos( int thePartIndex, int thePosition )
3019 while ( myBegPos.Length() < thePartIndex )
3020 myBegPos.Append( UNKNOWN );
3021 myBegPos( thePartIndex ) = thePosition;
3024 //================================================================================
3026 * \brief Returns whitespace symbols at the line beginning
3027 * \retval TCollection_AsciiString - result
3029 //================================================================================
3031 TCollection_AsciiString _pyCommand::GetIndentation()
3034 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
3035 GetWord( myString, end, true );
3037 end = GetBegPos( RESULT_IND );
3038 return myString.SubString( 1, end - 1 );
3041 //================================================================================
3043 * \brief Return substring of python command looking like ResultValue = Obj.Meth()
3044 * \retval const TCollection_AsciiString & - ResultValue substring
3046 //================================================================================
3048 const TCollection_AsciiString & _pyCommand::GetResultValue()
3050 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
3052 SetBegPos( RESULT_IND, EMPTY );
3053 int begPos, endPos = myString.Location( "=", 1, Length() );
3057 while ( begPos < endPos && isspace( myString.Value( begPos ))) ++begPos;
3058 if ( begPos < endPos )
3060 SetBegPos( RESULT_IND, begPos );
3062 while ( begPos < endPos && isspace( myString.Value( endPos ))) --endPos;
3063 myRes = myString.SubString( begPos, endPos );
3070 //================================================================================
3072 * \brief Return number of python command result value ResultValue = Obj.Meth()
3075 //================================================================================
3077 const int _pyCommand::GetNbResultValues()
3081 int endPos = myString.Location( "=", 1, Length() );
3082 TCollection_AsciiString str = "";
3083 while ( begPos < endPos) {
3084 str = GetWord( myString, begPos, true );
3085 begPos = begPos+ str.Length();
3092 //================================================================================
3094 * \brief Return substring of python command looking like
3095 * ResultValue1 , ResultValue2,... = Obj.Meth() with res index
3096 * \retval const TCollection_AsciiString & - ResultValue with res index substring
3098 //================================================================================
3099 TCollection_AsciiString _pyCommand::GetResultValue(int res)
3102 if ( SkipSpaces( myString, begPos ) && myString.Value( begPos ) == '[' )
3103 ++begPos; // skip [, else the whole list is returned
3104 int endPos = myString.Location( "=", 1, Length() );
3106 while ( begPos < endPos) {
3107 _AString result = GetWord( myString, begPos, true );
3108 begPos = begPos + result.Length();
3111 result.RemoveAll('[');
3112 result.RemoveAll(']');
3118 return theEmptyString;
3121 //================================================================================
3123 * \brief Return substring of python command looking like ResVal = Object.Meth()
3124 * \retval const TCollection_AsciiString & - Object substring
3126 //================================================================================
3128 const TCollection_AsciiString & _pyCommand::GetObject()
3130 if ( GetBegPos( OBJECT_IND ) == UNKNOWN )
3133 int begPos = GetBegPos( RESULT_IND ) + myRes.Length();
3135 begPos = myString.Location( "=", 1, Length() ) + 1;
3136 // is '=' in the string argument (for example, name) or not
3137 int nb1 = 0; // number of ' character at the left of =
3138 int nb2 = 0; // number of " character at the left of =
3139 for ( int i = 1; i < begPos-1; i++ ) {
3140 if ( myString.Value( i )=='\'' )
3142 else if ( myString.Value( i )=='"' )
3145 // if number of ' or " is not divisible by 2,
3146 // then get an object at the start of the command
3147 if ( nb1 % 2 != 0 || nb2 % 2 != 0 )
3150 myObj = GetWord( myString, begPos, true );
3151 // check if object is complex,
3152 // so far consider case like "smesh.smesh.Method()"
3153 if ( int bracketPos = myString.Location( "(", begPos, Length() )) {
3154 //if ( bracketPos==0 ) bracketPos = Length();
3155 int dotPos = begPos+myObj.Length();
3156 while ( dotPos+1 < bracketPos ) {
3157 if ( int pos = myString.Location( ".", dotPos+1, bracketPos ))
3162 if ( dotPos > begPos+myObj.Length() )
3163 myObj = myString.SubString( begPos, dotPos-1 );
3165 // 1st word after '=' is an object
3166 // else // no method -> no object
3172 SetBegPos( OBJECT_IND, begPos );
3178 //================================================================================
3180 * \brief Return substring of python command looking like ResVal = Obj.Method()
3181 * \retval const TCollection_AsciiString & - Method substring
3183 //================================================================================
3185 const TCollection_AsciiString & _pyCommand::GetMethod()
3187 if ( GetBegPos( METHOD_IND ) == UNKNOWN )
3190 int begPos = GetBegPos( OBJECT_IND ) + myObj.Length();
3191 bool forward = true;
3193 begPos = myString.Location( "(", 1, Length() ) - 1;
3197 myMeth = GetWord( myString, begPos, forward );
3198 SetBegPos( METHOD_IND, begPos );
3204 //================================================================================
3206 * \brief Return substring of python command looking like ResVal = Obj.Meth(Arg1,...)
3207 * \retval const TCollection_AsciiString & - Arg<index> substring
3209 //================================================================================
3211 const TCollection_AsciiString & _pyCommand::GetArg( int index )
3213 if ( GetBegPos( ARG1_IND ) == UNKNOWN )
3217 int pos = GetBegPos( METHOD_IND ) + myMeth.Length();
3219 pos = myString.Location( "(", 1, Length() );
3223 // we are at or before '(', skip it if present
3225 while ( pos <= Length() && myString.Value( pos ) != '(' ) ++pos;
3226 if ( pos > Length() )
3230 SetBegPos( ARG1_IND, 0 ); // even no '('
3231 return theEmptyString;
3235 list< TCollection_AsciiString > separatorStack( 1, ",)");
3236 bool ignoreNesting = false;
3238 while ( pos <= Length() )
3240 const char chr = myString.Value( pos );
3242 if ( separatorStack.back().Location( chr, 1, separatorStack.back().Length()))
3244 if ( separatorStack.size() == 1 ) // a comma dividing args or a terminal ')' found
3246 while ( pos-1 >= prevPos && isspace( myString.Value( prevPos )))
3248 TCollection_AsciiString arg;
3249 if ( pos-1 >= prevPos ) {
3250 arg = myString.SubString( prevPos, pos-1 );
3251 arg.RightAdjust(); // remove spaces
3254 if ( !arg.IsEmpty() || chr == ',' )
3256 SetBegPos( ARG1_IND + myArgs.Length(), prevPos );
3257 myArgs.Append( arg );
3263 else // end of nesting args found
3265 separatorStack.pop_back();
3266 ignoreNesting = false;
3269 else if ( !ignoreNesting )
3272 case '(' : separatorStack.push_back(")"); break;
3273 case '[' : separatorStack.push_back("]"); break;
3274 case '\'': separatorStack.push_back("'"); ignoreNesting=true; break;
3275 case '"' : separatorStack.push_back("\""); ignoreNesting=true; break;
3282 if ( myArgs.Length() < index )
3283 return theEmptyString;
3284 return myArgs( index );
3287 //================================================================================
3289 * \brief Check if char is a word part
3290 * \param c - The character to check
3291 * \retval bool - The check result
3293 //================================================================================
3295 static inline bool isWord(const char c, const bool dotIsWord)
3298 !isspace(c) && c != ',' && c != '=' && c != ')' && c != '(' && ( dotIsWord || c != '.');
3301 //================================================================================
3303 * \brief Looks for a word in the string and returns word's beginning
3304 * \param theString - The input string
3305 * \param theStartPos - The position to start the search, returning word's beginning
3306 * \param theForward - The search direction
3307 * \retval TCollection_AsciiString - The found word
3309 //================================================================================
3311 TCollection_AsciiString _pyCommand::GetWord( const _AString & theString,
3313 const bool theForward,
3314 const bool dotIsWord )
3316 int beg = theStartPos, end = theStartPos;
3317 theStartPos = EMPTY;
3318 if ( beg < 1 || beg > theString.Length() )
3319 return theEmptyString;
3321 if ( theForward ) { // search forward
3323 while ( beg <= theString.Length() && !isWord( theString.Value( beg ), dotIsWord))
3325 if ( beg > theString.Length() )
3326 return theEmptyString; // no word found
3329 char begChar = theString.Value( beg );
3330 if ( begChar == '"' || begChar == '\'' || begChar == '[') {
3331 char endChar = ( begChar == '[' ) ? ']' : begChar;
3332 // end is at the corresponding quoting mark or bracket
3333 while ( end < theString.Length() &&
3334 ( theString.Value( end ) != endChar || theString.Value( end-1 ) == '\\'))
3338 while ( end <= theString.Length() && isWord( theString.Value( end ), dotIsWord))
3343 else { // search backward
3345 while ( end > 0 && !isWord( theString.Value( end ), dotIsWord))
3348 return theEmptyString; // no word found
3350 char endChar = theString.Value( end );
3351 if ( endChar == '"' || endChar == '\'' || endChar == ']') {
3352 char begChar = ( endChar == ']' ) ? '[' : endChar;
3353 // beg is at the corresponding quoting mark
3355 ( theString.Value( beg ) != begChar || theString.Value( beg-1 ) == '\\'))
3359 while ( beg > 0 && isWord( theString.Value( beg ), dotIsWord))
3365 //cout << theString << " ---- " << beg << " - " << end << endl;
3366 return theString.SubString( beg, end );
3369 //================================================================================
3371 * \brief Returns true if the string looks like a study entry
3373 //================================================================================
3375 bool _pyCommand::IsStudyEntry( const TCollection_AsciiString& str )
3377 if ( str.Length() < 5 ) return false;
3379 int nbColons = 0, isColon;
3380 for ( int i = 1; i <= str.Length(); ++i )
3382 char c = str.Value(i);
3383 if (!( isColon = (c == ':')) && ( c < '0' || c > '9' ))
3385 nbColons += isColon;
3387 return nbColons > 2 && str.Length()-nbColons > 2;
3390 //================================================================================
3392 * \brief Finds entries in a sting
3394 //================================================================================
3396 std::list< _pyID > _pyCommand::GetStudyEntries( const TCollection_AsciiString& str )
3398 std::list< _pyID > resList;
3400 while ( ++pos <= str.Length() )
3402 if ( !isdigit( str.Value( pos ))) continue;
3403 if ( pos != 1 && ( isalpha( str.Value( pos-1 ) || str.Value( pos-1 ) == ':'))) continue;
3406 while ( ++end <= str.Length() && ( isdigit( str.Value( end )) || str.Value( end ) == ':' ));
3407 _pyID entry = str.SubString( pos, end-1 );
3409 if ( IsStudyEntry( entry ))
3410 resList.push_back( entry );
3415 //================================================================================
3417 * \brief Look for position where not space char is
3418 * \param theString - The string
3419 * \param thePos - The position to search from and which returns result
3420 * \retval bool - false if there are only space after thePos in theString
3422 //================================================================================
3424 bool _pyCommand::SkipSpaces( const TCollection_AsciiString & theString, int & thePos )
3426 if ( thePos < 1 || thePos > theString.Length() )
3429 while ( thePos <= theString.Length() && isspace( theString.Value( thePos )))
3432 return thePos <= theString.Length();
3435 //================================================================================
3437 * \brief Modify a part of the command
3438 * \param thePartIndex - The index of the part
3439 * \param thePart - The new part string
3440 * \param theOldPart - The old part
3442 //================================================================================
3444 void _pyCommand::SetPart(int thePartIndex, const TCollection_AsciiString& thePart,
3445 TCollection_AsciiString& theOldPart)
3447 int pos = GetBegPos( thePartIndex );
3448 if ( pos <= Length() && theOldPart != thePart)
3450 TCollection_AsciiString seperator;
3452 pos = GetBegPos( thePartIndex + 1 );
3453 if ( pos < 1 ) return;
3454 switch ( thePartIndex ) {
3455 case RESULT_IND: seperator = " = "; break;
3456 case OBJECT_IND: seperator = "."; break;
3457 case METHOD_IND: seperator = "()"; break;
3461 myString.Remove( pos, theOldPart.Length() );
3462 if ( !seperator.IsEmpty() )
3463 myString.Insert( pos , seperator );
3464 myString.Insert( pos, thePart );
3465 // update starting positions of the following parts
3466 int posDelta = thePart.Length() + seperator.Length() - theOldPart.Length();
3467 for ( int i = thePartIndex + 1; i <= myBegPos.Length(); ++i ) {
3468 if ( myBegPos( i ) > 0 )
3469 myBegPos( i ) += posDelta;
3471 theOldPart = thePart;
3475 //================================================================================
3477 * \brief Set agrument
3478 * \param index - The argument index, it counts from 1
3479 * \param theArg - The argument string
3481 //================================================================================
3483 void _pyCommand::SetArg( int index, const TCollection_AsciiString& theArg)
3486 int argInd = ARG1_IND + index - 1;
3487 int pos = GetBegPos( argInd );
3488 if ( pos < 1 ) // no index-th arg exist, append inexistent args
3490 // find a closing parenthesis
3491 if ( GetNbArgs() != 0 && index <= GetNbArgs() ) {
3492 int lastArgInd = GetNbArgs();
3493 pos = GetBegPos( ARG1_IND + lastArgInd - 1 ) + GetArg( lastArgInd ).Length();
3494 while ( pos > 0 && pos <= Length() && myString.Value( pos ) != ')' )
3499 while ( pos > 0 && myString.Value( pos ) != ')' )
3502 if ( pos < 1 || myString.Value( pos ) != ')' ) { // no parentheses at all
3506 while ( myArgs.Length() < index ) {
3507 if ( myArgs.Length() )
3508 myString.Insert( pos++, "," );
3509 myArgs.Append("None");
3510 myString.Insert( pos, myArgs.Last() );
3511 SetBegPos( ARG1_IND + myArgs.Length() - 1, pos );
3512 pos += myArgs.Last().Length();
3515 SetPart( argInd, theArg, myArgs( index ));
3518 //================================================================================
3520 * \brief Empty arg list
3522 //================================================================================
3524 void _pyCommand::RemoveArgs()
3526 if ( int pos = myString.Location( '(', 1, Length() ))
3527 myString.Trunc( pos );
3530 if ( myBegPos.Length() >= ARG1_IND )
3531 myBegPos.Remove( ARG1_IND, myBegPos.Length() );
3534 //================================================================================
3536 * \brief Comment a python command
3538 //================================================================================
3540 void _pyCommand::Comment()
3542 if ( IsEmpty() ) return;
3545 while ( i <= Length() && isspace( myString.Value(i) )) ++i;
3546 if ( i <= Length() )
3548 myString.Insert( i, "#" );
3549 for ( int iPart = 0; iPart < myBegPos.Length(); ++iPart )
3551 int begPos = GetBegPos( iPart );
3552 if ( begPos != UNKNOWN )
3553 SetBegPos( iPart, begPos + 1 );
3558 //================================================================================
3560 * \brief Set dependent commands after this one
3562 //================================================================================
3564 bool _pyCommand::SetDependentCmdsAfter() const
3566 bool orderChanged = false;
3567 list< Handle(_pyCommand)>::const_reverse_iterator cmd = myDependentCmds.rbegin();
3568 for ( ; cmd != myDependentCmds.rend(); ++cmd ) {
3569 if ( (*cmd)->GetOrderNb() < GetOrderNb() ) {
3570 orderChanged = true;
3571 theGen->SetCommandAfter( *cmd, this );
3572 (*cmd)->SetDependentCmdsAfter();
3575 return orderChanged;
3577 //================================================================================
3579 * \brief Insert accessor method after theObjectID
3580 * \param theObjectID - id of the accessed object
3581 * \param theAcsMethod - name of the method giving access to the object
3582 * \retval bool - false if theObjectID is not found in the command string
3584 //================================================================================
3586 bool _pyCommand::AddAccessorMethod( _pyID theObjectID, const char* theAcsMethod )
3588 if ( !theAcsMethod )
3590 // start object search from the object, i.e. ignore result
3592 int beg = GetBegPos( OBJECT_IND );
3593 if ( beg < 1 || beg > Length() )
3596 while (( beg = myString.Location( theObjectID, beg, Length() )))
3598 // check that theObjectID is not just a part of a longer ID
3599 int afterEnd = beg + theObjectID.Length();
3600 Standard_Character c = myString.Value( afterEnd );
3601 if ( !isalnum( c ) && c != ':' ) {
3602 // check if accessor method already present
3604 myString.Location( (char*) theAcsMethod, afterEnd, Length() ) != afterEnd+1) {
3606 int oldLen = Length();
3607 myString.Insert( afterEnd, (char*) theAcsMethod );
3608 myString.Insert( afterEnd, "." );
3609 // update starting positions of the parts following the modified one
3610 int posDelta = Length() - oldLen;
3611 for ( int i = 1; i <= myBegPos.Length(); ++i ) {
3612 if ( myBegPos( i ) > afterEnd )
3613 myBegPos( i ) += posDelta;
3618 beg = afterEnd; // is a part - next search
3623 //================================================================================
3625 * \brief Creates pyObject
3627 //================================================================================
3629 _pyObject::_pyObject(const Handle(_pyCommand)& theCreationCmd, const _pyID& theID)
3630 : myID(theID), myCreationCmd(theCreationCmd), myIsPublished(false)
3635 //================================================================================
3637 * \brief Set up myID and myIsPublished
3639 //================================================================================
3641 void _pyObject::setID(const _pyID& theID)
3644 myIsPublished = !theGen->IsNotPublished( GetID() );
3647 //================================================================================
3649 * \brief Clear myCreationCmd and myProcessedCmds
3651 //================================================================================
3653 void _pyObject::ClearCommands()
3658 if ( !myCreationCmd.IsNull() )
3659 myCreationCmd->Clear();
3661 list< Handle(_pyCommand) >::iterator cmd = myProcessedCmds.begin();
3662 for ( ; cmd != myProcessedCmds.end(); ++cmd )
3666 //================================================================================
3668 * \brief Return method name giving access to an interaface object wrapped by python class
3669 * \retval const char* - method name
3671 //================================================================================
3673 const char* _pyObject::AccessorMethod() const
3677 //================================================================================
3679 * \brief Return ID of a father
3681 //================================================================================
3683 _pyID _pyObject::FatherID(const _pyID & childID)
3685 int colPos = childID.SearchFromEnd(':');
3687 return childID.SubString( 1, colPos-1 );
3691 //================================================================================
3693 * \brief SelfEraser erases creation command if no more it's commands invoked
3695 //================================================================================
3697 void _pySelfEraser::Flush()
3699 if ( GetNbCalls() == 0 )
3700 GetCreationCmd()->Clear();
3703 //================================================================================
3705 * \brief _pySubMesh constructor
3707 //================================================================================
3709 _pySubMesh::_pySubMesh(const Handle(_pyCommand)& theCreationCmd):
3710 _pyObject(theCreationCmd)
3712 myMesh = ObjectToMesh( theGen->FindObject( theCreationCmd->GetObject() ));
3715 //================================================================================
3717 * \brief Return true if a sub-mesh can be used as argument of the given method
3719 //================================================================================
3721 bool _pySubMesh::CanBeArgOfMethod(const _AString& theMethodName)
3723 // names of all methods where a sub-mesh can be used as argument
3724 static TStringSet methods;
3725 if ( methods.empty() ) {
3726 const char * names[] = {
3727 // methods of SMESH_Gen
3729 // methods of SMESH_Group
3731 // methods of SMESH_Measurements
3733 // methods of SMESH_Mesh
3734 "ExportPartToMED","ExportCGNS","ExportPartToDAT","ExportPartToUNV","ExportPartToSTL",
3736 // methods of SMESH_MeshEditor
3737 "ReorientObject","Reorient2D","TriToQuadObject","QuadToTriObject","SplitQuadObject",
3738 "SplitVolumesIntoTetra","SmoothObject","SmoothParametricObject","ConvertFromQuadraticObject",
3739 "RotationSweepObject","RotationSweepObjectMakeGroups","RotationSweepObject1D",
3740 "RotationSweepObject1DMakeGroups","RotationSweepObject2D","RotationSweepObject2DMakeGroups",
3741 "ExtrusionSweepObject","ExtrusionSweepObjectMakeGroups","ExtrusionSweepObject0D",
3742 "ExtrusionSweepObject0DMakeGroups","ExtrusionSweepObject1D","ExtrusionSweepObject2D",
3743 "ExtrusionSweepObject1DMakeGroups","ExtrusionSweepObject2DMakeGroups",
3744 "ExtrusionAlongPathObjX","ExtrusionAlongPathObject","ExtrusionAlongPathObjectMakeGroups",
3745 "ExtrusionAlongPathObject1D","ExtrusionAlongPathObject1DMakeGroups",
3746 "ExtrusionAlongPathObject2D","ExtrusionAlongPathObject2DMakeGroups","MirrorObject",
3747 "MirrorObjectMakeGroups","MirrorObjectMakeMesh","TranslateObject","Scale",
3748 "TranslateObjectMakeGroups","TranslateObjectMakeMesh","ScaleMakeGroups","ScaleMakeMesh",
3749 "RotateObject","RotateObjectMakeGroups","RotateObjectMakeMesh","FindCoincidentNodesOnPart",
3750 "FindCoincidentNodesOnPartBut","FindEqualElements","FindAmongElementsByPoint",
3752 "" }; // <- mark of end
3753 methods.Insert( names );
3755 return methods.Contains( theMethodName );
3758 //================================================================================
3760 * \brief count invoked commands
3762 //================================================================================
3764 void _pySubMesh::Process( const Handle(_pyCommand)& theCommand )
3766 _pyObject::Process(theCommand); // count calls of Process()
3767 GetCreationCmd()->AddDependantCmd( theCommand );
3770 //================================================================================
3772 * \brief Move creation command depending on invoked commands
3774 //================================================================================
3776 void _pySubMesh::Flush()
3778 if ( GetNbCalls() == 0 ) // move to the end of all commands
3779 theGen->GetLastCommand()->AddDependantCmd( GetCreationCmd() );
3780 else if ( !myCreator.IsNull() )
3781 // move to be just after creator
3782 myCreator->GetCreationCmd()->AddDependantCmd( GetCreationCmd() );
3785 //================================================================================
3787 * \brief Creates _pyGroup
3789 //================================================================================
3791 _pyGroup::_pyGroup(const Handle(_pyCommand)& theCreationCmd, const _pyID & id)
3792 :_pySubMesh(theCreationCmd)
3794 if ( !id.IsEmpty() )
3797 myCanClearCreationCmd = true;
3799 const _AString& method = theCreationCmd->GetMethod();
3800 if ( method == "CreateGroup" ) // CreateGroup() --> CreateEmptyGroup()
3802 theCreationCmd->SetMethod( "CreateEmptyGroup" );
3804 // ----------------------------------------------------------------------
3805 else if ( method == "CreateGroupFromGEOM" ) // (type, name, grp)
3807 _pyID geom = theCreationCmd->GetArg( 3 );
3808 // VSR 24/12/2010. PAL21106: always use GroupOnGeom() function on dump
3809 // next if(){...} section is commented
3810 //if ( sameGroupType( geom, theCreationCmd->GetArg( 1 )) ) { // --> Group(geom)
3811 // theCreationCmd->SetMethod( "Group" );
3812 // theCreationCmd->RemoveArgs();
3813 // theCreationCmd->SetArg( 1, geom );
3816 // ------------------------->>>>> GroupOnGeom( geom, name, typ )
3817 _pyID type = theCreationCmd->GetArg( 1 );
3818 _pyID name = theCreationCmd->GetArg( 2 );
3819 theCreationCmd->SetMethod( "GroupOnGeom" );
3820 theCreationCmd->RemoveArgs();
3821 theCreationCmd->SetArg( 1, geom );
3822 theCreationCmd->SetArg( 2, name );
3823 theCreationCmd->SetArg( 3, type );
3826 else if ( method == "CreateGroupFromFilter" )
3828 // -> GroupOnFilter(typ, name, aFilter0x4743dc0 -> aFilter_1)
3829 theCreationCmd->SetMethod( "GroupOnFilter" );
3831 _pyID filterID = theCreationCmd->GetArg(3);
3832 Handle(_pyFilter) filter = Handle(_pyFilter)::DownCast( theGen->FindObject( filterID ));
3833 if ( !filter.IsNull())
3835 if ( !filter->GetNewID().IsEmpty() )
3836 theCreationCmd->SetArg( 3, filter->GetNewID() );
3837 filter->AddUser( this );
3843 // theCreationCmd does something else apart from creation of this group
3844 // and thus it can't be cleared if this group is removed
3845 myCanClearCreationCmd = false;
3849 //================================================================================
3851 * \brief To convert creation of a group by filter
3853 //================================================================================
3855 void _pyGroup::Process( const Handle(_pyCommand)& theCommand)
3857 // Convert the following set of commands into mesh.MakeGroupByFilter(groupName, theFilter)
3858 // group = mesh.CreateEmptyGroup( elemType, groupName )
3859 // aFilter.SetMesh(mesh)
3860 // nbAdd = group.AddFrom( aFilter )
3861 Handle(_pyFilter) filter;
3862 if ( theCommand->GetMethod() == "AddFrom" )
3864 _pyID idSource = theCommand->GetArg(1);
3865 // check if idSource is a filter
3866 filter = Handle(_pyFilter)::DownCast( theGen->FindObject( idSource ));
3867 if ( !filter.IsNull() )
3869 // find aFilter.SetMesh(mesh) to clear it, it should be just before theCommand
3870 list< Handle(_pyCommand) >::reverse_iterator cmdIt = theGen->GetCommands().rbegin();
3871 while ( *cmdIt != theCommand ) ++cmdIt;
3872 while ( (*cmdIt)->GetOrderNb() != 1 )
3874 const Handle(_pyCommand)& setMeshCmd = *(++cmdIt);
3875 if ((setMeshCmd->GetObject() == idSource ||
3876 setMeshCmd->GetObject() == filter->GetNewID() )
3878 setMeshCmd->GetMethod() == "SetMesh")
3880 setMeshCmd->Clear();
3884 // replace 3 commands by one
3885 theCommand->Clear();
3886 const Handle(_pyCommand)& makeGroupCmd = GetCreationCmd();
3887 TCollection_AsciiString name = makeGroupCmd->GetArg( 2 );
3888 if ( !filter->GetNewID().IsEmpty() )
3889 idSource = filter->GetNewID();
3890 makeGroupCmd->SetMethod( "MakeGroupByFilter" );
3891 makeGroupCmd->SetArg( 1, name );
3892 makeGroupCmd->SetArg( 2, idSource );
3895 else if ( theCommand->GetMethod() == "SetFilter" )
3897 // set new name of a filter or clear the command if the same filter is set
3898 _pyID filterID = theCommand->GetArg(1);
3899 filter = Handle(_pyFilter)::DownCast( theGen->FindObject( filterID ));
3900 if ( !myFilter.IsNull() && filter == myFilter )
3901 theCommand->Clear();
3902 else if ( !filter.IsNull() && !filter->GetNewID().IsEmpty() )
3903 theCommand->SetArg( 1, filter->GetNewID() );
3906 else if ( theCommand->GetMethod() == "GetFilter" )
3908 // GetFilter() returns a filter with other ID, make myFilter process
3909 // calls of the returned filter
3910 if ( !myFilter.IsNull() )
3912 theGen->SetProxyObject( theCommand->GetResultValue(), myFilter );
3913 theCommand->Clear();
3917 if ( !filter.IsNull() )
3918 filter->AddUser( this );
3920 theGen->AddMeshAccessorMethod( theCommand );
3923 //================================================================================
3925 * \brief Prevent clearing "DoubleNode...() command if a group created by it is removed
3929 //================================================================================
3931 void _pyGroup::Flush()
3933 if ( !theGen->IsToKeepAllCommands() &&
3934 myCreationCmd && !myCanClearCreationCmd )
3936 myCreationCmd.Nullify(); // this way myCreationCmd won't be cleared
3940 //================================================================================
3942 * \brief Constructor of _pyFilter
3944 //================================================================================
3946 _pyFilter::_pyFilter(const Handle(_pyCommand)& theCreationCmd, const _pyID& newID/*=""*/)
3947 :_pyObject(theCreationCmd), myNewID( newID )
3951 //================================================================================
3953 * \brief To convert creation of a filter by criteria and
3954 * to replace an old name by a new one
3956 //================================================================================
3958 void _pyFilter::Process( const Handle(_pyCommand)& theCommand)
3960 if ( theCommand->GetObject() == GetID() )
3961 _pyObject::Process(theCommand); // count commands
3963 if ( !myNewID.IsEmpty() )
3964 theCommand->SetObject( myNewID );
3966 // Convert the following set of commands into smesh.GetFilterFromCriteria(criteria)
3967 // aFilter0x2aaab0487080 = aFilterManager.CreateFilter()
3968 // aFilter0x2aaab0487080.SetCriteria(aCriteria)
3969 if ( GetNbCalls() == 1 && // none method was called before this SetCriteria() call
3970 theCommand->GetMethod() == "SetCriteria")
3972 // aFilter.SetCriteria(aCriteria) ->
3973 // aFilter = smesh.GetFilterFromCriteria(criteria)
3974 if ( myNewID.IsEmpty() )
3975 theCommand->SetResultValue( GetID() );
3977 theCommand->SetResultValue( myNewID );
3978 theCommand->SetObject( SMESH_2smeshpy::GenName() );
3979 theCommand->SetMethod( "GetFilterFromCriteria" );
3981 // Clear aFilterManager.CreateFilter()
3982 GetCreationCmd()->Clear();
3984 else if ( theCommand->GetMethod() == "SetMesh" )
3986 if ( myMesh == theCommand->GetArg( 1 ))
3987 theCommand->Clear();
3989 myMesh = theCommand->GetArg( 1 );
3990 theGen->AddMeshAccessorMethod( theCommand );
3994 //================================================================================
3996 * \brief Set new filter name to the creation command
3998 //================================================================================
4000 void _pyFilter::Flush()
4002 if ( !myNewID.IsEmpty() && !GetCreationCmd()->IsEmpty() )
4003 GetCreationCmd()->SetResultValue( myNewID );
4006 //================================================================================
4008 * \brief Return true if all my users can be cleared
4010 //================================================================================
4012 bool _pyFilter::CanClear()
4014 list< Handle(_pyObject) >::iterator obj = myUsers.begin();
4015 for ( ; obj != myUsers.end(); ++obj )
4016 if ( !(*obj)->CanClear() )
4022 //================================================================================
4024 * \brief Reads _pyHypothesis'es from resource files of mesher Plugins
4026 //================================================================================
4028 _pyHypothesisReader::_pyHypothesisReader()
4031 vector< string > xmlPaths = SMESH_Gen::GetPluginXMLPaths();
4032 LDOMParser xmlParser;
4033 for ( size_t i = 0; i < xmlPaths.size(); ++i )
4035 bool error = xmlParser.parse( xmlPaths[i].c_str() );
4039 INFOS( xmlParser.GetError(data) );
4042 // <algorithm type="Regular_1D"
4043 // label-id="Wire discretisation"
4046 // <algo>Regular_1D=Segment()</algo>
4047 // <hypo>LocalLength=LocalLength(SetLength(1),,SetPrecision(1))</hypo>
4049 LDOM_Document xmlDoc = xmlParser.getDocument();
4050 LDOM_NodeList algoNodeList = xmlDoc.getElementsByTagName( "algorithm" );
4051 for ( int i = 0; i < algoNodeList.getLength(); ++i )
4053 LDOM_Node algoNode = algoNodeList.item( i );
4054 LDOM_Element& algoElem = (LDOM_Element&) algoNode;
4055 LDOM_NodeList pyAlgoNodeList = algoElem.getElementsByTagName( "algo" );
4056 if ( pyAlgoNodeList.getLength() < 1 ) continue;
4058 _AString text, algoType, method, arg;
4059 for ( int iA = 0; iA < pyAlgoNodeList.getLength(); ++iA )
4061 LDOM_Node pyAlgoNode = pyAlgoNodeList.item( iA );
4062 LDOM_Node textNode = pyAlgoNode.getFirstChild();
4063 text = textNode.getNodeValue();
4064 Handle(_pyCommand) algoCmd = new _pyCommand( text );
4065 algoType = algoCmd->GetResultValue();
4066 method = algoCmd->GetMethod();
4067 arg = algoCmd->GetArg(1);
4068 if ( !algoType.IsEmpty() && !method.IsEmpty() )
4070 Handle(_pyAlgorithm) algo = new _pyAlgorithm( algoCmd );
4071 algo->SetConvMethodAndType( method, algoType );
4072 if ( !arg.IsEmpty() )
4073 algo->setCreationArg( 1, arg );
4075 myType2Hyp[ algoType ] = algo;
4079 if ( algoType.IsEmpty() ) continue;
4081 LDOM_NodeList pyHypoNodeList = algoElem.getElementsByTagName( "hypo" );
4083 Handle( _pyHypothesis ) hyp;
4084 for ( int iH = 0; iH < pyHypoNodeList.getLength(); ++iH )
4086 LDOM_Node pyHypoNode = pyHypoNodeList.item( iH );
4087 LDOM_Node textNode = pyHypoNode.getFirstChild();
4088 text = textNode.getNodeValue();
4089 Handle(_pyCommand) hypoCmd = new _pyCommand( text );
4090 hypType = hypoCmd->GetResultValue();
4091 method = hypoCmd->GetMethod();
4092 if ( !hypType.IsEmpty() && !method.IsEmpty() )
4094 map<_AString, Handle(_pyHypothesis)>::iterator type2hyp = myType2Hyp.find( hypType );
4095 if ( type2hyp == myType2Hyp.end() )
4096 hyp = new _pyHypothesis( hypoCmd );
4098 hyp = type2hyp->second;
4099 hyp->SetConvMethodAndType( method, algoType );
4100 for ( int iArg = 1; iArg <= hypoCmd->GetNbArgs(); ++iArg )
4102 _pyCommand argCmd( hypoCmd->GetArg( iArg ));
4103 _AString argMethod = argCmd.GetMethod();
4104 _AString argNbText = argCmd.GetArg( 1 );
4105 if ( argMethod.IsEmpty() && !argCmd.IsEmpty() )
4106 hyp->setCreationArg( 1, argCmd.GetString() ); // e.g. Parameters(smesh.SIMPLE)
4108 hyp->AddArgMethod( argMethod,
4109 argNbText.IsIntegerValue() ? argNbText.IntegerValue() : 1 );
4111 myType2Hyp[ hypType ] = hyp;
4115 // <hypothesis type="BLSURF_Parameters"
4119 // <accumulative-methods>
4120 // SetEnforcedVertex,
4121 // SetEnforcedVertexNamed
4122 // </accumulative-methods>
4126 LDOM_NodeList hypNodeList = xmlDoc.getElementsByTagName( "hypothesis" );
4127 for ( int i = 0; i < hypNodeList.getLength(); ++i )
4129 LDOM_Node hypNode = hypNodeList.item( i );
4130 LDOM_Element& hypElem = (LDOM_Element&) hypNode;
4131 _AString hypType = hypElem.getAttribute("type");
4132 LDOM_NodeList methNodeList = hypElem.getElementsByTagName( "accumulative-methods" );
4133 if ( methNodeList.getLength() != 1 || hypType.IsEmpty() ) continue;
4135 map<_AString, Handle(_pyHypothesis)>::const_iterator type2hyp = myType2Hyp.find( hypType );
4136 if ( type2hyp == myType2Hyp.end() ) continue;
4138 LDOM_Node methNode = methNodeList.item( 0 );
4139 LDOM_Node textNode = methNode.getFirstChild();
4140 _AString text = textNode.getNodeValue();
4144 method = _pyCommand::GetWord( text, pos, /*forward= */true );
4145 pos += method.Length();
4146 type2hyp->second->AddAccumulativeMethod( method );
4148 while ( !method.IsEmpty() );
4151 } // loop on xmlPaths
4154 //================================================================================
4156 * \brief Returns a new hypothesis initialized according to the read information
4158 //================================================================================
4160 Handle(_pyHypothesis)
4161 _pyHypothesisReader::GetHypothesis(const _AString& hypType,
4162 const Handle(_pyCommand)& creationCmd) const
4164 Handle(_pyHypothesis) resHyp, sampleHyp;
4166 map<_AString, Handle(_pyHypothesis)>::const_iterator type2hyp = myType2Hyp.find( hypType );
4167 if ( type2hyp != myType2Hyp.end() )
4168 sampleHyp = type2hyp->second;
4170 if ( sampleHyp.IsNull() )
4172 resHyp = new _pyHypothesis(creationCmd);
4176 if ( sampleHyp->IsAlgo() )
4177 resHyp = new _pyAlgorithm( creationCmd );
4179 resHyp = new _pyHypothesis(creationCmd);
4180 resHyp->Assign( sampleHyp, _pyID() );