1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
3 // The content of this file is subject to the Open CASCADE Technology Public
4 // License Version 6.5 (the "License"). You may not use the content of this file
5 // except in compliance with the License. Please obtain a copy of the License
6 // at http://www.opencascade.org and read it completely before using this file.
8 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11 // The Original Code and all software distributed under the License is
12 // distributed on an "AS IS" basis, without warranty of any kind, and the
13 // Initial Developer hereby disclaims all such warranties, including without
14 // limitation, any warranties of merchantability, fitness for a particular
15 // purpose or non-infringement. Please see the License for the specific terms
16 // and conditions governing the rights and limitations under the License.
18 #include <XCAFDoc_MaterialTool.ixx>
20 #include <XCAFDoc.hxx>
21 #include <XCAFDoc_Material.hxx>
22 #include <XCAFDoc_DocumentTool.hxx>
23 #include <TDataStd_TreeNode.hxx>
24 #include <TDataStd_Name.hxx>
25 #include <TDF_ChildIDIterator.hxx>
28 //=======================================================================
29 //function : XCAFDoc_MaterialTool
31 //=======================================================================
33 XCAFDoc_MaterialTool::XCAFDoc_MaterialTool()
38 //=======================================================================
41 //=======================================================================
43 Handle(XCAFDoc_MaterialTool) XCAFDoc_MaterialTool::Set(const TDF_Label& L)
45 Handle(XCAFDoc_MaterialTool) A;
46 if (!L.FindAttribute (XCAFDoc_MaterialTool::GetID(), A)) {
47 A = new XCAFDoc_MaterialTool ();
49 A->myShapeTool = XCAFDoc_DocumentTool::ShapeTool(L);
55 //=======================================================================
58 //=======================================================================
60 const Standard_GUID& XCAFDoc_MaterialTool::GetID()
62 static Standard_GUID MatTblID ("efd212f9-6dfd-11d4-b9c8-0060b0ee281b");
67 //=======================================================================
68 //function : BaseLabel
70 //=======================================================================
72 TDF_Label XCAFDoc_MaterialTool::BaseLabel() const
78 //=======================================================================
79 //function : ShapeTool
81 //=======================================================================
83 const Handle(XCAFDoc_ShapeTool)& XCAFDoc_MaterialTool::ShapeTool()
85 if(myShapeTool.IsNull())
86 myShapeTool = XCAFDoc_DocumentTool::ShapeTool(Label());
91 //=======================================================================
92 //function : IsMaterial
94 //=======================================================================
96 Standard_Boolean XCAFDoc_MaterialTool::IsMaterial(const TDF_Label& lab) const
98 Handle(XCAFDoc_Material) MatAttr;
99 if(lab.FindAttribute(XCAFDoc_Material::GetID(),MatAttr)) {
100 return Standard_True;
102 return Standard_False;
106 //=======================================================================
107 //function : GetMaterialLabels
109 //=======================================================================
111 void XCAFDoc_MaterialTool::GetMaterialLabels(TDF_LabelSequence& Labels) const
114 TDF_ChildIterator ChildIterator( Label() );
115 for (; ChildIterator.More(); ChildIterator.Next()) {
116 TDF_Label L = ChildIterator.Value();
117 if ( IsMaterial(L)) Labels.Append(L);
122 //=======================================================================
123 //function : AddMaterial
125 //=======================================================================
127 TDF_Label XCAFDoc_MaterialTool::AddMaterial(const Handle(TCollection_HAsciiString)& aName,
128 const Handle(TCollection_HAsciiString)& aDescription,
129 const Standard_Real aDensity,
130 const Handle(TCollection_HAsciiString)& aDensName,
131 const Handle(TCollection_HAsciiString)& aDensValType) const
135 MatL = aTag.NewChild ( Label() );
136 XCAFDoc_Material::Set(MatL,aName,aDescription,aDensity,aDensName,aDensValType);
137 TDataStd_Name::Set(MatL,TCollection_AsciiString(aName->ToCString()));
142 //=======================================================================
143 //function : SetMaterial
145 //=======================================================================
147 void XCAFDoc_MaterialTool::SetMaterial(const TDF_Label& L,const TDF_Label& MatL) const
150 Handle(TDataStd_TreeNode) refNode, mainNode;
151 mainNode = TDataStd_TreeNode::Set ( MatL, XCAFDoc::MaterialRefGUID() );
152 refNode = TDataStd_TreeNode::Set ( L, XCAFDoc::MaterialRefGUID() );
153 refNode->Remove(); // abv: fix against bug in TreeNode::Append()
154 mainNode->Append(refNode);
158 //=======================================================================
159 //function : SetMaterial
161 //=======================================================================
163 void XCAFDoc_MaterialTool::SetMaterial(const TDF_Label& L,
164 const Handle(TCollection_HAsciiString)& aName,
165 const Handle(TCollection_HAsciiString)& aDescription,
166 const Standard_Real aDensity,
167 const Handle(TCollection_HAsciiString)& aDensName,
168 const Handle(TCollection_HAsciiString)& aDensValType) const
170 TDF_Label MatL = AddMaterial(aName,aDescription,aDensity,aDensName,aDensValType);
175 //=======================================================================
176 //function : GetMaterial
178 //=======================================================================
180 Standard_Boolean XCAFDoc_MaterialTool::GetMaterial(const TDF_Label& MatL,
181 Handle(TCollection_HAsciiString)& aName,
182 Handle(TCollection_HAsciiString)& aDescription,
183 Standard_Real& aDensity,
184 Handle(TCollection_HAsciiString)& aDensName,
185 Handle(TCollection_HAsciiString)& aDensValType) const
187 Handle(XCAFDoc_Material) MatAttr;
188 if(!MatL.FindAttribute(XCAFDoc_Material::GetID(),MatAttr)) {
189 return Standard_False;
191 aName = MatAttr->GetName();
192 aDescription = MatAttr->GetDescription();
193 aDensity = MatAttr->GetDensity();
194 aDensName = MatAttr->GetDensName();
195 aDensValType = MatAttr->GetDensValType();
197 return Standard_True;
201 //=======================================================================
202 //function : GetDensityForShape
204 //=======================================================================
206 Standard_Real XCAFDoc_MaterialTool::GetDensityForShape(const TDF_Label& ShapeL)
208 Standard_Real Dens=0.0;
209 Handle(TDataStd_TreeNode) Node;
210 if ( ! ShapeL.FindAttribute ( XCAFDoc::MaterialRefGUID(), Node) ||
211 ! Node->HasFather() ) return Dens;
212 TDF_Label MatL = Node->Father()->Label();
213 Handle(XCAFDoc_Material) MatAttr;
214 if(!MatL.FindAttribute(XCAFDoc_Material::GetID(),MatAttr)) {
217 // defaul dimension fo density - gram/sm^3
218 // we transfer "sm" into "mm"
219 Dens = MatAttr->GetDensity()*0.001;
224 //=======================================================================
227 //=======================================================================
229 const Standard_GUID& XCAFDoc_MaterialTool::ID() const
235 //=======================================================================
238 //=======================================================================
240 void XCAFDoc_MaterialTool::Restore(const Handle(TDF_Attribute)& /*with*/)
245 //=======================================================================
246 //function : NewEmpty
248 //=======================================================================
250 Handle(TDF_Attribute) XCAFDoc_MaterialTool::NewEmpty() const
252 return new XCAFDoc_MaterialTool;
256 //=======================================================================
259 //=======================================================================
261 void XCAFDoc_MaterialTool::Paste(const Handle(TDF_Attribute)& /*into*/,
262 const Handle(TDF_RelocationTable)& /*RT*/) const