//------------------------------------------------------------------------
int g_nLastId = 0;
int g_nCurId = 0;

static int g_nCurRow = 0;
void ReflashRowNum(int nRow)
{
	g_nCurRow = nRow;
}

int GetScriptRowNum()
{
	return g_nCurRow;
}

ents GetLastAddEntId()
{	
	ents arid;
	int i = 0;	
	for(i = g_nLastId + 1; i <= g_nCurId; i++)
	{
		arid.add(i);
	}
	return arid;
}

void outputscript(CString s)
{
	CString sT = "\n";
	sT += X2S(GetScriptRowNum());
	sT += SIGN_CODE;
	sT += X2S(g_nCurId);
	sT += SIGN_CODE;
	sT += s;
	joutputscript(sT.GetBuffer());
}
/*////////////////////////////////////////////////////////////////////////////////
画线
point ptSt - 起点 
point ptEd - 终点
CString sLayer = ""	- 图层含义
int nColor = 256 - 颜色
////////////////////////////////////////////////////////////////////////////////*/
int line(point ptSt, point ptEd, CString sLayer = "", int nColor = 256);
int line(point ptSt, point ptEd, CString sLayer, int nColor)
{		
	CString sPt0 = "";
	X2S(sPt0, ptSt);

	CString sPt1 = "";
	X2S(sPt1, ptEd);

	char ch[4096];
	sprintf(ch, "LINE%s%s%s%s%s%s%s%d",
		SPLITE_ATTVALUE,
		sPt0.GetBuffer(),
		SPLITE_ATTVALUE,
		sPt1.GetBuffer(),
		SPLITE_ATTVALUE,	
		sLayer.GetBuffer(),
		SPLITE_ATTVALUE,
		nColor);
		
	outputscript(ch);	
	return g_nCurId;
}

/*////////////////////////////////////////////////////////////////////////////////
画圆
point ptCen - 中心点 
double dRadius - 半径
CString sLayer = ""	- 图层含义
int nColor = 256 - 颜色
////////////////////////////////////////////////////////////////////////////////*/
int circle(point ptCen, double dRadius, CString sLayer = "", int nColor = 256);
int circle(point ptCen, double dRadius, CString sLayer, int nColor)
{
	CString sPt0 = "";
	X2S(sPt0, ptCen);
	
	char ch[4096];
	sprintf(ch, "CIRCLE%s%s%s%f%s%s%s%d",
		SPLITE_ATTVALUE,
		sPt0.GetBuffer(),
		SPLITE_ATTVALUE,
		dRadius,
		SPLITE_ATTVALUE,	
		sLayer.GetBuffer(),
		SPLITE_ATTVALUE,
		nColor);
	
	outputscript(ch);	
	return g_nCurId;
}

/*////////////////////////////////////////////////////////////////////////////////
画圆弧
point ptCen - 中心点 
double dRadius - 半径
double dAngleSt - 起始角度
double dAngleEd - 终止角度
CString sLayer = ""	- 图层含义
int nColor = 256 - 颜色
////////////////////////////////////////////////////////////////////////////////*/
int arc(point ptCen, double dRadius, double dAngleSt, double dAngleEd, CString sLayer = "", int nColor = 256);
int arc(point ptCen, double dRadius, double dAngleSt, double dAngleEd, CString sLayer, int nColor)
{
	CString sPt0 = "";
	X2S(sPt0, ptCen);

	char ch[4096];
	sprintf(ch, "ARC%s%s%s%f%s%f%s%f%s%s%s%d",
		SPLITE_ATTVALUE,
		sPt0.GetBuffer(),
		SPLITE_ATTVALUE,
		dRadius,
		SPLITE_ATTVALUE,	
		dAngleSt,
		SPLITE_ATTVALUE,	
		dAngleEd,
		SPLITE_ATTVALUE,	
		sLayer.GetBuffer(),
		SPLITE_ATTVALUE,
		nColor);
		
	outputscript(ch);	
	return g_nCurId;
}

/*////////////////////////////////////////////////////////////////////////////////
画圆弧
point pt1 - 圆弧上的点1（三点不共线）
point pt2 - 圆弧上的点2（三点不共线）
point pt3 - 圆弧上的点3（三点不共线）
CString sLayer = ""	- 图层含义
int nColor = 256 - 颜色
////////////////////////////////////////////////////////////////////////////////*/
int arc(point pt1, point pt2, point pt3, CString sLayer = "", int nColor = 256);
int arc(point pt1, point pt2, point pt3, CString sLayer, int nColor)
{
	if( pt1.y == pt2.y && pt2.y == pt3.y)
	{
		//cout<<"三点不构成圆!"<<endl;
		return 0;
	}

	double k1, k2;
	k1 = k2 = 0.0;
	if( pt1.y != pt2.y && pt2.y != pt3.y)
	{
		k1=(pt2.x-pt1.x)/(pt2.y-pt1.y);
		k2=(pt3.x-pt2.x)/(pt3.y-pt2.y);
	}
	if( fabs(k1 - k2) < ZERO)
	{
		//cout<<"三点不构成圆!"<<endl;
		return 0;
	}

	point ptCen;
	double dRadius = 0.0;
	double a=2*(pt2.x-pt1.x);
	double b=2*(pt2.y-pt1.y);
	double c=pt2.x*pt2.x+pt2.y*pt2.y-pt1.x*pt1.x-pt1.y*pt1.y;
	double d=2*(pt3.x-pt2.x);
	double e=2*(pt3.y-pt2.y);
	double f=pt3.x*pt3.x+pt3.y*pt3.y-pt2.x*pt2.x-pt2.y*pt2.y;
	ptCen.x=(b*f-e*c)/(b*d-e*a);
	ptCen.y=(d*c-a*f)/(b*d-e*a);
	//cout<<"圆心为("<<x<<","<<y<<")"<<endl;
	dRadius=GetLength(ptCen, pt1);
	//cout<<"半径为"<<r<<endl;

	double dAngleSt, dAngleEd, dAngleCc;	
	dAngleSt = GetAngle(ptCen, pt1);
	dAngleEd = GetAngle(ptCen, pt3);
	dAngleCc = GetAngle(ptCen, pt2);
	 
	int nDir = 0;
	if(dAngleCc > dAngleSt)
	{
		if(fabs(dAngleEd) < ZERO)
		{
			dAngleEd = 2.0 * PI;
		}

		if(dAngleEd < dAngleCc)
		{
			nDir = 1;
		}
	}
	else
	{
		if(fabs(dAngleEd - 2.0 * PI) < ZERO)
		{
			dAngleEd = 0.0;
		}
		if(dAngleEd < dAngleSt)
		{
			nDir = 1;
		}
	}
	
	if(nDir == 1)
	{
		double dTest = dAngleEd;
		dAngleEd = dAngleSt;
		dAngleSt = dTest;
	}
		
	return arc(ptCen, dRadius, dAngleSt, dAngleEd, sLayer.GetBuffer(), nColor);
}

/*////////////////////////////////////////////////////////////////////////////////
画圆弧
point ptCen - 中心点 
point ptSt - 圆弧上的起始点
point ptEd - 圆弧上的终止点
CString sLayer = ""	- 图层含义
int nColor = 256 - 颜色
////////////////////////////////////////////////////////////////////////////////*/
int arc0(point ptCen, point ptSt, point ptEd, CString sLayer = "", int nColor = 256);
int arc0(point ptCen, point ptSt, point ptEd, CString sLayer, int nColor)
{
	double dRadius, dAngleSt, dAngleEd;
	dRadius = GetDistance(ptSt, ptCen);
	dAngleSt = GetAngle(ptCen, ptSt);
	dAngleEd = GetAngle(ptCen, ptEd);
	
	return arc(ptCen, dRadius, dAngleSt, dAngleEd, sLayer.GetBuffer(), nColor);
}


/*////////////////////////////////////////////////////////////////////////////////
画线性（转角）标注
double dAngle - 转角
point ptSt - 第一尺寸界线定义点
point ptEd - 第二尺寸界线定义点
point ptDim - 尺寸线定义点
CString sDimText = "<>" - 标注文字（"<>"为图纸长度）
CString sDimStyle = "" - 标注样式含义
CString sLayer = ""	- 图层含义
int nColor = 256 - 颜色
////////////////////////////////////////////////////////////////////////////////*/
int dim(double dAngle, point ptSt, point ptEd, point ptDim, CString sDimText = "<>", CString sDimStyle = "", CString sLayer = "", int nColor = 256);
int dim(double dAngle, point ptSt, point ptEd, point ptDim, CString sDimText, CString sDimStyle, CString sLayer, int nColor)
{
	CString sPt0 = "";
	X2S(sPt0, ptSt);
	
	CString sPt1 = "";
	X2S(sPt1, ptEd);

	CString sPt2 = "";
	X2S(sPt2, ptDim);

	char ch[4096];
	sprintf(ch, "DIM%s%d%s%f%s%s%s%s%s%s%s%s%s%s%s%s%s%d%s%s",	
	SPLITE_ATTVALUE,
	0,
	SPLITE_ATTVALUE,
	dAngle,
	SPLITE_ATTVALUE,
	sPt0.GetBuffer(),
	SPLITE_ATTVALUE,
	sPt1.GetBuffer(),
	SPLITE_ATTVALUE,
	sPt2.GetBuffer(),	
	SPLITE_ATTVALUE,	
	sLayer.GetBuffer(),
	SPLITE_ATTVALUE,	
	sDimText.GetBuffer(),
	SPLITE_ATTVALUE,	
	sDimStyle.GetBuffer(),
	SPLITE_ATTVALUE,
	nColor,
	SPLITE_ATTVALUE,	
	"");

	outputscript(ch);	
	return g_nCurId;
}

/*////////////////////////////////////////////////////////////////////////////////
画对齐标注
point ptSt - 第一尺寸界线定义点
point ptEd - 第二尺寸界线定义点
point ptDim - 尺寸线定义点
CString sDimText = "<>" - 标注文字（"<>"为图纸长度）
CString sDimStyle = "" - 标注样式含义
CString sLayer = ""	- 图层含义
int nColor = 256 - 颜色
////////////////////////////////////////////////////////////////////////////////*/
int dim(point ptSt, point ptEd, point ptDim, CString sDimText = "<>", CString sDimStyle = "", CString sLayer = "", int nColor = 256);
int dim(point ptSt, point ptEd, point ptDim, CString sDimText, CString sDimStyle, CString sLayer, int nColor)
{
	CString sPt0 = "";
	X2S(sPt0, ptSt);
	
	CString sPt1 = "";
	X2S(sPt1, ptEd);
	
	CString sPt2 = "";
	X2S(sPt2, ptDim);
	
	char ch[4096];
	sprintf(ch, "DIM%s%d%s%f%s%s%s%s%s%s%s%s%s%s%s%s%s%d%s%s",
		SPLITE_ATTVALUE,
		1,
		SPLITE_ATTVALUE,
		0.0,
		SPLITE_ATTVALUE,
		sPt0.GetBuffer(),
		SPLITE_ATTVALUE,
		sPt1.GetBuffer(),
		SPLITE_ATTVALUE,
		sPt2.GetBuffer(),		
		SPLITE_ATTVALUE,	
		sLayer.GetBuffer(),
		SPLITE_ATTVALUE,	
		sDimText.GetBuffer(),
		SPLITE_ATTVALUE,	
		sDimStyle.GetBuffer(),
		SPLITE_ATTVALUE,
		nColor,
		SPLITE_ATTVALUE,	
		"");
	
	outputscript(ch);	
	return g_nCurId;
}

/*////////////////////////////////////////////////////////////////////////////////
画角度标注(三点)
point ptCen - 圆心点
point ptSt - 第一尺寸界线定义点
point ptEd - 第二尺寸界线定义点
point ptDim - 尺寸线定义点
CString sDimText = "<>" - 标注文字（"<>"为图纸长度）
CString sDimStyle = "" - 标注样式含义
CString sLayer = ""	- 图层含义
int nColor = 256 - 颜色
////////////////////////////////////////////////////////////////////////////////*/
int dima(point ptCen, point ptSt, point ptEd, point ptDim, CString sDimText = "<>", CString sDimStyle = "", CString sLayer = "", int nColor = 256);
int dima(point ptCen, point ptSt, point ptEd, point ptDim, CString sDimText, CString sDimStyle, CString sLayer, int nColor)
{
	CString sPt0 = "";
	X2S(sPt0, ptSt);
	
	CString sPt1 = "";
	X2S(sPt1, ptEd);
	
	CString sPt2 = "";
	X2S(sPt2, ptDim);

	CString sPt3 = "";
	X2S(sPt3, ptCen);
	
	char ch[4096];
	sprintf(ch, "DIM%s%d%s%f%s%s%s%s%s%s%s%s%s%s%s%s%s%d%s%s%s%s%s%s%s%s%s%s%s%s",
		SPLITE_ATTVALUE,
		4,
		SPLITE_ATTVALUE,
		0.0,
		SPLITE_ATTVALUE,
		"",
		SPLITE_ATTVALUE,
		sPt0.GetBuffer(),
		SPLITE_ATTVALUE,
		sPt2.GetBuffer(),		
		SPLITE_ATTVALUE,	
		sLayer.GetBuffer(),
		SPLITE_ATTVALUE,	
		sDimText.GetBuffer(),
		SPLITE_ATTVALUE,	
		sDimStyle.GetBuffer(),
		SPLITE_ATTVALUE,
		nColor,
		SPLITE_ATTVALUE,	
		"", 
		SPLITE_ATTVALUE,	
		"", 
		SPLITE_ATTVALUE,	
		"0",
		SPLITE_ATTVALUE,	
		sPt3.GetBuffer(),	
		SPLITE_ATTVALUE,	
		sPt1.GetBuffer(),	
		SPLITE_ATTVALUE,	
		"0"
		);
	
	outputscript(ch);	
	return g_nCurId;
}

/*////////////////////////////////////////////////////////////////////////////////
画角度标注(四点)
point ptSt1 - 第一条线起点
point ptEd1 - 第一条线终点
point ptSt2 - 第二条线起点
point ptEd2 - 第二条线终点
point ptDim - 尺寸线定义点
CString sDimText = "<>" - 标注文字（"<>"为图纸长度）
CString sDimStyle = "" - 标注样式含义
CString sLayer = ""	- 图层含义
int nColor = 256 - 颜色
////////////////////////////////////////////////////////////////////////////////*/
int dima(point ptSt1, point ptEd1, point ptSt2, point ptEd2, point ptDim, CString sDimText = "<>", CString sDimStyle = "", CString sLayer = "", int nColor = 256);
int dima(point ptSt1, point ptEd1, point ptSt2, point ptEd2, point ptDim, CString sDimText, CString sDimStyle, CString sLayer, int nColor)
{
	CString sPt0 = "";
	X2S(sPt0, ptSt1);
	
	CString sPt1 = "";
	X2S(sPt1, ptEd1);
	
	CString sPt2 = "";
	X2S(sPt2, ptDim);

	CString sPt3 = "";
	X2S(sPt3, ptSt2);

	CString sPt4 = "";
	X2S(sPt4, ptEd2);
	
	char ch[4096];
	sprintf(ch, "DIM%s%d%s%f%s%s%s%s%s%s%s%s%s%s%s%s%s%d%s%s%s%s%s%s%s%s%s%s%s%s",
		SPLITE_ATTVALUE,
		4,
		SPLITE_ATTVALUE,
		0.0,
		SPLITE_ATTVALUE,
		sPt0.GetBuffer(),
		SPLITE_ATTVALUE,
		sPt1.GetBuffer(),
		SPLITE_ATTVALUE,
		sPt2.GetBuffer(),		
		SPLITE_ATTVALUE,	
		sLayer.GetBuffer(),
		SPLITE_ATTVALUE,	
		sDimText.GetBuffer(),
		SPLITE_ATTVALUE,	
		sDimStyle.GetBuffer(),
		SPLITE_ATTVALUE,
		nColor,
		SPLITE_ATTVALUE,	
		"", 
		SPLITE_ATTVALUE,	
		"", 
		SPLITE_ATTVALUE,	
		"1",
		SPLITE_ATTVALUE,	
		sPt3.GetBuffer(),	
		SPLITE_ATTVALUE,	
		sPt4.GetBuffer(),	
		SPLITE_ATTVALUE,	
		"0"
		);
	
	outputscript(ch);	
	return g_nCurId;
}

/*////////////////////////////////////////////////////////////////////////////////
半径标注
point ptCen - 圆心点
point ptR - 圆上点
point ptDim - 尺寸线定义点
CString sDimText = "<>" - 标注文字（"<>"为图纸长度）
CString sDimStyle = "" - 标注样式含义
CString sLayer = ""	- 图层含义
int nColor = 256 - 颜色
////////////////////////////////////////////////////////////////////////////////*/
int dimr(point ptCen, point ptR, double dLen = 0.0, CString sDimText = "<>", CString sDimStyle = "", CString sLayer = "", int nColor = 256);
int dimr(point ptCen, point ptR, double dLen, CString sDimText, CString sDimStyle, CString sLayer, int nColor)
{
	CString sPt0 = "";
	X2S(sPt0, ptCen);
	
	CString sPt1 = "";
	X2S(sPt1, ptR);
	
	CString sPt2 = "";
	X2S(sPt2, dLen);
	
	char ch[4096];
	//				   0   1   2   3   4   5   6   7   8   9  10  11  12  13
	sprintf(ch, "DIM%s%d%s%f%s%s%s%s%s%s%s%s%s%s%s%s%s%d%s%s%s%s%s%s%s%s%s%s%s%s",
		SPLITE_ATTVALUE,
		2,			
		SPLITE_ATTVALUE,
		0.0,
		SPLITE_ATTVALUE,
		"",
		SPLITE_ATTVALUE,
		"",
		SPLITE_ATTVALUE,
		"",		
		SPLITE_ATTVALUE,	
		sLayer.GetBuffer(),
		SPLITE_ATTVALUE,	
		sDimText.GetBuffer(),
		SPLITE_ATTVALUE,	
		sDimStyle.GetBuffer(),
		SPLITE_ATTVALUE,
		nColor,
		SPLITE_ATTVALUE,	
		"", 
		SPLITE_ATTVALUE,	
		"", 
		SPLITE_ATTVALUE,	
		"0",
		SPLITE_ATTVALUE,	
		sPt0.GetBuffer(),	
		SPLITE_ATTVALUE,	
		sPt1.GetBuffer(),	
		SPLITE_ATTVALUE,	
		sPt2.GetBuffer(),
		);
	
	outputscript(ch);	
	return g_nCurId;
}

/*////////////////////////////////////////////////////////////////////////////////
直径标注
point ptCen - 圆心点
point ptR - 圆上点
point ptDim - 尺寸线定义点
CString sDimText = "<>" - 标注文字（"<>"为图纸长度）
CString sDimStyle = "" - 标注样式含义
CString sLayer = ""	- 图层含义
int nColor = 256 - 颜色
////////////////////////////////////////////////////////////////////////////////*/
int dimd(point ptCen, point ptR, double dLen = 0.0, CString sDimText = "<>", CString sDimStyle = "", CString sLayer = "", int nColor = 256);
int dimd(point ptCen, point ptR, double dLen, CString sDimText, CString sDimStyle, CString sLayer, int nColor)
{
	CString sPt0 = "";
	X2S(sPt0, ptCen);
	
	CString sPt1 = "";
	X2S(sPt1, ptR);
	
	CString sPt2 = "";
	X2S(sPt2, dLen);
	
	char ch[4096];
	sprintf(ch, "DIM%s%d%s%f%s%s%s%s%s%s%s%s%s%s%s%s%s%d%s%s%s%s%s%s%s%s%s%s%s%s",
		SPLITE_ATTVALUE,
		3,
		SPLITE_ATTVALUE,
		0.0,
		SPLITE_ATTVALUE,
		"",
		SPLITE_ATTVALUE,
		"",
		SPLITE_ATTVALUE,
		"",		
		SPLITE_ATTVALUE,	
		sLayer.GetBuffer(),
		SPLITE_ATTVALUE,	
		sDimText.GetBuffer(),
		SPLITE_ATTVALUE,	
		sDimStyle.GetBuffer(),
		SPLITE_ATTVALUE,
		nColor,
		SPLITE_ATTVALUE,	
		"", 
		SPLITE_ATTVALUE,	
		"", 
		SPLITE_ATTVALUE,	
		"0",
		SPLITE_ATTVALUE,	
		sPt0.GetBuffer(),	
		SPLITE_ATTVALUE,	
		sPt1.GetBuffer(),	
		SPLITE_ATTVALUE,	
		sPt2.GetBuffer(),	
		);
	
	outputscript(ch);	
	return g_nCurId;
}

/*////////////////////////////////////////////////////////////////////////////////
点标注
point pt - 点位置
CString sLayer = ""	- 图层含义
int nColor = 256 - 颜色
////////////////////////////////////////////////////////////////////////////////*/
int dimp(point pt, CString sLayer = "", int nColor = 256);
int dimp(point pt, CString sLayer, int nColor)
{
	CString sPt = "";
	X2S(sPt, pt);
		
	char ch[4096];
	sprintf(ch, "DIM%s%d%s%f%s%s%s%s%s%s%s%s%s%s%s%s%s%d%s%s%s%s%s%s%s%s%s%s%s%s",
		SPLITE_ATTVALUE,
		5,
		SPLITE_ATTVALUE,
		0.0,
		SPLITE_ATTVALUE,
		sPt.GetBuffer(),
		SPLITE_ATTVALUE,
		"",
		SPLITE_ATTVALUE,
		"",
		SPLITE_ATTVALUE,	
		sLayer.GetBuffer(),
		SPLITE_ATTVALUE,	
		"",
		SPLITE_ATTVALUE,	
		"",
		SPLITE_ATTVALUE,
		nColor,
		SPLITE_ATTVALUE,	
		"", 
		SPLITE_ATTVALUE,	
		"", 
		SPLITE_ATTVALUE,	
		"0",
		SPLITE_ATTVALUE,	
		"",
		SPLITE_ATTVALUE,	
		"",
		SPLITE_ATTVALUE,	
		"0"
		);
	
	outputscript(ch);	
	return g_nCurId;
}





/*////////////////////////////////////////////////////////////////////////////////
画文字
point pt - 文字插入点
double dAngle - 倾斜角度（弧度）
CString string - 文字内容
double dHeight = 3.0 - 文字高度
double dWFactor = 0.75 - 文字宽高比
int nHType = 1 - 水平对齐方式（0 居左，1 居中，2居右）
int nVType = 1 - 垂直对齐方式（0 上对齐，1 居中，2下对齐）
CString sStyle = "" - 文字样式含义
CString sLayer = ""	- 图层含义
int nColor = 256 - 颜色
////////////////////////////////////////////////////////////////////////////////*/
int text(point pt, double dAngle, CString string, double dHeight = 3.0, double dWFactor = 0.75, 
		 int nHType = 1, int nVType = 1, CString sStyle = "", CString sLayer = "", int nColor = 256);
int text(point pt, double dAngle, CString string, double dHeight, double dWFactor, 
		 int nHType, int nVType, CString sStyle, CString sLayer, int nColor)
{
	CString sPt0 = "";
	X2S(sPt0, pt);

	char ch[4096];
					 //0 1 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 20
	sprintf(ch, "TEXT%s%s%s%s%s%s%s%f%s%f%s%f%s%d%s%d%s%d%s%s",
	SPLITE_ATTVALUE,
	sPt0.GetBuffer(),	
	SPLITE_ATTVALUE,
	sLayer.GetBuffer(),
	SPLITE_ATTVALUE,
	sStyle.GetBuffer(),
	SPLITE_ATTVALUE,
	dHeight,
	SPLITE_ATTVALUE,
	dWFactor,
	SPLITE_ATTVALUE,
	dAngle,
	SPLITE_ATTVALUE,
	nHType,
	SPLITE_ATTVALUE,
	nVType,
	SPLITE_ATTVALUE,
	nColor,
	SPLITE_ATTVALUE,
	//string.GetBuffer()
	);
	CString sOut = "";
	sOut += ch;
	sOut += string;
	
	outputscript(sOut.GetBuffer());	
	return g_nCurId;
}

/*////////////////////////////////////////////////////////////////////////////////
画多段线（多义线）
points arpt - 节点数组
doubles arBulge - 凸度数组
double dLineW - 线宽
CString sLayer = ""	- 图层含义
int nColor = 256 - 颜色
////////////////////////////////////////////////////////////////////////////////*/
int pline(points& arpt, doubles& arBulge, double dLineW = 0.0, CString sLayer = "", int nColor = 256);
int pline(points& arpt, doubles& arBulge, double dLineW, CString sLayer, int nColor)
{	
	CString s1 = "";
	X2S(s1, arpt);
	
	CString s2 = "";	
	X2S(s2, arBulge);
	
	CString sT = "PLINE";
	sT += SPLITE_ATTVALUE;
	sT += s1.GetBuffer();
	sT += SPLITE_ATTVALUE;
	sT += s2.GetBuffer();
	sT += SPLITE_ATTVALUE;

	char ch[4096];	
						  //0 1 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 20
	sprintf(ch, "%f%s%d%s%s%s%d%s%s",		
		dLineW,
		SPLITE_ATTVALUE,	
		0,
		SPLITE_ATTVALUE,	
		sLayer.GetBuffer(),
		SPLITE_ATTVALUE,
		nColor,
		SPLITE_ATTVALUE,	
		"");
	sT += ch;

	outputscript(sT.GetBuffer());	
	return g_nCurId;
}

int pline(points& arpt, doubles& arBulge, doubles ardLineWSt, doubles ardLineWEd, CString sLayer = "", int nColor = 256);
int pline(points& arpt, doubles& arBulge, doubles ardLineWSt, doubles ardLineWEd, CString sLayer, int nColor)
{	
	CString s1 = "";
	X2S(s1, arpt);
	
	CString s2 = "";	
	X2S(s2, arBulge);

	CString s3 = "";	
	X2S(s3, ardLineWSt);

	CString s4 = "";	
	X2S(s4, ardLineWEd);
	
	CString sT = "PLINE";
	sT += SPLITE_ATTVALUE;
	sT += s1.GetBuffer();
	sT += SPLITE_ATTVALUE;
	sT += s2.GetBuffer();
	sT += SPLITE_ATTVALUE;
	
	char ch[4096];	
	//0 1 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 20
	sprintf(ch, "%f%s%d%s%s%s%d%s%s",		
		0,
		SPLITE_ATTVALUE,	
		0,
		SPLITE_ATTVALUE,	
		sLayer.GetBuffer(),
		SPLITE_ATTVALUE,
		nColor,
		SPLITE_ATTVALUE,	
		"");
	sT += ch;

	sT += SPLITE_ATTVALUE;
	sT += s3.GetBuffer();
	sT += SPLITE_ATTVALUE;
	sT += s4.GetBuffer();
	
	outputscript(sT.GetBuffer());	
	return g_nCurId;
}


/*////////////////////////////////////////////////////////////////////////////////
画多段线（多义线）
points arpt - 节点数组
double dLineW - 线宽
CString sLayer = ""	- 图层含义
int nColor = 256 - 颜色
////////////////////////////////////////////////////////////////////////////////*/
int pline(points& arpt, double dLineW = 0.0, CString sLayer = "", int nColor = 256);
int pline(points& arpt, double dLineW, CString sLayer, int nColor)
{
	doubles arBulge;
	return pline(arpt, arBulge, dLineW, sLayer.GetBuffer(), nColor);
}

int line(point ptSt, point ptEd, double dLineW, CString sLayer = "", int nColor = 256);
int line(point ptSt, point ptEd, double dLineW, CString sLayer, int nColor)
{
	points arpt;
	arpt.add(ptSt);
	arpt.add(ptEd);

	return pline(arpt, dLineW, sLayer.GetBuffer(), nColor);
}

int line(point ptSt, point ptEd, double dLineWSt, double dLineWEd, CString sLayer = "", int nColor = 256);
int line(point ptSt, point ptEd, double dLineWSt, double dLineWEd, CString sLayer, int nColor)
{
	points arpt;
	arpt.add(ptSt);
	arpt.add(ptEd);

	doubles arBulge, ardSt, ardEd;
	ardSt.add(dLineWSt);
	ardEd.add(dLineWEd);

	return pline(arpt, arBulge, ardSt, ardEd, sLayer.GetBuffer(), nColor);
}

ents rect(point ptCen, double dL, double dW, CString sLayer = "", int nColor = 256, double dAngle = 0);
ents rect(point ptCen, double dL, double dW, CString sLayer, int nColor, double dAngle)
{
	point pt1, pt2, pt3, pt4;
	pt1 = pt2 = pt3 = pt4 = ptCen;

	move(pt1, dL / 2.0, PI + dAngle);
	move(pt4, dL / 2.0, PI + dAngle);
	move(pt2, dL / 2.0, 0 + dAngle);
	move(pt3, dL / 2.0, 0 + dAngle);

	move(pt1, dW / 2.0, PI / 2.0 + dAngle);
	move(pt2, dW / 2.0, PI / 2.0 + dAngle);
	move(pt3, dW / 2.0, 3.0 * PI / 2.0 + dAngle);
	move(pt4, dW / 2.0, 3.0 * PI / 2.0 + dAngle);
	
	ents arid;

	arid.add(line(pt1, pt2, sLayer.GetBuffer(), nColor));
	arid.add(line(pt2, pt3, sLayer.GetBuffer(), nColor));
	arid.add(line(pt3, pt4, sLayer.GetBuffer(), nColor));
	arid.add(line(pt4, pt1, sLayer.GetBuffer(), nColor));
	
	return arid;
}

int circle(point ptCen, double dRadius, double dLineW, CString sLayer = "", int nColor = 256, double dAngle = 0);
int circle(point ptCen, double dRadius, double dLineW, CString sLayer, int nColor, double dAngle)
{
	point pt1,pt2;
	pt1 = pt2 = ptCen;
	move(pt1, dRadius, PI + dAngle);
	move(pt2, dRadius, dAngle);

	points arpt;
	doubles arBulge;
	arpt.add(pt1);
	arpt.add(pt2);
	arpt.add(pt1);
	arBulge.add(1.0);
	arBulge.add(1.0);
	arBulge.add(0.0);
	
	return pline(arpt, arBulge, dLineW, sLayer.GetBuffer(), nColor);
}


/*////////////////////////////////////////////////////////////////////////////////
画填充
points arpt - 边界点数组
CString sHatch = "Solid" - 填充样式
double dScale = 1.0 - 填充比例
CString sLayer = ""	- 图层含义
int nColor = 256 - 颜色
////////////////////////////////////////////////////////////////////////////////*/
int hatch(points arpt, CString sHatch = "Solid", double dScale = 1.0, CString sLayer = "", int nColor = 256);
int hatch(points  arpt, CString sHatch, double dScale, CString sLayer, int nColor)
{
	CString s1 = "";
	X2S(s1, arpt);
	
	CString sTmp = "";
	char ch[4096];
	sprintf(ch, "PLINE%s", SPLITE_ATTVALUE);
	sTmp += ch;
	sTmp += s1.GetBuffer();
	
	char ch1[4096];
	//0 1 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 20
	sprintf(ch1, "%s%s%s%f%s%d%s%s%s%d%s%s",		
		SPLITE_ATTVALUE,
		"",
		SPLITE_ATTVALUE,
		dScale,
		SPLITE_ATTVALUE,	
		1,
		SPLITE_ATTVALUE,	
		sLayer.GetBuffer(),
		SPLITE_ATTVALUE,
		nColor,
		SPLITE_ATTVALUE,	
		sHatch.GetBuffer());
	sTmp += ch1;
	
	outputscript(sTmp.GetBuffer());	
	return g_nCurId;
}

int hatch(points arpt, CString sHatch = "Solid", double dScale = 1.0, double dAngle = 0.0, CString sLayer = "", int nColor = 256);
int hatch(points  arpt, CString sHatch, double dScale, double dAngle, CString sLayer, int nColor)
{
	CString s1 = "";
	X2S(s1, arpt);
	
	CString sTmp = "";
	char ch[4096];
	sprintf(ch, "PLINE%s", SPLITE_ATTVALUE);
	sTmp += ch;
	sTmp += s1.GetBuffer();

	CString s3 = "";
	X2S(s3, dAngle);
	
	char ch1[4096];
	//0 1 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 20
	sprintf(ch1, "%s%s%s%f%s%d%s%s%s%d%s%s",		
		SPLITE_ATTVALUE,
		s3.GetBuffer(),
		SPLITE_ATTVALUE,
		dScale,
		SPLITE_ATTVALUE,	
		1,
		SPLITE_ATTVALUE,	
		sLayer.GetBuffer(),
		SPLITE_ATTVALUE,
		nColor,
		SPLITE_ATTVALUE,	
		sHatch.GetBuffer());
	sTmp += ch1;
	
	outputscript(sTmp.GetBuffer());	
	return g_nCurId;
}

/*////////////////////////////////////////////////////////////////////////////////
画填充
points arpt - 边界点数组
CString sHatch = "Solid" - 填充样式
CString sLayer = ""	- 图层含义
int nColor = 256 - 颜色
////////////////////////////////////////////////////////////////////////////////*/
int hatch(points arpt, CString sHatch = "Solid", CString sLayer = "", int nColor = 256);
int hatch(points  arpt, CString sHatch, CString sLayer, int nColor)
{
	CString s1 = "";
	X2S(s1, arpt);
	
	
	CString sTmp = "";
	char ch[4096];
	sprintf(ch, "PLINE%s", SPLITE_ATTVALUE);
	sTmp += ch;
	sTmp += s1.GetBuffer();
	
	char ch1[4096];
	//0 1 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 20
	sprintf(ch1, "%s%s%s%f%s%d%s%s%s%d%s%s",		
		SPLITE_ATTVALUE,
		"",
		SPLITE_ATTVALUE,
		1.0,
		SPLITE_ATTVALUE,	
		1,
		SPLITE_ATTVALUE,	
		sLayer.GetBuffer(),
		SPLITE_ATTVALUE,
		nColor,
		SPLITE_ATTVALUE,	
		sHatch.GetBuffer());
	sTmp += ch1;
	
	outputscript(sTmp.GetBuffer());	
	return g_nCurId;
}

/*////////////////////////////////////////////////////////////////////////////////
标注样式比例
CString sSymbol - 标注样式含义
CString sName = "" - 标注样式名称（输入""空为自动生成）
double dDScale = 1.0 - 图戳比例
double dPScale = 1.0 - 打印比例
double dUScale = 1.0 - 单位比例
////////////////////////////////////////////////////////////////////////////////*/
int dimTextScale(CString sSymbol, CString sName = "", double dDScale = 1.0, double dPScale = 1.0, double dUScale = 1.0);
int dimTextScale(CString sSymbol, CString sName, double dDScale, double dPScale, double dUScale)
{
	char ch[4096];	
	sprintf(ch, "dimTextScale%s%s%s%s%s%f%s%f%s%f",
		SPLITE_ATTVALUE,
		sSymbol.GetBuffer(),
		SPLITE_ATTVALUE,
		sName.GetBuffer(),
		SPLITE_ATTVALUE,
		dDScale,
		SPLITE_ATTVALUE,
		dPScale,
		SPLITE_ATTVALUE,
		dUScale);
	
	outputscript(ch);	
	return g_nCurId;
}

//-----------------------------------------------------------------------------
//定义图块
//int nDelSc 0 - 保留 1 - 转化为块 2 - 删除
int blockMake(ents ids, point pt, CString sName, int nDelSc = 1);
int blockMake(ents ids, point pt, CString sName, int nDelSc)
{
	CString s1 = "";
	X2S(s1, ids);
	CString s2 = "";
	X2S(s2, pt);
	
	CString sTmp = "";
	char ch[4096];	
	sprintf(ch, "BlockMake%s", SPLITE_ATTVALUE);
	sTmp += ch;
	sTmp += s1.GetBuffer();

	char ch1[4096];	
	sprintf(ch1, "%s%s%s%s%s%d",
		SPLITE_ATTVALUE,
		s2.GetBuffer(),
		SPLITE_ATTVALUE,
		sName.GetBuffer(),
		SPLITE_ATTVALUE,
		nDelSc);

	sTmp += ch1;
	JCallFunction(sTmp.GetBuffer(), sTmp.GetBuffer(), "n");	
	return g_nJResult0;
}

//插入图块
//int nReplace 0 - 不替换 1 - 替换
int insert(CString sFile, CString sName, int nReplace = 1);
int insert(CString sFile, CString sName, int nReplace)
{	
	CString sTmp = "";	
	
	char ch1[4096];	
	sprintf(ch1, "%s%s%s%s%s%s%d",
		sName.GetBuffer(),
		SPLITE_ATTVALUE,
		sFile.GetBuffer(),
		SPLITE_ATTVALUE,
		sName.GetBuffer(),
		SPLITE_ATTVALUE,
		nReplace);
	
	sTmp += ch1;
	JCallFunction("AcadBlockInsert", sTmp.GetBuffer(), "n");	
	return g_nJResult0;
}

int insert(CString sFile, CString sName, CString sNewName, int nReplace = 1);
int insert(CString sFile, CString sName, CString sNewName, int nReplace)
{	
	CString sTmp = "";	
	
	char ch1[4096];	
	sprintf(ch1, "%s%s%s%s%s%s%d",
		sName.GetBuffer(),
		SPLITE_ATTVALUE,
		sFile.GetBuffer(),
		SPLITE_ATTVALUE,
		sNewName.GetBuffer(),
		SPLITE_ATTVALUE,
		nReplace);
	
	sTmp += ch1;
	JCallFunction("AcadBlockInsert", sTmp.GetBuffer(), "n");	
	return g_nJResult0;
}

//绘制图块
int block(CString sBlockName, point pt, double dAngle, double dXScale, double dYScale, double dZScale, CString sLayer = "", int nColor = 256, int nExplode = 0);
int block(CString sBlockName, point pt, double dAngle, double dXScale, double dYScale, double dZScale, CString sLayer, int nColor, int nExplode)
{
	CString s1 = "";
	X2S(s1, pt);
	
	char ch[4096];	
	sprintf(ch, "BLOCK%s%d%s%s%s%s%s%f%s%f,%f,%f%s%s%s%s",
		SPLITE_ATTVALUE,
		0,
		SPLITE_ATTVALUE,
		sBlockName.GetBuffer(),
		SPLITE_ATTVALUE,
		s1.GetBuffer(),
		SPLITE_ATTVALUE,
		dAngle,
		SPLITE_ATTVALUE,
		dXScale,
		dYScale,
		dZScale,
		SPLITE_ATTVALUE,	
		sLayer.GetBuffer(),
		SPLITE_ATTVALUE,
		nColor,
		SPLITE_ATTVALUE,
		nExplode);
	
	outputscript(ch);	
	return g_nCurId;
}

int block(CString sBlockName, point pt, double dAngle, double dScale, CString sLayer = "", int nColor = 256, int nExplode = 0);
int block(CString sBlockName, point pt, double dAngle, double dScale, CString sLayer, int nColor, int nExplode)
{
	CString s1 = "";
	X2S(s1, pt);
	
	char ch[4096];	 //0 1   2   3   4   5         6   7  
	sprintf(ch, "BLOCK%s%d%s%s%s%s%s%f%s%f,%f,%f%s%s%s%d",
		SPLITE_ATTVALUE,
		0,
		SPLITE_ATTVALUE,
		sBlockName.GetBuffer(),
		SPLITE_ATTVALUE,
		s1.GetBuffer(),
		SPLITE_ATTVALUE,
		dAngle,
		SPLITE_ATTVALUE,
		dScale,
		dScale,
		dScale,
		SPLITE_ATTVALUE,	
		sLayer.GetBuffer(),
		SPLITE_ATTVALUE,
		nColor,
		SPLITE_ATTVALUE,
		nExplode);
	
	outputscript(ch);	
	return g_nCurId;
}

int block(int idBlock, point pt, double dAngle, double dXScale, double dYScale, double dZScale, CString sLayer = "", int nColor = 256, int nExplode = 0);
int block(int idBlock, point pt, double dAngle, double dXScale, double dYScale, double dZScale, CString sLayer, int nColor, int nExplode)
{
	CString s1 = "";
	X2S(s1, pt);

	char ch[4096];	 //0 1     2   3   4         5   6 
	sprintf(ch, "BLOCK%s%d%s%s%s%s%f%s%f,%f,%f%s%s%s%d",
		SPLITE_ATTVALUE,
		idBlock,
		SPLITE_ATTVALUE,
		SPLITE_ATTVALUE,
		s1.GetBuffer(),
		SPLITE_ATTVALUE,
		dAngle,
		SPLITE_ATTVALUE,
		dXScale,
		dYScale,
		dZScale,
		SPLITE_ATTVALUE,	
		sLayer.GetBuffer(),
		SPLITE_ATTVALUE,
		nColor,
		SPLITE_ATTVALUE,
		nExplode);
	
	outputscript(ch);	
	return g_nCurId;
}

int block(int idBlock, point pt, double dAngle, double dScale, CString sLayer = "", int nColor = 256, int nExplode = 0);
int block(int idBlock, point pt, double dAngle, double dScale, CString sLayer, int nColor, int nExplode)
{
	CString s1 = "";
	X2S(s1, pt);
	
	char ch[4096];	 //0 1     2   3   4         5   6
	sprintf(ch, "BLOCK%s%d%s%s%s%s%f%s%f,%f,%f%s%s%s%d",
		SPLITE_ATTVALUE,
		idBlock,
		SPLITE_ATTVALUE,
		SPLITE_ATTVALUE,
		s1.GetBuffer(),
		SPLITE_ATTVALUE,
		dAngle,
		SPLITE_ATTVALUE,
		dScale,
		dScale,
		dScale,
		SPLITE_ATTVALUE,	
		sLayer.GetBuffer(),
		SPLITE_ATTVALUE,
		nColor,
		SPLITE_ATTVALUE,
		nExplode);
	
	outputscript(ch);	
	return g_nCurId;
}

ents explode(ent id);
ents explode(ent id)
{		
	char ch[128];	
	sprintf(ch, "%d", id);
	
	JCallFunction("AcadBlockExplode", ch, "n|s");
	
	CString s = g_sJResult0;
	array<CString> ars;
	StringSplite(s.GetBuffer(), ":", ars);
	
	ents arid;
	for(int i = 0; i < ars.getSize(); i++)
	{
		CString sTmp = ars[i];
		ent id = atoi(sTmp.GetBuffer());
		arid.add(id);
	}
	
	return arid;
}

//------------------------------------------------------------------------------
//移动
void moveEnt(ent id, point ptSt, point ptEd)
{
	CString s1 = "";
	X2S(s1, ptSt);

	CString s2 = "";
	X2S(s2, ptEd);
	
	char ch[4096];	
	sprintf(ch, "MOVEENT%s%d%s%s%s%s",
		SPLITE_ATTVALUE,
		id,
		SPLITE_ATTVALUE,
		s1.GetBuffer(),
		SPLITE_ATTVALUE,
		s2.GetBuffer());
		
	outputscript(ch);
}

void moveEnt(ents ids, point ptSt, point ptEd)
{
	CString s1 = "";
	X2S(s1, ptSt);
	
	CString s2 = "";
	X2S(s2, ptEd);

	CString s3 = "";
	X2S(s3, ids);
	
	CString sTmp = "";
	sTmp += "MOVEENT";
	sTmp += SPLITE_ATTVALUE;
	sTmp += s3.GetBuffer();

	char ch[4096];	
	sprintf(ch, "%s%s%s%s",		
		SPLITE_ATTVALUE,
		s1.GetBuffer(),
		SPLITE_ATTVALUE,
		s2.GetBuffer());

	sTmp += ch;
	
	outputscript(sTmp);
}

//旋转
void rotateEnt(ent id, point pt, double dAngle)
{
	CString s1 = "";
	X2S(s1, pt);
		
	char ch[4096];	
	sprintf(ch, "ROTATEENT%s%d%s%s%s%f",
		SPLITE_ATTVALUE,
		id,
		SPLITE_ATTVALUE,
		s1.GetBuffer(),
		SPLITE_ATTVALUE,
		dAngle);
	
	outputscript(ch);
}

void rotateEnt(ents ids, point pt, double dAngle)
{
	CString s1 = "";
	X2S(s1, pt);
	
	CString s3 = "";
	X2S(s3, ids);

	CString sTmp = "";
	sTmp += "ROTATEENT";
	sTmp += SPLITE_ATTVALUE;
	sTmp += s3.GetBuffer();
	
	char ch[4096];	
	sprintf(ch, "%s%s%s%f",
		SPLITE_ATTVALUE,
		s1.GetBuffer(),
		SPLITE_ATTVALUE,
		dAngle);

	sTmp += ch;
	
	outputscript(sTmp.GetBuffer());
}

void rotateEnt(ent id, point pt, double dAngle, point vec)
{
	CString s1 = "";
	X2S(s1, pt);
	
	CString s2 = "";
	X2S(s2, vec);
	
	char ch[4096];	
	sprintf(ch, "%d%s%s%s%f%s%s",
		id,
		SPLITE_ATTVALUE,
		s1.GetBuffer(),
		SPLITE_ATTVALUE,
		dAngle,
		SPLITE_ATTVALUE,
		s2.GetBuffer());
	
	JCallFunction("AcadRotateEnt", ch, "");
}

void rotateEnt(ents ids, point pt, double dAngle, point vec)
{
	CString s1 = "";
	X2S(s1, pt);
	
	CString s3 = "";
	X2S(s3, ids);

	CString s2 = "";
	X2S(s2, vec);

	CString sTmp = "";
	sTmp += s3.GetBuffer();
	
	char ch[4096];	
	sprintf(ch, "%s%s%s%f%s%s",
		SPLITE_ATTVALUE,
		s1.GetBuffer(),
		SPLITE_ATTVALUE,
		dAngle,
		SPLITE_ATTVALUE,
		s2.GetBuffer());

	sTmp += ch;
	
	JCallFunction("AcadRotateEnt", sTmp.GetBuffer(), "");
}

//缩放
void scaleEnt(ent id, point pt, double dScale)
{
	CString s1 = "";
	X2S(s1, pt);
	
	char ch[4096];	
	sprintf(ch, "SCALEENT%s%d%s%s%s%f",
		SPLITE_ATTVALUE,
		id,
		SPLITE_ATTVALUE,
		s1.GetBuffer(),
		SPLITE_ATTVALUE,
		dScale);
	
	outputscript(ch);
}

void scaleEnt(ents ids, point pt, double dScale)
{
	CString s1 = "";
	X2S(s1, pt);
	
	CString s3 = "";
	X2S(s3, ids);

	CString sTmp = "";
	sTmp += "SCALEENT";
	sTmp += SPLITE_ATTVALUE;
	sTmp += s3.GetBuffer();
	
	char ch[4096];	
	sprintf(ch, "%s%s%s%f",
		SPLITE_ATTVALUE,
		s1.GetBuffer(),
		SPLITE_ATTVALUE,
		dScale);

	sTmp += ch;
		
	outputscript(sTmp.GetBuffer());
}

void scaleEnt(ents ids, point pt, point ptScale)
{
	CString s1 = "";
	X2S(s1, pt);
	
	CString s3 = "";
	X2S(s3, ids);

	CString s2 = "";
	X2S(s2, ptScale);

	CString sTmp = "";
	sTmp += "SCALEENT";
	sTmp += SPLITE_ATTVALUE;
	sTmp += s3.GetBuffer();
	
	char ch[4096];	
	sprintf(ch, "%s%s%s%f",
		SPLITE_ATTVALUE,
		s1.GetBuffer(),
		SPLITE_ATTVALUE,
		s2.GetBuffer());
	
	sTmp += ch;	
	outputscript(sTmp.GetBuffer());
}

//拷贝
ents copyEnt(ents ids, point ptSt, point ptEd)
{
	CString s1 = "";
	X2S(s1, ptSt);
	
	CString s2 = "";
	X2S(s2, ptEd);
	
	CString s3 = "";
	X2S(s3, ids);

	CString sTmp = "";
	sTmp += "COPYENT";
	sTmp += SPLITE_ATTVALUE;
	sTmp += s3.GetBuffer();
	
	char ch[8192];	
	sprintf(ch, "%s%s%s%s",
		SPLITE_ATTVALUE,
		s1.GetBuffer(),
		SPLITE_ATTVALUE,
		s2.GetBuffer());
	
	sTmp += ch;	
	outputscript(sTmp.GetBuffer());

	return GetLastAddEntId();
}

ent copyEnt(ent id, point ptSt, point ptEd) 
{
	CString s1 = "";
	X2S(s1, ptSt);
	
	CString s2 = "";
	X2S(s2, ptEd);
	
	CString s3 = "";
	X2S(s3, id);
	
	char ch[4096];	
	sprintf(ch, "COPYENT%s%s%s%s%s%s",
		SPLITE_ATTVALUE,
		s3.GetBuffer(),
		SPLITE_ATTVALUE,
		s1.GetBuffer(),
		SPLITE_ATTVALUE,
		s2.GetBuffer());
	
	outputscript(ch);	
	return g_nCurId;
}

//镜像
ents mirrorEnt(ents ids, point ptSt, point ptEd, int nDelSc = 1)
ents mirrorEnt(ents ids, point ptSt, point ptEd, int nDelSc)
{
	CString s1 = "";
	X2S(s1, ptSt);
	
	CString s2 = "";
	X2S(s2, ptEd);
	
	CString s3 = "";
	X2S(s3, ids);
	
	CString sTmp = "";
	sTmp += "MIRRORENT";
	sTmp += SPLITE_ATTVALUE;
	sTmp += s3.GetBuffer();
	
	char ch[4096];	
	sprintf(ch, "%s%s%s%s%s%d",
		SPLITE_ATTVALUE,
		s1.GetBuffer(),
		SPLITE_ATTVALUE,
		s2.GetBuffer(),
		SPLITE_ATTVALUE,
		nDelSc);
	
	sTmp += ch;	
	outputscript(sTmp.GetBuffer());
		
	return GetLastAddEntId();
}

ent mirrorEnt(ent id, point ptSt, point ptEd, int nDelSc = 1);
ent mirrorEnt(ent id, point ptSt, point ptEd, int nDelSc)
{
	CString s1 = "";
	X2S(s1, ptSt);
	
	CString s2 = "";
	X2S(s2, ptEd);
	
	CString s3 = "";
	X2S(s3, id);
	
	char ch[4096];	
	sprintf(ch, "MIRRORENT%s%s%s%s%s%s%s%d",
		SPLITE_ATTVALUE,
		s3.GetBuffer(),
		SPLITE_ATTVALUE,
		s1.GetBuffer(),
		SPLITE_ATTVALUE,
		s2.GetBuffer(),
		SPLITE_ATTVALUE,
		nDelSc);
	
	outputscript(ch);	
	return g_nCurId;
}

void delEnt(ents ids, int nDel = 1);
void delEnt(ents ids, int nDel)
{
	CString s3 = "";
	X2S(s3, ids);

	CString sTmp = "";
	sTmp += "DELENT";
	sTmp += SPLITE_ATTVALUE;
	sTmp += s3.GetBuffer();
	
	char ch[4096];	
	sprintf(ch, "%s%d",
		SPLITE_ATTVALUE,
		nDel);
	
	sTmp += ch;	
	outputscript(sTmp.GetBuffer());	
}

void delEnt(ent id, int nDel = 1);
void delEnt(ent id, int nDel)
{
	ents arid;
	arid.add(id);

	delEnt(arid, nDel);
}

void setEnt(ent id, CString sProp, CString sValue)
{
	CString s3 = "";
	X2S(s3, id);
	
	char ch[4096];	
	sprintf(ch, "SETENT%s%s%s%s%s%s",
		SPLITE_ATTVALUE,
		s3.GetBuffer(),
		SPLITE_ATTVALUE,
		sProp.GetBuffer(),
		SPLITE_ATTVALUE,
		sValue.GetBuffer());
	
	outputscript(ch);
}

void setEnt(ents ids, CString sProp, CString sValue)
{
	CString s3 = "";
	X2S(s3, ids);

	CString sTmp = "";
	sTmp += "SETENT";
	sTmp += SPLITE_ATTVALUE;
	sTmp += s3.GetBuffer();
	
	char ch[4096];	
	sprintf(ch, "%s%s%s%s",		
		SPLITE_ATTVALUE,
		sProp.GetBuffer(),
		SPLITE_ATTVALUE,
		sValue.GetBuffer());
	
	sTmp += ch;
	outputscript(sTmp.GetBuffer());
}

CString getEnt(ent id, CString sProp)
{
	if(id < 0)
		return "";

	CString s3 = "";
	X2S(s3, id);

	CString s4 = sProp;
	
	char ch[4096];	
	sprintf(ch, "GETENT%s%s%s%s",
		SPLITE_ATTVALUE,
		s3.GetBuffer(),
		SPLITE_ATTVALUE,
		s4.GetBuffer());
	
	joutputreturn(ch);

	if(g_sJResult0 == "")
		return "";
	
	return g_sJResult0;
}

CString getEnt(ents ids, CString sProp)
{
	CString s3 = "";
	X2S(s3, ids);

	CString sTmp = "";
	sTmp += "GETENT";
	sTmp += SPLITE_ATTVALUE;
	sTmp += s3.GetBuffer();
	
	char ch[4096];	
	sprintf(ch, "%s%s",
		SPLITE_ATTVALUE,
		sProp.GetBuffer());
		
	sTmp += ch;
	joutputreturn(sTmp.GetBuffer());

	return g_sJResult0;
}

ent select(CString sInfo, point *pt = 0);
ent select(CString sInfo, point *pt)
{		
	char ch[4096];	
	sprintf(ch, "%s", sInfo.GetBuffer());
	
	JCallFunction("AcadSelectSingle", ch, "n|n|d|d|d");

	if(g_nJResult0 != 1)
		return -1;

	if(pt)
	{
		pt->x = g_dJResult0;
		pt->y = g_dJResult1;
		pt->z = g_dJResult2;
	}
	
	return g_nJResult1;
}

ents S2ID(CString s)
{	
	ents arid;
	
	array<CString> ars;
	StringSplite(s.GetBuffer(), ":", ars);
	
	if(ars.getSize() == 1)
	{
		CString sTmp = s.GetBuffer();

		CString sTmp0 = s.GetBuffer();		
		int nFind = StringFind(sTmp0.GetBuffer(), "-", 0);	
		
		if(nFind != -1)
		{
			CString sT0 = s.GetBuffer();
			CString sT1 = sT0.Left(nFind);
			ent nSt = atoi(sT1.GetBuffer());
			sT0 = sTmp;			
			sT1 = sT0.Mid(nFind + 1);
			ent nEd = atoi(sT1.GetBuffer());
			
			for(int j = nSt; j <= nEd; j++)
			{
				arid.add(j);
			}
		}
		else
		{
			ent id = atoi(sTmp.GetBuffer());
			arid.add(id);
		}
		
		return arid;
	}

	for(int i = 0; i < ars.getSize(); i++)
	{
		CString sTmp = ars[i].GetBuffer();
		//int nFind = sTmp.Find("-");

		CString sTmp0 = ars[i].GetBuffer();		
		int nFind = StringFind(sTmp0.GetBuffer(), "-", 0);	
				
		if(nFind != -1)
		{
			CString sT0 = ars[i].GetBuffer();
			CString sT1 = sT0.Left(nFind);
			ent nSt = atoi(sT1.GetBuffer());
			sT0 = sTmp;			
			sT1 = sT0.Mid(nFind + 1);
			ent nEd = atoi(sT1.GetBuffer());
			
			for(int j = nSt; j <= nEd; j++)
			{
				arid.add(j);
			}
		}
		else
		{
			ent id = atoi(sTmp.GetBuffer());
			arid.add(id);
		}
	}
	
	return arid;
}

CString handle(int id);
CString handle(int id)
{
	CString s1;
	s1 = X2S(id);

	CString sTmp = "";
	sTmp += s1.GetBuffer();
	
	g_sJResult0 = "";

	JCallFunction("AcadId2Handle", sTmp.GetBuffer(), "s");

	return g_sJResult0;
}

ent handle(CString sHandle);
ent handle(CString sHandle)
{	
	CString sTmp = "";
	sTmp += sHandle.GetBuffer();
	
	g_nJResult0 = -1;

	JCallFunction("AcadHandle2Id", sTmp.GetBuffer(), "n");
		
	return g_nJResult0;
}



ents selects(CString sInfo, CString sType = "");
ents selects(CString sInfo, CString sType)
{		
	char ch[4096];	
	if(sType == "")
		sprintf(ch, "%s%s", sInfo.GetBuffer(), SPLITE_ATTVALUE);
	else
		sprintf(ch, "%s%s%s", sInfo.GetBuffer(), SPLITE_ATTVALUE, sType.GetBuffer());
	
	JCallFunction("AcadSelectMultiple", ch, "n|s");
	
	//if(g_nJResult0 != 1)
	//	return -1;
		
	CString s = g_sJResult0;
	
	return S2ID(s);
}


ents selectrect(CString sInfo, CString sType, point ptSt, point ptEd, int nCross = 1);
ents selectrect(CString sInfo, CString sType, point ptSt, point ptEd, int nCross)
{		
	char ch[4096];	
	if(sType == "")
		sprintf(ch, "%s%s", sInfo.GetBuffer(), SPLITE_ATTVALUE);
	else
		sprintf(ch, "%s%s%s", sInfo.GetBuffer(), SPLITE_ATTVALUE, sType.GetBuffer());

	CString s1;
	ptSt.GetDataToString(s1);
	CString s2;
	ptSt.GetDataToString(s2);
	CString s2;
	s3 = X2S(nCross);

	CString sTmp = "";
	sTmp += ch;
	sTmp += SPLITE_ATTVALUE;
	sTmp += s1.GetBuffer();
	sTmp += SPLITE_ATTVALUE;
	sTmp += s2.GetBuffer();
	sTmp += SPLITE_ATTVALUE;
	sTmp += s3.GetBuffer();	
	
	JCallFunction("AcadSelect2Point", sTmp.GetBuffer(), "n|s");
	
	if(g_nJResult0 != 1)
		return -1;
	
	CString s = g_sJResult0;
	array<CString> ars;
	StringSplite(s.GetBuffer(), ":", ars);
	
	ents arid;
	for(int i = 0; i < ars.getSize(); i++)
	{
		CString sTmp = ars[i];
		ent id = atoi(sTmp.GetBuffer());
		arid.add(id);
	}
	
	return arid;
}

int selectpoint(CString sInfo, point &pt);
int selectpoint(CString sInfo, point &pt)
{		
	char ch[4096];	
	sprintf(ch, "%s", sInfo.GetBuffer());
	
	JCallFunction("AcadSelectPoint", ch, "n|d|d|d");
	
	if(g_nJResult0 != 1)
		return 0;
		
	pt.x = g_dJResult0;
	pt.y = g_dJResult1;
	pt.z = g_dJResult2;
		
	return 1;
}

void show()
{	
	char ch[4096];	
	JCallFunction("AcadFrameShow", ch, "");	
}

void hide()
{	
	char ch[4096];	
	JCallFunction("AcadFrameHide", ch, "");	
}


void EsEditKey(CString sKey)
{	
	char ch[4096];	
	sprintf(ch, "%s", sKey.GetBuffer());

	JCallFunction("EasyScriptEditKey", ch, "n");	
}

int distAtPoint(ent id, point pt, double &dDist)
{	
	CString s1;
	s1 = X2S(id);
	
	CString s2;
	pt.GetDataToString(s2);	
		
	CString sTmp = "";	
	sTmp += s1.GetBuffer();
	sTmp += SPLITE_ATTVALUE;
	sTmp += s2.GetBuffer();
		
	JCallFunction("CurveDistAtPoint", sTmp.GetBuffer(), "n|d");	

	//if(g_nJResult0 != 1)
	//	return g_nJResult0;
		
	dDist = g_dJResult0;

	return g_nJResult0;
}


int closestPoint(ent id, point pt, point &ptOnCurve)
{	
	CString s1;
	s1 = X2S(id);
	
	CString s2;
	pt.GetDataToString(s2);	
	
	CString sTmp = "";	
	sTmp += s1.GetBuffer();
	sTmp += SPLITE_ATTVALUE;
	sTmp += s2.GetBuffer();
	
	JCallFunction("CurveClosestPoint", sTmp.GetBuffer(), "n|s");	
	
	//if(g_nJResult0 != 1)
	//	return g_nJResult0;
	
	CString s = g_sJResult0;
	ptOnCurve = point(s);
	
	return g_nJResult0;
}

int endDist(ent id, double &dDist)
{	
	CString s1;
	s1 = X2S(id);
		
	CString sTmp = "";	
	sTmp += s1.GetBuffer();
		
	JCallFunction("CurveEndDist", sTmp.GetBuffer(), "n|d");	
	
	//if(g_nJResult0 != 1)
	//	return g_nJResult0;
	
	dDist = g_dJResult0;
	
	return g_nJResult0;
}

int pointAtDist(ent id, double dDist, point &pt)
{	
	CString s1;
	s1 = X2S(id);
	
	CString s2;
	s2 = X2S(dDist);
	
	CString sTmp = "";	
	sTmp += s1.GetBuffer();
	sTmp += SPLITE_ATTVALUE;
	sTmp += s2.GetBuffer();
	
	JCallFunction("CurvePointAtDist", sTmp.GetBuffer(), "n|s");	
	
	//if(g_nJResult0 != 1)
	//	return g_nJResult0;
	
	CString s = g_sJResult0;
	pt = point(s);
	
	return g_nJResult0;
}

void NewDwg(CString sPath = "");
void NewDwg(CString sPath)
{	
	char ch[4096];	
	sprintf(ch, "%s", sPath.GetBuffer());
	
	JCallFunction("AcadFileNew", ch, "n");	
}

void OpenDwg(CString sPath)
{	
	char ch[4096];	
	sprintf(ch, "%s", sPath.GetBuffer());
	
	JCallFunction("AcadFileOpen", ch, "");	
}

void SaveDwg(CString sPath = "");
void SaveDwg(CString sPath)
{	
	char ch[4096];	
	sprintf(ch, "%s", sPath.GetBuffer());
	
	JCallFunction("AcadFileSave", ch, "");	
}

void SaveAsDwg(CString sPath)
{	
	char ch[4096];	
	sprintf(ch, "%s", sPath.GetBuffer());
	
	JCallFunction("AcadFileSaveAs", ch, "");	
}

void CloseDwg(CString sPath = "");
void CloseDwg(CString sPath)
{	
	char ch[4096];	
	sprintf(ch, "%s", sPath.GetBuffer());
	
	JCallFunction("FileClose", ch, "");	
}