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