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