0028044: Data Exchange - implement data structures for Saved Views
[occt.git] / src / XCAFDoc / XCAFDoc_DocumentTool.cxx
1 // Created on: 2000-08-30
2 // Created by: data exchange team
3 // Copyright (c) 2000-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_GUID.hxx>
18 #include <Standard_Type.hxx>
19 #include <TDataStd_Name.hxx>
20 #include <TDataStd_TreeNode.hxx>
21 #include <TDF_Attribute.hxx>
22 #include <TDF_Data.hxx>
23 #include <TDF_Label.hxx>
24 #include <TDF_RelocationTable.hxx>
25 #include <TDF_Tool.hxx>
26 #include <TDocStd_Document.hxx>
27 #include <XCAFDoc_ColorTool.hxx>
28 #include <XCAFDoc_DimTolTool.hxx>
29 #include <XCAFDoc_DocumentTool.hxx>
30 #include <XCAFDoc_LayerTool.hxx>
31 #include <XCAFDoc_MaterialTool.hxx>
32 #include <XCAFDoc_ShapeTool.hxx>
33 #include <XCAFDoc_ViewTool.hxx>
34
35 IMPLEMENT_STANDARD_RTTIEXT(XCAFDoc_DocumentTool,TDF_Attribute)
36
37 //=======================================================================
38 //function : GetID
39 //purpose  : 
40 //=======================================================================
41 const Standard_GUID& XCAFDoc_DocumentTool::GetID() 
42 {
43   static Standard_GUID DocumentToolID ("efd212ec-6dfd-11d4-b9c8-0060b0ee281b");
44   return DocumentToolID; 
45 }
46
47 namespace {
48 //=======================================================================
49 //function : GetRefID
50 //purpose  : Returns a reference id to find a tree node attribute at the root
51 //           label
52 //=======================================================================
53
54 static const Standard_GUID& GetDocumentToolRefID() 
55 {
56   static Standard_GUID DocumentToolRefID ("efd212eb-6dfd-11d4-b9c8-0060b0ee281b");
57   return DocumentToolRefID; 
58 }
59 }
60
61
62 //=======================================================================
63 //function : Set
64 //purpose  : 
65 //=======================================================================
66
67 Handle(XCAFDoc_DocumentTool) XCAFDoc_DocumentTool::Set(const TDF_Label& L,
68                                                        const Standard_Boolean IsAcces)
69 {
70   Handle(XCAFDoc_DocumentTool) A;
71   TDF_Label aL = DocLabel (L);
72   if (!aL.FindAttribute (XCAFDoc_DocumentTool::GetID(), A)) {
73     if (!IsAcces)
74       aL = L;
75
76     A = new XCAFDoc_DocumentTool;
77     aL.AddAttribute(A);
78     A->Init();
79     // set ShapeTool, ColorTool and LayerTool attributes
80     XCAFDoc_ShapeTool::Set(ShapesLabel(L));
81     XCAFDoc_ColorTool::Set(ColorsLabel(L));
82     XCAFDoc_LayerTool::Set(LayersLabel(L));
83     XCAFDoc_DimTolTool::Set(DGTsLabel(L));
84     XCAFDoc_MaterialTool::Set(MaterialsLabel(L));
85     XCAFDoc_ViewTool::Set(ViewsLabel(L));
86   }
87   return A;
88 }
89
90
91 //=======================================================================
92 //function : DocLabel
93 //purpose  : 
94 //=======================================================================
95
96 TDF_Label XCAFDoc_DocumentTool::DocLabel(const TDF_Label& acces) 
97 {
98   TDF_Label DocL, RootL = acces.Root();
99   const Standard_GUID& aRefGuid = GetDocumentToolRefID();
100   Handle(TDataStd_TreeNode) aRootNode, aLabNode;
101
102   if (RootL.FindAttribute (aRefGuid, aRootNode)) {
103     aLabNode = aRootNode->First();
104     DocL = aLabNode->Label();
105     return DocL;
106   }
107
108   DocL = RootL.FindChild(1);
109   return DocL;
110 }
111
112
113 //=======================================================================
114 //function : Constructor
115 //purpose  : 
116 //=======================================================================
117
118 XCAFDoc_DocumentTool::XCAFDoc_DocumentTool()
119 {
120 }
121
122
123 //=======================================================================
124 //function : ShapesLabel
125 //purpose  : 
126 //=======================================================================
127
128 TDF_Label XCAFDoc_DocumentTool::ShapesLabel(const TDF_Label& acces) 
129 {
130   TDF_Label L = DocLabel(acces).FindChild(1,Standard_True);
131   TDataStd_Name::Set(L, "Shapes");
132   return L;
133 }
134
135
136 //=======================================================================
137 //function : ColorsLabel
138 //purpose  : 
139 //=======================================================================
140
141 TDF_Label XCAFDoc_DocumentTool::ColorsLabel(const TDF_Label& acces) 
142 {
143   TDF_Label L = DocLabel(acces).FindChild(2,Standard_True);
144   TDataStd_Name::Set(L, "Colors");
145   return L;
146 }
147
148
149 //=======================================================================
150 //function : LayersLabel
151 //purpose  : 
152 //=======================================================================
153
154 TDF_Label XCAFDoc_DocumentTool::LayersLabel(const TDF_Label& acces) 
155 {
156   TDF_Label L = DocLabel(acces).FindChild(3,Standard_True);
157   TDataStd_Name::Set(L, "Layers");
158   return L;
159 }
160
161
162 //=======================================================================
163 //function : DGTsLabel
164 //purpose  : 
165 //=======================================================================
166
167 TDF_Label XCAFDoc_DocumentTool::DGTsLabel(const TDF_Label& acces) 
168 {
169   TDF_Label L = DocLabel(acces).FindChild(4,Standard_True);
170   TDataStd_Name::Set(L, "D&GTs");
171   return L;
172 }
173
174
175 //=======================================================================
176 //function : MaterialsLabel
177 //purpose  : 
178 //=======================================================================
179
180 TDF_Label XCAFDoc_DocumentTool::MaterialsLabel(const TDF_Label& acces) 
181 {
182   TDF_Label L = DocLabel(acces).FindChild(5,Standard_True);
183   TDataStd_Name::Set(L, "Materials");
184   return L;
185 }
186
187
188 //=======================================================================
189 //function : ViewsLabel
190 //purpose  : 
191 //=======================================================================
192
193 TDF_Label XCAFDoc_DocumentTool::ViewsLabel(const TDF_Label& acces)
194 {
195   TDF_Label L = DocLabel(acces).FindChild(7, Standard_True);
196   TDataStd_Name::Set(L, "Views");
197   return L;
198 }
199
200
201 //=======================================================================
202 //function : ShapeTool
203 //purpose  : 
204 //=======================================================================
205
206  Handle(XCAFDoc_ShapeTool) XCAFDoc_DocumentTool::ShapeTool(const TDF_Label& acces) 
207 {
208   return XCAFDoc_ShapeTool::Set(ShapesLabel(acces));
209 }
210
211
212 //=======================================================================
213 //function : ColorTool
214 //purpose  : 
215 //=======================================================================
216
217 Handle(XCAFDoc_ColorTool) XCAFDoc_DocumentTool::ColorTool (const TDF_Label& acces) 
218 {
219   return XCAFDoc_ColorTool::Set(ColorsLabel(acces));
220 }
221
222
223 //=======================================================================
224 //function : LayerTool
225 //purpose  : 
226 //=======================================================================
227
228 Handle(XCAFDoc_LayerTool) XCAFDoc_DocumentTool::LayerTool (const TDF_Label& acces) 
229 {
230   return XCAFDoc_LayerTool::Set(LayersLabel(acces));
231 }
232
233
234 //=======================================================================
235 //function : DimTolTool
236 //purpose  : 
237 //=======================================================================
238
239 Handle(XCAFDoc_DimTolTool) XCAFDoc_DocumentTool::DimTolTool(const TDF_Label& acces) 
240 {
241   return XCAFDoc_DimTolTool::Set(DGTsLabel(acces));
242 }
243
244
245 //=======================================================================
246 //function : MaterialTool
247 //purpose  : 
248 //=======================================================================
249
250 Handle(XCAFDoc_MaterialTool) XCAFDoc_DocumentTool::MaterialTool(const TDF_Label& acces) 
251 {
252   return XCAFDoc_MaterialTool::Set(MaterialsLabel(acces));
253 }
254
255 //=======================================================================
256 //function : ViewTool
257 //purpose  : 
258 //=======================================================================
259
260 Handle(XCAFDoc_ViewTool) XCAFDoc_DocumentTool::ViewTool(const TDF_Label& acces)
261 {
262   return XCAFDoc_ViewTool::Set(ViewsLabel(acces));
263 }
264
265
266 //=======================================================================
267 //function : ID
268 //purpose  : 
269 //=======================================================================
270
271 const Standard_GUID& XCAFDoc_DocumentTool::ID() const
272 {
273   return GetID();
274 }
275
276
277 //=======================================================================
278 //function : Restore
279 //purpose  : 
280 //=======================================================================
281
282 void XCAFDoc_DocumentTool::Restore(const Handle(TDF_Attribute)& /* with */) 
283 {
284 }
285
286
287 //=======================================================================
288 //function : NewEmpty
289 //purpose  : 
290 //=======================================================================
291
292 Handle(TDF_Attribute) XCAFDoc_DocumentTool::NewEmpty() const
293 {
294   return new XCAFDoc_DocumentTool;
295 }
296
297
298 //=======================================================================
299 //function : Paste
300 //purpose  : 
301 //=======================================================================
302
303 void XCAFDoc_DocumentTool::Paste (const Handle(TDF_Attribute)& /* into */,
304                                   const Handle(TDF_RelocationTable)& /* RT */) const
305 {
306 }
307
308
309 //=======================================================================
310 //function : Init
311 //purpose  : 
312 //=======================================================================
313
314 void XCAFDoc_DocumentTool::Init() const
315 {
316   TDF_Label DocL = Label(), RootL = DocL.Root();
317   const Standard_GUID& aRefGuid = GetDocumentToolRefID();
318   Handle(TDataStd_TreeNode) aRootNode, aLabNode;
319
320   if (!RootL.FindAttribute (aRefGuid, aRootNode)) {
321     Handle(TDataStd_TreeNode) aRootNodeNew = TDataStd_TreeNode::Set (RootL, aRefGuid);
322     Handle(TDataStd_TreeNode) aLNode = TDataStd_TreeNode::Set (DocL, aRefGuid);
323     aLNode->SetFather (aRootNodeNew);
324     aRootNodeNew->SetFirst (aLNode);
325   }
326 }
327
328
329 //=======================================================================
330 //function : IsXCAFDocument
331 //purpose  : 
332 //=======================================================================
333
334 Standard_Boolean XCAFDoc_DocumentTool::IsXCAFDocument(const  Handle(TDocStd_Document)& D)
335 {
336   TDF_Label RootL = D->Main().Root();
337   const Standard_GUID& aRefGuid = GetDocumentToolRefID();
338   Handle(TDataStd_TreeNode) aRootNode;
339   return RootL.FindAttribute (aRefGuid, aRootNode);
340 }