]> SALOME platform Git repositories - plugins/ghs3dprlplugin.git/blobdiff - src/GHS3DPRLPlugin/GHS3DPRLPlugin_GHS3DPRL.cxx
Salome HOME
Merge from BR_V5_DEV 16Feb09
[plugins/ghs3dprlplugin.git] / src / GHS3DPRLPlugin / GHS3DPRLPlugin_GHS3DPRL.cxx
index 46f7df423a134abb86885e5c54ac906d36958fbd..0278eacc40d2762c10828897594a3ea4a0e7de33 100755 (executable)
@@ -1,34 +1,30 @@
-//  GHS3DPRLPlugin : C++ implementation
+// Copyright (C) 2007-2008 OPEN CASCADE, CEA/DEN, EDF R&D
 //
-//  Copyright (C) 2006  OPEN CASCADE, 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
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License.
 //
-//  This library is free software; you can redistribute it and/or
-//  modify it under the terms of the GNU Lesser General Public
-//  License as published by the Free Software Foundation; either
-//  version 2.1 of the License.
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
 //
-//  This library is distributed in the hope that it will be useful,
-//  but WITHOUT ANY WARRANTY; without even the implied warranty of
-//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-//  Lesser General Public License for more details.
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-//  You should have received a copy of the GNU Lesser General Public
-//  License along with this library; if not, write to the Free Software
-//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
-//  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
+// ---
 //
+// File   : GHS3DPRLPlugin_GHS3DPRL.cxx
+// Author : Christian VAN WAMBEKE (CEA) (from Hexotic plugin Lioka RAZAFINDRAZAKA)
 //
-// File    : GHS3DPRLPlugin_GHS3DPRL.cxx
-// Author  : Christian VAN WAMBEKE (CEA) (from Hexotic plugin Lioka RAZAFINDRAZAKA)
-// Date    : 2007/02/01
-// Project : SALOME
-//=============================================================================
-using namespace std;
+// ---
 
 #include "GHS3DPRLPlugin_GHS3DPRL.hxx"
 #include "GHS3DPRLPlugin_Hypothesis.hxx"
-// #include "GHS3DPRLPlugin_Mesher.hxx"
 
 #include "SMDS_MeshElement.hxx"
 #include "SMDS_MeshNode.hxx"
@@ -56,8 +52,17 @@ using namespace std;
 #include <list>
 #include <TCollection_AsciiString.hxx>
 
-//=============================================================================
+/* 
+extern "C" {
+#include <med.h>
+//#include <med_config.h>
+#include <med_utils.h>
+//#include <med_misc.h>
+#include <stdlib.h>
+}
+using namespace med_2_2;*/
 
+//=============================================================================
 GHS3DPRLPlugin_GHS3DPRL::GHS3DPRLPlugin_GHS3DPRL(int hypId, int studyId, SMESH_Gen* gen)
   : SMESH_3D_Algo(hypId, studyId, gen)
 {
@@ -70,21 +75,18 @@ GHS3DPRLPlugin_GHS3DPRL::GHS3DPRLPlugin_GHS3DPRL(int hypId, int studyId, SMESH_G
 }
 
 //=============================================================================
-
 GHS3DPRLPlugin_GHS3DPRL::~GHS3DPRLPlugin_GHS3DPRL()
 {
   MESSAGE("GHS3DPRLPlugin_GHS3DPRL::~GHS3DPRLPlugin_GHS3DPRL");
 }
 
 //=============================================================================
-
 bool GHS3DPRLPlugin_GHS3DPRL::CheckHypothesis
                          (SMESH_Mesh& aMesh,
                           const TopoDS_Shape& aShape,
                           SMESH_Hypothesis::Hypothesis_Status& aStatus)
 {
-  MESSAGE("GHS3DPRLPlugin_GHS3DPRL::CheckHypothesis");
-
+  //MESSAGE("GHS3DPRLPlugin_GHS3DPRL::CheckHypothesis");
   _hypothesis = NULL;
 
   list<const SMESHDS_Hypothesis*>::const_iterator itl;
@@ -115,13 +117,13 @@ bool GHS3DPRLPlugin_GHS3DPRL::CheckHypothesis
 }
 
 //=======================================================================
-
 static bool writeGHS3DPRLFiles (const TCollection_AsciiString &  GHS3DPRL_In,
                                 SMESHDS_Mesh *                   theMesh,
                                 map <int,int> &                  theSmdsToGHS3DPRLIdMap,
                                 map <int,const SMDS_MeshNode*> & theGHS3DPRLIdToNodeMap)
 {
    bool Ok;
+   int ifam=0;
    TCollection_AsciiString namefile(GHS3DPRL_In);
    namefile+=".points";
    OSD_File(namefile).Remove();
@@ -137,6 +139,7 @@ static bool writeGHS3DPRLFiles (const TCollection_AsciiString &  GHS3DPRL_In,
       INFOS("Can't write into "<<namefile.ToCString());
       return false;
    }
+   cout<<endl<<"writeGHS3DPRLFiles version 2.1 "<<endl;
    cout<<endl<<"Creating GHS3DPRL processed mesh file : "<<namefile<<endl;
 
    int nbVertices=theMesh->NbNodes();
@@ -147,16 +150,20 @@ static bool writeGHS3DPRLFiles (const TCollection_AsciiString &  GHS3DPRL_In,
    // Writing SMESH points into GHS3DPRL File.points
    theFile<<nbVertices<<endl;
 
-   int aSmdsNodeID = 1;
+   int aSmdsNodeID=1;
    const SMDS_MeshNode* node_2;
-   SMDS_NodeIteratorPtr itOnNode = theMesh->nodesIterator();
+   SMDS_NodeIteratorPtr itOnNode=theMesh->nodesIterator();
+   //int ifam=100;//test famille
+   theFile.precision(15); theFile.setf(ios::scientific,ios::floatfield);
+   //cout<<"set precision 15 on float\n";
    while (itOnNode->more())
    {
       node_2 = itOnNode->next();
       theSmdsToGHS3DPRLIdMap.insert(map <int,int>::value_type(node_2->GetID(),aSmdsNodeID));
       theGHS3DPRLIdToNodeMap.insert(map <int,const SMDS_MeshNode*>::value_type(aSmdsNodeID,node_2));
+      theFile<<node_2->X()<<space<<node_2->Y()<<space<<node_2->Z()<<space<<ifam<<endl;
       aSmdsNodeID++;
-      theFile<<node_2->X()<<space<<node_2->Y()<<space<<node_2->Z()<<space<<dummyint<<endl;
+      //if (aSmdsNodeID==11) ifam++;
    }
    //no specified points;
    theFile.close();
@@ -185,12 +192,15 @@ static bool writeGHS3DPRLFiles (const TCollection_AsciiString &  GHS3DPRL_In,
    SMDS_ElemIteratorPtr itOnFaceNode;
    SMDS_FaceIteratorPtr itOnSmdsFace = theMesh->facesIterator();
    long nbNoTriangles=0;
+   int ifaces=0;
+   //ifam=300;
    while (itOnSmdsFace->more())
    {
       aFace=itOnSmdsFace->next();
       itOnFaceNode=aFace->nodesIterator();
       const int nbNodes=aFace->NbNodes();
       if (nbNodes!=3) nbNoTriangles++;
+      ifaces++;
       theFile<<nbNodes<<space;        // NB_NODES
       while (itOnFaceNode->more())
       {
@@ -200,7 +210,9 @@ static bool writeGHS3DPRLFiles (const TCollection_AsciiString &  GHS3DPRL_In,
           theFile<<space<<(*itOnSmdsNode).second; //NODE_1 NODE_2 ...
       }
       //(NB_NODES+1) times: DUMMY_INT
-      for ( int i=0; i<=nbNodes; i++) theFile<<space<<dummyint;
+      //if (ifaces==11) ifam++;
+      theFile<<space<<ifam;
+      for ( int i=1; i<=nbNodes; i++) theFile<<space<<200+i;
       theFile<<endl;
    }
    theFile.close();
@@ -215,7 +227,6 @@ static bool writeGHS3DPRLFiles (const TCollection_AsciiString &  GHS3DPRL_In,
 }
 
 //=======================================================================
-
 static bool getInt( int & theValue, char * & theLine )
 {
   char *ptr;
@@ -231,7 +242,6 @@ static bool getInt( int & theValue, char * & theLine )
 }
 
 //=======================================================================
-
 static bool getDouble( double & theValue, char * & theLine )
 {
   char *ptr;
@@ -251,7 +261,6 @@ static bool getDouble( double & theValue, char * & theLine )
 {  aPtr = fgets( aBuf, GHS3DPRLPlugin_BUFLENGTH - 2, aFile ); aLineNb++; DUMP(endl); }
 
 //=======================================================================
-
 static bool readResult(FILE *                           theFile,
                        SMESHDS_Mesh *                   theMesh,
                        const TopoDS_Shape &             theShape,
@@ -375,24 +384,26 @@ static bool readResult(FILE *                           theFile,
 
 //=============================================================================
 // Pass parameters to GHS3DPRL
-
-void GHS3DPRLPlugin_GHS3DPRL::SetParameters(const GHS3DPRLPlugin_Hypothesis* hyp) {
+void GHS3DPRLPlugin_GHS3DPRL::SetParameters(const GHS3DPRLPlugin_Hypothesis* hyp)
+{
   if (hyp) {
     MESSAGE("GHS3DPRLPlugin_GHS3DPRL::SetParameters");
     _MEDName = hyp->GetMEDName();  //"DOMAIN\0"
     _NbPart = hyp->GetNbPart();
     _KeepFiles = hyp->GetKeepFiles();
+    _Background = hyp->GetBackground();
+    _ToMeshHoles = hyp->GetToMeshHoles();
   }
 }
 
 //=======================================================================
-
+//before launching salome
+//SALOME_TMP_DIR (for keep tepal intermediates files) could be set in user's directories
 static TCollection_AsciiString getTmpDir()
 {
   TCollection_AsciiString aTmpDir;
-
   char *Tmp_dir = getenv("SALOME_TMP_DIR");
-  if (Tmp_dir == NULL) Tmp_dir = getenv("TMPDIR");
+  if (Tmp_dir == NULL) Tmp_dir = getenv("TMP");
   if(Tmp_dir != NULL)
   {
     aTmpDir = Tmp_dir;
@@ -407,76 +418,65 @@ static TCollection_AsciiString getTmpDir()
 #ifdef WIN32
       aTmpDir = TCollection_AsciiString("C:\\");
 #else
-      aTmpDir = TCollection_AsciiString("/export/home/");
+      aTmpDir = TCollection_AsciiString("/tmp/");
 #endif
   }
-  //cout<<"getTmpDir()="<<aTmpDir<<endl;
   return aTmpDir;
 }
 
 //=============================================================================
 // Here we are going to use the GHS3DPRL mesher
-
-bool GHS3DPRLPlugin_GHS3DPRL::Compute(SMESH_Mesh&          theMesh,
-                                     const TopoDS_Shape& theShape) {
-
-
+bool GHS3DPRLPlugin_GHS3DPRL::Compute(SMESH_Mesh& theMesh,
+                                     const TopoDS_Shape& theShape)
+{
    bool Ok;
-   MESSAGE("GHS3DPRLPlugin_GHS3DPRL::Compute");
-
-   if (_hypothesis==NULL)
-   {
-      Ok = false;
-      cout <<"\nNo existing parameters/hypothesis for GHS3DPRL!\n\n";
-      return Ok;
-   }
-   SetParameters(_hypothesis);
-
-  cout << endl;
-  cout << _name << " parameters :" << endl;
-  cout << "     generic path/name of MED Files = " << _MEDName << endl;
-  cout << "     number of partitions = " << _NbPart << endl;
-  cout << "     keep intermediates files (from tepal) = " << _KeepFiles << endl;
-  cout << endl;
-
-  SMESHDS_Mesh* meshDS = theMesh.GetMeshDS();
-
-  if (_countSubMesh==0)
-  {
+   TCollection_AsciiString pluginerror("ghs3dprl: ");
+   SMESHDS_Mesh* meshDS = theMesh.GetMeshDS();
+   //cout<<"GetMeshDS done\n";
+   if (_countSubMesh==0){
+      MESSAGE("GHS3DPRLPlugin_GHS3DPRL::Compute");
       _countTotal=0;
       TopExp_Explorer expf(meshDS->ShapeToMesh(), TopAbs_SOLID);
-      for ( ; expf.More(); expf.Next() )
-          _countTotal++;
-  }
-
-  _countSubMesh++;
-
-  if (_countSubMesh == _countTotal )
-  {
+      for ( ; expf.More(); expf.Next() ) _countTotal++;
+   }
+   _countSubMesh++;
+   //cout<<"Compute _countSubMesh "<<_countSubMesh<<endl;
+   //no stuff if multiples submesh, multiple call compute
+   //mesh all in one pass tepal (the last)
+   if (_countSubMesh != _countTotal ) return true;
+
+   //stuff on last call 
+   if (_hypothesis==NULL){
+      pluginerror += "No existing parameters/hypothesis for GHS3DPRL";
+      cout <<"\n"<<pluginerror<<"\n\n";
+      error(COMPERR_ALGO_FAILED, pluginerror.ToCString() );
+      return false;
+   }
+   SetParameters(_hypothesis);
+   cout << "\n" << _name << " parameters :\n" << endl;
+   cout << "     generic path/name of MED Files = " << _MEDName << endl;
+   cout << "     number of partitions = " << _NbPart << endl;
+   cout << "     mesh holes = " << _ToMeshHoles << endl;
+   cout << "     keep intermediates files (from tepal) = " << _KeepFiles << endl;
+   cout << "     background (from tepal) = " << _Background << "\n\n";
+
+      //string tmpDir=getTmpDir_new();
       TCollection_AsciiString
          tmpDir=getTmpDir(),
-         GHS3DPRL_In,
-         GHS3DPRL_Out,
-         NbPart,
-         run_GHS3DPRL("tepal2med "),
-         run_keep_files("rm ");
-
-      //example: tepal2med GHS3DPRL 4 12500000 4 Test noMemu LaunchTepal
+         GHS3DPRL_In,GHS3DPRL_Out,GHS3DPRL_Outxml,
+         run_GHS3DPRL("tepal2med "),rm("rm "),run_nokeep_files,
+         NbPart,fileskinmed(""),path,casenamemed;  //_MEDName.c_str());
 
-      TCollection_AsciiString path;
-      TCollection_AsciiString casenamemed;//_MEDName.c_str());
       casenamemed += (char *)_MEDName.c_str();
       int n=casenamemed.SearchFromEnd('/');
-      if (n>0)
-      {
+      if (n>0) {
          path=casenamemed.SubString(1,n);
-         casenamemed=casenamemed.SubString(n+1,casenamemed.Length());
+         casenamemed=casenamemed.SubString(n+1,casenamemed.Length()); 
       }
       else
          path=tmpDir;
 
-      if (casenamemed.Length()>20)
-      {
+      if (casenamemed.Length()>20){
          casenamemed=casenamemed.SubString(1,20);
          cerr<<"MEDName truncated (no more 20 characters) = "<<casenamemed<<endl;
       }
@@ -485,69 +485,141 @@ bool GHS3DPRLPlugin_GHS3DPRL::Compute(SMESH_Mesh&          theMesh,
 
       map <int,int> aSmdsToGHS3DPRLIdMap;
       map <int,const SMDS_MeshNode*> aGHS3DPRLIdToNodeMap;
-      GHS3DPRL_In=path + "GHS3DPRL";
-      GHS3DPRL_Out=path + casenamemed;
+      GHS3DPRL_In = path + "GHS3DPRL";
+      GHS3DPRL_Out = path + casenamemed;
+      GHS3DPRL_Outxml = path + casenamemed + ".xml"; //master file
       NbPart=_NbPart;
-      run_GHS3DPRL += GHS3DPRL_In + " " + NbPart + " 12500000 1 Test noMemu LaunchTepal " + GHS3DPRL_Out;
-      run_keep_files += GHS3DPRL_In + ".*.*.* " + path + "tepal.out";
-      cout<<"GHS3DPRL command : "<<run_GHS3DPRL<<endl;
-
-      OSD_File( GHS3DPRL_Out ).Remove(); //only the master med file
+      //tepal2med --casename=/home/whoami/tmp/GHS3DPRL --number=5 --medname=DOMAIN 
+      //          --limitswap=1000 --verbose=0 --test=no --menu=no --launchtepal=yes;
+      
+      run_GHS3DPRL = run_GHS3DPRL +
+                     " --casename=" + GHS3DPRL_In + 
+                     " --number=" + NbPart + 
+                     " --medname=" + GHS3DPRL_Out +
+                     " --launchtepal=yes";
+      if (_ToMeshHoles) run_GHS3DPRL += " --meshholes=yes"; else run_GHS3DPRL += " --meshholes=no";
+      if (_Background) run_GHS3DPRL += " --background=yes"; else run_GHS3DPRL += " --background=no";
+      run_nokeep_files = rm +GHS3DPRL_In + "* " + path + "tepal.log";
+      system( run_nokeep_files.ToCString() ); //clean files
+      run_nokeep_files = rm + GHS3DPRL_In + "* ";
+
+      cout<<"GHS3DPRL command : "<<run_GHS3DPRL.ToCString()<<endl;
+      cout<<"Write files .faces .point ...";
+      GHS3DPRL_Out = path + casenamemed;
+      OSD_File( GHS3DPRL_Outxml ).Remove(); //only the master xml file
       Ok=writeGHS3DPRLFiles(GHS3DPRL_In, meshDS, aSmdsToGHS3DPRLIdMap, aGHS3DPRLIdToNodeMap);
+      if (Ok) {cout<<" ...done\n";}
+      else {
+         cout<<" ...NOT done\n";
+         pluginerror = pluginerror + "problem writing input tepal files " + GHS3DPRL_In + "[.faces|.points]";
+      }
+
+      //Ecriture dans un fichier MED ?v2.? meme si not Ok
+      //create empty file -> avoid warning message
+      //med_idt fid=MEDouvrir((const char *)fileskinmed.ToCString(),MED_CREATION);
+      //med_err ret=MEDfermer(fid);
+      //fileskinmed=fileskinmed + "cp /home/wambeke/empty.med "+ path + "GHS3DPRL_skin.med";
+      //system( fileskinmed.ToCString() );
+      fileskinmed=path + "GHS3DPRL_skin.med";
+      cout<<"Write file "<<fileskinmed<<"...";
+      theMesh.ExportMED(fileskinmed.ToCString(),"SKIN_INITIAL",true,1);
+      cout<<" ...done\n";
+
+      if (!Ok) {
+         error(COMPERR_ALGO_FAILED, pluginerror.ToCString());
+         return false; //pb abandonne
+      }
+
+      //sometimes it is better to wait flushing files on slow filesystem...
+      system( "sleep 3" );
+      system( run_GHS3DPRL.ToCString() );
+      system( "sleep 3" );
 
-      if (Ok) system( run_GHS3DPRL.ToCString() );
+      //cout<<"!!!reprise plugin!!!"<<endl;
+
+      if (_Background) {
+         pluginerror = pluginerror + "backgrounding... plugin is not waiting for output files "+ casenamemed + "_*.med";
+         cout<<pluginerror<<endl;
+         error(COMPERR_ALGO_FAILED, pluginerror.ToCString());
+         return false; //but it is not a problem but if true my message is overwritten
+         //return true; //but it is not a problem, 
+      }
 
       // read a result, GHS3DPRL_Out is the name of master file (previous xml format)
-      FILE * aResultFile = fopen( GHS3DPRL_Out.ToCString(), "r" );
-      if (aResultFile)
-      {
+      FILE * aResultFile = fopen( GHS3DPRL_Outxml.ToCString(), "r" );
+      if (aResultFile){
           //Ok = readResult( aResultFile, meshDS, theShape, aGHS3DPRLIdToNodeMap, GHS3DPRL_Out, _nodeRefNumber );
           Ok = true;
+          Ok = false; //but it is not a problem but if true my message is overwritten
           fclose(aResultFile);
-          cout << "GHS3DPRL OK output file "<<GHS3DPRL_Out<<" exist !\n\n";
-          if (!_KeepFiles)
-          {
-             system( run_keep_files.ToCString() );
-          }
+          cout<<"GHS3DPRL OK output master file "<<casenamemed<<".xml exist !\n\n";
+          pluginerror = pluginerror + "new tetaedra not in memory, but stored in files "+ casenamemed + "_*.med";
+          cout<<pluginerror<<endl;
+          error(COMPERR_ALGO_FAILED, pluginerror.ToCString());
+          if (!_KeepFiles) system( run_nokeep_files.ToCString() );
       }
-      else
-      {
-          Ok = false;
-          cout << "GHS3DPRL KO output files "<<GHS3DPRL_Out<<" do not exist ! see intermediates files keeped:\n";
-          TCollection_AsciiString run_keep_files("ls -alt ");
-          run_keep_files += GHS3DPRL_Out + "* " + GHS3DPRL_In + "* " + path + "tepal.out";
-          system( run_keep_files.ToCString() );
+      else{
+          Ok = false; //it is a problem AND my message is NOT overwritten
+          pluginerror = pluginerror + "output master file " + casenamemed + ".xml do not exist";
+          cout<<pluginerror<<endl;
+          error(COMPERR_ALGO_FAILED, pluginerror.ToCString() );
+          cout<<"GHS3DPRL KO output files "<<GHS3DPRL_Out<<" do not exist ! see intermediates files keeped:\n";
+          TCollection_AsciiString run_list_files("ls -alt ");
+          run_list_files +=  GHS3DPRL_Out + "* " + GHS3DPRL_In + "* " + path + "tepal.log";
+          system( run_list_files.ToCString() );
           cout<<endl;
       }
       _countSubMesh=0;
 
-  }
-  return Ok;
+
+    return Ok;
+   /*pid_t pid = fork();
+   if (pid > 0) {
+      //Processus père
+      cout<<"le pere est la\n";
+      system("echo le pere > lepere.tmp");
+      system("sleep 10");
+      system("ps -edf |grep wambeke > pslepere.tmp");
+      cout<<"le pere return 0\n";
+      return 0; //ok
+      //exit(0);
+   } else if (pid == 0) {
+      //Processus fils
+      cout<<"le fils est la\n";
+      //On rend le fils indépendant de tout terminal
+      setsid();
+      system("sleep 20");
+      system("echo le fils > lefils.tmp");
+      system("sleep 20");
+      system("ps -edf |grep wambeke > pslefils.tmp");
+      cout<<"le fils return 0\n";
+      return 0; //ok
+   } else {
+      //Traitement d'erreur
+      cout<<"ya probleme sur fork()\n";
+      return 1; //ko
+   }*/
 }
 
 //=============================================================================
-
 ostream & GHS3DPRLPlugin_GHS3DPRL::SaveTo(ostream & save)
 {
   return save;
 }
 
 //=============================================================================
-
 istream & GHS3DPRLPlugin_GHS3DPRL::LoadFrom(istream & load)
 {
   return load;
 }
 
 //=============================================================================
-
 ostream & operator << (ostream & save, GHS3DPRLPlugin_GHS3DPRL & hyp)
 {
   return hyp.SaveTo( save );
 }
 
 //=============================================================================
-
 istream & operator >> (istream & load, GHS3DPRLPlugin_GHS3DPRL & hyp)
 {
   return hyp.LoadFrom( load );