Back to index

salome-smesh  6.5.0
StdMeshers_MaxLength.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 //  SMESH SMESH : implementaion of SMESH idl descriptions
00021 //  File   : StdMeshers_MaxLength.cxx
00022 //  Module : SMESH
00023 //
00024 #include "StdMeshers_MaxLength.hxx"
00025 
00026 #include "SMESH_Mesh.hxx"
00027 #include "SMESH_Algo.hxx"
00028 
00029 #include "utilities.h"
00030 
00031 #include <BRep_Tool.hxx>
00032 #include <GCPnts_AbscissaPoint.hxx>
00033 #include <GeomAdaptor_Curve.hxx>
00034 #include <Geom_Curve.hxx>
00035 #include <TopExp.hxx>
00036 #include <TopLoc_Location.hxx>
00037 #include <TopTools_IndexedMapOfShape.hxx>
00038 #include <TopoDS.hxx>
00039 #include <TopoDS_Edge.hxx>
00040 #include <Precision.hxx>
00041 
00042 using namespace std;
00043 
00044 //=============================================================================
00048 //=============================================================================
00049 
00050 StdMeshers_MaxLength::StdMeshers_MaxLength(int hypId, int studyId, SMESH_Gen * gen)
00051   :SMESH_Hypothesis(hypId, studyId, gen)
00052 {
00053   _length = 1.;
00054   _preestimated = 0.;
00055   _preestimation = false;
00056   _name = "MaxLength";
00057   _param_algo_dim = 1; // is used by SMESH_Regular_1D
00058 }
00059 
00060 //=============================================================================
00064 //=============================================================================
00065 
00066 StdMeshers_MaxLength::~StdMeshers_MaxLength()
00067 {
00068 }
00069 
00070 //=============================================================================
00074 //=============================================================================
00075 
00076 void StdMeshers_MaxLength::SetLength(double length) throw(SALOME_Exception)
00077 {
00078   if (length <= 0)
00079     throw SALOME_Exception(LOCALIZED("length must be positive"));
00080   if ( _length != length ) {
00081     _length = length;
00082     NotifySubMeshesHypothesisModification();
00083   }
00084 }
00085 
00086 //=============================================================================
00090 //=============================================================================
00091 
00092 double StdMeshers_MaxLength::GetLength() const
00093 {
00094   return ( _preestimation && _preestimated > 0. ) ? _preestimated : _length;
00095 }
00096 
00097 //================================================================================
00102 //================================================================================
00103 
00104 void StdMeshers_MaxLength::SetUsePreestimatedLength(bool toUse)
00105 {
00106   if ( toUse != _preestimation )
00107   {
00108     _preestimation = toUse;
00109     // this parameter is just to help the user
00110     //NotifySubMeshesHypothesisModification();
00111   }
00112 }
00113 
00114 //================================================================================
00118 //================================================================================
00119 
00120 void StdMeshers_MaxLength::SetPreestimatedLength(double length)
00121 {
00122   if ( length > 0 )
00123     _preestimated = length;
00124 }
00125 
00126 //================================================================================
00131 //================================================================================
00132 
00133 bool StdMeshers_MaxLength::GetUsePreestimatedLength() const
00134 {
00135   return _preestimation;
00136 }
00137 
00138 //=============================================================================
00142 //=============================================================================
00143 
00144 ostream & StdMeshers_MaxLength::SaveTo(ostream & save)
00145 {
00146   save << _length << " " << _preestimated << " " << _preestimation;
00147   return save;
00148 }
00149 
00150 //=============================================================================
00154 //=============================================================================
00155 
00156 istream & StdMeshers_MaxLength::LoadFrom(istream & load)
00157 {
00158   bool isOK = true;
00159   double a;
00160 
00161   isOK = (load >> a);
00162   if (isOK)
00163     _length = a;
00164   else
00165     load.clear(ios::badbit | load.rdstate());
00166 
00167   isOK = (load >> a);
00168   if (isOK)
00169     _preestimated = a;
00170   else
00171     load.clear(ios::badbit | load.rdstate());
00172 
00173   bool pre;
00174   isOK = (load >> pre);
00175   if ( isOK )
00176     _preestimation = pre;
00177   else
00178     load.clear(ios::badbit | load.rdstate());
00179 
00180   return load;
00181 }
00182 
00183 //================================================================================
00190 //================================================================================
00191 
00192 bool StdMeshers_MaxLength::SetParametersByMesh(const SMESH_Mesh*   theMesh,
00193                                                const TopoDS_Shape& theShape)
00194 {
00195   if ( !theMesh || theShape.IsNull() )
00196     return false;
00197 
00198   _length = 0.;
00199 
00200   Standard_Real UMin, UMax;
00201   TopLoc_Location L;
00202 
00203   int nbEdges = 0;
00204   TopTools_IndexedMapOfShape edgeMap;
00205   TopExp::MapShapes( theShape, TopAbs_EDGE, edgeMap );
00206   for ( int iE = 1; iE <= edgeMap.Extent(); ++iE )
00207   {
00208     const TopoDS_Edge& edge = TopoDS::Edge( edgeMap( iE ));
00209     Handle(Geom_Curve) C = BRep_Tool::Curve( edge, L, UMin, UMax );
00210     GeomAdaptor_Curve AdaptCurve(C, UMin, UMax);
00211 
00212     vector< double > params;
00213     SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* >( theMesh )->GetMeshDS();
00214     if ( SMESH_Algo::GetNodeParamOnEdge( aMeshDS, edge, params ))
00215     {
00216       for ( int i = 1; i < params.size(); ++i )
00217         _length += GCPnts_AbscissaPoint::Length( AdaptCurve, params[ i-1 ], params[ i ]);
00218       nbEdges += params.size() - 1;
00219     }
00220   }
00221   if ( nbEdges )
00222     _length /= nbEdges;
00223 
00224   return nbEdges;
00225 }
00226 //================================================================================
00231 //================================================================================
00232 
00233 bool StdMeshers_MaxLength::SetParametersByDefaults(const TDefaults&  dflts,
00234                                                    const SMESH_Mesh* /*theMesh*/)
00235 {
00236   //_preestimation = ( dflts._elemLength > 0.);
00237   if ( dflts._elemLength > 0. )
00238     _preestimated = dflts._elemLength;
00239   return ( _length = dflts._elemLength );
00240 }
00241