0022048: Visualization, AIS_InteractiveContext - single object selection should alway...
[occt.git] / src / TFunction / TFunction_Logbook.cxx
1 // Created on: 1999-07-20
2 // Created by: Vladislav ROMASHKO
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_OStream.hxx>
19 #include <TCollection_AsciiString.hxx>
20 #include <TDF_ChildIterator.hxx>
21 #include <TDF_Label.hxx>
22 #include <TDF_LabelMap.hxx>
23 #include <TDF_MapIteratorOfLabelMap.hxx>
24 #include <TDF_RelocationTable.hxx>
25 #include <TDF_Tool.hxx>
26 #include <TFunction_Logbook.hxx>
27 #include <Standard_GUID.hxx>
28
29 //=======================================================================
30 //function : GetID
31 //purpose  : Static method to get an ID
32 //=======================================================================
33 const Standard_GUID& TFunction_Logbook::GetID() 
34 {  
35   static Standard_GUID TFunction_LogbookID("CF519724-5CA4-4B90-835F-8919BE1DDE4B");
36   return TFunction_LogbookID; 
37 }
38
39 //=======================================================================
40 //function : Set
41 //purpose  : Finds or creates a Scope attribute
42 //=======================================================================
43
44 Handle(TFunction_Logbook) TFunction_Logbook::Set(const TDF_Label& Access)
45 {
46   Handle(TFunction_Logbook) S;
47   if (!Access.Root().FindAttribute(TFunction_Logbook::GetID(), S)) 
48   {
49     S = new TFunction_Logbook();
50     Access.Root().AddAttribute(S);
51   }
52   return S;
53 }
54
55 //=======================================================================
56 //function : ID
57 //purpose  : Returns GUID of the function
58 //=======================================================================
59
60 const Standard_GUID& TFunction_Logbook::ID() const
61
62   return GetID(); 
63 }
64
65 //=======================================================================
66 //function : TFunction_Logbook
67 //purpose  : A Logbook creation
68 //=======================================================================
69 TFunction_Logbook::TFunction_Logbook():isDone(Standard_False)
70 {}
71
72 //=======================================================================
73 //function : Clear
74 //purpose  : Clears the valid and modified labels
75 //=======================================================================
76
77 void TFunction_Logbook::Clear()
78 {
79   if (!IsEmpty())
80   {
81     Backup();
82     myTouched.Clear();
83     myImpacted.Clear();
84     myValid.Clear();
85   }
86 }
87
88 //=======================================================================
89 //function : IsEmpty
90 //purpose  : Returns Standard_True if the nothing is reccorded in the logbook
91 //=======================================================================
92
93 Standard_Boolean TFunction_Logbook::IsEmpty () const
94 {
95   return (myTouched.IsEmpty() && myImpacted.IsEmpty() && myValid.IsEmpty());
96 }
97
98 //=======================================================================
99 //function : IsModified
100 //purpose  : Returns Standard_True if the label is modified
101 //=======================================================================
102
103 Standard_Boolean TFunction_Logbook::IsModified(const TDF_Label& L,
104                                                const Standard_Boolean WithChildren) const
105 {
106   if (myTouched.Contains(L))
107     return Standard_True;
108   if (myImpacted.Contains(L))
109     return Standard_True;
110   if (WithChildren)
111   {
112     TDF_ChildIterator itr(L);
113     for (; itr.More(); itr.Next())
114     {
115       if (IsModified(itr.Value(), Standard_True))
116       {
117         return Standard_True;
118       }
119     }
120   }
121   return Standard_False;
122 }
123
124 //=======================================================================
125 //function : SetValid
126 //purpose  : 
127 //=======================================================================
128
129 void TFunction_Logbook::SetValid(const TDF_Label& L,
130                                  const Standard_Boolean WithChildren)
131 {
132   Backup();
133   myValid.Add(L);
134   if (WithChildren)
135   {
136     TDF_ChildIterator itr(L, Standard_True);
137     for (; itr.More(); itr.Next())
138     {
139       myValid.Add(itr.Value());
140     }
141   }
142 }
143
144 void TFunction_Logbook::SetValid(const TDF_LabelMap& Ls)
145 {
146   Backup();
147   TDF_MapIteratorOfLabelMap itrm(Ls);
148   for (; itrm.More(); itrm.Next())
149   {
150     const TDF_Label& L = itrm.Key();
151     myValid.Add(L);
152   }
153 }
154
155 //=======================================================================
156 //function : SetImpacted
157 //purpose  : 
158 //=======================================================================
159
160 void TFunction_Logbook::SetImpacted(const TDF_Label& L,
161                                     const Standard_Boolean WithChildren)
162 {
163   Backup();
164   myImpacted.Add(L);
165   if (WithChildren)
166   {
167     TDF_ChildIterator itr(L, Standard_True);
168     for (; itr.More(); itr.Next())
169     {
170       myImpacted.Add(itr.Value());
171     }
172   }  
173 }
174
175 //=======================================================================
176 //function : GetValid
177 //purpose  : Returns valid labels.
178 //=======================================================================
179
180 void TFunction_Logbook::GetValid(TDF_LabelMap& Ls) const
181 {
182   // Copy valid labels.
183   TDF_MapIteratorOfLabelMap itrm(myValid);
184   for (; itrm.More(); itrm.Next())
185   {
186     const TDF_Label& L = itrm.Key();
187     Ls.Add(L);
188   }
189 }
190
191 //=======================================================================
192 //function : Restore
193 //purpose  : Undos (and redos) the attribute.
194 //=======================================================================
195
196 void TFunction_Logbook::Restore(const Handle(TDF_Attribute)& other) 
197 {
198   Handle(TFunction_Logbook) logbook = Handle(TFunction_Logbook)::DownCast(other);
199
200   // Status.
201   isDone = logbook->isDone;
202
203   // Valid labels
204   TDF_MapIteratorOfLabelMap itrm;
205   for (itrm.Initialize(logbook->myValid); itrm.More(); itrm.Next())
206   {
207     myValid.Add(itrm.Key());
208   }
209   // Touched labels
210   for (itrm.Initialize(logbook->myTouched); itrm.More(); itrm.Next())
211   {
212     myTouched.Add(itrm.Key());
213   }
214   // Impacted labels
215   for (itrm.Initialize(logbook->myImpacted); itrm.More(); itrm.Next())
216   {
217     myImpacted.Add(itrm.Key());
218   }
219 }
220
221 //=======================================================================
222 //function : Paste
223 //purpose  : Method for Copy mechanism
224 //=======================================================================
225
226 void TFunction_Logbook::Paste(const Handle(TDF_Attribute)& into,
227                               const Handle(TDF_RelocationTable)& RT) const
228 {
229   Handle(TFunction_Logbook) logbook = Handle(TFunction_Logbook)::DownCast(into);
230   
231   // Status.
232   logbook->isDone = isDone;
233
234   // Touched.
235   logbook->myTouched.Clear();
236   TDF_MapIteratorOfLabelMap itr(myTouched);
237   for (; itr.More(); itr.Next())
238   {
239     const TDF_Label& L = itr.Value();
240     if (!L.IsNull())
241     {
242       TDF_Label relocL;
243       if (RT->HasRelocation(L, relocL))
244         logbook->myTouched.Add(relocL);
245       else
246         logbook->myTouched.Add(L);
247     }
248   }
249
250   // Impacted.
251   logbook->myImpacted.Clear();
252   itr.Initialize(myImpacted);
253   for (; itr.More(); itr.Next())
254   {
255     const TDF_Label& L = itr.Value();
256     if (!L.IsNull())
257     {
258       TDF_Label relocL;
259       if (RT->HasRelocation(L, relocL))
260         logbook->myImpacted.Add(relocL);
261       else
262         logbook->myImpacted.Add(L);
263     }
264   }
265
266   // Valid.
267   logbook->myValid.Clear();
268   itr.Initialize(myValid);
269   for (; itr.More(); itr.Next())
270   {
271     const TDF_Label& L = itr.Value();
272     if (!L.IsNull())
273     {
274       TDF_Label relocL;
275       if (RT->HasRelocation(L, relocL))
276         logbook->myValid.Add(relocL);
277       else
278         logbook->myValid.Add(L);
279     }
280   }
281 }
282
283 //=======================================================================
284 //function : NewEmpty
285 //purpose  : Returns new empty graph node attribute
286 //=======================================================================
287
288 Handle(TDF_Attribute) TFunction_Logbook::NewEmpty() const
289 {
290   return new TFunction_Logbook();
291 }
292
293 //=======================================================================
294 //function : Dump
295 //purpose  : Dump of modifications
296 //=======================================================================
297
298 Standard_OStream& TFunction_Logbook::Dump(Standard_OStream& stream) const
299 {
300   TDF_MapIteratorOfLabelMap itr;
301   TCollection_AsciiString as;
302   
303   stream<<"Done = "<<isDone<<endl;
304   stream<<"Touched labels: "<<endl;
305   for (itr.Initialize(myTouched); itr.More(); itr.Next())
306   {
307     TDF_Tool::Entry(itr.Key(), as);
308     stream<<as<<endl;
309   }
310   stream<<"Impacted labels: "<<endl;
311   for (itr.Initialize(myImpacted); itr.More(); itr.Next())
312   {
313     TDF_Tool::Entry(itr.Key(), as);
314     stream<<as<<endl;
315   }  
316   stream<<"Valid labels: "<<endl;
317   for (itr.Initialize(myValid); itr.More(); itr.Next())
318   {
319     TDF_Tool::Entry(itr.Key(), as);
320     stream<<as<<endl;
321   }  
322
323   return stream;
324 }