77723ce73d4b6a5f3e41eb673afecafa9fe14081
[occt.git] / src / TDataStd / TDataStd_RealArray.cxx
1 // Created on: 1999-06-16
2 // Created by: Sergey RUIN
3 // Copyright (c) 1999-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17
18 #include <Standard_GUID.hxx>
19 #include <Standard_Type.hxx>
20 #include <TDataStd_DeltaOnModificationOfRealArray.hxx>
21 #include <TDataStd_RealArray.hxx>
22 #include <TDF_Attribute.hxx>
23 #include <TDF_DefaultDeltaOnModification.hxx>
24 #include <TDF_DeltaOnModification.hxx>
25 #include <TDF_Label.hxx>
26 #include <TDF_RelocationTable.hxx>
27
28 IMPLEMENT_STANDARD_RTTIEXT(TDataStd_RealArray,TDF_Attribute)
29
30 //=======================================================================
31 //function : GetID
32 //purpose  : 
33 //=======================================================================
34 const Standard_GUID& TDataStd_RealArray::GetID() 
35
36   static Standard_GUID TDataStd_RealArrayID ("2a96b61e-ec8b-11d0-bee7-080009dc3333");
37   return TDataStd_RealArrayID; 
38 }
39
40 //=======================================================================
41 //function : SetAttr
42 //purpose  : Implements Set functionality
43 //=======================================================================
44 static Handle(TDataStd_RealArray) SetAttr(const TDF_Label&       label,
45                                           const Standard_Integer lower,
46                                           const Standard_Integer upper,
47                                           const Standard_Boolean isDelta,
48                                           const Standard_GUID&   theGuid) 
49 {
50   Handle(TDataStd_RealArray) A;
51   if (!label.FindAttribute (theGuid, A)) 
52   {
53     A = new TDataStd_RealArray;
54     A->Init (lower, upper);
55     A->SetDelta(isDelta); 
56     A->SetID(theGuid);
57     label.AddAttribute(A);
58   }
59   else if (lower != A->Lower() || upper != A->Upper())
60   {
61     A->Init(lower, upper);
62   }
63   return A;
64 }
65
66 //=======================================================================
67 //function : TDataStd_RealArray
68 //purpose  : Empty Constructor
69 //=======================================================================
70
71 TDataStd_RealArray::TDataStd_RealArray() : myIsDelta(Standard_False)
72 {}
73
74 //=======================================================================
75 //function : Init
76 //purpose  : 
77 //=======================================================================
78
79 void TDataStd_RealArray::Init(const Standard_Integer lower,
80                               const Standard_Integer upper)
81 {
82   Standard_RangeError_Raise_if(upper < lower,"TDataStd_RealArray::Init");  
83   Backup(); // jfa 15.01.2003 for LH3D1378
84   myValue = new TColStd_HArray1OfReal(lower, upper, 0.);
85 }
86
87 //=======================================================================
88 //function : Set
89 //purpose  : 
90 //=======================================================================
91
92 Handle(TDataStd_RealArray) TDataStd_RealArray::Set
93                                           (const TDF_Label&       label,
94                                            const Standard_Integer lower,
95                                            const Standard_Integer upper,
96                                            const Standard_Boolean isDelta) 
97 {
98   return SetAttr(label, lower, upper, isDelta, GetID());
99 }
100
101 //=======================================================================
102 //function : Set
103 //purpose  : Set user defined attribute with specific ID
104 //=======================================================================
105
106 Handle(TDataStd_RealArray) TDataStd_RealArray::Set
107                                           (const TDF_Label&       label,
108                                            const Standard_GUID&   theGuid,
109                                            const Standard_Integer lower,
110                                            const Standard_Integer upper,
111                                            const Standard_Boolean isDelta) 
112 {
113   return SetAttr(label, lower, upper, isDelta, theGuid);
114 }
115 //=======================================================================
116 //function : SetValue
117 //purpose  : 
118 //=======================================================================
119
120 void TDataStd_RealArray::SetValue (const Standard_Integer index,
121                                    const Standard_Real value) 
122 {
123   // OCC2932 correction
124   if(myValue.IsNull()) return;
125   if(myValue->Value(index) == value)
126     return;
127   Backup();
128   myValue->SetValue(index, value);
129 }
130
131
132 //=======================================================================
133 //function : GetValue
134 //purpose  : 
135 //=======================================================================
136
137 Standard_Real TDataStd_RealArray::Value (const Standard_Integer index) const 
138 {
139   if(myValue.IsNull()) return RealFirst();
140   return myValue->Value(index); 
141 }
142
143
144
145 //=======================================================================
146 //function : Lower
147 //purpose  : 
148 //=======================================================================
149 Standard_Integer TDataStd_RealArray::Lower (void) const 
150
151   if(myValue.IsNull()) return 0;
152   return myValue->Lower(); 
153 }
154
155
156 //=======================================================================
157 //function : Upper
158 //purpose  : 
159 //=======================================================================
160 Standard_Integer TDataStd_RealArray::Upper (void) const 
161
162   if(myValue.IsNull()) return 0;
163   return myValue->Upper(); 
164 }
165
166
167 //=======================================================================
168 //function : Length
169 //purpose  : 
170 //=======================================================================
171 Standard_Integer TDataStd_RealArray::Length (void) const 
172 {
173   if(myValue.IsNull()) return 0;
174   return myValue->Length(); 
175 }
176
177
178 //=======================================================================
179 //function : ChangeArray
180 //purpose  : If value of <newArray> differs from <myValue>, Backup 
181 //         : performed and myValue refers to new instance of HArray1OfReal
182 //         : that holds <newArray>
183 //=======================================================================
184
185 void TDataStd_RealArray::ChangeArray(const Handle(TColStd_HArray1OfReal)& newArray,
186                                      const Standard_Boolean isCheckItems) 
187 {
188   Standard_Integer aLower    = newArray->Lower();
189   Standard_Integer anUpper   = newArray->Upper();
190   Standard_Boolean aDimEqual = Standard_False;
191   Standard_Integer i;
192
193   if (Lower() == aLower && Upper() == anUpper ) {
194     aDimEqual = Standard_True;
195     Standard_Boolean isEqual = Standard_True;
196     if(isCheckItems) {
197       for(i = aLower; i <= anUpper; i++) {
198         if(myValue->Value(i) != newArray->Value(i)) {
199           isEqual = Standard_False;
200           break;
201         }
202       }
203       if(isEqual)
204         return;
205     }
206   }
207
208   Backup();
209
210   if(myValue.IsNull() || !aDimEqual) 
211     myValue = new TColStd_HArray1OfReal(aLower, anUpper);
212
213   for(i = aLower; i <= anUpper; i++) 
214     myValue->SetValue(i, newArray->Value(i));
215 }
216
217 //=======================================================================
218 //function : ID
219 //purpose  : 
220 //=======================================================================
221
222 const Standard_GUID& TDataStd_RealArray::ID () const { return myID; }
223
224 //=======================================================================
225 //function : SetID
226 //purpose  :
227 //=======================================================================
228
229 void TDataStd_RealArray::SetID( const Standard_GUID&  theGuid)
230 {  
231   if(myID == theGuid) return;
232   Backup();
233   myID = theGuid;
234 }
235
236 //=======================================================================
237 //function : SetID
238 //purpose  : sets default ID
239 //=======================================================================
240
241 void TDataStd_RealArray::SetID()
242 {
243   Backup();
244   myID = GetID();
245 }
246
247 //=======================================================================
248 //function : NewEmpty
249 //purpose  : 
250 //=======================================================================
251
252 Handle(TDF_Attribute) TDataStd_RealArray::NewEmpty () const
253 {  
254   return new TDataStd_RealArray(); 
255 }
256
257 //=======================================================================
258 //function : Restore
259 //purpose  : 
260 //=======================================================================
261
262 void TDataStd_RealArray::Restore(const Handle(TDF_Attribute)& With) 
263 {
264   Standard_Integer i, lower, upper;
265   Handle(TDataStd_RealArray) anArray = Handle(TDataStd_RealArray)::DownCast(With);
266   if(!anArray->myValue.IsNull()) {
267     lower = anArray->Lower();
268     upper = anArray->Upper();
269     myIsDelta = anArray->myIsDelta;
270     myValue = new TColStd_HArray1OfReal(lower, upper);
271     for(i = lower; i<=upper; i++)
272       myValue->SetValue(i, anArray->Value(i)); 
273     myID = anArray->ID();
274   }
275   else
276     myValue.Nullify();
277 }
278
279 //=======================================================================
280 //function : Paste
281 //purpose  : 
282 //=======================================================================
283
284 void TDataStd_RealArray::Paste (const Handle(TDF_Attribute)& Into,
285                                 const Handle(TDF_RelocationTable)& ) const
286 {
287   if(!myValue.IsNull()) {    
288     Handle(TDataStd_RealArray) anAtt = Handle(TDataStd_RealArray)::DownCast(Into);
289     if(!anAtt.IsNull()) {
290       anAtt->ChangeArray( myValue, Standard_False );
291       anAtt->SetDelta(myIsDelta);
292       anAtt->SetID(myID);
293     }
294   }
295 }
296
297 //=======================================================================
298 //function : Dump
299 //purpose  : 
300 //=======================================================================
301
302 Standard_OStream& TDataStd_RealArray::Dump (Standard_OStream& anOS) const
303 {  
304   anOS << "\nRealArray::" << this <<" :";
305   if(!myValue.IsNull()) {
306     Standard_Integer i, lower, upper;
307     lower = myValue->Lower();
308     upper = myValue->Upper();
309     for(i = lower; i<=upper; i++)
310       anOS << " " <<myValue->Value(i);
311   }
312   anOS << " Delta is " << (myIsDelta ? "ON":"OFF");
313   Standard_Character sguid[Standard_GUID_SIZE_ALLOC];
314   myID.ToCString(sguid);
315   anOS << sguid;
316   anOS << endl;
317   return anOS;
318 }
319
320 //=======================================================================
321 //function : DeltaOnModification
322 //purpose  : 
323 //=======================================================================
324
325 Handle(TDF_DeltaOnModification) TDataStd_RealArray::DeltaOnModification
326 (const Handle(TDF_Attribute)& OldAtt) const
327 {
328   if(myIsDelta)
329     return new TDataStd_DeltaOnModificationOfRealArray(Handle(TDataStd_RealArray)::DownCast (OldAtt));
330   else return new TDF_DefaultDeltaOnModification(OldAtt);
331 }