1 // Copyright (c) 2015 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
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.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #include <StdLPersistent_Data.hxx>
15 #include <StdObjMgt_ReadData.hxx>
16 #include <StdObjMgt_WriteData.hxx>
18 #include <TDF_Data.hxx>
19 #include <TDF_Attribute.hxx>
21 //! Create a transient label tree from persistent data
22 class StdLPersistent_Data::Parser
25 //! Start parsing a persistent data.
26 Parser (const TColStd_HArray1OfInteger& theLabels,
27 const StdLPersistent_HArray1OfPersistent& theAttributes)
28 : myLabelsIter (theLabels)
29 , myAttribIter (theAttributes) {}
31 //! Fill a transient label with data.
32 void FillLabel (TDF_Label theLabel)
36 // Read attributes count
38 Standard_Integer anAttribCount = myLabelsIter.Value();
40 // Add attributes to the label
41 for (i = 0; i < anAttribCount; i++)
43 // read persistent attribute
44 Handle(StdObjMgt_Persistent)& aPAttrib = myAttribIter.ChangeValue();
46 // create transient attribute and add it to the label
48 Handle (TDF_Attribute) anAtt = aPAttrib->CreateAttribute();
50 theLabel.AddAttribute (anAtt);
54 // Read child labels count
56 Standard_Integer aSubLabelsCount = myLabelsIter.Value();
58 // Create child labels
59 for (i = 0; i < aSubLabelsCount; i++)
61 // read tag of child label
63 Standard_Integer aSubLabelTag = myLabelsIter.Value();
65 // create and fill child label
66 TDF_Label aSubLabel = theLabel.FindChild (aSubLabelTag, Standard_True);
67 FillLabel (aSubLabel);
72 TColStd_HArray1OfInteger ::Iterator myLabelsIter;
73 StdLPersistent_HArray1OfPersistent::Iterator myAttribIter;
76 //=======================================================================
78 //purpose : Read persistent data from a file
79 //=======================================================================
80 void StdLPersistent_Data::Read (StdObjMgt_ReadData& theReadData)
82 theReadData >> myVersion >> myLabels >> myAttributes;
85 //=======================================================================
87 //purpose : Write persistent data to a file
88 //=======================================================================
89 void StdLPersistent_Data::Write (StdObjMgt_WriteData& theWriteData) const
91 theWriteData << myVersion << myLabels << myAttributes;
94 //=======================================================================
96 //purpose : Import transient data from the persistent data
97 //=======================================================================
98 Handle(TDF_Data) StdLPersistent_Data::Import() const
100 if (myLabels.IsNull() || myAttributes.IsNull())
103 // Create tree of labels and add empty transient attributes to them
104 Handle(TDF_Data) aData = new TDF_Data;
105 Parser (*myLabels->Array(), *myAttributes->Array()).FillLabel (aData->Root());
107 // Import transient attribuites from persistent data
108 StdLPersistent_HArray1OfPersistent::Iterator anAttribIter (*myAttributes->Array());
109 for (; anAttribIter.More(); anAttribIter.Next())
111 Handle(StdObjMgt_Persistent)& aPAttrib = anAttribIter.ChangeValue();
113 aPAttrib->ImportAttribute();