Salome HOME
Implementation of the "20830: EDF 1357 GUI : Hide/Show Icon"
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_AttributeSequenceOfReal.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  File   : SALOMEDSImpl_AttributeSequenceOfReal.cxx
24 //  Author : Sergey RUIN
25 //  Module : SALOME
26 //
27 #include "SALOMEDSImpl_AttributeSequenceOfReal.hxx"
28 #include <string.h>
29
30 //=======================================================================
31 //function : GetID
32 //purpose  : 
33 //=======================================================================
34
35 const std::string& SALOMEDSImpl_AttributeSequenceOfReal::GetID () 
36 {
37   static std::string SALOMEDSImpl_AttributeSequenceOfRealID ("12837183-8F52-11d6-A8A3-0001021E8C7F");
38   return SALOMEDSImpl_AttributeSequenceOfRealID;
39 }
40
41
42
43 //=======================================================================
44 //function : Set
45 //purpose  : 
46 //=======================================================================
47
48 SALOMEDSImpl_AttributeSequenceOfReal* SALOMEDSImpl_AttributeSequenceOfReal::Set (const DF_Label& L) 
49 {
50   SALOMEDSImpl_AttributeSequenceOfReal* A = NULL;
51   if (!(A=(SALOMEDSImpl_AttributeSequenceOfReal*)L.FindAttribute(SALOMEDSImpl_AttributeSequenceOfReal::GetID()))) {
52     A = new  SALOMEDSImpl_AttributeSequenceOfReal(); 
53     L.AddAttribute(A);
54   }
55   return A;
56 }
57
58
59 //=======================================================================
60 //function : constructor
61 //purpose  : 
62 //=======================================================================
63 SALOMEDSImpl_AttributeSequenceOfReal::SALOMEDSImpl_AttributeSequenceOfReal()
64 :SALOMEDSImpl_GenericAttribute("AttributeSequenceOfReal")
65 {
66   myValue.clear();
67 }
68
69 //=======================================================================
70 //function : ID
71 //purpose  : 
72 //=======================================================================
73
74 const std::string& SALOMEDSImpl_AttributeSequenceOfReal::ID () const { return GetID(); }
75
76
77 //=======================================================================
78 //function : NewEmpty
79 //purpose  : 
80 //=======================================================================
81
82 DF_Attribute* SALOMEDSImpl_AttributeSequenceOfReal::NewEmpty () const
83 {  
84   return new SALOMEDSImpl_AttributeSequenceOfReal(); 
85 }
86
87 //=======================================================================
88 //function : Restore
89 //purpose  : 
90 //=======================================================================
91
92 void SALOMEDSImpl_AttributeSequenceOfReal::Restore(DF_Attribute* with) 
93 {
94   SALOMEDSImpl_AttributeSequenceOfReal* anSeq = dynamic_cast<SALOMEDSImpl_AttributeSequenceOfReal*>(with);
95   myValue.clear();
96   for(int i = 0, len = anSeq->Length(); i<len; i++)
97     myValue.push_back(anSeq->myValue[i]);    
98   return;
99 }
100
101 //=======================================================================
102 //function : Paste
103 //purpose  : 
104 //=======================================================================
105
106 void SALOMEDSImpl_AttributeSequenceOfReal::Paste (DF_Attribute* into)
107 {
108     dynamic_cast<SALOMEDSImpl_AttributeSequenceOfReal*>(into)->Assign(myValue);
109 }
110
111 void SALOMEDSImpl_AttributeSequenceOfReal::Assign(const std::vector<double>& other) 
112 {
113   CheckLocked();  
114   Backup();
115   myValue = other;
116
117   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
118 }
119
120 void SALOMEDSImpl_AttributeSequenceOfReal::ChangeValue(const int Index,const double& Value) 
121 {
122   CheckLocked();  
123   Backup();
124
125   if(Index <= 0 || Index > myValue.size()) throw DFexception("Out of range");
126
127   myValue[Index-1] = Value;
128   
129   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
130 }
131
132 void SALOMEDSImpl_AttributeSequenceOfReal::Add(const double& Value) 
133 {
134   CheckLocked();  
135   Backup();
136   myValue.push_back(Value);
137   
138   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
139 }
140
141 void SALOMEDSImpl_AttributeSequenceOfReal::Remove(const int Index) 
142 {
143   CheckLocked();  
144   Backup();
145
146   if(Index <= 0 || Index > myValue.size()) throw DFexception("Out of range");
147
148   typedef std::vector<double>::iterator VI;
149   int i = 1;    
150   for(VI p = myValue.begin(); p!=myValue.end(); p++, i++) {
151     if(i == Index) {
152       myValue.erase(p);
153       break;
154     }     
155   }
156
157   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved   
158 }
159
160 int SALOMEDSImpl_AttributeSequenceOfReal::Length() 
161 {
162   return myValue.size();
163 }
164
165 double SALOMEDSImpl_AttributeSequenceOfReal::Value(const int Index) 
166 {
167   if(Index <= 0 || Index > myValue.size()) throw DFexception("Out of range");
168   return myValue[Index-1];
169 }
170
171
172 std::string SALOMEDSImpl_AttributeSequenceOfReal::Save()
173 {
174   int aLength = Length();
175   char* aResult = new char[aLength * 127];
176   aResult[0] = 0;
177   int aPosition = 0;
178   for (int i = 1; i <= aLength; i++) {
179     sprintf(aResult + aPosition , "%.64e ", Value(i));
180     aPosition += strlen(aResult + aPosition);
181   }
182   std::string ret(aResult);
183   delete aResult;
184                           
185   return ret;
186 }
187                             
188 void SALOMEDSImpl_AttributeSequenceOfReal::Load(const std::string& value)
189 {
190                               
191   char* aCopy = (char*)value.c_str();
192   char* adr = strtok(aCopy, " ");
193   char *err = NULL; 
194   while (adr) {
195     double r =  strtod(adr, &err); 
196     Add(r);
197     adr = strtok(NULL, " ");
198   }
199 }