#include "pch.h"
#include "MxHead.h"
#include "MxOptLayerTable.h"
#include "DxfLib\dl_codes.h"
#include "DxfLib\dl_dxf.h"

#include "DxfLib\dl_writer_ascii.h"
#include "Tool.h"
#include "MxReadFilter.h"
#include "MxReadContext.h"
#include "MxWriteContext.h"


MxOptLayerTable::MxOptLayerTable(MxReadContext*  pContext
				)
				: m_pReadContext(pContext)
{
	m_pWriteContext = NULL;
}

MxOptLayerTable::MxOptLayerTable(MxWriteContext*  pContext)
	: m_pWriteContext(pContext)
{
	m_pReadContext = NULL;
}


MxOptLayerTable::~MxOptLayerTable(void)
{
}


bool MxOptLayerTable::Mx2Dxf(DL_WriterA* dw)
{
	if(m_pWriteContext == NULL)
	{
		ASSERT(0);
		return false;
	}



	int iLayers = 1;
	McDbLayerTablePointer spMxTab(m_pWriteContext->GetDatabase(),McDb::kForRead);
	ASSERT(spMxTab.openStatus() == Mcad::eOk);

	McDbLayerTableIterator* pIter = NULL;
	spMxTab->newIterator(pIter);
	std::auto_ptr<McDbLayerTableIterator> spIter(pIter);
	ASSERT(pIter != NULL);

	for(iLayers = 0;!pIter->done();pIter->step(),iLayers++ );

	dw->tableLayers(iLayers);

	pIter->start();
	for (; !pIter->done(); pIter->step()) 
	{
		McDbObjectId mxRecId;
		pIter->getRecordId(mxRecId);

		McDbObjectPointer<McDbLayerTableRecord> spMxRec(mxRecId,McDb::kForRead);
		ASSERT(spMxRec.openStatus() == Mcad::eOk);


		MxRecord2Dxf(spMxRec.object(),dw);
	}

	dw->tableEnd();

	return true;
}


McDbObjectId MxOptLayerTable::DxfRecord2Mx(const DL_LayerData& data)
{
	if(m_pReadContext == NULL)
	{
		ASSERT(0);
		return McDbObjectId::kNull;
	}

	MxReadFilter* pFilter = m_pReadContext->GetReadFilter();

	McDbLayerTablePointer spMxTable(m_pReadContext->GetDatabase(),McDb::kForWrite);
	if(spMxTable.openStatus() != Mcad::eOk)
	{
		ASSERT(0);
		return McDbObjectId::kNull;
	}

	MxString sName = MXDXF_A2W(data.name.c_str() );

	McDbLayerTableRecord* pMxRec  = NULL;
	if(spMxTable->getAt(sName,pMxRec,McDb::kForWrite,true) == Mcad::eOk)
	{
		// 如果块表记录已经删除，再这又新读进该块表记录，所以需要清除删除标志.
		pMxRec->erase(false);	
	}
	else
	{
		pMxRec = new McDbLayerTableRecord;
		pMxRec->setName(sName);
		spMxTable->add(pMxRec);
	}

	pMxRec->setColor(Tool::DxfColor2Mx(pFilter->getAttributes().getColor(),false) );

	pMxRec->setIsFrozen(data.flags & 0x01);
	pMxRec->setIsLocked(data.flags & 0x04);

	pMxRec->setLineWeight(Tool::DxfLineWeight2Mx(pFilter->getAttributes().getWidth() ) );

	// 线型的设置,...
	//attributes.lineType;



	McDbObjectId retId;
	retId = pMxRec->objectId();
	pMxRec->close();

	m_pReadContext->AddLineType(retId,MXDXF_A2W(pFilter->getAttributes().getLineType().c_str() ) );


	return retId;
}

bool MxOptLayerTable::MxRecord2Dxf(McDbLayerTableRecord* pMxRec,
								   DL_WriterA* dw
								   )
{
	LPCTSTR pszName = NULL;
	pMxRec->getName(pszName);
	MxStringA sNameA(MXDXF_W2A(pszName) );

	int iFlag = 0;
	if(pMxRec->isFrozen() )
		iFlag = iFlag | 0x01;

	if(pMxRec->isLocked() )
		iFlag = iFlag | 0x04;

	int iColor		= Tool::MxColor2Dxf(pMxRec->color());
	int iLineWidth	= Tool::MxLineWeight2Dxf(pMxRec->lineWeight());

	MxString sLineType = _T("CONTINUOUS");

	McDbObjectPointer<McDbLinetypeTableRecord> spLineType(pMxRec->linetypeObjectId(),McDb::kForRead);
	if(spLineType.openStatus() == Mcad::eOk)
	{
		spLineType->getName(sLineType);
	}

	MxStringA sLineTypeA;
	sLineTypeA = MXDXF_W2A(sLineType);

	m_pWriteContext->GetDxf()->writeLayer(
			*dw,
			DL_LayerData((const char*)sNameA,iFlag),

			DL_Attributes(std::string(""),
				iColor,
				iLineWidth,
				(const char*)sLineTypeA
			)
		);

	return true;
}