492ef6db0c1faa6d240a6bde95817af6a47be3bf
[occt.git] / src / BinDrivers / BinDrivers_DocumentStorageDriver.cxx
1 // Created on: 2002-10-29
2 // Created by: Michael SAZONOV
3 // Copyright (c) 2002-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <BinDrivers_DocumentStorageDriver.hxx>
17
18 #include <BinDrivers.hxx>
19 #include <BinLDrivers_DocumentSection.hxx>
20 #include <BinMDF_ADriver.hxx>
21 #include <BinMDF_ADriverTable.hxx>
22 #include <BinMNaming_NamedShapeDriver.hxx>
23 #include <Message_Messenger.hxx>
24 #include <Standard_ErrorHandler.hxx>
25 #include <Standard_NotImplemented.hxx>
26 #include <Standard_Type.hxx>
27 #include <TCollection_AsciiString.hxx>
28 #include <TNaming_NamedShape.hxx>
29
30 IMPLEMENT_STANDARD_RTTIEXT(BinDrivers_DocumentStorageDriver,BinLDrivers_DocumentStorageDriver)
31
32 //=======================================================================
33 //function : BinDrivers_DocumentStorageDriver
34 //purpose  : Constructor
35 //=======================================================================
36 BinDrivers_DocumentStorageDriver::BinDrivers_DocumentStorageDriver ()
37 {
38 }
39
40 //=======================================================================
41 //function : AttributeDrivers
42 //purpose  :
43 //=======================================================================
44
45 Handle(BinMDF_ADriverTable) BinDrivers_DocumentStorageDriver::AttributeDrivers
46        (const Handle(Message_Messenger)& theMessageDriver)
47 {
48   return BinDrivers::AttributeDrivers (theMessageDriver);
49 }
50
51 //=======================================================================
52 //function : IsWithTriangles
53 //purpose  :
54 //=======================================================================
55 Standard_Boolean BinDrivers_DocumentStorageDriver::IsWithTriangles() const
56 {
57   if (myDrivers.IsNull())
58   {
59     return Standard_False;
60   }
61
62   Handle(BinMDF_ADriver) aDriver;
63   myDrivers->GetDriver (STANDARD_TYPE(TNaming_NamedShape), aDriver);
64   Handle(BinMNaming_NamedShapeDriver) aShapesDriver = Handle(BinMNaming_NamedShapeDriver)::DownCast(aDriver);
65   return !aShapesDriver.IsNull()
66        && aShapesDriver->IsWithTriangles();
67 }
68
69 //=======================================================================
70 //function : SetWithTriangles
71 //purpose  :
72 //=======================================================================
73 void BinDrivers_DocumentStorageDriver::SetWithTriangles (const Handle(Message_Messenger)& theMessageDriver,
74                                                          const Standard_Boolean theWithTriangulation)
75 {
76   if (myDrivers.IsNull())
77   {
78     myDrivers = AttributeDrivers (theMessageDriver);
79   }
80   if (myDrivers.IsNull())
81   {
82     return;
83   }
84
85   Handle(BinMDF_ADriver) aDriver;
86   myDrivers->GetDriver (STANDARD_TYPE(TNaming_NamedShape), aDriver);
87   Handle(BinMNaming_NamedShapeDriver) aShapesDriver = Handle(BinMNaming_NamedShapeDriver)::DownCast(aDriver);
88   if (aShapesDriver.IsNull())
89   {
90     throw Standard_NotImplemented("Internal Error - TNaming_NamedShape is not found!");
91   }
92
93   aShapesDriver->SetWithTriangles (theWithTriangulation);
94 }
95
96 //=======================================================================
97 //function : IsWithNormals
98 //purpose  :
99 //=======================================================================
100 Standard_Boolean BinDrivers_DocumentStorageDriver::IsWithNormals() const
101 {
102   if (myDrivers.IsNull())
103   {
104     return Standard_False;
105   }
106
107   Handle(BinMDF_ADriver) aDriver;
108   myDrivers->GetDriver(STANDARD_TYPE(TNaming_NamedShape), aDriver);
109   Handle(BinMNaming_NamedShapeDriver) aShapesDriver = Handle(BinMNaming_NamedShapeDriver)::DownCast(aDriver);
110   return !aShapesDriver.IsNull()
111     && aShapesDriver->IsWithNormals();
112 }
113
114 //=======================================================================
115 //function : SetWithNormals
116 //purpose  :
117 //=======================================================================
118 void BinDrivers_DocumentStorageDriver::SetWithNormals(const Handle(Message_Messenger)& theMessageDriver,
119   const Standard_Boolean theWithNormals)
120 {
121   if (myDrivers.IsNull())
122   {
123     myDrivers = AttributeDrivers(theMessageDriver);
124   }
125   if (myDrivers.IsNull())
126   {
127     return;
128   }
129
130   Handle(BinMDF_ADriver) aDriver;
131   myDrivers->GetDriver(STANDARD_TYPE(TNaming_NamedShape), aDriver);
132   Handle(BinMNaming_NamedShapeDriver) aShapesDriver = Handle(BinMNaming_NamedShapeDriver)::DownCast(aDriver);
133   if (aShapesDriver.IsNull())
134   {
135     throw Standard_NotImplemented("Internal Error - TNaming_NamedShape is not found!");
136   }
137
138   aShapesDriver->SetWithNormals(theWithNormals);
139 }
140
141 //=======================================================================
142 //function : WriteShapeSection
143 //purpose  : Implements WriteShapeSection
144 //=======================================================================
145 void BinDrivers_DocumentStorageDriver::WriteShapeSection
146                                (BinLDrivers_DocumentSection&   theSection,
147                                 Standard_OStream&              theOS,
148                                 const TDocStd_FormatVersion    theDocVer,
149                                 const Message_ProgressRange&   theRange)
150 {
151   const Standard_Size aShapesSectionOffset = (Standard_Size) theOS.tellp();
152   
153   Handle(BinMDF_ADriver) aDriver;
154   if (myDrivers->GetDriver(STANDARD_TYPE(TNaming_NamedShape), aDriver))
155   {
156     try { 
157       OCC_CATCH_SIGNALS
158       Handle(BinMNaming_NamedShapeDriver) aNamedShapeDriver =
159         Handle(BinMNaming_NamedShapeDriver)::DownCast (aDriver);
160       aNamedShapeDriver->WriteShapeSection (theOS, theDocVer, theRange);
161     }
162     catch(Standard_Failure const& anException) {
163       TCollection_ExtendedString anErrorStr ("BinDrivers_DocumentStorageDriver, Shape Section :");
164       myMsgDriver->Send (anErrorStr  + anException.GetMessageString(), Message_Fail);
165     }
166   }
167    
168   // Write the section info in the TOC.
169   theSection.Write (theOS, aShapesSectionOffset, theDocVer);
170 }