1 // Copyright (c) 1999-2014 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.
15 #include <Standard_GUID.hxx>
16 #include <Standard_Type.hxx>
17 #include <TCollection_HAsciiString.hxx>
18 #include <TDataStd_Name.hxx>
19 #include <TDataStd_TreeNode.hxx>
20 #include <TDF_Attribute.hxx>
21 #include <TDF_ChildIDIterator.hxx>
22 #include <TDF_Label.hxx>
23 #include <TDF_RelocationTable.hxx>
24 #include <XCAFDoc.hxx>
25 #include <XCAFDoc_DocumentTool.hxx>
26 #include <XCAFDoc_Material.hxx>
27 #include <XCAFDoc_MaterialTool.hxx>
28 #include <XCAFDoc_ShapeTool.hxx>
30 IMPLEMENT_STANDARD_RTTIEXT(XCAFDoc_MaterialTool,TDF_Attribute)
32 //=======================================================================
33 //function : XCAFDoc_MaterialTool
35 //=======================================================================
36 XCAFDoc_MaterialTool::XCAFDoc_MaterialTool()
41 //=======================================================================
44 //=======================================================================
46 Handle(XCAFDoc_MaterialTool) XCAFDoc_MaterialTool::Set(const TDF_Label& L)
48 Handle(XCAFDoc_MaterialTool) A;
49 if (!L.FindAttribute (XCAFDoc_MaterialTool::GetID(), A)) {
50 A = new XCAFDoc_MaterialTool ();
52 A->myShapeTool = XCAFDoc_DocumentTool::ShapeTool(L);
58 //=======================================================================
61 //=======================================================================
63 const Standard_GUID& XCAFDoc_MaterialTool::GetID()
65 static Standard_GUID MatTblID ("efd212f9-6dfd-11d4-b9c8-0060b0ee281b");
70 //=======================================================================
71 //function : BaseLabel
73 //=======================================================================
75 TDF_Label XCAFDoc_MaterialTool::BaseLabel() const
81 //=======================================================================
82 //function : ShapeTool
84 //=======================================================================
86 const Handle(XCAFDoc_ShapeTool)& XCAFDoc_MaterialTool::ShapeTool()
88 if(myShapeTool.IsNull())
89 myShapeTool = XCAFDoc_DocumentTool::ShapeTool(Label());
94 //=======================================================================
95 //function : IsMaterial
97 //=======================================================================
99 Standard_Boolean XCAFDoc_MaterialTool::IsMaterial(const TDF_Label& lab) const
101 Handle(XCAFDoc_Material) MatAttr;
102 if(lab.FindAttribute(XCAFDoc_Material::GetID(),MatAttr)) {
103 return Standard_True;
105 return Standard_False;
109 //=======================================================================
110 //function : GetMaterialLabels
112 //=======================================================================
114 void XCAFDoc_MaterialTool::GetMaterialLabels(TDF_LabelSequence& Labels) const
117 TDF_ChildIterator ChildIterator( Label() );
118 for (; ChildIterator.More(); ChildIterator.Next()) {
119 TDF_Label L = ChildIterator.Value();
120 if ( IsMaterial(L)) Labels.Append(L);
125 //=======================================================================
126 //function : AddMaterial
128 //=======================================================================
130 TDF_Label XCAFDoc_MaterialTool::AddMaterial(const Handle(TCollection_HAsciiString)& aName,
131 const Handle(TCollection_HAsciiString)& aDescription,
132 const Standard_Real aDensity,
133 const Handle(TCollection_HAsciiString)& aDensName,
134 const Handle(TCollection_HAsciiString)& aDensValType) const
138 MatL = aTag.NewChild ( Label() );
139 XCAFDoc_Material::Set(MatL,aName,aDescription,aDensity,aDensName,aDensValType);
140 TDataStd_Name::Set(MatL,TCollection_AsciiString(aName->ToCString()));
145 //=======================================================================
146 //function : SetMaterial
148 //=======================================================================
150 void XCAFDoc_MaterialTool::SetMaterial(const TDF_Label& L,const TDF_Label& MatL) const
153 Handle(TDataStd_TreeNode) refNode, mainNode;
154 mainNode = TDataStd_TreeNode::Set ( MatL, XCAFDoc::MaterialRefGUID() );
155 refNode = TDataStd_TreeNode::Set ( L, XCAFDoc::MaterialRefGUID() );
156 refNode->Remove(); // abv: fix against bug in TreeNode::Append()
157 mainNode->Append(refNode);
161 //=======================================================================
162 //function : SetMaterial
164 //=======================================================================
166 void XCAFDoc_MaterialTool::SetMaterial(const TDF_Label& L,
167 const Handle(TCollection_HAsciiString)& aName,
168 const Handle(TCollection_HAsciiString)& aDescription,
169 const Standard_Real aDensity,
170 const Handle(TCollection_HAsciiString)& aDensName,
171 const Handle(TCollection_HAsciiString)& aDensValType) const
173 TDF_Label MatL = AddMaterial(aName,aDescription,aDensity,aDensName,aDensValType);
178 //=======================================================================
179 //function : GetMaterial
181 //=======================================================================
183 Standard_Boolean XCAFDoc_MaterialTool::GetMaterial(const TDF_Label& MatL,
184 Handle(TCollection_HAsciiString)& aName,
185 Handle(TCollection_HAsciiString)& aDescription,
186 Standard_Real& aDensity,
187 Handle(TCollection_HAsciiString)& aDensName,
188 Handle(TCollection_HAsciiString)& aDensValType) const
190 Handle(XCAFDoc_Material) MatAttr;
191 if(!MatL.FindAttribute(XCAFDoc_Material::GetID(),MatAttr)) {
192 return Standard_False;
194 aName = MatAttr->GetName();
195 aDescription = MatAttr->GetDescription();
196 aDensity = MatAttr->GetDensity();
197 aDensName = MatAttr->GetDensName();
198 aDensValType = MatAttr->GetDensValType();
200 return Standard_True;
204 //=======================================================================
205 //function : GetDensityForShape
207 //=======================================================================
209 Standard_Real XCAFDoc_MaterialTool::GetDensityForShape(const TDF_Label& ShapeL)
211 Standard_Real Dens=0.0;
212 Handle(TDataStd_TreeNode) Node;
213 if ( ! ShapeL.FindAttribute ( XCAFDoc::MaterialRefGUID(), Node) ||
214 ! Node->HasFather() ) return Dens;
215 TDF_Label MatL = Node->Father()->Label();
216 Handle(XCAFDoc_Material) MatAttr;
217 if(!MatL.FindAttribute(XCAFDoc_Material::GetID(),MatAttr)) {
220 // defaul dimension fo density - gram/sm^3
221 // we transfer "sm" into "mm"
222 Dens = MatAttr->GetDensity()*0.001;
227 //=======================================================================
230 //=======================================================================
232 const Standard_GUID& XCAFDoc_MaterialTool::ID() const
238 //=======================================================================
241 //=======================================================================
243 void XCAFDoc_MaterialTool::Restore(const Handle(TDF_Attribute)& /*with*/)
248 //=======================================================================
249 //function : NewEmpty
251 //=======================================================================
253 Handle(TDF_Attribute) XCAFDoc_MaterialTool::NewEmpty() const
255 return new XCAFDoc_MaterialTool;
259 //=======================================================================
262 //=======================================================================
264 void XCAFDoc_MaterialTool::Paste(const Handle(TDF_Attribute)& /*into*/,
265 const Handle(TDF_RelocationTable)& /*RT*/) const