Files
Slnkdwf/SlnkDWFImpl/myEPlotSectionImpl.cpp
Jos Groot Lipman 80d0142c23 SLNKDWF 2.00
svn path=/Slnkdwf/trunk/; revision=12481
2007-08-01 13:42:28 +00:00

345 lines
8.0 KiB
C++

// EPlotSection.cpp : Implementation of CEPlotSection
#include "stdafx.h"
#include "myEPlotSectionImpl.h"
#include "myWT_File.h"
#include "dwfcore/mime.h"
// CEPlotSection
CEPlotSectionImpl::~CEPlotSectionImpl()
{
#ifndef DWFTK_READ_ONLY
if (m_pW2DOutStream)
delete m_pW2DOutStream;
#endif
if (m_pW2DInStream)
delete m_pW2DInStream;
}
void CEPlotSectionImpl::Init(DWFToolkit::DWFEPlotSection* pSection,
const CString SourceDescription,
BOOL pForWrite)
{
m_pSection = pSection;
ATLASSERT(m_pSection == pSection);
m_ForWrite = pForWrite;
m_SourceDescription = SourceDescription;
if (!pForWrite)
{
//#ifdef _DWFTK_SAMPLE_FIX_BAD_EPLOT_DESCRIPTOR_XML
// Nodig voor 222306-1-0601-05_Malacko_-_WD2.dwf
//
// some sections might not also be descriptor readers which implement this interface...
//
DWFToolkit::DWFXMLCallback* pXMLCallback = dynamic_cast<DWFToolkit::DWFXMLCallback*>(pSection);
if (pXMLCallback)
{
myTRACE("\nXML fixing requested - inserting duplicate attribute filter.");
//
// hook up the filter - as and added bonus tell the section to delete it too
//
DWFToolkit::DWFDuplicateAttributeFilter* pFilter = DWFCORE_ALLOC_OBJECT( DWFToolkit::DWFDuplicateAttributeFilter );
pXMLCallback->setStreamFilter( pFilter, true );
}
//#endif
m_pSection->readDescriptor();
}
}
const CString CEPlotSectionImpl::get_Name()
{
return CString(m_pSection->name());
}
const CString CEPlotSectionImpl::get_Title()
{
return CString(m_pSection->title());
}
ULONG CEPlotSectionImpl::get_PaperColor()
{
DWFPaper *paper = m_pSection->paper();
return paper->color();
}
// Always inches
double CEPlotSectionImpl::get_PaperClipWidth()
{
DWFPaper *paper = m_pSection->paper();
if (paper)
{
const double *clip = paper->clip();
switch (paper->units())
{
case DWFPaper::eMillimeters:
return (clip[2] - clip[0])/25.4;
break;
case DWFPaper::eInches:
return clip[2] - clip[0];
break;
}
}
return -1; // Invalid
}
double CEPlotSectionImpl::get_PaperClipHeight()
{
DWFPaper *paper = m_pSection->paper();
if (paper)
{
const double *clip = paper->clip();
switch (paper->units())
{
case DWFPaper::eMillimeters:
return (clip[3] - clip[1])/25.4;
break;
case DWFPaper::eInches:
return clip[3] - clip[1];
break;
}
}
return -1; // Invalid
}
STDMETHODIMP CEPlotSectionImpl::Open(BOOL bForWrite)
{
#ifdef DWFTK_READ_ONLY
ATLASSERT(!bForWrite);
#endif
try
{
//
// get the graphics stream
//
DWFResourceContainer::ResourceIterator* piResources =
m_pSection->findResourcesByRole( DWFXML::kzRole_Graphics2d );
if ((piResources == NULL) || (piResources->valid() == false))
{
if (piResources)
{
DWFCORE_FREE_OBJECT( piResources );
}
myTRACE("%ls", "Illegal EPlot section - no graphics");
return E_FAIL;
}
//
// 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());
ATLASSERT(pW2D != NULL);
DWFString s = pW2D->creationTime();
if (s.chars())
myTRACE("\nCreationTime %s", (const wchar_t *)pW2D->creationTime());
//
// done with the iterator
//
DWFCORE_FREE_OBJECT( piResources );
if (pW2D->mime() != DWFCore::DWFMIME::kzMIMEType_W2D)
{
myDoTRACE("\nMime: %ls", (const wchar_t*)pW2D->mime());
throw myCString("Unsupported graphics resource (%ls)."
"\nMeasurement/Print disabled?", (const wchar_t*)pW2D->mime());
}
if (pW2D == NULL)
{
myTRACE("%ls", L"Type mismatch - not a W2D resource");
return E_FAIL;
}
//
// get the data stream
//
#ifndef DWFTK_READ_ONLY
if (bForWrite)
{
m_pW2DOutStream = new DWFCore::DWFBufferOutputStream(1024);
}
else
#endif
{
// TODO: Fors geheugenlek?
m_pW2DInStream = pW2D->getInputStream();
m_nSize = pW2D->size();
#ifndef DWFTK_READ_ONLY
if (!m_pW2DInStream && m_pW2DOutStream)
m_pW2DInStream = new DWFCore::DWFBufferInputStream(m_pW2DOutStream->buffer(), m_pW2DOutStream->bytes());
#endif
}
return S_OK;
}
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("\nCEPlotSectionImpl::open(\"%s\")\n%s", m_SourceDescription, err);
}
}
const DWFProperty* const CEPlotSectionImpl::findProperty( const DWFString& zName,
const DWFString& zCategory )
{
return m_pSection->findProperty( zName, zCategory);
}
HRESULT CEPlotSectionImpl::Read( void *pv, ULONG cb, ULONG *pcbRead)
{
ATLASSERT(m_pW2DInStream); // Open first!
(*pcbRead)=0;
if (m_pW2DInStream->available()>0)
{
(*pcbRead) = (ULONG)m_pW2DInStream->read( pv, cb );
}
return S_OK;
};
HRESULT CEPlotSectionImpl::Seek( ULONG cb, ULONG *pcbRead)
{
ATLASSERT(m_pW2DInStream); // Open first!
(*pcbRead)=0;
if (m_pW2DInStream->available()>0)
{
(*pcbRead) = (ULONG)m_pW2DInStream->seek( SEEK_CUR, cb );
}
return S_OK;
};
#ifndef DWFTK_READ_ONLY
HRESULT CEPlotSectionImpl::Write( const void *pv, ULONG cb, ULONG *pcbWritten)
{
ATLASSERT(m_pW2DOutStream); // Open first!
(*pcbWritten)=0;
(*pcbWritten) = (ULONG)m_pW2DOutStream->write( pv, cb );
return S_OK;
};
#endif
STDMETHODIMP CEPlotSectionImpl::Close(void)
{
// Don't bother ASSERT(m_pW2DInStream); // Open first!
// if (m_pW2DOutStream)
// m_pSection->res m_pW2DOutStream
if (m_pW2DInStream)
delete m_pW2DInStream;
m_pW2DInStream = NULL;
return S_OK;
}
STDMETHODIMP CEPlotSectionImpl::get_SourceDescription(CString & pVal)
{
pVal = m_SourceDescription;
return S_OK;
}
#ifndef DWFTK_READ_ONLY
STDMETHODIMP CEPlotSectionImpl::get_PropertiesXML(CString &pVal)
{
if (m_pSection!=NULL)
{
DWFCore::DWFBufferOutputStream m_XMLProps(1024);
DWFUUID _oUUID;
DWFXMLSerializer XML(_oUUID);
XML.attach(m_XMLProps);
//DWFManifest& rManifest = m_pSection;
m_pSection->serializeXML(XML, DWFPackageWriter::eDescriptor);
XML.detach();
pVal = CString((LPCSTR)m_XMLProps.buffer(), (int)m_XMLProps.bytes());
return S_OK;
}
else
return E_FAIL;
return S_OK;
}
bool CEPlotSectionImpl::SaveAs(const CString &destpath)
{
ATLASSERT(!m_pW2DInStream); // Don't open first!
try
{
Open(false);
DWFFile m_pTempFile(destpath);
DWFStreamFileDescriptor fds (m_pTempFile, L"wb");
fds.open();
DWFFileOutputStream rTempStream;
rTempStream.attach(&fds,false);
// Copy pW2DStream to rTempStream
#define DATA_BUFFER_BYTES 16384
unsigned char* pDataBuffer = DWFCORE_ALLOC_MEMORY( unsigned char, DATA_BUFFER_BYTES );
if (pDataBuffer == NULL)
{
_DWFCORE_THROW( DWFMemoryException, L"No memory for data buffer" );
}
while (m_pW2DInStream->available()>0)
{
size_t nBytes;
nBytes = m_pW2DInStream->read( pDataBuffer, DATA_BUFFER_BYTES );
rTempStream.write( pDataBuffer, nBytes );
}
rTempStream.flush();
rTempStream.detach();
fds.close();
DWFCORE_FREE_MEMORY( pDataBuffer )
Close();
}
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("\nCEPlotSectionImpl::open(\"%s\")\n%s", m_SourceDescription, err);
}
return true;
};
bool CEPlotSectionImpl::SaveAsAscii(const CString &srcpath, const CString &destpath)
{
myWT_File my_input_file;
my_input_file.set_filename(srcpath);
my_input_file.SaveAsAscii(destpath);
return true;
}
bool CEPlotSectionImpl::SaveAsAscii(const CString &destpath)
{
ATLASSERT(!m_pW2DInStream); // Don't open first!
myWT_File my_input_file;
my_input_file.set_eplotsection(this);
my_input_file.SaveAsAscii(destpath);
return true;
};
#endif