0031682: Visualization - Prs3d_ShadingAspect::SetTransparency() has no effect with...
[occt.git] / src / TDataStd / TDataStd_ReferenceArray.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_ReferenceArray.hxx>
17
18 #include <Standard_Dump.hxx>
19 #include <Standard_GUID.hxx>
20 #include <Standard_Type.hxx>
21 #include <TDF_Attribute.hxx>
22 #include <TDF_DataSet.hxx>
23 #include <TDF_Label.hxx>
24 #include <TDF_RelocationTable.hxx>
25 #include <TDF_Tool.hxx>
26
27 IMPLEMENT_STANDARD_RTTIEXT(TDataStd_ReferenceArray,TDF_Attribute)
28
29 //=======================================================================
30 //function : GetID
31 //purpose  : 
32 //=======================================================================
33 const Standard_GUID& TDataStd_ReferenceArray::GetID() 
34
35   static Standard_GUID TDataStd_ReferenceArrayID ("7EE745A6-BB50-446c-BB0B-C195B23AB5CA");
36   return TDataStd_ReferenceArrayID; 
37 }
38
39 //=======================================================================
40 //function : SetAttr
41 //purpose  : Implements Set functionality
42 //=======================================================================
43 static Handle(TDataStd_ReferenceArray) SetAttr(const TDF_Label&     label,
44                                                const Standard_Integer lower,
45                                                const Standard_Integer upper,
46                                                const Standard_GUID&   theGuid) 
47 {
48   Handle(TDataStd_ReferenceArray) A;
49   if (!label.FindAttribute (theGuid, A)) 
50   {
51     A = new TDataStd_ReferenceArray;
52     A->Init (lower, upper);
53     A->SetID(theGuid);
54     label.AddAttribute(A);
55   }
56   else if (lower != A->Lower() || upper != A->Upper())
57   {
58     A->Init(lower, upper);
59   }
60   return A;
61 }
62
63 //=======================================================================
64 //function : TDataStd_ReferenceArray
65 //purpose  : Empty Constructor
66 //=======================================================================
67 TDataStd_ReferenceArray::TDataStd_ReferenceArray() : myID(GetID())
68 {}
69
70 //=======================================================================
71 //function : Init
72 //purpose  : 
73 //=======================================================================
74 void TDataStd_ReferenceArray::Init(const Standard_Integer lower,
75                                    const Standard_Integer upper)
76 {
77   Standard_RangeError_Raise_if(upper < lower,"TDataStd_ReferenceArray::Init");
78   Backup();
79   myArray = new TDataStd_HLabelArray1(lower, upper);
80 }
81
82 //=======================================================================
83 //function : Set
84 //purpose  : 
85 //=======================================================================
86 Handle(TDataStd_ReferenceArray) TDataStd_ReferenceArray::Set(const TDF_Label&       label,
87                                                              const Standard_Integer lower,
88                                                              const Standard_Integer upper)
89 {
90   return SetAttr(label, lower, upper, GetID());
91 }
92
93
94 //=======================================================================
95 //function : Set
96 //purpose  : Set user defined attribute with specific ID
97 //=======================================================================
98 Handle(TDataStd_ReferenceArray) TDataStd_ReferenceArray::Set(const TDF_Label&       label,
99                                                              const Standard_GUID&   theGuid,
100                                                              const Standard_Integer lower,
101                                                              const Standard_Integer upper) 
102 {
103   return SetAttr(label, lower, upper, theGuid);
104 }
105 //=======================================================================
106 //function : SetValue
107 //purpose  : 
108 //=======================================================================
109 void TDataStd_ReferenceArray::SetValue (const Standard_Integer index,
110                                         const TDF_Label&       value) 
111 {
112   if(myArray.IsNull()) return;
113   if (value == myArray->Value(index))
114     return;
115   Backup();
116
117   myArray->SetValue(index, value);
118 }
119
120 //=======================================================================
121 //function : Value
122 //purpose  : 
123 //=======================================================================
124 TDF_Label TDataStd_ReferenceArray::Value (const Standard_Integer index) const 
125 {
126   return myArray->Value(index);
127 }
128
129 //=======================================================================
130 //function : Lower
131 //purpose  : 
132 //=======================================================================
133 Standard_Integer TDataStd_ReferenceArray::Lower () const 
134
135   if (myArray.IsNull())
136     return 0;
137   return myArray->Lower();
138 }
139
140 //=======================================================================
141 //function : Upper
142 //purpose  : 
143 //=======================================================================
144 Standard_Integer TDataStd_ReferenceArray::Upper () const 
145
146   if (myArray.IsNull())
147     return -1;
148   return myArray->Upper();
149 }
150
151 //=======================================================================
152 //function : Length
153 //purpose  : 
154 //=======================================================================
155 Standard_Integer TDataStd_ReferenceArray::Length () const 
156 {
157   if (myArray.IsNull())
158     return 0;
159   return myArray->Length();
160 }
161
162 //=======================================================================
163 //function : InternalArray
164 //purpose  : 
165 //=======================================================================
166 const Handle(TDataStd_HLabelArray1)& TDataStd_ReferenceArray::InternalArray () const 
167 {
168   return myArray;
169 }
170
171 //=======================================================================
172 //function : SetInternalArray
173 //purpose  : 
174 //=======================================================================
175 void TDataStd_ReferenceArray::SetInternalArray (const Handle(TDataStd_HLabelArray1)& values,
176                                                 const Standard_Boolean)
177 {
178 //  myArray = values;
179   Standard_Integer aLower    = values->Lower();
180   Standard_Integer anUpper   = values->Upper();
181   Standard_Boolean aDimEqual = Standard_False;
182   Standard_Integer i;
183
184 #ifdef OCC2932
185   if (Lower() == aLower && Upper() == anUpper ) {
186     aDimEqual = Standard_True;
187     Standard_Boolean isEqual = Standard_True;
188     if(isCheckItems) {
189       for(i = aLower; i <= anUpper; i++) {
190         if(myArray->Value(i) != values->Value(i)) {
191           isEqual = Standard_False;
192           break;
193         }
194       }
195       if(isEqual)
196         return;
197     }
198   }
199 #endif
200
201   Backup();
202
203   if(myArray.IsNull() || !aDimEqual) 
204     myArray = new TDataStd_HLabelArray1(aLower, anUpper);
205
206   for(i = aLower; i <= anUpper; i++) 
207     myArray->SetValue(i, values->Value(i));
208 }
209
210 //=======================================================================
211 //function : ID
212 //purpose  : 
213 //=======================================================================
214 const Standard_GUID& TDataStd_ReferenceArray::ID () const 
215
216   return myID;
217 }
218
219 //=======================================================================
220 //function : SetID
221 //purpose  :
222 //=======================================================================
223
224 void TDataStd_ReferenceArray::SetID( const Standard_GUID&  theGuid)
225 {  
226   if(myID == theGuid) return;
227   Backup();
228   myID = theGuid;
229 }
230
231 //=======================================================================
232 //function : SetID
233 //purpose  : sets default ID
234 //=======================================================================
235
236 void TDataStd_ReferenceArray::SetID()
237 {  
238   Backup();
239   myID = GetID();
240 }
241
242 //=======================================================================
243 //function : NewEmpty
244 //purpose  : 
245 //=======================================================================
246 Handle(TDF_Attribute) TDataStd_ReferenceArray::NewEmpty () const
247 {  
248   return new TDataStd_ReferenceArray(); 
249 }
250
251 //=======================================================================
252 //function : Restore
253 //purpose  : 
254 //=======================================================================
255 void TDataStd_ReferenceArray::Restore(const Handle(TDF_Attribute)& With) 
256 {
257   Handle(TDataStd_ReferenceArray) anArray = Handle(TDataStd_ReferenceArray)::DownCast(With);
258   if (!anArray->myArray.IsNull()) 
259   {
260     const TDataStd_LabelArray1& arr = anArray->myArray->Array1();
261     Standard_Integer lower = arr.Lower(), i = lower, upper = arr.Upper();
262     Init(lower, upper);
263     for (; i <= upper; i++)
264     {
265       myArray->SetValue(i, arr.Value(i));
266     }
267     myID = anArray->ID();
268   }
269   else
270   {
271     myArray.Nullify();
272   }
273 }
274
275 //=======================================================================
276 //function : Paste
277 //purpose  : 
278 //=======================================================================
279 void TDataStd_ReferenceArray::Paste (const Handle(TDF_Attribute)& Into,
280                                      const Handle(TDF_RelocationTable)& RT) const
281 {
282   Handle(TDataStd_ReferenceArray) anArray = Handle(TDataStd_ReferenceArray)::DownCast(Into);
283   if (myArray.IsNull())
284   {
285     anArray->myArray.Nullify();
286     return;
287   }
288   const TDataStd_LabelArray1& arr = myArray->Array1();
289   Standard_Integer lower = arr.Lower(), i = lower, upper = arr.Upper();
290   if (lower != anArray->Lower() || upper != anArray->Upper())
291     anArray->Init(lower, upper);
292   for (; i <= upper; i++)
293   {
294     TDF_Label L = arr.Value(i), rL;
295     if (!L.IsNull())
296     {
297       if (!RT->HasRelocation(L, rL))
298         rL = L;
299       anArray->myArray->SetValue(i, rL);
300     }
301   }
302   anArray->SetID(myID);
303 }
304
305 //=======================================================================
306 //function : References
307 //purpose  : Adds the referenced attributes or labels.
308 //=======================================================================
309 void TDataStd_ReferenceArray::References(const Handle(TDF_DataSet)& aDataSet) const
310 {
311   if (!Label().IsImported() && !myArray.IsNull()) 
312   {
313     const TDataStd_LabelArray1& arr = myArray->Array1();
314     Standard_Integer lower = arr.Lower(), i = lower, upper = arr.Upper();
315     for (; i <= upper; i++)
316     {
317       if (!arr.Value(i).IsNull())
318         aDataSet->AddLabel(arr.Value(i));
319     }
320   }
321 }
322
323 //=======================================================================
324 //function : Dump
325 //purpose  : 
326 //=======================================================================
327 Standard_OStream& TDataStd_ReferenceArray::Dump (Standard_OStream& anOS) const
328 {  
329   anOS << "\nReferenceArray: ";
330   Standard_Character sguid[Standard_GUID_SIZE_ALLOC];
331   myID.ToCString(sguid);
332   anOS << sguid << std::endl;
333   return anOS;
334 }
335
336 //=======================================================================
337 //function : DumpJson
338 //purpose  : 
339 //=======================================================================
340 void TDataStd_ReferenceArray::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
341 {
342   OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
343
344   OCCT_DUMP_BASE_CLASS (theOStream, theDepth, TDF_Attribute)
345
346   TCollection_AsciiString aLabel;
347   for (TDataStd_LabelArray1::Iterator anArrayIt (myArray->Array1()); anArrayIt.More(); anArrayIt.Next())
348   {
349     aLabel.Clear();
350     TDF_Tool::Entry (anArrayIt.Value(), aLabel);
351     OCCT_DUMP_FIELD_VALUE_STRING (theOStream, aLabel)
352   }
353   
354   OCCT_DUMP_FIELD_VALUE_GUID (theOStream, myID)
355 }