1 // Copyright (c) 2019 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
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.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #include <XCAFDoc_VisMaterialTool.hxx>
16 #include <Standard_GUID.hxx>
17 #include <TDataStd_Name.hxx>
18 #include <TDataStd_TreeNode.hxx>
19 #include <TDataStd_UAttribute.hxx>
20 #include <TDF_Attribute.hxx>
21 #include <TDF_ChildIDIterator.hxx>
22 #include <TDF_Label.hxx>
23 #include <TNaming_NamedShape.hxx>
24 #include <TopoDS_Shape.hxx>
25 #include <XCAFDoc.hxx>
26 #include <XCAFDoc_VisMaterial.hxx>
27 #include <XCAFDoc_DocumentTool.hxx>
28 #include <XCAFDoc_ShapeTool.hxx>
30 IMPLEMENT_STANDARD_RTTIEXT(XCAFDoc_VisMaterialTool, TDF_Attribute)
32 //=======================================================================
35 //=======================================================================
36 const Standard_GUID& XCAFDoc_VisMaterialTool::GetID()
38 static Standard_GUID THE_VIS_MAT_TOOL_ID ("87B511CE-DA15-4A5E-98AF-E3F46AB5B6E8");
39 return THE_VIS_MAT_TOOL_ID;
42 //=======================================================================
45 //=======================================================================
46 Handle(XCAFDoc_VisMaterialTool) XCAFDoc_VisMaterialTool::Set (const TDF_Label& theLabel)
48 Handle(XCAFDoc_VisMaterialTool) aTool;
49 if (!theLabel.FindAttribute (XCAFDoc_VisMaterialTool::GetID(), aTool))
51 aTool = new XCAFDoc_VisMaterialTool();
52 theLabel.AddAttribute (aTool);
53 aTool->myShapeTool = XCAFDoc_DocumentTool::ShapeTool (theLabel);
59 //=======================================================================
60 //function : XCAFDoc_VisMaterialTool
62 //=======================================================================
63 XCAFDoc_VisMaterialTool::XCAFDoc_VisMaterialTool()
68 //=======================================================================
69 //function : ShapeTool
71 //=======================================================================
72 const Handle(XCAFDoc_ShapeTool)& XCAFDoc_VisMaterialTool::ShapeTool()
74 if (myShapeTool.IsNull())
76 myShapeTool = XCAFDoc_DocumentTool::ShapeTool (Label());
81 //=======================================================================
82 //function : GetMaterial
84 //=======================================================================
85 Handle(XCAFDoc_VisMaterial) XCAFDoc_VisMaterialTool::GetMaterial (const TDF_Label& theMatLabel) const
87 Handle(XCAFDoc_VisMaterial) aMatAttrib;
88 if (theMatLabel.Father() == Label())
90 theMatLabel.FindAttribute (XCAFDoc_VisMaterial::GetID(), aMatAttrib);
95 //=======================================================================
96 //function : AddMaterial
98 //=======================================================================
99 TDF_Label XCAFDoc_VisMaterialTool::AddMaterial (const Handle(XCAFDoc_VisMaterial)& theMat,
100 const TCollection_AsciiString& theName) const
103 TDF_Label aLab = aTag.NewChild (Label());
104 aLab.AddAttribute (theMat);
105 if (!theName.IsEmpty())
107 TDataStd_Name::Set (aLab, theName);
112 //=======================================================================
113 //function : AddMaterial
115 //=======================================================================
116 TDF_Label XCAFDoc_VisMaterialTool::AddMaterial (const TCollection_AsciiString& theName) const
118 Handle(XCAFDoc_VisMaterial) aNewMat = new XCAFDoc_VisMaterial();
120 TDF_Label aLab = aTag.NewChild (Label());
121 aLab.AddAttribute (aNewMat);
122 if (!theName.IsEmpty())
124 TDataStd_Name::Set (aLab, theName);
129 //=======================================================================
130 //function : RemoveMaterial
132 //=======================================================================
133 void XCAFDoc_VisMaterialTool::RemoveMaterial (const TDF_Label& theLabel) const
135 theLabel.ForgetAllAttributes (true);
138 //=======================================================================
139 //function : GetMaterials
141 //=======================================================================
142 void XCAFDoc_VisMaterialTool::GetMaterials (TDF_LabelSequence& theLabels) const
145 for (TDF_ChildIDIterator aChildIDIterator (Label(), XCAFDoc_VisMaterial::GetID()); aChildIDIterator.More(); aChildIDIterator.Next())
147 const TDF_Label aLabel = aChildIDIterator.Value()->Label();
148 if (IsMaterial (aLabel))
150 theLabels.Append (aLabel);
155 //=======================================================================
156 //function : SetShapeMaterial
158 //=======================================================================
159 void XCAFDoc_VisMaterialTool::SetShapeMaterial (const TDF_Label& theShapeLabel,
160 const TDF_Label& theMaterialLabel) const
162 if (theMaterialLabel.IsNull())
164 theShapeLabel.ForgetAttribute (XCAFDoc::VisMaterialRefGUID());
169 Handle(TDataStd_TreeNode) aMainNode = TDataStd_TreeNode::Set (theMaterialLabel, XCAFDoc::VisMaterialRefGUID());
170 Handle(TDataStd_TreeNode) aRefNode = TDataStd_TreeNode::Set (theShapeLabel, XCAFDoc::VisMaterialRefGUID());
171 aRefNode->Remove(); // abv: fix against bug in TreeNode::Append()
172 aMainNode->Prepend (aRefNode);
175 //=======================================================================
176 //function : UnSetShapeMaterial
178 //=======================================================================
179 void XCAFDoc_VisMaterialTool::UnSetShapeMaterial (const TDF_Label& theShapeLabel) const
181 theShapeLabel.ForgetAttribute (XCAFDoc::VisMaterialRefGUID());
184 //=======================================================================
185 //function : IsSetShapeMaterial
187 //=======================================================================
188 Standard_Boolean XCAFDoc_VisMaterialTool::IsSetShapeMaterial (const TDF_Label& theLabel) const
190 Handle(TDataStd_TreeNode) aNode;
191 return theLabel.FindAttribute (XCAFDoc::VisMaterialRefGUID(), aNode)
192 && aNode->HasFather();
195 //=======================================================================
196 //function : GetShapeMaterial
198 //=======================================================================
199 Standard_Boolean XCAFDoc_VisMaterialTool::GetShapeMaterial (const TDF_Label& theShapeLabel,
200 TDF_Label& theMaterialLabel)
202 Handle(TDataStd_TreeNode) aNode;
203 if (!theShapeLabel.FindAttribute (XCAFDoc::VisMaterialRefGUID(), aNode)
204 || !aNode->HasFather())
206 return Standard_False;
209 theMaterialLabel = aNode->Father()->Label();
210 return Standard_True;
213 //=======================================================================
214 //function : GetShapeMaterial
216 //=======================================================================
217 Handle(XCAFDoc_VisMaterial) XCAFDoc_VisMaterialTool::GetShapeMaterial (const TDF_Label& theShapeLabel)
220 return Label().HasChild() // do not waste time on shape attributes if materials map is empty
221 && GetShapeMaterial (theShapeLabel, aMatLabel)
222 ? GetMaterial (aMatLabel)
223 : Handle(XCAFDoc_VisMaterial)();
226 //=======================================================================
227 //function : SetShapeMaterial
229 //=======================================================================
230 Standard_Boolean XCAFDoc_VisMaterialTool::SetShapeMaterial (const TopoDS_Shape& theShape,
231 const TDF_Label& theMaterialLabel)
233 TDF_Label aShapeLabel;
234 if (!ShapeTool()->Search (theShape, aShapeLabel))
236 return Standard_False;
239 SetShapeMaterial (aShapeLabel, theMaterialLabel);
240 return Standard_True;
243 //=======================================================================
244 //function : UnSetShapeMaterial
246 //=======================================================================
247 Standard_Boolean XCAFDoc_VisMaterialTool::UnSetShapeMaterial (const TopoDS_Shape& theShape)
249 TDF_Label aShapeLabel;
250 if (!ShapeTool()->Search (theShape, aShapeLabel))
252 return Standard_False;
255 UnSetShapeMaterial (aShapeLabel);
256 return Standard_True;
259 //=======================================================================
260 //function : IsSetShapeMaterial
262 //=======================================================================
263 Standard_Boolean XCAFDoc_VisMaterialTool::IsSetShapeMaterial (const TopoDS_Shape& theShape)
265 TDF_Label aShapeLabel;
266 return ShapeTool()->Search (theShape, aShapeLabel)
267 && IsSetShapeMaterial (aShapeLabel);
270 //=======================================================================
271 //function : GetShapeMaterial
273 //=======================================================================
274 Standard_Boolean XCAFDoc_VisMaterialTool::GetShapeMaterial (const TopoDS_Shape& theShape,
275 TDF_Label& theMaterialLabel)
277 TDF_Label aShapeLabel;
278 return ShapeTool()->Search (theShape, aShapeLabel)
279 && GetShapeMaterial (aShapeLabel, theMaterialLabel);
282 //=======================================================================
283 //function : GetShapeMaterial
285 //=======================================================================
286 Handle(XCAFDoc_VisMaterial) XCAFDoc_VisMaterialTool::GetShapeMaterial (const TopoDS_Shape& theShape)
289 return GetShapeMaterial (theShape, aMatLabel)
290 ? GetMaterial (aMatLabel)
291 : Handle(XCAFDoc_VisMaterial)();