1 // Created on: 2004-04-08
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 <BinMNaming_NamedShapeDriver.hxx>
18 #include <BinObjMgt_Persistent.hxx>
19 #include <BinTools_LocationSet.hxx>
20 #include <BinTools_ShapeSet.hxx>
21 #include <Message_Messenger.hxx>
22 #include <Standard_DomainError.hxx>
23 #include <Standard_Type.hxx>
24 #include <TCollection_AsciiString.hxx>
25 #include <TDF_Attribute.hxx>
26 #include <TDF_Label.hxx>
27 #include <TNaming_Builder.hxx>
28 #include <TNaming_Evolution.hxx>
29 #include <TNaming_Iterator.hxx>
30 #include <TNaming_NamedShape.hxx>
31 #include <TopAbs_Orientation.hxx>
32 #include <TopoDS_Shape.hxx>
34 IMPLEMENT_STANDARD_RTTIEXT(BinMNaming_NamedShapeDriver,BinMDF_ADriver)
36 #define SHAPESET "SHAPE_SECTION"
37 #define FORMAT_NUMBER 3
38 //=======================================================================
39 static Standard_Character EvolutionToChar(const TNaming_Evolution theEvol)
42 case TNaming_PRIMITIVE : return 'P';
43 case TNaming_GENERATED : return 'G';
44 case TNaming_MODIFY : return 'M';
45 case TNaming_DELETE : return 'D';
46 case TNaming_SELECTED : return 'S';
47 case TNaming_REPLACE : return 'M'; // for compatibility case TNaming_REPLACE : return 'R';
49 throw Standard_DomainError("TNaming_Evolution:: Evolution Unknown");
53 //=======================================================================
54 static TNaming_Evolution EvolutionToEnum(const Standard_Character theEvol)
57 case 'P': return TNaming_PRIMITIVE;
58 case 'G': return TNaming_GENERATED;
59 case 'M': return TNaming_MODIFY;
60 case 'D': return TNaming_DELETE;
61 case 'S': return TNaming_SELECTED;
62 case 'R': return TNaming_MODIFY; //for compatibility //TNaming_REPLACE;
64 throw Standard_DomainError("TNaming_Evolution:: Evolution Unknown");
67 //=======================================================================
68 static Standard_Character OrientationToChar(const TopAbs_Orientation theOrient)
71 case TopAbs_FORWARD : return 'F';
72 case TopAbs_REVERSED : return 'R';
73 case TopAbs_INTERNAL : return 'I';
74 case TopAbs_EXTERNAL : return 'E';
76 throw Standard_DomainError("TopAbs_Orientation:: Orientation Unknown");
79 //=======================================================================
80 static TopAbs_Orientation CharToOrientation(const Standard_Character theCharOrient)
82 switch(theCharOrient) {
83 case 'F': return TopAbs_FORWARD;
84 case 'R': return TopAbs_REVERSED;
85 case 'I': return TopAbs_INTERNAL;
86 case 'E': return TopAbs_EXTERNAL;
88 throw Standard_DomainError("TopAbs_Orientation:: Orientation Unknown");
92 //=======================================================================
93 static void TranslateTo (const TopoDS_Shape& theShape,
94 BinObjMgt_Persistent& theResult,
95 BinTools_ShapeSet& theShapeSet)
97 // Check for empty shape
98 if (theShape.IsNull()) {
99 theResult.PutInteger(-1);
100 theResult.PutInteger(-1);
101 theResult.PutInteger(-1);
104 // Add to shape set both TShape and Location contained in <theShape>
105 const Standard_Integer aTShapeID = theShapeSet.Add (theShape);
106 const Standard_Integer aLocID =
107 theShapeSet.Locations().Index (theShape.Location());
109 // Fill theResult with shape parameters: TShape ID, Location, Orientation
110 theResult << aTShapeID;
112 theResult << OrientationToChar(theShape.Orientation());
114 //=======================================================================
115 static int TranslateFrom (const BinObjMgt_Persistent& theSource,
116 TopoDS_Shape& theResult,
117 BinTools_ShapeSet& theShapeSet)
119 Standard_Integer aShapeID, aLocID;
120 Standard_Character aCharOrient;
121 Standard_Boolean Ok = theSource >> aShapeID; //TShapeID;
123 // Read TShape and Orientation
124 if (aShapeID <= 0 || aShapeID > theShapeSet.NbShapes())
126 Ok = theSource >> aLocID;
128 Ok = theSource >> aCharOrient;
130 TopAbs_Orientation anOrient = CharToOrientation(aCharOrient);
132 theResult.TShape (theShapeSet.Shape(aShapeID).TShape());//TShape
133 theResult.Location (theShapeSet.Locations().Location (aLocID)); //Location
134 theResult.Orientation (anOrient);//Orientation
138 //=======================================================================
139 //function : BinMNaming_NamedShapeDriver
140 //purpose : Constructor
141 //=======================================================================
143 BinMNaming_NamedShapeDriver::BinMNaming_NamedShapeDriver
144 (const Handle(Message_Messenger)& theMsgDriver)
145 : BinMDF_ADriver (theMsgDriver, STANDARD_TYPE(TNaming_NamedShape)->Name()), myShapeSet(Standard_False),myFormatNb(FORMAT_NUMBER)
149 //=======================================================================
150 //function : NewEmpty
152 //=======================================================================
154 Handle(TDF_Attribute) BinMNaming_NamedShapeDriver::NewEmpty() const
156 return new TNaming_NamedShape();
159 //=======================================================================
161 //purpose : persistent => transient (retrieve)
162 //=======================================================================
164 Standard_Boolean BinMNaming_NamedShapeDriver::Paste
165 (const BinObjMgt_Persistent& theSource,
166 const Handle(TDF_Attribute)& theTarget,
167 BinObjMgt_RRelocationTable& ) const
169 Handle(TNaming_NamedShape) aTAtt= Handle(TNaming_NamedShape)::DownCast(theTarget);
170 Standard_Integer aNbShapes;
171 theSource >> aNbShapes;
172 TDF_Label aLabel = theTarget->Label ();
173 TNaming_Builder aBuilder (aLabel);
174 Standard_Integer aVer;
175 Standard_Boolean ok = theSource >> aVer;
176 if(!ok) return Standard_False;
177 aTAtt->SetVersion(aVer); //Version
178 Standard_Character aCharEvol;
179 ok = theSource >> aCharEvol;
180 if(!ok) return Standard_False;
181 TNaming_Evolution anEvol = EvolutionToEnum(aCharEvol); //Evolution
182 aTAtt->SetVersion(anEvol);
184 BinTools_ShapeSet& aShapeSet = (BinTools_ShapeSet&) myShapeSet;
186 NCollection_List<TopoDS_Shape> anOldShapes, aNewShapes;
187 for (Standard_Integer i = 1; i <= aNbShapes; i++)
189 TopoDS_Shape anOldShape, aNewShape;
191 if (anEvol != TNaming_PRIMITIVE)
192 if (TranslateFrom (theSource, anOldShape, aShapeSet)) return Standard_False;
194 if (anEvol != TNaming_DELETE)
195 if (TranslateFrom (theSource, aNewShape, aShapeSet)) return Standard_False;
197 // Here we add shapes in reverse order because TNaming_Builder also adds them in reverse order.
198 anOldShapes.Prepend (anOldShape);
199 aNewShapes.Prepend (aNewShape);
202 for (NCollection_List<TopoDS_Shape>::Iterator anOldIt (anOldShapes), aNewIt (aNewShapes);
203 anOldIt.More() && aNewIt.More();
204 anOldIt.Next(), aNewIt.Next())
208 case TNaming_PRIMITIVE:
209 aBuilder.Generated (aNewIt.Value ());
211 case TNaming_GENERATED:
212 aBuilder.Generated (anOldIt.Value(), aNewIt.Value());
215 aBuilder.Modify (anOldIt.Value(), aNewIt.Value());
218 aBuilder.Delete (anOldIt.Value());
220 case TNaming_SELECTED:
221 aBuilder.Select (aNewIt.Value(), anOldIt.Value());
223 case TNaming_REPLACE:
224 aBuilder.Modify (anOldIt.Value(), aNewIt.Value()); // for compatibility aBuilder.Replace(anOldShape, aNewShape);
227 throw Standard_DomainError("TNaming_Evolution:: Evolution Unknown");
230 return Standard_True;
233 //=======================================================================
235 //purpose : transient => persistent (store)
236 //=======================================================================
238 void BinMNaming_NamedShapeDriver::Paste (const Handle(TDF_Attribute)& theSource,
239 BinObjMgt_Persistent& theTarget,
240 BinObjMgt_SRelocationTable& ) const
242 Handle(TNaming_NamedShape) aSAtt= Handle(TNaming_NamedShape)::DownCast(theSource);
244 //--------------------------------------------------------------
245 Standard_Integer NbShapes = 0;
246 for (TNaming_Iterator SItr (aSAtt); SItr.More (); SItr.Next ()) NbShapes++;
247 //--------------------------------------------------------------
249 BinTools_ShapeSet& aShapeSet = (BinTools_ShapeSet&) myShapeSet;
250 TNaming_Evolution anEvol = aSAtt->Evolution();
252 theTarget << NbShapes;
253 theTarget << aSAtt->Version();
254 theTarget << EvolutionToChar(anEvol);
257 Standard_Integer i = 1;
258 for (TNaming_Iterator SIterator(aSAtt) ;SIterator.More(); SIterator.Next()) {
259 const TopoDS_Shape& OldShape = SIterator.OldShape();
260 const TopoDS_Shape& NewShape = SIterator.NewShape();
262 if ( anEvol != TNaming_PRIMITIVE )
263 TranslateTo (OldShape, theTarget, aShapeSet);
265 if (anEvol != TNaming_DELETE)
266 TranslateTo (NewShape, theTarget, aShapeSet);
274 //=======================================================================
275 //function : WriteShapeSection
277 //=======================================================================
279 void BinMNaming_NamedShapeDriver::WriteShapeSection (Standard_OStream& theOS,
280 const Handle(Message_ProgressIndicator)& theProgress)
283 myShapeSet.SetFormatNb(myFormatNb);
284 myShapeSet.Write (theOS, theProgress);
288 //=======================================================================
291 //=======================================================================
293 void BinMNaming_NamedShapeDriver::Clear()
298 //=======================================================================
299 //function : ReadShapeSection
301 //=======================================================================
303 void BinMNaming_NamedShapeDriver::ReadShapeSection (Standard_IStream& theIS,
304 const Handle(Message_ProgressIndicator)& theProgress)
306 // check section title string; note that some versions of OCCT (up to 6.3.1)
307 // might avoid writing shape section if it is empty
308 std::streamoff aPos = theIS.tellg();
309 TCollection_AsciiString aSectionTitle;
310 theIS >> aSectionTitle;
311 if(aSectionTitle.Length() > 0 && aSectionTitle == SHAPESET) {
313 myShapeSet.Read (theIS, theProgress);
314 SetFormatNb(myShapeSet.FormatNb());
317 theIS.seekg(aPos); // no shape section is present, try to return to initial point