#include "pch.h"
#include "MxHead.h"
#include "MxReadContext.h"
#include "MxLineTypeData.h"

MxReadContext::MxReadContext(McDbDatabase* pMxDataBase,
							 DL_Dxf*			pDxf,
								MxReadFilter*   pFilter
									)
	: m_pMxDataBase(pMxDataBase),m_pDxf(pDxf),m_pFilter(pFilter)
{
	McDbBlockTableRecordPointer spModeSapce(MCDB_MODEL_SPACE,
		pMxDataBase,McDb::kForRead);

	ASSERT(spModeSapce.openStatus() == Mcad::eOk);

	if(spModeSapce.openStatus() == Mcad::eOk)
	{
		m_modelSpaceId = spModeSapce->objectId();
		m_curSpaceId   = m_modelSpaceId;
	}

}

MxReadContext::~MxReadContext(void)
{
	std::map<MxString,struct resbuf*>::iterator iter 
		= m_mapReplaceSysVar.begin();

	for(;iter != m_mapReplaceSysVar.end();++iter)
	{
		// 释放链表。
		Mx::mcutRelRb(iter->second);
	}
	m_mapReplaceSysVar.clear();
}

void MxReadContext::AddLineType(McDbObjectId id,MxString sTypeLine)
{
	if(sTypeLine.IsEmpty() )
		return;

	sTypeLine.MakeUpper();

	m_setReadLineType.insert(sTypeLine);

	m_mapReplaceLineType.insert(std::make_pair(id,sTypeLine) );
}


void MxReadContext::EndReadEvent()
{
	ReadLineType();
	ReplaceLineTypeId();

	ReplaceSysVar();
}

void MxReadContext::ReplaceLineTypeId()
{
	McDbLinetypeTablePointer spLinetypeTable(m_pMxDataBase,McDb::kForRead);
	if(spLinetypeTable.openStatus() != Mcad::eOk)
		return;


	std::map<McDbObjectId,MxString>::iterator iter 
		=  m_mapReplaceLineType.begin();

	for(;iter != m_mapReplaceLineType.end();++iter)
	{

		McDbObjectId lineTypeRecId;
		spLinetypeTable->getAt(iter->second,lineTypeRecId);
		
		if(lineTypeRecId.isNull() )
			continue;


		McDbObjectPointer<McDbObject> spObj(iter->first,McDb::kForWrite);
		if(spObj.openStatus() != Mcad::eOk)
		{
			ASSERT(0);
			continue;
		}

		McDbLayerTableRecord* pLayerTableRec	= McDbLayerTableRecord::cast(spObj.object() );
		McDbEntity* pEntity						= McDbEntity::cast(spObj.object());
		if(pLayerTableRec != NULL)
		{
			pLayerTableRec->setLinetypeObjectId(lineTypeRecId);
		}
		else if(pEntity != NULL)
		{
			pEntity->setLinetype(lineTypeRecId);
		}
	}

}

void MxReadContext::ReadLineType()
{
	McDbLinetypeTablePointer spLinetypeTable(m_pMxDataBase,McDb::kForWrite);
	if(spLinetypeTable.openStatus() != Mcad::eOk)
		return;

	std::set<MxString>::iterator iter = m_setReadLineType.begin();
	for(;iter != m_setReadLineType.end();++iter)
	{
		MxString sName = *iter;
		if(spLinetypeTable->has(sName))
		{
			continue;
		}

		McDbLinetypeTableRecord* pNewRec = 
			MxLineTypeData::Instance().CreateLineTypeRecord(sName);
		
		if(pNewRec == NULL)
		{
			continue;
		}

		McDbObjectId  recId;
		if(spLinetypeTable->add(recId,pNewRec) != Mcad::eOk)
		{
			ASSERT(0);
			delete pNewRec;
			continue;
		}
		pNewRec->close();
	}
}

void MxReadContext::AddSysVar(MxString sVarName,struct resbuf* pVal)
{
	sVarName.MakeUpper();
	std::map<MxString,struct resbuf*>::iterator iter 
		= m_mapReplaceSysVar.find(sVarName);

	if(iter == m_mapReplaceSysVar.end()) 
	{
		m_mapReplaceSysVar.insert(std::make_pair(sVarName,pVal));
	}
	else
	{
		// 已经有了。
		Mx::mcutRelRb(pVal);
	}
}

void MxReadContext::ReplaceSysVar()
{
	std::map<MxString,struct resbuf*>::iterator iter 
		= m_mapReplaceSysVar.begin();

	for(;iter != m_mapReplaceSysVar.end();++iter)
	{
		IMxDxf::SetSysVar(iter->first,iter->second,m_pMxDataBase);

		// 释放链表。
		Mx::mcutRelRb(iter->second);
	}
	m_mapReplaceSysVar.clear();
}