Files
Slnkdwf/SlnkDWFImpl/DWFFileImpl.cpp
Jos Groot Lipman 6596ce32c5 TUDE#13285 AsDWF Schaal behouden
svn path=/Slnkdwf/trunk/; revision=12484
2007-12-19 09:44:56 +00:00

286 lines
8.0 KiB
C++

// DWFFile.cpp : Implementation of CDWFFile
#include "stdafx.h"
#include "assert.h"
#include "DWFFileImpl.h"
#include "myEPlotSectionImpl.h"
#include "dwfcore/mime.h"
#include "../SLNKDwfCom/SLNKDwfVersion.h"
// CDWFFile
//CDWFFileImpl::~CDWFFile()
//{
// // It seems we never get here? Use FinalRelease
//};
CDWFFileImpl::~CDWFFileImpl()
{
if (m_oReader!=NULL)
delete m_oReader;
#ifndef DWFTK_READ_ONLY
if (m_oWriter!=NULL)
delete m_oWriter;
#endif
if (m_DWF!=NULL)
delete m_DWF;
}
#ifndef DWFTK_READ_ONLY
bool CDWFFileImpl::Create(const CString &DWFPath, CDWFFileImpl *DWFFileTemplate)
{
DWFCore::DWFFile oDWF( DWFPath );
m_oWriter = new DWFToolkit::DWFPackageWriter ( oDWF );
return true;
}
// Returns index of created item. -1 is error
// W2DPath wijst naar de W2D die we inlezen
// ePlotTemplate nemen we papier van over
LONG CDWFFileImpl::CreateEPlotSection(const CString &W2DPath,
CEPlotSectionImpl *ePlotTemplate)
{
if (!m_oWriter)
throw CString("DWFFile package must be created first");
// Neem zo veel mogelijk van onze originele EPlotSection over
DWFToolkit::DWFEPlotSection* oldSection = ePlotTemplate->get_Section();
// Maak een nieuwe DWFEPlotSection aan
DWFToolkit::DWFEPlotSection* pPage =
DWFCORE_ALLOC_OBJECT( DWFToolkit::DWFEPlotSection(
oldSection->title(),
oldSection->objectID(), oldSection->order(),
oldSection->source(), oldSection->color(),
oldSection->paper()) );
pPage->addProperty( DWFCORE_ALLOC_OBJECT(DWFToolkit::DWFProperty(L"Version", SLNK_BUILDVERSION, L"SLNKDWF", L"", L"")), true );
// Zoek de originele graphicsResource voor de units e.d.
DWFResourceContainer::ResourceIterator* piResources =
oldSection->findResourcesByRole( DWFXML::kzRole_Graphics2d );
if ((piResources == NULL) || (piResources->valid() == false))
{
if (piResources)
{
DWFCORE_FREE_OBJECT( piResources );
}
myTRACE("%ls", "Illegal EPlot section - no graphics");
return false;
}
//
// get the w2d resource
//
//Since we know it's a Graphics2D role, we should be able to cast
//into an EPlotGraphicResource, otherwise the authoring tool didn't
//read the spec.
DWFToolkit::DWFGraphicResource* pW2D = dynamic_cast<DWFGraphicResource*>(piResources->get());
//
// define the resource - this must be allocated on the heap
//
DWFToolkit::DWFGraphicResource* p2Dgfx =
DWFCORE_ALLOC_OBJECT( DWFGraphicResource(L"SLNKDWF" , // title
DWFXML::kzRole_Graphics2d, // role
DWFMIME::kzMIMEType_W2D, // MIME type
L"Autodesk, Inc.", // author
L"SLNKDWF modfied", // description
L"", // creation time
L"") ); // modification time
if (p2Dgfx == NULL)
{
_DWFCORE_THROW( DWFMemoryException, L"Failed to allocate resource" );
}
//
// configure the resource
//
p2Dgfx->configureGraphic( pW2D->transform(),
NULL,
pW2D->clip() );
//
// most importantly - bind a stream to the resource
// in this case, we have a file on disk so we open the
// file with a streaming descriptor. we will also
// create everything on the heap since the package writer
// will not use the stream immediately, he will need to own
// these resources.
//
DWFCore::DWFFile oW2DFilename( W2DPath );
DWFCore::DWFStreamFileDescriptor* pW2DFile = DWFCORE_ALLOC_OBJECT( DWFStreamFileDescriptor(oW2DFilename, L"rb") );
if (pW2DFile == NULL)
{
DWFCORE_FREE_OBJECT( p2Dgfx );
_DWFCORE_THROW( DWFMemoryException, L"Failed to allocate file descriptor" );
}
DWFCore::DWFFileInputStream* pW2DFilestream = DWFCORE_ALLOC_OBJECT( DWFFileInputStream );
if (pW2DFilestream == NULL)
{
DWFCORE_FREE_OBJECT( p2Dgfx );
DWFCORE_FREE_OBJECT( pW2DFile );
_DWFCORE_THROW( DWFMemoryException, L"Failed to allocate file stream" );
}
//
// open the file and bind it to the stream
//
pW2DFile->open();
pW2DFilestream->attach( pW2DFile, true );
//
// hand the stream off to the resource
// NOTE: since we don't already know the filesize (in the application space - of course we can look on disk...)
// leave the second parameter (nBytes) default as zero, this will tell the package writer
// to use the number of bytes it processed through the stream as the size attribute in the descriptor.
//
p2Dgfx->setInputStream( pW2DFilestream );
//
// finally, drop the resource into the page
//
pPage->addResource( p2Dgfx, true );
///
///
CEPlotSectionImpl *mySection = new CEPlotSectionImpl;
CString s;s.Format("%s (page %d)", "sourceDescription", m_EPlotSections.get_Count());
mySection->Init(pPage, s, true);
m_EPlotSections.Add(mySection);
m_oWriter->addSection( pPage );
// Aan het einde van de packagewriter gebeurt wel een flush...DWFCORE_FREE_OBJECT( pW2DFilestream );
return m_EPlotSections.get_Count()-1;
}
bool CDWFFileImpl::Save()
{
if (!m_oWriter)
throw CString("DWFFile package must be created first");
try
{
m_oWriter->write( L"Dijkoraad IT", L"SLNKDWF", SLNK_BUILDVERSION, // <== Wij
L"Autodesk, Inc", _DWFTK_VERSION_STRING ); // <== Zij
}
catch (DWFException& ex)
{
CString err;
err.Format("%ls\n%ls\n%s\n%ls(%d)",ex.type(),ex.message(),ex.function(),ex.file(),ex.line());
throw myCString ("\nCDWFFileImpl::Save()\n%s", err);
}
return true;
}
bool CDWFFileImpl::Open(const CString &DWFPath)
{
if (m_isOpen)
{
throw CString("\nCDWFFileImpl::Open already open");
}
try
{
myDoTRACE("\nCDWFFileImpl::Open('%s')", DWFPath);
m_DWF = new DWFFile( DWFPath );
m_oReader = new DWFPackageReader( *m_DWF );
m_oReader->getPackageInfo( m_tInfo );
wchar_t zBuffer[256] = {0};
myTRACE("\nCDWFFileImpl::Opened('%s')", DWFPath);
if (m_tInfo.eType == DWFPackageReader::eW2DStream ||
m_tInfo.eType == DWFPackageReader::eDWFStream)
{ // Dit is wel heel gemakkelijk
m_W2DFileName = DWFString(DWFPath);
assert(m_W2DFileName.chars() != 0);
m_isOpen = TRUE;
return true;
}
if (m_tInfo.eType != DWFPackageReader::eDWFPackage)
{
_DWFCORE_SWPRINTF( zBuffer, 256, L"File is not a DWF package [%s]",
(m_tInfo.eType == DWFPackageReader::eW2DStream) ? L"W2D Stream" :
(m_tInfo.eType == DWFPackageReader::eDWFStream) ? L"DWF Stream (<6.0)" :
(m_tInfo.eType == DWFPackageReader::eZIPFile) ? L"ZIP Archive" : L"Unknown" );
myTRACE("%ls", zBuffer);
throw myCString("ERROR: Unable to open file: %s\n%ls", DWFPath, zBuffer);
}
//
// read and parse the manifest
//
DWFManifest& rManifest = m_oReader->getManifest();
if (!m_EPlotSections.ProcessManifest(rManifest, CString(DWFPath)))
{
CString err("Unable to retrieve EPlotSections. Perhaps it is a 3D DWF?");
throw myCString ("\nCDWFFileImpl::Open('%s')\n%s", DWFPath, err);
}
m_isOpen = TRUE;
return true;
}
catch (DWFException& ex)
{
CString err;
err.Format("%ls\n%ls\n%s\n%ls(%d)",ex.type(),ex.message(),ex.function(),ex.file(),ex.line());
throw myCString ("\nCDWFFileImpl::Open('%s')\n%s", DWFPath, err);
}
}
#endif
#if DESIRED_CODE(WHIP_OUTPUT)
bool CDWFFileImpl::get_PropertiesXML(CString & pVal)
{
if (!m_isOpen)
throw myCString ("\nDWF File is not opened");
if (m_DWF!=NULL)
{
DWFCore::DWFBufferOutputStream m_XMLProps(1024);
DWFUUID _oUUID;
DWFXMLSerializer XML(_oUUID);
XML.attach(m_XMLProps);
DWFManifest& rManifest = m_oReader->getManifest();
rManifest.serializeXML(XML, DWFPackageWriter::eManifest);
XML.detach();
pVal = CString((LPCSTR)m_XMLProps.buffer(), (int)m_XMLProps.bytes());
return true;
}
else
return false;
return true;
}
#endif
CEPlotSectionsImpl *CDWFFileImpl::get_EPlotSections()
{
if (!m_isOpen)
throw myCString("\nDWF File is not opened");
return &m_EPlotSections;
}