Salome HOME
Merge from V6_main 01/04/2013
[plugins/hybridplugin.git] / src / GHS3DPlugin / GHS3DPlugin_GHS3D.cxx
index 6da9eec38bb19cd651e6931cf2c492e38177b143..843a8d3528e3f2af0afa7e16ef638f00a1d95984 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2004-2012  CEA/DEN, EDF R&D
+// Copyright (C) 2004-2013  CEA/DEN, EDF R&D
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -37,6 +37,8 @@
 #include "SMESH_MeshEditor.hxx"
 #include "SMESH_OctreeNode.hxx"
 #include "SMESH_Group.hxx"
+#include <SMESH_subMeshEventListener.hxx>
+#include <SMESH_HypoFilter.hxx>
 
 #include "SMDS_MeshElement.hxx"
 #include "SMDS_MeshNode.hxx"
@@ -107,10 +109,6 @@ extern "C"
 
 #define HOLE_ID -1
 
-#ifndef GHS3D_VERSION
-#define GHS3D_VERSION 41
-#endif
-
 typedef const list<const SMDS_MeshFace*> TTriaList;
 
 static void removeFile( const TCollection_AsciiString& fileName )
@@ -213,13 +211,10 @@ TopoDS_Shape GHS3DPlugin_GHS3D::entryToShape(std::string entry)
   GEOM::GEOM_Object_var aGeomObj;
   TopoDS_Shape S = TopoDS_Shape();
   SALOMEDS::SObject_var aSObj = myStudy->FindObjectID( entry.c_str() );
-  SALOMEDS::GenericAttribute_var anAttr;
-
-  if (!aSObj->_is_nil() && aSObj->FindAttribute(anAttr, "AttributeIOR")) {
-    SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
-    CORBA::String_var aVal = anIOR->Value();
-    CORBA::Object_var obj = myStudy->ConvertIORToObject(aVal);
+  if (!aSObj->_is_nil() ) {
+    CORBA::Object_var obj = aSObj->GetObject();
     aGeomObj = GEOM::GEOM_Object::_narrow(obj);
+    aSObj->UnRegister();
   }
   if ( !aGeomObj->_is_nil() )
     S = smeshGen_i->GeomObjectToShape( aGeomObj.in() );
@@ -479,11 +474,11 @@ static int findShapeID(SMESH_Mesh&          mesh,
     return meshDS->ShapeToIndex( solids(2) );
 }
 
-//=======================================================================
-//function : countShape
-//purpose  :
-//=======================================================================
-
+// //=======================================================================
+// //function : countShape
+// //purpose  :
+// //=======================================================================
+// 
 // template < class Mesh, class Shape >
 // static int countShape( Mesh* mesh, Shape shape ) {
 //   TopExp_Explorer expShape ( mesh->ShapeToMesh(), shape );
@@ -496,12 +491,12 @@ static int findShapeID(SMESH_Mesh&          mesh,
 //   }
 //   return nbShape;
 // }
-
-//=======================================================================
-//function : getShape
-//purpose  :
-//=======================================================================
-
+// 
+// //=======================================================================
+// //function : getShape
+// //purpose  :
+// //=======================================================================
+// 
 // template < class Mesh, class Shape, class Tab >
 // void getShape(Mesh* mesh, Shape shape, Tab *t_Shape) {
 //   TopExp_Explorer expShape ( mesh->ShapeToMesh(), shape );
@@ -514,11 +509,11 @@ static int findShapeID(SMESH_Mesh&          mesh,
 //   }
 //   return;
 // }
-
-// //=======================================================================
-// //function : findEdgeID
-// //purpose  :
-// //=======================================================================
+// 
+// // //=======================================================================
+// // //function : findEdgeID
+// // //purpose  :
+// // //=======================================================================
 // 
 // static int findEdgeID(const SMDS_MeshNode* aNode,
 //                       const SMESHDS_Mesh*  theMesh,
@@ -552,16 +547,14 @@ static int findShapeID(SMESH_Mesh&          mesh,
 //   delete [] t_Dist;
 //   return theMesh->ShapeToIndex( foundEdge );
 // }
-
-
-//=======================================================================
-//function : readGMFFile
-//purpose  : read GMF file with geometry associated to mesh
-// TODO
-//=======================================================================
-
-// static bool readGMFFile(
-//                         const int                       fileOpen,
+// 
+// 
+// // =======================================================================
+// // function : readGMFFile
+// // purpose  : read GMF file with geometry associated to mesh
+// // =======================================================================
+// 
+// static bool readGMFFile(const int                       fileOpen,
 //                         const char*                     theFileName, 
 //                         SMESH_Mesh&                     theMesh,
 //                         const int                       nbShape,
@@ -644,7 +637,7 @@ static int findShapeID(SMESH_Mesh&          mesh,
 // 
 //   
 //   // The keyword does not exist yet => to update when it is created
-// //   int nbTriangle = GmfStatKwd(InpMsh, GmfSubdomain);
+// //   int nbSubdomains = GmfStatKwd(InpMsh, GmfSubdomain);
 // //   int id_tri[3];
 // 
 // 
@@ -828,11 +821,11 @@ static int findShapeID(SMESH_Mesh&          mesh,
 // //     case GmfHexahedra:
 //     {
 //       int nodeDim, shapeID, *nodeID;
-//       SMDS_MeshNode** node;
+//       const SMDS_MeshNode** node;
 // //       std::vector< SMDS_MeshNode* > enfNode( nbRef );
 //       SMDS_MeshElement * aGMFElement;
 //       
-//       node    = new SMDS_MeshNode*[nbRef];
+//       node    = new const SMDS_MeshNode*[nbRef];
 //       nodeID  = new int[ nbRef ];
 // 
 //       for ( int iElem = 0; iElem < nbElem; iElem++ )
@@ -1001,11 +994,13 @@ static int findShapeID(SMESH_Mesh&          mesh,
 //purpose  : Update or create groups in mesh
 //=======================================================================
 
-static void addElemInMeshGroup(SMESH_Mesh* theMesh,
-                             const SMDS_MeshElement* anElem,
-                             std::string groupName,
-                             std::set<std::string> groupsToRemove)
+static void addElemInMeshGroup(SMESH_Mesh*             theMesh,
+                               const SMDS_MeshElement* anElem,
+                               std::string&            groupName,
+                               std::set<std::string>&  groupsToRemove)
 {
+  if ( !anElem ) return; // issue 0021776
+
   bool groupDone = false;
   SMESH_Mesh::GroupIteratorPtr grIt = theMesh->GetGroups();
   while (grIt->more()) {
@@ -1033,7 +1028,7 @@ static void addElemInMeshGroup(SMESH_Mesh* theMesh,
     groupDone = true;
   }
   if (!groupDone)
-    throw SALOME_Exception(LOCALIZED("A enforced vertex node was not added to a group"));
+    throw SALOME_Exception(LOCALIZED("A given element was not added to a group"));
 }
 
 
@@ -1331,6 +1326,7 @@ static bool readGMFFile(const char*                     theFile,
             // for ( int iRef = 0; iRef < nbRef; iRef++ )
             //   nodeAssigne[ nodeID[ iRef ]] = 1;
           }
+          break;
         case GmfTetrahedra:
           if ( elemSearcher ) {
             // Issue 0020682. Avoid creating nodes and tetras at place where
@@ -1779,8 +1775,8 @@ static bool writeGMFFile(const char*                                     theMesh
       TopAbs_State result = pntCls->GetPointState( myPoint );
       if ( result == TopAbs_OUT )
         continue;
-      if (pntCls->FindElementsByPoint(myPoint, SMDSAbs_Node, foundElems) == 0)
-        continue;
+      //if (pntCls->FindElementsByPoint(myPoint, SMDSAbs_Node, foundElems) == 0)
+      //continue;
 
 //       if ( result != TopAbs_IN )
 //         continue;
@@ -1821,13 +1817,14 @@ static bool writeGMFFile(const char*                                     theMesh
       return false;
     }
     int TypTab[] = {GmfSca};
+    double ValTab[] = {0.0};
     GmfSetKwd(idxRequired, GmfVertices, requiredNodes + solSize);
     GmfSetKwd(idxSol, GmfSolAtVertices, requiredNodes + solSize, 1, TypTab);
 //     int usedEnforcedNodes = 0;
 //     std::string gn = "";
     for (ghs3dNodeIt = theRequiredNodes.begin();ghs3dNodeIt != theRequiredNodes.end();++ghs3dNodeIt) {
       GmfSetLin(idxRequired, GmfVertices, (*ghs3dNodeIt)->X(), (*ghs3dNodeIt)->Y(), (*ghs3dNodeIt)->Z(), dummyint);
-      GmfSetLin(idxSol, GmfSolAtVertices, 0.0);
+      GmfSetLin(idxSol, GmfSolAtVertices, ValTab);
       if (theEnforcedNodes.find((*ghs3dNodeIt)) != theEnforcedNodes.end())
         gn = theEnforcedNodes.find((*ghs3dNodeIt))->second;
       aNodeGroupByGhs3dId[usedEnforcedNodes] = gn;
@@ -2745,7 +2742,7 @@ static bool writePoints (ofstream &                       theFile,
       if (theEnforcedVertices.find(coords) != theEnforcedVertices.end())
         continue;
         
-      double size = theNodeIDToSizeMap.find(nodeIt->first->GetID())->second;
+//      double size = theNodeIDToSizeMap.find(nodeIt->first->GetID())->second;
   //       theGhs3dIdToNodeMap.insert( make_pair( nbNodes + i, (*nodeIt) ));
   //       MESSAGE("Adding enforced node (" << x << "," << y <<"," << z << ")");
       // X Y Z PHY_SIZE DUMMY_INT
@@ -2753,7 +2750,7 @@ static bool writePoints (ofstream &                       theFile,
       << x << space 
       << y << space 
       << z << space
-      << size << space
+      << -1 << space
       << dummyint << space;
       theFile << std::endl;
       theEnforcedNodeIdToGhs3dIdMap.insert( make_pair( nodeIt->first->GetID(), aGhs3dID ));
@@ -4312,3 +4309,84 @@ bool GHS3DPlugin_GHS3D::importGMFMesh(const char* theGMFFileName, SMESH_Mesh& th
   theMesh.GetMeshDS()->Modified();
   return ok;
 }
+
+namespace
+{
+  //================================================================================
+  /*!
+   * \brief Sub-mesh event listener setting enforced elements as soon as an enforced
+   *        mesh is loaded
+   */
+  struct _EnforcedMeshRestorer : public SMESH_subMeshEventListener
+  {
+    _EnforcedMeshRestorer():
+      SMESH_subMeshEventListener( /*isDeletable = */true, Name() )
+    {}
+
+    //================================================================================
+    /*!
+     * \brief Returns an ID of listener
+     */
+    static const char* Name() { return "GHS3DPlugin_GHS3D::_EnforcedMeshRestorer"; }
+
+    //================================================================================
+    /*!
+     * \brief Treat events of the subMesh
+     */
+    void ProcessEvent(const int                       event,
+                      const int                       eventType,
+                      SMESH_subMesh*                  subMesh,
+                      SMESH_subMeshEventListenerData* data,
+                      const SMESH_Hypothesis*         hyp)
+    {
+      if ( SMESH_subMesh::SUBMESH_LOADED == event &&
+           SMESH_subMesh::COMPUTE_EVENT  == eventType &&
+           data &&
+           !data->mySubMeshes.empty() )
+      {
+        // An enforced mesh (subMesh->_father) has been loaded from hdf file
+        if ( GHS3DPlugin_Hypothesis* hyp = GetGHSHypothesis( data->mySubMeshes.front() ))
+          hyp->RestoreEnfElemsByMeshes();
+      }
+    }
+    //================================================================================
+    /*!
+     * \brief Returns GHS3DPlugin_Hypothesis used to compute a subMesh
+     */
+    static GHS3DPlugin_Hypothesis* GetGHSHypothesis( SMESH_subMesh* subMesh )
+    {
+      SMESH_HypoFilter ghsHypFilter( SMESH_HypoFilter::HasName( "GHS3D_Parameters" ));
+      return (GHS3DPlugin_Hypothesis* )
+        subMesh->GetFather()->GetHypothesis( subMesh->GetSubShape(),
+                                             ghsHypFilter,
+                                             /*visitAncestors=*/true);
+    }
+  };
+}
+
+//================================================================================
+/*!
+ * \brief Set an event listener to set enforced elements as soon as an enforced
+ *        mesh is loaded
+ */
+//================================================================================
+
+void GHS3DPlugin_GHS3D::SubmeshRestored(SMESH_subMesh* subMesh)
+{
+  if ( GHS3DPlugin_Hypothesis* hyp = _EnforcedMeshRestorer::GetGHSHypothesis( subMesh ))
+  {
+    GHS3DPlugin_Hypothesis::TGHS3DEnforcedMeshList enfMeshes = hyp->_GetEnforcedMeshes();
+    GHS3DPlugin_Hypothesis::TGHS3DEnforcedMeshList::iterator it = enfMeshes.begin();
+    for(;it != enfMeshes.end();++it) {
+      GHS3DPlugin_Hypothesis::TGHS3DEnforcedMesh* enfMesh = *it;
+      if ( SMESH_Mesh* mesh = GetMeshByPersistentID( enfMesh->persistID ))
+      {
+        SMESH_subMesh* smToListen = mesh->GetSubMesh( mesh->GetShapeToMesh() );
+        // a listener set to smToListen will care of hypothesis stored in SMESH_EventListenerData
+        subMesh->SetEventListener( new _EnforcedMeshRestorer(),
+                                   SMESH_subMeshEventListenerData::MakeData( subMesh ),
+                                   smToListen);
+      }
+    }
+  }
+}