From bc1c59d0a0c502a49914ebb2f433a394206b79c3 Mon Sep 17 00:00:00 2001 From: snn Date: Wed, 15 Feb 2017 14:09:53 +0300 Subject: [PATCH] XCAF: binary notes with array of bytes --- .../BinMXCAFDoc_NoteBinDataDriver.cxx | 23 +++-- src/XCAFDoc/XCAFDoc_NoteBinData.cxx | 90 +++++++++++++------ src/XCAFDoc/XCAFDoc_NoteBinData.hxx | 39 +++++--- src/XCAFDoc/XCAFDoc_NotesTool.cxx | 20 ++++- src/XCAFDoc/XCAFDoc_NotesTool.hxx | 11 ++- .../XmlMXCAFDoc_NoteBinDataDriver.cxx | 41 +++++++-- 6 files changed, 165 insertions(+), 59 deletions(-) diff --git a/src/BinMXCAFDoc/BinMXCAFDoc_NoteBinDataDriver.cxx b/src/BinMXCAFDoc/BinMXCAFDoc_NoteBinDataDriver.cxx index 707bb5d9ce..ee73b46636 100644 --- a/src/BinMXCAFDoc/BinMXCAFDoc_NoteBinDataDriver.cxx +++ b/src/BinMXCAFDoc/BinMXCAFDoc_NoteBinDataDriver.cxx @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -58,11 +59,19 @@ Standard_Boolean BinMXCAFDoc_NoteBinDataDriver::Paste(const BinObjMgt_Persistent return Standard_False; TCollection_ExtendedString aTitle; - TCollection_AsciiString aData, aMIMEtype; - if (!(theSource >> aTitle >> aData >> aMIMEtype)) + TCollection_AsciiString aMIMEtype; + Standard_Integer nbSize; + if (!(theSource >> aTitle >> aMIMEtype >> nbSize)) return Standard_False; - aNote->Set(aTitle, aData, aMIMEtype); + Handle(TColStd_HArray1OfByte) aData; + if (nbSize > 0) + { + aData.reset(new TColStd_HArray1OfByte(1, nbSize)); + theSource.GetByteArray(&aData->ChangeFirst(), nbSize); + } + + aNote->Set(aTitle, aMIMEtype, aData); return Standard_True; } @@ -80,10 +89,8 @@ void BinMXCAFDoc_NoteBinDataDriver::Paste(const Handle(TDF_Attribute)& theSource Handle(XCAFDoc_NoteBinData) aNote = Handle(XCAFDoc_NoteBinData)::DownCast(theSource); if (!aNote.IsNull()) { - theTarget - << aNote->Title() - << aNote->MIMEtype() - << aNote->Data() - ; + theTarget << aNote->Title() << aNote->MIMEtype() << aNote->Size(); + if (aNote->Size() > 0) + theTarget.PutByteArray(&aNote->Data()->ChangeFirst(), aNote->Size()); } } diff --git a/src/XCAFDoc/XCAFDoc_NoteBinData.cxx b/src/XCAFDoc/XCAFDoc_NoteBinData.cxx index f45c546aa1..c772c42603 100644 --- a/src/XCAFDoc/XCAFDoc_NoteBinData.cxx +++ b/src/XCAFDoc/XCAFDoc_NoteBinData.cxx @@ -15,7 +15,6 @@ #include #include -#include #include #include @@ -33,19 +32,41 @@ Standard_Boolean XCAFDoc_NoteBinData::IsMine(const TDF_Label& theLabel) return (!theLabel.IsNull() && theLabel.FindAttribute(XCAFDoc_NoteBinData::GetID(), anAttr)); } -Handle(XCAFDoc_NoteBinData) XCAFDoc_NoteBinData::Set(const TDF_Label& theLabel, - const TCollection_ExtendedString& theUserName, - const TCollection_ExtendedString& theTimeStamp, - const TCollection_ExtendedString& theTitle, - OSD_File& theFile, - const TCollection_AsciiString& theMIMEtype) +Handle(XCAFDoc_NoteBinData) +XCAFDoc_NoteBinData::Set(const TDF_Label& theLabel, + const TCollection_ExtendedString& theUserName, + const TCollection_ExtendedString& theTimeStamp, + const TCollection_ExtendedString& theTitle, + const TCollection_AsciiString& theMIMEtype, + OSD_File& theFile) { Handle(XCAFDoc_NoteBinData) aNoteBinData; if (!theLabel.IsNull() && !theLabel.FindAttribute(XCAFDoc_NoteBinData::GetID(), aNoteBinData)) { aNoteBinData = new XCAFDoc_NoteBinData(); aNoteBinData->XCAFDoc_Note::Set(theUserName, theTimeStamp); - aNoteBinData->Set(theTitle, theFile, theMIMEtype); + if (aNoteBinData->Set(theTitle, theMIMEtype, theFile)) + theLabel.AddAttribute(aNoteBinData); + else + aNoteBinData.Nullify(); + } + return aNoteBinData; +} + +Handle(XCAFDoc_NoteBinData) +XCAFDoc_NoteBinData::Set(const TDF_Label& theLabel, + const TCollection_ExtendedString& theUserName, + const TCollection_ExtendedString& theTimeStamp, + const TCollection_ExtendedString& theTitle, + const TCollection_AsciiString& theMIMEtype, + const Handle(TColStd_HArray1OfByte)& theData) +{ + Handle(XCAFDoc_NoteBinData) aNoteBinData; + if (!theLabel.IsNull() && !theLabel.FindAttribute(XCAFDoc_NoteBinData::GetID(), aNoteBinData)) + { + aNoteBinData = new XCAFDoc_NoteBinData(); + aNoteBinData->XCAFDoc_Note::Set(theUserName, theTimeStamp); + aNoteBinData->Set(theTitle, theMIMEtype, theData); theLabel.AddAttribute(aNoteBinData); } return aNoteBinData; @@ -55,25 +76,33 @@ XCAFDoc_NoteBinData::XCAFDoc_NoteBinData() { } -void XCAFDoc_NoteBinData::Set(const TCollection_ExtendedString& theTitle, - OSD_File& theFile, - const TCollection_AsciiString& theMIMEtype) +Standard_Boolean XCAFDoc_NoteBinData::Set(const TCollection_ExtendedString& theTitle, + const TCollection_AsciiString& theMIMEtype, + OSD_File& theFile) { if (!theFile.IsOpen() || !theFile.IsReadable()) - return; + return Standard_False; Backup(); - TCollection_AsciiString myData; - theFile.Read(myData, theFile.Size()); + if (theFile.Size() > IntegerLast()) + return Standard_False; + + myData.reset(new TColStd_HArray1OfByte(1, theFile.Size())); + Standard_Integer nbReadBytes = 0; + theFile.Read((Standard_Address)&myData->First(), myData->Length(), nbReadBytes); + if (nbReadBytes < myData->Length()) + return Standard_False; myTitle = theTitle; myMIMEtype = theMIMEtype; + + return Standard_True; } -void XCAFDoc_NoteBinData::Set(const TCollection_ExtendedString& theTitle, - const TCollection_AsciiString& theData, - const TCollection_AsciiString& theMIMEtype) +void XCAFDoc_NoteBinData::Set(const TCollection_ExtendedString& theTitle, + const TCollection_AsciiString& theMIMEtype, + const Handle(TColStd_HArray1OfByte)& theData) { Backup(); @@ -87,16 +116,21 @@ const TCollection_ExtendedString& XCAFDoc_NoteBinData::Title() const return myTitle; } -const TCollection_AsciiString& XCAFDoc_NoteBinData::Data() const -{ - return myData; -} - const TCollection_AsciiString& XCAFDoc_NoteBinData::MIMEtype() const { return myMIMEtype; } +Standard_Integer XCAFDoc_NoteBinData::Size() const +{ + return (!myData.IsNull() ? myData->Length() : 0); +} + +const Handle(TColStd_HArray1OfByte)& XCAFDoc_NoteBinData::Data() const +{ + return myData; +} + const Standard_GUID& XCAFDoc_NoteBinData::ID() const { return GetID(); @@ -114,9 +148,9 @@ void XCAFDoc_NoteBinData::Restore(const Handle(TDF_Attribute)& theAttr) Handle(XCAFDoc_NoteBinData) aMine = Handle(XCAFDoc_NoteBinData)::DownCast(theAttr); if (!aMine.IsNull()) { - myData = aMine->myData; myTitle = aMine->myTitle; myMIMEtype = aMine->myMIMEtype; + myData = aMine->myData; } } @@ -127,7 +161,7 @@ void XCAFDoc_NoteBinData::Paste(const Handle(TDF_Attribute)& theAttrInto, Handle(XCAFDoc_NoteBinData) aMine = Handle(XCAFDoc_NoteBinData)::DownCast(theAttrInto); if (!aMine.IsNull()) - aMine->Set(myTitle, myData, myMIMEtype); + aMine->Set(myTitle, myMIMEtype, myData); } Standard_OStream& XCAFDoc_NoteBinData::Dump(Standard_OStream& theOS) const @@ -136,8 +170,12 @@ Standard_OStream& XCAFDoc_NoteBinData::Dump(Standard_OStream& theOS) const theOS << "\n" << "Title : " << (!myTitle.IsEmpty() ? myMIMEtype : "") << "\n" << "MIME type : " << (!myMIMEtype.IsEmpty() ? myMIMEtype : "") << "\n" - << "Size : " << myData.Length() << " bytes" << "\n" - << myData + << "Size : " << Size() << " bytes" << "\n" ; + if (!myData.IsNull()) + { + for (Standard_Integer i = myData->Lower(); i <= myData->Upper(); ++i) + theOS << myData->Value(i); + } return theOS; } diff --git a/src/XCAFDoc/XCAFDoc_NoteBinData.hxx b/src/XCAFDoc/XCAFDoc_NoteBinData.hxx index a508a36d2a..6b3f2dbcce 100644 --- a/src/XCAFDoc/XCAFDoc_NoteBinData.hxx +++ b/src/XCAFDoc/XCAFDoc_NoteBinData.hxx @@ -17,10 +17,11 @@ #define _XCAFDoc_NoteBinData_HeaderFile #include +#include +#include +#include class OSD_File; -class TCollection_AsciiString; -class TCollection_ExtendedString; class XCAFDoc_NoteBinData; DEFINE_STANDARD_HANDLE(XCAFDoc_NoteBinData, XCAFDoc_Note) @@ -39,23 +40,33 @@ public: const TCollection_ExtendedString& theUserName, const TCollection_ExtendedString& theTimeStamp, const TCollection_ExtendedString& theTitle, - OSD_File& theFile, - const TCollection_AsciiString& theMIMEtype); + const TCollection_AsciiString& theMIMEtype, + OSD_File& theFile); + + Standard_EXPORT static Handle(XCAFDoc_NoteBinData) Set(const TDF_Label& theLabel, + const TCollection_ExtendedString& theUserName, + const TCollection_ExtendedString& theTimeStamp, + const TCollection_ExtendedString& theTitle, + const TCollection_AsciiString& theMIMEtype, + const Handle(TColStd_HArray1OfByte)& theData); Standard_EXPORT XCAFDoc_NoteBinData(); - Standard_EXPORT void Set(const TCollection_ExtendedString& theTitle, - OSD_File& theFile, - const TCollection_AsciiString& theMIMEtype); + Standard_EXPORT Standard_Boolean Set(const TCollection_ExtendedString& theTitle, + const TCollection_AsciiString& theMIMEtype, + OSD_File& theFile); - Standard_EXPORT void Set(const TCollection_ExtendedString& theTitle, - const TCollection_AsciiString& theData, - const TCollection_AsciiString& theMIMEtype); + Standard_EXPORT void Set(const TCollection_ExtendedString& theTitle, + const TCollection_AsciiString& theMIMEtype, + const Handle(TColStd_HArray1OfByte)& theData); Standard_EXPORT const TCollection_ExtendedString& Title() const; - Standard_EXPORT const TCollection_AsciiString& Data() const; Standard_EXPORT const TCollection_AsciiString& MIMEtype() const; + Standard_EXPORT Standard_Integer Size() const; + + Standard_EXPORT const Handle(TColStd_HArray1OfByte)& Data() const; + public: Standard_EXPORT const Standard_GUID& ID() const Standard_OVERRIDE; @@ -71,9 +82,9 @@ public: protected: - TCollection_ExtendedString myTitle; - TCollection_AsciiString myData; - TCollection_AsciiString myMIMEtype; + TCollection_ExtendedString myTitle; + TCollection_AsciiString myMIMEtype; + Handle(TColStd_HArray1OfByte) myData; }; #endif // _XCAFDoc_NoteBinData_HeaderFile diff --git a/src/XCAFDoc/XCAFDoc_NotesTool.cxx b/src/XCAFDoc/XCAFDoc_NotesTool.cxx index 4d5d4b32ca..5899d693d7 100644 --- a/src/XCAFDoc/XCAFDoc_NotesTool.cxx +++ b/src/XCAFDoc/XCAFDoc_NotesTool.cxx @@ -14,6 +14,7 @@ // commercial license or contractual agreement. #include +#include #include #include #include @@ -88,13 +89,26 @@ Handle(XCAFDoc_Note) XCAFDoc_NotesTool::AddBinData(const TCollection_ExtendedString& theUserName, const TCollection_ExtendedString& theTimeStamp, const TCollection_ExtendedString& theTitle, - OSD_File& theFile, - const TCollection_AsciiString& theMIMEtype) + const TCollection_AsciiString& theMIMEtype, + OSD_File& theFile) { TDF_Label aNoteLabel; TDF_TagSource aTag; aNoteLabel = aTag.NewChild(Label()); - return XCAFDoc_NoteBinData::Set(aNoteLabel, theUserName, theTimeStamp, theTitle, theFile, theMIMEtype); + return XCAFDoc_NoteBinData::Set(aNoteLabel, theUserName, theTimeStamp, theTitle, theMIMEtype, theFile); +} + +Handle(XCAFDoc_Note) +XCAFDoc_NotesTool::AddBinData(const TCollection_ExtendedString& theUserName, + const TCollection_ExtendedString& theTimeStamp, + const TCollection_ExtendedString& theTitle, + const TCollection_AsciiString& theMIMEtype, + const Handle(TColStd_HArray1OfByte)& theData) +{ + TDF_Label aNoteLabel; + TDF_TagSource aTag; + aNoteLabel = aTag.NewChild(Label()); + return XCAFDoc_NoteBinData::Set(aNoteLabel, theUserName, theTimeStamp, theTitle, theMIMEtype, theData); } Standard_Boolean XCAFDoc_NotesTool::HasAttachedNotes(const TDF_Label& theLabel) const diff --git a/src/XCAFDoc/XCAFDoc_NotesTool.hxx b/src/XCAFDoc/XCAFDoc_NotesTool.hxx index 19702f5d9b..8dc7cb1016 100644 --- a/src/XCAFDoc/XCAFDoc_NotesTool.hxx +++ b/src/XCAFDoc/XCAFDoc_NotesTool.hxx @@ -25,6 +25,7 @@ class OSD_File; class Standard_GUID; class TCollection_AsciiString; class TCollection_ExtendedString; +class TColStd_HArray1OfByte; class TDF_RelocationTable; class XCAFDoc_Note; @@ -54,8 +55,14 @@ public: Standard_EXPORT Handle(XCAFDoc_Note) AddBinData(const TCollection_ExtendedString& theUserName, const TCollection_ExtendedString& theTimeStamp, const TCollection_ExtendedString& theTitle, - OSD_File& theFile, - const TCollection_AsciiString& theMIMEtype); + const TCollection_AsciiString& theMIMEtype, + OSD_File& theFile); + + Standard_EXPORT Handle(XCAFDoc_Note) AddBinData(const TCollection_ExtendedString& theUserName, + const TCollection_ExtendedString& theTimeStamp, + const TCollection_ExtendedString& theTitle, + const TCollection_AsciiString& theMIMEtype, + const Handle(TColStd_HArray1OfByte)& theData); Standard_EXPORT Standard_Boolean HasAttachedNotes(const TDF_Label& theLabel) const; diff --git a/src/XmlMXCAFDoc/XmlMXCAFDoc_NoteBinDataDriver.cxx b/src/XmlMXCAFDoc/XmlMXCAFDoc_NoteBinDataDriver.cxx index b53d3bc552..7a0c9c413e 100644 --- a/src/XmlMXCAFDoc/XmlMXCAFDoc_NoteBinDataDriver.cxx +++ b/src/XmlMXCAFDoc/XmlMXCAFDoc_NoteBinDataDriver.cxx @@ -17,13 +17,15 @@ #include #include #include +#include #include #include +#include IMPLEMENT_STANDARD_RTTIEXT(XmlMXCAFDoc_NoteBinDataDriver, XmlMXCAFDoc_NoteDriver) IMPLEMENT_DOMSTRING(Title, "title") IMPLEMENT_DOMSTRING(MIMEtype, "mime_type") -IMPLEMENT_DOMSTRING(Data, "data") +IMPLEMENT_DOMSTRING(Size, "size") //======================================================================= //function : @@ -57,15 +59,30 @@ Standard_Boolean XmlMXCAFDoc_NoteBinDataDriver::Paste(const XmlObjMgt_Persistent XmlObjMgt_DOMString aTitle = anElement.getAttribute(::Title()); XmlObjMgt_DOMString aMIMEtype = anElement.getAttribute(::MIMEtype()); - XmlObjMgt_DOMString aData = anElement.getAttribute(::Data()); - if (aTitle == NULL || aMIMEtype == NULL || aData == NULL) + XmlObjMgt_DOMString aSize = anElement.getAttribute(::Size()); + if (aTitle == NULL || aMIMEtype == NULL || aSize == NULL) return Standard_False; Handle(XCAFDoc_NoteBinData) aNote = Handle(XCAFDoc_NoteBinData)::DownCast(theTarget); if (aNote.IsNull()) return Standard_False; - aNote->Set(aTitle.GetString(), aData.GetString(), aMIMEtype.GetString()); + Standard_Integer nbSize = 0; + if (!aSize.GetInteger(nbSize)) + return Standard_False; + + XmlObjMgt_DOMString aDataStr = XmlObjMgt::GetStringValue(theSource); + Standard_SStream anSS(aDataStr.GetString()); + + Handle(TColStd_HArray1OfByte) aData = new TColStd_HArray1OfByte(1, nbSize); + for (Standard_Integer i = 1; i <= nbSize; ++i) + { + Standard_Byte aValue; + anSS >> aValue; + aData->ChangeValue(i) = aValue; + } + + aNote->Set(aTitle.GetString(), aMIMEtype.GetString(), aData); return Standard_True; } @@ -84,9 +101,21 @@ void XmlMXCAFDoc_NoteBinDataDriver::Paste(const Handle(TDF_Attribute)& theSource XmlObjMgt_DOMString aTitle(TCollection_AsciiString(aNote->Title()).ToCString()); XmlObjMgt_DOMString aMIMEtype(aNote->MIMEtype().ToCString()); - XmlObjMgt_DOMString aData(aNote->Data().ToCString()); theTarget.Element().setAttribute(::Title(), aTitle); theTarget.Element().setAttribute(::MIMEtype(), aMIMEtype); - theTarget.Element().setAttribute(::Data(), aData); + theTarget.Element().setAttribute(::Size(), aNote->Size()); + + if (aNote->Size() > 0) + { + const Handle(TColStd_HArray1OfByte)& aData = aNote->Data(); + LDOM_OSStream anOSS(aNote->Size()); + for (Standard_Integer i = aData->Lower(); i <= aData->Upper(); ++i) + { + anOSS << std::hex << aData->Value(i); + } + Standard_Character* dump = (Standard_Character*)anOSS.str(); // copying! Don't forget to delete it. + XmlObjMgt::SetStringValue(theTarget, dump, Standard_True); + delete[] dump; + } }