Integration of OCCT 6.5.0 from SVN
[occt.git] / src / BinMNaming / BinMNaming_NamingDriver.cxx
CommitLineData
7fd59977 1// File: BinMNaming_NamingDriver.cxx
2// Created: Thu May 13 16:57:08 2004
3// Author: Sergey ZARITCHNY <szy@opencascade.com>
4// Copyright: Open CasCade S.A. 2004
5
6
7
8#include <BinMNaming_NamingDriver.ixx>
9#include <TopAbs_ShapeEnum.hxx>
10#include <TCollection_ExtendedString.hxx>
11#include <TColStd_Array1OfInteger.hxx>
12#include <CDM_MessageDriver.hxx>
13#include <TDF_Attribute.hxx>
14#include <TNaming_NameType.hxx>
15#include <TNaming_Naming.hxx>
16#include <TNaming_NamedShape.hxx>
17#include <TNaming_ListIteratorOfListOfNamedShape.hxx>
18#include <BinMDF_ADriver.hxx>
19#include <BinObjMgt_Persistent.hxx>
20#include <BinObjMgt_RRelocationTable.hxx>
21#include <BinObjMgt_SRelocationTable.hxx>
22#include <BinMNaming.hxx>
23#include <TCollection_AsciiString.hxx>
24#include <TDF_Tool.hxx>
25#define NULL_ENTRY "0:0"
26#define OBSOLETE_NUM sizeof(Standard_Integer)
27//=======================================================================
28// 'Z' - is reserved for: forfidden to use
29//=======================================================================
30static Standard_Character NameTypeToChar(const TNaming_NameType theNameType)
31{
32 switch(theNameType) {
33 case TNaming_UNKNOWN : return 'N';
34 case TNaming_IDENTITY : return 'I';
35 case TNaming_MODIFUNTIL : return 'M';
36 case TNaming_GENERATION : return 'G';
37 case TNaming_INTERSECTION : return 'S';
38 case TNaming_UNION : return 'U';
39 case TNaming_SUBSTRACTION : return 'B';
40 case TNaming_CONSTSHAPE : return 'C';
41 case TNaming_FILTERBYNEIGHBOURGS : return 'F';
42 case TNaming_ORIENTATION : return 'O';
43 case TNaming_WIREIN : return 'W';
44 default:
45 Standard_DomainError::Raise("TNaming_NameType:: Name Type Unknown");
46 }
47 return 'N'; // To avoid compilation error message.
48}
49
50//=======================================================================
51static TNaming_NameType CharTypeToName(const Standard_Character theCharType)
52{
53 switch(theCharType) {
54 case 'N' : return TNaming_UNKNOWN;
55 case 'I' : return TNaming_IDENTITY;
56 case 'M' : return TNaming_MODIFUNTIL;
57 case 'G' : return TNaming_GENERATION;
58 case 'S' : return TNaming_INTERSECTION;
59 case 'U' : return TNaming_UNION;
60 case 'B' : return TNaming_SUBSTRACTION;
61 case 'C' : return TNaming_CONSTSHAPE;
62 case 'F' : return TNaming_FILTERBYNEIGHBOURGS;
63 case 'O' : return TNaming_ORIENTATION;
64 case 'W' : return TNaming_WIREIN;
65 default:
66 Standard_DomainError::Raise("TNaming_NameType:: Name Type Unknown");
67 }
68 return TNaming_UNKNOWN; // To avoid compilation error message.
69}
70
71//=======================================================================
72static Standard_Character ShapeTypeToChar(const TopAbs_ShapeEnum theShapeType)
73{
74 switch (theShapeType)
75 {
76 case TopAbs_COMPOUND : return 'C';
77 case TopAbs_COMPSOLID : return 'O';
78 case TopAbs_SOLID : return 'S';
79 case TopAbs_SHELL : return 'H';
80 case TopAbs_FACE : return 'F';
81 case TopAbs_WIRE : return 'W';
82 case TopAbs_EDGE : return 'E';
83 case TopAbs_VERTEX : return 'V';
84 case TopAbs_SHAPE : return 'A';
85 }
86 return 'A'; // To avoid compilation error message.
87}
88//=======================================================================
89static TopAbs_ShapeEnum CharToShapeType(const Standard_Character theCharType)
90{
91 switch (theCharType)
92 {
93 case 'C' : return TopAbs_COMPOUND;
94 case 'O' : return TopAbs_COMPSOLID;
95 case 'S' : return TopAbs_SOLID;
96 case 'H' : return TopAbs_SHELL;
97 case 'F' : return TopAbs_FACE;
98 case 'W' : return TopAbs_WIRE;
99 case 'E' : return TopAbs_EDGE;
100 case 'V' : return TopAbs_VERTEX;
101 case 'A' : return TopAbs_SHAPE;
102 }
103 return TopAbs_SHAPE; // To avoid compilation error message.
104}
105//=======================================================================
106//function : BinMNaming_NamingDriver
107//purpose : Constructor
108//=======================================================================
109
110BinMNaming_NamingDriver::BinMNaming_NamingDriver
111 (const Handle(CDM_MessageDriver)& theMsgDriver)
112 : BinMDF_ADriver (theMsgDriver, STANDARD_TYPE(TNaming_Naming)->Name())
113{
114}
115
116//=======================================================================
117//function : NewEmpty
118//purpose :
119//=======================================================================
120
121Handle(TDF_Attribute) BinMNaming_NamingDriver::NewEmpty() const
122{
123 return new TNaming_Naming();
124}
125
126//=======================================================================
127//function : Paste
128//purpose : persistent -> transient (retrieve)
129//=======================================================================
130
131Standard_Boolean BinMNaming_NamingDriver::Paste
132 (const BinObjMgt_Persistent& theSource,
133 const Handle(TDF_Attribute)& theTarget,
134 BinObjMgt_RRelocationTable& theRelocTable) const
135{
136 Handle(TNaming_Naming) anAtt = Handle(TNaming_Naming)::DownCast(theTarget);
137 if(anAtt.IsNull()) return Standard_False;
138
139 TNaming_Name& aName = anAtt->ChangeName();
140 TCollection_ExtendedString aMsg;
141//1. NameType
142 Standard_Character aValue;
143 Standard_Boolean ok = theSource >> aValue;
144 Standard_Boolean aNewF = Standard_False;
145 if (ok) {
146 if(aValue == 'Z') { // new format
147 aNewF = Standard_True;
148 ok = theSource >> aValue; //skip the sign & get NameType
149 if(!ok) return ok;
150 }
151
152 aName.Type(CharTypeToName(aValue));
153
154//2. ShapeType
155 ok = theSource >> aValue;
156 if (ok) {
157 aName.ShapeType(CharToShapeType(aValue));
158
159//3. Args
160 Standard_Integer aNbArgs=0;
161 Standard_Integer anIndx;
162 Handle(TNaming_NamedShape) aNS;
163 ok = theSource >> aNbArgs;
164 if (ok) {
165 if(aNbArgs > 0) {
166 Standard_Integer i;
167 // read array
168 for(i=1; i<=aNbArgs;i++) {
169 if(!aNewF && i > OBSOLETE_NUM) break;//interrupt reading as old format can have only 4 items
170 ok = theSource >> anIndx;
171 if (!ok)
172 break;
173 else {
174 if (theRelocTable.IsBound(anIndx))
175 aNS = Handle(TNaming_NamedShape)::DownCast(theRelocTable.Find(anIndx));
176 else {
177 aNS = new TNaming_NamedShape;
178 theRelocTable.Bind(anIndx, aNS);
179 }
180 aName.Append(aNS);
181 }
182 }
183 //patch to release the rest of items
184 if(!aNewF && aNbArgs < OBSOLETE_NUM) {
185 for(i = aNbArgs+1;i <= OBSOLETE_NUM;i++)
186 theSource >> anIndx;
187 }
188 }
189//4. StopNS
190 ok = theSource >> anIndx;
191 if(ok) {
192 if(anIndx > 0) {
193 if (theRelocTable.IsBound(anIndx))
194 aNS = Handle(TNaming_NamedShape)::DownCast(theRelocTable.Find(anIndx));
195 else
196 {
197 aNS = new TNaming_NamedShape;
198 theRelocTable.Bind(anIndx, aNS);
199 }
200 aName.StopNamedShape(aNS);
201 }
202
203//5. Index
204 ok = theSource >> anIndx;
205 if(ok)
206 aName.Index(anIndx);
207 else {
208 aMsg = TCollection_ExtendedString("BinMNaming_NamingDriver: "
209 "Cannot retrieve Index of Name");
210 WriteMessage (aMsg);
211 }
212 } else {
213 aMsg = TCollection_ExtendedString("BinMNaming_NamingDriver: "
214 "Cannot retrieve reference on "
215 "StopNamedShape");
216 WriteMessage (aMsg);
217 }
218 } else {
219 aMsg = TCollection_ExtendedString("BinMNaming_NamingDriver: "
220 "Cannot retrieve reference on "
221 "Arguments of Name");
222 WriteMessage (aMsg);
223 }
224
225#ifdef DEB
226 //cout << "CurDocVersion = " << BinMNaming::DocumentVersion() <<endl;
227#endif
228 if(BinMNaming::DocumentVersion() > 3) {
229 TCollection_AsciiString entry;
230 ok = theSource >> entry;
231 if(ok) {
232#ifdef DEB
233 cout << "NamingDriver:: Retrieved Context Label = " << entry << " Ok = " << theSource.IsOK() <<endl;
234#endif
235
236//6. context label
237 if(!entry.IsEmpty() && !entry.IsEqual(TCollection_AsciiString(NULL_ENTRY)))
238 {
239 TDF_Label tLab; // Null label.
240 TDF_Tool::Label(anAtt->Label().Data(),entry, tLab, Standard_True);
241 if (!tLab.IsNull())
242 aName.ContextLabel(tLab);
243 }
244 }
245 }
246#ifdef DEB
247 else if(BinMNaming::DocumentVersion() == -1)
248 cout << "Current DocVersion field is not initialized. " <<endl;
249 else
250 cout << "Current DocVersion = " << BinMNaming::DocumentVersion() <<endl;
251#endif
252 }
253 }
254 return ok;
255}
256
257//=======================================================================
258//function : Paste
259//purpose : transient -> persistent (store)
260//=======================================================================
261
262void BinMNaming_NamingDriver::Paste (const Handle(TDF_Attribute)& theSource,
263 BinObjMgt_Persistent& theTarget,
264 BinObjMgt_SRelocationTable& theRelocTable) const
265{
266 Handle(TNaming_Naming) anAtt = Handle(TNaming_Naming)::DownCast(theSource);
267 const TNaming_Name& aName = anAtt->GetName();
268
269//0. add the sign of new format (to fix misprint with Array size)
270 theTarget.PutCharacter('Z');
271
272//1. << NameType to Char
273 theTarget << NameTypeToChar(aName.Type());
274
275//2. << ShapeType to Char
276 theTarget << ShapeTypeToChar(aName.ShapeType());
277
278//3. Keep Args
279 Standard_Integer anIndx;
280 Standard_Integer aNbArgs = aName.Arguments().Extent();
281 theTarget << aNbArgs; // keep Number
282 if (aNbArgs > 0) {
283 Standard_Integer i=0;
284 TColStd_Array1OfInteger anArray(1, aNbArgs);
285 //fill array
286 for (TNaming_ListIteratorOfListOfNamedShape it (aName.Arguments()); it.More(); it.Next()) {
287 Handle(TNaming_NamedShape) anArg = it.Value();
288 anIndx = 0; i++;
289 if (!anArg.IsNull()) {
290 anIndx = theRelocTable.FindIndex(anArg);
291 if (anIndx == 0)
292 anIndx = theRelocTable.Add(anArg);
293 }
294 anArray.SetValue(i, anIndx);
295 }
296
297 theTarget.PutIntArray ((BinObjMgt_PInteger) &anArray.Value(1), aNbArgs); // keep Array
298 }
299
300//4. keep StopNS
301 Handle(TNaming_NamedShape) aStopNS = aName.StopNamedShape();
302 if (!aStopNS.IsNull()) {
303 anIndx = theRelocTable.FindIndex(aStopNS);
304 if (anIndx == 0)
305 anIndx = theRelocTable.Add(aStopNS);
306 } else
307 anIndx = 0;
308 theTarget << anIndx;
309
310//5. keep Index
311 theTarget << aName.Index();
312
313//6. keep context label
314 TCollection_AsciiString entry(NULL_ENTRY);
315 if(!aName.ContextLabel().IsNull())
316 TDF_Tool::Entry(aName.ContextLabel(), entry);
317 theTarget << entry;
318}