#include "StdAfx.h"
#include "CartoBaseShpManager.h"
#include "Fshape.h"
//**************************************************************************
CartoBaseShpManager::CartoBaseShpManager(void)
{
	cad = NULL;
}
//**************************************************************************
CartoBaseShpManager::~CartoBaseShpManager(void)
{
}
bool CartoBaseShpManager::ExportShp(Cb_file *file, Cb_file*fshx, SetEntities *entities, int type)
{
	Fshape shp;
	if(type == CartoBaseTypes::Ndef)
	{
		if(entities->getNumberEntities()>0)
			type = entities->getEntity(0).entity()->type;
		else
			type=CartoBaseTypes::Line;
	}
	switch(type)
	{
	case(CartoBaseTypes::Point):
		{
			return ExportPts(file,fshx, entities);
		}
	case(CartoBaseTypes::Line):
		{
			return ExportLine(file,fshx, entities);
		}
	default:
		return false;

	}
	return true;
}
//**************************************************************************
bool CartoBaseShpManager::ImportShp(Cb_file *file)
{
	Fshape shp;
	if(!shp.leeHead(file))
		return false;
	switch(shp.getType())
	{
	case(Fshape::Rpoint):
	case(Fshape::RMpoint):
		return ImportPoints(&shp);
		break;
	case(Fshape::RPolyLine):
		return Importline(&shp);
		break;
	case(Fshape::Rpolygon):
		return ImportPoligon(&shp);
		break;
	}
	return false;
}
//**************************************************************************
bool CartoBaseShpManager::Importline(Fshape *shp)
{
	CartoLine lin;
	lin.idPropsP = cad->addDefaultPaintedProps();
	double *pt;
	//piner indice de color
	while(shp->LeeRecord())
	{
		int nn = shp->getNp();
		for(int p=0; p<nn; p++)
		{
			lin.pts.n = 0;
			int np = shp->getNPt(p);
			if(np<=0)
				continue;
			if(!(lin.pts+=np))
				return false;

			lin.pts.n = np;
			for(int i = 0; i<np; i++)
			{
				 pt= shp->get(p,i);
				 lin.pts[i][0]=pt[0];
				 lin.pts[i][1]=pt[1];
				 lin.pts[i][2]=0;
			}
			if(!cad->add(lin))
				return false;
		}
	}
	return true;
}
//**************************************************************************
bool CartoBaseShpManager::ImportPoints(Fshape *shp)
{
	CartoPto pto;
	pto.pt[2]=0;
	pto.idPropsP = cad->addDefaultPaintedProps();
	
	//piner indice de color
	while(shp->LeeRecord())
	{
		int i = 0;
		double *p = shp->get(i++);
		while(p)
		{
			pto.pt[0] = p[0];
			pto.pt[1] = p[1];
			if(!cad->add(pto))
				return false;
			p = shp->get(i++);
		}
	}
	return true;
}
//**************************************************************************
bool CartoBaseShpManager::ExportPts(Cb_file *file,Cb_file *fshx, SetEntities *entities)
{
	Fshape shp;
	Cgarray<double> pts;
	int nn = 100;
	if(nn>entities->getNumberEntities())
		nn = entities->getNumberEntities();
	if(!(pts+=(nn*2)))
		return false;//sin memoria

	if(!shp.set(Fshape::Rpoint,2,file, fshx))
		return false;
	int j =0;
	pts.n = nn*2;
	for(int i = 0; i<entities->getNumberEntities(); i++)
	{
		if(j == pts.n)
		{
			if(!shp.grabaRecord(&pts))
				return false;
			j = 0;
		}
		CartoBaseInfoEntity ee =entities->getEntity(i);
		if(ee.entity()->type != CartoBaseTypes::Point)
			continue;
		memcpy(&pts[j],ee.pto()->pts,sizeof(double)*2);
		j+=2;
	}
	pts.n = j;
	if(j>0 && !shp.grabaRecord(&pts))
		return false;
	return shp.FinGraba();
}
//**************************************************************************
bool CartoBaseShpManager::ExportLine(Cb_file *file,Cb_file *fshx, SetEntities *entities)
{
	Fshape shp;
	Cgarray<double> pts;
	Cgarray<int> parts;
	CartoBaseLine *l;
	int nn = 1;
	if(nn>entities->getNumberEntities())
		nn = entities->getNumberEntities();
	if(!(parts+=nn))
		return false;
	if(!shp.set(Fshape::RPolyLine, 2,file,fshx))
		return false;
	int j =0;
	parts.n = nn;
	for(int i = 0; i<entities->getNumberEntities(); i++)
	{
		if(j >= parts.n)
		{
			if(!shp.grabaRecord(&pts, &parts))
				return false;
			j = 0;
			pts.n =0;
		}
		CartoBaseInfoEntity ee =entities->getEntity(i);
		if(ee.entity()->type != CartoBaseTypes::Line)
			continue;
		l =ee.line();
		if(!l)
			continue;
		parts[j++] = pts.n;
		
		if(!(pts+=(l->n*2)))
			return false;
		CartoBasePoint *pp = entities->getEntity(i).pto();
		for(int p = 0; p<l->n; p++)
		{
			pts.n=pts.n+2;
			memcpy(&pts[pts.n-2],pp[p].pts,sizeof(double)*2);
		}
	}
	parts.n = j;
	if(j>0 && !shp.grabaRecord(&pts, &parts))
		return false;
	return shp.FinGraba();
}
//**************************************************************************
bool CartoBaseShpManager::ImportPoligon(Fshape *shp)
{
	return false;
}
//**************************************************************************
