Back to index

salome-smesh  6.5.0
StdMeshers_StartEndLength.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  SMESH StdMeshers_StartEndLength : implementaion of SMESH idl descriptions
00024 //  File   : StdMeshers_StartEndLength.cxx
00025 //  Module : SMESH
00026 //
00027 #include "StdMeshers_StartEndLength.hxx"
00028 
00029 #include "SMESH_Algo.hxx"
00030 #include "SMESH_Mesh.hxx"
00031 
00032 #include <BRep_Tool.hxx>
00033 #include <GCPnts_AbscissaPoint.hxx>
00034 #include <GeomAdaptor_Curve.hxx>
00035 #include <Geom_Curve.hxx>
00036 #include <TopExp.hxx>
00037 #include <TopLoc_Location.hxx>
00038 #include <TopTools_IndexedMapOfShape.hxx>
00039 #include <TopoDS.hxx>
00040 #include <TopoDS_Edge.hxx>
00041 
00042 using namespace std;
00043 
00044 //=============================================================================
00048 //=============================================================================
00049 
00050 StdMeshers_StartEndLength::StdMeshers_StartEndLength(int         hypId,
00051                                                      int         studyId,
00052                                                      SMESH_Gen * gen)
00053      :SMESH_Hypothesis(hypId, studyId, gen)
00054 {
00055   _begLength = 1.;
00056   _endLength = 10.;
00057   _name = "StartEndLength";
00058   _param_algo_dim = 1; // is used by SMESH_Regular_1D
00059 }
00060 
00061 //=============================================================================
00065 //=============================================================================
00066 
00067 StdMeshers_StartEndLength::~StdMeshers_StartEndLength()
00068 {
00069 }
00070 
00071 //=============================================================================
00075 //=============================================================================
00076 
00077 void StdMeshers_StartEndLength::SetLength(double length, bool isStartLength)
00078      throw(SALOME_Exception)
00079 {
00080   if ( (isStartLength ? _begLength : _endLength) != length ) {
00081     if (length <= 0)
00082       throw SALOME_Exception(LOCALIZED("length must be positive"));
00083     if ( isStartLength )
00084       _begLength = length;
00085     else
00086       _endLength = length;
00087 
00088     NotifySubMeshesHypothesisModification();
00089   }
00090 }
00091 
00092 //=============================================================================
00096 //=============================================================================
00097 
00098 double StdMeshers_StartEndLength::GetLength(bool isStartLength) const
00099 {
00100   return isStartLength ? _begLength : _endLength;
00101 }
00102 
00103 //=============================================================================
00107 //=============================================================================
00108 
00109 void StdMeshers_StartEndLength::SetReversedEdges( std::vector<int>& ids )
00110 {
00111   if ( ids != _edgeIDs ) {
00112     _edgeIDs = ids;
00113 
00114     NotifySubMeshesHypothesisModification();
00115   }
00116 }
00117 
00118 //=============================================================================
00122 //=============================================================================
00123 
00124 ostream & StdMeshers_StartEndLength::SaveTo(ostream & save)
00125 {
00126   int listSize = _edgeIDs.size();
00127   save << _begLength << " " << _endLength << " " << listSize;
00128 
00129   if ( listSize > 0 ) {
00130     for ( int i = 0; i < listSize; i++) {
00131       save << " " << _edgeIDs[i];
00132     }
00133     save << " " << _objEntry;
00134   }
00135 
00136   return save;
00137 }
00138 
00139 //=============================================================================
00143 //=============================================================================
00144 
00145 istream & StdMeshers_StartEndLength::LoadFrom(istream & load)
00146 {
00147   bool isOK = true;
00148   int intVal;
00149   isOK = (load >> _begLength);
00150   if (!isOK)
00151     load.clear(ios::badbit | load.rdstate());
00152   isOK = (load >> _endLength);
00153 
00154   if (!isOK)
00155     load.clear(ios::badbit | load.rdstate());
00156   
00157   isOK = (load >> intVal);
00158   if (isOK && intVal > 0) {
00159     _edgeIDs.reserve( intVal );
00160     for (int i = 0; i < _edgeIDs.capacity() && isOK; i++) {
00161       isOK = (load >> intVal);
00162       if ( isOK ) _edgeIDs.push_back( intVal );
00163     }
00164     isOK = (load >> _objEntry);
00165   }
00166 
00167   return load;
00168 }
00169 
00170 //=============================================================================
00174 //=============================================================================
00175 
00176 ostream & operator <<(ostream & save, StdMeshers_StartEndLength & hyp)
00177 {
00178   return hyp.SaveTo( save );
00179 }
00180 
00181 //=============================================================================
00185 //=============================================================================
00186 
00187 istream & operator >>(istream & load, StdMeshers_StartEndLength & hyp)
00188 {
00189   return hyp.LoadFrom( load );
00190 }
00191 
00192 //================================================================================
00199 //================================================================================
00200 
00201 bool StdMeshers_StartEndLength::SetParametersByMesh(const SMESH_Mesh*   theMesh,
00202                                                     const TopoDS_Shape& theShape)
00203 {
00204   if ( !theMesh || theShape.IsNull() )
00205     return false;
00206 
00207   _begLength = _endLength = 0.;
00208 
00209   Standard_Real UMin, UMax;
00210   TopLoc_Location L;
00211 
00212   int nbEdges = 0;
00213   TopTools_IndexedMapOfShape edgeMap;
00214   TopExp::MapShapes( theShape, TopAbs_EDGE, edgeMap );
00215   for ( int i = 1; i <= edgeMap.Extent(); ++i )
00216   {
00217     const TopoDS_Edge& edge = TopoDS::Edge( edgeMap( i ));
00218     Handle(Geom_Curve) C = BRep_Tool::Curve(edge, L, UMin, UMax);
00219     GeomAdaptor_Curve AdaptCurve(C, UMin, UMax);
00220 
00221     vector< double > params;
00222     SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* >( theMesh )->GetMeshDS();
00223     if ( SMESH_Algo::GetNodeParamOnEdge( aMeshDS, edge, params ))
00224     {
00225       nbEdges++;
00226       _begLength += GCPnts_AbscissaPoint::Length( AdaptCurve, params[0], params[1]);
00227       int nb = params.size();
00228       _endLength += GCPnts_AbscissaPoint::Length( AdaptCurve, params[nb-2], params[nb-1]);
00229     }
00230   }
00231   if ( nbEdges ) {
00232     _begLength /= nbEdges;
00233     _endLength /= nbEdges;
00234   }
00235   return nbEdges;
00236 }
00237 
00238 //================================================================================
00243 //================================================================================
00244 
00245 bool StdMeshers_StartEndLength::SetParametersByDefaults(const TDefaults&  dflts,
00246                                                         const SMESH_Mesh* /*theMesh*/)
00247 {
00248   return (_begLength = _endLength = dflts._elemLength );
00249 }
00250