Salome HOME
0022401]: [CEA 992] Regression on SMESH using NETGEN 2D
[plugins/netgenplugin.git] / src / NETGENPlugin / NETGENPlugin_NETGEN_2D_ONLY.cxx
index a474e036b136b22274d0ed90ba4a9364a2e4ec65..27d459357a54a5391de794bdef72a5965ff456e9 100644 (file)
@@ -1,12 +1,11 @@
-// Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS, L3S, LJLL, MENSI
+// Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // 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
+// 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.
 //
 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+
 // File      : NETGENPlugin_NETGEN_2D_ONLY.cxx
 // Author    : Edward AGAPOV (OCC)
 // Project   : SALOME
-
-
+//
 #include "NETGENPlugin_NETGEN_2D_ONLY.hxx"
 
 #include "NETGENPlugin_Mesher.hxx"
-
-#include "SMDS_MeshElement.hxx"
-#include "SMDS_MeshNode.hxx"
-#include "SMESHDS_Mesh.hxx"
-#include "SMESH_Comment.hxx"
-#include "SMESH_Gen.hxx"
-#include "SMESH_Mesh.hxx"
-#include "SMESH_MesherHelper.hxx"
-#include "StdMeshers_FaceSide.hxx"
-#include "StdMeshers_MaxElementArea.hxx"
-#include "StdMeshers_LengthFromEdges.hxx"
-#include "StdMeshers_QuadranglePreference.hxx"
-
+#include "NETGENPlugin_Hypothesis_2D.hxx"
+
+#include <SMDS_MeshElement.hxx>
+#include <SMDS_MeshNode.hxx>
+#include <SMESHDS_Mesh.hxx>
+#include <SMESH_Comment.hxx>
+#include <SMESH_Gen.hxx>
+#include <SMESH_Mesh.hxx>
+#include <SMESH_MesherHelper.hxx>
+#include <SMESH_subMesh.hxx>
+#include <StdMeshers_FaceSide.hxx>
+#include <StdMeshers_LengthFromEdges.hxx>
+#include <StdMeshers_MaxElementArea.hxx>
+#include <StdMeshers_QuadranglePreference.hxx>
+#include <StdMeshers_ViscousLayers2D.hxx>
+
+#include <Precision.hxx>
 #include <Standard_ErrorHandler.hxx>
 #include <Standard_Failure.hxx>
 
-#include "utilities.h"
+#include <utilities.h>
 
 #include <list>
 #include <vector>
+#include <limits>
 
 /*
   Netgen include files
 namespace nglib {
 #include <nglib.h>
 }
+#ifndef OCCGEOMETRY
 #define OCCGEOMETRY
+#endif
 #include <occgeom.hpp>
 #include <meshing.hpp>
 //#include <meshtype.hpp>
 namespace netgen {
+#ifdef NETGEN_V5
+  extern int OCCGenerateMesh (OCCGeometry&, Mesh*&, MeshingParameters&, int, int);
+#else
   extern int OCCGenerateMesh (OCCGeometry&, Mesh*&, int, int, char*);
-  /*extern*/ MeshingParameters mparam;
+#endif
+  extern MeshingParameters mparam;
 }
 
 using namespace std;
 using namespace netgen;
 using namespace nglib;
 
+//#define DUMP_SEGMENTS
+
 //=============================================================================
 /*!
  *  
@@ -77,16 +89,19 @@ NETGENPlugin_NETGEN_2D_ONLY::NETGENPlugin_NETGEN_2D_ONLY(int hypId, int studyId,
 {
   MESSAGE("NETGENPlugin_NETGEN_2D_ONLY::NETGENPlugin_NETGEN_2D_ONLY");
   _name = "NETGEN_2D_ONLY";
-
+  
   _shapeType = (1 << TopAbs_FACE);// 1 bit /shape type
 
   _compatibleHypothesis.push_back("MaxElementArea");
   _compatibleHypothesis.push_back("LengthFromEdges");
   _compatibleHypothesis.push_back("QuadranglePreference");
+  _compatibleHypothesis.push_back("NETGEN_Parameters_2D");
+  _compatibleHypothesis.push_back("ViscousLayers2D");
 
   _hypMaxElementArea = 0;
   _hypLengthFromEdges = 0;
   _hypQuadranglePreference = 0;
+  _hypParameters = 0;
 }
 
 //=============================================================================
@@ -113,12 +128,17 @@ bool NETGENPlugin_NETGEN_2D_ONLY::CheckHypothesis (SMESH_Mesh&         aMesh,
   _hypMaxElementArea = 0;
   _hypLengthFromEdges = 0;
   _hypQuadranglePreference = 0;
-
-  aStatus = HYP_MISSING;
+  _progressByTic = -1;
 
   const list<const SMESHDS_Hypothesis*>& hyps = GetUsedHypothesis(aMesh, aShape, false);
 
-  if (hyps.empty()) return false;  // can't work with no hypothesis
+  if (hyps.empty())
+  {
+    aStatus = HYP_OK; //SMESH_Hypothesis::HYP_MISSING;
+    return true;  // (PAL13464) can work with no hypothesis, LengthFromEdges is default one
+  }
+
+  aStatus = HYP_MISSING;
 
   list<const SMESHDS_Hypothesis*>::const_iterator ith;
   for (ith = hyps.begin(); ith != hyps.end(); ++ith )
@@ -133,143 +153,23 @@ bool NETGENPlugin_NETGEN_2D_ONLY::CheckHypothesis (SMESH_Mesh&         aMesh,
       _hypLengthFromEdges = static_cast<const StdMeshers_LengthFromEdges*> (hyp);
     else if ( hypName == "QuadranglePreference" )
       _hypQuadranglePreference = static_cast<const StdMeshers_QuadranglePreference*>(hyp);
+    else if ( hypName == "NETGEN_Parameters_2D" )
+      _hypParameters = static_cast<const NETGENPlugin_Hypothesis_2D*>(hyp);
+    else if ( hypName == StdMeshers_ViscousLayers2D::GetHypType() )
+      continue;
     else {
       aStatus = HYP_INCOMPATIBLE;
       return false;
     }
   }
 
-  if ( _hypMaxElementArea && _hypLengthFromEdges ) {
+  int nbHyps = bool(_hypMaxElementArea) + bool(_hypLengthFromEdges) + bool(_hypParameters );
+  if ( nbHyps > 1 )
     aStatus = HYP_CONCURENT;
-    return false;
-  }
-
-  if ( _hypMaxElementArea || _hypLengthFromEdges )
+  else
     aStatus = HYP_OK;
 
-  return aStatus == HYP_OK;
-}
-
-//================================================================================
-/*!
- * \brief Fill netgen mesh with segments
-  * \retval SMESH_ComputeErrorPtr - error description
- */
-//================================================================================
-
-static TError AddSegmentsToMesh(netgen::Mesh&                    ngMesh,
-                                OCCGeometry&                     geom,
-                                const TSideVector&               wires,
-                                SMESH_MesherHelper&              helper,
-                                vector< const SMDS_MeshNode* > & nodeVec)
-{
-  // ----------------------------
-  // Check wires and count nodes
-  // ----------------------------
-  int nbNodes = 0;
-  for ( int iW = 0; iW < wires.size(); ++iW )
-  {
-    StdMeshers_FaceSidePtr wire = wires[ iW ];
-    if ( wire->MissVertexNode() )
-      return TError
-        (new SMESH_ComputeError(COMPERR_BAD_INPUT_MESH, "Missing nodes on vertices"));
-      
-    const vector<UVPtStruct>& uvPtVec = wire->GetUVPtStruct();
-    if ( uvPtVec.size() != wire->NbPoints() )
-      return TError
-        (new SMESH_ComputeError(COMPERR_BAD_INPUT_MESH,
-                                SMESH_Comment("Unexpected nb of points on wire ") << iW
-                                << ": " << uvPtVec.size()<<" != "<<wire->NbPoints()));
-    nbNodes += wire->NbSegments();
-  }
-  nodeVec.reserve( nbNodes );
-
-  // -----------------
-  // Fill netgen mesh
-  // -----------------
-
-//   netgen::Box<3> bb = geom.GetBoundingBox();
-//   bb.Increase (bb.Diam()/10);
-//   ngMesh.SetLocalH (bb.PMin(), bb.PMax(), 0.5); // set grading
-
-  const int faceID = 1, solidID = 0;
-  ngMesh.AddFaceDescriptor (FaceDescriptor(faceID, solidID, solidID, 0));
-
-  for ( int iW = 0; iW < wires.size(); ++iW )
-  {
-    StdMeshers_FaceSidePtr wire = wires[ iW ];
-    const vector<UVPtStruct>& uvPtVec = wire->GetUVPtStruct();
-
-    int firstPointID = ngMesh.GetNP() + 1;
-    int edgeID = 1, posID = -2;
-    for ( int i = 0; i < wire->NbSegments(); ++i ) // loop on segments
-    {
-      // Add the first point of a segment
-      const SMDS_MeshNode * n = uvPtVec[ i ].node;
-      const int posShapeID = n->GetPosition()->GetShapeId();
-
-      // skip nodes on degenerated edges
-      if ( helper.IsDegenShape( posShapeID ) &&
-           helper.IsDegenShape( uvPtVec[ i+1 ].node->GetPosition()->GetShapeId() ))
-        continue;
-
-      nodeVec.push_back( n );
-
-      MeshPoint mp( Point<3> (n->X(), n->Y(), n->Z()) );
-      ngMesh.AddPoint ( mp, 1, EDGEPOINT );
-
-      // Add the segment
-      Segment seg;
-
-      seg.p1 = ngMesh.GetNP();          // ng node id
-      seg.p2 = seg.p1 + 1;              // ng node id
-      seg.edgenr = ngMesh.GetNSeg() + 1;// segment id
-      seg.si = faceID;                  // = geom.fmap.FindIndex (face);
-
-      for ( int iEnd = 0; iEnd < 2; ++iEnd)
-      {
-        const UVPtStruct& pnt = uvPtVec[ i + iEnd ];
-
-        seg.epgeominfo[ iEnd ].dist = pnt.param; // param on curve
-        seg.epgeominfo[ iEnd ].u    = pnt.u;
-        seg.epgeominfo[ iEnd ].v    = pnt.v;
-
-        // find out edge id and node parameter on edge
-        bool onVertex = ( pnt.node->GetPosition()->GetTypeOfPosition() == SMDS_TOP_VERTEX );
-        if ( onVertex || posShapeID != posID )
-        {
-          // get edge id
-          double normParam = pnt.normParam;
-          if ( onVertex )
-            normParam = 0.5 * ( uvPtVec[ i ].normParam + uvPtVec[ i+1 ].normParam );
-          const TopoDS_Edge& edge = wire->Edge( wire->EdgeIndex( normParam ));
-          edgeID = geom.emap.FindIndex( edge );
-          posID  = posShapeID;
-          if ( onVertex ) // param on curve is different on each of two edges
-            seg.epgeominfo[ iEnd ].dist = helper.GetNodeU( edge, pnt.node );
-        }
-        seg.epgeominfo[ iEnd ].edgenr = edgeID; //  = geom.emap.FindIndex(edge);
-      }
-
-      ngMesh.AddSegment (seg);
-
-//       cout << "Segment: " << seg.edgenr << endl
-//            << "\tp1: " << seg.p1 << endl
-//            << "\tp2: " << seg.p2 << endl
-//            << "\tp0 param: " << seg.epgeominfo[ 0 ].dist << endl
-//            << "\tp0 uv: " << seg.epgeominfo[ 0 ].u <<", "<< seg.epgeominfo[ 0 ].v << endl
-//            << "\tp0 edge: " << seg.epgeominfo[ 0 ].edgenr << endl
-//            << "\tp1 param: " << seg.epgeominfo[ 1 ].dist << endl
-//            << "\tp1 uv: " << seg.epgeominfo[ 1 ].u <<", "<< seg.epgeominfo[ 1 ].v << endl
-//            << "\tp1 edge: " << seg.epgeominfo[ 1 ].edgenr << endl;
-    }
-    Segment& seg = ngMesh.LineSegment( ngMesh.GetNSeg() );
-    seg.p2 = firstPointID;
-  }
-
-  ngMesh.CalcSurfacesOfNode();  
-
-  return TError();
+  return ( aStatus == HYP_OK );
 }
 
 //=============================================================================
@@ -281,7 +181,8 @@ static TError AddSegmentsToMesh(netgen::Mesh&                    ngMesh,
 bool NETGENPlugin_NETGEN_2D_ONLY::Compute(SMESH_Mesh&         aMesh,
                                           const TopoDS_Shape& aShape)
 {
-  MESSAGE("NETGENPlugin_NETGEN_2D_ONLY::Compute()");
+  netgen::multithread.terminate = 0;
+  netgen::multithread.task = "Surface meshing";
 
   SMESHDS_Mesh* meshDS = aMesh.GetMeshDS();
   int faceID = meshDS->ShapeToIndex( aShape );
@@ -290,13 +191,22 @@ bool NETGENPlugin_NETGEN_2D_ONLY::Compute(SMESH_Mesh&         aMesh,
   _quadraticMesh = helper.IsQuadraticSubMesh(aShape);
   helper.SetElementsOnShape( true );
   const bool ignoreMediumNodes = _quadraticMesh;
-  
+
+  // build viscous layers if required
+  TopoDS_Face F = TopoDS::Face( aShape/*.Oriented( TopAbs_FORWARD )*/);
+  if ( F.Orientation() != TopAbs_FORWARD &&
+       F.Orientation() != TopAbs_REVERSED )
+    F.Orientation( TopAbs_FORWARD ); // avoid pb with TopAbs_INTERNAL
+  SMESH_ProxyMesh::Ptr proxyMesh = StdMeshers_ViscousLayers2D::Compute( aMesh, F );
+  if ( !proxyMesh )
+    return false;
+
   // ------------------------
   // get all edges of a face
   // ------------------------
-  const TopoDS_Face F = TopoDS::Face( aShape.Oriented( TopAbs_FORWARD ));
   TError problem;
-  TSideVector wires = StdMeshers_FaceSide::GetFaceWires( F, aMesh, ignoreMediumNodes, problem );
+  TSideVector wires =
+    StdMeshers_FaceSide::GetFaceWires( F, aMesh, ignoreMediumNodes, problem, proxyMesh );
   if ( problem && !problem->IsOK() )
     return error( problem );
   int nbWires = wires.size();
@@ -306,132 +216,333 @@ bool NETGENPlugin_NETGEN_2D_ONLY::Compute(SMESH_Mesh&         aMesh,
     return error(COMPERR_BAD_INPUT_MESH,
                  SMESH_Comment("Too few segments: ")<<wires[0]->NbSegments());
 
-  // -------------------------
-  // Make input netgen mesh
-  // -------------------------
-
-  Ng_Init();
-  netgen::Mesh * ngMesh = new netgen::Mesh ();
-
-  netgen::OCCGeometry occgeo;
-  NETGENPlugin_Mesher::PrepareOCCgeometry( occgeo, F );
-
-  vector< const SMDS_MeshNode* > nodeVec;
-  problem = AddSegmentsToMesh( *ngMesh, occgeo, wires, helper, nodeVec );
-  if ( problem && !problem->IsOK() ) {
-    delete ngMesh; Ng_Exit();
-    return error( problem );
-  }
-
   // --------------------
   // compute edge length
   // --------------------
 
-  double edgeLength = 0;
-  if (_hypLengthFromEdges)
+  NETGENPlugin_Mesher aMesher( &aMesh, aShape, /*isVolume=*/false);
+  netgen::OCCGeometry occgeo;
+  aMesher.PrepareOCCgeometry( occgeo, F, aMesh );
+  occgeo.fmap.Clear(); // face can be reversed, which is wrong in this case (issue 19978)
+  occgeo.fmap.Add( F );
+
+  if ( _hypParameters )
   {
-    int nbSegments = 0;
-    for ( int iW = 0; iW < nbWires; ++iW )
-    {
-      edgeLength += wires[ iW ]->Length();
-      nbSegments += wires[ iW ]->NbSegments();
-    }
-    if ( nbSegments )
-      edgeLength /= nbSegments;
+    aMesher.SetParameters(_hypParameters);
   }
-  if ( _hypMaxElementArea )
+  else
   {
-    double maxArea = _hypMaxElementArea->GetMaxArea();
-    edgeLength = sqrt(2. * maxArea/sqrt(3.0));
-  }
-  if ( edgeLength < DBL_MIN )
-    edgeLength = occgeo.GetBoundingBox().Diam();
-
-  //cout << " edgeLength = " << edgeLength << endl;
+    double edgeLength = 0;
+    if (_hypLengthFromEdges /*|| (!_hypLengthFromEdges && !_hypMaxElementArea)*/)
+    {
+      int nbSegments = 0;
+      for ( int iW = 0; iW < nbWires; ++iW )
+      {
+        edgeLength += wires[ iW ]->Length();
+        nbSegments += wires[ iW ]->NbSegments();
+      }
+      if ( nbSegments )
+        edgeLength /= nbSegments;
+    }
+    if ( _hypMaxElementArea )
+    {
+      double maxArea = _hypMaxElementArea->GetMaxArea();
+      edgeLength = sqrt(2. * maxArea/sqrt(3.0));
+    }
+    if ( edgeLength < DBL_MIN )
+      edgeLength = occgeo.GetBoundingBox().Diam();
 
-  netgen::mparam.maxh = edgeLength;
-  netgen::mparam.quad = _hypQuadranglePreference ? 1 : 0;
-  //ngMesh->SetGlobalH ( edgeLength );
+    netgen::mparam.maxh = edgeLength;
+    netgen::mparam.minh = aMesher.GetDefaultMinSize( aShape, netgen::mparam.maxh );
+    netgen::mparam.quad = _hypQuadranglePreference ? 1 : 0;
+    netgen::mparam.grading = 0.5; // coarse mesh by default
+  }
+  occgeo.face_maxh = netgen::mparam.maxh;
 
   // -------------------------
-  // Generate surface mesh
+  // Make input netgen mesh
   // -------------------------
 
-  char *optstr;
-  int startWith = MESHCONST_MESHSURFACE;
-  int endWith   = MESHCONST_OPTSURFACE;
+  // MESHCONST_ANALYSE step may lead to a failure, so we make an attempt
+  // w/o MESHCONST_ANALYSE at the second loop
   int err = 1;
+  for ( int iLoop = 0; iLoop < 2; iLoop++ )
+  {
+    bool isMESHCONST_ANALYSE = false;
+    InitComputeError();
+
+    NETGENPlugin_NetgenLibWrapper ngLib;
+    netgen::Mesh * ngMesh = (netgen::Mesh*) ngLib._ngMesh;
+    ngLib._isComputeOk = false;
 
-  try {
-#if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
-    OCC_CATCH_SIGNALS;
+#ifndef NETGEN_V5
+    char *optstr = 0;
 #endif
-    err = netgen::OCCGenerateMesh(occgeo, ngMesh, startWith, endWith, optstr);
-  }
-  catch (Standard_Failure& ex) {
-    string comment = ex.DynamicType()->Name();
-    if ( ex.GetMessageString() && strlen( ex.GetMessageString() )) {
-      comment += ": ";
-      comment += ex.GetMessageString();
+    int startWith = MESHCONST_ANALYSE;
+    int endWith   = MESHCONST_ANALYSE;
+
+    if ( !_hypLengthFromEdges && !_hypMaxElementArea && !iLoop == 1 )
+    {
+      isMESHCONST_ANALYSE = true;
+#ifdef NETGEN_V5
+      err = netgen::OCCGenerateMesh(occgeo, ngMesh, netgen::mparam, startWith, endWith);
+#else
+      err = netgen::OCCGenerateMesh(occgeo, ngMesh, startWith, endWith, optstr);
+#endif
+      ngLib._ngMesh = 0;
+      ngLib.setMesh(( nglib::Ng_Mesh*) ngMesh );
+    }
+    else
+    {
+      Box<3> bb = occgeo.GetBoundingBox();
+      bb.Increase (bb.Diam()/10);
+      ngMesh->SetLocalH (bb.PMin(), bb.PMax(), netgen::mparam.grading);
+      ngMesh->SetGlobalH (netgen::mparam.maxh);
     }
-    error(COMPERR_OCC_EXCEPTION, comment);
-  }
-  catch (NgException exc) {
-    error( SMESH_Comment("NgException: ") << exc.What() );
-  }
-  catch (...) {
-    error(COMPERR_EXCEPTION,"Exception in netgen::OCCGenerateMesh()");
-  }
 
-  // ----------------------------------------------------
-  // Fill the SMESHDS with the generated nodes and faces
-  // ----------------------------------------------------
+    vector< const SMDS_MeshNode* > nodeVec;
+    problem = aMesher.AddSegmentsToMesh( *ngMesh, occgeo, wires, helper, nodeVec );
+    if ( problem && !problem->IsOK() )
+      return error( problem );
 
-  int nbNodes = ngMesh->GetNP();
-  int nbFaces = ngMesh->GetNSE();
+    // limit element size near existing segments
+    TopTools_IndexedMapOfShape edgeMap, faceMap;
+    TopExp::MapShapes( aMesh.GetShapeToMesh(), TopAbs_EDGE, edgeMap );
+    for ( int iE = 1; iE <= edgeMap.Extent(); ++iE )
+    {
+      const TopoDS_Shape& edge = edgeMap( iE );
+      if ( SMESH_Algo::isDegenerated( TopoDS::Edge( edge )) ||
+           helper.IsSubShape( edge, aShape ))
+        continue;
+      SMESHDS_SubMesh* smDS = aMesh.GetMeshDS()->MeshElements( edge );
+      if ( !smDS ) continue;
+      SMDS_ElemIteratorPtr segIt = smDS->GetElements();
+      while ( segIt->more() )
+      {
+        const SMDS_MeshElement* seg = segIt->next();
+        SMESH_TNodeXYZ n1 = seg->GetNode(0);
+        SMESH_TNodeXYZ n2 = seg->GetNode(1);
+        gp_XYZ p = 0.5 * ( n1 + n2 );
+        netgen::Point3d pi(p.X(), p.Y(), p.Z());
+        ngMesh->RestrictLocalH( pi, Max(( n1 - n2 ).Modulus(), netgen::mparam.minh ));
+      }
+    }
 
-  int nbInputNodes = nodeVec.size();
-  nodeVec.resize( nbNodes, 0 );
+    // -------------------------
+    // Generate surface mesh
+    // -------------------------
 
-  // add nodes
-  for ( int i = nbInputNodes + 1; i <= nbNodes; ++i )
-  {
-    const MeshPoint& ngPoint = ngMesh->Point(i);
-    SMDS_MeshNode * node = meshDS->AddNode(ngPoint.X(), ngPoint.Y(), ngPoint.Z());
-    nodeVec[ i-1 ] = node;
-  }
+    startWith = MESHCONST_MESHSURFACE;
+    endWith   = MESHCONST_OPTSURFACE;
 
-  // create faces
-  bool reverse = ( aShape.Orientation() == TopAbs_REVERSED );
-  for ( int i = 1; i <= nbFaces ; ++i )
-  {
-    const Element2d& elem = ngMesh->SurfaceElement(i);
-    vector<const SMDS_MeshNode*> nodes( elem.GetNP() );
-    for (int j=1; j <= elem.GetNP(); ++j)
+    try {
+      OCC_CATCH_SIGNALS;
+
+#ifdef NETGEN_V5
+      err = netgen::OCCGenerateMesh(occgeo, ngMesh, netgen::mparam, startWith, endWith);
+#else
+      err = netgen::OCCGenerateMesh(occgeo, ngMesh, startWith, endWith, optstr);
+#endif
+      if(netgen::multithread.terminate)
+        return false;
+      if ( err )
+        error(SMESH_Comment("Error in netgen::OCCGenerateMesh() at ") << netgen::multithread.task);
+    }
+    catch (Standard_Failure& ex)
     {
-      int pind = elem.PNum(j);
-      const SMDS_MeshNode* node = nodeVec.at(pind-1);
-      if ( reverse )
-        nodes[ nodes.size()-j ] = node;
-      else
-        nodes[ j-1 ] = node;
-      if ( node->GetPosition()->GetTypeOfPosition() == SMDS_TOP_3DSPACE )
+      SMESH_Comment str("Exception in  netgen::OCCGenerateMesh()");
+      str << " at " << netgen::multithread.task
+          << ": " << ex.DynamicType()->Name();
+      if ( ex.GetMessageString() && strlen( ex.GetMessageString() ))
+        str << ": " << ex.GetMessageString();
+      error(str);
+      err = 1;
+    }
+    catch (...) {
+      SMESH_Comment str("Exception in  netgen::OCCGenerateMesh()");
+      str << " at " << netgen::multithread.task;
+      error(str);
+      err = 1;
+    }
+    if ( err && isMESHCONST_ANALYSE )
+      continue;
+
+    // ----------------------------------------------------
+    // Fill the SMESHDS with the generated nodes and faces
+    // ----------------------------------------------------
+
+    int nbNodes = ngMesh->GetNP();
+    int nbFaces = ngMesh->GetNSE();
+
+    int nbInputNodes = nodeVec.size()-1;
+    nodeVec.resize( nbNodes+1, 0 );
+
+    // add nodes
+    for ( int ngID = nbInputNodes + 1; ngID <= nbNodes; ++ngID )
+    {
+      const MeshPoint& ngPoint = ngMesh->Point( ngID );
+      SMDS_MeshNode * node = meshDS->AddNode(ngPoint(0), ngPoint(1), ngPoint(2));
+      nodeVec[ ngID ] = node;
+    }
+
+    // create faces
+    const bool reverse = false; //( aShape.Orientation() == TopAbs_REVERSED );
+    int i,j;
+    for ( i = 1; i <= nbFaces ; ++i )
+    {
+      const Element2d& elem = ngMesh->SurfaceElement(i);
+      vector<const SMDS_MeshNode*> nodes( elem.GetNP() );
+      for (j=1; j <= elem.GetNP(); ++j)
+      {
+        int pind = elem.PNum(j);
+        if ( pind < 1 )
+          break;
+        const SMDS_MeshNode* node = nodeVec[ pind ];
+        if ( reverse )
+          nodes[ nodes.size()-j ] = node;
+        else
+          nodes[ j-1 ] = node;
+        if ( node->GetPosition()->GetTypeOfPosition() == SMDS_TOP_3DSPACE )
+        {
+          const PointGeomInfo& pgi = elem.GeomInfoPi(j);
+          meshDS->SetNodeOnFace((SMDS_MeshNode*)node, faceID, pgi.u, pgi.v);
+        }
+      }
+      if ( j > elem.GetNP() )
       {
-        const PointGeomInfo& pgi = elem.GeomInfoPi(j);
-        meshDS->SetNodeOnFace((SMDS_MeshNode*)node, faceID, pgi.u, pgi.v);
+        SMDS_MeshFace* face = 0;
+        if ( elem.GetType() == TRIG )
+          face = helper.AddFace(nodes[0],nodes[1],nodes[2]);
+        else
+          face = helper.AddFace(nodes[0],nodes[1],nodes[2],nodes[3]);
       }
     }
-    SMDS_MeshFace* face = 0;
-    if ( elem.GetType() == TRIG )
-      face = helper.AddFace(nodes[0],nodes[1],nodes[2]);
-    else
-      face = helper.AddFace(nodes[0],nodes[1],nodes[2],nodes[3]);
-  }
 
-  Ng_DeleteMesh((nglib::Ng_Mesh*)ngMesh);
-  Ng_Exit();
+    ngLib._isComputeOk = !err;
+    break;
 
-  NETGENPlugin_Mesher::RemoveTmpFiles();
+  } // two attempts
 
   return !err;
 }
+
+void NETGENPlugin_NETGEN_2D_ONLY::CancelCompute()
+{
+  SMESH_Algo::CancelCompute();
+  netgen::multithread.terminate = 1;
+}
+
+//================================================================================
+/*!
+ * \brief Return progress of Compute() [0.,1]
+ */
+//================================================================================
+
+double NETGENPlugin_NETGEN_2D_ONLY::GetProgress() const
+{
+  const char* task1 = "Surface meshing";
+  //const char* task2 = "Optimizing surface";
+  double& progress = const_cast<NETGENPlugin_NETGEN_2D_ONLY*>( this )->_progress;
+  if ( _progressByTic < 0. &&
+       strncmp( netgen::multithread.task, task1, 3 ) == 0 )
+  {
+    progress = Min( 0.25, SMESH_Algo::GetProgressByTic() ); // [0, 0.25]
+  }
+  else //if ( strncmp( netgen::multithread.task, task2, 3 ) == 0)
+  {
+    if ( _progressByTic < 0 )
+    {
+      NETGENPlugin_NETGEN_2D_ONLY* me = (NETGENPlugin_NETGEN_2D_ONLY*) this;
+      me->_progressByTic = 0.25 / (_progressTic+1);
+    }
+    const_cast<NETGENPlugin_NETGEN_2D_ONLY*>( this )->_progressTic++;
+    progress = Max( progress, _progressByTic * _progressTic );
+  }
+  //cout << netgen::multithread.task << " " << _progressTic << endl;
+  return Min( progress, 0.99 );
+}
+
+//=============================================================================
+/*!
+ *
+ */
+//=============================================================================
+
+bool NETGENPlugin_NETGEN_2D_ONLY::Evaluate(SMESH_Mesh& aMesh,
+                                           const TopoDS_Shape& aShape,
+                                           MapShapeNbElems& aResMap)
+{
+  TopoDS_Face F = TopoDS::Face(aShape);
+  if(F.IsNull())
+    return false;
+
+  // collect info from edges
+  int nb0d = 0, nb1d = 0;
+  bool IsQuadratic = false;
+  bool IsFirst = true;
+  double fullLen = 0.0;
+  TopTools_MapOfShape tmpMap;
+  for (TopExp_Explorer exp(F, TopAbs_EDGE); exp.More(); exp.Next()) {
+    TopoDS_Edge E = TopoDS::Edge(exp.Current());
+    if( tmpMap.Contains(E) )
+      continue;
+    tmpMap.Add(E);
+    SMESH_subMesh *aSubMesh = aMesh.GetSubMesh(exp.Current());
+    MapShapeNbElemsItr anIt = aResMap.find(aSubMesh);
+    if( anIt==aResMap.end() ) {
+      SMESH_subMesh *sm = aMesh.GetSubMesh(F);
+      SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
+      smError.reset( new SMESH_ComputeError(COMPERR_ALGO_FAILED,"Submesh can not be evaluated",this));
+      return false;
+    }
+    std::vector<int> aVec = (*anIt).second;
+    nb0d += aVec[SMDSEntity_Node];
+    nb1d += Max(aVec[SMDSEntity_Edge],aVec[SMDSEntity_Quad_Edge]);
+    double aLen = SMESH_Algo::EdgeLength(E);
+    fullLen += aLen;
+    if(IsFirst) {
+      IsQuadratic = (aVec[SMDSEntity_Quad_Edge] > aVec[SMDSEntity_Edge]);
+      IsFirst = false;
+    }
+  }
+  tmpMap.Clear();
+
+  // compute edge length
+  double ELen = 0;
+  if (_hypLengthFromEdges || !_hypLengthFromEdges && !_hypMaxElementArea) {
+    if ( nb1d > 0 )
+      ELen = fullLen / nb1d;
+  }
+  if ( _hypMaxElementArea ) {
+    double maxArea = _hypMaxElementArea->GetMaxArea();
+    ELen = sqrt(2. * maxArea/sqrt(3.0));
+  }
+  GProp_GProps G;
+  BRepGProp::SurfaceProperties(F,G);
+  double anArea = G.Mass();
+
+  const int hugeNb = numeric_limits<int>::max()/10;
+  if ( anArea / hugeNb > ELen*ELen )
+  {
+    SMESH_subMesh *sm = aMesh.GetSubMesh(F);
+    SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
+    smError.reset( new SMESH_ComputeError(COMPERR_ALGO_FAILED,"Submesh can not be evaluated.\nToo small element length",this));
+    return false;
+  }
+  int nbFaces = (int) ( anArea / ( ELen*ELen*sqrt(3.) / 4 ) );
+  int nbNodes = (int) ( ( nbFaces*3 - (nb1d-1)*2 ) / 6 + 1 );
+  std::vector<int> aVec(SMDSEntity_Last);
+  for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aVec[i]=0;
+  if( IsQuadratic ) {
+    aVec[SMDSEntity_Node] = nbNodes;
+    aVec[SMDSEntity_Quad_Triangle] = nbFaces;
+  }
+  else {
+    aVec[SMDSEntity_Node] = nbNodes;
+    aVec[SMDSEntity_Triangle] = nbFaces;
+  }
+  SMESH_subMesh *sm = aMesh.GetSubMesh(F);
+  aResMap.insert(std::make_pair(sm,aVec));
+
+  return true;
+}