0033041: Coding - get rid of unused headers [TopTools to Xw]
[occt.git] / src / XCAFDoc / XCAFDoc_VisMaterialTool.cxx
1 // Copyright (c) 2019 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 #include <XCAFDoc_VisMaterialTool.hxx>
15
16 #include <TDataStd_Name.hxx>
17 #include <TDataStd_TreeNode.hxx>
18 #include <TDF_Attribute.hxx>
19 #include <TDF_ChildIDIterator.hxx>
20 #include <TDF_Label.hxx>
21 #include <TNaming_NamedShape.hxx>
22 #include <XCAFDoc.hxx>
23 #include <XCAFDoc_VisMaterial.hxx>
24 #include <XCAFDoc_DocumentTool.hxx>
25 #include <XCAFDoc_ShapeTool.hxx>
26
27 IMPLEMENT_STANDARD_RTTIEXT(XCAFDoc_VisMaterialTool, TDF_Attribute)
28
29 //=======================================================================
30 //function : GetID
31 //purpose  :
32 //=======================================================================
33 const Standard_GUID& XCAFDoc_VisMaterialTool::GetID()
34 {
35   static Standard_GUID THE_VIS_MAT_TOOL_ID ("87B511CE-DA15-4A5E-98AF-E3F46AB5B6E8");
36   return THE_VIS_MAT_TOOL_ID;
37 }
38
39 //=======================================================================
40 //function : Set
41 //purpose  :
42 //=======================================================================
43 Handle(XCAFDoc_VisMaterialTool) XCAFDoc_VisMaterialTool::Set (const TDF_Label& theLabel)
44 {
45   Handle(XCAFDoc_VisMaterialTool) aTool;
46   if (!theLabel.FindAttribute (XCAFDoc_VisMaterialTool::GetID(), aTool))
47   {
48     aTool = new XCAFDoc_VisMaterialTool();
49     theLabel.AddAttribute (aTool);
50     aTool->myShapeTool = XCAFDoc_DocumentTool::ShapeTool (theLabel);
51   }
52   return aTool;
53 }
54
55
56 //=======================================================================
57 //function : XCAFDoc_VisMaterialTool
58 //purpose  :
59 //=======================================================================
60 XCAFDoc_VisMaterialTool::XCAFDoc_VisMaterialTool()
61 {
62   //
63 }
64
65 //=======================================================================
66 //function : ShapeTool
67 //purpose  :
68 //=======================================================================
69 const Handle(XCAFDoc_ShapeTool)& XCAFDoc_VisMaterialTool::ShapeTool()
70 {
71   if (myShapeTool.IsNull())
72   {
73     myShapeTool = XCAFDoc_DocumentTool::ShapeTool (Label());
74   }
75   return myShapeTool;
76 }
77
78 //=======================================================================
79 //function : GetMaterial
80 //purpose  :
81 //=======================================================================
82 Handle(XCAFDoc_VisMaterial) XCAFDoc_VisMaterialTool::GetMaterial (const TDF_Label& theMatLabel) const
83 {
84   Handle(XCAFDoc_VisMaterial) aMatAttrib;
85   if (theMatLabel.Father() == Label())
86   {
87     theMatLabel.FindAttribute (XCAFDoc_VisMaterial::GetID(), aMatAttrib);
88   }
89   return aMatAttrib;
90 }
91
92 //=======================================================================
93 //function : AddMaterial
94 //purpose  :
95 //=======================================================================
96 TDF_Label XCAFDoc_VisMaterialTool::AddMaterial (const Handle(XCAFDoc_VisMaterial)& theMat,
97                                                 const TCollection_AsciiString& theName) const
98 {
99   TDF_TagSource aTag;
100   TDF_Label aLab = aTag.NewChild (Label());
101   aLab.AddAttribute (theMat);
102   if (!theName.IsEmpty())
103   {
104     TDataStd_Name::Set (aLab, theName);
105   }
106   return aLab;
107 }
108
109 //=======================================================================
110 //function : AddMaterial
111 //purpose  :
112 //=======================================================================
113 TDF_Label XCAFDoc_VisMaterialTool::AddMaterial (const TCollection_AsciiString& theName) const
114 {
115   Handle(XCAFDoc_VisMaterial) aNewMat = new XCAFDoc_VisMaterial();
116   TDF_TagSource aTag;
117   TDF_Label aLab = aTag.NewChild (Label());
118   aLab.AddAttribute (aNewMat);
119   if (!theName.IsEmpty())
120   {
121     TDataStd_Name::Set (aLab, theName);
122   }
123   return aLab;
124 }
125
126 //=======================================================================
127 //function : RemoveMaterial
128 //purpose  :
129 //=======================================================================
130 void XCAFDoc_VisMaterialTool::RemoveMaterial (const TDF_Label& theLabel) const
131 {
132   theLabel.ForgetAllAttributes (true);
133 }
134
135 //=======================================================================
136 //function : GetMaterials
137 //purpose  :
138 //=======================================================================
139 void XCAFDoc_VisMaterialTool::GetMaterials (TDF_LabelSequence& theLabels) const
140 {
141   theLabels.Clear();
142   for (TDF_ChildIDIterator aChildIDIterator (Label(), XCAFDoc_VisMaterial::GetID()); aChildIDIterator.More(); aChildIDIterator.Next())
143   {
144     const TDF_Label aLabel = aChildIDIterator.Value()->Label();
145     if (IsMaterial (aLabel))
146     {
147       theLabels.Append (aLabel);
148     }
149   }
150 }
151
152 //=======================================================================
153 //function : SetShapeMaterial
154 //purpose  :
155 //=======================================================================
156 void XCAFDoc_VisMaterialTool::SetShapeMaterial (const TDF_Label& theShapeLabel,
157                                                 const TDF_Label& theMaterialLabel) const
158 {
159   if (theMaterialLabel.IsNull())
160   {
161     theShapeLabel.ForgetAttribute (XCAFDoc::VisMaterialRefGUID());
162     return;
163   }
164
165   // set reference
166   Handle(TDataStd_TreeNode) aMainNode = TDataStd_TreeNode::Set (theMaterialLabel, XCAFDoc::VisMaterialRefGUID());
167   Handle(TDataStd_TreeNode) aRefNode  = TDataStd_TreeNode::Set (theShapeLabel,    XCAFDoc::VisMaterialRefGUID());
168   aRefNode->Remove(); // abv: fix against bug in TreeNode::Append()
169   aMainNode->Prepend (aRefNode);
170 }
171
172 //=======================================================================
173 //function : UnSetShapeMaterial
174 //purpose  :
175 //=======================================================================
176 void XCAFDoc_VisMaterialTool::UnSetShapeMaterial (const TDF_Label& theShapeLabel) const
177 {
178   theShapeLabel.ForgetAttribute (XCAFDoc::VisMaterialRefGUID());
179 }
180
181 //=======================================================================
182 //function : IsSetShapeMaterial
183 //purpose  :
184 //=======================================================================
185 Standard_Boolean XCAFDoc_VisMaterialTool::IsSetShapeMaterial (const TDF_Label& theLabel) const
186 {
187   Handle(TDataStd_TreeNode) aNode;
188   return theLabel.FindAttribute (XCAFDoc::VisMaterialRefGUID(), aNode)
189       && aNode->HasFather();
190 }
191
192 //=======================================================================
193 //function : GetShapeMaterial
194 //purpose  :
195 //=======================================================================
196 Standard_Boolean XCAFDoc_VisMaterialTool::GetShapeMaterial (const TDF_Label& theShapeLabel,
197                                                             TDF_Label& theMaterialLabel)
198 {
199   Handle(TDataStd_TreeNode) aNode;
200   if (!theShapeLabel.FindAttribute (XCAFDoc::VisMaterialRefGUID(), aNode)
201    || !aNode->HasFather())
202   {
203     return Standard_False;
204   }
205
206   theMaterialLabel = aNode->Father()->Label();
207   return Standard_True;
208 }
209
210 //=======================================================================
211 //function : GetShapeMaterial
212 //purpose  :
213 //=======================================================================
214 Handle(XCAFDoc_VisMaterial) XCAFDoc_VisMaterialTool::GetShapeMaterial (const TDF_Label& theShapeLabel)
215 {
216   TDF_Label aMatLabel;
217   return Label().HasChild() // do not waste time on shape attributes if materials map is empty
218       && GetShapeMaterial (theShapeLabel, aMatLabel)
219        ? GetMaterial (aMatLabel)
220        : Handle(XCAFDoc_VisMaterial)();
221 }
222
223 //=======================================================================
224 //function : SetShapeMaterial
225 //purpose  :
226 //=======================================================================
227 Standard_Boolean XCAFDoc_VisMaterialTool::SetShapeMaterial (const TopoDS_Shape& theShape,
228                                                             const TDF_Label& theMaterialLabel)
229 {
230   TDF_Label aShapeLabel;
231   if (!ShapeTool()->Search (theShape, aShapeLabel))
232   {
233     return Standard_False;
234   }
235
236   SetShapeMaterial (aShapeLabel, theMaterialLabel);
237   return Standard_True;
238 }
239
240 //=======================================================================
241 //function : UnSetShapeMaterial
242 //purpose  :
243 //=======================================================================
244 Standard_Boolean XCAFDoc_VisMaterialTool::UnSetShapeMaterial (const TopoDS_Shape& theShape)
245 {
246   TDF_Label aShapeLabel;
247   if (!ShapeTool()->Search (theShape, aShapeLabel))
248   {
249     return Standard_False;
250   }
251
252   UnSetShapeMaterial (aShapeLabel);
253   return Standard_True;
254 }
255
256 //=======================================================================
257 //function : IsSetShapeMaterial
258 //purpose  :
259 //=======================================================================
260 Standard_Boolean XCAFDoc_VisMaterialTool::IsSetShapeMaterial (const TopoDS_Shape& theShape)
261 {
262   TDF_Label aShapeLabel;
263   return ShapeTool()->Search (theShape, aShapeLabel)
264       && IsSetShapeMaterial (aShapeLabel);
265 }
266
267 //=======================================================================
268 //function : GetShapeMaterial
269 //purpose  :
270 //=======================================================================
271 Standard_Boolean XCAFDoc_VisMaterialTool::GetShapeMaterial (const TopoDS_Shape& theShape,
272                                                             TDF_Label& theMaterialLabel)
273 {
274   TDF_Label aShapeLabel;
275   return ShapeTool()->Search (theShape, aShapeLabel)
276       && GetShapeMaterial (aShapeLabel, theMaterialLabel);
277 }
278
279 //=======================================================================
280 //function : GetShapeMaterial
281 //purpose  :
282 //=======================================================================
283 Handle(XCAFDoc_VisMaterial) XCAFDoc_VisMaterialTool::GetShapeMaterial (const TopoDS_Shape& theShape)
284 {
285   TDF_Label aMatLabel;
286   return GetShapeMaterial (theShape, aMatLabel)
287        ? GetMaterial (aMatLabel)
288        : Handle(XCAFDoc_VisMaterial)();
289 }