0026689: Necessary to improve STEPCAFControl_Reader to read GD&T data from STEP model...
[occt.git] / src / XCAFDoc / XCAFDoc_Datum.cxx
CommitLineData
973c2be1 1// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 2//
973c2be1 3// This file is part of Open CASCADE Technology software library.
b311480e 4//
d5f74e42 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
973c2be1 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.
b311480e 10//
973c2be1 11// Alternatively, this file may be used under the terms of Open CASCADE
12// commercial license or contractual agreement.
b311480e 13
7fd59977 14
42cf5bc1 15#include <Standard_GUID.hxx>
16#include <Standard_Type.hxx>
17#include <TCollection_HAsciiString.hxx>
18#include <TDF_Attribute.hxx>
19#include <TDF_Label.hxx>
1c9d3225 20#include <TDF_ChildIterator.hxx>
7fd59977 21#include <TDF_RelocationTable.hxx>
42cf5bc1 22#include <XCAFDoc_Datum.hxx>
9ebaae37 23#include <TDataStd_AsciiString.hxx>
24#include <TDataStd_IntegerArray.hxx>
1c9d3225 25#include <TDataStd_RealArray.hxx>
9ebaae37 26#include <TDataStd_Integer.hxx>
27#include <TDataStd_Real.hxx>
28#include <TNaming_NamedShape.hxx>
29#include <TNaming_Builder.hxx>
30
31
32#include <XCAFDimTolObjects_DatumObject.hxx>
33
34enum ChildLab
35{
36 ChildLab_Name = 1,
37 ChildLab_Modifiers,
38 ChildLab_ModifierWithValue,
1c9d3225 39 ChildLab_IsDTarget,
40 ChildLab_DTargetType,
41 ChildLab_AxisLoc,
42 ChildLab_AxisN,
43 ChildLab_AxisRef,
44 ChildLab_DTargetLength,
45 ChildLab_DTargetWidth,
46 ChildLab_DatumTarget,
9ebaae37 47};
7fd59977 48
49//=======================================================================
50//function : XCAFDoc_Datum
51//purpose :
52//=======================================================================
7fd59977 53XCAFDoc_Datum::XCAFDoc_Datum()
54{
55}
56
57
58//=======================================================================
59//function : GetID
60//purpose :
61//=======================================================================
62
63const Standard_GUID& XCAFDoc_Datum::GetID()
64{
65 static Standard_GUID DID("58ed092e-44de-11d8-8776-001083004c77");
66 //static Standard_GUID ID("efd212e2-6dfd-11d4-b9c8-0060b0ee281b");
67 return DID;
68 //return ID;
69}
70
71
72//=======================================================================
73//function : Set
74//purpose :
75//=======================================================================
76
9ebaae37 77Handle(XCAFDoc_Datum) XCAFDoc_Datum::Set(const TDF_Label& theLabel,
78 const Handle(TCollection_HAsciiString)& theName,
79 const Handle(TCollection_HAsciiString)& theDescription,
80 const Handle(TCollection_HAsciiString)& theIdentification)
81{
82 Handle(XCAFDoc_Datum) aDatum;
83 if (!theLabel.FindAttribute(XCAFDoc_Datum::GetID(), aDatum)) {
84 aDatum = new XCAFDoc_Datum();
85 theLabel.AddAttribute(aDatum);
86 }
87 aDatum->Set(theName,theDescription,theIdentification);
88 return aDatum;
89}
90
91//=======================================================================
92//function : Set
93//purpose :
94//=======================================================================
95
96Handle(XCAFDoc_Datum) XCAFDoc_Datum::Set(const TDF_Label& theLabel)
7fd59977 97{
9ebaae37 98 Handle(XCAFDoc_Datum) aDatum;
99 if (!theLabel.FindAttribute(XCAFDoc_Datum::GetID(), aDatum)) {
100 aDatum = new XCAFDoc_Datum();
101 theLabel.AddAttribute(aDatum);
7fd59977 102 }
9ebaae37 103 return aDatum;
7fd59977 104}
105
106
107//=======================================================================
108//function : Set
109//purpose :
110//=======================================================================
111
9ebaae37 112void XCAFDoc_Datum::Set(const Handle(TCollection_HAsciiString)& theName,
113 const Handle(TCollection_HAsciiString)& theDescription,
114 const Handle(TCollection_HAsciiString)& theIdentification)
7fd59977 115{
9ebaae37 116 Backup();
117 myName = theName;
118 myDescription = theDescription;
119 myIdentification = theIdentification;
7fd59977 120}
121
122
123//=======================================================================
124//function : GetName
125//purpose :
126//=======================================================================
127
128Handle(TCollection_HAsciiString) XCAFDoc_Datum::GetName() const
129{
9ebaae37 130 if(myName.IsNull())
131 return new TCollection_HAsciiString();
7fd59977 132 return myName;
133}
134
7fd59977 135//=======================================================================
9ebaae37 136//function : GetDescriptio7n
7fd59977 137//purpose :
138//=======================================================================
139
140Handle(TCollection_HAsciiString) XCAFDoc_Datum::GetDescription() const
141{
142 return myDescription;
143}
144
145
146//=======================================================================
147//function : GetIdentification
148//purpose :
149//=======================================================================
150
151Handle(TCollection_HAsciiString) XCAFDoc_Datum::GetIdentification() const
152{
153 return myIdentification;
154}
155
9ebaae37 156//=======================================================================
157//function : SetObject
158//purpose :
159//=======================================================================
160
161void XCAFDoc_Datum::SetObject(const Handle(XCAFDimTolObjects_DatumObject)& theObject)
162{
163 Backup();
1c9d3225 164 TDF_ChildIterator anIter(Label());
165 for(;anIter.More(); anIter.Next())
166 {
167 anIter.Value().ForgetAllAttributes();
168 }
169 if (!theObject->GetName().IsNull() && !theObject->GetName()->IsEmpty())
9ebaae37 170 {
171 Handle(TDataStd_AsciiString) anAttName;
172 if(!Label().FindChild(ChildLab_Name).FindAttribute(TDataStd_AsciiString::GetID(), anAttName))
173 {
174 anAttName = new TDataStd_AsciiString();
175 Label().FindChild(ChildLab_Name).AddAttribute(anAttName);
176 }
177 anAttName->Set(theObject->GetName()->String());
178 }
9ebaae37 179
1c9d3225 180 if(theObject->GetModifiers().Length() > 0)
9ebaae37 181 {
182 Handle(TDataStd_IntegerArray) aModifiers;
183 if(!Label().FindChild(ChildLab_Modifiers).FindAttribute(TDataStd_IntegerArray::GetID(), aModifiers))
184 {
185 aModifiers = new TDataStd_IntegerArray();
186 Label().FindChild(ChildLab_Modifiers).AddAttribute(aModifiers);
187 }
188 Handle(TColStd_HArray1OfInteger) anArr = new TColStd_HArray1OfInteger(1,theObject->GetModifiers().Length());
189 for(Standard_Integer i = 1; i <= theObject->GetModifiers().Length(); i++)
190 anArr->SetValue(i,theObject->GetModifiers().Value(i));
191 aModifiers->ChangeArray(anArr);
192 }
193
194 XCAFDimTolObjects_DatumModifWithValue aM;
195 Standard_Real aV;
196 theObject->GetModifierWithValue(aM, aV);
197 if(aM != XCAFDimTolObjects_DatumModifWithValue_None)
198 {
199 Handle(TDataStd_Integer) aModifierWithValueM;
200 if(!Label().FindChild(ChildLab_ModifierWithValue).FindAttribute(TDataStd_Integer::GetID(), aModifierWithValueM))
201 {
202 aModifierWithValueM = new TDataStd_Integer();
203 Label().FindChild(ChildLab_ModifierWithValue).AddAttribute(aModifierWithValueM);
204 }
205 Handle(TDataStd_Real) aModifierWithValueV;
206 if(!Label().FindChild(ChildLab_ModifierWithValue).FindAttribute(TDataStd_Real::GetID(), aModifierWithValueV))
207 {
208 aModifierWithValueV = new TDataStd_Real();
209 Label().FindChild(ChildLab_ModifierWithValue).AddAttribute(aModifierWithValueV);
210 }
211 aModifierWithValueM->Set(aM);
212 aModifierWithValueV->Set(aV);
213 }
9ebaae37 214
1c9d3225 215 Handle(TDataStd_Integer) aIsTarget = new TDataStd_Integer();
216 aIsTarget->Set(theObject->IsDatumTarget());
217 Label().FindChild(ChildLab_IsDTarget).AddAttribute(aIsTarget);
218
219 if(theObject->IsDatumTarget())
9ebaae37 220 {
1c9d3225 221 Handle(TDataStd_Integer) aType = new TDataStd_Integer();
222 aType->Set(theObject->GetDatumTargetType());
223 Label().FindChild(ChildLab_DTargetType).AddAttribute(aType);
224
225 if(theObject->GetDatumTargetType() == XCAFDimTolObjects_DatumTargetType_Area)
226 {
227 if(!theObject->GetDatumTarget().IsNull())
228 {
229 TNaming_Builder tnBuild(Label().FindChild(ChildLab_DatumTarget));
230 tnBuild.Generated(theObject->GetDatumTarget());
231 }
232 }
233 else
234 {
235 Handle(TDataStd_RealArray) aLoc = new TDataStd_RealArray();
236 Handle(TDataStd_RealArray) aN = new TDataStd_RealArray();
237 Handle(TDataStd_RealArray) aR = new TDataStd_RealArray();
238 gp_Ax2 anAx = theObject->GetDatumTargetAxis();
239 aLoc->SetValue(aLoc->Upper(),anAx.Location().X());
240 aLoc->SetValue(aLoc->Upper()+1,anAx.Location().Y());
241 aLoc->SetValue(aLoc->Upper()+2,anAx.Location().Z());
242
243 aN->SetValue(aN->Upper(),anAx.Axis().Direction().X());
244 aN->SetValue(aN->Upper(),anAx.Axis().Direction().X());
245 aN->SetValue(aN->Upper(),anAx.Axis().Direction().X());
246
247 aR->SetValue(aR->Upper(),anAx.Direction().X());
248 aR->SetValue(aR->Upper(),anAx.Direction().X());
249 aR->SetValue(aR->Upper(),anAx.Direction().X());
250
251 Label().FindChild(ChildLab_AxisLoc).AddAttribute(aLoc);
252 Label().FindChild(ChildLab_AxisN).AddAttribute(aN);
253 Label().FindChild(ChildLab_AxisRef).AddAttribute(aR);
254
255 if(theObject->GetDatumTargetType() != XCAFDimTolObjects_DatumTargetType_Point)
256 {
257 Handle(TDataStd_Real) aLen = new TDataStd_Real();
258 aLen->Set(theObject->GetDatumTargetLength());
259 Label().FindChild(ChildLab_DTargetLength).AddAttribute(aLen);
260 if(theObject->GetDatumTargetType() == XCAFDimTolObjects_DatumTargetType_Rectangle)
261 {
262 Handle(TDataStd_Real) aWidth = new TDataStd_Real();
263 aWidth->Set(theObject->GetDatumTargetWidth());
264 Label().FindChild(ChildLab_DTargetWidth).AddAttribute(aWidth);
265 }
266 }
267 }
9ebaae37 268 }
269}
270
271//=======================================================================
272//function : GetObject
273//purpose :
274//=======================================================================
275
276Handle(XCAFDimTolObjects_DatumObject) XCAFDoc_Datum::GetObject() const
277{
278 Handle(XCAFDimTolObjects_DatumObject) anObj = new XCAFDimTolObjects_DatumObject();
279
280 Handle(TDataStd_AsciiString) anAttName;
281 if(Label().FindChild(ChildLab_Name).FindAttribute(TDataStd_AsciiString::GetID(), anAttName))
282 {
283 Handle(TCollection_HAsciiString) aStr = new TCollection_HAsciiString(anAttName->Get());
284 anObj->SetName(aStr);
285 }
286
287 Handle(TDataStd_IntegerArray) anArr;
288 if(Label().FindChild(ChildLab_Modifiers).FindAttribute(TDataStd_IntegerArray::GetID(), anArr)
289 && !anArr->Array().IsNull())
290 {
291 XCAFDimTolObjects_DatumModifiersSequence aModifiers;
292 for(Standard_Integer i = 1; i <= anArr->Length(); i++)
293 aModifiers.Append((XCAFDimTolObjects_DatumSingleModif)anArr->Value(i));
294 anObj->SetModifiers(aModifiers);
295 }
296
297 Handle(TDataStd_Integer) aModifierWithValueM;
298 if(Label().FindChild(ChildLab_ModifierWithValue).FindAttribute(TDataStd_Integer::GetID(), aModifierWithValueM))
299 {
300 Handle(TDataStd_Real) aModifierWithValueV;
301 if(Label().FindChild(ChildLab_ModifierWithValue).FindAttribute(TDataStd_Real::GetID(), aModifierWithValueV))
302 {
303 anObj->SetModifierWithValue((XCAFDimTolObjects_DatumModifWithValue)aModifierWithValueM->Get(),aModifierWithValueV->Get());
304 }
305 }
306
1c9d3225 307 Handle(TDataStd_Integer) aIsDTarget;
308 if(Label().FindChild(ChildLab_IsDTarget).FindAttribute(TDataStd_Integer::GetID(), aIsDTarget))
309 {
310 anObj->IsDatumTarget((aIsDTarget->Get() != 0));
311 }
312 else
313 {
314 return anObj;
315 }
316
317 if (aIsDTarget->Get() != 0)
9ebaae37 318 {
1c9d3225 319 Handle(TDataStd_Integer) aDTargetType;
320 if(Label().FindChild(ChildLab_DTargetType).FindAttribute(TDataStd_Integer::GetID(), aDTargetType))
321 {
322 anObj->SetDatumTargetType((XCAFDimTolObjects_DatumTargetType)aDTargetType->Get());
323 if(anObj->GetDatumTargetType() == XCAFDimTolObjects_DatumTargetType_Area)
324 {
325 Handle(TNaming_NamedShape) aDatumTarget;
326 if(Label().FindChild(ChildLab_DatumTarget).FindAttribute(TNaming_NamedShape::GetID(), aDatumTarget))
327 {
328 anObj->SetDatumTarget(aDatumTarget->Get());
329 }
330 }
331 else
332 {
333 Handle(TDataStd_RealArray) aLoc;
334 Handle(TDataStd_RealArray) aN;
335 Handle(TDataStd_RealArray) aR;
336 if(Label().FindChild(ChildLab_AxisLoc).FindAttribute(TDataStd_RealArray::GetID(), aLoc) && aLoc->Length() == 3 &&
337 Label().FindChild(ChildLab_AxisN).FindAttribute(TDataStd_RealArray::GetID(), aN) && aN->Length() == 3 &&
338 Label().FindChild(ChildLab_AxisRef).FindAttribute(TDataStd_RealArray::GetID(), aR) && aR->Length() == 3 )
339 {
340 gp_Pnt aL(aLoc->Value(aLoc->Upper()), aLoc->Value(aLoc->Upper()+1), aLoc->Value(aLoc->Upper()+2));
341 gp_Dir aD(aN->Value(aN->Upper()), aN->Value(aN->Upper()+1), aN->Value(aN->Upper()+2));
342 gp_Dir aDR(aR->Value(aR->Upper()), aR->Value(aR->Upper()+1), aR->Value(aR->Upper()+2));
343 gp_Ax2 anAx(aL, aD, aDR);
344 anObj->SetDatumTargetAxis(anAx);
345 }
346
347 if(anObj->GetDatumTargetType() != XCAFDimTolObjects_DatumTargetType_Point)
348 {
349 Handle(TDataStd_Real) aLen;
350 if(Label().FindChild(ChildLab_DTargetLength).FindAttribute(TDataStd_Integer::GetID(), aLen))
351 {
352 anObj->SetDatumTargetLength(aLen->Get());
353 }
354 if(anObj->GetDatumTargetType() == XCAFDimTolObjects_DatumTargetType_Rectangle)
355 {
356 Handle(TDataStd_Real) aWidth;
357 if(Label().FindChild(ChildLab_DTargetWidth).FindAttribute(TDataStd_Integer::GetID(), aWidth))
358 {
359 anObj->SetDatumTargetWidth(aWidth->Get());
360 }
361 }
362 }
363 }
364 }
9ebaae37 365 }
366
367 return anObj;
368}
7fd59977 369
370//=======================================================================
371//function : ID
372//purpose :
373//=======================================================================
374
375const Standard_GUID& XCAFDoc_Datum::ID() const
376{
377 return GetID();
378}
379
380
381//=======================================================================
382//function : Restore
383//purpose :
384//=======================================================================
385
9ebaae37 386void XCAFDoc_Datum::Restore(const Handle(TDF_Attribute)& theWith)
7fd59977 387{
9ebaae37 388 myName = Handle(XCAFDoc_Datum)::DownCast(theWith)->GetName();
389 myDescription = Handle(XCAFDoc_Datum)::DownCast(theWith)->GetDescription();
390 myIdentification = Handle(XCAFDoc_Datum)::DownCast(theWith)->GetIdentification();
7fd59977 391}
392
393
394//=======================================================================
395//function : NewEmpty
396//purpose :
397//=======================================================================
398
399Handle(TDF_Attribute) XCAFDoc_Datum::NewEmpty() const
400{
401 return new XCAFDoc_Datum();
402}
403
404
405//=======================================================================
406//function : Paste
407//purpose :
408//=======================================================================
409
9ebaae37 410void XCAFDoc_Datum::Paste(const Handle(TDF_Attribute)& theInto,
7fd59977 411 const Handle(TDF_RelocationTable)& /*RT*/) const
412{
9ebaae37 413 Handle(XCAFDoc_Datum)::DownCast(theInto)->Set(myName,myDescription,myIdentification);
7fd59977 414}
415