0022048: Visualization, AIS_InteractiveContext - single object selection should alway...
[occt.git] / src / TDataStd / TDataStd_DeltaOnModificationOfByteArray.cxx
1 // Created on: 2007-12-05
2 // Created by: Sergey ZARITCHNY
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
17 #include <Standard_Type.hxx>
18 #include <TColStd_HArray1OfByte.hxx>
19 #include <TColStd_HArray1OfInteger.hxx>
20 #include <TColStd_ListIteratorOfListOfInteger.hxx>
21 #include <TColStd_ListOfInteger.hxx>
22 #include <TDataStd_ByteArray.hxx>
23 #include <TDataStd_DeltaOnModificationOfByteArray.hxx>
24 #include <TDF_AttributeIterator.hxx>
25 #include <TDF_DeltaOnModification.hxx>
26 #include <TDF_Label.hxx>
27
28 IMPLEMENT_STANDARD_RTTIEXT(TDataStd_DeltaOnModificationOfByteArray,TDF_DeltaOnModification)
29
30 #ifdef OCCT_DEBUG
31 #define MAXUP 1000
32 #endif
33 //=======================================================================
34 //function : TDataStd_DeltaOnModificationOfByteArray
35 //purpose  : 
36 //=======================================================================
37
38 TDataStd_DeltaOnModificationOfByteArray::TDataStd_DeltaOnModificationOfByteArray(const Handle(TDataStd_ByteArray)& OldAtt)
39 : TDF_DeltaOnModification(OldAtt)
40 {
41   Handle(TDataStd_ByteArray) CurrAtt;
42   if (Label().FindAttribute(OldAtt->ID(),CurrAtt)) {
43     {
44       Handle(TColStd_HArray1OfByte) Arr1, Arr2;
45       Arr1 = OldAtt->InternalArray();
46       Arr2 = CurrAtt->InternalArray();
47 #ifdef OCCT_DEBUG_DELTA
48       if(Arr1.IsNull())
49         cout <<"DeltaOnModificationOfByteArray:: Old ByteArray is Null" <<endl;
50       if(Arr2.IsNull())
51         cout <<"DeltaOnModificationOfByteArray:: Current ByteArray is Null" <<endl;
52 #endif
53
54       if(Arr1.IsNull() || Arr2.IsNull()) return;
55       if(Arr1 != Arr2) {
56         myUp1 = Arr1->Upper();
57         myUp2 = Arr2->Upper();
58         Standard_Integer i, N=0, aCase=0; 
59         if(myUp1 == myUp2) 
60           {aCase = 1; N = myUp1;}
61         else if(myUp1 < myUp2) 
62           {aCase = 2; N = myUp1;}
63         else 
64           {aCase = 3; N = myUp2;}//Up1 > Up2
65
66         TColStd_ListOfInteger aList;
67         for(i=Arr1->Lower();i<= N; i++)
68           if(Arr1->Value(i) != Arr2->Value(i)) 
69             aList.Append(i);
70         if(aCase == 3) {
71           for(i = N+1;i <= myUp1; i++)
72             aList.Append(i);
73         }
74
75         if(aList.Extent()) {
76           myIndxes = new TColStd_HArray1OfInteger(1,aList.Extent());
77           myValues = new TColStd_HArray1OfByte(1,aList.Extent());
78           TColStd_ListIteratorOfListOfInteger anIt(aList);
79           for(i =1;anIt.More();anIt.Next(),i++) {
80             myIndxes->SetValue(i, anIt.Value());
81             myValues->SetValue(i, Arr1->Value(anIt.Value()));
82           }
83         }
84       }
85     }
86     OldAtt->RemoveArray();
87 #ifdef OCCT_DEBUG
88     if(OldAtt->InternalArray().IsNull())
89       cout << "BackUp Arr is Nullified" << endl;
90 #endif
91   }
92 }
93
94
95 //=======================================================================
96 //function : Apply
97 //purpose  : 
98 //=======================================================================
99
100 void TDataStd_DeltaOnModificationOfByteArray::Apply()
101 {
102
103   Handle(TDF_Attribute) TDFAttribute = Attribute();
104   Handle(TDataStd_ByteArray) BackAtt = Handle(TDataStd_ByteArray)::DownCast (TDFAttribute);
105   if(BackAtt.IsNull()) {
106 #ifdef OCCT_DEBUG
107     cout << "DeltaOnModificationOfByteArray::Apply: OldAtt is Null" <<endl;
108 #endif
109     return;
110   }
111   
112   Handle(TDataStd_ByteArray) aCurAtt;
113   if (!Label().FindAttribute(BackAtt->ID(),aCurAtt)) {
114
115     Label().AddAttribute(BackAtt);
116   }
117
118   if(aCurAtt.IsNull()) {
119 #ifdef OCCT_DEBUG
120     cout << "DeltaOnModificationOfByteArray::Apply: CurAtt is Null" <<endl;
121 #endif
122     return;
123   }
124   else 
125     aCurAtt->Backup();
126
127   Standard_Integer aCase;
128   if(myUp1 == myUp2) 
129     aCase = 1;
130   else if(myUp1 < myUp2) 
131     aCase = 2;
132   else 
133     aCase = 3;//Up1 > Up2
134
135   if (aCase == 1 && (myIndxes.IsNull() || myValues.IsNull()))
136     return;
137   
138   Standard_Integer i;
139   Handle(TColStd_HArray1OfByte) BArr = aCurAtt->InternalArray();
140   if(BArr.IsNull()) return;
141   if(aCase == 1)   
142     for(i = 1; i <= myIndxes->Upper();i++) 
143       BArr->ChangeArray1().SetValue(myIndxes->Value(i), myValues->Value(i));
144   else if(aCase == 2) {    
145     Handle(TColStd_HArray1OfByte) byteArr = new TColStd_HArray1OfByte(BArr->Lower(), myUp1);
146     for(i = BArr->Lower(); i <= myUp1 && i <= BArr->Upper(); i++) 
147       byteArr->SetValue(i, BArr->Value(i));
148     if(!myIndxes.IsNull() && !myValues.IsNull())
149       for(i = 1; i <= myIndxes->Upper();i++) 
150         byteArr->ChangeArray1().SetValue(myIndxes->Value(i), myValues->Value(i));
151     aCurAtt->myValue = byteArr;
152   }
153   else { // aCase == 3
154     Standard_Integer low = BArr->Lower();
155     Handle(TColStd_HArray1OfByte) byteArr = new TColStd_HArray1OfByte(low, myUp1);
156     for(i = BArr->Lower(); i <= myUp2 && i <= BArr->Upper(); i++) 
157       byteArr->SetValue(i, BArr->Value(i));
158     if(!myIndxes.IsNull() && !myValues.IsNull())
159       for(i = 1; i <= myIndxes->Upper();i++) {
160 #ifdef OCCT_DEBUG
161         cout << "i = " << i << "  myIndxes->Upper = " << myIndxes->Upper() << endl;
162         cout << "myIndxes->Value(i) = " << myIndxes->Value(i) << endl;
163         cout << "myValues->Value(i) = " << myValues->Value(i) << endl;
164 #endif
165         byteArr->ChangeArray1().SetValue(myIndxes->Value(i), myValues->Value(i));      
166       }
167     aCurAtt->myValue = byteArr;
168   }
169   
170 #ifdef OCCT_DEBUG
171   cout << " << Array Dump after Delta Apply >>" <<endl;
172   Handle(TColStd_HArray1OfByte) BArr2 = aCurAtt->InternalArray();
173   for(i=BArr2->Lower(); i<=BArr2->Upper() && i<= MAXUP;i++)
174     cout << BArr2->Value(i) << "  ";
175   cout <<endl;
176 #endif
177 }
178
179