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 <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>
27 IMPLEMENT_STANDARD_RTTIEXT(XCAFDoc_VisMaterialTool, TDF_Attribute)
29 //=======================================================================
32 //=======================================================================
33 const Standard_GUID& XCAFDoc_VisMaterialTool::GetID()
35 static Standard_GUID THE_VIS_MAT_TOOL_ID ("87B511CE-DA15-4A5E-98AF-E3F46AB5B6E8");
36 return THE_VIS_MAT_TOOL_ID;
39 //=======================================================================
42 //=======================================================================
43 Handle(XCAFDoc_VisMaterialTool) XCAFDoc_VisMaterialTool::Set (const TDF_Label& theLabel)
45 Handle(XCAFDoc_VisMaterialTool) aTool;
46 if (!theLabel.FindAttribute (XCAFDoc_VisMaterialTool::GetID(), aTool))
48 aTool = new XCAFDoc_VisMaterialTool();
49 theLabel.AddAttribute (aTool);
50 aTool->myShapeTool = XCAFDoc_DocumentTool::ShapeTool (theLabel);
56 //=======================================================================
57 //function : XCAFDoc_VisMaterialTool
59 //=======================================================================
60 XCAFDoc_VisMaterialTool::XCAFDoc_VisMaterialTool()
65 //=======================================================================
66 //function : ShapeTool
68 //=======================================================================
69 const Handle(XCAFDoc_ShapeTool)& XCAFDoc_VisMaterialTool::ShapeTool()
71 if (myShapeTool.IsNull())
73 myShapeTool = XCAFDoc_DocumentTool::ShapeTool (Label());
78 //=======================================================================
79 //function : GetMaterial
81 //=======================================================================
82 Handle(XCAFDoc_VisMaterial) XCAFDoc_VisMaterialTool::GetMaterial (const TDF_Label& theMatLabel) const
84 Handle(XCAFDoc_VisMaterial) aMatAttrib;
85 if (theMatLabel.Father() == Label())
87 theMatLabel.FindAttribute (XCAFDoc_VisMaterial::GetID(), aMatAttrib);
92 //=======================================================================
93 //function : AddMaterial
95 //=======================================================================
96 TDF_Label XCAFDoc_VisMaterialTool::AddMaterial (const Handle(XCAFDoc_VisMaterial)& theMat,
97 const TCollection_AsciiString& theName) const
100 TDF_Label aLab = aTag.NewChild (Label());
101 aLab.AddAttribute (theMat);
102 if (!theName.IsEmpty())
104 TDataStd_Name::Set (aLab, theName);
109 //=======================================================================
110 //function : AddMaterial
112 //=======================================================================
113 TDF_Label XCAFDoc_VisMaterialTool::AddMaterial (const TCollection_AsciiString& theName) const
115 Handle(XCAFDoc_VisMaterial) aNewMat = new XCAFDoc_VisMaterial();
117 TDF_Label aLab = aTag.NewChild (Label());
118 aLab.AddAttribute (aNewMat);
119 if (!theName.IsEmpty())
121 TDataStd_Name::Set (aLab, theName);
126 //=======================================================================
127 //function : RemoveMaterial
129 //=======================================================================
130 void XCAFDoc_VisMaterialTool::RemoveMaterial (const TDF_Label& theLabel) const
132 theLabel.ForgetAllAttributes (true);
135 //=======================================================================
136 //function : GetMaterials
138 //=======================================================================
139 void XCAFDoc_VisMaterialTool::GetMaterials (TDF_LabelSequence& theLabels) const
142 for (TDF_ChildIDIterator aChildIDIterator (Label(), XCAFDoc_VisMaterial::GetID()); aChildIDIterator.More(); aChildIDIterator.Next())
144 const TDF_Label aLabel = aChildIDIterator.Value()->Label();
145 if (IsMaterial (aLabel))
147 theLabels.Append (aLabel);
152 //=======================================================================
153 //function : SetShapeMaterial
155 //=======================================================================
156 void XCAFDoc_VisMaterialTool::SetShapeMaterial (const TDF_Label& theShapeLabel,
157 const TDF_Label& theMaterialLabel) const
159 if (theMaterialLabel.IsNull())
161 theShapeLabel.ForgetAttribute (XCAFDoc::VisMaterialRefGUID());
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);
172 //=======================================================================
173 //function : UnSetShapeMaterial
175 //=======================================================================
176 void XCAFDoc_VisMaterialTool::UnSetShapeMaterial (const TDF_Label& theShapeLabel) const
178 theShapeLabel.ForgetAttribute (XCAFDoc::VisMaterialRefGUID());
181 //=======================================================================
182 //function : IsSetShapeMaterial
184 //=======================================================================
185 Standard_Boolean XCAFDoc_VisMaterialTool::IsSetShapeMaterial (const TDF_Label& theLabel) const
187 Handle(TDataStd_TreeNode) aNode;
188 return theLabel.FindAttribute (XCAFDoc::VisMaterialRefGUID(), aNode)
189 && aNode->HasFather();
192 //=======================================================================
193 //function : GetShapeMaterial
195 //=======================================================================
196 Standard_Boolean XCAFDoc_VisMaterialTool::GetShapeMaterial (const TDF_Label& theShapeLabel,
197 TDF_Label& theMaterialLabel)
199 Handle(TDataStd_TreeNode) aNode;
200 if (!theShapeLabel.FindAttribute (XCAFDoc::VisMaterialRefGUID(), aNode)
201 || !aNode->HasFather())
203 return Standard_False;
206 theMaterialLabel = aNode->Father()->Label();
207 return Standard_True;
210 //=======================================================================
211 //function : GetShapeMaterial
213 //=======================================================================
214 Handle(XCAFDoc_VisMaterial) XCAFDoc_VisMaterialTool::GetShapeMaterial (const TDF_Label& theShapeLabel)
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)();
223 //=======================================================================
224 //function : SetShapeMaterial
226 //=======================================================================
227 Standard_Boolean XCAFDoc_VisMaterialTool::SetShapeMaterial (const TopoDS_Shape& theShape,
228 const TDF_Label& theMaterialLabel)
230 TDF_Label aShapeLabel;
231 if (!ShapeTool()->Search (theShape, aShapeLabel))
233 return Standard_False;
236 SetShapeMaterial (aShapeLabel, theMaterialLabel);
237 return Standard_True;
240 //=======================================================================
241 //function : UnSetShapeMaterial
243 //=======================================================================
244 Standard_Boolean XCAFDoc_VisMaterialTool::UnSetShapeMaterial (const TopoDS_Shape& theShape)
246 TDF_Label aShapeLabel;
247 if (!ShapeTool()->Search (theShape, aShapeLabel))
249 return Standard_False;
252 UnSetShapeMaterial (aShapeLabel);
253 return Standard_True;
256 //=======================================================================
257 //function : IsSetShapeMaterial
259 //=======================================================================
260 Standard_Boolean XCAFDoc_VisMaterialTool::IsSetShapeMaterial (const TopoDS_Shape& theShape)
262 TDF_Label aShapeLabel;
263 return ShapeTool()->Search (theShape, aShapeLabel)
264 && IsSetShapeMaterial (aShapeLabel);
267 //=======================================================================
268 //function : GetShapeMaterial
270 //=======================================================================
271 Standard_Boolean XCAFDoc_VisMaterialTool::GetShapeMaterial (const TopoDS_Shape& theShape,
272 TDF_Label& theMaterialLabel)
274 TDF_Label aShapeLabel;
275 return ShapeTool()->Search (theShape, aShapeLabel)
276 && GetShapeMaterial (aShapeLabel, theMaterialLabel);
279 //=======================================================================
280 //function : GetShapeMaterial
282 //=======================================================================
283 Handle(XCAFDoc_VisMaterial) XCAFDoc_VisMaterialTool::GetShapeMaterial (const TopoDS_Shape& theShape)
286 return GetShapeMaterial (theShape, aMatLabel)
287 ? GetMaterial (aMatLabel)
288 : Handle(XCAFDoc_VisMaterial)();