1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include <HYDROData_StricklerTable.h>
21 #include <TDataStd_NamedData.hxx>
23 #include <TDataStd_DataMapOfStringReal.hxx>
24 #include <TDataStd_DataMapIteratorOfDataMapOfStringReal.hxx>
26 #include <TCollection_AsciiString.hxx>
27 #include <TCollection_ExtendedString.hxx>
29 #include <QtCore/QString>
30 #include <QtCore/QStringList>
32 IMPLEMENT_STANDARD_HANDLE( HYDROData_StricklerTable, HYDROData_Entity )
33 IMPLEMENT_STANDARD_RTTIEXT( HYDROData_StricklerTable, HYDROData_Entity )
35 HYDROData_StricklerTable::HYDROData_StricklerTable()
39 HYDROData_StricklerTable::~HYDROData_StricklerTable()
43 const ObjectKind HYDROData_StricklerTable::GetKind() const
45 return KIND_STRICKLER_TABLE;
48 bool HYDROData_StricklerTable::Import( const TCollection_AsciiString& theFileName )
50 std::ifstream aStream( theFileName.ToCString(), std::ios::in | std::ios::binary );
56 const int aBufLen = 512;
58 while( !aStream.eof() && aRes )
60 aStream.getline( aBuf, aBufLen );
61 TCollection_ExtendedString aStr( aBuf, Standard_True );
63 Standard_Integer aPos = aStr.SearchFromEnd( " " );
68 TCollection_ExtendedString aValueStr = aStr.Split( aPos );
69 while ( aStr.Length() > 0 && ( aStr.Value( aStr.Length() ) == ' ' || aStr.Value( aStr.Length() ) == '\t' ) )
70 aStr.Remove( aStr.Length() );
71 while ( aStr.Length() > 0 && ( aStr.Value( 1 ) == ' ' || aStr.Value( 1 ) == '\t' ) || aStr.Value( 1 ) < 0 )
74 if ( aStr.Length() > 0 && aStr.Value( aStr.Length() ) == '\"' )
75 aStr.Remove( aStr.Length() );
76 if ( aStr.Length() > 0 && aStr.Value( 1 ) == '\"' )
79 if ( aValueStr.IsEmpty() || aStr.IsEmpty() )
83 Standard_Real aValue = TCollection_AsciiString( aValueStr ).RealValue();
93 bool HYDROData_StricklerTable::Export( const TCollection_AsciiString& theFileName )
95 Handle(TDataStd_NamedData) aMap = Map();
99 std::ofstream aStream( theFileName.ToCString(), std::ios::out | std::ios::binary );
104 for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More() && aRes; it.Next() )
106 TCollection_ExtendedString aLine = TCollection_ExtendedString( '\"' ) + it.Key() + TCollection_ExtendedString( '\"' ) +
107 TCollection_ExtendedString( ' ' ) + TCollection_ExtendedString( it.Value() );
108 Standard_PCharacter aBuf = (Standard_PCharacter)malloc( aLine.LengthOfCString() + 1 );
109 aStream.write( aBuf, aLine.ToUTF8CString( aBuf ) );
110 aStream.write( "\r\n", 2 );
118 double HYDROData_StricklerTable::Get( const TCollection_ExtendedString& theType, double theDefault ) const
120 Handle( TDataStd_NamedData ) aMap = Map();
121 if( aMap->HasReal( theType ) )
122 return aMap->GetReal( theType );
127 void HYDROData_StricklerTable::Set( const TCollection_ExtendedString& theType, double theCoefficient )
129 Handle(TDataStd_NamedData) aMap = Map();
130 aMap->SetReal( theType, theCoefficient );
133 void HYDROData_StricklerTable::GetCoefficientRange( double& theMin, double& theMax ) const
138 Handle(TDataStd_NamedData) aMap = Map();
139 Standard_Boolean isFirst = Standard_True;
140 for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() ) {
141 Standard_Real aValue = it.Value();
142 if ( theMin == 0 || aValue < theMin ) {
145 if ( theMax == 0 || aValue > theMax ) {
151 TColStd_SequenceOfExtendedString HYDROData_StricklerTable::GetTypes() const
153 TColStd_SequenceOfExtendedString aSeq;
154 Handle(TDataStd_NamedData) aMap = Map();
155 if ( !aMap.IsNull() )
157 for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
158 aSeq.Append( it.Key() );
163 bool HYDROData_StricklerTable::HasType( const TCollection_ExtendedString& theType ) const
165 Handle(TDataStd_NamedData) aMap = Map();
167 return !aMap.IsNull() && aMap->HasReal( theType );
170 void HYDROData_StricklerTable::Clear()
172 Handle(TDataStd_NamedData) aMap = Map();
173 if ( !aMap.IsNull() )
174 aMap->ChangeReals( TDataStd_DataMapOfStringReal() );
177 QStringList HYDROData_StricklerTable::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
179 QStringList aResList = dumpObjectCreation( theTreatedObjects );
180 QString aPyName = GetObjPyName();
182 aResList << QString( "" );
183 Handle(TDataStd_NamedData) aMap = Map();
184 if ( !aMap.IsNull() )
186 for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
188 TCollection_ExtendedString aType = it.Key();
189 Standard_Real aValue = it.Value();
190 aResList << QString( "%1.Set( \"%2\", %3 );" ).arg( aPyName ).arg( QString( (QChar*)aType.ToExtString(), aType.Length() ) ).arg( aValue );
193 aResList << QString( "" );
194 aResList << QString( "%1.Update();" ).arg( aPyName );
199 Handle(TDataStd_NamedData) HYDROData_StricklerTable::Map() const
201 TDF_Label aLabel = myLab.FindChild( DataTag_Table );
202 Handle( TDataStd_NamedData ) aMap;
203 if( !aLabel.FindAttribute( TDataStd_NamedData::GetID(), aMap ) )
204 aMap = TDataStd_NamedData::Set( aLabel );