+//=============================================================================
+/*!
+ * Get the SMESH_subMesh object implementation. Dont create it, return null
+ * if it does not exist.
+ */
+//=============================================================================
+
+SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const int aShapeID) const
+throw(SALOME_Exception)
+{
+ SMESH_subMesh *aSubMesh = _subMeshHolder->Get( aShapeID );
+
+ return aSubMesh;
+}
+
+//================================================================================
+/*!
+ * \brief Return submeshes of groups containing the given sub-shape
+ */
+//================================================================================
+
+list<SMESH_subMesh*>
+SMESH_Mesh::GetGroupSubMeshesContaining(const TopoDS_Shape & aSubShape) const
+ throw(SALOME_Exception)
+{
+ list<SMESH_subMesh*> found;
+
+ SMESH_subMesh * subMesh = GetSubMeshContaining(aSubShape);
+ if ( !subMesh )
+ return found;
+
+ // submeshes of groups have max IDs, so search from the map end
+SMESH_subMeshIteratorPtr smIt( _subMeshHolder->GetIterator( /*reverse=*/true ) );
+ while ( smIt->more() ) {
+ SMESH_subMesh* sm = smIt->next();
+ SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
+ if ( ds && ds->IsComplexSubmesh() ) {
+ if ( SMESH_MesherHelper::IsSubShape( aSubShape, sm->GetSubShape() ))
+ {
+ found.push_back( sm );
+ //break;
+ }
+ } else {
+ break; // the rest sub-meshes are not those of groups
+ }
+ }
+
+ if ( found.empty() ) // maybe the main shape is a COMPOUND (issue 0021530)
+ {
+ if ( SMESH_subMesh * mainSM = GetSubMeshContaining(1) )
+ if ( mainSM->GetSubShape().ShapeType() == TopAbs_COMPOUND )
+ {
+ TopoDS_Iterator it( mainSM->GetSubShape() );
+ if ( it.Value().ShapeType() == aSubShape.ShapeType() &&
+ SMESH_MesherHelper::IsSubShape( aSubShape, mainSM->GetSubShape() ))
+ found.push_back( mainSM );
+ }
+ }
+ return found;
+}
+//=======================================================================
+//function : IsUsedHypothesis
+//purpose : Return True if anHyp is used to mesh aSubShape
+//=======================================================================
+
+bool SMESH_Mesh::IsUsedHypothesis(SMESHDS_Hypothesis * anHyp,
+ const SMESH_subMesh* aSubMesh)
+{
+ SMESH_Hypothesis* hyp = static_cast<SMESH_Hypothesis*>(anHyp);
+
+ // check if anHyp can be used to mesh aSubMesh
+ if ( !aSubMesh || !aSubMesh->IsApplicableHypotesis( hyp ))
+ return false;
+
+ SMESH_Algo *algo = aSubMesh->GetAlgo();
+
+ // algorithm
+ if (anHyp->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
+ return ( anHyp == algo );
+
+ // algorithm parameter
+ if (algo)
+ {
+ // look trough hypotheses used by algo
+ const SMESH_HypoFilter* hypoKind;
+ if (( hypoKind = algo->GetCompatibleHypoFilter( !hyp->IsAuxiliary() ))) {
+ list <const SMESHDS_Hypothesis * > usedHyps;
+ if ( GetHypotheses( aSubMesh, *hypoKind, usedHyps, true ))
+ return ( find( usedHyps.begin(), usedHyps.end(), anHyp ) != usedHyps.end() );
+ }
+ }
+
+ return false;
+}
+
+//=============================================================================
+/*!
+ *
+ */
+//=============================================================================
+
+// const list < SMESH_subMesh * >&
+// SMESH_Mesh::GetSubMeshUsingHypothesis(SMESHDS_Hypothesis * anHyp)
+// throw(SALOME_Exception)
+// {
+// _subMeshesUsingHypothesisList.clear();
+// SMESH_subMeshIteratorPtr smIt( _subMeshHolder->GetIterator() );
+// while ( smIt->more() )
+// {
+// SMESH_subMesh* aSubMesh = smIt->next();
+// if ( IsUsedHypothesis ( anHyp, aSubMesh ))
+// _subMeshesUsingHypothesisList.push_back( aSubMesh );
+// }
+// return _subMeshesUsingHypothesisList;
+// }
+
+//=======================================================================
+//function : NotifySubMeshesHypothesisModification
+//purpose : Say all submeshes using theChangedHyp that it has been modified
+//=======================================================================
+
+void SMESH_Mesh::NotifySubMeshesHypothesisModification(const SMESH_Hypothesis* hyp)
+{
+ Unexpect aCatch(SalomeException);
+
+ if ( !GetMeshDS()->IsUsedHypothesis( hyp ))
+ return;
+
+ if (_callUp)
+ _callUp->HypothesisModified();
+
+ SMESH_Algo *algo;
+ const SMESH_HypoFilter* compatibleHypoKind;
+ list <const SMESHDS_Hypothesis * > usedHyps;
+
+ // keep sub-meshes not to miss ones whose state can change due to notifying others
+ vector< SMESH_subMesh* > smToNotify;
+
+ SMESH_subMeshIteratorPtr smIt( _subMeshHolder->GetIterator() );
+ while ( smIt->more() )
+ {
+ SMESH_subMesh* aSubMesh = smIt->next();
+
+ // if aSubMesh meshing depends on hyp,
+ // we call aSubMesh->AlgoStateEngine( MODIF_HYP, hyp ) that causes either
+ // 1) clearing of already computed aSubMesh or
+ // 2) changing algo_state from MISSING_HYP to HYP_OK when parameters of hyp becomes valid,
+ // other possible changes are not interesting. (IPAL0052457 - assigning hyp performance pb)
+ if ( aSubMesh->GetComputeState() != SMESH_subMesh::COMPUTE_OK &&
+ aSubMesh->GetComputeState() != SMESH_subMesh::FAILED_TO_COMPUTE &&
+ aSubMesh->GetAlgoState() != SMESH_subMesh::MISSING_HYP )
+ continue;
+
+ const TopoDS_Shape & aSubShape = aSubMesh->GetSubShape();
+
+ if (( aSubMesh->IsApplicableHypotesis( hyp )) &&
+ ( algo = aSubMesh->GetAlgo() ) &&
+ ( compatibleHypoKind = algo->GetCompatibleHypoFilter( !hyp->IsAuxiliary() )) &&
+ ( compatibleHypoKind->IsOk( hyp, aSubShape )))
+ {
+ // check if hyp is used by algo
+ usedHyps.clear();
+ if ( GetHypotheses( aSubMesh, *compatibleHypoKind, usedHyps, true ) &&
+ find( usedHyps.begin(), usedHyps.end(), hyp ) != usedHyps.end() )
+ {
+ smToNotify.push_back( aSubMesh );
+ }
+ }
+ }
+
+ for ( size_t i = 0; i < smToNotify.size(); ++i )
+ {
+ smToNotify[i]->AlgoStateEngine(SMESH_subMesh::MODIF_HYP,
+ const_cast< SMESH_Hypothesis*>( hyp ));
+ }
+
+ HasModificationsToDiscard(); // to reset _isModified flag if mesh becomes empty
+ GetMeshDS()->Modified();
+}
+
+//=============================================================================
+/*!
+ * Auto color functionality
+ */
+//=============================================================================
+void SMESH_Mesh::SetAutoColor(bool theAutoColor) throw(SALOME_Exception)
+{
+ Unexpect aCatch(SalomeException);
+ _isAutoColor = theAutoColor;
+}
+
+bool SMESH_Mesh::GetAutoColor() throw(SALOME_Exception)
+{
+ Unexpect aCatch(SalomeException);
+ return _isAutoColor;
+}
+
+//=======================================================================
+//function : SetIsModified
+//purpose : Set the flag meaning that the mesh has been edited "manually"
+//=======================================================================
+
+void SMESH_Mesh::SetIsModified(bool isModified)
+{
+ _isModified = isModified;
+
+ if ( _isModified )
+ // check if mesh becomes empty as result of modification
+ HasModificationsToDiscard();
+}
+
+//=======================================================================
+//function : HasModificationsToDiscard
+//purpose : Return true if the mesh has been edited since a total re-compute
+// and those modifications may prevent successful partial re-compute.
+// As a side effect reset _isModified flag if mesh is empty
+//issue : 0020693
+//=======================================================================
+
+bool SMESH_Mesh::HasModificationsToDiscard() const
+{
+ if ( ! _isModified )
+ return false;
+
+ // return true if the next Compute() will be partial and
+ // existing but changed elements may prevent successful re-compute
+ bool hasComputed = false, hasNotComputed = false;
+SMESH_subMeshIteratorPtr smIt( _subMeshHolder->GetIterator() );
+ while ( smIt->more() )
+ {
+ const SMESH_subMesh* aSubMesh = smIt->next();
+ switch ( aSubMesh->GetSubShape().ShapeType() )
+ {
+ case TopAbs_EDGE:
+ case TopAbs_FACE:
+ case TopAbs_SOLID:
+ if ( aSubMesh->IsMeshComputed() )
+ hasComputed = true;
+ else
+ hasNotComputed = true;
+ if ( hasComputed && hasNotComputed)
+ return true;
+ }
+ }
+ if ( NbNodes() < 1 )
+ const_cast<SMESH_Mesh*>(this)->_isModified = false;
+
+ return false;
+}
+
+//================================================================================
+/*!
+ * \brief Check if any groups of the same type have equal names
+ */
+//================================================================================
+
+bool SMESH_Mesh::HasDuplicatedGroupNamesMED()
+{
+ //set<string> aGroupNames; // Corrected for Mantis issue 0020028
+ map< SMDSAbs_ElementType, set<string> > aGroupNames;
+ for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ )
+ {
+ SMESH_Group* aGroup = it->second;
+ SMDSAbs_ElementType aType = aGroup->GetGroupDS()->GetType();
+ string aGroupName = aGroup->GetName();
+ aGroupName.resize(MAX_MED_GROUP_NAME_LENGTH);
+ if (!aGroupNames[aType].insert(aGroupName).second)
+ return true;
+ }
+
+ return false;
+}
+
+//================================================================================
+/*!
+ * \brief Export the mesh to a med file
+ * \param [in] file - name of the MED file
+ * \param [in] theMeshName - name of this mesh
+ * \param [in] theAutoGroups - boolean parameter for creating/not creating
+ * the groups Group_On_All_Nodes, Group_On_All_Faces, ... ;
+ * the typical use is auto_groups=false.
+ * \param [in] theVersion - defines the version of format of MED file, that will be created
+ * \param [in] meshPart - mesh data to export
+ * \param [in] theAutoDimension - if \c true, a space dimension of a MED mesh can be either
+ * - 1D if all mesh nodes lie on OX coordinate axis, or
+ * - 2D if all mesh nodes lie on XOY coordinate plane, or
+ * - 3D in the rest cases.
+ * If \a theAutoDimension is \c false, the space dimension is always 3.
+ * \return int - mesh index in the file
+ */
+//================================================================================
+
+void SMESH_Mesh::ExportMED(const char * file,
+ const char* theMeshName,
+ bool theAutoGroups,
+ int theVersion,
+ const SMESHDS_Mesh* meshPart,
+ bool theAutoDimension,
+ bool theAddODOnVertices)
+ throw(SALOME_Exception)
+{
+ SMESH_TRY;
+
+ DriverMED_W_SMESHDS_Mesh myWriter;
+ myWriter.SetFile ( file, MED::EVersion(theVersion) );
+ myWriter.SetMesh ( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
+ myWriter.SetAutoDimension( theAutoDimension );
+ myWriter.AddODOnVertices ( theAddODOnVertices );
+ if ( !theMeshName )
+ myWriter.SetMeshId ( _id );
+ else {
+ myWriter.SetMeshId ( -1 );
+ myWriter.SetMeshName ( theMeshName );
+ }
+
+ if ( theAutoGroups ) {
+ myWriter.AddGroupOfNodes();
+ myWriter.AddGroupOfEdges();
+ myWriter.AddGroupOfFaces();
+ myWriter.AddGroupOfVolumes();
+ }
+
+ // Pass groups to writer. Provide unique group names.
+ //set<string> aGroupNames; // Corrected for Mantis issue 0020028
+ if ( !meshPart )
+ {
+ map< SMDSAbs_ElementType, set<string> > aGroupNames;
+ char aString [256];
+ int maxNbIter = 10000; // to guarantee cycle finish
+ for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ ) {
+ SMESH_Group* aGroup = it->second;
+ SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
+ if ( aGroupDS ) {
+ SMDSAbs_ElementType aType = aGroupDS->GetType();
+ string aGroupName0 = aGroup->GetName();
+ aGroupName0.resize(MAX_MED_GROUP_NAME_LENGTH);
+ string aGroupName = aGroupName0;
+ for (int i = 1; !aGroupNames[aType].insert(aGroupName).second && i < maxNbIter; i++) {
+ sprintf(&aString[0], "GR_%d_%s", i, aGroupName0.c_str());
+ aGroupName = aString;
+ aGroupName.resize(MAX_MED_GROUP_NAME_LENGTH);
+ }
+ aGroupDS->SetStoreName( aGroupName.c_str() );
+ myWriter.AddGroup( aGroupDS );
+ }
+ }
+ }
+ // Perform export
+ myWriter.Perform();
+
+ SMESH_CATCH( SMESH::throwSalomeEx );
+}
+
+//================================================================================
+/*!
+ * \brief Export the mesh to a SAUV file
+ */
+//================================================================================
+
+void SMESH_Mesh::ExportSAUV(const char *file,
+ const char* theMeshName,
+ bool theAutoGroups)
+ throw(SALOME_Exception)
+{
+ std::string medfilename(file);
+ medfilename += ".med";
+ std::string cmd;
+#ifdef WIN32
+ cmd = "%PYTHONBIN% ";
+#else
+ cmd = "python ";
+#endif
+ cmd += "-c \"";
+ cmd += "from medutilities import my_remove ; my_remove(r'" + medfilename + "')";
+ cmd += "\"";
+ system(cmd.c_str());
+ ExportMED(medfilename.c_str(), theMeshName, theAutoGroups, 1);
+#ifdef WIN32
+ cmd = "%PYTHONBIN% ";
+#else
+ cmd = "python ";
+#endif
+ cmd += "-c \"";
+ cmd += "from medutilities import convert ; convert(r'" + medfilename + "', 'MED', 'GIBI', 1, r'" + file + "')";
+ cmd += "\"";
+ system(cmd.c_str());
+#ifdef WIN32
+ cmd = "%PYTHONBIN% ";
+#else
+ cmd = "python ";
+#endif
+ cmd += "-c \"";
+ cmd += "from medutilities import my_remove ; my_remove(r'" + medfilename + "')";
+ cmd += "\"";
+ system(cmd.c_str());
+}
+
+//================================================================================
+/*!
+ * \brief Export the mesh to a DAT file
+ */
+//================================================================================
+
+void SMESH_Mesh::ExportDAT(const char * file,
+ const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
+{
+ Unexpect aCatch(SalomeException);
+ DriverDAT_W_SMDS_Mesh myWriter;
+ myWriter.SetFile( file );
+ myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
+ myWriter.SetMeshId(_id);
+ myWriter.Perform();
+}
+
+//================================================================================
+/*!
+ * \brief Export the mesh to an UNV file
+ */
+//================================================================================
+
+void SMESH_Mesh::ExportUNV(const char * file,
+ const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
+{
+ Unexpect aCatch(SalomeException);
+ DriverUNV_W_SMDS_Mesh myWriter;
+ myWriter.SetFile( file );
+ myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
+ myWriter.SetMeshId(_id);
+ // myWriter.SetGroups(_mapGroup);
+
+ if ( !meshPart )
+ {
+ for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ ) {
+ SMESH_Group* aGroup = it->second;
+ SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
+ if ( aGroupDS ) {
+ string aGroupName = aGroup->GetName();
+ aGroupDS->SetStoreName( aGroupName.c_str() );
+ myWriter.AddGroup( aGroupDS );
+ }
+ }