Salome HOME
Fix dump of ExportGMF() and ExportCGNS()
[modules/smesh.git] / src / SMESH_I / SMESH_Mesh_i.cxx
index e0340e99aa7abae16d7c144ce5adbd74e71ef60a..721476f831d6885c5f88a1913dd897958c9966d9 100644 (file)
@@ -43,6 +43,7 @@
 #include "SMESH_MEDMesh_i.hxx"
 #include "SMESH_MeshEditor.hxx"
 #include "SMESH_MeshEditor_i.hxx"
+#include "SMESH_MeshPartDS.hxx"
 #include "SMESH_MesherHelper.hxx"
 #include "SMESH_PreMeshInfo.hxx"
 #include "SMESH_PythonDump.hxx"
 #include <OSD_File.hxx>
 #include <OSD_Path.hxx>
 #include <OSD_Protection.hxx>
+#include <Standard_OutOfMemory.hxx>
 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
 #include <TColStd_MapOfInteger.hxx>
 #include <TColStd_SequenceOfInteger.hxx>
 #include <TCollection_AsciiString.hxx>
 #include <TopExp.hxx>
 #include <TopExp_Explorer.hxx>
-#include <TopoDS_Compound.hxx>
-#include <TopTools_MapOfShape.hxx>
 #include <TopTools_MapIteratorOfMapOfShape.hxx>
+#include <TopTools_MapOfShape.hxx>
+#include <TopoDS_Compound.hxx>
 
 // STL Includes
 #include <algorithm>
@@ -304,7 +306,7 @@ void SMESH_Mesh_i::ClearSubMesh(CORBA::Long ShapeID)
 
 //=============================================================================
 /*!
- *
+ * Convert enum Driver_Mesh::Status to SMESH::DriverMED_ReadStatus
  */
 //=============================================================================
 
@@ -328,6 +330,30 @@ static SMESH::DriverMED_ReadStatus ConvertDriverMEDReadStatus (int theStatus)
   return res;
 }
 
+//=============================================================================
+/*!
+ * Convert ::SMESH_ComputeError to SMESH::ComputeError
+ */
+//=============================================================================
+
+static SMESH::ComputeError* ConvertComputeError( SMESH_ComputeErrorPtr errorPtr )
+{
+  SMESH::ComputeError_var errVar = new SMESH::ComputeError();
+  errVar->subShapeID = -1;
+  errVar->hasBadMesh = false;
+
+  if ( !errorPtr || errorPtr->IsOK() )
+  {
+    errVar->code = SMESH::COMPERR_OK;
+  }
+  else
+  {
+    errVar->code = ConvertDriverMEDReadStatus( errorPtr->myName );
+    errVar->comment = errorPtr->myComment.c_str();
+  }
+  return errVar._retn();
+}
+
 //=============================================================================
 /*!
  *  ImportMEDFile
@@ -451,6 +477,45 @@ int SMESH_Mesh_i::ImportSTLFile( const char* theFileName )
   return 1;
 }
 
+//================================================================================
+/*!
+ * \brief Imports data from a GMF file and returns an error description
+ */
+//================================================================================
+
+SMESH::ComputeError* SMESH_Mesh_i::ImportGMFFile( const char* theFileName )
+  throw (SALOME::SALOME_Exception)
+{
+  SMESH_ComputeErrorPtr error;
+  try {
+    error = _impl->GMFToMesh( theFileName );
+  }
+  catch ( std::bad_alloc& exc ) {
+    error = SMESH_ComputeError::New( Driver_Mesh::DRS_FAIL, "std::bad_alloc raised" );
+  }
+  catch ( Standard_OutOfMemory& exc ) {
+    error = SMESH_ComputeError::New( Driver_Mesh::DRS_FAIL, "Standard_OutOfMemory raised" );
+  }
+  catch (Standard_Failure& ex) {
+    error = SMESH_ComputeError::New( Driver_Mesh::DRS_FAIL, ex.DynamicType()->Name() );
+    if ( ex.GetMessageString() && strlen( ex.GetMessageString() ))
+      error->myComment += string(": ") + ex.GetMessageString();
+  }
+  catch ( SALOME_Exception& S_ex ) {
+    error = SMESH_ComputeError::New( Driver_Mesh::DRS_FAIL, S_ex.what() );
+  }
+  catch ( std::exception& exc ) {
+    error = SMESH_ComputeError::New( Driver_Mesh::DRS_FAIL, exc.what() );
+  }
+  catch (...) {
+    error = SMESH_ComputeError::New( Driver_Mesh::DRS_FAIL, "Unknown exception" );
+  }
+
+  CreateGroupServants();
+
+  return ConvertComputeError( error );
+}
+
 //=============================================================================
 /*!
  *
@@ -2838,37 +2903,6 @@ void SMESH_Mesh_i::ExportSTL (const char *file, const bool isascii)
   _impl->ExportSTL(file, isascii);
 }
 
-//=============================================================================
-/*!
- * \brief Class providing SMESHDS_Mesh API to SMESH_IDSource. 
- *        It is used to export a part of mesh as a whole mesh.
- */
-class SMESH_MeshPartDS : public SMESHDS_Mesh
-{
-public:
-  SMESH_MeshPartDS(SMESH::SMESH_IDSource_ptr meshPart);
-
-  virtual SMDS_NodeIteratorPtr   nodesIterator     (bool idInceasingOrder=false) const;
-  virtual SMDS_EdgeIteratorPtr   edgesIterator     (bool idInceasingOrder=false) const;
-  virtual SMDS_FaceIteratorPtr   facesIterator     (bool idInceasingOrder=false) const;
-  virtual SMDS_VolumeIteratorPtr volumesIterator   (bool idInceasingOrder=false) const;
-
-  virtual SMDS_ElemIteratorPtr elementsIterator(SMDSAbs_ElementType type=SMDSAbs_All) const;
-  virtual SMDS_ElemIteratorPtr elementGeomIterator(SMDSAbs_GeometryType type) const;
-  virtual SMDS_ElemIteratorPtr elementEntityIterator(SMDSAbs_EntityType type) const;
-
-private:
-  TIDSortedElemSet _elements[ SMDSAbs_NbElementTypes ];
-  SMESHDS_Mesh*    _meshDS;
-  /*!
-   * \brief Class used to access to protected data of SMDS_MeshInfo
-   */
-  struct TMeshInfo : public SMDS_MeshInfo
-  {
-    void Add(const SMDS_MeshElement* e) { SMDS_MeshInfo::addWithPoly( e ); }
-  };
-};
-
 //================================================================================
 /*!
  * \brief Export a part of mesh to a med file
@@ -2998,6 +3032,28 @@ void SMESH_Mesh_i::ExportCGNS(::SMESH::SMESH_IDSource_ptr meshPart,
 #endif
 }
 
+//================================================================================
+/*!
+ * \brief Export a part of mesh to a GMF file
+ */
+//================================================================================
+
+void SMESH_Mesh_i::ExportGMF(::SMESH::SMESH_IDSource_ptr meshPart,
+                             const char*                 file)
+  throw (SALOME::SALOME_Exception)
+{
+  Unexpect aCatch(SALOME_SalomeException);
+  if ( _preMeshInfo )
+    _preMeshInfo->FullLoadFromFile();
+
+  PrepareForWriting(file,/*overwrite=*/true);
+
+  SMESH_MeshPartDS partDS( meshPart );
+  _impl->ExportGMF(file, &partDS);
+
+  TPythonDump() << _this() << ".ExportGMF( " << meshPart<< ", r'" << file << "')";
+}
+
 //=============================================================================
 /*!
  * Return implementation of SALOME_MED::MESH interfaces
@@ -4083,7 +4139,7 @@ SMESH::double_array* SMESH_Mesh_i::BaryCenter(const CORBA::Long id)
  */
 //=============================================================================
 
-void SMESH_Mesh_i::CreateGroupServants() 
+void SMESH_Mesh_i::CreateGroupServants()
 {
   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
 
@@ -4901,6 +4957,27 @@ SMESH_MeshPartDS::SMESH_MeshPartDS(SMESH::SMESH_IDSource_ptr meshPart):
   }
 }
 // -------------------------------------------------------------------------------------
+SMESH_MeshPartDS::SMESH_MeshPartDS(const std::list< const SMDS_MeshElement* > & meshPart):
+  SMESHDS_Mesh( /*meshID=*/-1, /*isEmbeddedMode=*/true), _meshDS(0)
+{
+  TMeshInfo tmpInfo;
+  list< const SMDS_MeshElement* >::const_iterator partIt = meshPart.begin();
+  for ( ; partIt != meshPart.end(); ++partIt )
+    if ( const SMDS_MeshElement * e = *partIt )
+      if ( _elements[ e->GetType() ].insert( e ).second )
+      {
+        tmpInfo.Add( e );
+        SMDS_ElemIteratorPtr nIt = e->nodesIterator();
+        while ( nIt->more() )
+        {
+          const SMDS_MeshNode * n = (const SMDS_MeshNode*) nIt->next();
+          if ( _elements[ SMDSAbs_Node ].insert( n ).second )
+            tmpInfo.Add( n );
+        }
+      }
+  myInfo = tmpInfo;
+}
+// -------------------------------------------------------------------------------------
 SMDS_ElemIteratorPtr SMESH_MeshPartDS::elementGeomIterator(SMDSAbs_GeometryType geomType) const
 {
   if ( _meshDS ) return _meshDS->elementGeomIterator( geomType );
@@ -4973,3 +5050,5 @@ _GET_ITER_DEFINE( SMDS_VolumeIteratorPtr, volumesIterator, SMDS_MeshVolume, SMDS
 // END Implementation of SMESH_MeshPartDS
 //
 //================================================================================
+
+