1 // Created on: 2004-05-13
2 // Created by: Sergey ZARITCHNY
3 // Copyright (c) 2004-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
17 #include <BinMDF_ADriver.hxx>
18 #include <BinMNaming.hxx>
19 #include <BinMNaming_NamingDriver.hxx>
20 #include <BinObjMgt_Persistent.hxx>
21 #include <BinObjMgt_RRelocationTable.hxx>
22 #include <BinObjMgt_SRelocationTable.hxx>
23 #include <CDM_MessageDriver.hxx>
24 #include <Standard_Type.hxx>
25 #include <TCollection_AsciiString.hxx>
26 #include <TCollection_ExtendedString.hxx>
27 #include <TColStd_Array1OfInteger.hxx>
28 #include <TDF_Attribute.hxx>
29 #include <TDF_Tool.hxx>
30 #include <TNaming_Iterator.hxx>
31 #include <TNaming_ListIteratorOfListOfNamedShape.hxx>
32 #include <TNaming_NamedShape.hxx>
33 #include <TNaming_NameType.hxx>
34 #include <TNaming_Naming.hxx>
35 #include <TopAbs_ShapeEnum.hxx>
37 IMPLEMENT_STANDARD_RTTIEXT(BinMNaming_NamingDriver,BinMDF_ADriver)
39 #define NULL_ENTRY "0:0"
40 #define OBSOLETE_NUM (int)sizeof(Standard_Integer)
42 //=======================================================================
43 // 'Z' - is reserved for: forfidden to use
44 //=======================================================================
45 static Standard_Character NameTypeToChar(const TNaming_NameType theNameType)
48 case TNaming_UNKNOWN : return 'N';
49 case TNaming_IDENTITY : return 'I';
50 case TNaming_MODIFUNTIL : return 'M';
51 case TNaming_GENERATION : return 'G';
52 case TNaming_INTERSECTION : return 'S';
53 case TNaming_UNION : return 'U';
54 case TNaming_SUBSTRACTION : return 'B';
55 case TNaming_CONSTSHAPE : return 'C';
56 case TNaming_FILTERBYNEIGHBOURGS : return 'F';
57 case TNaming_ORIENTATION : return 'O';
58 case TNaming_WIREIN : return 'W';
59 case TNaming_SHELLIN : return 'H';
61 Standard_DomainError::Raise("TNaming_NameType:: Name Type Unknown");
63 return 'N'; // To avoid compilation error message.
66 //=======================================================================
67 static TNaming_NameType CharTypeToName(const Standard_Character theCharType)
70 case 'N' : return TNaming_UNKNOWN;
71 case 'I' : return TNaming_IDENTITY;
72 case 'M' : return TNaming_MODIFUNTIL;
73 case 'G' : return TNaming_GENERATION;
74 case 'S' : return TNaming_INTERSECTION;
75 case 'U' : return TNaming_UNION;
76 case 'B' : return TNaming_SUBSTRACTION;
77 case 'C' : return TNaming_CONSTSHAPE;
78 case 'F' : return TNaming_FILTERBYNEIGHBOURGS;
79 case 'O' : return TNaming_ORIENTATION;
80 case 'W' : return TNaming_WIREIN;
81 case 'H' : return TNaming_SHELLIN;
83 Standard_DomainError::Raise("TNaming_NameType:: Name Type Unknown");
85 return TNaming_UNKNOWN; // To avoid compilation error message.
88 //=======================================================================
89 static Standard_Character ShapeTypeToChar(const TopAbs_ShapeEnum theShapeType)
93 case TopAbs_COMPOUND : return 'C';
94 case TopAbs_COMPSOLID : return 'O';
95 case TopAbs_SOLID : return 'S';
96 case TopAbs_SHELL : return 'H';
97 case TopAbs_FACE : return 'F';
98 case TopAbs_WIRE : return 'W';
99 case TopAbs_EDGE : return 'E';
100 case TopAbs_VERTEX : return 'V';
101 case TopAbs_SHAPE : return 'A';
103 return 'A'; // To avoid compilation error message.
105 //=======================================================================
106 static TopAbs_ShapeEnum CharToShapeType(const Standard_Character theCharType)
110 case 'C' : return TopAbs_COMPOUND;
111 case 'O' : return TopAbs_COMPSOLID;
112 case 'S' : return TopAbs_SOLID;
113 case 'H' : return TopAbs_SHELL;
114 case 'F' : return TopAbs_FACE;
115 case 'W' : return TopAbs_WIRE;
116 case 'E' : return TopAbs_EDGE;
117 case 'V' : return TopAbs_VERTEX;
118 case 'A' : return TopAbs_SHAPE;
120 return TopAbs_SHAPE; // To avoid compilation error message.
122 //=======================================================================
123 //function : BinMNaming_NamingDriver
124 //purpose : Constructor
125 //=======================================================================
127 BinMNaming_NamingDriver::BinMNaming_NamingDriver
128 (const Handle(CDM_MessageDriver)& theMsgDriver)
129 : BinMDF_ADriver (theMsgDriver, STANDARD_TYPE(TNaming_Naming)->Name())
133 //=======================================================================
134 //function : NewEmpty
136 //=======================================================================
138 Handle(TDF_Attribute) BinMNaming_NamingDriver::NewEmpty() const
140 return new TNaming_Naming();
143 //=======================================================================
145 //purpose : persistent -> transient (retrieve)
146 //=======================================================================
148 Standard_Boolean BinMNaming_NamingDriver::Paste
149 (const BinObjMgt_Persistent& theSource,
150 const Handle(TDF_Attribute)& theTarget,
151 BinObjMgt_RRelocationTable& theRelocTable) const
153 Handle(TNaming_Naming) anAtt = Handle(TNaming_Naming)::DownCast(theTarget);
154 if(anAtt.IsNull()) return Standard_False;
156 TNaming_Name& aName = anAtt->ChangeName();
157 TCollection_ExtendedString aMsg;
159 Standard_Character aValue;
160 Standard_Boolean ok = theSource >> aValue;
161 Standard_Boolean aNewF = Standard_False;
163 if(aValue == 'Z') { // new format
164 aNewF = Standard_True;
165 ok = theSource >> aValue; //skip the sign & get NameType
169 aName.Type(CharTypeToName(aValue));
172 ok = theSource >> aValue;
174 aName.ShapeType(CharToShapeType(aValue));
177 Standard_Integer aNbArgs=0;
178 Standard_Integer anIndx;
179 Handle(TNaming_NamedShape) aNS;
180 ok = theSource >> aNbArgs;
185 for(i=1; i<=aNbArgs;i++) {
186 if(!aNewF && i > OBSOLETE_NUM) break;//interrupt reading as old format can have only 4 items
187 ok = theSource >> anIndx;
191 if (theRelocTable.IsBound(anIndx))
192 aNS = Handle(TNaming_NamedShape)::DownCast(theRelocTable.Find(anIndx));
194 aNS = new TNaming_NamedShape;
195 theRelocTable.Bind(anIndx, aNS);
200 //patch to release the rest of items
201 if(!aNewF && aNbArgs < OBSOLETE_NUM) {
202 for(i = aNbArgs+1;i <= OBSOLETE_NUM;i++)
207 ok = theSource >> anIndx;
210 if (theRelocTable.IsBound(anIndx))
211 aNS = Handle(TNaming_NamedShape)::DownCast(theRelocTable.Find(anIndx));
214 aNS = new TNaming_NamedShape;
215 theRelocTable.Bind(anIndx, aNS);
217 aName.StopNamedShape(aNS);
221 ok = theSource >> anIndx;
225 aMsg = TCollection_ExtendedString("BinMNaming_NamingDriver: "
226 "Cannot retrieve Index of Name");
230 aMsg = TCollection_ExtendedString("BinMNaming_NamingDriver: "
231 "Cannot retrieve reference on "
236 aMsg = TCollection_ExtendedString("BinMNaming_NamingDriver: "
237 "Cannot retrieve reference on "
238 "Arguments of Name");
242 if(BinMNaming::DocumentVersion() > 3) {
243 TCollection_AsciiString entry;
244 ok = theSource >> entry;
247 cout << "NamingDriver:: Retrieved Context Label = " << entry << " Ok = " << theSource.IsOK() <<endl;
251 if(!entry.IsEmpty() && !entry.IsEqual(TCollection_AsciiString(NULL_ENTRY)))
253 TDF_Label tLab; // Null label.
254 TDF_Tool::Label(anAtt->Label().Data(),entry, tLab, Standard_True);
256 aName.ContextLabel(tLab);
259 if(BinMNaming::DocumentVersion() > 4 && BinMNaming::DocumentVersion() < 7) {
260 // Orientation processing - converting from old format
261 Handle(TNaming_NamedShape) aNShape;
262 if(anAtt->Label().FindAttribute(TNaming_NamedShape::GetID(), aNShape)) {
263 //const TDF_Label& aLab = aNS->Label();
264 TNaming_Iterator itL (aNShape);
265 for (; itL.More(); itL.Next()) {
266 const TopoDS_Shape& S = itL.NewShape();
267 if (S.IsNull()) continue;
268 if(aNShape->Evolution() == TNaming_SELECTED) {
269 if (itL.More() && itL.NewShape().ShapeType() != TopAbs_VERTEX &&
270 !itL.OldShape().IsNull() && itL.OldShape().ShapeType() == TopAbs_VERTEX ) {//OR-N
271 TopAbs_Orientation OrientationToApply = itL.OldShape().Orientation();
272 aName.Orientation(OrientationToApply);
278 if(BinMNaming::DocumentVersion() > 6) {
279 ok = theSource >> anIndx;
280 TopAbs_Orientation OrientationToApply(TopAbs_FORWARD);
282 OrientationToApply = (TopAbs_Orientation)anIndx;
283 aName.Orientation(OrientationToApply);
285 cout << "NamingDriver:: Retrieved Orientation = " << OrientationToApply << " Ok = " << theSource.IsOK() <<endl;
288 aMsg = TCollection_ExtendedString("BinMNaming_NamingDriver: "
289 "Cannot retrieve Name Orientation ");
295 else if(BinMNaming::DocumentVersion() == -1)
296 cout << "Current DocVersion field is not initialized. " <<endl;
298 cout << "Current DocVersion = " << BinMNaming::DocumentVersion() <<endl;
305 //=======================================================================
307 //purpose : transient -> persistent (store)
308 //=======================================================================
310 void BinMNaming_NamingDriver::Paste (const Handle(TDF_Attribute)& theSource,
311 BinObjMgt_Persistent& theTarget,
312 BinObjMgt_SRelocationTable& theRelocTable) const
314 Handle(TNaming_Naming) anAtt = Handle(TNaming_Naming)::DownCast(theSource);
315 const TNaming_Name& aName = anAtt->GetName();
317 //0. add the sign of new format (to fix misprint with Array size)
318 theTarget.PutCharacter('Z');
320 //1. << NameType to Char
321 theTarget << NameTypeToChar(aName.Type());
323 //2. << ShapeType to Char
324 theTarget << ShapeTypeToChar(aName.ShapeType());
327 Standard_Integer anIndx;
328 Standard_Integer aNbArgs = aName.Arguments().Extent();
329 theTarget << aNbArgs; // keep Number
331 Standard_Integer i=0;
332 TColStd_Array1OfInteger anArray(1, aNbArgs);
334 for (TNaming_ListIteratorOfListOfNamedShape it (aName.Arguments()); it.More(); it.Next()) {
335 Handle(TNaming_NamedShape) anArg = it.Value();
337 if (!anArg.IsNull()) {
338 anIndx = theRelocTable.FindIndex(anArg);
340 anIndx = theRelocTable.Add(anArg);
342 anArray.SetValue(i, anIndx);
345 theTarget.PutIntArray ((BinObjMgt_PInteger) &anArray.Value(1), aNbArgs); // keep Array
349 Handle(TNaming_NamedShape) aStopNS = aName.StopNamedShape();
350 if (!aStopNS.IsNull()) {
351 anIndx = theRelocTable.FindIndex(aStopNS);
353 anIndx = theRelocTable.Add(aStopNS);
359 theTarget << aName.Index();
361 //6. keep context label
362 TCollection_AsciiString entry(NULL_ENTRY);
363 if(!aName.ContextLabel().IsNull())
364 TDF_Tool::Entry(aName.ContextLabel(), entry);
367 //7. keep Orientation
368 theTarget << (Standard_Integer)aName.Orientation();