a62e8a12bf56a3648c5a2eca74c9dfc9ef079c4a
[occt.git] / src / XCAFDoc / XCAFDoc_MaterialTool.cxx
1 // Copyright (c) 1999-2014 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
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>
29
30 IMPLEMENT_STANDARD_RTTIEXT(XCAFDoc_MaterialTool,TDF_Attribute)
31
32 //=======================================================================
33 //function : XCAFDoc_MaterialTool
34 //purpose  : 
35 //=======================================================================
36 XCAFDoc_MaterialTool::XCAFDoc_MaterialTool()
37 {
38 }
39
40
41 //=======================================================================
42 //function : Set
43 //purpose  : 
44 //=======================================================================
45
46 Handle(XCAFDoc_MaterialTool) XCAFDoc_MaterialTool::Set(const TDF_Label& L) 
47 {
48   Handle(XCAFDoc_MaterialTool) A;
49   if (!L.FindAttribute (XCAFDoc_MaterialTool::GetID(), A)) {
50     A = new XCAFDoc_MaterialTool ();
51     L.AddAttribute(A);
52     A->myShapeTool = XCAFDoc_DocumentTool::ShapeTool(L);
53   }
54   return A;
55 }
56
57
58 //=======================================================================
59 //function : GetID
60 //purpose  : 
61 //=======================================================================
62
63 const Standard_GUID& XCAFDoc_MaterialTool::GetID() 
64 {
65   static Standard_GUID MatTblID ("efd212f9-6dfd-11d4-b9c8-0060b0ee281b");
66   return MatTblID; 
67 }
68
69
70 //=======================================================================
71 //function : BaseLabel
72 //purpose  : 
73 //=======================================================================
74
75 TDF_Label XCAFDoc_MaterialTool::BaseLabel() const
76 {
77   return Label();
78 }
79
80
81 //=======================================================================
82 //function : ShapeTool
83 //purpose  : 
84 //=======================================================================
85
86 const Handle(XCAFDoc_ShapeTool)& XCAFDoc_MaterialTool::ShapeTool() 
87 {
88   if(myShapeTool.IsNull())
89     myShapeTool = XCAFDoc_DocumentTool::ShapeTool(Label());
90   return myShapeTool;
91 }
92
93
94 //=======================================================================
95 //function : IsMaterial
96 //purpose  : 
97 //=======================================================================
98
99 Standard_Boolean XCAFDoc_MaterialTool::IsMaterial(const TDF_Label& lab) const
100 {
101   Handle(XCAFDoc_Material) MatAttr;
102   if(lab.FindAttribute(XCAFDoc_Material::GetID(),MatAttr)) {
103     return Standard_True;
104   }
105   return Standard_False;
106 }
107
108
109 //=======================================================================
110 //function : GetMaterialLabels
111 //purpose  : 
112 //=======================================================================
113
114 void XCAFDoc_MaterialTool::GetMaterialLabels(TDF_LabelSequence& Labels) const
115 {
116   Labels.Clear();
117   TDF_ChildIterator ChildIterator( Label() ); 
118   for (; ChildIterator.More(); ChildIterator.Next()) {
119     TDF_Label L = ChildIterator.Value();
120     if ( IsMaterial(L)) Labels.Append(L);
121   }
122 }
123
124
125 //=======================================================================
126 //function : AddMaterial
127 //purpose  : 
128 //=======================================================================
129
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
135 {
136   TDF_Label MatL;
137   TDF_TagSource aTag;
138   MatL = aTag.NewChild ( Label() );
139   XCAFDoc_Material::Set(MatL,aName,aDescription,aDensity,aDensName,aDensValType);
140   TDataStd_Name::Set(MatL,TCollection_AsciiString(aName->ToCString()));
141   return MatL;
142 }
143
144
145 //=======================================================================
146 //function : SetMaterial
147 //purpose  : 
148 //=======================================================================
149
150 void XCAFDoc_MaterialTool::SetMaterial(const TDF_Label& L,const TDF_Label& MatL) const
151 {
152   // set reference
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);
158 }
159
160
161 //=======================================================================
162 //function : SetMaterial
163 //purpose  : 
164 //=======================================================================
165
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
172 {
173   TDF_Label MatL = AddMaterial(aName,aDescription,aDensity,aDensName,aDensValType);
174   SetMaterial(L,MatL);
175 }
176
177
178 //=======================================================================
179 //function : GetMaterial
180 //purpose  : 
181 //=======================================================================
182
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
189 {
190   Handle(XCAFDoc_Material) MatAttr;
191   if(!MatL.FindAttribute(XCAFDoc_Material::GetID(),MatAttr)) {
192     return Standard_False;
193   }
194   aName = MatAttr->GetName();
195   aDescription = MatAttr->GetDescription();
196   aDensity = MatAttr->GetDensity();
197   aDensName = MatAttr->GetDensName();
198   aDensValType = MatAttr->GetDensValType();
199   
200   return Standard_True;
201 }
202
203
204 //=======================================================================
205 //function : GetDensityForShape
206 //purpose  : 
207 //=======================================================================
208
209 Standard_Real XCAFDoc_MaterialTool::GetDensityForShape(const TDF_Label& ShapeL)
210 {
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)) {
218     return Dens;
219   }
220   // defaul dimension fo density - gram/sm^3
221   // we transfer "sm" into "mm"
222   Dens = MatAttr->GetDensity()*0.001; 
223   return Dens;
224 }
225
226
227 //=======================================================================
228 //function : ID
229 //purpose  : 
230 //=======================================================================
231
232 const Standard_GUID& XCAFDoc_MaterialTool::ID() const
233 {
234   return GetID();
235 }
236
237
238 //=======================================================================
239 //function : Restore
240 //purpose  : 
241 //=======================================================================
242
243 void XCAFDoc_MaterialTool::Restore(const Handle(TDF_Attribute)& /*with*/) 
244 {
245 }
246
247
248 //=======================================================================
249 //function : NewEmpty
250 //purpose  : 
251 //=======================================================================
252
253 Handle(TDF_Attribute) XCAFDoc_MaterialTool::NewEmpty() const
254 {
255   return new XCAFDoc_MaterialTool;
256 }
257
258
259 //=======================================================================
260 //function : Paste
261 //purpose  : 
262 //=======================================================================
263
264 void XCAFDoc_MaterialTool::Paste(const Handle(TDF_Attribute)& /*into*/,
265                                  const Handle(TDF_RelocationTable)& /*RT*/) const
266 {
267 }
268