#include "pch.h"
#include "MxHead.h"
#include "Tool.h"


int Tool::MxColor2Dxf(McCmColor color) 
{
	switch(color.colorMethod())
	{
	case McCmColor::kByLayer:
		return 256;
		break;

	case McCmColor::kByBlock:
		return 0;
		break;

	default:
		return color.colorIndex();
	}
	return 7;
}

McCmColor Tool::DxfColor2Mx(int num, bool comp) 
{
	McCmColor ret;
	ret.setColorMethod(McCmColor::kByColor);
	ret.setColorIndex(MrxDbgUtils::kWhite);

	if (comp) 
	{
		ret.setColorMethod(McCmColor::kByColor);

		switch(num) 
		{
		case 0:
			ret.setColorIndex(MrxDbgUtils::kWhite);
			break;
		case 1:
			ret.setColorIndex(MrxDbgUtils::kBlue);
			break;
		case 2:
			ret.setColorIndex(MrxDbgUtils::kGreen);
			break;
		case 3:
			ret.setColorIndex(MrxDbgUtils::kCyan);
			break;
		case 4:
			ret.setColorIndex(MrxDbgUtils::kRed);
			break;
		case 5:
			ret.setColorIndex(MrxDbgUtils::kMagenta);
			break;
		case 6:
			ret.setColorIndex(MrxDbgUtils::kYellow);
			break;
		case 7:
			ret.setColorIndex(9);
			break;
		case 8:
			ret.setColorIndex(8);
			break;
		case 9:
			ret.setColorIndex(MrxDbgUtils::kBlue);
			break;
		case 10:
			ret.setColorIndex(MrxDbgUtils::kGreen);
			break;
		case 11:
			ret.setColorIndex(MrxDbgUtils::kCyan);
			break;
		case 12:
			ret.setColorIndex(MrxDbgUtils::kRed);
			break;
		case 13:
			ret.setColorIndex(MrxDbgUtils::kMagenta);
			break;
		case 14:
			ret.setColorIndex(MrxDbgUtils::kYellow);
			break;
		case 15:
			ret.setColorIndex(MrxDbgUtils::kWhite);
			break;
		default:
			break;
		}
	} 
	else 
	{
		if (num == 0) 
		{
			ret.setColorMethod(McCmColor::kByBlock);
	
		} 
		else if (num == 256) 
		{
			ret.setColorMethod(McCmColor::kByLayer);	
		}
		else if (num <= 255 && num>=0 ) 
		{
			ret.setColorMethod(McCmColor::kByColor);
			ret.setColorIndex(num);
		} 
		else 
		{
			ret.setColorMethod(McCmColor::kByLayer);
		}
	}

	return ret;
}

McDb::LineWeight Tool::DxfLineWeight2Mx(int iWeight)
{
	if(iWeight < -3 || iWeight > 211)
	{
		return McDb::kLnWt000;
	}
	else
	{
		return (McDb::LineWeight)(iWeight);
	}
}

int  Tool::MxLineWeight2Dxf(McDb::LineWeight weight)
{
	return (int)(weight);
}

bool  Tool::IsSystemDefaultBlockName(const MxString& sName)
{
	if(sName.CompareNoCase(_T("*paper_space")) == 0
		|| sName.CompareNoCase(_T("$paper_space")) ==  0
		|| sName.CompareNoCase(_T("*model_space")) ==  0
		|| sName.CompareNoCase(_T("$model_space")) ==  0
		)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool     Tool::IsZero(const double& diff,const double& epsilon)
{
	return ((-epsilon <= diff) && (diff <= epsilon));
}