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()
36 : HYDROData_Entity( Geom_No )
40 HYDROData_StricklerTable::~HYDROData_StricklerTable()
44 const ObjectKind HYDROData_StricklerTable::GetKind() const
46 return KIND_STRICKLER_TABLE;
49 bool HYDROData_StricklerTable::Import( const TCollection_AsciiString& theFileName )
51 std::ifstream aStream( theFileName.ToCString(), std::ios::in | std::ios::binary );
57 const int aBufLen = 512;
59 while( !aStream.eof() && aRes )
61 aStream.getline( aBuf, aBufLen );
62 TCollection_ExtendedString aStr( aBuf, Standard_True );
64 Standard_Integer aPos = aStr.SearchFromEnd( " " );
69 TCollection_ExtendedString aValueStr = aStr.Split( aPos );
70 while ( aStr.Length() > 0 && ( aStr.Value( aStr.Length() ) == ' ' || aStr.Value( aStr.Length() ) == '\t' ) )
71 aStr.Remove( aStr.Length() );
72 while ( aStr.Length() > 0 && ( aStr.Value( 1 ) == ' ' || aStr.Value( 1 ) == '\t' ) || aStr.Value( 1 ) < 0 )
75 if ( aStr.Length() > 0 && aStr.Value( aStr.Length() ) == '\"' )
76 aStr.Remove( aStr.Length() );
77 if ( aStr.Length() > 0 && aStr.Value( 1 ) == '\"' )
80 if ( aValueStr.IsEmpty() || aStr.IsEmpty() )
84 Standard_Real aValue = TCollection_AsciiString( aValueStr ).RealValue();
85 Set( QString( (QChar*)aStr.ToExtString(), aStr.Length() ), aValue );
94 bool HYDROData_StricklerTable::Export( const TCollection_AsciiString& theFileName )
96 Handle(TDataStd_NamedData) aMap = Map();
100 std::ofstream aStream( theFileName.ToCString(), std::ios::out | std::ios::binary );
105 for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More() && aRes; it.Next() )
107 TCollection_ExtendedString aLine = TCollection_ExtendedString( '\"' ) + it.Key() + TCollection_ExtendedString( '\"' ) +
108 TCollection_ExtendedString( ' ' ) + TCollection_ExtendedString( it.Value() );
109 Standard_PCharacter aBuf = (Standard_PCharacter)malloc( aLine.LengthOfCString() + 1 );
110 aStream.write( aBuf, aLine.ToUTF8CString( aBuf ) );
111 aStream.write( "\r\n", 2 );
119 double HYDROData_StricklerTable::Get( const QString& theType, double theDefault ) const
121 Handle( TDataStd_NamedData ) aMap = Map();
122 TCollection_ExtendedString aType = toExtString( theType );
123 if( aMap->HasReal( aType ) )
124 return aMap->GetReal( aType );
129 void HYDROData_StricklerTable::Set( const QString& theType, double theCoefficient )
131 Handle(TDataStd_NamedData) aMap = Map();
132 aMap->SetReal( toExtString( theType ), theCoefficient );
135 void HYDROData_StricklerTable::GetCoefficientRange( double& theMin, double& theMax ) const
140 Handle(TDataStd_NamedData) aMap = Map();
141 Standard_Boolean isFirst = Standard_True;
142 for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() ) {
143 Standard_Real aValue = it.Value();
144 if ( theMin == 0 || aValue < theMin ) {
147 if ( theMax == 0 || aValue > theMax ) {
153 QStringList HYDROData_StricklerTable::GetTypes() const
156 Handle(TDataStd_NamedData) aMap = Map();
157 if ( !aMap.IsNull() )
159 for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
160 aSeq.append( toQString( it.Key() ) );
165 bool HYDROData_StricklerTable::HasType( const QString& theType ) const
167 Handle(TDataStd_NamedData) aMap = Map();
169 TCollection_ExtendedString aType = toExtString( theType );
170 return !aMap.IsNull() && aMap->HasReal( aType );
173 void HYDROData_StricklerTable::Clear()
175 Handle(TDataStd_NamedData) aMap = Map();
176 if ( !aMap.IsNull() )
177 aMap->ChangeReals( TDataStd_DataMapOfStringReal() );
180 QStringList HYDROData_StricklerTable::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
182 QStringList aResList = dumpObjectCreation( theTreatedObjects );
183 QString aPyName = GetObjPyName();
185 aResList << QString( "" );
186 Handle(TDataStd_NamedData) aMap = Map();
187 if ( !aMap.IsNull() )
189 for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
191 TCollection_ExtendedString aType = it.Key();
192 Standard_Real aValue = it.Value();
193 aResList << QString( "%1.Set( \"%2\", %3 );" ).arg( aPyName ).arg( QString( (QChar*)aType.ToExtString(), aType.Length() ) ).arg( aValue );
196 aResList << QString( "" );
197 aResList << QString( "%1.Update();" ).arg( aPyName );
202 Handle(TDataStd_NamedData) HYDROData_StricklerTable::Map() const
204 TDF_Label aLabel = myLab.FindChild( DataTag_Table );
205 Handle( TDataStd_NamedData ) aMap;
206 if( !aLabel.FindAttribute( TDataStd_NamedData::GetID(), aMap ) )
207 aMap = TDataStd_NamedData::Set( aLabel );
211 TCollection_ExtendedString HYDROData_StricklerTable::toExtString( const QString& theStr ) const
213 TCollection_ExtendedString aRes;
214 if( !theStr.isEmpty() )
216 Standard_ExtString extStr = new Standard_ExtCharacter[ ( theStr.length() + 1 ) * 2 ];
217 memcpy( (void*)extStr, theStr.unicode(), theStr.length() * 2 );
218 ((short*)extStr)[theStr.length()] = '\0';
220 aRes = TCollection_ExtendedString( extStr );
226 QString HYDROData_StricklerTable::toQString( const TCollection_ExtendedString& theStr ) const
228 return QString( (QChar*)theStr.ToExtString(), theStr.Length() );