1 // Created on: 2007-05-29
2 // Created by: Vlad Romashko
3 // Copyright (c) 2007-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <TDataStd_ReferenceArray.hxx>
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>
27 IMPLEMENT_STANDARD_RTTIEXT(TDataStd_ReferenceArray,TDF_Attribute)
29 //=======================================================================
32 //=======================================================================
33 const Standard_GUID& TDataStd_ReferenceArray::GetID()
35 static Standard_GUID TDataStd_ReferenceArrayID ("7EE745A6-BB50-446c-BB0B-C195B23AB5CA");
36 return TDataStd_ReferenceArrayID;
39 //=======================================================================
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)
48 Handle(TDataStd_ReferenceArray) A;
49 if (!label.FindAttribute (theGuid, A))
51 A = new TDataStd_ReferenceArray;
52 A->Init (lower, upper);
54 label.AddAttribute(A);
56 else if (lower != A->Lower() || upper != A->Upper())
58 A->Init(lower, upper);
63 //=======================================================================
64 //function : TDataStd_ReferenceArray
65 //purpose : Empty Constructor
66 //=======================================================================
67 TDataStd_ReferenceArray::TDataStd_ReferenceArray() : myID(GetID())
70 //=======================================================================
73 //=======================================================================
74 void TDataStd_ReferenceArray::Init(const Standard_Integer lower,
75 const Standard_Integer upper)
77 Standard_RangeError_Raise_if(upper < lower,"TDataStd_ReferenceArray::Init");
79 myArray = new TDataStd_HLabelArray1(lower, upper);
82 //=======================================================================
85 //=======================================================================
86 Handle(TDataStd_ReferenceArray) TDataStd_ReferenceArray::Set(const TDF_Label& label,
87 const Standard_Integer lower,
88 const Standard_Integer upper)
90 return SetAttr(label, lower, upper, GetID());
94 //=======================================================================
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)
103 return SetAttr(label, lower, upper, theGuid);
105 //=======================================================================
106 //function : SetValue
108 //=======================================================================
109 void TDataStd_ReferenceArray::SetValue (const Standard_Integer index,
110 const TDF_Label& value)
112 if(myArray.IsNull()) return;
113 if (value == myArray->Value(index))
117 myArray->SetValue(index, value);
120 //=======================================================================
123 //=======================================================================
124 TDF_Label TDataStd_ReferenceArray::Value (const Standard_Integer index) const
126 return myArray->Value(index);
129 //=======================================================================
132 //=======================================================================
133 Standard_Integer TDataStd_ReferenceArray::Lower () const
135 if (myArray.IsNull())
137 return myArray->Lower();
140 //=======================================================================
143 //=======================================================================
144 Standard_Integer TDataStd_ReferenceArray::Upper () const
146 if (myArray.IsNull())
148 return myArray->Upper();
151 //=======================================================================
154 //=======================================================================
155 Standard_Integer TDataStd_ReferenceArray::Length () const
157 if (myArray.IsNull())
159 return myArray->Length();
162 //=======================================================================
163 //function : InternalArray
165 //=======================================================================
166 const Handle(TDataStd_HLabelArray1)& TDataStd_ReferenceArray::InternalArray () const
171 //=======================================================================
172 //function : SetInternalArray
174 //=======================================================================
175 void TDataStd_ReferenceArray::SetInternalArray (const Handle(TDataStd_HLabelArray1)& values,
176 const Standard_Boolean)
179 Standard_Integer aLower = values->Lower();
180 Standard_Integer anUpper = values->Upper();
181 Standard_Boolean aDimEqual = Standard_False;
185 if (Lower() == aLower && Upper() == anUpper ) {
186 aDimEqual = Standard_True;
187 Standard_Boolean isEqual = Standard_True;
189 for(i = aLower; i <= anUpper; i++) {
190 if(myArray->Value(i) != values->Value(i)) {
191 isEqual = Standard_False;
203 if(myArray.IsNull() || !aDimEqual)
204 myArray = new TDataStd_HLabelArray1(aLower, anUpper);
206 for(i = aLower; i <= anUpper; i++)
207 myArray->SetValue(i, values->Value(i));
210 //=======================================================================
213 //=======================================================================
214 const Standard_GUID& TDataStd_ReferenceArray::ID () const
219 //=======================================================================
222 //=======================================================================
224 void TDataStd_ReferenceArray::SetID( const Standard_GUID& theGuid)
226 if(myID == theGuid) return;
231 //=======================================================================
233 //purpose : sets default ID
234 //=======================================================================
236 void TDataStd_ReferenceArray::SetID()
242 //=======================================================================
243 //function : NewEmpty
245 //=======================================================================
246 Handle(TDF_Attribute) TDataStd_ReferenceArray::NewEmpty () const
248 return new TDataStd_ReferenceArray();
251 //=======================================================================
254 //=======================================================================
255 void TDataStd_ReferenceArray::Restore(const Handle(TDF_Attribute)& With)
257 Handle(TDataStd_ReferenceArray) anArray = Handle(TDataStd_ReferenceArray)::DownCast(With);
258 if (!anArray->myArray.IsNull())
260 const TDataStd_LabelArray1& arr = anArray->myArray->Array1();
261 Standard_Integer lower = arr.Lower(), i = lower, upper = arr.Upper();
263 for (; i <= upper; i++)
265 myArray->SetValue(i, arr.Value(i));
267 myID = anArray->ID();
275 //=======================================================================
278 //=======================================================================
279 void TDataStd_ReferenceArray::Paste (const Handle(TDF_Attribute)& Into,
280 const Handle(TDF_RelocationTable)& RT) const
282 Handle(TDataStd_ReferenceArray) anArray = Handle(TDataStd_ReferenceArray)::DownCast(Into);
283 if (myArray.IsNull())
285 anArray->myArray.Nullify();
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++)
294 TDF_Label L = arr.Value(i), rL;
297 if (!RT->HasRelocation(L, rL))
299 anArray->myArray->SetValue(i, rL);
302 anArray->SetID(myID);
305 //=======================================================================
306 //function : References
307 //purpose : Adds the referenced attributes or labels.
308 //=======================================================================
309 void TDataStd_ReferenceArray::References(const Handle(TDF_DataSet)& aDataSet) const
311 if (!Label().IsImported() && !myArray.IsNull())
313 const TDataStd_LabelArray1& arr = myArray->Array1();
314 Standard_Integer lower = arr.Lower(), i = lower, upper = arr.Upper();
315 for (; i <= upper; i++)
317 if (!arr.Value(i).IsNull())
318 aDataSet->AddLabel(arr.Value(i));
323 //=======================================================================
326 //=======================================================================
327 Standard_OStream& TDataStd_ReferenceArray::Dump (Standard_OStream& anOS) const
329 anOS << "\nReferenceArray: ";
330 Standard_Character sguid[Standard_GUID_SIZE_ALLOC];
331 myID.ToCString(sguid);
332 anOS << sguid << std::endl;
336 //=======================================================================
337 //function : DumpJson
339 //=======================================================================
340 void TDataStd_ReferenceArray::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
342 OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
344 OCCT_DUMP_BASE_CLASS (theOStream, theDepth, TDF_Attribute)
346 TCollection_AsciiString aLabel;
347 for (TDataStd_LabelArray1::Iterator anArrayIt (myArray->Array1()); anArrayIt.More(); anArrayIt.Next())
350 TDF_Tool::Entry (anArrayIt.Value(), aLabel);
351 OCCT_DUMP_FIELD_VALUE_STRING (theOStream, aLabel)
354 OCCT_DUMP_FIELD_VALUE_GUID (theOStream, myID)