0031687: Draw Harness, ViewerTest - extend command vrenderparams with option updating...
[occt.git] / src / XCAFDoc / XCAFDoc_NoteBinData.cxx
1 // Copyright (c) 2017-2018 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 #include <XCAFDoc_NoteBinData.hxx>
15
16 #include <OSD_File.hxx>
17 #include <Standard_GUID.hxx>
18 #include <TDF_Label.hxx>
19
20 IMPLEMENT_STANDARD_RTTIEXT(XCAFDoc_NoteBinData, XCAFDoc_Note)
21
22 // =======================================================================
23 // function : GetID
24 // purpose  :
25 // =======================================================================
26 const Standard_GUID&
27 XCAFDoc_NoteBinData::GetID()
28 {
29   static Standard_GUID s_ID("E9055501-F0FC-4864-BE4B-284FDA7DDEAC");
30   return s_ID;
31 }
32
33 // =======================================================================
34 // function : Get
35 // purpose  :
36 // =======================================================================
37 Handle(XCAFDoc_NoteBinData)
38 XCAFDoc_NoteBinData::Get(const TDF_Label& theLabel)
39 {
40   Handle(XCAFDoc_NoteBinData) aThis;
41   theLabel.FindAttribute(XCAFDoc_NoteBinData::GetID(), aThis);
42   return aThis;
43 }
44
45 // =======================================================================
46 // function : Set
47 // purpose  :
48 // =======================================================================
49 Handle(XCAFDoc_NoteBinData)
50 XCAFDoc_NoteBinData::Set(const TDF_Label&                  theLabel,
51                          const TCollection_ExtendedString& theUserName,
52                          const TCollection_ExtendedString& theTimeStamp,
53                          const TCollection_ExtendedString& theTitle,
54                          const TCollection_AsciiString&    theMIMEtype,
55                          OSD_File&                         theFile)
56 {
57   Handle(XCAFDoc_NoteBinData) aNoteBinData;
58   if (!theLabel.IsNull() && !theLabel.FindAttribute(XCAFDoc_NoteBinData::GetID(), aNoteBinData))
59   {
60     aNoteBinData = new XCAFDoc_NoteBinData();
61     aNoteBinData->XCAFDoc_Note::Set(theUserName, theTimeStamp);
62     if (aNoteBinData->Set(theTitle, theMIMEtype, theFile))
63       theLabel.AddAttribute(aNoteBinData);
64     else
65       aNoteBinData.Nullify();
66   }
67   return aNoteBinData;
68 }
69
70 // =======================================================================
71 // function : Set
72 // purpose  :
73 // =======================================================================
74 Handle(XCAFDoc_NoteBinData)
75 XCAFDoc_NoteBinData::Set(const TDF_Label&                     theLabel,
76                          const TCollection_ExtendedString&    theUserName,
77                          const TCollection_ExtendedString&    theTimeStamp,
78                          const TCollection_ExtendedString&    theTitle,
79                          const TCollection_AsciiString&       theMIMEtype,
80                          const Handle(TColStd_HArray1OfByte)& theData)
81 {
82   Handle(XCAFDoc_NoteBinData) aNoteBinData;
83   if (!theLabel.IsNull() && !theLabel.FindAttribute(XCAFDoc_NoteBinData::GetID(), aNoteBinData))
84   {
85     aNoteBinData = new XCAFDoc_NoteBinData();
86     aNoteBinData->XCAFDoc_Note::Set(theUserName, theTimeStamp);
87     aNoteBinData->Set(theTitle, theMIMEtype, theData);
88     theLabel.AddAttribute(aNoteBinData);
89   }
90   return aNoteBinData;
91 }
92
93 // =======================================================================
94 // function : XCAFDoc_NoteBinData
95 // purpose  :
96 // =======================================================================
97 XCAFDoc_NoteBinData::XCAFDoc_NoteBinData()
98 {
99 }
100
101 // =======================================================================
102 // function : Set
103 // purpose  :
104 // =======================================================================
105 Standard_Boolean
106 XCAFDoc_NoteBinData::Set(const TCollection_ExtendedString& theTitle,
107                          const TCollection_AsciiString&    theMIMEtype,
108                          OSD_File&                         theFile)
109 {
110   if (!theFile.IsOpen() || !theFile.IsReadable())
111     return Standard_False;
112
113   Backup();
114
115   if (theFile.Size() > (Standard_Size)IntegerLast())
116     return Standard_False;
117
118   myData.reset(new TColStd_HArray1OfByte(1, (Standard_Integer)theFile.Size()));
119   Standard_Integer nbReadBytes = 0;
120   theFile.Read((Standard_Address)&myData->First(), myData->Length(), nbReadBytes);
121   if (nbReadBytes < myData->Length())
122     return Standard_False;
123
124   myTitle = theTitle;
125   myMIMEtype = theMIMEtype;
126
127   return Standard_True;
128 }
129
130 // =======================================================================
131 // function : Set
132 // purpose  :
133 // =======================================================================
134 void
135 XCAFDoc_NoteBinData::Set(const TCollection_ExtendedString&    theTitle, 
136                          const TCollection_AsciiString&       theMIMEtype,
137                          const Handle(TColStd_HArray1OfByte)& theData)
138 {
139   Backup();
140
141   myData = theData;
142   myTitle = theTitle;
143   myMIMEtype = theMIMEtype;
144 }
145
146 // =======================================================================
147 // function : ID
148 // purpose  :
149 // =======================================================================
150 const
151 Standard_GUID& XCAFDoc_NoteBinData::ID() const
152 {
153   return GetID();
154 }
155
156 // =======================================================================
157 // function : NewEmpty
158 // purpose  :
159 // =======================================================================
160 Handle(TDF_Attribute)
161 XCAFDoc_NoteBinData::NewEmpty() const
162 {
163   return new XCAFDoc_NoteBinData();
164 }
165
166 // =======================================================================
167 // function : Restore
168 // purpose  :
169 // =======================================================================
170 void
171 XCAFDoc_NoteBinData::Restore(const Handle(TDF_Attribute)& theAttr)
172 {
173   XCAFDoc_Note::Restore(theAttr);
174
175   Handle(XCAFDoc_NoteBinData) aMine = Handle(XCAFDoc_NoteBinData)::DownCast(theAttr);
176   if (!aMine.IsNull())
177   {
178     myTitle = aMine->myTitle;
179     myMIMEtype = aMine->myMIMEtype;
180     myData = aMine->myData;
181   }
182 }
183
184 // =======================================================================
185 // function : Paste
186 // purpose  :
187 // =======================================================================
188 void
189 XCAFDoc_NoteBinData::Paste(const Handle(TDF_Attribute)&       theAttrInto,
190                            const Handle(TDF_RelocationTable)& theRT) const
191 {
192   XCAFDoc_Note::Paste(theAttrInto, theRT);
193
194   Handle(XCAFDoc_NoteBinData) aMine = Handle(XCAFDoc_NoteBinData)::DownCast(theAttrInto);
195   if (!aMine.IsNull())
196     aMine->Set(myTitle, myMIMEtype, myData);
197 }
198
199 // =======================================================================
200 // function : Dump
201 // purpose  :
202 // =======================================================================
203 Standard_OStream&
204 XCAFDoc_NoteBinData::Dump(Standard_OStream& theOS) const
205 {
206   XCAFDoc_Note::Dump(theOS);
207   theOS << "\n"
208     << "Title : " << (!myTitle.IsEmpty() ? myMIMEtype : "<untitled>") << "\n"
209     << "MIME type : " << (!myMIMEtype.IsEmpty() ? myMIMEtype : "<none>") << "\n"
210     << "Size : " << Size() << " bytes" << "\n"
211     ;
212   if (!myData.IsNull())
213   {
214     for (Standard_Integer i = myData->Lower(); i <= myData->Upper(); ++i)
215       theOS << myData->Value(i);
216   }
217   return theOS;
218 }