1 // Copyright (C) 2007-2013 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 "SMESH_PythonDump.hxx"
30 #include "SMESH_NoteBook.hxx"
31 #include "SMESH_Filter_i.hxx"
33 #include <SALOMEDS_wrap.hxx>
34 #include <utilities.h>
36 #include <Resource_DataMapOfAsciiStringAsciiString.hxx>
37 #include <Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString.hxx>
39 #include "SMESH_Gen_i.hxx"
40 /* SALOME headers that include CORBA headers that include windows.h
41 * that defines GetObject symbol as GetObjectA should stand before SALOME headers
42 * that declare methods named GetObject - to apply the same rules of GetObject renaming
43 * and thus to avoid mess with GetObject symbol on Windows */
45 #include <LDOMParser.hxx>
54 IMPLEMENT_STANDARD_HANDLE (_pyObject ,Standard_Transient);
55 IMPLEMENT_STANDARD_HANDLE (_pyCommand ,Standard_Transient);
56 IMPLEMENT_STANDARD_HANDLE (_pyHypothesisReader,Standard_Transient);
57 IMPLEMENT_STANDARD_HANDLE (_pyGen ,_pyObject);
58 IMPLEMENT_STANDARD_HANDLE (_pyMesh ,_pyObject);
59 IMPLEMENT_STANDARD_HANDLE (_pySubMesh ,_pyObject);
60 IMPLEMENT_STANDARD_HANDLE (_pyMeshEditor ,_pyObject);
61 IMPLEMENT_STANDARD_HANDLE (_pyHypothesis ,_pyObject);
62 IMPLEMENT_STANDARD_HANDLE (_pySelfEraser ,_pyObject);
63 IMPLEMENT_STANDARD_HANDLE (_pyGroup ,_pyObject);
64 IMPLEMENT_STANDARD_HANDLE (_pyFilter ,_pyObject);
65 IMPLEMENT_STANDARD_HANDLE (_pyAlgorithm ,_pyHypothesis);
66 IMPLEMENT_STANDARD_HANDLE (_pyComplexParamHypo,_pyHypothesis);
67 IMPLEMENT_STANDARD_HANDLE (_pyNumberOfSegmentsHyp,_pyHypothesis);
69 IMPLEMENT_STANDARD_RTTIEXT(_pyObject ,Standard_Transient);
70 IMPLEMENT_STANDARD_RTTIEXT(_pyCommand ,Standard_Transient);
71 IMPLEMENT_STANDARD_RTTIEXT(_pyHypothesisReader,Standard_Transient);
72 IMPLEMENT_STANDARD_RTTIEXT(_pyGen ,_pyObject);
73 IMPLEMENT_STANDARD_RTTIEXT(_pyMesh ,_pyObject);
74 IMPLEMENT_STANDARD_RTTIEXT(_pySubMesh ,_pyObject);
75 IMPLEMENT_STANDARD_RTTIEXT(_pyMeshEditor ,_pyObject);
76 IMPLEMENT_STANDARD_RTTIEXT(_pyHypothesis ,_pyObject);
77 IMPLEMENT_STANDARD_RTTIEXT(_pySelfEraser ,_pyObject);
78 IMPLEMENT_STANDARD_RTTIEXT(_pyGroup ,_pyObject);
79 IMPLEMENT_STANDARD_RTTIEXT(_pyFilter ,_pyObject);
80 IMPLEMENT_STANDARD_RTTIEXT(_pyAlgorithm ,_pyHypothesis);
81 IMPLEMENT_STANDARD_RTTIEXT(_pyComplexParamHypo,_pyHypothesis);
82 IMPLEMENT_STANDARD_RTTIEXT(_pyNumberOfSegmentsHyp,_pyHypothesis);
83 IMPLEMENT_STANDARD_RTTIEXT(_pyLayerDistributionHypo,_pyHypothesis);
84 IMPLEMENT_STANDARD_RTTIEXT(_pySegmentLengthAroundVertexHyp,_pyHypothesis);
87 using SMESH::TPythonDump;
90 * \brief Container of commands into which the initial script is split.
91 * It also contains data coresponding to SMESH_Gen contents
93 static Handle(_pyGen) theGen;
95 static TCollection_AsciiString theEmptyString;
97 //#define DUMP_CONVERSION
99 #if !defined(_DEBUG_) && defined(DUMP_CONVERSION)
100 #undef DUMP_CONVERSION
106 //================================================================================
108 * \brief Set of TCollection_AsciiString initialized by C array of C strings
110 //================================================================================
112 struct TStringSet: public set<TCollection_AsciiString>
115 * \brief Filling. The last string must be ""
117 void Insert(const char* names[]) {
118 for ( int i = 0; names[i][0] ; ++i )
119 insert( (char*) names[i] );
122 * \brief Check if a string is in
124 bool Contains(const TCollection_AsciiString& name ) {
125 return find( name ) != end();
129 //================================================================================
131 * \brief Map of TCollection_AsciiString initialized by C array of C strings.
132 * Odd items of the C array are map keys, and even items are values
134 //================================================================================
136 struct TStringMap: public map<TCollection_AsciiString,TCollection_AsciiString>
139 * \brief Filling. The last string must be ""
141 void Insert(const char* names_values[]) {
142 for ( int i = 0; names_values[i][0] ; i += 2 )
143 insert( make_pair( (char*) names_values[i], names_values[i+1] ));
146 * \brief Check if a string is in
148 TCollection_AsciiString Value(const TCollection_AsciiString& name ) {
149 map< _AString, _AString >::iterator it = find( name );
150 return it == end() ? "" : it->second;
154 //================================================================================
156 * \brief Returns a mesh by object
158 //================================================================================
160 Handle(_pyMesh) ObjectToMesh( const Handle( _pyObject )& obj )
164 if ( obj->IsKind( STANDARD_TYPE( _pyMesh )))
165 return Handle(_pyMesh)::DownCast( obj );
166 else if ( obj->IsKind( STANDARD_TYPE( _pySubMesh )))
167 return Handle(_pySubMesh)::DownCast( obj )->GetMesh();
168 else if ( obj->IsKind( STANDARD_TYPE( _pyGroup )))
169 return Handle(_pyGroup)::DownCast( obj )->GetMesh();
171 return Handle(_pyMesh)();
174 //================================================================================
176 * \brief Check if objects used as args have been created by previous commands
178 //================================================================================
180 void CheckObjectPresence( const Handle(_pyCommand)& cmd, set<_pyID> & presentObjects)
182 // either comment or erase a command including NotPublishedObjectName()
183 if ( cmd->GetString().Location( TPythonDump::NotPublishedObjectName(), 1, cmd->Length() ))
185 bool isResultPublished = false;
186 for ( int i = 0; i < cmd->GetNbResultValues(); i++ )
188 _pyID objID = cmd->GetResultValue( i+1 );
189 if ( cmd->IsStudyEntry( objID ))
190 isResultPublished = (! theGen->IsNotPublished( objID ));
191 theGen->ObjectCreationRemoved( objID ); // objID.SetName( name ) is not needed
193 if ( isResultPublished )
199 // comment a command having not created args
200 for ( int iArg = cmd->GetNbArgs(); iArg; --iArg )
202 const _pyID& arg = cmd->GetArg( iArg );
203 if ( arg.IsEmpty() || arg.Value( 1 ) == '"' || arg.Value( 1 ) == '\'' )
205 list< _pyID > idList = cmd->GetStudyEntries( arg );
206 list< _pyID >::iterator id = idList.begin();
207 for ( ; id != idList.end(); ++id )
208 if ( !theGen->IsGeomObject( *id ) && !presentObjects.count( *id ))
211 cmd->GetString() += " ### " ;
212 cmd->GetString() += *id + " has not been yet created";
213 for ( int i = 0; i < cmd->GetNbResultValues(); i++ ) {
214 _pyID objID = cmd->GetResultValue( i+1 );
215 theGen->ObjectCreationRemoved( objID ); // objID.SetName( name ) is not needed
220 // comment a command with an Object that was not created in the script
221 const _pyID& obj = cmd->GetObject();
222 if ( !obj.IsEmpty() && cmd->IsStudyEntry( obj ) && !presentObjects.count( obj ))
225 cmd->GetString() += " ### not created Object" ;
226 for ( int i = 0; i < cmd->GetNbResultValues(); i++ ) {
227 _pyID objID = cmd->GetResultValue( i+1 );
228 theGen->ObjectCreationRemoved( objID ); // objID.SetName( name ) is not needed
232 const _pyID& result = cmd->GetResultValue();
233 if ( result.IsEmpty() || result.Value( 1 ) == '"' || result.Value( 1 ) == '\'' )
235 list< _pyID > idList = cmd->GetStudyEntries( result );
236 list< _pyID >::iterator id = idList.begin();
237 for ( ; id != idList.end(); ++id )
238 presentObjects.insert( *id );
241 //================================================================================
243 * \brief Fix SMESH::FunctorType arguments of SMESH::Filter::Criterion()
245 //================================================================================
247 void fixFunctorType( TCollection_AsciiString& Type,
248 TCollection_AsciiString& Compare,
249 TCollection_AsciiString& UnaryOp,
250 TCollection_AsciiString& BinaryOp )
252 // The problem is that dumps of old studies created using filters becomes invalid
253 // when new items are inserted in the enum SMESH::FunctorType since values
254 // of this enum are dumped as integer values.
255 // This function corrects enum values of old studies given as args (Type,Compare,...)
256 // We can find out how to correct them by value of BinaryOp which can have only two
257 // values: FT_Undefined or FT_LogicalNOT.
258 // Hereafter is the history of the enum SMESH::FunctorType since v3.0.0
259 // where PythonDump appeared
260 // v 3.0.0: FT_Undefined == 25
261 // v 3.1.0: FT_Undefined == 26, new items:
263 // v 4.1.2: FT_Undefined == 27, new items:
264 // - FT_BelongToGenSurface = 17
265 // v 5.1.1: FT_Undefined == 32, new items:
266 // - FT_FreeNodes = 10
267 // - FT_FreeFaces = 11
268 // - FT_LinearOrQuadratic = 23
269 // - FT_GroupColor = 24
270 // - FT_ElemGeomType = 25
271 // v 5.1.5: FT_Undefined == 33, new items:
272 // - FT_CoplanarFaces = 26
273 // v 6.2.0: FT_Undefined == 39, new items:
274 // - FT_MaxElementLength2D = 8
275 // - FT_MaxElementLength3D = 9
276 // - FT_BareBorderVolume = 25
277 // - FT_BareBorderFace = 26
278 // - FT_OverConstrainedVolume = 27
279 // - FT_OverConstrainedFace = 28
280 // v 6.5.0: FT_Undefined == 43, new items:
281 // - FT_EqualNodes = 14
282 // - FT_EqualEdges = 15
283 // - FT_EqualFaces = 16
284 // - FT_EqualVolumes = 17
285 // v 6.6.0: FT_Undefined == 44, new items:
286 // - FT_BallDiameter = 37
287 // v 6.7.1: FT_Undefined == 45, new items:
288 // - FT_EntityType = 36
289 // v 7.3.0: FT_Undefined == 46, new items:
290 // - FT_ConnectedElements = 39
292 // It's necessary to continue recording this history and to fill
293 // undef2newItems (see below) accordingly.
295 typedef map< int, vector< int > > TUndef2newItems;
296 static TUndef2newItems undef2newItems;
297 if ( undef2newItems.empty() )
299 undef2newItems[ 26 ].push_back( 7 );
300 undef2newItems[ 27 ].push_back( 17 );
301 { int items[] = { 10, 11, 23, 24, 25 };
302 undef2newItems[ 32 ].assign( items, items+5 ); }
303 undef2newItems[ 33 ].push_back( 26 );
304 { int items[] = { 8, 9, 25, 26, 27, 28 };
305 undef2newItems[ 39 ].assign( items, items+6 ); }
306 { int items[] = { 14, 15, 16, 17 };
307 undef2newItems[ 43 ].assign( items, items+4 ); }
308 undef2newItems[ 44 ].push_back( 37 );
309 undef2newItems[ 45 ].push_back( 36 );
310 undef2newItems[ 46 ].push_back( 39 );
313 int iType = Type.IntegerValue();
314 int iCompare = Compare.IntegerValue();
315 int iUnaryOp = UnaryOp.IntegerValue();
316 int iBinaryOp = BinaryOp.IntegerValue();
318 // find out integer value of FT_Undefined at the moment of dump
319 int oldUndefined = iBinaryOp;
320 if ( iBinaryOp < iUnaryOp ) // BinaryOp was FT_LogicalNOT
323 // apply history to args
324 TUndef2newItems::const_iterator undef_items =
325 undef2newItems.upper_bound( oldUndefined );
326 if ( undef_items != undef2newItems.end() )
328 int* pArg[4] = { &iType, &iCompare, &iUnaryOp, &iBinaryOp };
329 for ( ; undef_items != undef2newItems.end(); ++undef_items )
331 const vector< int > & addedItems = undef_items->second;
332 for ( size_t i = 0; i < addedItems.size(); ++i )
333 for ( int iArg = 0; iArg < 4; ++iArg )
335 int& arg = *pArg[iArg];
336 if ( arg >= addedItems[i] )
340 Type = TCollection_AsciiString( iType );
341 Compare = TCollection_AsciiString( iCompare );
342 UnaryOp = TCollection_AsciiString( iUnaryOp );
343 BinaryOp = TCollection_AsciiString( iBinaryOp );
347 //================================================================================
349 * \brief Replaces "SMESH.PointStruct(x,y,z)" and "SMESH.DirStruct( SMESH.PointStruct(x,y,z))"
350 * arguments of a given command by a list "[x,y,z]" if the list is accesible
353 //================================================================================
355 void StructToList( Handle( _pyCommand)& theCommand )
357 static TStringSet methodsAcceptingList;
358 if ( methodsAcceptingList.empty() ) {
359 const char * methodNames[] = {
360 "GetCriterion","Reorient2D","ExtrusionSweep","ExtrusionSweepMakeGroups0D",
361 "ExtrusionSweepMakeGroups","ExtrusionSweep0D",
362 "AdvancedExtrusion","AdvancedExtrusionMakeGroups",
363 "ExtrusionSweepObject","ExtrusionSweepObject0DMakeGroups",
364 "ExtrusionSweepObjectMakeGroups","ExtrusionSweepObject0D",
365 "ExtrusionSweepObject1D","ExtrusionSweepObject1DMakeGroups",
366 "ExtrusionSweepObject2D","ExtrusionSweepObject2DMakeGroups",
367 "Translate","TranslateMakeGroups","TranslateMakeMesh",
368 "TranslateObject","TranslateObjectMakeGroups", "TranslateObjectMakeMesh"
369 ,"" }; // <- mark of the end
370 methodsAcceptingList.Insert( methodNames );
372 if ( methodsAcceptingList.Contains( theCommand->GetMethod() ))
374 for ( int i = theCommand->GetNbArgs(); i > 0; --i )
376 const _AString & arg = theCommand->GetArg( i );
377 if ( arg.Search( "SMESH.PointStruct" ) == 1 ||
378 arg.Search( "SMESH.DirStruct" ) == 1 )
380 Handle(_pyCommand) workCmd = new _pyCommand( arg );
381 if ( workCmd->GetNbArgs() == 1 ) // SMESH.DirStruct( SMESH.PointStruct(x,y,z))
383 workCmd = new _pyCommand( workCmd->GetArg( 1 ) );
385 if ( workCmd->GetNbArgs() == 3 ) // SMESH.PointStruct(x,y,z)
387 _AString newArg = "[ ";
388 newArg += ( workCmd->GetArg( 1 ) + ", " +
389 workCmd->GetArg( 2 ) + ", " +
390 workCmd->GetArg( 3 ) + " ]");
391 theCommand->SetArg( i, newArg );
397 //================================================================================
399 * \brief Replaces "mesh.GetIDSource([id1,id2])" argument of a given command by
400 * a list "[id1,id2]" if the list is an accesible type of argument.
402 //================================================================================
404 void GetIDSourceToList( Handle( _pyCommand)& theCommand )
406 static TStringSet methodsAcceptingList;
407 if ( methodsAcceptingList.empty() ) {
408 const char * methodNames[] = {
409 "ExportPartToMED","ExportPartToDAT","ExportPartToUNV","ExportPartToSTL",
410 "ExportCGNS","ExportGMF",
411 "Create0DElementsOnAllNodes","Reorient2D","QuadTo4Tri",
412 "ScaleMakeGroups","Scale","ScaleMakeMesh",
413 "FindCoincidentNodesOnPartBut","DoubleElements"
414 ,"" }; // <- mark of the end
415 methodsAcceptingList.Insert( methodNames );
417 if ( methodsAcceptingList.Contains( theCommand->GetMethod() ))
419 for ( int i = theCommand->GetNbArgs(); i > 0; --i )
421 _pyCommand argCmd( theCommand->GetArg( i ));
422 if ( argCmd.GetMethod() == "GetIDSource" &&
423 argCmd.GetNbArgs() == 2 )
425 theCommand->SetArg( i, argCmd.GetArg( 1 ));
432 //================================================================================
434 * \brief Convert a python script using commands of smeshBuilder.py
435 * \param theScript - Input script
436 * \param theEntry2AccessorMethod - returns method names to access to
437 * objects wrapped with python class
438 * \param theObjectNames - names of objects
439 * \param theRemovedObjIDs - entries of objects whose created commands were removed
440 * \param theHistoricalDump - true means to keep all commands, false means
441 * to exclude commands relating to objects removed from study
442 * \retval TCollection_AsciiString - Convertion result
444 //================================================================================
446 TCollection_AsciiString
447 SMESH_2smeshpy::ConvertScript(const TCollection_AsciiString& theScript,
448 Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod,
449 Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
450 std::set< TCollection_AsciiString >& theRemovedObjIDs,
451 SALOMEDS::Study_ptr& theStudy,
452 const bool theToKeepAllCommands)
454 theGen = new _pyGen( theEntry2AccessorMethod,
458 theToKeepAllCommands );
460 // split theScript into separate commands
462 SMESH_NoteBook * aNoteBook = new SMESH_NoteBook();
464 int from = 1, end = theScript.Length(), to;
465 while ( from < end && ( to = theScript.Location( "\n", from, end )))
468 // cut out and store a command
469 aNoteBook->AddCommand( theScript.SubString( from, to - 1 ));
473 aNoteBook->ReplaceVariables();
475 TCollection_AsciiString aNoteScript = aNoteBook->GetResultScript();
479 // split theScript into separate commands
480 from = 1, end = aNoteScript.Length();
481 while ( from < end && ( to = aNoteScript.Location( "\n", from, end )))
484 // cut out and store a command
485 theGen->AddCommand( aNoteScript.SubString( from, to - 1 ));
491 #ifdef DUMP_CONVERSION
492 MESSAGE_BEGIN ( std::endl << " ######## RESULT ######## " << std::endl<< std::endl );
495 // clean commmands of removed objects depending on myIsPublished flag
496 theGen->ClearCommands();
498 // reorder commands after conversion
499 list< Handle(_pyCommand) >::iterator cmd;
502 orderChanges = false;
503 for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
504 if ( (*cmd)->SetDependentCmdsAfter() )
506 } while ( orderChanges );
508 // concat commands back into a script
509 TCollection_AsciiString aScript, aPrevCmd;
510 set<_pyID> createdObjects;
511 for ( cmd = theGen->GetCommands().begin(); cmd != theGen->GetCommands().end(); ++cmd )
513 #ifdef DUMP_CONVERSION
514 MESSAGE_ADD ( "## COM " << (*cmd)->GetOrderNb() << ": "<< (*cmd)->GetString() << std::endl );
516 if ( !(*cmd)->IsEmpty() && aPrevCmd != (*cmd)->GetString()) {
517 CheckObjectPresence( *cmd, createdObjects );
518 aPrevCmd = (*cmd)->GetString();
531 //================================================================================
533 * \brief _pyGen constructor
535 //================================================================================
537 _pyGen::_pyGen(Resource_DataMapOfAsciiStringAsciiString& theEntry2AccessorMethod,
538 Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
539 std::set< TCollection_AsciiString >& theRemovedObjIDs,
540 SALOMEDS::Study_ptr& theStudy,
541 const bool theToKeepAllCommands)
542 : _pyObject( new _pyCommand( "", 0 )),
544 myID2AccessorMethod( theEntry2AccessorMethod ),
545 myObjectNames( theObjectNames ),
546 myRemovedObjIDs( theRemovedObjIDs ),
548 myToKeepAllCommands( theToKeepAllCommands ),
549 myStudy( SALOMEDS::Study::_duplicate( theStudy )),
550 myGeomIDNb(0), myGeomIDIndex(-1)
552 // make that GetID() to return TPythonDump::SMESHGenName()
553 GetCreationCmd()->Clear();
554 GetCreationCmd()->GetString() = TPythonDump::SMESHGenName();
555 GetCreationCmd()->GetString() += "=";
557 // Find 1st digit of study entry by which a GEOM object differs from a SMESH object
558 if ( !theObjectNames.IsEmpty() && !CORBA::is_nil( theStudy ))
562 SALOMEDS::SComponent_wrap geomComp = theStudy->FindComponent("GEOM");
563 if ( geomComp->_is_nil() ) return;
564 CORBA::String_var entry = geomComp->GetID();
567 // find a SMESH entry
569 Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString e2n( theObjectNames );
570 for ( ; e2n.More() && smeshID.IsEmpty(); e2n.Next() )
571 if ( _pyCommand::IsStudyEntry( e2n.Key() ))
574 // find 1st difference between smeshID and geomID
575 if ( !geomID.IsEmpty() && !smeshID.IsEmpty() )
576 for ( int i = 1; i <= geomID.Length() && i <= smeshID.Length(); ++i )
577 if ( geomID.Value( i ) != smeshID.Value( i ))
579 myGeomIDNb = geomID.Value( i );
585 //================================================================================
587 * \brief name of SMESH_Gen in smeshBuilder.py
589 //================================================================================
591 const char* _pyGen::AccessorMethod() const
593 return SMESH_2smeshpy::GenName();
596 //================================================================================
598 * \brief Convert a command using a specific converter
599 * \param theCommand - the command to convert
601 //================================================================================
603 Handle(_pyCommand) _pyGen::AddCommand( const TCollection_AsciiString& theCommand)
605 // store theCommand in the sequence
606 myCommands.push_back( new _pyCommand( theCommand, ++myNbCommands ));
608 Handle(_pyCommand) aCommand = myCommands.back();
609 #ifdef DUMP_CONVERSION
610 MESSAGE ( "## COM " << myNbCommands << ": "<< aCommand->GetString() );
613 const _pyID& objID = aCommand->GetObject();
615 if ( objID.IsEmpty() )
618 // Prevent moving a command creating a sub-mesh to the end of the script
619 // if the sub-mesh is used in theCommand as argument
620 if ( _pySubMesh::CanBeArgOfMethod( aCommand->GetMethod() ))
622 PlaceSubmeshAfterItsCreation( aCommand );
625 // Method( SMESH.PointStruct(x,y,z) -> Method( [x,y,z]
626 StructToList( aCommand );
628 // Find an object to process theCommand
631 if ( objID == this->GetID() || objID == SMESH_2smeshpy::GenName())
633 this->Process( aCommand );
634 addFilterUser( aCommand, theGen ); // protect filters from clearing
638 // SMESH_Mesh method?
639 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( objID );
640 if ( id_mesh != myMeshes.end() )
642 //id_mesh->second->AddProcessedCmd( aCommand );
644 // check for mesh editor object
645 if ( aCommand->GetMethod() == "GetMeshEditor" ) { // MeshEditor creation
646 _pyID editorID = aCommand->GetResultValue();
647 Handle(_pyMeshEditor) editor = new _pyMeshEditor( aCommand );
648 myMeshEditors.insert( make_pair( editorID, editor ));
651 // check for SubMesh objects
652 else if ( aCommand->GetMethod() == "GetSubMesh" ) { // SubMesh creation
653 _pyID subMeshID = aCommand->GetResultValue();
654 Handle(_pySubMesh) subMesh = new _pySubMesh( aCommand );
655 myObjects.insert( make_pair( subMeshID, subMesh ));
658 // Method( mesh.GetIDSource([id1,id2]) -> Method( [id1,id2]
659 GetIDSourceToList( aCommand );
661 addFilterUser( aCommand, theGen ); // protect filters from clearing
663 id_mesh->second->Process( aCommand );
664 id_mesh->second->AddProcessedCmd( aCommand );
668 // SMESH_MeshEditor method?
669 map< _pyID, Handle(_pyMeshEditor) >::iterator id_editor = myMeshEditors.find( objID );
670 if ( id_editor != myMeshEditors.end() )
672 // Method( mesh.GetIDSource([id1,id2]) -> Method( [id1,id2]
673 GetIDSourceToList( aCommand );
675 addFilterUser( aCommand, theGen ); // protect filters from clearing
677 const TCollection_AsciiString& method = aCommand->GetMethod();
679 // some commands of SMESH_MeshEditor create meshes and groups
680 _pyID meshID, groups;
681 if ( method.Search("MakeMesh") != -1 )
682 meshID = aCommand->GetResultValue();
683 else if ( method == "MakeBoundaryMesh")
684 meshID = aCommand->GetResultValue(1);
685 else if ( method == "MakeBoundaryElements")
686 meshID = aCommand->GetResultValue(2);
688 if ( method.Search("MakeGroups") != -1 ||
689 method == "ExtrusionAlongPathX" ||
690 method == "ExtrusionAlongPathObjX" ||
691 method == "DoubleNodeGroupNew" ||
692 method == "DoubleNodeGroupsNew" ||
693 method == "DoubleNodeElemGroupNew" ||
694 method == "DoubleNodeElemGroupsNew"||
695 method == "DoubleNodeElemGroup2New"||
696 method == "DoubleNodeElemGroups2New"
698 groups = aCommand->GetResultValue();
699 else if ( method == "MakeBoundaryMesh" )
700 groups = aCommand->GetResultValue(2);
701 else if ( method == "MakeBoundaryElements")
702 groups = aCommand->GetResultValue(3);
703 else if ( method == "Create0DElementsOnAllNodes" &&
704 aCommand->GetArg(2).Length() > 2 ) // group name != ''
705 groups = aCommand->GetResultValue();
707 id_editor->second->Process( aCommand );
708 id_editor->second->AddProcessedCmd( aCommand );
711 if ( !meshID.IsEmpty() &&
712 !myMeshes.count( meshID ) &&
713 aCommand->IsStudyEntry( meshID ))
715 TCollection_AsciiString processedCommand = aCommand->GetString();
716 Handle(_pyMesh) mesh = new _pyMesh( aCommand, meshID );
717 myMeshes.insert( make_pair( meshID, mesh ));
719 aCommand->GetString() = processedCommand; // discard changes made by _pyMesh
722 if ( !groups.IsEmpty() )
724 if ( !aCommand->IsStudyEntry( meshID ))
725 meshID = id_editor->second->GetMesh();
726 Handle(_pyMesh) mesh = myMeshes[ meshID ];
728 list< _pyID > idList = aCommand->GetStudyEntries( groups );
729 list< _pyID >::iterator grID = idList.begin();
730 for ( ; grID != idList.end(); ++grID )
731 if ( !myObjects.count( *grID ))
733 Handle(_pyGroup) group = new _pyGroup( aCommand, *grID );
735 if ( !mesh.IsNull() ) mesh->AddGroup( group );
739 } // SMESH_MeshEditor methods
741 // SMESH_Hypothesis method?
742 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
743 for ( ; hyp != myHypos.end(); ++hyp )
744 if ( !(*hyp)->IsAlgo() && objID == (*hyp)->GetID() ) {
745 (*hyp)->Process( aCommand );
746 (*hyp)->AddProcessedCmd( aCommand );
750 // aFilterManager.CreateFilter() ?
751 if ( aCommand->GetMethod() == "CreateFilter" )
753 // Set a more human readable name to a filter
754 // aFilter0x7fbf6c71cfb0 -> aFilter_nb
755 _pyID newID, filterID = aCommand->GetResultValue();
756 int pos = filterID.Search( "0x" );
758 newID = (filterID.SubString(1,pos-1) + "_") + _pyID( ++myNbFilters );
760 Handle(_pyObject) filter( new _pyFilter( aCommand, newID ));
764 // other object method?
765 map< _pyID, Handle(_pyObject) >::iterator id_obj = myObjects.find( objID );
766 if ( id_obj != myObjects.end() ) {
767 id_obj->second->Process( aCommand );
768 id_obj->second->AddProcessedCmd( aCommand );
772 // Add access to a wrapped mesh
773 AddMeshAccessorMethod( aCommand );
775 // Add access to a wrapped algorithm
776 // AddAlgoAccessorMethod( aCommand ); // ??? what if algo won't be wrapped at all ???
778 // PAL12227. PythonDump was not updated at proper time; result is
779 // aCriteria.append(SMESH.Filter.Criterion(17,26,0,'L1',26,25,1e-07,SMESH.EDGE,-1))
780 // TypeError: __init__() takes exactly 11 arguments (10 given)
781 const char wrongCommand[] = "SMESH.Filter.Criterion(";
782 if ( int beg = theCommand.Location( wrongCommand, 1, theCommand.Length() ))
784 _pyCommand tmpCmd( theCommand.SubString( beg, theCommand.Length() ), -1);
785 // there must be 10 arguments, 5-th arg ThresholdID is missing,
786 const int wrongNbArgs = 9, missingArg = 5;
787 if ( tmpCmd.GetNbArgs() == wrongNbArgs )
789 for ( int i = wrongNbArgs; i > missingArg; --i )
790 tmpCmd.SetArg( i + 1, tmpCmd.GetArg( i ));
791 tmpCmd.SetArg( missingArg, "''");
792 aCommand->GetString().Trunc( beg - 1 );
793 aCommand->GetString() += tmpCmd.GetString();
796 // set GetCriterion(elementType,CritType,Compare,Treshold,UnaryOp,BinaryOp,Tolerance)
798 // instead of "SMESH.Filter.Criterion(
799 // Type,Compare,Threshold,ThresholdStr,ThresholdID,UnaryOp,BinaryOp,Tolerance,TypeOfElement,Precision)
800 // 1 2 3 4 5 6 7 8 9 10
801 // in order to avoid the problem of type mismatch of long and FunctorType
802 const TCollection_AsciiString
803 SMESH("SMESH."), dfltFunctor("SMESH.FT_Undefined"), dfltTol("1e-07"), dfltPreci("-1");
804 TCollection_AsciiString
805 Type = aCommand->GetArg(1), // long
806 Compare = aCommand->GetArg(2), // long
807 Threshold = aCommand->GetArg(3), // double
808 ThresholdStr = aCommand->GetArg(4), // string
809 ThresholdID = aCommand->GetArg(5), // string
810 UnaryOp = aCommand->GetArg(6), // long
811 BinaryOp = aCommand->GetArg(7), // long
812 Tolerance = aCommand->GetArg(8), // double
813 TypeOfElement = aCommand->GetArg(9), // ElementType
814 Precision = aCommand->GetArg(10); // long
815 fixFunctorType( Type, Compare, UnaryOp, BinaryOp );
816 Type = SMESH + SMESH::FunctorTypeToString( SMESH::FunctorType( Type.IntegerValue() ));
817 Compare = SMESH + SMESH::FunctorTypeToString( SMESH::FunctorType( Compare.IntegerValue() ));
818 UnaryOp = SMESH + SMESH::FunctorTypeToString( SMESH::FunctorType( UnaryOp.IntegerValue() ));
819 BinaryOp = SMESH + SMESH::FunctorTypeToString( SMESH::FunctorType( BinaryOp.IntegerValue() ));
821 if ( Compare == "SMESH.FT_EqualTo" )
824 aCommand->RemoveArgs();
825 aCommand->SetObject( SMESH_2smeshpy::GenName() );
826 aCommand->SetMethod( "GetCriterion" );
828 aCommand->SetArg( 1, TypeOfElement );
829 aCommand->SetArg( 2, Type );
830 aCommand->SetArg( 3, Compare );
832 if ( Threshold.IsIntegerValue() )
834 int iGeom = Threshold.IntegerValue();
835 if ( Type == "SMESH.FT_ElemGeomType" )
837 // set SMESH.GeometryType instead of a numerical Threshold
838 const char* types[SMESH::Geom_BALL+1] = {
839 "Geom_POINT", "Geom_EDGE", "Geom_TRIANGLE", "Geom_QUADRANGLE", "Geom_POLYGON",
840 "Geom_TETRA", "Geom_PYRAMID", "Geom_HEXA", "Geom_PENTA", "Geom_HEXAGONAL_PRISM",
841 "Geom_POLYHEDRA", "Geom_BALL" };
842 if ( -1 < iGeom && iGeom < SMESH::Geom_POLYHEDRA+1 )
843 Threshold = SMESH + types[ iGeom ];
845 if (Type == "SMESH.FT_EntityType")
847 // set SMESH.EntityType instead of a numerical Threshold
848 const char* types[SMESH::Entity_Ball+1] = {
849 "Entity_Node", "Entity_0D", "Entity_Edge", "Entity_Quad_Edge",
850 "Entity_Triangle", "Entity_Quad_Triangle", "Entity_BiQuad_Triangle",
851 "Entity_Quadrangle", "Entity_Quad_Quadrangle", "Entity_BiQuad_Quadrangle",
852 "Entity_Polygon", "Entity_Quad_Polygon", "Entity_Tetra", "Entity_Quad_Tetra",
853 "Entity_Pyramid", "Entity_Quad_Pyramid",
854 "Entity_Hexa", "Entity_Quad_Hexa", "Entity_TriQuad_Hexa",
855 "Entity_Penta", "Entity_Quad_Penta", "Entity_Hexagonal_Prism",
856 "Entity_Polyhedra", "Entity_Quad_Polyhedra", "Entity_Ball" };
857 if ( -1 < iGeom && iGeom < SMESH::Entity_Quad_Polyhedra+1 )
858 Threshold = SMESH + types[ iGeom ];
861 if ( ThresholdID.Length() != 2 ) // neither '' nor ""
862 aCommand->SetArg( 4, ThresholdID.SubString( 2, ThresholdID.Length()-1 )); // shape entry
863 else if ( ThresholdStr.Length() != 2 )
864 aCommand->SetArg( 4, ThresholdStr );
865 else if ( ThresholdID.Length() != 2 )
866 aCommand->SetArg( 4, ThresholdID );
868 aCommand->SetArg( 4, Threshold );
869 // find the last not default arg
871 if ( Tolerance == dfltTol ) {
873 if ( BinaryOp == dfltFunctor ) {
875 if ( UnaryOp == dfltFunctor )
879 if ( 5 < lastDefault ) aCommand->SetArg( 5, UnaryOp );
880 if ( 6 < lastDefault ) aCommand->SetArg( 6, BinaryOp );
881 if ( 7 < lastDefault ) aCommand->SetArg( 7, Tolerance );
882 if ( Precision != dfltPreci )
884 TCollection_AsciiString crit = aCommand->GetResultValue();
885 aCommand->GetString() += "; ";
886 aCommand->GetString() += crit + ".Precision = " + Precision;
892 //================================================================================
894 * \brief Convert the command or remember it for later conversion
895 * \param theCommand - The python command calling a method of SMESH_Gen
897 //================================================================================
899 void _pyGen::Process( const Handle(_pyCommand)& theCommand )
901 // there are methods to convert:
902 // CreateMesh( shape )
903 // Concatenate( [mesh1, ...], ... )
904 // CreateHypothesis( theHypType, theLibName )
905 // Compute( mesh, geom )
906 // Evaluate( mesh, geom )
908 TCollection_AsciiString method = theCommand->GetMethod();
910 if ( method == "CreateMesh" || method == "CreateEmptyMesh")
912 Handle(_pyMesh) mesh = new _pyMesh( theCommand );
913 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
916 if ( method == "CreateMeshesFromUNV" ||
917 method == "CreateMeshesFromSTL" ||
918 method == "CopyMesh" ) // command result is a mesh
920 Handle(_pyMesh) mesh = new _pyMesh( theCommand, theCommand->GetResultValue() );
921 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
924 if( method == "CreateMeshesFromMED" ||
925 method == "CreateMeshesFromSAUV"||
926 method == "CreateMeshesFromCGNS" ||
927 method == "CreateMeshesFromGMF" ) // command result is ( [mesh1,mesh2], status )
929 for ( int ind = 0; ind < theCommand->GetNbResultValues(); ind++ )
931 _pyID meshID = theCommand->GetResultValue(ind+1);
932 if ( !theCommand->IsStudyEntry( meshID ) ) continue;
933 Handle(_pyMesh) mesh = new _pyMesh( theCommand, theCommand->GetResultValue(ind+1));
934 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
936 if ( method == "CreateMeshesFromGMF" )
938 // CreateMeshesFromGMF( theFileName, theMakeRequiredGroups ) ->
939 // CreateMeshesFromGMF( theFileName )
940 _AString file = theCommand->GetArg(1);
941 theCommand->RemoveArgs();
942 theCommand->SetArg( 1, file );
946 // CreateHypothesis()
947 if ( method == "CreateHypothesis" )
949 // issue 199929, remove standard library name (default parameter)
950 const TCollection_AsciiString & aLibName = theCommand->GetArg( 2 );
951 if ( aLibName.Search( "StdMeshersEngine" ) != -1 ) {
952 // keep first argument
953 TCollection_AsciiString arg = theCommand->GetArg( 1 );
954 theCommand->RemoveArgs();
955 theCommand->SetArg( 1, arg );
958 myHypos.push_back( _pyHypothesis::NewHypothesis( theCommand ));
962 // smeshgen.Compute( mesh, geom ) --> mesh.Compute()
963 if ( method == "Compute" )
965 const _pyID& meshID = theCommand->GetArg( 1 );
966 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( meshID );
967 if ( id_mesh != myMeshes.end() ) {
968 theCommand->SetObject( meshID );
969 theCommand->RemoveArgs();
970 id_mesh->second->Process( theCommand );
971 id_mesh->second->AddProcessedCmd( theCommand );
976 // smeshgen.Evaluate( mesh, geom ) --> mesh.Evaluate(geom)
977 if ( method == "Evaluate" )
979 const _pyID& meshID = theCommand->GetArg( 1 );
980 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.find( meshID );
981 if ( id_mesh != myMeshes.end() ) {
982 theCommand->SetObject( meshID );
983 _pyID geom = theCommand->GetArg( 2 );
984 theCommand->RemoveArgs();
985 theCommand->SetArg( 1, geom );
986 id_mesh->second->AddProcessedCmd( theCommand );
991 // objects erasing creation command if no more its commands invoked:
992 // SMESH_Pattern, FilterManager
993 if ( method == "GetPattern" ||
994 method == "CreateFilterManager" ||
995 method == "CreateMeasurements" ) {
996 Handle(_pyObject) obj = new _pySelfEraser( theCommand );
997 if ( !myObjects.insert( make_pair( obj->GetID(), obj )).second )
998 theCommand->Clear(); // already created
1000 // Concatenate( [mesh1, ...], ... )
1001 else if ( method == "Concatenate" || method == "ConcatenateWithGroups")
1003 if ( method == "ConcatenateWithGroups" ) {
1004 theCommand->SetMethod( "Concatenate" );
1005 theCommand->SetArg( theCommand->GetNbArgs() + 1, "True" );
1007 Handle(_pyMesh) mesh = new _pyMesh( theCommand, theCommand->GetResultValue() );
1008 myMeshes.insert( make_pair( mesh->GetID(), mesh ));
1009 AddMeshAccessorMethod( theCommand );
1011 else if ( method == "SetName" ) // SetName(obj,name)
1013 // store theCommand as one of object commands to erase it along with the object
1014 const _pyID& objID = theCommand->GetArg( 1 );
1015 Handle(_pyObject) obj = FindObject( objID );
1016 if ( !obj.IsNull() )
1017 obj->AddProcessedCmd( theCommand );
1020 // Replace name of SMESH_Gen
1022 // names of SMESH_Gen methods fully equal to methods defined in smeshBuilder.py
1023 static TStringSet smeshpyMethods;
1024 if ( smeshpyMethods.empty() ) {
1025 const char * names[] =
1026 { "SetEmbeddedMode","IsEmbeddedMode","SetCurrentStudy","GetCurrentStudy",
1027 "GetPattern","GetSubShapesId",
1028 "" }; // <- mark of array end
1029 smeshpyMethods.Insert( names );
1031 if ( smeshpyMethods.Contains( theCommand->GetMethod() ))
1032 // smeshgen.Method() --> smesh.Method()
1033 theCommand->SetObject( SMESH_2smeshpy::SmeshpyName() );
1035 // smeshgen.Method() --> smesh.Method()
1036 theCommand->SetObject( SMESH_2smeshpy::GenName() );
1039 //================================================================================
1041 * \brief Convert the remembered commands
1043 //================================================================================
1045 void _pyGen::Flush()
1047 // create an empty command
1048 myLastCommand = new _pyCommand();
1050 map< _pyID, Handle(_pyMesh) >::iterator id_mesh;
1051 map< _pyID, Handle(_pyObject) >::iterator id_obj;
1052 list< Handle(_pyHypothesis) >::iterator hyp;
1054 if ( IsToKeepAllCommands() ) // historical dump
1056 // set myIsPublished = true to all objects
1057 for ( id_mesh = myMeshes.begin(); id_mesh != myMeshes.end(); ++id_mesh )
1058 id_mesh->second->SetRemovedFromStudy( false );
1059 for ( hyp = myHypos.begin(); hyp != myHypos.end(); ++hyp )
1060 (*hyp)->SetRemovedFromStudy( false );
1061 for ( id_obj = myObjects.begin(); id_obj != myObjects.end(); ++id_obj )
1062 id_obj->second->SetRemovedFromStudy( false );
1066 // let hypotheses find referred objects in order to prevent clearing
1067 // not published referred hyps (it's needed for hyps like "LayerDistribution")
1068 list< Handle(_pyMesh) > fatherMeshes;
1069 for ( hyp = myHypos.begin(); hyp != myHypos.end(); ++hyp )
1070 if ( !hyp->IsNull() )
1071 (*hyp)->GetReferredMeshesAndGeom( fatherMeshes );
1073 // set myIsPublished = false to all objects depending on
1074 // meshes built on a removed geometry
1075 for ( id_mesh = myMeshes.begin(); id_mesh != myMeshes.end(); ++id_mesh )
1076 if ( id_mesh->second->IsNotGeomPublished() )
1077 id_mesh->second->SetRemovedFromStudy( true );
1080 for ( id_mesh = myMeshes.begin(); id_mesh != myMeshes.end(); ++id_mesh )
1081 if ( ! id_mesh->second.IsNull() )
1082 id_mesh->second->Flush();
1085 for ( hyp = myHypos.begin(); hyp != myHypos.end(); ++hyp )
1086 if ( !hyp->IsNull() ) {
1088 // smeshgen.CreateHypothesis() --> smesh.CreateHypothesis()
1089 if ( !(*hyp)->IsWrapped() )
1090 (*hyp)->GetCreationCmd()->SetObject( SMESH_2smeshpy::GenName() );
1093 // Flush other objects
1094 for ( id_obj = myObjects.begin(); id_obj != myObjects.end(); ++id_obj )
1095 if ( ! id_obj->second.IsNull() )
1096 id_obj->second->Flush();
1098 myLastCommand->SetOrderNb( ++myNbCommands );
1099 myCommands.push_back( myLastCommand );
1102 //================================================================================
1104 * \brief Prevent moving a command creating a sub-mesh to the end of the script
1105 * if the sub-mesh is used in theCmdUsingSubmesh as argument
1107 //================================================================================
1109 void _pyGen::PlaceSubmeshAfterItsCreation( Handle(_pyCommand) theCmdUsingSubmesh ) const
1111 map< _pyID, Handle(_pyObject) >::const_iterator id_obj = myObjects.begin();
1112 for ( ; id_obj != myObjects.end(); ++id_obj )
1114 if ( !id_obj->second->IsKind( STANDARD_TYPE( _pySubMesh ))) continue;
1115 for ( int iArg = theCmdUsingSubmesh->GetNbArgs(); iArg; --iArg )
1117 const _pyID& arg = theCmdUsingSubmesh->GetArg( iArg );
1118 if ( arg.IsEmpty() || arg.Value( 1 ) == '"' || arg.Value( 1 ) == '\'' )
1120 list< _pyID > idList = theCmdUsingSubmesh->GetStudyEntries( arg );
1121 list< _pyID >::iterator id = idList.begin();
1122 for ( ; id != idList.end(); ++id )
1123 if ( id_obj->first == *id )
1124 // _pySubMesh::Process() does what we need
1125 Handle(_pySubMesh)::DownCast( id_obj->second )->Process( theCmdUsingSubmesh );
1130 //================================================================================
1132 * \brief Clean commmands of removed objects depending on myIsPublished flag
1134 //================================================================================
1136 void _pyGen::ClearCommands()
1138 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.begin();
1139 for ( ; id_mesh != myMeshes.end(); ++id_mesh )
1140 id_mesh->second->ClearCommands();
1142 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
1143 for ( ; hyp != myHypos.end(); ++hyp )
1144 if ( !hyp->IsNull() )
1145 (*hyp)->ClearCommands();
1147 map< _pyID, Handle(_pyObject) >::iterator id_obj = myObjects.begin();
1148 for ( ; id_obj != myObjects.end(); ++id_obj )
1149 id_obj->second->ClearCommands();
1152 //================================================================================
1154 * \brief Release mutual handles of objects
1156 //================================================================================
1160 map< _pyID, Handle(_pyMesh) >::iterator id_mesh = myMeshes.begin();
1161 for ( ; id_mesh != myMeshes.end(); ++id_mesh )
1162 id_mesh->second->Free();
1165 map< _pyID, Handle(_pyMeshEditor) >::iterator id_ed = myMeshEditors.begin();
1166 for ( ; id_ed != myMeshEditors.end(); ++id_ed )
1167 id_ed->second->Free();
1168 myMeshEditors.clear();
1170 map< _pyID, Handle(_pyObject) >::iterator id_obj = myObjects.begin();
1171 for ( ; id_obj != myObjects.end(); ++id_obj )
1172 id_obj->second->Free();
1175 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
1176 for ( ; hyp != myHypos.end(); ++hyp )
1177 if ( !hyp->IsNull() )
1181 myFile2ExportedMesh.clear();
1184 //================================================================================
1186 * \brief Add access method to mesh that is an argument
1187 * \param theCmd - command to add access method
1188 * \retval bool - true if added
1190 //================================================================================
1192 bool _pyGen::AddMeshAccessorMethod( Handle(_pyCommand) theCmd ) const
1195 map< _pyID, Handle(_pyMesh) >::const_iterator id_mesh = myMeshes.begin();
1196 for ( ; id_mesh != myMeshes.end(); ++id_mesh ) {
1197 if ( theCmd->AddAccessorMethod( id_mesh->first, id_mesh->second->AccessorMethod() ))
1203 //================================================================================
1205 * \brief Add access method to algo that is an object or an argument
1206 * \param theCmd - command to add access method
1207 * \retval bool - true if added
1209 //================================================================================
1211 bool _pyGen::AddAlgoAccessorMethod( Handle(_pyCommand) theCmd ) const
1214 list< Handle(_pyHypothesis) >::const_iterator hyp = myHypos.begin();
1215 for ( ; hyp != myHypos.end(); ++hyp ) {
1216 if ( (*hyp)->IsAlgo() && /*(*hyp)->IsWrapped() &&*/
1217 theCmd->AddAccessorMethod( (*hyp)->GetID(), (*hyp)->AccessorMethod() ))
1223 //================================================================================
1225 * \brief Find hypothesis by ID (entry)
1226 * \param theHypID - The hypothesis ID
1227 * \retval Handle(_pyHypothesis) - The found hypothesis
1229 //================================================================================
1231 Handle(_pyHypothesis) _pyGen::FindHyp( const _pyID& theHypID )
1233 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
1234 for ( ; hyp != myHypos.end(); ++hyp )
1235 if ( !hyp->IsNull() && theHypID == (*hyp)->GetID() )
1237 return Handle(_pyHypothesis)();
1240 //================================================================================
1242 * \brief Find algorithm the created algorithm
1243 * \param theGeom - The shape ID the algorithm was created on
1244 * \param theMesh - The mesh ID that created the algorithm
1245 * \param dim - The algo dimension
1246 * \retval Handle(_pyHypothesis) - The found algo
1248 //================================================================================
1250 Handle(_pyHypothesis) _pyGen::FindAlgo( const _pyID& theGeom, const _pyID& theMesh,
1251 const Handle(_pyHypothesis)& theHypothesis )
1253 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
1254 for ( ; hyp != myHypos.end(); ++hyp )
1255 if ( !hyp->IsNull() &&
1257 theHypothesis->CanBeCreatedBy( (*hyp)->GetAlgoType() ) &&
1258 (*hyp)->GetGeom() == theGeom &&
1259 (*hyp)->GetMesh() == theMesh )
1264 //================================================================================
1266 * \brief Find subMesh by ID (entry)
1267 * \param theSubMeshID - The subMesh ID
1268 * \retval Handle(_pySubMesh) - The found subMesh
1270 //================================================================================
1272 Handle(_pySubMesh) _pyGen::FindSubMesh( const _pyID& theSubMeshID )
1274 map< _pyID, Handle(_pyObject) >::iterator id_subMesh = myObjects.find(theSubMeshID);
1275 if ( id_subMesh != myObjects.end() )
1276 return Handle(_pySubMesh)::DownCast( id_subMesh->second );
1277 return Handle(_pySubMesh)();
1281 //================================================================================
1283 * \brief Change order of commands in the script
1284 * \param theCmd1 - One command
1285 * \param theCmd2 - Another command
1287 //================================================================================
1289 void _pyGen::ExchangeCommands( Handle(_pyCommand) theCmd1, Handle(_pyCommand) theCmd2 )
1291 list< Handle(_pyCommand) >::iterator pos1, pos2;
1292 pos1 = find( myCommands.begin(), myCommands.end(), theCmd1 );
1293 pos2 = find( myCommands.begin(), myCommands.end(), theCmd2 );
1294 myCommands.insert( pos1, theCmd2 );
1295 myCommands.insert( pos2, theCmd1 );
1296 myCommands.erase( pos1 );
1297 myCommands.erase( pos2 );
1299 int nb1 = theCmd1->GetOrderNb();
1300 theCmd1->SetOrderNb( theCmd2->GetOrderNb() );
1301 theCmd2->SetOrderNb( nb1 );
1302 // cout << "BECOME " << theCmd1->GetOrderNb() << "\t" << theCmd1->GetString() << endl
1303 // << "BECOME " << theCmd2->GetOrderNb() << "\t" << theCmd2->GetString() << endl << endl;
1306 //================================================================================
1308 * \brief Set one command after the other
1309 * \param theCmd - Command to move
1310 * \param theAfterCmd - Command ater which to insert the first one
1312 //================================================================================
1314 void _pyGen::SetCommandAfter( Handle(_pyCommand) theCmd, Handle(_pyCommand) theAfterCmd )
1316 setNeighbourCommand( theCmd, theAfterCmd, true );
1319 //================================================================================
1321 * \brief Set one command before the other
1322 * \param theCmd - Command to move
1323 * \param theBeforeCmd - Command before which to insert the first one
1325 //================================================================================
1327 void _pyGen::SetCommandBefore( Handle(_pyCommand) theCmd, Handle(_pyCommand) theBeforeCmd )
1329 setNeighbourCommand( theCmd, theBeforeCmd, false );
1332 //================================================================================
1334 * \brief Set one command before or after the other
1335 * \param theCmd - Command to move
1336 * \param theOtherCmd - Command ater or before which to insert the first one
1338 //================================================================================
1340 void _pyGen::setNeighbourCommand( Handle(_pyCommand)& theCmd,
1341 Handle(_pyCommand)& theOtherCmd,
1342 const bool theIsAfter )
1344 list< Handle(_pyCommand) >::iterator pos;
1345 pos = find( myCommands.begin(), myCommands.end(), theCmd );
1346 myCommands.erase( pos );
1347 pos = find( myCommands.begin(), myCommands.end(), theOtherCmd );
1348 myCommands.insert( (theIsAfter ? ++pos : pos), theCmd );
1351 for ( pos = myCommands.begin(); pos != myCommands.end(); ++pos)
1352 (*pos)->SetOrderNb( i++ );
1355 //================================================================================
1357 * \brief Call _pyFilter.AddUser() if a filter is used as a command arg
1359 //================================================================================
1361 void _pyGen::addFilterUser( Handle(_pyCommand)& theCommand, const Handle(_pyObject)& user )
1363 const char filterPrefix[] = "aFilter0x";
1364 if ( theCommand->GetString().Search( filterPrefix ) < 1 )
1367 for ( int i = theCommand->GetNbArgs(); i > 0; --i )
1369 const _AString & arg = theCommand->GetArg( i );
1370 // NOT TREATED CASE: arg == "[something, aFilter0x36a2f60]"
1371 if ( arg.Search( filterPrefix ) != 1 )
1374 Handle(_pyFilter) filter = Handle(_pyFilter)::DownCast( FindObject( arg ));
1375 if ( !filter.IsNull() )
1377 filter->AddUser( user );
1378 if ( !filter->GetNewID().IsEmpty() )
1379 theCommand->SetArg( i, filter->GetNewID() );
1384 //================================================================================
1386 * \brief Set command be last in list of commands
1387 * \param theCmd - Command to be last
1389 //================================================================================
1391 Handle(_pyCommand)& _pyGen::GetLastCommand()
1393 return myLastCommand;
1396 //================================================================================
1398 * \brief Set method to access to object wrapped with python class
1399 * \param theID - The wrapped object entry
1400 * \param theMethod - The accessor method
1402 //================================================================================
1404 void _pyGen::SetAccessorMethod(const _pyID& theID, const char* theMethod )
1406 myID2AccessorMethod.Bind( theID, (char*) theMethod );
1409 //================================================================================
1411 * \brief Generated new ID for object and assign with existing name
1412 * \param theID - ID of existing object
1414 //================================================================================
1416 _pyID _pyGen::GenerateNewID( const _pyID& theID )
1421 aNewID = theID + _pyID( ":" ) + _pyID( index++ );
1423 while ( myObjectNames.IsBound( aNewID ) );
1425 myObjectNames.Bind( aNewID, myObjectNames.IsBound( theID )
1426 ? (myObjectNames.Find( theID ) + _pyID( "_" ) + _pyID( index-1 ))
1427 : _pyID( "A" ) + aNewID );
1431 //================================================================================
1433 * \brief Stores theObj in myObjects
1435 //================================================================================
1437 void _pyGen::AddObject( Handle(_pyObject)& theObj )
1439 if ( theObj.IsNull() ) return;
1441 if ( theObj->IsKind( STANDARD_TYPE( _pyMesh )))
1442 myMeshes.insert( make_pair( theObj->GetID(), Handle(_pyMesh)::DownCast( theObj )));
1444 else if ( theObj->IsKind( STANDARD_TYPE( _pyMeshEditor )))
1445 myMeshEditors.insert( make_pair( theObj->GetID(), Handle(_pyMeshEditor)::DownCast( theObj )));
1448 myObjects.insert( make_pair( theObj->GetID(), theObj ));
1451 //================================================================================
1453 * \brief Re-register an object with other ID to make it Process() commands of
1454 * other object having this ID
1456 //================================================================================
1458 void _pyGen::SetProxyObject( const _pyID& theID, Handle(_pyObject)& theObj )
1460 if ( theObj.IsNull() ) return;
1462 if ( theObj->IsKind( STANDARD_TYPE( _pyMesh )))
1463 myMeshes.insert( make_pair( theID, Handle(_pyMesh)::DownCast( theObj )));
1465 else if ( theObj->IsKind( STANDARD_TYPE( _pyMeshEditor )))
1466 myMeshEditors.insert( make_pair( theID, Handle(_pyMeshEditor)::DownCast( theObj )));
1469 myObjects.insert( make_pair( theID, theObj ));
1472 //================================================================================
1474 * \brief Finds a _pyObject by ID
1476 //================================================================================
1478 Handle(_pyObject) _pyGen::FindObject( const _pyID& theObjID ) const
1481 map< _pyID, Handle(_pyObject) >::const_iterator id_obj = myObjects.find( theObjID );
1482 if ( id_obj != myObjects.end() )
1483 return id_obj->second;
1486 map< _pyID, Handle(_pyMesh) >::const_iterator id_obj = myMeshes.find( theObjID );
1487 if ( id_obj != myMeshes.end() )
1488 return id_obj->second;
1491 // map< _pyID, Handle(_pyMeshEditor) >::const_iterator id_obj = myMeshEditors.find( theObjID );
1492 // if ( id_obj != myMeshEditors.end() )
1493 // return id_obj->second;
1495 return Handle(_pyObject)();
1498 //================================================================================
1500 * \brief Check if a study entry is under GEOM component
1502 //================================================================================
1504 bool _pyGen::IsGeomObject(const _pyID& theObjID) const
1508 return ( myGeomIDIndex <= theObjID.Length() &&
1509 int( theObjID.Value( myGeomIDIndex )) == myGeomIDNb &&
1510 _pyCommand::IsStudyEntry( theObjID ));
1515 //================================================================================
1517 * \brief Returns true if an object is not present in a study
1519 //================================================================================
1521 bool _pyGen::IsNotPublished(const _pyID& theObjID) const
1523 if ( theObjID.IsEmpty() ) return false;
1525 if ( myObjectNames.IsBound( theObjID ))
1526 return false; // SMESH object is in study
1528 // either the SMESH object is not in study or it is a GEOM object
1529 if ( IsGeomObject( theObjID ))
1531 SALOMEDS::SObject_wrap so = myStudy->FindObjectID( theObjID.ToCString() );
1532 if ( so->_is_nil() ) return true;
1533 CORBA::Object_var obj = so->GetObject();
1534 return CORBA::is_nil( obj );
1536 return true; // SMESH object not in study
1539 //================================================================================
1541 * \brief Add an object to myRemovedObjIDs that leads to that SetName() for
1542 * this object is not dumped
1543 * \param [in] theObjID - entry of the object whose creation command was eliminated
1545 //================================================================================
1547 void _pyGen::ObjectCreationRemoved(const _pyID& theObjID)
1549 myRemovedObjIDs.insert( theObjID );
1552 //================================================================================
1554 * \brief Return reader of hypotheses of plugins
1556 //================================================================================
1558 Handle( _pyHypothesisReader ) _pyGen::GetHypothesisReader() const
1560 if (myHypReader.IsNull() )
1561 ((_pyGen*) this)->myHypReader = new _pyHypothesisReader;
1567 //================================================================================
1569 * \brief Mesh created by SMESH_Gen
1571 //================================================================================
1573 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd)
1574 : _pyObject( theCreationCmd ), myGeomNotInStudy( false )
1576 if ( theCreationCmd->GetMethod() == "CreateMesh" && theGen->IsNotPublished( GetGeom() ))
1577 myGeomNotInStudy = true;
1579 // convert my creation command --> smeshpy.Mesh(...)
1580 Handle(_pyCommand) creationCmd = GetCreationCmd();
1581 creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
1582 creationCmd->SetMethod( "Mesh" );
1583 theGen->SetAccessorMethod( GetID(), _pyMesh::AccessorMethod() );
1586 //================================================================================
1588 * \brief Mesh created by SMESH_MeshEditor
1590 //================================================================================
1592 _pyMesh::_pyMesh(const Handle(_pyCommand) theCreationCmd, const _pyID& meshId):
1593 _pyObject(theCreationCmd,meshId), myGeomNotInStudy(false )
1595 if ( theCreationCmd->MethodStartsFrom( "CreateMeshesFrom" ))
1597 // this mesh depends on the exported mesh
1598 const TCollection_AsciiString& file = theCreationCmd->GetArg( 1 );
1599 if ( !file.IsEmpty() )
1601 ExportedMeshData& exportData = theGen->FindExportedMesh( file );
1602 addFatherMesh( exportData.myMesh );
1603 if ( !exportData.myLastComputeCmd.IsNull() )
1605 // restore cleared Compute() by which the exported mesh was generated
1606 exportData.myLastComputeCmd->GetString() = exportData.myLastComputeCmdString;
1607 // protect that Compute() cmd from clearing
1608 if ( exportData.myMesh->myLastComputeCmd == exportData.myLastComputeCmd )
1609 exportData.myMesh->myLastComputeCmd.Nullify();
1613 else if ( theCreationCmd->MethodStartsFrom( "Concatenate" ))
1615 // this mesh depends on concatenated meshes
1616 const TCollection_AsciiString& meshIDs = theCreationCmd->GetArg( 1 );
1617 list< _pyID > idList = theCreationCmd->GetStudyEntries( meshIDs );
1618 list< _pyID >::iterator meshID = idList.begin();
1619 for ( ; meshID != idList.end(); ++meshID )
1620 addFatherMesh( *meshID );
1622 else if ( theCreationCmd->GetMethod() == "CopyMesh" )
1624 // this mesh depends on a copied IdSource
1625 const _pyID& objID = theCreationCmd->GetArg( 1 );
1626 addFatherMesh( objID );
1628 else if ( theCreationCmd->GetMethod().Search("MakeMesh") != -1 ||
1629 theCreationCmd->GetMethod() == "MakeBoundaryMesh" ||
1630 theCreationCmd->GetMethod() == "MakeBoundaryElements" )
1632 // this mesh depends on a source mesh
1633 // (theCreationCmd is already Process()ed by _pyMeshEditor)
1634 const _pyID& meshID = theCreationCmd->GetObject();
1635 addFatherMesh( meshID );
1638 // convert my creation command
1639 Handle(_pyCommand) creationCmd = GetCreationCmd();
1640 creationCmd->SetObject( SMESH_2smeshpy::SmeshpyName() );
1641 theGen->SetAccessorMethod( meshId, _pyMesh::AccessorMethod() );
1644 //================================================================================
1646 * \brief Convert an IDL API command of SMESH::SMESH_Mesh to a method call of python Mesh
1647 * \param theCommand - Engine method called for this mesh
1649 //================================================================================
1651 void _pyMesh::Process( const Handle(_pyCommand)& theCommand )
1653 // some methods of SMESH_Mesh interface needs special conversion
1654 // to methods of Mesh python class
1656 // 1. GetSubMesh(geom, name) + AddHypothesis(geom, algo)
1657 // --> in Mesh_Algorithm.Create(mesh, geom, hypo, so)
1658 // 2. AddHypothesis(geom, hyp)
1659 // --> in Mesh_Algorithm.Hypothesis(hyp, args, so)
1660 // 3. CreateGroupFromGEOM(type, name, grp)
1661 // --> in Mesh.Group(grp, name="")
1662 // 4. ExportToMED(f, auto_groups, version)
1663 // --> in Mesh.ExportMED( f, auto_groups, version )
1666 const TCollection_AsciiString& method = theCommand->GetMethod();
1667 // ----------------------------------------------------------------------
1668 if ( method == "Compute" ) // in snapshot mode, clear the previous Compute()
1670 if ( !theGen->IsToKeepAllCommands() ) // !historical
1672 list< Handle(_pyHypothesis) >::iterator hyp;
1673 if ( !myLastComputeCmd.IsNull() )
1675 for ( hyp = myHypos.begin(); hyp != myHypos.end(); ++hyp )
1676 (*hyp)->ComputeDiscarded( myLastComputeCmd );
1678 myLastComputeCmd->Clear();
1680 myLastComputeCmd = theCommand;
1682 for ( hyp = myHypos.begin(); hyp != myHypos.end(); ++hyp )
1683 (*hyp)->MeshComputed( myLastComputeCmd );
1687 // ----------------------------------------------------------------------
1688 else if ( method == "Clear" ) // in snapshot mode, clear all previous commands
1690 if ( !theGen->IsToKeepAllCommands() ) // !historical
1693 myChildMeshes.empty() ? 0 : myChildMeshes.back()->GetCreationCmd()->GetOrderNb();
1694 // list< Handle(_pyCommand) >::reverse_iterator cmd = myProcessedCmds.rbegin();
1695 // for ( ; cmd != myProcessedCmds.rend() && (*cmd)->GetOrderNb() > untilCmdNb; ++cmd )
1697 if ( !myLastComputeCmd.IsNull() )
1699 list< Handle(_pyHypothesis) >::iterator hyp;
1700 for ( hyp = myHypos.begin(); hyp != myHypos.end(); ++hyp )
1701 (*hyp)->ComputeDiscarded( myLastComputeCmd );
1703 myLastComputeCmd->Clear();
1706 list< Handle(_pyMeshEditor)>::iterator e = myEditors.begin();
1707 for ( ; e != myEditors.end(); ++e )
1709 list< Handle(_pyCommand)>& cmds = (*e)->GetProcessedCmds();
1710 list< Handle(_pyCommand) >::reverse_iterator cmd = cmds.rbegin();
1711 for ( ; cmd != cmds.rend() && (*cmd)->GetOrderNb() > untilCmdNb; ++cmd )
1712 if ( !(*cmd)->IsEmpty() )
1714 if ( (*cmd)->GetStudyEntries( (*cmd)->GetResultValue() ).empty() ) // no object created
1718 myLastComputeCmd = theCommand; // to clear Clear() the same way as Compute()
1721 // ----------------------------------------------------------------------
1722 else if ( method == "GetSubMesh" ) { // collect submeshes of the mesh
1723 Handle(_pySubMesh) subMesh = theGen->FindSubMesh( theCommand->GetResultValue() );
1724 if ( !subMesh.IsNull() ) {
1725 subMesh->SetCreator( this );
1726 mySubmeshes.push_back( subMesh );
1729 // ----------------------------------------------------------------------
1730 else if ( method == "AddHypothesis" ) { // mesh.AddHypothesis(geom, HYPO )
1731 myAddHypCmds.push_back( theCommand );
1733 const _pyID& hypID = theCommand->GetArg( 2 );
1734 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
1735 if ( !hyp.IsNull() ) {
1736 myHypos.push_back( hyp );
1737 if ( hyp->GetMesh().IsEmpty() )
1738 hyp->SetMesh( this->GetID() );
1741 // ----------------------------------------------------------------------
1742 else if ( method == "CreateGroup" ||
1743 method == "CreateGroupFromGEOM" ||
1744 method == "CreateGroupFromFilter" )
1746 Handle(_pyGroup) group = new _pyGroup( theCommand );
1747 myGroups.push_back( group );
1748 theGen->AddObject( group );
1750 // ----------------------------------------------------------------------
1751 // update list of groups
1752 else if ( method == "GetGroups" )
1754 bool allGroupsRemoved = true;
1755 TCollection_AsciiString grIDs = theCommand->GetResultValue();
1756 list< _pyID > idList = theCommand->GetStudyEntries( grIDs );
1757 list< _pyID >::iterator grID = idList.begin();
1758 const int nbGroupsBefore = myGroups.size();
1759 Handle(_pyObject) obj;
1760 for ( ; grID != idList.end(); ++grID )
1762 obj = theGen->FindObject( *grID );
1765 Handle(_pyGroup) group = new _pyGroup( theCommand, *grID );
1766 theGen->AddObject( group );
1767 myGroups.push_back( group );
1770 if ( !obj->CanClear() )
1771 allGroupsRemoved = false;
1773 if ( nbGroupsBefore == myGroups.size() ) // no new _pyGroup created
1774 obj->AddProcessedCmd( theCommand ); // to clear theCommand if all groups are removed
1776 if ( !allGroupsRemoved && !theGen->IsToKeepAllCommands() )
1778 // check if the preceding command is Compute();
1779 // if GetGroups() is just after Compute(), this can mean that the groups
1780 // were created by some algorithm and hence Compute() should not be discarded
1781 std::list< Handle(_pyCommand) >& cmdList = theGen->GetCommands();
1782 std::list< Handle(_pyCommand) >::iterator cmd = cmdList.begin();
1783 while ( (*cmd)->GetMethod() == "GetGroups" )
1785 if ( myLastComputeCmd == (*cmd))
1786 // protect last Compute() from clearing by the next Compute()
1787 myLastComputeCmd.Nullify();
1790 // ----------------------------------------------------------------------
1791 // notify a group about full removal
1792 else if ( method == "RemoveGroupWithContents" ||
1793 method == "RemoveGroup")
1795 if ( !theGen->IsToKeepAllCommands() ) { // snapshot mode
1796 const _pyID groupID = theCommand->GetArg( 1 );
1797 Handle(_pyGroup) grp = Handle(_pyGroup)::DownCast( theGen->FindObject( groupID ));
1798 if ( !grp.IsNull() )
1800 if ( method == "RemoveGroupWithContents" )
1801 grp->RemovedWithContents();
1802 // to clear RemoveGroup() if the group creation is cleared
1803 grp->AddProcessedCmd( theCommand );
1807 // ----------------------------------------------------------------------
1808 else if ( theCommand->MethodStartsFrom( "Export" ))
1810 if ( method == "ExportToMED" || // ExportToMED() --> ExportMED()
1811 method == "ExportToMEDX" ) // ExportToMEDX() --> ExportMED()
1813 theCommand->SetMethod( "ExportMED" );
1814 if ( theCommand->GetNbArgs() == 5 )
1816 // ExportToMEDX(...,autoDimension) -> ExportToMEDX(...,meshPart=None,autoDimension)
1817 _AString autoDimension = theCommand->GetArg( 5 );
1818 theCommand->SetArg( 5, "None" );
1819 theCommand->SetArg( 6, autoDimension );
1822 else if ( method == "ExportCGNS" )
1823 { // ExportCGNS(part, ...) -> ExportCGNS(..., part)
1824 _pyID partID = theCommand->GetArg( 1 );
1825 int nbArgs = theCommand->GetNbArgs();
1826 for ( int i = 2; i <= nbArgs; ++i )
1827 theCommand->SetArg( i-1, theCommand->GetArg( i ));
1828 theCommand->SetArg( nbArgs, partID );
1830 else if ( method == "ExportGMF" )
1831 { // ExportGMF(part,file,bool) -> ExportCGNS(file, part)
1832 _pyID partID = theCommand->GetArg( 1 );
1833 _AString file = theCommand->GetArg( 2 );
1834 theCommand->RemoveArgs();
1835 theCommand->SetArg( 1, file );
1836 theCommand->SetArg( 2, partID );
1838 else if ( theCommand->MethodStartsFrom( "ExportPartTo" ))
1839 { // ExportPartTo*(part, ...) -> Export*(..., part)
1841 // remove "PartTo" from the method
1842 TCollection_AsciiString newMethod = method;
1843 newMethod.Remove( 7, 6 );
1844 theCommand->SetMethod( newMethod );
1845 // make the 1st arg be the last one (or last but one for ExportMED())
1846 _pyID partID = theCommand->GetArg( 1 );
1847 int nbArgs = theCommand->GetNbArgs() - (newMethod == "ExportMED");
1848 for ( int i = 2; i <= nbArgs; ++i )
1849 theCommand->SetArg( i-1, theCommand->GetArg( i ));
1850 theCommand->SetArg( nbArgs, partID );
1852 // remember file name
1853 theGen->AddExportedMesh( theCommand->GetArg( 1 ),
1854 ExportedMeshData( this, myLastComputeCmd ));
1856 // ----------------------------------------------------------------------
1857 else if ( method == "RemoveHypothesis" ) // (geom, hyp)
1859 _pyID hypID = theCommand->GetArg( 2 );
1860 _pyID geomID = theCommand->GetArg( 1 );
1861 bool isLocal = ( geomID != GetGeom() );
1863 // check if this mesh still has corresponding addition command
1864 Handle(_pyCommand) addCmd;
1865 list< Handle(_pyCommand) >::iterator cmd;
1866 list< Handle(_pyCommand) >* addCmds[2] = { &myAddHypCmds, &myNotConvertedAddHypCmds };
1867 for ( int i = 0; i < 2; ++i )
1869 list< Handle(_pyCommand )> & addHypCmds = *(addCmds[i]);
1870 for ( cmd = addHypCmds.begin(); cmd != addHypCmds.end(); )
1872 bool sameHyp = true;
1873 if ( hypID != (*cmd)->GetArg( 1 ) && hypID != (*cmd)->GetArg( 2 ))
1874 sameHyp = false; // other hyp
1875 if ( (*cmd)->GetNbArgs() == 2 &&
1876 geomID != (*cmd)->GetArg( 1 ) && geomID != (*cmd)->GetArg( 2 ))
1877 sameHyp = false; // other geom
1878 if ( (*cmd)->GetNbArgs() == 1 && isLocal )
1879 sameHyp = false; // other geom
1883 cmd = addHypCmds.erase( cmd );
1884 if ( !theGen->IsToKeepAllCommands() ) {
1886 theCommand->Clear();
1895 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
1896 if ( !theCommand->IsEmpty() && !hypID.IsEmpty() ) {
1897 // RemoveHypothesis(geom, hyp) --> RemoveHypothesis( hyp, geom=0 )
1898 _pyID geom = theCommand->GetArg( 1 );
1899 theCommand->RemoveArgs();
1900 theCommand->SetArg( 1, hypID );
1901 if ( geom != GetGeom() )
1902 theCommand->SetArg( 2, geom );
1904 // remove hyp from myHypos
1905 myHypos.remove( hyp );
1907 // check for SubMesh order commands
1908 else if ( method == "GetMeshOrder" || method == "SetMeshOrder" )
1910 // make commands GetSubMesh() returning sub-meshes be before using sub-meshes
1911 // by GetMeshOrder() and SetMeshOrder(), since by defalut GetSubMesh()
1912 // commands are moved at the end of the script
1913 TCollection_AsciiString subIDs =
1914 ( method == "SetMeshOrder" ) ? theCommand->GetArg(1) : theCommand->GetResultValue();
1915 list< _pyID > idList = theCommand->GetStudyEntries( subIDs );
1916 list< _pyID >::iterator subID = idList.begin();
1917 for ( ; subID != idList.end(); ++subID )
1919 Handle(_pySubMesh) subMesh = theGen->FindSubMesh( *subID );
1920 if ( !subMesh.IsNull() )
1921 subMesh->Process( theCommand ); // it moves GetSubMesh() before theCommand
1924 // add accessor method if necessary
1927 if ( NeedMeshAccess( theCommand ))
1928 // apply theCommand to the mesh wrapped by smeshpy mesh
1929 AddMeshAccess( theCommand );
1933 //================================================================================
1935 * \brief Return True if addition of accesor method is needed
1937 //================================================================================
1939 bool _pyMesh::NeedMeshAccess( const Handle(_pyCommand)& theCommand )
1941 // names of SMESH_Mesh methods fully equal to methods of python class Mesh,
1942 // so no conversion is needed for them at all:
1943 static TStringSet sameMethods;
1944 if ( sameMethods.empty() ) {
1945 const char * names[] =
1946 { "ExportDAT","ExportUNV","ExportSTL","ExportSAUV", "RemoveGroup","RemoveGroupWithContents",
1947 "GetGroups","UnionGroups","IntersectGroups","CutGroups","GetLog","GetId","ClearLog",
1948 "GetStudyId","HasDuplicatedGroupNamesMED","GetMEDMesh","NbNodes","NbElements",
1949 "NbEdges","NbEdgesOfOrder","NbFaces","NbFacesOfOrder","NbTriangles",
1950 "NbTrianglesOfOrder","NbQuadrangles","NbQuadranglesOfOrder","NbPolygons","NbVolumes",
1951 "NbVolumesOfOrder","NbTetras","NbTetrasOfOrder","NbHexas","NbHexasOfOrder",
1952 "NbPyramids","NbPyramidsOfOrder","NbPrisms","NbPrismsOfOrder","NbPolyhedrons",
1953 "NbSubMesh","GetElementsId","GetElementsByType","GetNodesId","GetElementType",
1954 "GetSubMeshElementsId","GetSubMeshNodesId","GetSubMeshElementType","Dump","GetNodeXYZ",
1955 "GetNodeInverseElements","GetShapeID","GetShapeIDForElem","GetElemNbNodes",
1956 "GetElemNode","IsMediumNode","IsMediumNodeOfAnyElem","ElemNbEdges","ElemNbFaces",
1957 "IsPoly","IsQuadratic","BaryCenter","GetHypothesisList", "SetAutoColor", "GetAutoColor",
1958 "Clear", "ConvertToStandalone", "GetMeshOrder", "SetMeshOrder"
1959 ,"" }; // <- mark of end
1960 sameMethods.Insert( names );
1963 return !sameMethods.Contains( theCommand->GetMethod() );
1966 //================================================================================
1968 * \brief Convert creation and addition of all algos and hypos
1970 //================================================================================
1972 void _pyMesh::Flush()
1975 // get the meshes this mesh depends on via hypotheses
1976 list< Handle(_pyMesh) > fatherMeshes;
1977 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
1978 for ( ; hyp != myHypos.end(); ++hyp )
1979 if ( ! (*hyp)->GetReferredMeshesAndGeom( fatherMeshes ))
1980 myGeomNotInStudy = true;
1982 list< Handle(_pyMesh) >::iterator m = fatherMeshes.begin();
1983 for ( ; m != fatherMeshes.end(); ++m )
1984 addFatherMesh( *m );
1985 // if ( removedGeom )
1986 // SetRemovedFromStudy(); // as reffered geometry not in study
1988 if ( myGeomNotInStudy )
1991 list < Handle(_pyCommand) >::iterator cmd;
1993 // try to convert algo addition like this:
1994 // mesh.AddHypothesis(geom, ALGO ) --> ALGO = mesh.Algo()
1995 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
1997 Handle(_pyCommand) addCmd = *cmd;
1999 _pyID algoID = addCmd->GetArg( 2 );
2000 Handle(_pyHypothesis) algo = theGen->FindHyp( algoID );
2001 if ( algo.IsNull() || !algo->IsAlgo() )
2004 // check and create new algorithm instance if it is already wrapped
2005 if ( algo->IsWrapped() ) {
2006 _pyID localAlgoID = theGen->GenerateNewID( algoID );
2007 TCollection_AsciiString aNewCmdStr = addCmd->GetIndentation() + localAlgoID +
2008 TCollection_AsciiString( " = " ) + theGen->GetID() +
2009 TCollection_AsciiString( ".CreateHypothesis( \"" ) + algo->GetAlgoType() +
2010 TCollection_AsciiString( "\" )" );
2012 Handle(_pyCommand) newCmd = theGen->AddCommand( aNewCmdStr );
2013 Handle(_pyAlgorithm) newAlgo = Handle(_pyAlgorithm)::DownCast(theGen->FindHyp( localAlgoID ));
2014 if ( !newAlgo.IsNull() ) {
2015 newAlgo->Assign( algo, this->GetID() );
2016 newAlgo->SetCreationCmd( newCmd );
2018 // set algorithm creation
2019 theGen->SetCommandBefore( newCmd, addCmd );
2020 myHypos.push_back( newAlgo );
2021 if ( !myLastComputeCmd.IsNull() &&
2022 newCmd->GetOrderNb() == myLastComputeCmd->GetOrderNb() + 1)
2023 newAlgo->MeshComputed( myLastComputeCmd );
2028 _pyID geom = addCmd->GetArg( 1 );
2029 bool isLocalAlgo = ( geom != GetGeom() );
2032 if ( algo->Addition2Creation( addCmd, this->GetID() )) // OK
2034 // wrapped algo is created after mesh creation
2035 GetCreationCmd()->AddDependantCmd( addCmd );
2037 if ( isLocalAlgo ) {
2038 // mesh.AddHypothesis(geom, ALGO ) --> mesh.AlgoMethod(geom)
2039 addCmd->SetArg( addCmd->GetNbArgs() + 1,
2040 TCollection_AsciiString( "geom=" ) + geom );
2041 // sm = mesh.GetSubMesh(geom, name) --> sm = ALGO.GetSubMesh()
2042 list < Handle(_pySubMesh) >::iterator smIt;
2043 for ( smIt = mySubmeshes.begin(); smIt != mySubmeshes.end(); ++smIt ) {
2044 Handle(_pySubMesh) subMesh = *smIt;
2045 Handle(_pyCommand) subCmd = subMesh->GetCreationCmd();
2046 if ( geom == subCmd->GetArg( 1 )) {
2047 subCmd->SetObject( algo->GetID() );
2048 subCmd->RemoveArgs();
2049 subMesh->SetCreator( algo );
2054 else // KO - ALGO was already created
2056 // mesh.AddHypothesis(geom, ALGO) --> mesh.AddHypothesis(ALGO, geom=0)
2057 addCmd->RemoveArgs();
2058 addCmd->SetArg( 1, algoID );
2060 addCmd->SetArg( 2, geom );
2061 myNotConvertedAddHypCmds.push_back( addCmd );
2065 // try to convert hypo addition like this:
2066 // mesh.AddHypothesis(geom, HYPO ) --> HYPO = algo.Hypo()
2067 for ( cmd = myAddHypCmds.begin(); cmd != myAddHypCmds.end(); ++cmd )
2069 Handle(_pyCommand) addCmd = *cmd;
2070 _pyID hypID = addCmd->GetArg( 2 );
2071 Handle(_pyHypothesis) hyp = theGen->FindHyp( hypID );
2072 if ( hyp.IsNull() || hyp->IsAlgo() )
2074 bool converted = hyp->Addition2Creation( addCmd, this->GetID() );
2076 // mesh.AddHypothesis(geom, HYP) --> mesh.AddHypothesis(HYP, geom=0)
2077 _pyID geom = addCmd->GetArg( 1 );
2078 addCmd->RemoveArgs();
2079 addCmd->SetArg( 1, hypID );
2080 if ( geom != GetGeom() )
2081 addCmd->SetArg( 2, geom );
2082 myNotConvertedAddHypCmds.push_back( addCmd );
2086 myAddHypCmds.clear();
2087 mySubmeshes.clear();
2090 list< Handle(_pyHypothesis) >::iterator hyp = myHypos.begin();
2091 for ( hyp = myHypos.begin(); hyp != myHypos.end(); ++hyp )
2095 //================================================================================
2097 * \brief Sets myIsPublished of me and of all objects depending on me.
2099 //================================================================================
2101 void _pyMesh::SetRemovedFromStudy(const bool isRemoved)
2103 _pyObject::SetRemovedFromStudy(isRemoved);
2105 list< Handle(_pySubMesh) >::iterator sm = mySubmeshes.begin();
2106 for ( ; sm != mySubmeshes.end(); ++sm )
2107 (*sm)->SetRemovedFromStudy(isRemoved);
2109 list< Handle(_pyGroup) >::iterator gr = myGroups.begin();
2110 for ( ; gr != myGroups.end(); ++gr )
2111 (*gr)->SetRemovedFromStudy(isRemoved);
2113 list< Handle(_pyMesh) >::iterator m = myChildMeshes.begin();
2114 for ( ; m != myChildMeshes.end(); ++m )
2115 (*m)->SetRemovedFromStudy(isRemoved);
2117 list< Handle(_pyMeshEditor)>::iterator e = myEditors.begin();
2118 for ( ; e != myEditors.end(); ++e )
2119 (*e)->SetRemovedFromStudy(isRemoved);
2122 //================================================================================
2124 * \brief Return true if none of myChildMeshes is in study
2126 //================================================================================
2128 bool _pyMesh::CanClear()
2133 list< Handle(_pyMesh) >::iterator m = myChildMeshes.begin();
2134 for ( ; m != myChildMeshes.end(); ++m )
2135 if ( !(*m)->CanClear() )
2141 //================================================================================
2143 * \brief Clear my commands and commands of mesh editor
2145 //================================================================================
2147 void _pyMesh::ClearCommands()
2153 // mark all sub-objects as not removed, except child meshes
2154 list< Handle(_pyMesh) > children;
2155 children.swap( myChildMeshes );
2156 SetRemovedFromStudy( false );
2157 children.swap( myChildMeshes );
2161 _pyObject::ClearCommands();
2163 list< Handle(_pySubMesh) >::iterator sm = mySubmeshes.begin();
2164 for ( ; sm != mySubmeshes.end(); ++sm )
2165 (*sm)->ClearCommands();
2167 list< Handle(_pyGroup) >::iterator gr = myGroups.begin();
2168 for ( ; gr != myGroups.end(); ++gr )
2169 (*gr)->ClearCommands();
2171 list< Handle(_pyMeshEditor)>::iterator e = myEditors.begin();
2172 for ( ; e != myEditors.end(); ++e )
2173 (*e)->ClearCommands();
2176 //================================================================================
2178 * \brief Add a father mesh by ID
2180 //================================================================================
2182 void _pyMesh::addFatherMesh( const _pyID& meshID )
2184 if ( !meshID.IsEmpty() && meshID != GetID() )
2185 addFatherMesh( Handle(_pyMesh)::DownCast( theGen->FindObject( meshID )));
2188 //================================================================================
2190 * \brief Add a father mesh
2192 //================================================================================
2194 void _pyMesh::addFatherMesh( const Handle(_pyMesh)& mesh )
2196 if ( !mesh.IsNull() && mesh->GetID() != GetID() )
2198 //myFatherMeshes.push_back( mesh );
2199 mesh->myChildMeshes.push_back( this );
2201 // protect last Compute() from clearing by the next Compute()
2202 mesh->myLastComputeCmd.Nullify();
2206 //================================================================================
2208 * \brief MeshEditor convert its commands to ones of mesh
2210 //================================================================================
2212 _pyMeshEditor::_pyMeshEditor(const Handle(_pyCommand)& theCreationCmd):
2213 _pyObject( theCreationCmd )
2215 myMesh = theCreationCmd->GetObject();
2216 myCreationCmdStr = theCreationCmd->GetString();
2217 theCreationCmd->Clear();
2219 Handle(_pyMesh) mesh = ObjectToMesh( theGen->FindObject( myMesh ));
2220 if ( !mesh.IsNull() )
2221 mesh->AddEditor( this );
2224 //================================================================================
2226 * \brief convert its commands to ones of mesh
2228 //================================================================================
2230 void _pyMeshEditor::Process( const Handle(_pyCommand)& theCommand)
2232 // Names of SMESH_MeshEditor methods fully equal to methods of the python class Mesh, so
2233 // commands calling these methods are converted to calls of Mesh methods without
2234 // additional modifs, only object is changed from MeshEditor to Mesh.
2235 static TStringSet sameMethods;
2236 if ( sameMethods.empty() ) {
2237 const char * names[] = {
2238 "RemoveElements","RemoveNodes","RemoveOrphanNodes",
2239 "AddNode","Add0DElement","AddEdge","AddFace","AddPolygonalFace","AddBall",
2240 "AddVolume","AddPolyhedralVolume","AddPolyhedralVolumeByFaces",
2241 "MoveNode", "MoveClosestNodeToPoint",
2242 "InverseDiag","DeleteDiag","Reorient","ReorientObject",
2243 "TriToQuad","TriToQuadObject", "QuadTo4Tri", "SplitQuad","SplitQuadObject",
2244 "BestSplit","Smooth","SmoothObject","SmoothParametric","SmoothParametricObject",
2245 "ConvertToQuadratic","ConvertFromQuadratic","RenumberNodes","RenumberElements",
2246 "RotationSweep","RotationSweepObject","RotationSweepObject1D","RotationSweepObject2D",
2247 "ExtrusionSweep","AdvancedExtrusion","ExtrusionSweepObject","ExtrusionSweepObject1D",
2248 "ExtrusionSweepObject2D","ExtrusionAlongPath","ExtrusionAlongPathObject",
2249 "ExtrusionAlongPathX","ExtrusionAlongPathObject1D","ExtrusionAlongPathObject2D",
2250 "Mirror","MirrorObject","Translate","TranslateObject","Rotate","RotateObject",
2251 "FindCoincidentNodes","MergeNodes","FindEqualElements",
2252 "MergeElements","MergeEqualElements","SewFreeBorders","SewConformFreeBorders",
2253 "SewBorderToSide","SewSideElements","ChangeElemNodes","GetLastCreatedNodes",
2254 "GetLastCreatedElems",
2255 "MirrorMakeMesh","MirrorObjectMakeMesh","TranslateMakeMesh","TranslateObjectMakeMesh",
2256 "Scale","ScaleMakeMesh","RotateMakeMesh","RotateObjectMakeMesh","MakeBoundaryMesh",
2257 "MakeBoundaryElements", "SplitVolumesIntoTetra",
2258 "DoubleElements","DoubleNodes","DoubleNode","DoubleNodeGroup","DoubleNodeGroups",
2259 "DoubleNodeElem","DoubleNodeElemInRegion","DoubleNodeElemGroup",
2260 "DoubleNodeElemGroupInRegion","DoubleNodeElemGroups","DoubleNodeElemGroupsInRegion",
2261 "DoubleNodesOnGroupBoundaries","CreateFlatElementsOnFacesGroups","CreateHoleSkin"
2262 ,"" }; // <- mark of the end
2263 sameMethods.Insert( names );
2266 // names of SMESH_MeshEditor commands in which only a method name must be replaced
2267 TStringMap diffMethods;
2268 if ( diffMethods.empty() ) {
2269 const char * orig2newName[] = {
2270 // original name --------------> new name
2271 "ExtrusionAlongPathObjX" , "ExtrusionAlongPathX",
2272 "FindCoincidentNodesOnPartBut", "FindCoincidentNodesOnPart",
2273 "ConvertToQuadraticObject" , "ConvertToQuadratic",
2274 "ConvertFromQuadraticObject" , "ConvertFromQuadratic",
2275 "Create0DElementsOnAllNodes" , "Add0DElementsToAllNodes",
2276 ""};// <- mark of the end
2277 diffMethods.Insert( orig2newName );
2280 // names of SMESH_MeshEditor methods which differ from methods of Mesh class
2281 // only by last two arguments
2282 static TStringSet diffLastTwoArgsMethods;
2283 if (diffLastTwoArgsMethods.empty() ) {
2284 const char * names[] = {
2285 "MirrorMakeGroups","MirrorObjectMakeGroups",
2286 "TranslateMakeGroups","TranslateObjectMakeGroups","ScaleMakeGroups",
2287 "RotateMakeGroups","RotateObjectMakeGroups",
2288 ""};// <- mark of the end
2289 diffLastTwoArgsMethods.Insert( names );
2292 // only a method name is to change?
2293 const TCollection_AsciiString & method = theCommand->GetMethod();
2294 bool isPyMeshMethod = sameMethods.Contains( method );
2295 if ( !isPyMeshMethod )
2297 TCollection_AsciiString newMethod = diffMethods.Value( method );
2298 if (( isPyMeshMethod = ( newMethod.Length() > 0 )))
2299 theCommand->SetMethod( newMethod );
2301 // ConvertToBiQuadratic(...) -> ConvertToQuadratic(...,True)
2302 if ( !isPyMeshMethod && (method == "ConvertToBiQuadratic" || method == "ConvertToBiQuadraticObject") )
2304 isPyMeshMethod = true;
2305 theCommand->SetMethod( method.SubString( 1, 9) + method.SubString( 12, method.Length()));
2306 theCommand->SetArg( theCommand->GetNbArgs() + 1, "True" );
2309 if ( !isPyMeshMethod )
2311 // Replace SMESH_MeshEditor "*MakeGroups" functions by the Mesh
2312 // functions with the flag "theMakeGroups = True" like:
2313 // SMESH_MeshEditor.CmdMakeGroups => Mesh.Cmd(...,True)
2314 int pos = method.Search("MakeGroups");
2317 isPyMeshMethod = true;
2318 bool is0DmethId = ( method == "ExtrusionSweepMakeGroups0D" );
2319 bool is0DmethObj = ( method == "ExtrusionSweepObject0DMakeGroups");
2321 // 1. Remove "MakeGroups" from the Command
2322 TCollection_AsciiString aMethod = theCommand->GetMethod();
2323 int nbArgsToAdd = diffLastTwoArgsMethods.Contains(aMethod) ? 2 : 1;
2326 pos = pos-2; //Remove "0D" from the Command too
2327 aMethod.Trunc(pos-1);
2328 theCommand->SetMethod(aMethod);
2330 // 2. And add last "True" argument(s)
2331 while(nbArgsToAdd--)
2332 theCommand->SetArg(theCommand->GetNbArgs()+1,"True");
2333 if( is0DmethId || is0DmethObj )
2334 theCommand->SetArg(theCommand->GetNbArgs()+1,"True");
2338 // ExtrusionSweep0D() -> ExtrusionSweep()
2339 // ExtrusionSweepObject0D() -> ExtrusionSweepObject()
2340 if ( !isPyMeshMethod && ( method == "ExtrusionSweep0D" ||
2341 method == "ExtrusionSweepObject0D" ))
2343 isPyMeshMethod = true;
2344 theCommand->SetMethod( method.SubString( 1, method.Length()-2));
2345 theCommand->SetArg(theCommand->GetNbArgs()+1,"False"); //sets flag "MakeGroups = False"
2346 theCommand->SetArg(theCommand->GetNbArgs()+1,"True"); //sets flag "IsNode = True"
2349 // DoubleNode...New(...) -> DoubleNode...(...,True)
2350 if ( !isPyMeshMethod && ( method == "DoubleNodeElemGroupNew" ||
2351 method == "DoubleNodeElemGroupsNew" ||
2352 method == "DoubleNodeGroupNew" ||
2353 method == "DoubleNodeGroupsNew" ||
2354 method == "DoubleNodeElemGroup2New" ||
2355 method == "DoubleNodeElemGroups2New"))
2357 isPyMeshMethod = true;
2358 const int excessLen = 3 + int( method.Value( method.Length()-3 ) == '2' );
2359 theCommand->SetMethod( method.SubString( 1, method.Length()-excessLen));
2360 if ( excessLen == 3 )
2362 theCommand->SetArg(theCommand->GetNbArgs()+1,"True");
2364 else if ( theCommand->GetArg(4) == "0" ||
2365 theCommand->GetArg(5) == "0" )
2367 // [ nothing, Group ] = DoubleNodeGroup2New(,,,False, True) ->
2368 // Group = DoubleNodeGroup2New(,,,False, True)
2369 _pyID groupID = theCommand->GetResultValue( 1 + int( theCommand->GetArg(4) == "0"));
2370 theCommand->SetResultValue( groupID );
2373 // FindAmongElementsByPoint(meshPart, x, y, z, elementType) ->
2374 // FindElementsByPoint(x, y, z, elementType, meshPart)
2375 if ( !isPyMeshMethod && method == "FindAmongElementsByPoint" )
2377 isPyMeshMethod = true;
2378 theCommand->SetMethod( "FindElementsByPoint" );
2379 // make the 1st arg be the last one
2380 _pyID partID = theCommand->GetArg( 1 );
2381 int nbArgs = theCommand->GetNbArgs();
2382 for ( int i = 2; i <= nbArgs; ++i )
2383 theCommand->SetArg( i-1, theCommand->GetArg( i ));
2384 theCommand->SetArg( nbArgs, partID );
2386 // Reorient2D( mesh, dir, face, point ) -> Reorient2D( mesh, dir, faceORpoint )
2387 if ( !isPyMeshMethod && method == "Reorient2D" )
2389 isPyMeshMethod = true;
2390 _AString mesh = theCommand->GetArg( 1 );
2391 _AString dir = theCommand->GetArg( 2 );
2392 _AString face = theCommand->GetArg( 3 );
2393 _AString point = theCommand->GetArg( 4 );
2394 theCommand->RemoveArgs();
2395 theCommand->SetArg( 1, mesh );
2396 theCommand->SetArg( 2, dir );
2397 if ( face.Value(1) == '-' || face.Value(1) == '0' ) // invalid: face <= 0
2398 theCommand->SetArg( 3, point );
2400 theCommand->SetArg( 3, face );
2403 if ( method == "QuadToTri" || method == "QuadToTriObject" )
2405 isPyMeshMethod = true;
2406 int crit_arg = theCommand->GetNbArgs();
2407 const _AString& crit = theCommand->GetArg(crit_arg);
2408 if (crit.Search("MaxElementLength2D") != -1)
2409 theCommand->SetArg(crit_arg, "");
2412 if ( isPyMeshMethod )
2414 theCommand->SetObject( myMesh );
2418 // editor creation command is needed only if any editor function is called
2419 theGen->AddMeshAccessorMethod( theCommand ); // for *Object() methods
2420 if ( !myCreationCmdStr.IsEmpty() ) {
2421 GetCreationCmd()->GetString() = myCreationCmdStr;
2422 myCreationCmdStr.Clear();
2427 //================================================================================
2429 * \brief Return true if my mesh can be removed
2431 //================================================================================
2433 bool _pyMeshEditor::CanClear()
2435 Handle(_pyMesh) mesh = ObjectToMesh( theGen->FindObject( myMesh ));
2436 return mesh.IsNull() ? true : mesh->CanClear();
2439 //================================================================================
2441 * \brief _pyHypothesis constructor
2442 * \param theCreationCmd -
2444 //================================================================================
2446 _pyHypothesis::_pyHypothesis(const Handle(_pyCommand)& theCreationCmd):
2447 _pyObject( theCreationCmd ), myCurCrMethod(0)
2449 myIsAlgo = myIsWrapped = /*myIsConverted = myIsLocal = myDim = */false;
2452 //================================================================================
2454 * \brief Creates algorithm or hypothesis
2455 * \param theCreationCmd - The engine command creating a hypothesis
2456 * \retval Handle(_pyHypothesis) - Result _pyHypothesis
2458 //================================================================================
2460 Handle(_pyHypothesis) _pyHypothesis::NewHypothesis( const Handle(_pyCommand)& theCreationCmd)
2462 // theCreationCmd: CreateHypothesis( "theHypType", "theLibName" )
2463 ASSERT (( theCreationCmd->GetMethod() == "CreateHypothesis"));
2465 Handle(_pyHypothesis) hyp, algo;
2468 const TCollection_AsciiString & hypTypeQuoted = theCreationCmd->GetArg( 1 );
2469 if ( hypTypeQuoted.IsEmpty() )
2472 TCollection_AsciiString hypType =
2473 hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
2475 algo = new _pyAlgorithm( theCreationCmd );
2476 hyp = new _pyHypothesis( theCreationCmd );
2478 if ( hypType == "NumberOfSegments" ) {
2479 hyp = new _pyNumberOfSegmentsHyp( theCreationCmd );
2480 hyp->SetConvMethodAndType( "NumberOfSegments", "Regular_1D");
2481 // arg of SetNumberOfSegments() will become the 1-st arg of hyp creation command
2482 hyp->AddArgMethod( "SetNumberOfSegments" );
2483 // arg of SetScaleFactor() will become the 2-nd arg of hyp creation command
2484 hyp->AddArgMethod( "SetScaleFactor" );
2485 hyp->AddArgMethod( "SetReversedEdges" );
2486 // same for ""CompositeSegment_1D:
2487 hyp->SetConvMethodAndType( "NumberOfSegments", "CompositeSegment_1D");
2488 hyp->AddArgMethod( "SetNumberOfSegments" );
2489 hyp->AddArgMethod( "SetScaleFactor" );
2490 hyp->AddArgMethod( "SetReversedEdges" );
2492 else if ( hypType == "SegmentLengthAroundVertex" ) {
2493 hyp = new _pySegmentLengthAroundVertexHyp( theCreationCmd );
2494 hyp->SetConvMethodAndType( "LengthNearVertex", "Regular_1D" );
2495 hyp->AddArgMethod( "SetLength" );
2496 // same for ""CompositeSegment_1D:
2497 hyp->SetConvMethodAndType( "LengthNearVertex", "CompositeSegment_1D");
2498 hyp->AddArgMethod( "SetLength" );
2500 else if ( hypType == "LayerDistribution2D" ) {
2501 hyp = new _pyLayerDistributionHypo( theCreationCmd, "Get2DHypothesis" );
2502 hyp->SetConvMethodAndType( "LayerDistribution", "RadialQuadrangle_1D2D");
2504 else if ( hypType == "LayerDistribution" ) {
2505 hyp = new _pyLayerDistributionHypo( theCreationCmd, "Get3DHypothesis" );
2506 hyp->SetConvMethodAndType( "LayerDistribution", "RadialPrism_3D");
2508 else if ( hypType == "CartesianParameters3D" ) {
2509 hyp = new _pyComplexParamHypo( theCreationCmd );
2510 hyp->SetConvMethodAndType( "SetGrid", "Cartesian_3D");
2511 for ( int iArg = 0; iArg < 4; ++iArg )
2512 hyp->setCreationArg( iArg+1, "[]");
2516 hyp = theGen->GetHypothesisReader()->GetHypothesis( hypType, theCreationCmd );
2519 return algo->IsValid() ? algo : hyp;
2522 //================================================================================
2524 * \brief Returns true if addition of this hypothesis to a given mesh can be
2525 * wrapped into hypothesis creation
2527 //================================================================================
2529 bool _pyHypothesis::IsWrappable(const _pyID& theMesh) const
2531 if ( !myIsWrapped && myMesh == theMesh && IsInStudy() )
2533 Handle(_pyObject) pyMesh = theGen->FindObject( myMesh );
2534 if ( !pyMesh.IsNull() && pyMesh->IsInStudy() )
2540 //================================================================================
2542 * \brief Convert the command adding a hypothesis to mesh into a smesh command
2543 * \param theCmd - The command like mesh.AddHypothesis( geom, hypo )
2544 * \param theAlgo - The algo that can create this hypo
2545 * \retval bool - false if the command cant be converted
2547 //================================================================================
2549 bool _pyHypothesis::Addition2Creation( const Handle(_pyCommand)& theCmd,
2550 const _pyID& theMesh)
2552 ASSERT(( theCmd->GetMethod() == "AddHypothesis" ));
2554 if ( !IsWrappable( theMesh ))
2557 myGeom = theCmd->GetArg( 1 );
2559 Handle(_pyHypothesis) algo;
2561 // find algo created on myGeom in theMesh
2562 algo = theGen->FindAlgo( myGeom, theMesh, this );
2563 if ( algo.IsNull() )
2565 // attach hypothesis creation command to be after algo creation command
2566 // because it can be new created instance of algorithm
2567 algo->GetCreationCmd()->AddDependantCmd( theCmd );
2571 // mesh.AddHypothesis(geom,hyp) --> hyp = <theMesh or algo>.myCreationMethod(args)
2572 theCmd->SetResultValue( GetID() );
2573 theCmd->SetObject( IsAlgo() ? theMesh : algo->GetID());
2574 theCmd->SetMethod( IsAlgo() ? GetAlgoCreationMethod() : GetCreationMethod( algo->GetAlgoType() ));
2575 // set args (geom will be set by _pyMesh calling this method)
2576 theCmd->RemoveArgs();
2577 for ( size_t i = 0; i < myCurCrMethod->myArgs.size(); ++i ) {
2578 if ( !myCurCrMethod->myArgs[ i ].IsEmpty() )
2579 theCmd->SetArg( i+1, myCurCrMethod->myArgs[ i ]);
2581 theCmd->SetArg( i+1, "[]");
2583 // set a new creation command
2584 GetCreationCmd()->Clear();
2585 // replace creation command by wrapped instance
2586 // please note, that hypothesis attaches to algo creation command (see upper)
2587 SetCreationCmd( theCmd );
2590 // clear commands setting arg values
2591 list < Handle(_pyCommand) >::iterator argCmd = myArgCommands.begin();
2592 for ( ; argCmd != myArgCommands.end(); ++argCmd )
2595 // set unknown arg commands after hypo creation
2596 Handle(_pyCommand) afterCmd = myIsWrapped ? theCmd : GetCreationCmd();
2597 list<Handle(_pyCommand)>::iterator cmd = myUnusedCommands.begin();
2598 for ( ; cmd != myUnusedCommands.end(); ++cmd ) {
2599 afterCmd->AddDependantCmd( *cmd );
2605 //================================================================================
2607 * \brief Remember hypothesis parameter values
2608 * \param theCommand - The called hypothesis method
2610 //================================================================================
2612 void _pyHypothesis::Process( const Handle(_pyCommand)& theCommand)
2614 ASSERT( !myIsAlgo );
2615 if ( !theGen->IsToKeepAllCommands() )
2616 rememberCmdOfParameter( theCommand );
2618 bool usedCommand = false;
2619 TType2CrMethod::iterator type2meth = myAlgoType2CreationMethod.begin();
2620 for ( ; type2meth != myAlgoType2CreationMethod.end(); ++type2meth )
2622 CreationMethod& crMethod = type2meth->second;
2623 for ( size_t i = 0; i < crMethod.myArgMethods.size(); ++i ) {
2624 if ( crMethod.myArgMethods[ i ] == theCommand->GetMethod() ) {
2626 myArgCommands.push_back( theCommand );
2628 while ( crMethod.myArgs.size() < i+1 )
2629 crMethod.myArgs.push_back( "[]" );
2630 crMethod.myArgs[ i ] = theCommand->GetArg( crMethod.myArgNb[i] );
2635 myUnusedCommands.push_back( theCommand );
2638 //================================================================================
2640 * \brief Finish conversion
2642 //================================================================================
2644 void _pyHypothesis::Flush()
2648 list < Handle(_pyCommand) >::iterator cmd = myArgCommands.begin();
2649 for ( ; cmd != myArgCommands.end(); ++cmd ) {
2650 // Add access to a wrapped mesh
2651 theGen->AddMeshAccessorMethod( *cmd );
2652 // Add access to a wrapped algorithm
2653 theGen->AddAlgoAccessorMethod( *cmd );
2655 cmd = myUnusedCommands.begin();
2656 for ( ; cmd != myUnusedCommands.end(); ++cmd ) {
2657 // Add access to a wrapped mesh
2658 theGen->AddMeshAccessorMethod( *cmd );
2659 // Add access to a wrapped algorithm
2660 theGen->AddAlgoAccessorMethod( *cmd );
2663 // forget previous hypothesis modifications
2664 myArgCommands.clear();
2665 myUnusedCommands.clear();
2668 //================================================================================
2670 * \brief clear creation, arg and unkown commands
2672 //================================================================================
2674 void _pyHypothesis::ClearAllCommands()
2676 GetCreationCmd()->Clear();
2677 list<Handle(_pyCommand)>::iterator cmd = myArgCommands.begin();
2678 for ( ; cmd != myArgCommands.end(); ++cmd )
2680 cmd = myUnusedCommands.begin();
2681 for ( ; cmd != myUnusedCommands.end(); ++cmd )
2686 //================================================================================
2688 * \brief Assign fields of theOther to me except myIsWrapped
2690 //================================================================================
2692 void _pyHypothesis::Assign( const Handle(_pyHypothesis)& theOther,
2693 const _pyID& theMesh )
2695 // myCreationCmd = theOther->myCreationCmd;
2696 myIsAlgo = theOther->myIsAlgo;
2697 myIsWrapped = false;
2698 myGeom = theOther->myGeom;
2700 myAlgoType2CreationMethod = theOther->myAlgoType2CreationMethod;
2701 myAccumulativeMethods = theOther->myAccumulativeMethods;
2702 //myUnusedCommands = theOther->myUnusedCommands;
2703 // init myCurCrMethod
2704 GetCreationMethod( theOther->GetAlgoType() );
2707 //================================================================================
2709 * \brief Analyze my erasability depending on myReferredObjs
2711 //================================================================================
2713 bool _pyHypothesis::CanClear()
2717 list< Handle(_pyObject) >::iterator obj = myReferredObjs.begin();
2718 for ( ; obj != myReferredObjs.end(); ++obj )
2719 if ( (*obj)->CanClear() )
2726 //================================================================================
2728 * \brief Clear my commands depending on usage by meshes
2730 //================================================================================
2732 void _pyHypothesis::ClearCommands()
2734 // if ( !theGen->IsToKeepAllCommands() )
2736 // bool isUsed = false;
2737 // int lastComputeOrder = 0;
2738 // list<Handle(_pyCommand) >::iterator cmd = myComputeCmds.begin();
2739 // for ( ; cmd != myComputeCmds.end(); ++cmd )
2740 // if ( ! (*cmd)->IsEmpty() )
2743 // if ( (*cmd)->GetOrderNb() > lastComputeOrder )
2744 // lastComputeOrder = (*cmd)->GetOrderNb();
2748 // SetRemovedFromStudy( true );
2752 // // clear my commands invoked after lastComputeOrder
2753 // // map<TCollection_AsciiString, list< Handle(_pyCommand) > >::iterator m2c;
2754 // // for ( m2c = myMeth2Commands.begin(); m2c != myMeth2Commands.end(); ++m2c )
2756 // // list< Handle(_pyCommand)> & cmds = m2c->second;
2757 // // if ( !cmds.empty() && cmds.back()->GetOrderNb() > lastComputeOrder )
2758 // // cmds.back()->Clear();
2762 _pyObject::ClearCommands();
2765 //================================================================================
2767 * \brief Find arguments that are objects like mesh, group, geometry
2768 * \param meshes - referred meshes (directly or indirrectly)
2769 * \retval bool - false if a referred geometry is not in the study
2771 //================================================================================
2773 bool _pyHypothesis::GetReferredMeshesAndGeom( list< Handle(_pyMesh) >& meshes )
2775 if ( IsAlgo() ) return true;
2777 bool geomPublished = true;
2778 vector< _AString > args;
2779 TType2CrMethod::iterator type2meth = myAlgoType2CreationMethod.begin();
2780 for ( ; type2meth != myAlgoType2CreationMethod.end(); ++type2meth )
2782 CreationMethod& crMethod = type2meth->second;
2783 args.insert( args.end(), crMethod.myArgs.begin(), crMethod.myArgs.end());
2785 list<Handle(_pyCommand)>::iterator cmd = myUnusedCommands.begin();
2786 for ( ; cmd != myUnusedCommands.end(); ++cmd ) {
2787 for ( int nb = (*cmd)->GetNbArgs(); nb; --nb )
2788 args.push_back( (*cmd)->GetArg( nb ));
2791 for ( size_t i = 0; i < args.size(); ++i )
2793 list< _pyID > idList = _pyCommand::GetStudyEntries( args[ i ]);
2794 if ( idList.empty() && !args[ i ].IsEmpty() )
2795 idList.push_back( args[ i ]);
2796 list< _pyID >::iterator id = idList.begin();
2797 for ( ; id != idList.end(); ++id )
2799 Handle(_pyObject) obj = theGen->FindObject( *id );
2800 if ( obj.IsNull() ) obj = theGen->FindHyp( *id );
2803 if ( theGen->IsGeomObject( *id ) && theGen->IsNotPublished( *id ))
2804 geomPublished = false;
2808 myReferredObjs.push_back( obj );
2809 Handle(_pyMesh) mesh = ObjectToMesh( obj );
2810 if ( !mesh.IsNull() )
2811 meshes.push_back( mesh );
2812 // prevent clearing not published hyps referred e.g. by "LayerDistribution"
2813 else if ( obj->IsKind( STANDARD_TYPE( _pyHypothesis )) && this->IsInStudy() )
2814 obj->SetRemovedFromStudy( false );
2818 return geomPublished;
2821 //================================================================================
2823 * \brief Remember theCommand setting a parameter
2825 //================================================================================
2827 void _pyHypothesis::rememberCmdOfParameter( const Handle(_pyCommand) & theCommand )
2829 // parameters are discriminated by method name
2830 _AString method = theCommand->GetMethod();
2831 if ( myAccumulativeMethods.count( method ))
2832 return; // this method adds values and not override the previus value
2834 // discriminate commands setting different parameters via one method
2835 // by passing parameter names like e.g. SetOption("size", "0.2")
2836 if ( theCommand->GetString().FirstLocationInSet( "'\"", 1, theCommand->Length() ) &&
2837 theCommand->GetNbArgs() > 1 )
2839 // mangle method by appending a 1st textual arg
2840 for ( int iArg = 1; iArg <= theCommand->GetNbArgs(); ++iArg )
2842 const TCollection_AsciiString& arg = theCommand->GetArg( iArg );
2843 if ( arg.Value(1) != '\"' && arg.Value(1) != '\'' ) continue;
2844 if ( !isalpha( arg.Value(2))) continue;
2849 // parameters are discriminated by method name
2850 list< Handle(_pyCommand)>& cmds = myMeth2Commands[ method /*theCommand->GetMethod()*/ ];
2851 if ( !cmds.empty() && !isCmdUsedForCompute( cmds.back() ))
2853 cmds.back()->Clear(); // previous parameter value has not been used
2854 cmds.back() = theCommand;
2858 cmds.push_back( theCommand );
2862 //================================================================================
2864 * \brief Return true if a setting parameter command ha been used to compute mesh
2866 //================================================================================
2868 bool _pyHypothesis::isCmdUsedForCompute( const Handle(_pyCommand) & cmd,
2869 _pyCommand::TAddr avoidComputeAddr ) const
2871 bool isUsed = false;
2872 map< _pyCommand::TAddr, list<Handle(_pyCommand) > >::const_iterator addr2cmds =
2873 myComputeAddr2Cmds.begin();
2874 for ( ; addr2cmds != myComputeAddr2Cmds.end() && !isUsed; ++addr2cmds )
2876 if ( addr2cmds->first == avoidComputeAddr ) continue;
2877 const list<Handle(_pyCommand)> & cmds = addr2cmds->second;
2878 isUsed = ( std::find( cmds.begin(), cmds.end(), cmd ) != cmds.end() );
2883 //================================================================================
2885 * \brief Save commands setting parameters as they are used for a mesh computation
2887 //================================================================================
2889 void _pyHypothesis::MeshComputed( const Handle(_pyCommand)& theComputeCmd )
2891 myComputeCmds.push_back( theComputeCmd );
2892 list<Handle(_pyCommand)>& savedCmds = myComputeAddr2Cmds[ theComputeCmd->GetAddress() ];
2894 map<TCollection_AsciiString, list< Handle(_pyCommand) > >::iterator m2c;
2895 for ( m2c = myMeth2Commands.begin(); m2c != myMeth2Commands.end(); ++m2c )
2896 savedCmds.push_back( m2c->second.back() );
2899 //================================================================================
2901 * \brief Clear commands setting parameters as a mesh computed using them is cleared
2903 //================================================================================
2905 void _pyHypothesis::ComputeDiscarded( const Handle(_pyCommand)& theComputeCmd )
2907 list<Handle(_pyCommand)>& savedCmds = myComputeAddr2Cmds[ theComputeCmd->GetAddress() ];
2909 list<Handle(_pyCommand)>::iterator cmd = savedCmds.begin();
2910 for ( ; cmd != savedCmds.end(); ++cmd )
2912 // check if a cmd has been used to compute another mesh
2913 if ( isCmdUsedForCompute( *cmd, theComputeCmd->GetAddress() ))
2915 // check if a cmd is a sole command setting its parameter;
2916 // don't use method name for search as it can change
2917 map<TCollection_AsciiString, list<Handle(_pyCommand)> >::iterator
2918 m2cmds = myMeth2Commands.begin();
2919 for ( ; m2cmds != myMeth2Commands.end(); ++m2cmds )
2921 list< Handle(_pyCommand)>& cmds = m2cmds->second;
2922 list< Handle(_pyCommand)>::iterator cmdIt = std::find( cmds.begin(), cmds.end(), *cmd );
2923 if ( cmdIt != cmds.end() )
2925 if ( cmds.back() != *cmd )
2927 cmds.erase( cmdIt );
2934 myComputeAddr2Cmds.erase( theComputeCmd->GetAddress() );
2937 //================================================================================
2939 * \brief Sets an argNb-th argument of current creation command
2940 * \param argNb - argument index countered from 1
2942 //================================================================================
2944 void _pyHypothesis::setCreationArg( const int argNb, const _AString& arg )
2946 if ( myCurCrMethod )
2948 while ( myCurCrMethod->myArgs.size() < argNb )
2949 myCurCrMethod->myArgs.push_back( "None" );
2950 if ( arg.IsEmpty() )
2951 myCurCrMethod->myArgs[ argNb-1 ] = "None";
2953 myCurCrMethod->myArgs[ argNb-1 ] = arg;
2958 //================================================================================
2960 * \brief Remember hypothesis parameter values
2961 * \param theCommand - The called hypothesis method
2963 //================================================================================
2965 void _pyComplexParamHypo::Process( const Handle(_pyCommand)& theCommand)
2967 if ( GetAlgoType() == "Cartesian_3D" )
2969 // CartesianParameters3D hyp
2971 if ( theCommand->GetMethod() == "SetSizeThreshold" )
2973 setCreationArg( 4, theCommand->GetArg( 1 ));
2974 myArgCommands.push_back( theCommand );
2977 if ( theCommand->GetMethod() == "SetGrid" ||
2978 theCommand->GetMethod() == "SetGridSpacing" )
2980 TCollection_AsciiString axis = theCommand->GetArg( theCommand->GetNbArgs() );
2981 int iArg = axis.Value(1) - '0';
2982 if ( theCommand->GetMethod() == "SetGrid" )
2984 setCreationArg( 1+iArg, theCommand->GetArg( 1 ));
2988 myCurCrMethod->myArgs[ iArg ] = "[ ";
2989 myCurCrMethod->myArgs[ iArg ] += theCommand->GetArg( 1 );
2990 myCurCrMethod->myArgs[ iArg ] += ", ";
2991 myCurCrMethod->myArgs[ iArg ] += theCommand->GetArg( 2 );
2992 myCurCrMethod->myArgs[ iArg ] += "]";
2994 myArgCommands.push_back( theCommand );
2995 rememberCmdOfParameter( theCommand );
3000 if( theCommand->GetMethod() == "SetLength" )
3002 // NOW it is OBSOLETE
3003 // ex: hyp.SetLength(start, 1)
3004 // hyp.SetLength(end, 0)
3005 ASSERT(( theCommand->GetArg( 2 ).IsIntegerValue() ));
3006 int i = 1 - theCommand->GetArg( 2 ).IntegerValue();
3007 TType2CrMethod::iterator type2meth = myAlgoType2CreationMethod.begin();
3008 for ( ; type2meth != myAlgoType2CreationMethod.end(); ++type2meth )
3010 CreationMethod& crMethod = type2meth->second;
3011 while ( crMethod.myArgs.size() < i+1 )
3012 crMethod.myArgs.push_back( "[]" );
3013 crMethod.myArgs[ i ] = theCommand->GetArg( 1 ); // arg value
3015 myArgCommands.push_back( theCommand );
3019 _pyHypothesis::Process( theCommand );
3022 //================================================================================
3024 * \brief Clear SetObjectEntry() as it is called by methods of Mesh_Segment
3026 //================================================================================
3028 void _pyComplexParamHypo::Flush()
3032 list < Handle(_pyCommand) >::iterator cmd = myUnusedCommands.begin();
3033 for ( ; cmd != myUnusedCommands.end(); ++cmd )
3034 if ((*cmd)->GetMethod() == "SetObjectEntry" )
3039 //================================================================================
3041 * \brief Convert methods of 1D hypotheses to my own methods
3042 * \param theCommand - The called hypothesis method
3044 //================================================================================
3046 void _pyLayerDistributionHypo::Process( const Handle(_pyCommand)& theCommand)
3048 if ( theCommand->GetMethod() != "SetLayerDistribution" )
3051 const _pyID& hyp1dID = theCommand->GetArg( 1 );
3052 // Handle(_pyHypothesis) hyp1d = theGen->FindHyp( hyp1dID );
3053 // if ( hyp1d.IsNull() && ! my1dHyp.IsNull()) // apparently hypId changed at study restoration
3055 // TCollection_AsciiString cmd =
3056 // my1dHyp->GetCreationCmd()->GetIndentation() + hyp1dID + " = " + my1dHyp->GetID();
3057 // Handle(_pyCommand) newCmd = theGen->AddCommand( cmd );
3058 // theGen->SetCommandAfter( newCmd, my1dHyp->GetCreationCmd() );
3061 // else if ( !my1dHyp.IsNull() && hyp1dID != my1dHyp->GetID() )
3063 // // 1D hypo is already set, so distribution changes and the old
3064 // // 1D hypo is thrown away
3065 // my1dHyp->ClearAllCommands();
3068 // //my1dHyp->SetRemovedFromStudy( false );
3070 // if ( !myArgCommands.empty() )
3071 // myArgCommands.back()->Clear();
3072 myCurCrMethod->myArgs.push_back( hyp1dID );
3073 myArgCommands.push_back( theCommand );
3076 //================================================================================
3079 * \param theAdditionCmd - command to be converted
3080 * \param theMesh - mesh instance
3081 * \retval bool - status
3083 //================================================================================
3085 bool _pyLayerDistributionHypo::Addition2Creation( const Handle(_pyCommand)& theAdditionCmd,
3086 const _pyID& theMesh)
3088 myIsWrapped = false;
3090 if ( my1dHyp.IsNull() )
3093 // set "SetLayerDistribution()" after addition cmd
3094 theAdditionCmd->AddDependantCmd( myArgCommands.front() );
3096 _pyID geom = theAdditionCmd->GetArg( 1 );
3098 Handle(_pyHypothesis) algo = theGen->FindAlgo( geom, theMesh, this );
3099 if ( !algo.IsNull() )
3101 my1dHyp->SetMesh( theMesh );
3102 my1dHyp->SetConvMethodAndType(my1dHyp->GetAlgoCreationMethod().ToCString(),
3103 algo->GetAlgoType().ToCString());
3104 if ( !my1dHyp->Addition2Creation( theAdditionCmd, theMesh ))
3107 // clear "SetLayerDistribution()" cmd
3108 myArgCommands.back()->Clear();
3110 // Convert my creation => me = RadialPrismAlgo.Get3DHypothesis()
3112 // find RadialPrism algo created on <geom> for theMesh
3113 GetCreationCmd()->SetObject( algo->GetID() );
3114 GetCreationCmd()->SetMethod( myAlgoMethod );
3115 GetCreationCmd()->RemoveArgs();
3116 theAdditionCmd->AddDependantCmd( GetCreationCmd() );
3122 //================================================================================
3126 //================================================================================
3128 void _pyLayerDistributionHypo::Flush()
3130 // as creation of 1D hyp was written later then it's edition,
3131 // we need to find all it's edition calls and process them
3132 list< Handle(_pyCommand) >::iterator cmd = myArgCommands.begin();
3134 for ( cmd = myArgCommands.begin(); cmd != myArgCommands.end(); ++cmd )
3136 const _pyID& hyp1dID = (*cmd)->GetArg( 1 );
3137 if ( hyp1dID.IsEmpty() ) continue;
3139 Handle(_pyHypothesis) hyp1d = theGen->FindHyp( hyp1dID );
3141 // make a new name for 1D hyp = "HypType" + "_Distribution"
3143 if ( hyp1d.IsNull() ) // apparently hypId changed at study restoration
3145 if ( prevNewName.IsEmpty() ) continue;
3146 newName = prevNewName;
3150 if ( hyp1d->IsWrapped() ) {
3151 newName = hyp1d->GetCreationCmd()->GetMethod();
3154 TCollection_AsciiString hypTypeQuoted = hyp1d->GetCreationCmd()->GetArg(1);
3155 newName = hypTypeQuoted.SubString( 2, hypTypeQuoted.Length() - 1 );
3157 newName += "_Distribution";
3158 prevNewName = newName;
3160 hyp1d->GetCreationCmd()->SetResultValue( newName );
3162 list< Handle(_pyCommand) >& cmds = theGen->GetCommands();
3163 list< Handle(_pyCommand) >::iterator cmdIt = cmds.begin();
3164 for ( ; cmdIt != cmds.end(); ++cmdIt ) {
3165 const _pyID& objID = (*cmdIt)->GetObject();
3166 if ( objID == hyp1dID ) {
3167 if ( !hyp1d.IsNull() )
3169 hyp1d->Process( *cmdIt );
3170 hyp1d->GetCreationCmd()->AddDependantCmd( *cmdIt );
3172 ( *cmdIt )->SetObject( newName );
3175 // Set new hyp name to SetLayerDistribution(hyp1dID) cmd
3176 (*cmd)->SetArg( 1, newName );
3180 //================================================================================
3182 * \brief additionally to Addition2Creation, clears SetDistrType() command
3183 * \param theCmd - AddHypothesis() command
3184 * \param theMesh - mesh to which a hypothesis is added
3185 * \retval bool - convertion result
3187 //================================================================================
3189 bool _pyNumberOfSegmentsHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
3190 const _pyID& theMesh)
3192 if ( IsWrappable( theMesh ) && myCurCrMethod->myArgs.size() > 1 ) {
3193 // scale factor (2-nd arg) is provided: clear SetDistrType(1) command
3194 bool scaleDistrType = false;
3195 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnusedCommands.rbegin();
3196 for ( ; cmd != myUnusedCommands.rend(); ++cmd ) {
3197 if ( (*cmd)->GetMethod() == "SetDistrType" ) {
3198 if ( (*cmd)->GetArg( 1 ) == "1" ) {
3199 scaleDistrType = true;
3202 else if ( !scaleDistrType ) {
3203 // distribution type changed: remove scale factor from args
3204 TType2CrMethod::iterator type2meth = myAlgoType2CreationMethod.begin();
3205 for ( ; type2meth != myAlgoType2CreationMethod.end(); ++type2meth )
3207 CreationMethod& crMethod = type2meth->second;
3208 if ( crMethod.myArgs.size() == 2 )
3209 crMethod.myArgs.pop_back();
3216 return _pyHypothesis::Addition2Creation( theCmd, theMesh );
3219 //================================================================================
3221 * \brief remove repeated commands defining distribution
3223 //================================================================================
3225 void _pyNumberOfSegmentsHyp::Flush()
3227 // find number of the last SetDistrType() command
3228 list<Handle(_pyCommand)>::reverse_iterator cmd = myUnusedCommands.rbegin();
3229 int distrTypeNb = 0;
3230 for ( ; !distrTypeNb && cmd != myUnusedCommands.rend(); ++cmd )
3231 if ( (*cmd)->GetMethod() == "SetDistrType" ) {
3232 if ( cmd != myUnusedCommands.rbegin() )
3233 distrTypeNb = (*cmd)->GetOrderNb();
3235 else if (IsWrapped() && (*cmd)->GetMethod() == "SetObjectEntry" ) {
3238 // clear commands before the last SetDistrType()
3239 list<Handle(_pyCommand)> * cmds[2] = { &myArgCommands, &myUnusedCommands };
3240 set< int > treatedCmdNbs; // avoid treating same cmd twice
3241 for ( int i = 0; i < 2; ++i ) {
3242 set<TCollection_AsciiString> uniqueMethods;
3243 list<Handle(_pyCommand)> & cmdList = *cmds[i];
3244 for ( cmd = cmdList.rbegin(); cmd != cmdList.rend(); ++cmd )
3246 if ( !treatedCmdNbs.insert( (*cmd)->GetOrderNb() ).second )
3247 continue;// avoid treating same cmd twice
3248 bool clear = ( (*cmd)->GetOrderNb() < distrTypeNb );
3249 const TCollection_AsciiString& method = (*cmd)->GetMethod();
3250 if ( !clear || method == "SetNumberOfSegments" ) {
3251 bool isNewInSet = uniqueMethods.insert( method ).second;
3252 clear = !isNewInSet;
3261 //================================================================================
3263 * \brief Convert the command adding "SegmentLengthAroundVertex" to mesh
3264 * into regular1D.LengthNearVertex( length, vertex )
3265 * \param theCmd - The command like mesh.AddHypothesis( vertex, SegmentLengthAroundVertex )
3266 * \param theMesh - The mesh needing this hypo
3267 * \retval bool - false if the command cant be converted
3269 //================================================================================
3271 bool _pySegmentLengthAroundVertexHyp::Addition2Creation( const Handle(_pyCommand)& theCmd,
3272 const _pyID& theMeshID)
3274 if ( IsWrappable( theMeshID )) {
3276 _pyID vertex = theCmd->GetArg( 1 );
3278 // the problem here is that segment algo will not be found
3279 // by pyHypothesis::Addition2Creation() for <vertex>, so we try to find
3280 // geometry where segment algorithm is assigned
3281 Handle(_pyHypothesis) algo;
3282 _pyID geom = vertex;
3283 while ( algo.IsNull() && !geom.IsEmpty()) {
3284 // try to find geom as a father of <vertex>
3285 geom = FatherID( geom );
3286 algo = theGen->FindAlgo( geom, theMeshID, this );
3288 if ( algo.IsNull() )
3289 return false; // also possible to find geom as brother of veretex...
3290 // set geom instead of vertex
3291 theCmd->SetArg( 1, geom );
3293 // set vertex as a second arg
3294 if ( myCurCrMethod->myArgs.size() < 1) setCreationArg( 1, "1" ); // :(
3295 setCreationArg( 2, vertex );
3297 // mesh.AddHypothesis(vertex, SegmentLengthAroundVertex) -->
3298 // theMeshID.LengthNearVertex( length, vertex )
3299 return _pyHypothesis::Addition2Creation( theCmd, theMeshID );
3304 //================================================================================
3306 * \brief _pyAlgorithm constructor
3307 * \param theCreationCmd - The command like "algo = smeshgen.CreateHypothesis(type,lib)"
3309 //================================================================================
3311 _pyAlgorithm::_pyAlgorithm(const Handle(_pyCommand)& theCreationCmd)
3312 : _pyHypothesis( theCreationCmd )
3317 //================================================================================
3319 * \brief Convert the command adding an algorithm to mesh
3320 * \param theCmd - The command like mesh.AddHypothesis( geom, algo )
3321 * \param theMesh - The mesh needing this algo
3322 * \retval bool - false if the command cant be converted
3324 //================================================================================
3326 bool _pyAlgorithm::Addition2Creation( const Handle(_pyCommand)& theCmd,
3327 const _pyID& theMeshID)
3329 // mesh.AddHypothesis(geom,algo) --> theMeshID.myCreationMethod()
3330 if ( _pyHypothesis::Addition2Creation( theCmd, theMeshID )) {
3331 theGen->SetAccessorMethod( GetID(), "GetAlgorithm()" );
3337 //================================================================================
3339 * \brief Return starting position of a part of python command
3340 * \param thePartIndex - The index of command part
3341 * \retval int - Part position
3343 //================================================================================
3345 int _pyCommand::GetBegPos( int thePartIndex )
3349 if ( myBegPos.Length() < thePartIndex )
3351 return myBegPos( thePartIndex );
3354 //================================================================================
3356 * \brief Store starting position of a part of python command
3357 * \param thePartIndex - The index of command part
3358 * \param thePosition - Part position
3360 //================================================================================
3362 void _pyCommand::SetBegPos( int thePartIndex, int thePosition )
3364 while ( myBegPos.Length() < thePartIndex )
3365 myBegPos.Append( UNKNOWN );
3366 myBegPos( thePartIndex ) = thePosition;
3369 //================================================================================
3371 * \brief Returns whitespace symbols at the line beginning
3372 * \retval TCollection_AsciiString - result
3374 //================================================================================
3376 TCollection_AsciiString _pyCommand::GetIndentation()
3379 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
3380 GetWord( myString, end, true );
3382 end = GetBegPos( RESULT_IND );
3383 return myString.SubString( 1, end - 1 );
3386 //================================================================================
3388 * \brief Return substring of python command looking like ResultValue = Obj.Meth()
3389 * \retval const TCollection_AsciiString & - ResultValue substring
3391 //================================================================================
3393 const TCollection_AsciiString & _pyCommand::GetResultValue()
3395 if ( GetBegPos( RESULT_IND ) == UNKNOWN )
3397 SetBegPos( RESULT_IND, EMPTY );
3398 int begPos, endPos = myString.Location( "=", 1, Length() );
3402 while ( begPos < endPos && isspace( myString.Value( begPos ))) ++begPos;
3403 if ( begPos < endPos )
3405 SetBegPos( RESULT_IND, begPos );
3407 while ( begPos < endPos && isspace( myString.Value( endPos ))) --endPos;
3408 myRes = myString.SubString( begPos, endPos );
3415 //================================================================================
3417 * \brief Return number of python command result value ResultValue = Obj.Meth()
3419 //================================================================================
3421 int _pyCommand::GetNbResultValues()
3425 int endPos = myString.Location( "=", 1, Length() );
3426 while ( begPos < endPos )
3428 _AString str = GetWord( myString, begPos, true );
3429 begPos = begPos+ str.Length();
3436 //================================================================================
3438 * \brief Return substring of python command looking like
3439 * ResultValue1 , ResultValue2,... = Obj.Meth() with res index
3440 * \retval const TCollection_AsciiString & - ResultValue with res index substring
3442 //================================================================================
3443 TCollection_AsciiString _pyCommand::GetResultValue(int res)
3446 if ( SkipSpaces( myString, begPos ) && myString.Value( begPos ) == '[' )
3447 ++begPos; // skip [, else the whole list is returned
3448 int endPos = myString.Location( "=", 1, Length() );
3450 while ( begPos < endPos) {
3451 _AString result = GetWord( myString, begPos, true );
3452 begPos = begPos + result.Length();
3455 result.RemoveAll('[');
3456 result.RemoveAll(']');
3462 return theEmptyString;
3465 //================================================================================
3467 * \brief Return substring of python command looking like ResVal = Object.Meth()
3468 * \retval const TCollection_AsciiString & - Object substring
3470 //================================================================================
3472 const TCollection_AsciiString & _pyCommand::GetObject()
3474 if ( GetBegPos( OBJECT_IND ) == UNKNOWN )
3477 int begPos = GetBegPos( RESULT_IND ) + myRes.Length();
3479 begPos = myString.Location( "=", 1, Length() ) + 1;
3480 // is '=' in the string argument (for example, name) or not
3481 int nb1 = 0; // number of ' character at the left of =
3482 int nb2 = 0; // number of " character at the left of =
3483 for ( int i = 1; i < begPos-1; i++ ) {
3484 if ( myString.Value( i )=='\'' )
3486 else if ( myString.Value( i )=='"' )
3489 // if number of ' or " is not divisible by 2,
3490 // then get an object at the start of the command
3491 if ( nb1 % 2 != 0 || nb2 % 2 != 0 )
3494 myObj = GetWord( myString, begPos, true );
3495 if ( begPos != EMPTY )
3497 // check if object is complex,
3498 // so far consider case like "smesh.Method()"
3499 if ( int bracketPos = myString.Location( "(", begPos, Length() )) {
3500 //if ( bracketPos==0 ) bracketPos = Length();
3501 int dotPos = begPos+myObj.Length();
3502 while ( dotPos+1 < bracketPos ) {
3503 if ( int pos = myString.Location( ".", dotPos+1, bracketPos ))
3508 if ( dotPos > begPos+myObj.Length() )
3509 myObj = myString.SubString( begPos, dotPos-1 );
3512 // 1st word after '=' is an object
3513 // else // no method -> no object
3519 SetBegPos( OBJECT_IND, begPos );
3525 //================================================================================
3527 * \brief Return substring of python command looking like ResVal = Obj.Method()
3528 * \retval const TCollection_AsciiString & - Method substring
3530 //================================================================================
3532 const TCollection_AsciiString & _pyCommand::GetMethod()
3534 if ( GetBegPos( METHOD_IND ) == UNKNOWN )
3537 int begPos = GetBegPos( OBJECT_IND ) + myObj.Length();
3538 bool forward = true;
3540 begPos = myString.Location( "(", 1, Length() ) - 1;
3544 myMeth = GetWord( myString, begPos, forward );
3545 SetBegPos( METHOD_IND, begPos );
3551 //================================================================================
3553 * \brief Return substring of python command looking like ResVal = Obj.Meth(Arg1,...)
3554 * \retval const TCollection_AsciiString & - Arg<index> substring
3556 //================================================================================
3558 const TCollection_AsciiString & _pyCommand::GetArg( int index )
3560 if ( GetBegPos( ARG1_IND ) == UNKNOWN )
3564 int pos = GetBegPos( METHOD_IND ) + myMeth.Length();
3566 pos = myString.Location( "(", 1, Length() );
3570 // we are at or before '(', skip it if present
3572 while ( pos <= Length() && myString.Value( pos ) != '(' ) ++pos;
3573 if ( pos > Length() )
3577 SetBegPos( ARG1_IND, 0 ); // even no '('
3578 return theEmptyString;
3582 list< TCollection_AsciiString > separatorStack( 1, ",)");
3583 bool ignoreNesting = false;
3585 while ( pos <= Length() )
3587 const char chr = myString.Value( pos );
3589 if ( separatorStack.back().Location( chr, 1, separatorStack.back().Length()))
3591 if ( separatorStack.size() == 1 ) // a comma dividing args or a terminal ')' found
3593 while ( pos-1 >= prevPos && isspace( myString.Value( prevPos )))
3595 TCollection_AsciiString arg;
3596 if ( pos-1 >= prevPos ) {
3597 arg = myString.SubString( prevPos, pos-1 );
3598 arg.RightAdjust(); // remove spaces
3601 if ( !arg.IsEmpty() || chr == ',' )
3603 SetBegPos( ARG1_IND + myArgs.Length(), prevPos );
3604 myArgs.Append( arg );
3610 else // end of nesting args found
3612 separatorStack.pop_back();
3613 ignoreNesting = false;
3616 else if ( !ignoreNesting )
3619 case '(' : separatorStack.push_back(")"); break;
3620 case '[' : separatorStack.push_back("]"); break;
3621 case '\'': separatorStack.push_back("'"); ignoreNesting=true; break;
3622 case '"' : separatorStack.push_back("\""); ignoreNesting=true; break;
3629 if ( myArgs.Length() < index )
3630 return theEmptyString;
3631 return myArgs( index );
3634 //================================================================================
3636 * \brief Check if char is a word part
3637 * \param c - The character to check
3638 * \retval bool - The check result
3640 //================================================================================
3642 static inline bool isWord(const char c, const bool dotIsWord)
3645 !isspace(c) && c != ',' && c != '=' && c != ')' && c != '(' && ( dotIsWord || c != '.');
3648 //================================================================================
3650 * \brief Looks for a word in the string and returns word's beginning
3651 * \param theString - The input string
3652 * \param theStartPos - The position to start the search, returning word's beginning
3653 * \param theForward - The search direction
3654 * \retval TCollection_AsciiString - The found word
3656 //================================================================================
3658 TCollection_AsciiString _pyCommand::GetWord( const _AString & theString,
3660 const bool theForward,
3661 const bool dotIsWord )
3663 int beg = theStartPos, end = theStartPos;
3664 theStartPos = EMPTY;
3665 if ( beg < 1 || beg > theString.Length() )
3666 return theEmptyString;
3668 if ( theForward ) { // search forward
3670 while ( beg <= theString.Length() && !isWord( theString.Value( beg ), dotIsWord))
3672 if ( beg > theString.Length() )
3673 return theEmptyString; // no word found
3676 char begChar = theString.Value( beg );
3677 if ( begChar == '"' || begChar == '\'' || begChar == '[') {
3678 char endChar = ( begChar == '[' ) ? ']' : begChar;
3679 // end is at the corresponding quoting mark or bracket
3680 while ( end < theString.Length() &&
3681 ( theString.Value( end ) != endChar || theString.Value( end-1 ) == '\\'))
3685 while ( end <= theString.Length() && isWord( theString.Value( end ), dotIsWord))
3690 else { // search backward
3692 while ( end > 0 && !isWord( theString.Value( end ), dotIsWord))
3695 return theEmptyString; // no word found
3697 char endChar = theString.Value( end );
3698 if ( endChar == '"' || endChar == '\'' || endChar == ']') {
3699 char begChar = ( endChar == ']' ) ? '[' : endChar;
3700 // beg is at the corresponding quoting mark
3702 ( theString.Value( beg ) != begChar || theString.Value( beg-1 ) == '\\'))
3706 while ( beg > 0 && isWord( theString.Value( beg ), dotIsWord))
3712 //cout << theString << " ---- " << beg << " - " << end << endl;
3713 return theString.SubString( beg, end );
3716 //================================================================================
3718 * \brief Returns true if the string looks like a study entry
3720 //================================================================================
3722 bool _pyCommand::IsStudyEntry( const TCollection_AsciiString& str )
3724 if ( str.Length() < 5 ) return false;
3726 int nbColons = 0, isColon;
3727 for ( int i = 1; i <= str.Length(); ++i )
3729 char c = str.Value(i);
3730 if (!( isColon = (c == ':')) && ( c < '0' || c > '9' ))
3732 nbColons += isColon;
3734 return nbColons > 2 && str.Length()-nbColons > 2;
3737 //================================================================================
3739 * \brief Finds entries in a sting
3741 //================================================================================
3743 std::list< _pyID > _pyCommand::GetStudyEntries( const TCollection_AsciiString& str )
3745 std::list< _pyID > resList;
3747 while ( ++pos <= str.Length() )
3749 if ( !isdigit( str.Value( pos ))) continue;
3750 if ( pos != 1 && ( isalpha( str.Value( pos-1 ) || str.Value( pos-1 ) == ':'))) continue;
3753 while ( ++end <= str.Length() && ( isdigit( str.Value( end )) || str.Value( end ) == ':' ));
3754 _pyID entry = str.SubString( pos, end-1 );
3756 if ( IsStudyEntry( entry ))
3757 resList.push_back( entry );
3762 //================================================================================
3764 * \brief Look for position where not space char is
3765 * \param theString - The string
3766 * \param thePos - The position to search from and which returns result
3767 * \retval bool - false if there are only space after thePos in theString
3769 //================================================================================
3771 bool _pyCommand::SkipSpaces( const TCollection_AsciiString & theString, int & thePos )
3773 if ( thePos < 1 || thePos > theString.Length() )
3776 while ( thePos <= theString.Length() && isspace( theString.Value( thePos )))
3779 return thePos <= theString.Length();
3782 //================================================================================
3784 * \brief Modify a part of the command
3785 * \param thePartIndex - The index of the part
3786 * \param thePart - The new part string
3787 * \param theOldPart - The old part
3789 //================================================================================
3791 void _pyCommand::SetPart(int thePartIndex, const TCollection_AsciiString& thePart,
3792 TCollection_AsciiString& theOldPart)
3794 int pos = GetBegPos( thePartIndex );
3795 if ( pos <= Length() && theOldPart != thePart)
3797 TCollection_AsciiString seperator;
3799 pos = GetBegPos( thePartIndex + 1 );
3800 if ( pos < 1 ) return;
3801 switch ( thePartIndex ) {
3802 case RESULT_IND: seperator = " = "; break;
3803 case OBJECT_IND: seperator = "."; break;
3804 case METHOD_IND: seperator = "()"; break;
3808 myString.Remove( pos, theOldPart.Length() );
3809 if ( !seperator.IsEmpty() )
3810 myString.Insert( pos , seperator );
3811 myString.Insert( pos, thePart );
3812 // update starting positions of the following parts
3813 int posDelta = thePart.Length() + seperator.Length() - theOldPart.Length();
3814 for ( int i = thePartIndex + 1; i <= myBegPos.Length(); ++i ) {
3815 if ( myBegPos( i ) > 0 )
3816 myBegPos( i ) += posDelta;
3818 theOldPart = thePart;
3822 //================================================================================
3824 * \brief Set agrument
3825 * \param index - The argument index, it counts from 1
3826 * \param theArg - The argument string
3828 //================================================================================
3830 void _pyCommand::SetArg( int index, const TCollection_AsciiString& theArg)
3833 int argInd = ARG1_IND + index - 1;
3834 int pos = GetBegPos( argInd );
3835 if ( pos < 1 ) // no index-th arg exist, append inexistent args
3837 // find a closing parenthesis
3838 if ( GetNbArgs() != 0 && index <= GetNbArgs() ) {
3839 int lastArgInd = GetNbArgs();
3840 pos = GetBegPos( ARG1_IND + lastArgInd - 1 ) + GetArg( lastArgInd ).Length();
3841 while ( pos > 0 && pos <= Length() && myString.Value( pos ) != ')' )
3846 while ( pos > 0 && myString.Value( pos ) != ')' )
3849 if ( pos < 1 || myString.Value( pos ) != ')' ) { // no parentheses at all
3853 while ( myArgs.Length() < index ) {
3854 if ( myArgs.Length() )
3855 myString.Insert( pos++, "," );
3856 myArgs.Append("None");
3857 myString.Insert( pos, myArgs.Last() );
3858 SetBegPos( ARG1_IND + myArgs.Length() - 1, pos );
3859 pos += myArgs.Last().Length();
3862 SetPart( argInd, theArg, myArgs( index ));
3865 //================================================================================
3867 * \brief Empty arg list
3869 //================================================================================
3871 void _pyCommand::RemoveArgs()
3873 if ( int pos = myString.Location( '(', Max( 1, GetBegPos( METHOD_IND )), Length() ))
3874 myString.Trunc( pos );
3877 if ( myBegPos.Length() >= ARG1_IND )
3878 myBegPos.Remove( ARG1_IND, myBegPos.Length() );
3881 //================================================================================
3883 * \brief Comment a python command
3885 //================================================================================
3887 void _pyCommand::Comment()
3889 if ( IsEmpty() ) return;
3892 while ( i <= Length() && isspace( myString.Value(i) )) ++i;
3893 if ( i <= Length() )
3895 myString.Insert( i, "#" );
3896 for ( int iPart = 0; iPart < myBegPos.Length(); ++iPart )
3898 int begPos = GetBegPos( iPart + 1 );
3899 if ( begPos != UNKNOWN )
3900 SetBegPos( iPart + 1, begPos + 1 );
3905 //================================================================================
3907 * \brief Set dependent commands after this one
3909 //================================================================================
3911 bool _pyCommand::SetDependentCmdsAfter() const
3913 bool orderChanged = false;
3914 list< Handle(_pyCommand)>::const_reverse_iterator cmd = myDependentCmds.rbegin();
3915 for ( ; cmd != myDependentCmds.rend(); ++cmd ) {
3916 if ( (*cmd)->GetOrderNb() < GetOrderNb() ) {
3917 orderChanged = true;
3918 theGen->SetCommandAfter( *cmd, this );
3919 (*cmd)->SetDependentCmdsAfter();
3922 return orderChanged;
3924 //================================================================================
3926 * \brief Insert accessor method after theObjectID
3927 * \param theObjectID - id of the accessed object
3928 * \param theAcsMethod - name of the method giving access to the object
3929 * \retval bool - false if theObjectID is not found in the command string
3931 //================================================================================
3933 bool _pyCommand::AddAccessorMethod( _pyID theObjectID, const char* theAcsMethod )
3935 if ( !theAcsMethod )
3937 // start object search from the object, i.e. ignore result
3939 int beg = GetBegPos( OBJECT_IND );
3940 if ( beg < 1 || beg > Length() )
3943 while (( beg = myString.Location( theObjectID, beg, Length() )))
3945 // check that theObjectID is not just a part of a longer ID
3946 int afterEnd = beg + theObjectID.Length();
3947 Standard_Character c = myString.Value( afterEnd );
3948 if ( !isalnum( c ) && c != ':' ) {
3949 // check if accessor method already present
3951 myString.Location( (char*) theAcsMethod, afterEnd, Length() ) != afterEnd+1) {
3953 int oldLen = Length();
3954 myString.Insert( afterEnd, (char*) theAcsMethod );
3955 myString.Insert( afterEnd, "." );
3956 // update starting positions of the parts following the modified one
3957 int posDelta = Length() - oldLen;
3958 for ( int i = 1; i <= myBegPos.Length(); ++i ) {
3959 if ( myBegPos( i ) > afterEnd )
3960 myBegPos( i ) += posDelta;
3965 beg = afterEnd; // is a part - next search
3970 //================================================================================
3972 * \brief Creates pyObject
3974 //================================================================================
3976 _pyObject::_pyObject(const Handle(_pyCommand)& theCreationCmd, const _pyID& theID)
3977 : myID(theID), myCreationCmd(theCreationCmd), myIsPublished(false)
3982 //================================================================================
3984 * \brief Set up myID and myIsPublished
3986 //================================================================================
3988 void _pyObject::setID(const _pyID& theID)
3991 myIsPublished = !theGen->IsNotPublished( GetID() );
3994 //================================================================================
3996 * \brief Clear myCreationCmd and myProcessedCmds
3998 //================================================================================
4000 void _pyObject::ClearCommands()
4005 if ( !myCreationCmd.IsNull() )
4006 myCreationCmd->Clear();
4008 list< Handle(_pyCommand) >::iterator cmd = myProcessedCmds.begin();
4009 for ( ; cmd != myProcessedCmds.end(); ++cmd )
4013 //================================================================================
4015 * \brief Return method name giving access to an interaface object wrapped by python class
4016 * \retval const char* - method name
4018 //================================================================================
4020 const char* _pyObject::AccessorMethod() const
4024 //================================================================================
4026 * \brief Return ID of a father
4028 //================================================================================
4030 _pyID _pyObject::FatherID(const _pyID & childID)
4032 int colPos = childID.SearchFromEnd(':');
4034 return childID.SubString( 1, colPos-1 );
4038 //================================================================================
4040 * \brief SelfEraser erases creation command if no more it's commands invoked
4042 //================================================================================
4044 void _pySelfEraser::Flush()
4046 int nbCalls = GetNbCalls();
4049 // ignore cleared commands
4050 std::list< Handle(_pyCommand) >& cmds = GetProcessedCmds();
4051 std::list< Handle(_pyCommand) >::const_iterator cmd = cmds.begin();
4052 for ( ; cmd != cmds.end(); ++cmd )
4053 nbCalls -= (*cmd)->IsEmpty();
4056 GetCreationCmd()->Clear();
4059 //================================================================================
4061 * \brief _pySubMesh constructor
4063 //================================================================================
4065 _pySubMesh::_pySubMesh(const Handle(_pyCommand)& theCreationCmd):
4066 _pyObject(theCreationCmd)
4068 myMesh = ObjectToMesh( theGen->FindObject( theCreationCmd->GetObject() ));
4071 //================================================================================
4073 * \brief Return true if a sub-mesh can be used as argument of the given method
4075 //================================================================================
4077 bool _pySubMesh::CanBeArgOfMethod(const _AString& theMethodName)
4079 // names of all methods where a sub-mesh can be used as argument
4080 static TStringSet methods;
4081 if ( methods.empty() ) {
4082 const char * names[] = {
4083 // methods of SMESH_Gen
4085 // methods of SMESH_Group
4087 // methods of SMESH_Measurements
4089 // methods of SMESH_Mesh
4090 "ExportPartToMED","ExportCGNS","ExportPartToDAT","ExportPartToUNV","ExportPartToSTL",
4092 // methods of SMESH_MeshEditor
4093 "ReorientObject","Reorient2D","TriToQuadObject","QuadToTriObject","SplitQuadObject",
4094 "SplitVolumesIntoTetra","SmoothObject","SmoothParametricObject","ConvertFromQuadraticObject",
4095 "RotationSweepObject","RotationSweepObjectMakeGroups","RotationSweepObject1D",
4096 "RotationSweepObject1DMakeGroups","RotationSweepObject2D","RotationSweepObject2DMakeGroups",
4097 "ExtrusionSweepObject","ExtrusionSweepObjectMakeGroups","ExtrusionSweepObject0D",
4098 "ExtrusionSweepObject0DMakeGroups","ExtrusionSweepObject1D","ExtrusionSweepObject2D",
4099 "ExtrusionSweepObject1DMakeGroups","ExtrusionSweepObject2DMakeGroups",
4100 "ExtrusionAlongPathObjX","ExtrusionAlongPathObject","ExtrusionAlongPathObjectMakeGroups",
4101 "ExtrusionAlongPathObject1D","ExtrusionAlongPathObject1DMakeGroups",
4102 "ExtrusionAlongPathObject2D","ExtrusionAlongPathObject2DMakeGroups","MirrorObject",
4103 "MirrorObjectMakeGroups","MirrorObjectMakeMesh","TranslateObject","Scale",
4104 "TranslateObjectMakeGroups","TranslateObjectMakeMesh","ScaleMakeGroups","ScaleMakeMesh",
4105 "RotateObject","RotateObjectMakeGroups","RotateObjectMakeMesh","FindCoincidentNodesOnPart",
4106 "FindCoincidentNodesOnPartBut","FindEqualElements","FindAmongElementsByPoint",
4107 "MakeBoundaryMesh","Create0DElementsOnAllNodes",
4108 "" }; // <- mark of end
4109 methods.Insert( names );
4111 return methods.Contains( theMethodName );
4114 //================================================================================
4116 * \brief count invoked commands
4118 //================================================================================
4120 void _pySubMesh::Process( const Handle(_pyCommand)& theCommand )
4122 _pyObject::Process(theCommand); // count calls of Process()
4123 GetCreationCmd()->AddDependantCmd( theCommand );
4126 //================================================================================
4128 * \brief Move creation command depending on invoked commands
4130 //================================================================================
4132 void _pySubMesh::Flush()
4134 if ( GetNbCalls() == 0 ) // move to the end of all commands
4135 theGen->GetLastCommand()->AddDependantCmd( GetCreationCmd() );
4136 else if ( !myCreator.IsNull() )
4137 // move to be just after creator
4138 myCreator->GetCreationCmd()->AddDependantCmd( GetCreationCmd() );
4141 //================================================================================
4143 * \brief Creates _pyGroup
4145 //================================================================================
4147 _pyGroup::_pyGroup(const Handle(_pyCommand)& theCreationCmd, const _pyID & id)
4148 :_pySubMesh(theCreationCmd)
4150 if ( !id.IsEmpty() )
4153 myCanClearCreationCmd = true;
4155 const _AString& method = theCreationCmd->GetMethod();
4156 if ( method == "CreateGroup" ) // CreateGroup() --> CreateEmptyGroup()
4158 theCreationCmd->SetMethod( "CreateEmptyGroup" );
4160 // ----------------------------------------------------------------------
4161 else if ( method == "CreateGroupFromGEOM" ) // (type, name, grp)
4163 _pyID geom = theCreationCmd->GetArg( 3 );
4164 // VSR 24/12/2010. PAL21106: always use GroupOnGeom() function on dump
4165 // next if(){...} section is commented
4166 //if ( sameGroupType( geom, theCreationCmd->GetArg( 1 )) ) { // --> Group(geom)
4167 // theCreationCmd->SetMethod( "Group" );
4168 // theCreationCmd->RemoveArgs();
4169 // theCreationCmd->SetArg( 1, geom );
4172 // ------------------------->>>>> GroupOnGeom( geom, name, typ )
4173 _pyID type = theCreationCmd->GetArg( 1 );
4174 _pyID name = theCreationCmd->GetArg( 2 );
4175 theCreationCmd->SetMethod( "GroupOnGeom" );
4176 theCreationCmd->RemoveArgs();
4177 theCreationCmd->SetArg( 1, geom );
4178 theCreationCmd->SetArg( 2, name );
4179 theCreationCmd->SetArg( 3, type );
4182 else if ( method == "CreateGroupFromFilter" )
4184 // -> GroupOnFilter(typ, name, aFilter0x4743dc0 -> aFilter_1)
4185 theCreationCmd->SetMethod( "GroupOnFilter" );
4187 _pyID filterID = theCreationCmd->GetArg(3);
4188 Handle(_pyFilter) filter = Handle(_pyFilter)::DownCast( theGen->FindObject( filterID ));
4189 if ( !filter.IsNull())
4191 if ( !filter->GetNewID().IsEmpty() )
4192 theCreationCmd->SetArg( 3, filter->GetNewID() );
4193 filter->AddUser( this );
4197 else if ( method == "GetGroups" )
4199 myCanClearCreationCmd = ( theCreationCmd->GetNbResultValues() == 1 );
4203 // theCreationCmd does something else apart from creation of this group
4204 // and thus it can't be cleared if this group is removed
4205 myCanClearCreationCmd = false;
4209 //================================================================================
4211 * \brief Check if "[ group1, group2 ] = mesh.GetGroups()" creation command
4214 //================================================================================
4216 bool _pyGroup::CanClear()
4221 if ( !myCanClearCreationCmd && myCreationCmd->GetMethod() == "GetGroups" )
4223 TCollection_AsciiString grIDs = myCreationCmd->GetResultValue();
4224 list< _pyID > idList = myCreationCmd->GetStudyEntries( grIDs );
4225 list< _pyID >::iterator grID = idList.begin();
4226 if ( GetID() == *grID )
4228 myCanClearCreationCmd = true;
4229 list< Handle(_pyGroup ) > groups;
4230 for ( ; grID != idList.end(); ++grID )
4232 Handle(_pyGroup) group = Handle(_pyGroup)::DownCast( theGen->FindObject( *grID ));
4233 if ( group.IsNull() ) continue;
4234 groups.push_back( group );
4235 if ( group->IsInStudy() )
4236 myCanClearCreationCmd = false;
4238 // set myCanClearCreationCmd == true to all groups
4239 list< Handle(_pyGroup ) >::iterator group = groups.begin();
4240 for ( ; group != groups.end(); ++group )
4241 (*group)->myCanClearCreationCmd = myCanClearCreationCmd;
4245 return myCanClearCreationCmd;
4248 //================================================================================
4250 * \brief set myCanClearCreationCmd = true if the main action of the creation
4251 * command is discarded
4253 //================================================================================
4255 void _pyGroup::RemovedWithContents()
4257 // this code would be appropriate if Add0DElementsToAllNodes() returned only new nodes
4258 // via a created group
4259 //if ( GetCreationCmd()->GetMethod() == "Add0DElementsToAllNodes")
4260 // myCanClearCreationCmd = true;
4263 //================================================================================
4265 * \brief To convert creation of a group by filter
4267 //================================================================================
4269 void _pyGroup::Process( const Handle(_pyCommand)& theCommand)
4271 // Convert the following set of commands into mesh.MakeGroupByFilter(groupName, theFilter)
4272 // group = mesh.CreateEmptyGroup( elemType, groupName )
4273 // aFilter.SetMesh(mesh)
4274 // nbAdd = group.AddFrom( aFilter )
4275 Handle(_pyFilter) filter;
4276 if ( theCommand->GetMethod() == "AddFrom" )
4278 _pyID idSource = theCommand->GetArg(1);
4279 // check if idSource is a filter
4280 filter = Handle(_pyFilter)::DownCast( theGen->FindObject( idSource ));
4281 if ( !filter.IsNull() )
4283 // find aFilter.SetMesh(mesh) to clear it, it should be just before theCommand
4284 list< Handle(_pyCommand) >::reverse_iterator cmdIt = theGen->GetCommands().rbegin();
4285 while ( *cmdIt != theCommand ) ++cmdIt;
4286 while ( (*cmdIt)->GetOrderNb() != 1 )
4288 const Handle(_pyCommand)& setMeshCmd = *(++cmdIt);
4289 if ((setMeshCmd->GetObject() == idSource ||
4290 setMeshCmd->GetObject() == filter->GetNewID() )
4292 setMeshCmd->GetMethod() == "SetMesh")
4294 setMeshCmd->Clear();
4298 // replace 3 commands by one
4299 theCommand->Clear();
4300 const Handle(_pyCommand)& makeGroupCmd = GetCreationCmd();
4301 TCollection_AsciiString name = makeGroupCmd->GetArg( 2 );
4302 if ( !filter->GetNewID().IsEmpty() )
4303 idSource = filter->GetNewID();
4304 makeGroupCmd->SetMethod( "MakeGroupByFilter" );
4305 makeGroupCmd->SetArg( 1, name );
4306 makeGroupCmd->SetArg( 2, idSource );
4309 else if ( theCommand->GetMethod() == "SetFilter" )
4311 // set new name of a filter or clear the command if the same filter is set
4312 _pyID filterID = theCommand->GetArg(1);
4313 filter = Handle(_pyFilter)::DownCast( theGen->FindObject( filterID ));
4314 if ( !myFilter.IsNull() && filter == myFilter )
4315 theCommand->Clear();
4316 else if ( !filter.IsNull() && !filter->GetNewID().IsEmpty() )
4317 theCommand->SetArg( 1, filter->GetNewID() );
4320 else if ( theCommand->GetMethod() == "GetFilter" )
4322 // GetFilter() returns a filter with other ID, make myFilter process
4323 // calls of the returned filter
4324 if ( !myFilter.IsNull() )
4326 theGen->SetProxyObject( theCommand->GetResultValue(), myFilter );
4327 theCommand->Clear();
4331 if ( !filter.IsNull() )
4332 filter->AddUser( this );
4334 theGen->AddMeshAccessorMethod( theCommand );
4337 //================================================================================
4339 * \brief Prevent clearing "DoubleNode...() command if a group created by it is removed
4341 //================================================================================
4343 void _pyGroup::Flush()
4345 if ( !theGen->IsToKeepAllCommands() &&
4346 myCreationCmd && !myCanClearCreationCmd )
4348 myCreationCmd.Nullify(); // this way myCreationCmd won't be cleared
4352 //================================================================================
4354 * \brief Constructor of _pyFilter
4356 //================================================================================
4358 _pyFilter::_pyFilter(const Handle(_pyCommand)& theCreationCmd, const _pyID& newID/*=""*/)
4359 :_pyObject(theCreationCmd), myNewID( newID )
4363 //================================================================================
4365 * \brief To convert creation of a filter by criteria and
4366 * to replace an old name by a new one
4368 //================================================================================
4370 void _pyFilter::Process( const Handle(_pyCommand)& theCommand)
4372 if ( theCommand->GetObject() == GetID() )
4373 _pyObject::Process(theCommand); // count commands
4375 if ( !myNewID.IsEmpty() )
4376 theCommand->SetObject( myNewID );
4378 // Convert the following set of commands into smesh.GetFilterFromCriteria(criteria)
4379 // aFilter0x2aaab0487080 = aFilterManager.CreateFilter()
4380 // aFilter0x2aaab0487080.SetCriteria(aCriteria)
4381 if ( GetNbCalls() == 1 && // none method was called before this SetCriteria() call
4382 theCommand->GetMethod() == "SetCriteria")
4384 // aFilter.SetCriteria(aCriteria) ->
4385 // aFilter = smesh.GetFilterFromCriteria(criteria)
4386 if ( myNewID.IsEmpty() )
4387 theCommand->SetResultValue( GetID() );
4389 theCommand->SetResultValue( myNewID );
4390 theCommand->SetObject( SMESH_2smeshpy::GenName() );
4391 theCommand->SetMethod( "GetFilterFromCriteria" );
4393 // Clear aFilterManager.CreateFilter()
4394 GetCreationCmd()->Clear();
4396 else if ( theCommand->GetMethod() == "SetMesh" )
4398 if ( myMesh == theCommand->GetArg( 1 ))
4399 theCommand->Clear();
4401 myMesh = theCommand->GetArg( 1 );
4402 theGen->AddMeshAccessorMethod( theCommand );
4406 //================================================================================
4408 * \brief Set new filter name to the creation command
4410 //================================================================================
4412 void _pyFilter::Flush()
4414 if ( !myNewID.IsEmpty() && !GetCreationCmd()->IsEmpty() )
4415 GetCreationCmd()->SetResultValue( myNewID );
4418 //================================================================================
4420 * \brief Return true if all my users can be cleared
4422 //================================================================================
4424 bool _pyFilter::CanClear()
4426 list< Handle(_pyObject) >::iterator obj = myUsers.begin();
4427 for ( ; obj != myUsers.end(); ++obj )
4428 if ( !(*obj)->CanClear() )
4434 //================================================================================
4436 * \brief Reads _pyHypothesis'es from resource files of mesher Plugins
4438 //================================================================================
4440 _pyHypothesisReader::_pyHypothesisReader()
4443 vector< string > xmlPaths = SMESH_Gen::GetPluginXMLPaths();
4444 LDOMParser xmlParser;
4445 for ( size_t i = 0; i < xmlPaths.size(); ++i )
4447 bool error = xmlParser.parse( xmlPaths[i].c_str() );
4451 INFOS( xmlParser.GetError(data) );
4454 // <algorithm type="Regular_1D"
4455 // label-id="Wire discretisation"
4458 // <algo>Regular_1D=Segment()</algo>
4459 // <hypo>LocalLength=LocalLength(SetLength(1),,SetPrecision(1))</hypo>
4461 LDOM_Document xmlDoc = xmlParser.getDocument();
4462 LDOM_NodeList algoNodeList = xmlDoc.getElementsByTagName( "algorithm" );
4463 for ( int i = 0; i < algoNodeList.getLength(); ++i )
4465 LDOM_Node algoNode = algoNodeList.item( i );
4466 LDOM_Element& algoElem = (LDOM_Element&) algoNode;
4467 LDOM_NodeList pyAlgoNodeList = algoElem.getElementsByTagName( "algo" );
4468 if ( pyAlgoNodeList.getLength() < 1 ) continue;
4470 _AString text, algoType, method, arg;
4471 for ( int iA = 0; iA < pyAlgoNodeList.getLength(); ++iA )
4473 LDOM_Node pyAlgoNode = pyAlgoNodeList.item( iA );
4474 LDOM_Node textNode = pyAlgoNode.getFirstChild();
4475 text = textNode.getNodeValue();
4476 Handle(_pyCommand) algoCmd = new _pyCommand( text );
4477 algoType = algoCmd->GetResultValue();
4478 method = algoCmd->GetMethod();
4479 arg = algoCmd->GetArg(1);
4480 if ( !algoType.IsEmpty() && !method.IsEmpty() )
4482 Handle(_pyAlgorithm) algo = new _pyAlgorithm( algoCmd );
4483 algo->SetConvMethodAndType( method, algoType );
4484 if ( !arg.IsEmpty() )
4485 algo->setCreationArg( 1, arg );
4487 myType2Hyp[ algoType ] = algo;
4491 if ( algoType.IsEmpty() ) continue;
4493 LDOM_NodeList pyHypoNodeList = algoElem.getElementsByTagName( "hypo" );
4495 Handle( _pyHypothesis ) hyp;
4496 for ( int iH = 0; iH < pyHypoNodeList.getLength(); ++iH )
4498 LDOM_Node pyHypoNode = pyHypoNodeList.item( iH );
4499 LDOM_Node textNode = pyHypoNode.getFirstChild();
4500 text = textNode.getNodeValue();
4501 Handle(_pyCommand) hypoCmd = new _pyCommand( text );
4502 hypType = hypoCmd->GetResultValue();
4503 method = hypoCmd->GetMethod();
4504 if ( !hypType.IsEmpty() && !method.IsEmpty() )
4506 map<_AString, Handle(_pyHypothesis)>::iterator type2hyp = myType2Hyp.find( hypType );
4507 if ( type2hyp == myType2Hyp.end() )
4508 hyp = new _pyHypothesis( hypoCmd );
4510 hyp = type2hyp->second;
4511 hyp->SetConvMethodAndType( method, algoType );
4512 for ( int iArg = 1; iArg <= hypoCmd->GetNbArgs(); ++iArg )
4514 _pyCommand argCmd( hypoCmd->GetArg( iArg ));
4515 _AString argMethod = argCmd.GetMethod();
4516 _AString argNbText = argCmd.GetArg( 1 );
4517 if ( argMethod.IsEmpty() && !argCmd.IsEmpty() )
4518 hyp->setCreationArg( 1, argCmd.GetString() ); // e.g. Parameters(smesh.SIMPLE)
4520 hyp->AddArgMethod( argMethod,
4521 argNbText.IsIntegerValue() ? argNbText.IntegerValue() : 1 );
4523 myType2Hyp[ hypType ] = hyp;
4527 // <hypothesis type="BLSURF_Parameters"
4531 // <accumulative-methods>
4532 // SetEnforcedVertex,
4533 // SetEnforcedVertexNamed
4534 // </accumulative-methods>
4538 LDOM_NodeList hypNodeList = xmlDoc.getElementsByTagName( "hypothesis" );
4539 for ( int i = 0; i < hypNodeList.getLength(); ++i )
4541 LDOM_Node hypNode = hypNodeList.item( i );
4542 LDOM_Element& hypElem = (LDOM_Element&) hypNode;
4543 _AString hypType = hypElem.getAttribute("type");
4544 LDOM_NodeList methNodeList = hypElem.getElementsByTagName( "accumulative-methods" );
4545 if ( methNodeList.getLength() != 1 || hypType.IsEmpty() ) continue;
4547 map<_AString, Handle(_pyHypothesis)>::const_iterator type2hyp = myType2Hyp.find( hypType );
4548 if ( type2hyp == myType2Hyp.end() ) continue;
4550 LDOM_Node methNode = methNodeList.item( 0 );
4551 LDOM_Node textNode = methNode.getFirstChild();
4552 _AString text = textNode.getNodeValue();
4556 method = _pyCommand::GetWord( text, pos, /*forward= */true );
4557 pos += method.Length();
4558 type2hyp->second->AddAccumulativeMethod( method );
4560 while ( !method.IsEmpty() );
4563 } // loop on xmlPaths
4566 //================================================================================
4568 * \brief Returns a new hypothesis initialized according to the read information
4570 //================================================================================
4572 Handle(_pyHypothesis)
4573 _pyHypothesisReader::GetHypothesis(const _AString& hypType,
4574 const Handle(_pyCommand)& creationCmd) const
4576 Handle(_pyHypothesis) resHyp, sampleHyp;
4578 map<_AString, Handle(_pyHypothesis)>::const_iterator type2hyp = myType2Hyp.find( hypType );
4579 if ( type2hyp != myType2Hyp.end() )
4580 sampleHyp = type2hyp->second;
4582 if ( sampleHyp.IsNull() )
4584 resHyp = new _pyHypothesis(creationCmd);
4588 if ( sampleHyp->IsAlgo() )
4589 resHyp = new _pyAlgorithm( creationCmd );
4591 resHyp = new _pyHypothesis(creationCmd);
4592 resHyp->Assign( sampleHyp, _pyID() );