0031845: Modeling Algorithms - BRepOffsetAPI_MakeThickSolid fails
[occt.git] / src / BinMNaming / BinMNaming_NamingDriver.cxx
CommitLineData
b311480e 1// Created on: 2004-05-13
2// Created by: Sergey ZARITCHNY
973c2be1 3// Copyright (c) 2004-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 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
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
7fd59977 15
42cf5bc1 16
7fd59977 17#include <BinMDF_ADriver.hxx>
42cf5bc1 18#include <BinMNaming.hxx>
19#include <BinMNaming_NamingDriver.hxx>
7fd59977 20#include <BinObjMgt_Persistent.hxx>
21#include <BinObjMgt_RRelocationTable.hxx>
22#include <BinObjMgt_SRelocationTable.hxx>
83ae3591 23#include <Message_Messenger.hxx>
42cf5bc1 24#include <Standard_Type.hxx>
7fd59977 25#include <TCollection_AsciiString.hxx>
42cf5bc1 26#include <TCollection_ExtendedString.hxx>
27#include <TColStd_Array1OfInteger.hxx>
28#include <TDF_Attribute.hxx>
7fd59977 29#include <TDF_Tool.hxx>
14eea829 30#include <TDocStd_FormatVersion.hxx>
42cf5bc1 31#include <TNaming_Iterator.hxx>
32#include <TNaming_ListIteratorOfListOfNamedShape.hxx>
33#include <TNaming_NamedShape.hxx>
34#include <TNaming_NameType.hxx>
35#include <TNaming_Naming.hxx>
36#include <TopAbs_ShapeEnum.hxx>
566f8441 37
92efcf78 38IMPLEMENT_STANDARD_RTTIEXT(BinMNaming_NamingDriver,BinMDF_ADriver)
39
7fd59977 40#define NULL_ENTRY "0:0"
566f8441 41#define OBSOLETE_NUM (int)sizeof(Standard_Integer)
42
7fd59977 43//=======================================================================
44// 'Z' - is reserved for: forfidden to use
45//=======================================================================
46static Standard_Character NameTypeToChar(const TNaming_NameType theNameType)
47{
48 switch(theNameType) {
49 case TNaming_UNKNOWN : return 'N';
50 case TNaming_IDENTITY : return 'I';
51 case TNaming_MODIFUNTIL : return 'M';
52 case TNaming_GENERATION : return 'G';
53 case TNaming_INTERSECTION : return 'S';
54 case TNaming_UNION : return 'U';
55 case TNaming_SUBSTRACTION : return 'B';
56 case TNaming_CONSTSHAPE : return 'C';
57 case TNaming_FILTERBYNEIGHBOURGS : return 'F';
58 case TNaming_ORIENTATION : return 'O';
59 case TNaming_WIREIN : return 'W';
1ec8a59e 60 case TNaming_SHELLIN : return 'H';
7fd59977 61 default:
9775fa61 62 throw Standard_DomainError("TNaming_NameType:: Name Type Unknown");
7fd59977 63 }
7fd59977 64}
65
66//=======================================================================
67static TNaming_NameType CharTypeToName(const Standard_Character theCharType)
68{
69 switch(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;
1ec8a59e 81 case 'H' : return TNaming_SHELLIN;
7fd59977 82 default:
9775fa61 83 throw Standard_DomainError("TNaming_NameType:: Name Type Unknown");
7fd59977 84 }
7fd59977 85}
86
87//=======================================================================
88static Standard_Character ShapeTypeToChar(const TopAbs_ShapeEnum theShapeType)
89{
90 switch (theShapeType)
91 {
92 case TopAbs_COMPOUND : return 'C';
93 case TopAbs_COMPSOLID : return 'O';
94 case TopAbs_SOLID : return 'S';
95 case TopAbs_SHELL : return 'H';
96 case TopAbs_FACE : return 'F';
97 case TopAbs_WIRE : return 'W';
98 case TopAbs_EDGE : return 'E';
99 case TopAbs_VERTEX : return 'V';
100 case TopAbs_SHAPE : return 'A';
101 }
102 return 'A'; // To avoid compilation error message.
103}
104//=======================================================================
105static TopAbs_ShapeEnum CharToShapeType(const Standard_Character theCharType)
106{
107 switch (theCharType)
108 {
109 case 'C' : return TopAbs_COMPOUND;
110 case 'O' : return TopAbs_COMPSOLID;
111 case 'S' : return TopAbs_SOLID;
112 case 'H' : return TopAbs_SHELL;
113 case 'F' : return TopAbs_FACE;
114 case 'W' : return TopAbs_WIRE;
115 case 'E' : return TopAbs_EDGE;
116 case 'V' : return TopAbs_VERTEX;
117 case 'A' : return TopAbs_SHAPE;
118 }
119 return TopAbs_SHAPE; // To avoid compilation error message.
120}
121//=======================================================================
122//function : BinMNaming_NamingDriver
123//purpose : Constructor
124//=======================================================================
125
126BinMNaming_NamingDriver::BinMNaming_NamingDriver
83ae3591 127 (const Handle(Message_Messenger)& theMsgDriver)
7fd59977 128 : BinMDF_ADriver (theMsgDriver, STANDARD_TYPE(TNaming_Naming)->Name())
129{
130}
131
132//=======================================================================
133//function : NewEmpty
134//purpose :
135//=======================================================================
136
137Handle(TDF_Attribute) BinMNaming_NamingDriver::NewEmpty() const
138{
139 return new TNaming_Naming();
140}
141
142//=======================================================================
143//function : Paste
144//purpose : persistent -> transient (retrieve)
145//=======================================================================
146
147Standard_Boolean BinMNaming_NamingDriver::Paste
148 (const BinObjMgt_Persistent& theSource,
149 const Handle(TDF_Attribute)& theTarget,
150 BinObjMgt_RRelocationTable& theRelocTable) const
151{
152 Handle(TNaming_Naming) anAtt = Handle(TNaming_Naming)::DownCast(theTarget);
153 if(anAtt.IsNull()) return Standard_False;
154
155 TNaming_Name& aName = anAtt->ChangeName();
156 TCollection_ExtendedString aMsg;
157//1. NameType
158 Standard_Character aValue;
159 Standard_Boolean ok = theSource >> aValue;
160 Standard_Boolean aNewF = Standard_False;
161 if (ok) {
162 if(aValue == 'Z') { // new format
163 aNewF = Standard_True;
164 ok = theSource >> aValue; //skip the sign & get NameType
165 if(!ok) return ok;
166 }
167
168 aName.Type(CharTypeToName(aValue));
169
170//2. ShapeType
171 ok = theSource >> aValue;
172 if (ok) {
173 aName.ShapeType(CharToShapeType(aValue));
174
175//3. Args
176 Standard_Integer aNbArgs=0;
177 Standard_Integer anIndx;
178 Handle(TNaming_NamedShape) aNS;
179 ok = theSource >> aNbArgs;
180 if (ok) {
181 if(aNbArgs > 0) {
182 Standard_Integer i;
183 // read array
184 for(i=1; i<=aNbArgs;i++) {
185 if(!aNewF && i > OBSOLETE_NUM) break;//interrupt reading as old format can have only 4 items
186 ok = theSource >> anIndx;
187 if (!ok)
188 break;
189 else {
190 if (theRelocTable.IsBound(anIndx))
191 aNS = Handle(TNaming_NamedShape)::DownCast(theRelocTable.Find(anIndx));
192 else {
193 aNS = new TNaming_NamedShape;
194 theRelocTable.Bind(anIndx, aNS);
195 }
196 aName.Append(aNS);
197 }
198 }
199 //patch to release the rest of items
200 if(!aNewF && aNbArgs < OBSOLETE_NUM) {
201 for(i = aNbArgs+1;i <= OBSOLETE_NUM;i++)
202 theSource >> anIndx;
203 }
204 }
205//4. StopNS
206 ok = theSource >> anIndx;
207 if(ok) {
208 if(anIndx > 0) {
209 if (theRelocTable.IsBound(anIndx))
210 aNS = Handle(TNaming_NamedShape)::DownCast(theRelocTable.Find(anIndx));
211 else
212 {
213 aNS = new TNaming_NamedShape;
214 theRelocTable.Bind(anIndx, aNS);
215 }
216 aName.StopNamedShape(aNS);
217 }
218
219//5. Index
220 ok = theSource >> anIndx;
221 if(ok)
222 aName.Index(anIndx);
223 else {
224 aMsg = TCollection_ExtendedString("BinMNaming_NamingDriver: "
225 "Cannot retrieve Index of Name");
83ae3591 226 myMessageDriver->Send (aMsg, Message_Warning);
7fd59977 227 }
228 } else {
229 aMsg = TCollection_ExtendedString("BinMNaming_NamingDriver: "
230 "Cannot retrieve reference on "
231 "StopNamedShape");
83ae3591 232 myMessageDriver->Send (aMsg, Message_Warning);
7fd59977 233 }
234 } else {
235 aMsg = TCollection_ExtendedString("BinMNaming_NamingDriver: "
236 "Cannot retrieve reference on "
237 "Arguments of Name");
83ae3591 238 myMessageDriver->Send (aMsg, Message_Warning);
7dcac1df 239 }
7fd59977 240
14eea829 241 if(theRelocTable.GetHeaderData()->StorageVersion().IntegerValue() >= TDocStd_FormatVersion_VERSION_4) {
7fd59977 242 TCollection_AsciiString entry;
243 ok = theSource >> entry;
244 if(ok) {
0797d9d3 245#ifdef OCCT_DEBUG
04232180 246 std::cout << "NamingDriver:: Retrieved Context Label = " << entry << " Ok = " << theSource.IsOK() <<std::endl;
7fd59977 247#endif
248
249//6. context label
250 if(!entry.IsEmpty() && !entry.IsEqual(TCollection_AsciiString(NULL_ENTRY)))
251 {
252 TDF_Label tLab; // Null label.
253 TDF_Tool::Label(anAtt->Label().Data(),entry, tLab, Standard_True);
254 if (!tLab.IsNull())
255 aName.ContextLabel(tLab);
256 }
257 }
14eea829 258 if(theRelocTable.GetHeaderData()->StorageVersion().IntegerValue() >= TDocStd_FormatVersion_VERSION_5 &&
259 theRelocTable.GetHeaderData()->StorageVersion().IntegerValue() <= TDocStd_FormatVersion_VERSION_6) {
7dcac1df 260 // Orientation processing - converting from old format
51740958 261 Handle(TNaming_NamedShape) aNShape;
262 if(anAtt->Label().FindAttribute(TNaming_NamedShape::GetID(), aNShape)) {
7dcac1df 263 //const TDF_Label& aLab = aNS->Label();
51740958 264 TNaming_Iterator itL (aNShape);
7dcac1df 265 for (; itL.More(); itL.Next()) {
266 const TopoDS_Shape& S = itL.NewShape();
267 if (S.IsNull()) continue;
51740958 268 if(aNShape->Evolution() == TNaming_SELECTED) {
7dcac1df 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);
273 }
274 }
275 }
276 }
277 }
14eea829 278 if(theRelocTable.GetHeaderData()->StorageVersion().IntegerValue() >= TDocStd_FormatVersion_VERSION_7) {
7dcac1df 279 ok = theSource >> anIndx;
280 TopAbs_Orientation OrientationToApply(TopAbs_FORWARD);
281 if(ok) {
282 OrientationToApply = (TopAbs_Orientation)anIndx;
283 aName.Orientation(OrientationToApply);
0797d9d3 284#ifdef OCCT_DEBUG
04232180 285 std::cout << "NamingDriver:: Retrieved Orientation = " << OrientationToApply << " Ok = " << theSource.IsOK() <<std::endl;
7dcac1df 286#endif
287 } else {
288 aMsg = TCollection_ExtendedString("BinMNaming_NamingDriver: "
289 "Cannot retrieve Name Orientation ");
83ae3591 290 myMessageDriver->Send (aMsg, Message_Warning);
7dcac1df 291 }
292 }
293 }
0797d9d3 294#ifdef OCCT_DEBUG
04232180 295 std::cout << "Current Document Format Version = " << theRelocTable.GetHeaderData()->StorageVersion().IntegerValue() <<std::endl;
7fd59977 296#endif
7dcac1df 297 }
7fd59977 298 }
299 return ok;
300}
301
302//=======================================================================
303//function : Paste
304//purpose : transient -> persistent (store)
305//=======================================================================
306
307void BinMNaming_NamingDriver::Paste (const Handle(TDF_Attribute)& theSource,
308 BinObjMgt_Persistent& theTarget,
309 BinObjMgt_SRelocationTable& theRelocTable) const
310{
311 Handle(TNaming_Naming) anAtt = Handle(TNaming_Naming)::DownCast(theSource);
312 const TNaming_Name& aName = anAtt->GetName();
313
314//0. add the sign of new format (to fix misprint with Array size)
315 theTarget.PutCharacter('Z');
316
317//1. << NameType to Char
318 theTarget << NameTypeToChar(aName.Type());
319
320//2. << ShapeType to Char
321 theTarget << ShapeTypeToChar(aName.ShapeType());
322
323//3. Keep Args
324 Standard_Integer anIndx;
325 Standard_Integer aNbArgs = aName.Arguments().Extent();
326 theTarget << aNbArgs; // keep Number
327 if (aNbArgs > 0) {
328 Standard_Integer i=0;
329 TColStd_Array1OfInteger anArray(1, aNbArgs);
330 //fill array
331 for (TNaming_ListIteratorOfListOfNamedShape it (aName.Arguments()); it.More(); it.Next()) {
332 Handle(TNaming_NamedShape) anArg = it.Value();
333 anIndx = 0; i++;
334 if (!anArg.IsNull()) {
335 anIndx = theRelocTable.FindIndex(anArg);
336 if (anIndx == 0)
337 anIndx = theRelocTable.Add(anArg);
338 }
339 anArray.SetValue(i, anIndx);
340 }
341
342 theTarget.PutIntArray ((BinObjMgt_PInteger) &anArray.Value(1), aNbArgs); // keep Array
343 }
344
345//4. keep StopNS
346 Handle(TNaming_NamedShape) aStopNS = aName.StopNamedShape();
347 if (!aStopNS.IsNull()) {
348 anIndx = theRelocTable.FindIndex(aStopNS);
349 if (anIndx == 0)
350 anIndx = theRelocTable.Add(aStopNS);
351 } else
352 anIndx = 0;
353 theTarget << anIndx;
354
355//5. keep Index
356 theTarget << aName.Index();
357
358//6. keep context label
359 TCollection_AsciiString entry(NULL_ENTRY);
360 if(!aName.ContextLabel().IsNull())
361 TDF_Tool::Entry(aName.ContextLabel(), entry);
362 theTarget << entry;
7dcac1df 363
364//7. keep Orientation
365 theTarget << (Standard_Integer)aName.Orientation();
366
7fd59977 367}