1 // Created on: 2017-03-28
2 // Created by: Sergey NIKONOV
3 // Copyright (c) 2000-2017 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.
16 #include <DDocStd.hxx>
18 #include <FSD_File.hxx>
19 #include <FSD_CmpFile.hxx>
20 #include <FSD_BinaryFile.hxx>
21 #include <BRep_Builder.hxx>
22 #include <NCollection_Handle.hxx>
23 #include <TCollection_AsciiString.hxx>
24 #include <TopTools_SequenceOfShape.hxx>
25 #include <Storage_BaseDriver.hxx>
26 #include <StdStorage.hxx>
27 #include <StdStorage_Data.hxx>
28 #include <StdStorage_HeaderData.hxx>
29 #include <StdStorage_RootData.hxx>
30 #include <StdStorage_TypeData.hxx>
31 #include <ShapePersistent_TopoDS.hxx>
33 //==========================================================
35 //==========================================================
37 static void DDocStd_StorageErrorMessage (Draw_Interpretor& theDI, const Storage_Error theStatus)
42 case Storage_VSOpenError:
43 theDI << "Storage error: failed to open the stream";
45 case Storage_VSModeError:
46 theDI << "Storage error: the stream is opened with a wrong mode for operation ";
48 case Storage_VSCloseError:
49 theDI << "Storage error: failed to closing the stream";
51 case Storage_VSAlreadyOpen:
52 theDI << "Storage error: stream is already opened";
54 case Storage_VSNotOpen:
55 theDI << "Storage error: stream not opened";
57 case Storage_VSSectionNotFound:
58 theDI << "Storage error: the section is not found";
60 case Storage_VSWriteError:
61 theDI << "Storage error: error during writing";
63 case Storage_VSFormatError:
64 theDI << "Storage error: wrong format error occured while reading";
66 case Storage_VSUnknownType:
67 theDI << "Storage error: try to read an unknown type";
69 case Storage_VSTypeMismatch:
70 theDI << "Storage error: try to read a wrong primitive type (read a char while expecting a real)";
72 case Storage_VSInternalError:
73 theDI << "Storage error: internal error";
75 case Storage_VSExtCharParityError:
76 theDI << "Storage error: parity error";
79 theDI << "Storage error: unknown error code";
84 //=======================================================================
85 //function : DDocStd_ShapeSchema_Write
86 //=======================================================================
88 static Standard_Integer DDocStd_fsdwrite(Draw_Interpretor& theDI,
89 Standard_Integer theArgNb,
94 theDI << "Usage : fsdwrite shapes filename [gen | cmp | bin]\n";
95 theDI << " Arguments:\n";
96 theDI << " shapes : list os shape names\n";
97 theDI << " filename : output file name\n";
98 theDI << " Storage driver:\n";
99 theDI << " gen : FSD_File driver (default)\n";
100 theDI << " cmp : FSD_CmpFile driver\n";
101 theDI << " bin : FSD_BinaryFile driver\n";
105 Handle(Storage_BaseDriver) aFileDriver(new FSD_File);
107 Standard_Boolean hasStorageDriver = Standard_False;
108 Standard_Integer iArgN = theArgNb - 1;
110 if (strncmp(theArgs[iArgN], "gen", 3) == 0)
112 aFileDriver = new FSD_File;
113 hasStorageDriver = Standard_True;
115 else if (strncmp(theArgs[iArgN], "cmp", 3) == 0)
117 aFileDriver = new FSD_CmpFile;
118 hasStorageDriver = Standard_True;
120 else if (strncmp(theArgs[iArgN], "bin", 3) == 0)
122 aFileDriver = new FSD_BinaryFile;
123 hasStorageDriver = Standard_True;
126 if (hasStorageDriver) --iArgN;
128 Storage_Error aStatus = aFileDriver->Open(theArgs[iArgN], Storage_VSWrite);
129 if (aStatus != Storage_VSOk) {
130 theDI << "Error: cannot open file '" << "' for writing (" << aStatus << ")\n";
131 DDocStd_StorageErrorMessage (theDI, aStatus);
135 TopTools_SequenceOfShape aShapes;
136 NCollection_DataMap<TCollection_AsciiString, Standard_Integer> aShapeNames;
137 for (Standard_Integer i = 1; i < iArgN; ++i)
139 TopoDS_Shape aShape = DBRep::Get(theArgs[i]);
142 theDI << "Error : null shape " << theArgs[i] << "\n";
145 aShapes.Append(aShape);
146 if (aShapeNames.IsBound(theArgs[i]))
147 aShapeNames.ChangeFind(theArgs[i]) += 1;
149 aShapeNames.Bind(theArgs[i], 1);
152 Handle(StdStorage_Data) aData = new StdStorage_Data;
154 aData->HeaderData()->SetApplicationName(TCollection_ExtendedString("DDocStd_ShapeSchema_Write"));
156 StdObjMgt_TransientPersistentMap aMap;
157 for (Standard_Integer i = 1; i <= aShapes.Length(); ++i)
159 TopoDS_Shape aShape = aShapes.Value(i);
161 Handle(ShapePersistent_TopoDS::HShape) aPShape =
162 ShapePersistent_TopoDS::Translate(aShape, aMap, ShapePersistent_WithTriangle);
163 if (aPShape.IsNull())
165 theDI << "Error : couldn't translate shape " << theArgs[i] << "\n";
169 TCollection_AsciiString aName = theArgs[i];
170 if (aShapeNames.IsBound(aName))
172 Standard_Integer n = aShapeNames.Find(theArgs[i]);
180 Handle(StdStorage_Root) aRoot = new StdStorage_Root(aName, aPShape);
181 aData->RootData()->AddRoot(aRoot);
184 Storage_Error anError = StdStorage::Write(aFileDriver, aData);
185 aFileDriver->Close();
186 DDocStd_StorageErrorMessage(theDI, anError);
191 //=======================================================================
192 //function : DDocStd_ShapeSchema_Read
193 //=======================================================================
195 static Standard_Integer DDocStd_fsdread(Draw_Interpretor& theDI,
196 Standard_Integer theArgNb,
197 const char** theArgs)
201 theDI << "Usage : fsdread filename shape\n";
202 theDI << " Arguments:\n";
203 theDI << " filename : input file name\n";
204 theDI << " shape : name of an output shape, or \n";
205 theDI << " reserved name = 'restore_with_names';\n";
206 theDI << " by default root shapes will be put into \n";
207 theDI << " a compound in case of multiple roots in the file;\n";
208 theDI << " if name = restore_with_names, the shapes will be put to\n";
209 theDI << " separate variables according kept names.\n";
212 Standard_Boolean rflag(Standard_False);
213 if (strcmp(theArgs[2],"restore_with_names") == 0)
214 rflag = Standard_True;
215 Handle(StdStorage_Data) aData;
216 Storage_Error anError = StdStorage::Read(TCollection_AsciiString(theArgs[1]), aData);
217 if (anError != Storage_VSOk)
219 DDocStd_StorageErrorMessage(theDI, anError);
223 TopTools_SequenceOfShape aShapes;
225 Handle(StdStorage_TypeData) aTypeData = aData->TypeData();
226 Handle(StdStorage_RootData) aRootData = aData->RootData();
227 Handle(StdStorage_HSequenceOfRoots) aRoots = aRootData->Roots();
228 if (!aRoots.IsNull())
230 for (StdStorage_HSequenceOfRoots::Iterator anIt(*aRoots); anIt.More(); anIt.Next())
232 Handle(StdStorage_Root)& aRoot = anIt.ChangeValue();
233 Handle(StdObjMgt_Persistent) aPObject = aRoot->Object();
234 if (!aPObject.IsNull())
236 Handle(ShapePersistent_TopoDS::HShape) aHShape = Handle(ShapePersistent_TopoDS::HShape)::DownCast(aPObject);
237 if (aHShape) // shapes are expected
239 TopoDS_Shape aShape = aHShape->Import();
241 if(!aRoot->Name().IsEmpty())
242 DBRep::Set(aRoot->Name().ToCString(), aShape);
244 TCollection_AsciiString aNam("name_");
245 aNam += aRoot->Reference();
246 DBRep::Set(aNam.ToCString(), aShape);
249 Standard_Integer indx = aRoot->Reference();
250 theDI << "Ref indx = " <<indx << " Name = " << aRoot->Name().ToCString() << "\n";
253 aShapes.Append(aShape);
259 theDI << "Info : " << aTypeData->NumberOfTypes() << " type(s)\n";
260 theDI << " " << aRoots->Length() << " root(s)\n";
263 if (aShapes.Length() > 1)
265 theDI << " " << aShapes.Length() << " shape(s) translated\n";
269 for (Standard_Integer i = 1; i <= aShapes.Length(); ++i)
270 aB.Add(aC, aShapes.Value(i));
271 DBRep::Set(theArgs[2], aC);
274 DBRep::Set(theArgs[2], aShapes.First());
279 //=======================================================================
280 //function : ShapeSchemaCommands
281 //purpose : registers shape schema related commands in Draw interpreter
282 //=======================================================================
284 void DDocStd::ShapeSchemaCommands(Draw_Interpretor& theCommands)
286 static Standard_Boolean done = Standard_False;
288 done = Standard_True;
290 const char* g = "Shape persistence commands";
292 theCommands.Add("fsdwrite",
293 "fsdrite shape filename [driver]",
294 __FILE__, DDocStd_fsdwrite, g);
296 theCommands.Add("fsdread",
297 "fsdread filename shape [name | or key 'restore_with_names']",
298 __FILE__, DDocStd_fsdread, g);