0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / src / StdStorage / StdStorage_RootData.cxx
1 // Copyright (c) 2017 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 <StdObjMgt_Persistent.hxx>
15 #include <Standard_ErrorHandler.hxx>
16 #include <Standard_NoSuchObject.hxx>
17 #include <StdStorage_RootData.hxx>
18 #include <StdStorage_Root.hxx>
19 #include <Storage_BaseDriver.hxx>
20 #include <Storage_StreamTypeMismatchError.hxx>
21 #include <Storage_DataMapIteratorOfMapOfPers.hxx>
22 #include <TCollection_AsciiString.hxx>
23
24 IMPLEMENT_STANDARD_RTTIEXT(StdStorage_RootData, Standard_Transient)
25
26 StdStorage_RootData::StdStorage_RootData() 
27   : myErrorStatus(Storage_VSOk)
28 {
29 }
30
31 Standard_Boolean StdStorage_RootData::Read(Storage_BaseDriver& theDriver)
32 {
33   // Check driver open mode
34   if (theDriver.OpenMode() != Storage_VSRead
35     && theDriver.OpenMode() != Storage_VSReadWrite)
36   {
37     myErrorStatus = Storage_VSModeError;
38     myErrorStatusExt = "OpenMode";
39     return Standard_False;
40   }
41
42   // Read root section
43   myErrorStatus = theDriver.BeginReadRootSection();
44   if (myErrorStatus != Storage_VSOk)
45   {
46     myErrorStatusExt = "BeginReadRootSection";
47     return Standard_False;
48   }
49
50   TCollection_AsciiString aRootName, aTypeName;
51   Standard_Integer aRef;
52
53   Standard_Integer len = theDriver.RootSectionSize();
54   for (Standard_Integer i = 1; i <= len; i++)
55   {
56     try
57     {
58       OCC_CATCH_SIGNALS
59       theDriver.ReadRoot(aRootName, aRef, aTypeName);
60     }
61     catch (Storage_StreamTypeMismatchError const&)
62     {
63       myErrorStatus = Storage_VSTypeMismatch;
64       myErrorStatusExt = "ReadRoot";
65       return Standard_False;
66     }
67
68     Handle(StdStorage_Root) aRoot = new StdStorage_Root(aRootName, aRef, aTypeName);
69     myObjects.Add(aRootName, aRoot);
70   }
71
72   myErrorStatus = theDriver.EndReadRootSection();
73   if (myErrorStatus != Storage_VSOk)
74   {
75     myErrorStatusExt = "EndReadRootSection";
76     return Standard_False;
77   }
78
79   return Standard_True;
80 }
81
82 Standard_Boolean StdStorage_RootData::Write(Storage_BaseDriver& theDriver)
83 {
84   // Check driver open mode
85   if (theDriver.OpenMode() != Storage_VSWrite
86     && theDriver.OpenMode() != Storage_VSReadWrite)
87   {
88     myErrorStatus = Storage_VSModeError;
89     myErrorStatusExt = "OpenMode";
90     return Standard_False;
91   }
92
93   // Write root section
94   myErrorStatus = theDriver.BeginWriteRootSection();
95   if (myErrorStatus != Storage_VSOk)
96   {
97     myErrorStatusExt = "BeginWriteRootSection";
98     return Standard_False;
99   }
100
101   theDriver.SetRootSectionSize(NumberOfRoots());
102   for (StdStorage_MapOfRoots::Iterator anIt(myObjects); anIt.More(); anIt.Next())
103   {
104     const Handle(StdStorage_Root)& aRoot = anIt.Value();
105     try
106     {
107       OCC_CATCH_SIGNALS
108       theDriver.WriteRoot(aRoot->Name(), aRoot->Reference(), aRoot->Type());
109     }
110     catch (Storage_StreamTypeMismatchError const&)
111     {
112       myErrorStatus = Storage_VSTypeMismatch;
113       myErrorStatusExt = "ReadRoot";
114       return Standard_False;
115     }
116   }
117
118   myErrorStatus = theDriver.EndWriteRootSection();
119   if (myErrorStatus != Storage_VSOk)
120   {
121     myErrorStatusExt = "EndWriteRootSection";
122     return Standard_False;
123   }
124
125   return Standard_True;
126 }
127
128 Standard_Integer StdStorage_RootData::NumberOfRoots() const
129 {
130   return myObjects.Extent();
131 }
132
133 void StdStorage_RootData::AddRoot(const Handle(StdStorage_Root)& aRoot)
134 {
135   myObjects.Add(aRoot->Name(), aRoot);
136   aRoot->myRef = myObjects.Size();
137 }
138
139 Handle(StdStorage_HSequenceOfRoots) StdStorage_RootData::Roots() const
140 {
141   Handle(StdStorage_HSequenceOfRoots)    anObjectsSeq = new StdStorage_HSequenceOfRoots;
142   StdStorage_DataMapIteratorOfMapOfRoots it(myObjects);
143
144   for (; it.More(); it.Next()) {
145     anObjectsSeq->Append(it.Value());
146   }
147
148   return anObjectsSeq;
149 }
150
151 Handle(StdStorage_Root) StdStorage_RootData::Find(const TCollection_AsciiString& aName) const
152 {
153   Handle(StdStorage_Root) p;
154   if (myObjects.Contains(aName)) {
155     p = myObjects.FindFromKey(aName);
156   }
157
158   return p;
159 }
160
161 Standard_Boolean StdStorage_RootData::IsRoot(const TCollection_AsciiString& aName) const
162 {
163   return myObjects.Contains(aName);
164 }
165
166 void StdStorage_RootData::RemoveRoot(const TCollection_AsciiString& aName)
167 {
168   if (myObjects.Contains(aName)) {
169     myObjects.ChangeFromKey(aName)->myRef = 0;
170     myObjects.RemoveKey(aName);
171     Standard_Integer aRef = 1;
172     for (StdStorage_MapOfRoots::Iterator anIt(myObjects); anIt.More(); anIt.Next(), ++aRef)
173       anIt.ChangeValue()->myRef = aRef;
174   }
175 }
176
177 void StdStorage_RootData::Clear()
178 {
179   for (StdStorage_MapOfRoots::Iterator anIt(myObjects); anIt.More(); anIt.Next())
180     anIt.ChangeValue()->myRef = 0;
181
182   myObjects.Clear();
183 }
184
185 Storage_Error  StdStorage_RootData::ErrorStatus() const
186 {
187   return myErrorStatus;
188 }
189
190 void StdStorage_RootData::SetErrorStatus(const Storage_Error anError)
191 {
192   myErrorStatus = anError;
193 }
194
195 void StdStorage_RootData::ClearErrorStatus()
196 {
197   myErrorStatus = Storage_VSOk;
198   myErrorStatusExt.Clear();
199 }
200
201 TCollection_AsciiString StdStorage_RootData::ErrorStatusExtension() const
202 {
203   return myErrorStatusExt;
204 }
205
206 void StdStorage_RootData::SetErrorStatusExtension(const TCollection_AsciiString& anErrorExt)
207 {
208   myErrorStatusExt = anErrorExt;
209 }