+
+//================================================================================
+/*!
+ * \brief SelfEraser erases creation command if no more it's commands invoked
+ */
+//================================================================================
+
+void _pySelfEraser::Flush()
+{
+ if ( GetNbCalls() == 0 )
+ GetCreationCmd()->Clear();
+}
+
+//================================================================================
+/*!
+ * \brief _pySubMesh constructor
+ */
+//================================================================================
+
+_pySubMesh::_pySubMesh(const Handle(_pyCommand)& theCreationCmd):
+ _pyObject(theCreationCmd)
+{
+ myMesh = ObjectToMesh( theGen->FindObject( theCreationCmd->GetObject() ));
+}
+
+//================================================================================
+/*!
+ * \brief Return true if a sub-mesh can be used as argument of the given method
+ */
+//================================================================================
+
+bool _pySubMesh::CanBeArgOfMethod(const _AString& theMethodName)
+{
+ // names of all methods where a sub-mesh can be used as argument
+ static TStringSet methods;
+ if ( methods.empty() ) {
+ const char * names[] = {
+ // methods of SMESH_Gen
+ "CopyMesh",
+ // methods of SMESH_Group
+ "AddFrom",
+ // methods of SMESH_Measurements
+ "MinDistance",
+ // methods of SMESH_Mesh
+ "ExportPartToMED","ExportCGNS","ExportPartToDAT","ExportPartToUNV","ExportPartToSTL",
+ "RemoveSubMesh",
+ // methods of SMESH_MeshEditor
+ "ReorientObject","Reorient2D","TriToQuadObject","QuadToTriObject","SplitQuadObject",
+ "SplitVolumesIntoTetra","SmoothObject","SmoothParametricObject","ConvertFromQuadraticObject",
+ "RotationSweepObject","RotationSweepObjectMakeGroups","RotationSweepObject1D",
+ "RotationSweepObject1DMakeGroups","RotationSweepObject2D","RotationSweepObject2DMakeGroups",
+ "ExtrusionSweepObject","ExtrusionSweepObjectMakeGroups","ExtrusionSweepObject0D",
+ "ExtrusionSweepObject0DMakeGroups","ExtrusionSweepObject1D","ExtrusionSweepObject2D",
+ "ExtrusionSweepObject1DMakeGroups","ExtrusionSweepObject2DMakeGroups",
+ "ExtrusionAlongPathObjX","ExtrusionAlongPathObject","ExtrusionAlongPathObjectMakeGroups",
+ "ExtrusionAlongPathObject1D","ExtrusionAlongPathObject1DMakeGroups",
+ "ExtrusionAlongPathObject2D","ExtrusionAlongPathObject2DMakeGroups","MirrorObject",
+ "MirrorObjectMakeGroups","MirrorObjectMakeMesh","TranslateObject","Scale",
+ "TranslateObjectMakeGroups","TranslateObjectMakeMesh","ScaleMakeGroups","ScaleMakeMesh",
+ "RotateObject","RotateObjectMakeGroups","RotateObjectMakeMesh","FindCoincidentNodesOnPart",
+ "FindCoincidentNodesOnPartBut","FindEqualElements","FindAmongElementsByPoint",
+ "MakeBoundaryMesh","Create0DElementsOnAllNodes",
+ "" }; // <- mark of end
+ methods.Insert( names );
+ }
+ return methods.Contains( theMethodName );
+}
+
+//================================================================================
+/*!
+ * \brief count invoked commands
+ */
+//================================================================================
+
+void _pySubMesh::Process( const Handle(_pyCommand)& theCommand )
+{
+ _pyObject::Process(theCommand); // count calls of Process()
+ GetCreationCmd()->AddDependantCmd( theCommand );
+}
+
+//================================================================================
+/*!
+ * \brief Move creation command depending on invoked commands
+ */
+//================================================================================
+
+void _pySubMesh::Flush()
+{
+ if ( GetNbCalls() == 0 ) // move to the end of all commands
+ theGen->GetLastCommand()->AddDependantCmd( GetCreationCmd() );
+ else if ( !myCreator.IsNull() )
+ // move to be just after creator
+ myCreator->GetCreationCmd()->AddDependantCmd( GetCreationCmd() );
+}
+
+//================================================================================
+/*!
+ * \brief Creates _pyGroup
+ */
+//================================================================================
+
+_pyGroup::_pyGroup(const Handle(_pyCommand)& theCreationCmd, const _pyID & id)
+ :_pySubMesh(theCreationCmd)
+{
+ if ( !id.IsEmpty() )
+ setID( id );
+
+ myCanClearCreationCmd = true;
+
+ const _AString& method = theCreationCmd->GetMethod();
+ if ( method == "CreateGroup" ) // CreateGroup() --> CreateEmptyGroup()
+ {
+ theCreationCmd->SetMethod( "CreateEmptyGroup" );
+ }
+ // ----------------------------------------------------------------------
+ else if ( method == "CreateGroupFromGEOM" ) // (type, name, grp)
+ {
+ _pyID geom = theCreationCmd->GetArg( 3 );
+ // VSR 24/12/2010. PAL21106: always use GroupOnGeom() function on dump
+ // next if(){...} section is commented
+ //if ( sameGroupType( geom, theCreationCmd->GetArg( 1 )) ) { // --> Group(geom)
+ // theCreationCmd->SetMethod( "Group" );
+ // theCreationCmd->RemoveArgs();
+ // theCreationCmd->SetArg( 1, geom );
+ //}
+ //else {
+ // ------------------------->>>>> GroupOnGeom( geom, name, typ )
+ _pyID type = theCreationCmd->GetArg( 1 );
+ _pyID name = theCreationCmd->GetArg( 2 );
+ theCreationCmd->SetMethod( "GroupOnGeom" );
+ theCreationCmd->RemoveArgs();
+ theCreationCmd->SetArg( 1, geom );
+ theCreationCmd->SetArg( 2, name );
+ theCreationCmd->SetArg( 3, type );
+ //}
+ }
+ else if ( method == "CreateGroupFromFilter" )
+ {
+ // -> GroupOnFilter(typ, name, aFilter0x4743dc0 -> aFilter_1)
+ theCreationCmd->SetMethod( "GroupOnFilter" );
+
+ _pyID filterID = theCreationCmd->GetArg(3);
+ Handle(_pyFilter) filter = Handle(_pyFilter)::DownCast( theGen->FindObject( filterID ));
+ if ( !filter.IsNull())
+ {
+ if ( !filter->GetNewID().IsEmpty() )
+ theCreationCmd->SetArg( 3, filter->GetNewID() );
+ filter->AddUser( this );
+ }
+ myFilter = filter;
+ }
+ else
+ {
+ // theCreationCmd does something else apart from creation of this group
+ // and thus it can't be cleared if this group is removed
+ myCanClearCreationCmd = false;
+ }
+}
+
+//================================================================================
+/*!
+ * \brief set myCanClearCreationCmd = true if the main action of the creation
+ * command is discarded
+ */
+//================================================================================
+
+void _pyGroup::RemovedWithContents()
+{
+ // this code would be appropriate if Add0DElementsToAllNodes() returned only new nodes
+ // via a created group
+ //if ( GetCreationCmd()->GetMethod() == "Add0DElementsToAllNodes")
+ // myCanClearCreationCmd = true;
+}
+
+//================================================================================
+/*!
+ * \brief To convert creation of a group by filter
+ */
+//================================================================================
+
+void _pyGroup::Process( const Handle(_pyCommand)& theCommand)
+{
+ // Convert the following set of commands into mesh.MakeGroupByFilter(groupName, theFilter)
+ // group = mesh.CreateEmptyGroup( elemType, groupName )
+ // aFilter.SetMesh(mesh)
+ // nbAdd = group.AddFrom( aFilter )
+ Handle(_pyFilter) filter;
+ if ( theCommand->GetMethod() == "AddFrom" )
+ {
+ _pyID idSource = theCommand->GetArg(1);
+ // check if idSource is a filter
+ filter = Handle(_pyFilter)::DownCast( theGen->FindObject( idSource ));
+ if ( !filter.IsNull() )
+ {
+ // find aFilter.SetMesh(mesh) to clear it, it should be just before theCommand
+ list< Handle(_pyCommand) >::reverse_iterator cmdIt = theGen->GetCommands().rbegin();
+ while ( *cmdIt != theCommand ) ++cmdIt;
+ while ( (*cmdIt)->GetOrderNb() != 1 )
+ {
+ const Handle(_pyCommand)& setMeshCmd = *(++cmdIt);
+ if ((setMeshCmd->GetObject() == idSource ||
+ setMeshCmd->GetObject() == filter->GetNewID() )
+ &&
+ setMeshCmd->GetMethod() == "SetMesh")
+ {
+ setMeshCmd->Clear();
+ break;
+ }
+ }
+ // replace 3 commands by one
+ theCommand->Clear();
+ const Handle(_pyCommand)& makeGroupCmd = GetCreationCmd();
+ TCollection_AsciiString name = makeGroupCmd->GetArg( 2 );
+ if ( !filter->GetNewID().IsEmpty() )
+ idSource = filter->GetNewID();
+ makeGroupCmd->SetMethod( "MakeGroupByFilter" );
+ makeGroupCmd->SetArg( 1, name );
+ makeGroupCmd->SetArg( 2, idSource );
+ }
+ }
+ else if ( theCommand->GetMethod() == "SetFilter" )
+ {
+ // set new name of a filter or clear the command if the same filter is set
+ _pyID filterID = theCommand->GetArg(1);
+ filter = Handle(_pyFilter)::DownCast( theGen->FindObject( filterID ));
+ if ( !myFilter.IsNull() && filter == myFilter )
+ theCommand->Clear();
+ else if ( !filter.IsNull() && !filter->GetNewID().IsEmpty() )
+ theCommand->SetArg( 1, filter->GetNewID() );
+ myFilter = filter;
+ }
+ else if ( theCommand->GetMethod() == "GetFilter" )
+ {
+ // GetFilter() returns a filter with other ID, make myFilter process
+ // calls of the returned filter
+ if ( !myFilter.IsNull() )
+ {
+ theGen->SetProxyObject( theCommand->GetResultValue(), myFilter );
+ theCommand->Clear();
+ }
+ }
+
+ if ( !filter.IsNull() )
+ filter->AddUser( this );
+
+ theGen->AddMeshAccessorMethod( theCommand );
+}
+
+//================================================================================
+/*!
+ * \brief Prevent clearing "DoubleNode...() command if a group created by it is removed
+ */
+//================================================================================
+
+void _pyGroup::Flush()
+{
+ if ( !theGen->IsToKeepAllCommands() &&
+ myCreationCmd && !myCanClearCreationCmd )
+ {
+ myCreationCmd.Nullify(); // this way myCreationCmd won't be cleared
+ }
+}
+
+//================================================================================
+/*!
+ * \brief Constructor of _pyFilter
+ */
+//================================================================================
+
+_pyFilter::_pyFilter(const Handle(_pyCommand)& theCreationCmd, const _pyID& newID/*=""*/)
+ :_pyObject(theCreationCmd), myNewID( newID )
+{
+}
+
+//================================================================================
+/*!
+ * \brief To convert creation of a filter by criteria and
+ * to replace an old name by a new one
+ */
+//================================================================================
+
+void _pyFilter::Process( const Handle(_pyCommand)& theCommand)
+{
+ if ( theCommand->GetObject() == GetID() )
+ _pyObject::Process(theCommand); // count commands
+
+ if ( !myNewID.IsEmpty() )
+ theCommand->SetObject( myNewID );
+
+ // Convert the following set of commands into smesh.GetFilterFromCriteria(criteria)
+ // aFilter0x2aaab0487080 = aFilterManager.CreateFilter()
+ // aFilter0x2aaab0487080.SetCriteria(aCriteria)
+ if ( GetNbCalls() == 1 && // none method was called before this SetCriteria() call
+ theCommand->GetMethod() == "SetCriteria")
+ {
+ // aFilter.SetCriteria(aCriteria) ->
+ // aFilter = smesh.GetFilterFromCriteria(criteria)
+ if ( myNewID.IsEmpty() )
+ theCommand->SetResultValue( GetID() );
+ else
+ theCommand->SetResultValue( myNewID );
+ theCommand->SetObject( SMESH_2smeshpy::GenName() );
+ theCommand->SetMethod( "GetFilterFromCriteria" );
+
+ // Clear aFilterManager.CreateFilter()
+ GetCreationCmd()->Clear();
+ }
+ else if ( theCommand->GetMethod() == "SetMesh" )
+ {
+ if ( myMesh == theCommand->GetArg( 1 ))
+ theCommand->Clear();
+ else
+ myMesh = theCommand->GetArg( 1 );
+ theGen->AddMeshAccessorMethod( theCommand );
+ }
+}
+
+//================================================================================
+/*!
+ * \brief Set new filter name to the creation command
+ */
+//================================================================================
+
+void _pyFilter::Flush()
+{
+ if ( !myNewID.IsEmpty() && !GetCreationCmd()->IsEmpty() )
+ GetCreationCmd()->SetResultValue( myNewID );
+}
+
+//================================================================================
+/*!
+ * \brief Return true if all my users can be cleared
+ */
+//================================================================================
+
+bool _pyFilter::CanClear()
+{
+ list< Handle(_pyObject) >::iterator obj = myUsers.begin();
+ for ( ; obj != myUsers.end(); ++obj )
+ if ( !(*obj)->CanClear() )
+ return false;
+
+ return true;
+}
+
+//================================================================================
+/*!
+ * \brief Reads _pyHypothesis'es from resource files of mesher Plugins
+ */
+//================================================================================
+
+_pyHypothesisReader::_pyHypothesisReader()
+{
+ // Read xml files
+ vector< string > xmlPaths = SMESH_Gen::GetPluginXMLPaths();
+ LDOMParser xmlParser;
+ for ( size_t i = 0; i < xmlPaths.size(); ++i )
+ {
+ bool error = xmlParser.parse( xmlPaths[i].c_str() );
+ if ( error )
+ {
+ _AString data;
+ INFOS( xmlParser.GetError(data) );
+ continue;
+ }
+ // <algorithm type="Regular_1D"
+ // label-id="Wire discretisation"
+ // ...>
+ // <python-wrap>
+ // <algo>Regular_1D=Segment()</algo>
+ // <hypo>LocalLength=LocalLength(SetLength(1),,SetPrecision(1))</hypo>
+ //
+ LDOM_Document xmlDoc = xmlParser.getDocument();
+ LDOM_NodeList algoNodeList = xmlDoc.getElementsByTagName( "algorithm" );
+ for ( int i = 0; i < algoNodeList.getLength(); ++i )
+ {
+ LDOM_Node algoNode = algoNodeList.item( i );
+ LDOM_Element& algoElem = (LDOM_Element&) algoNode;
+ LDOM_NodeList pyAlgoNodeList = algoElem.getElementsByTagName( "algo" );
+ if ( pyAlgoNodeList.getLength() < 1 ) continue;
+
+ _AString text, algoType, method, arg;
+ for ( int iA = 0; iA < pyAlgoNodeList.getLength(); ++iA )
+ {
+ LDOM_Node pyAlgoNode = pyAlgoNodeList.item( iA );
+ LDOM_Node textNode = pyAlgoNode.getFirstChild();
+ text = textNode.getNodeValue();
+ Handle(_pyCommand) algoCmd = new _pyCommand( text );
+ algoType = algoCmd->GetResultValue();
+ method = algoCmd->GetMethod();
+ arg = algoCmd->GetArg(1);
+ if ( !algoType.IsEmpty() && !method.IsEmpty() )
+ {
+ Handle(_pyAlgorithm) algo = new _pyAlgorithm( algoCmd );
+ algo->SetConvMethodAndType( method, algoType );
+ if ( !arg.IsEmpty() )
+ algo->setCreationArg( 1, arg );
+
+ myType2Hyp[ algoType ] = algo;
+ break;
+ }
+ }
+ if ( algoType.IsEmpty() ) continue;
+
+ LDOM_NodeList pyHypoNodeList = algoElem.getElementsByTagName( "hypo" );
+ _AString hypType;
+ Handle( _pyHypothesis ) hyp;
+ for ( int iH = 0; iH < pyHypoNodeList.getLength(); ++iH )
+ {
+ LDOM_Node pyHypoNode = pyHypoNodeList.item( iH );
+ LDOM_Node textNode = pyHypoNode.getFirstChild();
+ text = textNode.getNodeValue();
+ Handle(_pyCommand) hypoCmd = new _pyCommand( text );
+ hypType = hypoCmd->GetResultValue();
+ method = hypoCmd->GetMethod();
+ if ( !hypType.IsEmpty() && !method.IsEmpty() )
+ {
+ map<_AString, Handle(_pyHypothesis)>::iterator type2hyp = myType2Hyp.find( hypType );
+ if ( type2hyp == myType2Hyp.end() )
+ hyp = new _pyHypothesis( hypoCmd );
+ else
+ hyp = type2hyp->second;
+ hyp->SetConvMethodAndType( method, algoType );
+ for ( int iArg = 1; iArg <= hypoCmd->GetNbArgs(); ++iArg )
+ {
+ _pyCommand argCmd( hypoCmd->GetArg( iArg ));
+ _AString argMethod = argCmd.GetMethod();
+ _AString argNbText = argCmd.GetArg( 1 );
+ if ( argMethod.IsEmpty() && !argCmd.IsEmpty() )
+ hyp->setCreationArg( 1, argCmd.GetString() ); // e.g. Parameters(smesh.SIMPLE)
+ else
+ hyp->AddArgMethod( argMethod,
+ argNbText.IsIntegerValue() ? argNbText.IntegerValue() : 1 );
+ }
+ myType2Hyp[ hypType ] = hyp;
+ }
+ }
+ }
+ // <hypothesis type="BLSURF_Parameters"
+ // ...
+ // dim="2">
+ // <python-wrap>
+ // <accumulative-methods>
+ // SetEnforcedVertex,
+ // SetEnforcedVertexNamed
+ // </accumulative-methods>
+ // </python-wrap>
+ // </hypothesis>
+ //
+ LDOM_NodeList hypNodeList = xmlDoc.getElementsByTagName( "hypothesis" );
+ for ( int i = 0; i < hypNodeList.getLength(); ++i )
+ {
+ LDOM_Node hypNode = hypNodeList.item( i );
+ LDOM_Element& hypElem = (LDOM_Element&) hypNode;
+ _AString hypType = hypElem.getAttribute("type");
+ LDOM_NodeList methNodeList = hypElem.getElementsByTagName( "accumulative-methods" );
+ if ( methNodeList.getLength() != 1 || hypType.IsEmpty() ) continue;
+
+ map<_AString, Handle(_pyHypothesis)>::const_iterator type2hyp = myType2Hyp.find( hypType );
+ if ( type2hyp == myType2Hyp.end() ) continue;
+
+ LDOM_Node methNode = methNodeList.item( 0 );
+ LDOM_Node textNode = methNode.getFirstChild();
+ _AString text = textNode.getNodeValue();
+ _AString method;
+ int pos = 1;
+ do {
+ method = _pyCommand::GetWord( text, pos, /*forward= */true );
+ pos += method.Length();
+ type2hyp->second->AddAccumulativeMethod( method );
+ }
+ while ( !method.IsEmpty() );
+ }
+
+ } // loop on xmlPaths
+}
+
+//================================================================================
+/*!
+ * \brief Returns a new hypothesis initialized according to the read information
+ */
+//================================================================================
+
+Handle(_pyHypothesis)
+_pyHypothesisReader::GetHypothesis(const _AString& hypType,
+ const Handle(_pyCommand)& creationCmd) const
+{
+ Handle(_pyHypothesis) resHyp, sampleHyp;
+
+ map<_AString, Handle(_pyHypothesis)>::const_iterator type2hyp = myType2Hyp.find( hypType );
+ if ( type2hyp != myType2Hyp.end() )
+ sampleHyp = type2hyp->second;
+
+ if ( sampleHyp.IsNull() )
+ {
+ resHyp = new _pyHypothesis(creationCmd);
+ }
+ else
+ {
+ if ( sampleHyp->IsAlgo() )
+ resHyp = new _pyAlgorithm( creationCmd );
+ else
+ resHyp = new _pyHypothesis(creationCmd);
+ resHyp->Assign( sampleHyp, _pyID() );
+ }
+ return resHyp;
+}