summaryrefslogtreecommitdiff
path: root/src/XCAFDoc/XCAFDoc_MaterialTool.cxx
blob: e45ab019b30cd8d74aa5d39ae5bf35e41375f4be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include <XCAFDoc_MaterialTool.ixx>

#include <XCAFDoc.hxx>
#include <XCAFDoc_Material.hxx>
#include <XCAFDoc_DocumentTool.hxx>
#include <TDataStd_TreeNode.hxx>
#include <TDataStd_Name.hxx>
#include <TDF_ChildIDIterator.hxx>


//=======================================================================
//function : XCAFDoc_MaterialTool
//purpose  : 
//=======================================================================

XCAFDoc_MaterialTool::XCAFDoc_MaterialTool()
{
}


//=======================================================================
//function : Set
//purpose  : 
//=======================================================================

Handle(XCAFDoc_MaterialTool) XCAFDoc_MaterialTool::Set(const TDF_Label& L) 
{
  Handle(XCAFDoc_MaterialTool) A;
  if (!L.FindAttribute (XCAFDoc_MaterialTool::GetID(), A)) {
    A = new XCAFDoc_MaterialTool ();
    L.AddAttribute(A);
    A->myShapeTool = XCAFDoc_DocumentTool::ShapeTool(L);
  }
  return A;
}


//=======================================================================
//function : GetID
//purpose  : 
//=======================================================================

const Standard_GUID& XCAFDoc_MaterialTool::GetID() 
{
  static Standard_GUID MatTblID ("efd212f9-6dfd-11d4-b9c8-0060b0ee281b");
  return MatTblID; 
}


//=======================================================================
//function : BaseLabel
//purpose  : 
//=======================================================================

TDF_Label XCAFDoc_MaterialTool::BaseLabel() const
{
  return Label();
}


//=======================================================================
//function : ShapeTool
//purpose  : 
//=======================================================================

const Handle(XCAFDoc_ShapeTool)& XCAFDoc_MaterialTool::ShapeTool() 
{
  if(myShapeTool.IsNull())
    myShapeTool = XCAFDoc_DocumentTool::ShapeTool(Label());
  return myShapeTool;
}


//=======================================================================
//function : IsMaterial
//purpose  : 
//=======================================================================

Standard_Boolean XCAFDoc_MaterialTool::IsMaterial(const TDF_Label& lab) const
{
  Handle(XCAFDoc_Material) MatAttr;
  if(lab.FindAttribute(XCAFDoc_Material::GetID(),MatAttr)) {
    return Standard_True;
  }
  return Standard_False;
}


//=======================================================================
//function : GetMaterialLabels
//purpose  : 
//=======================================================================

void XCAFDoc_MaterialTool::GetMaterialLabels(TDF_LabelSequence& Labels) const
{
  Labels.Clear();
  TDF_ChildIterator ChildIterator( Label() ); 
  for (; ChildIterator.More(); ChildIterator.Next()) {
    TDF_Label L = ChildIterator.Value();
    if ( IsMaterial(L)) Labels.Append(L);
  }
}


//=======================================================================
//function : AddMaterial
//purpose  : 
//=======================================================================

TDF_Label XCAFDoc_MaterialTool::AddMaterial(const Handle(TCollection_HAsciiString)& aName,
                                            const Handle(TCollection_HAsciiString)& aDescription,
                                            const Standard_Real aDensity,
                                            const Handle(TCollection_HAsciiString)& aDensName,
                                            const Handle(TCollection_HAsciiString)& aDensValType) const
{
  TDF_Label MatL;
  TDF_TagSource aTag;
  MatL = aTag.NewChild ( Label() );
  XCAFDoc_Material::Set(MatL,aName,aDescription,aDensity,aDensName,aDensValType);
  TDataStd_Name::Set(MatL,TCollection_AsciiString(aName->ToCString()));
  return MatL;
}


//=======================================================================
//function : SetMaterial
//purpose  : 
//=======================================================================

void XCAFDoc_MaterialTool::SetMaterial(const TDF_Label& L,const TDF_Label& MatL) const
{
  // set reference
  Handle(TDataStd_TreeNode) refNode, mainNode;
  mainNode = TDataStd_TreeNode::Set ( MatL, XCAFDoc::MaterialRefGUID() );
  refNode  = TDataStd_TreeNode::Set ( L,       XCAFDoc::MaterialRefGUID() );
  refNode->Remove(); // abv: fix against bug in TreeNode::Append()
  mainNode->Append(refNode);
}


//=======================================================================
//function : SetMaterial
//purpose  : 
//=======================================================================

void XCAFDoc_MaterialTool::SetMaterial(const TDF_Label& L,
                                       const Handle(TCollection_HAsciiString)& aName,
                                       const Handle(TCollection_HAsciiString)& aDescription,
                                       const Standard_Real aDensity,
                                       const Handle(TCollection_HAsciiString)& aDensName,
                                       const Handle(TCollection_HAsciiString)& aDensValType) const
{
  TDF_Label MatL = AddMaterial(aName,aDescription,aDensity,aDensName,aDensValType);
  SetMaterial(L,MatL);
}


//=======================================================================
//function : GetMaterial
//purpose  : 
//=======================================================================

Standard_Boolean XCAFDoc_MaterialTool::GetMaterial(const TDF_Label& MatL,
                                                   Handle(TCollection_HAsciiString)& aName,
                                                   Handle(TCollection_HAsciiString)& aDescription,
                                                   Standard_Real& aDensity,
                                                   Handle(TCollection_HAsciiString)& aDensName,
                                                   Handle(TCollection_HAsciiString)& aDensValType) const
{
  Handle(XCAFDoc_Material) MatAttr;
  if(!MatL.FindAttribute(XCAFDoc_Material::GetID(),MatAttr)) {
    return Standard_False;
  }
  aName = MatAttr->GetName();
  aDescription = MatAttr->GetDescription();
  aDensity = MatAttr->GetDensity();
  aDensName = MatAttr->GetDensName();
  aDensValType = MatAttr->GetDensValType();
  
  return Standard_True;
}


//=======================================================================
//function : GetDensityForShape
//purpose  : 
//=======================================================================

Standard_Real XCAFDoc_MaterialTool::GetDensityForShape(const TDF_Label& ShapeL)
{
  Standard_Real Dens=0.0;
  Handle(TDataStd_TreeNode) Node;
  if ( ! ShapeL.FindAttribute ( XCAFDoc::MaterialRefGUID(), Node) ||
       ! Node->HasFather() ) return Dens;
  TDF_Label MatL = Node->Father()->Label();
  Handle(XCAFDoc_Material) MatAttr;
  if(!MatL.FindAttribute(XCAFDoc_Material::GetID(),MatAttr)) {
    return Dens;
  }
  // defaul dimension fo density - gram/sm^3
  // we transfer "sm" into "mm"
  Dens = MatAttr->GetDensity()*0.001; 
  return Dens;
}


//=======================================================================
//function : ID
//purpose  : 
//=======================================================================

const Standard_GUID& XCAFDoc_MaterialTool::ID() const
{
  return GetID();
}


//=======================================================================
//function : Restore
//purpose  : 
//=======================================================================

void XCAFDoc_MaterialTool::Restore(const Handle(TDF_Attribute)& /*with*/) 
{
}


//=======================================================================
//function : NewEmpty
//purpose  : 
//=======================================================================

Handle(TDF_Attribute) XCAFDoc_MaterialTool::NewEmpty() const
{
  return new XCAFDoc_MaterialTool;
}


//=======================================================================
//function : Paste
//purpose  : 
//=======================================================================

void XCAFDoc_MaterialTool::Paste(const Handle(TDF_Attribute)& /*into*/,
                                 const Handle(TDF_RelocationTable)& /*RT*/) const
{
}