#include "pch.h"
#include "MxHead.h"
#include "MxReadFilter.h"
#include "Tool.h"
#include "MxOptLayerTable.h"
#include "MxDxfData.h"
#include "MxReadContext.h"
#include "MxOptBlockTable.h"
#include "MxOptEntity.h"

MxReadFilter::MxReadFilter()
	: m_pContext(NULL),m_pOptEntity(NULL)
{

}

MxReadFilter::~MxReadFilter(void)
{
	delete m_pOptEntity;
}

void  MxReadFilter::SetContext(MxReadContext* pContext)
{
	m_pContext = pContext;
	m_pOptEntity = new MxOptEntity(pContext);
}



void MxReadFilter::addLayer(const DL_LayerData& data)
{
	ASSERT(m_pContext != NULL);

	MxOptLayerTable layerTable(m_pContext);

	layerTable.DxfRecord2Mx(data);
}

void MxReadFilter::setVariableVector(const char* key,
					   double v1, double v2, double v3, int code)
{
	struct resbuf* pVal = Mx::mcutNewRb(RTPOINT);
	pVal->resval.rpoint[0] = v1;
	pVal->resval.rpoint[1] = v2;
	pVal->resval.rpoint[2] = v3;

	MxString sKey = MXDXF_A2W(key);

	sKey.TrimLeft(_T('$'));

	m_pContext->AddSysVar(sKey,pVal);

	MxDxfData::Instance().SetSysVarCode(sKey,code);
}

void MxReadFilter::setVariableString(const char* key, const char* value, int code)
{
	MxString sKey	= MXDXF_A2W(key);
	sKey.TrimLeft(_T('$'));

	MxString sValue	= MXDXF_A2W(value);

	struct resbuf* pVal = Mx::mcutBuildList(RTSTR,sValue,0);
	
	m_pContext->AddSysVar(sKey,pVal);

	MxDxfData::Instance().SetSysVarCode(sKey,code);

}

void MxReadFilter::setVariableInt(const char* key, int value, int code)
{
	MxString sKey	= MXDXF_A2W(key);
	sKey.TrimLeft(_T('$'));

	long lVal = value;
	if(sKey == _T("CECOLOR") )
	{
		McCmColor color;
		color.setColorIndex(value);
		

		lVal = color.color();
	}

	struct resbuf* pVal = Mx::mcutBuildList(RTLONG,lVal,0);

	m_pContext->AddSysVar(sKey,pVal);

	MxDxfData::Instance().SetSysVarCode(sKey,code);
}

void MxReadFilter::setVariableDouble(const char* key, double value, int code)
{
	MxString sKey	= MXDXF_A2W(key);
	sKey.TrimLeft(_T('$'));

	struct resbuf* pVal = Mx::mcutBuildList(RTREAL,value,0);

	m_pContext->AddSysVar(sKey,pVal);

	MxDxfData::Instance().SetSysVarCode(sKey,code);
}


void MxReadFilter::addBlock(const DL_BlockData& data)
{
	MxOptBlockTable optBlock(m_pContext);
	McDbObjectId blkRecId = optBlock.Dxf2Mx(data);

	m_pContext->SetCurSpaceId(blkRecId);
}

void MxReadFilter::endBlock()
{
	// 这里默认，在块记录外的实体都在模型空间内，
	// 这样会出现布局对象的实体在读取时也到模型空中。

	m_pContext->SetCurSpaceId(m_pContext->GetModelSpaceId() );
}


void MxReadFilter::addLine(const DL_LineData& data)
{
	ASSERT(m_pOptEntity != NULL);
	m_pOptEntity->ReadLine(data);
}

void MxReadFilter::addPoint(const DL_PointData& data)
{
	ASSERT(m_pOptEntity != NULL);
	m_pOptEntity->ReadPoint(data);
}

void MxReadFilter::addArc(const DL_ArcData& data) 
{
	ASSERT(m_pOptEntity != NULL);
	m_pOptEntity->ReadArc(data);
}

void MxReadFilter::addEllipse(const DL_EllipseData& data) 
{
	ASSERT(m_pOptEntity != NULL);
	m_pOptEntity->ReadEllipse(data);
}


void MxReadFilter::addPolyline(const DL_PolylineData& data)
{
	ASSERT(m_pOptEntity != NULL);
	m_pOptEntity->ReadPolyline(data);
}

void MxReadFilter::addVertex(const DL_VertexData& data)
{
	ASSERT(m_pOptEntity != NULL);
	m_pOptEntity->ReadVertex(data);
}

void MxReadFilter::addText(const DL_TextData& data)
{
	ASSERT(m_pOptEntity != NULL);
	m_pOptEntity->ReadText(data);
}

void MxReadFilter::addInsert(const DL_InsertData& data)
{
	ASSERT(m_pOptEntity != NULL);
	m_pOptEntity->ReadInsert(data);
}

void MxReadFilter::addCircle(const DL_CircleData& data)
{
	ASSERT(m_pOptEntity != NULL);
	m_pOptEntity->ReadCircle(data);
}