96376fdf788c654f2c73c3a9b4e570e1533ec0ee
[occt.git] / src / XCAFDoc / XCAFDoc_GeomTolerance.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 #include <XCAFDoc_GeomTolerance.hxx>
15
16 #include <TDF_RelocationTable.hxx>
17 #include <TDF_ChildIterator.hxx>
18 #include <XCAFDoc.hxx>
19 #include <TDataStd_TreeNode.hxx>
20 #include <TDataStd_Integer.hxx>
21 #include <TDataStd_IntegerArray.hxx>
22 #include <TDataStd_RealArray.hxx>
23 #include <TDataStd_Real.hxx>
24 #include <TDataXtd_Geometry.hxx>
25 #include <TDataXtd_Plane.hxx>
26 #include <TDataXtd_Point.hxx>
27 #include <XCAFDimTolObjects_GeomToleranceObject.hxx>
28 #include <TNaming_Tool.hxx>
29 #include <TNaming_Builder.hxx>
30 #include <TCollection_ExtendedString.hxx>
31 #include <TDataStd_Name.hxx>
32
33 IMPLEMENT_STANDARD_RTTIEXT(XCAFDoc_GeomTolerance,TDF_Attribute)
34
35 enum ChildLab
36 {
37   ChildLab_Type = 1,
38   ChildLab_TypeOfValue,
39   ChildLab_Value,
40   ChildLab_MatReqModif,
41   ChildLab_ZoneModif,
42   ChildLab_ValueOfZoneModif,
43   ChildLab_Modifiers,
44   ChildLab_aMaxValueModif,
45   ChildLab_Axis,
46   ChildLab_Plane,
47   ChildLab_Pnt,
48   ChildLab_PntText,
49   ChildLab_Presentation,
50   ChildLab_AffectedPlane
51 };
52
53 //=======================================================================
54 //function : XCAFDoc_GeomTolerance
55 //purpose  : 
56 //=======================================================================
57
58 XCAFDoc_GeomTolerance::XCAFDoc_GeomTolerance()
59 {
60 }
61
62
63 //=======================================================================
64 //function : GetID
65 //purpose  : 
66 //=======================================================================
67
68 const Standard_GUID& XCAFDoc_GeomTolerance::GetID() 
69 {
70   static Standard_GUID DGTID ("58ed092f-44de-11d8-8776-001083004c77");
71   //static Standard_GUID ID("efd212e9-6dfd-11d4-b9c8-0060b0ee281b");
72   return DGTID; 
73   //return ID;
74 }
75
76 //=======================================================================
77 //function :
78 //purpose  : 
79 //=======================================================================
80
81 Handle(XCAFDoc_GeomTolerance) XCAFDoc_GeomTolerance::Set (const TDF_Label& theLabel)
82 {
83   Handle(XCAFDoc_GeomTolerance) A;
84   if (!theLabel.FindAttribute(XCAFDoc_GeomTolerance::GetID(), A)) {
85     A = new XCAFDoc_GeomTolerance();
86     theLabel.AddAttribute(A);
87   }
88   return A;
89 }
90
91 //=======================================================================
92 //function : SetObject
93 //purpose  : 
94 //=======================================================================
95
96 void XCAFDoc_GeomTolerance::SetObject (const Handle(XCAFDimTolObjects_GeomToleranceObject)& theObject) 
97 {
98   Backup();
99
100   if (theObject->GetSemanticName())
101   {
102     TCollection_ExtendedString str(theObject->GetSemanticName()->String());
103     TDataStd_Name::Set(Label(), str);
104   }
105
106   TDF_ChildIterator anIter(Label());
107   for(;anIter.More(); anIter.Next())
108   {
109     anIter.Value().ForgetAllAttributes();
110   }
111
112   Handle(TDataStd_Integer) aType = TDataStd_Integer::Set(Label().FindChild(ChildLab_Type), theObject->GetType());
113
114   if(theObject->GetTypeOfValue() != XCAFDimTolObjects_GeomToleranceTypeValue_None)
115     Handle(TDataStd_Integer) aTypeOfValue = TDataStd_Integer::Set(Label().FindChild(ChildLab_TypeOfValue), 
116                                             theObject->GetTypeOfValue());
117
118   Handle(TDataStd_Real) aValue = TDataStd_Real::Set(Label().FindChild(ChildLab_Value), theObject->GetValue());
119
120   Handle(TDataStd_Integer) aMatReqModif;
121   if(theObject->GetMaterialRequirementModifier() != XCAFDimTolObjects_GeomToleranceMatReqModif_None)
122     aMatReqModif = TDataStd_Integer::Set(Label().FindChild(ChildLab_MatReqModif), 
123                    theObject->GetMaterialRequirementModifier());
124
125   if(theObject->GetZoneModifier() != XCAFDimTolObjects_GeomToleranceZoneModif_None)
126     Handle(TDataStd_Integer) aZoneModif = TDataStd_Integer::Set(Label().FindChild(ChildLab_ZoneModif), 
127                                           theObject->GetZoneModifier());
128   
129   if(theObject->GetValueOfZoneModifier() > 0)
130     Handle(TDataStd_Real) aValueOfZoneModif = TDataStd_Real::Set(Label().FindChild(ChildLab_ValueOfZoneModif),
131                                               theObject->GetValueOfZoneModifier());
132
133   if(theObject->GetModifiers().Length() > 0)
134   {
135     Handle(TColStd_HArray1OfInteger) anArr = new TColStd_HArray1OfInteger(1,theObject->GetModifiers().Length());
136     for(Standard_Integer i = 1; i <= theObject->GetModifiers().Length(); i++)
137       anArr->SetValue(i,theObject->GetModifiers().Value(i));
138     Handle(TDataStd_IntegerArray) aModifiers = TDataStd_IntegerArray::Set(Label().FindChild(ChildLab_Modifiers), 
139                                                1, theObject->GetModifiers().Length());
140     if(!aModifiers.IsNull())
141       aModifiers->ChangeArray(anArr);
142   }
143
144   if(theObject->GetMaxValueModifier() > 0)
145     Handle(TDataStd_Real) aMaxValueModif = TDataStd_Real::Set(Label().FindChild(ChildLab_aMaxValueModif),
146                                            theObject->GetMaxValueModifier());
147
148   if (theObject->HasAxis())
149   {
150     TDataXtd_Plane::Set(Label().FindChild(ChildLab_Axis), gp_Pln(gp_Ax3(theObject->GetPlane())));
151   }
152
153   if (theObject->HasPlane())
154   {
155     TDataXtd_Plane::Set(Label().FindChild(ChildLab_Plane), gp_Pln(gp_Ax3(theObject->GetPlane())));
156   }
157
158   if (theObject->HasPoint())
159   {
160     TDataXtd_Point::Set(Label().FindChild(ChildLab_Pnt), theObject->GetPoint());
161   }
162
163   if (theObject->HasPointText())
164   {
165     TDataXtd_Point::Set(Label().FindChild(ChildLab_PntText), theObject->GetPointTextAttach());
166   }
167
168   TopoDS_Shape aPresentation = theObject->GetPresentation();
169   if( !aPresentation.IsNull())
170   {
171     TDF_Label aLPres = Label().FindChild( ChildLab_Presentation);
172     TNaming_Builder tnBuild(aLPres);
173     tnBuild.Generated(aPresentation);
174     Handle(TCollection_HAsciiString) aName =  theObject->GetPresentationName();
175     if( !aName.IsNull() )
176     {
177       TCollection_ExtendedString str ( aName->String() );
178       TDataStd_Name::Set ( aLPres, str );
179     }
180   }
181
182   if (theObject->HasAffectedPlane())
183   {
184     TDF_Label aLAffectedPlane = Label().FindChild(ChildLab_AffectedPlane);
185     TDataStd_Integer::Set(aLAffectedPlane, (Standard_Integer)theObject->GetAffectedPlaneType());
186     TDataXtd_Plane::Set(aLAffectedPlane, theObject->GetAffectedPlane());
187   }
188 }
189
190 //=======================================================================
191 //function :GetObject
192 //purpose  : 
193 //=======================================================================
194
195 Handle(XCAFDimTolObjects_GeomToleranceObject) XCAFDoc_GeomTolerance::GetObject()  const
196 {
197   Handle(XCAFDimTolObjects_GeomToleranceObject) anObj = new XCAFDimTolObjects_GeomToleranceObject();
198
199   Handle(TDataStd_Name) aSemanticNameAttr;
200   Handle(TCollection_HAsciiString) aSemanticName;
201   if (Label().FindAttribute(TDataStd_Name::GetID(), aSemanticNameAttr))
202   {
203     const TCollection_ExtendedString& aName = aSemanticNameAttr->Get();
204     if (!aName.IsEmpty())
205       aSemanticName = new TCollection_HAsciiString(aName);
206   }
207   anObj->SetSemanticName(aSemanticName);
208
209   Handle(TDataStd_Integer) aType;
210   if(Label().FindChild(ChildLab_Type).FindAttribute(TDataStd_Integer::GetID(), aType))
211   {
212     anObj->SetType((XCAFDimTolObjects_GeomToleranceType)aType->Get());
213   }
214
215   Handle(TDataStd_Integer) aTypeOfValue;
216   if(Label().FindChild(ChildLab_TypeOfValue).FindAttribute(TDataStd_Integer::GetID(), aTypeOfValue))
217   {
218     anObj->SetTypeOfValue((XCAFDimTolObjects_GeomToleranceTypeValue)aTypeOfValue->Get());
219   }
220
221   Handle(TDataStd_Real) aValue;
222   if(Label().FindChild(ChildLab_Value).FindAttribute(TDataStd_Real::GetID(), aValue))
223   {
224     anObj->SetValue(aValue->Get());
225   }
226
227   Handle(TDataStd_Integer) aMatReqModif;
228   if(Label().FindChild(ChildLab_MatReqModif).FindAttribute(TDataStd_Integer::GetID(), aMatReqModif))
229   {
230     anObj->SetMaterialRequirementModifier((XCAFDimTolObjects_GeomToleranceMatReqModif)aMatReqModif->Get());
231   }
232
233   Handle(TDataStd_Integer) aZoneModif;
234   if(Label().FindChild(ChildLab_ZoneModif).FindAttribute(TDataStd_Integer::GetID(), aZoneModif))
235   {
236     anObj->SetZoneModifier((XCAFDimTolObjects_GeomToleranceZoneModif)aZoneModif->Get());
237   }
238
239   Handle(TDataStd_Real) aValueOfZoneModif;
240   if(Label().FindChild(ChildLab_ValueOfZoneModif).FindAttribute(TDataStd_Real::GetID(), aValueOfZoneModif))
241   {
242     anObj->SetValueOfZoneModifier(aValueOfZoneModif->Get());
243   }
244
245   Handle(TDataStd_IntegerArray) anArr;
246   if(Label().FindChild(ChildLab_Modifiers).FindAttribute(TDataStd_IntegerArray::GetID(), anArr)
247      && !anArr->Array().IsNull())
248   {
249     XCAFDimTolObjects_GeomToleranceModifiersSequence aModifiers;
250     for(Standard_Integer i = 1; i <= anArr->Length(); i++)
251       aModifiers.Append((XCAFDimTolObjects_GeomToleranceModif)anArr->Value(i));
252     anObj->SetModifiers(aModifiers);
253   }
254
255   Handle(TDataStd_Real) aMaxValueModif;
256   if(Label().FindChild(ChildLab_aMaxValueModif).FindAttribute(TDataStd_Real::GetID(), aMaxValueModif))
257   {
258     anObj->SetMaxValueModifier(aMaxValueModif->Get());
259   }
260   
261   Handle(TDataXtd_Plane) anAxisAttr;
262   if (Label().FindChild(ChildLab_Axis).FindAttribute(TDataXtd_Plane::GetID(), anAxisAttr))
263   {
264     gp_Pln aPlane;
265     TDataXtd_Geometry::Plane(anAxisAttr->Label(), aPlane);
266     gp_Ax2 anAx(aPlane.Location(), aPlane.Axis().Direction(), aPlane.XAxis().Direction());
267     anObj->SetAxis(anAx);
268   }
269
270   Handle(TDataXtd_Plane) aPlaneAttr;
271   if (Label().FindChild(ChildLab_Plane).FindAttribute(TDataXtd_Plane::GetID(), aPlaneAttr))
272   {
273     gp_Pln aPlane;
274     TDataXtd_Geometry::Plane(aPlaneAttr->Label(), aPlane);
275     gp_Ax2 anAx(aPlane.Location(), aPlane.Axis().Direction(), aPlane.XAxis().Direction());
276     anObj->SetPlane(anAx);
277   }
278
279   Handle(TDataXtd_Point) aPntAttr;
280   if (Label().FindChild(ChildLab_Pnt).FindAttribute(TDataXtd_Point::GetID(), aPntAttr))
281   {
282     gp_Pnt aPoint;
283     TDataXtd_Geometry::Point(aPntAttr->Label(), aPoint);
284     anObj->SetPoint(aPoint);
285   }
286
287   Handle(TDataXtd_Point) aPntTextAttr;
288   if (Label().FindChild(ChildLab_PntText).FindAttribute(TDataXtd_Point::GetID(), aPntTextAttr))
289   {
290     gp_Pnt aPoint;
291     TDataXtd_Geometry::Point(aPntTextAttr->Label(), aPoint);
292     anObj->SetPointTextAttach(aPoint);
293   }
294
295   Handle(TNaming_NamedShape) aNS;
296   TDF_Label aLPres = Label().FindChild( ChildLab_Presentation);
297   if ( aLPres.FindAttribute(TNaming_NamedShape::GetID(), aNS) ) 
298   {
299
300     TopoDS_Shape aPresentation = TNaming_Tool::GetShape(aNS);
301     if( !aPresentation.IsNull())
302     {
303       Handle(TDataStd_Name) aNameAtrr;
304       Handle(TCollection_HAsciiString) aPresentName;
305       if (aLPres.FindAttribute(TDataStd_Name::GetID(),aNameAtrr))
306       {
307         const TCollection_ExtendedString& aName = aNameAtrr->Get();
308
309         if( !aName.IsEmpty())
310           aPresentName = new TCollection_HAsciiString(aName);
311       }
312
313       anObj->SetPresentation(aPresentation, aPresentName);
314     }
315   }
316
317   Handle(TDataXtd_Plane) anAffectedPlaneAttr;
318   if (Label().FindChild(ChildLab_AffectedPlane).FindAttribute(TDataXtd_Plane::GetID(), anAffectedPlaneAttr))
319   {
320     gp_Pln aPlane;
321     TDataXtd_Geometry::Plane(anAffectedPlaneAttr->Label(), aPlane);
322     Handle(TDataStd_Integer) aTypeAttr;
323     Label().FindChild(ChildLab_AffectedPlane).FindAttribute(TDataStd_Integer::GetID(), aTypeAttr);
324     anObj->SetAffectedPlane(aPlane, (XCAFDimTolObjects_ToleranceZoneAffectedPlane)aTypeAttr->Get());
325   }
326
327   return anObj;
328 }
329
330 //=======================================================================
331 //function : ID
332 //purpose  : 
333 //=======================================================================
334
335 const Standard_GUID& XCAFDoc_GeomTolerance::ID() const
336 {
337   return GetID();
338 }
339
340 //=======================================================================
341 //function : Restore
342 //purpose  : 
343 //=======================================================================
344
345 void XCAFDoc_GeomTolerance::Restore(const Handle(TDF_Attribute)& /*With*/) 
346 {
347 }
348
349
350 //=======================================================================
351 //function : NewEmpty
352 //purpose  : 
353 //=======================================================================
354
355 Handle(TDF_Attribute) XCAFDoc_GeomTolerance::NewEmpty() const
356 {
357   return new XCAFDoc_GeomTolerance();
358 }
359
360
361 //=======================================================================
362 //function : Paste
363 //purpose  : 
364 //=======================================================================
365
366 void XCAFDoc_GeomTolerance::Paste(const Handle(TDF_Attribute)& /*Into*/,
367                            const Handle(TDF_RelocationTable)& /*RT*/) const
368 {
369 }