#include "stdafx.h"
#include "BaseLine.h"
#include "StationDataBase.h"
#include "CalJXAndGD.h"

extern vector<CBaseLine*> pJXGTZXArray;

CBaseLine::CBaseLine() {

}

CBaseLine::CBaseLine(int iNJD, double iJDarray[][7], const ACHAR* GH, const ACHAR* JDchr)
{
	
	//	ACHAR mes[STR_EXTEND];

	int i = 0, j;
	ACHAR EGH[STR_MIN];

	INIT();

	_tcscpy(JDGH, JDchr);
	for (i = 0; i < MAXJDNUM; i++)
	{
		for (j = 0; j < 7; j++)
			JDarray[i][j] = iJDarray[i][j];
		_stprintf(JDStrArray[i], L"%s%d", JDGH, (int)(JDarray[i][0] + 0.1));
	};

	NJD = iNJD;
	NDL = 1;
	setDLM(0, JDarray[0][3], 0.0, GH, GH);

	NDL = 2;
	_tcscpy(EGH, GH);
	setDLM(1, 900000, 0, GH, EGH);
	getpxy_DATA();

	NDL = 2;
	setDLM(1, QXB[NJD - 1][0], 0.0, EGH, EGH);
	//if (V_JDARRAY.size() ==0)
	//	CreateBaseLinePolygen(2000.0,2000.0);
	m_JYLjbk = 3.5;//既有路基半宽//既有股道路基半宽
	m_MinSGkuan = 2.0;//帮宽宽度//最小施工宽度
	m_AllowingWide = 0.3; // 允许欠宽（m）
}

CBaseLine::~CBaseLine() {
}

int CBaseLine::INIT()
{
	TJindex = 1;

	V_JDARRAY.clear();
	PolygonPtArrayScreen.clear();
	_tcscpy(LCGH, L"JK");//里程冠号 
	startMileage = 0;//起始里程
	int i, j;
	for (i = 0; i < MAXJDNUM; i++)
	{
		_tcscpy(JDStrArray[i], L"\0");
		for (j = 0; j < 7; j++)
			JDarray[i][j] = 0.0;
		for (j = 0; j < 9; j++)   QXB[i][j] = 0.0;
		//  for (  j=0;j<3;j++)   QXLRL[i][j]=0.0;
	};
	NJD = 0;
	for (i = 0; i < MAXDLNUM; i++)
	{
		_tcscpy(DLB[i].BianHao, L"");
		DLB[i].DL = 0.0;
		DLB[i].BLC = 0.0;
		_tcscpy(DLB[i].BGH, L"\0");
		_tcscpy(DLB[i].EGH, L"\0");
		DLB[i].TYLC = 0.0;
		DLB[i].ELC = 0.0;
	};
	for (i = 0; i < MAXDLNUM; i++)
	{
		_tcscpy(Virtual_DLB[i].BianHao, DLB[i].BianHao);
		Virtual_DLB[i].BLC = DLB[i].BLC;
		Virtual_DLB[i].DL = DLB[i].DL;
		_tcscpy(Virtual_DLB[i].BGH, DLB[i].BGH);
		_tcscpy(Virtual_DLB[i].EGH, DLB[i].EGH);
		Virtual_DLB[i].TYLC = DLB[i].TYLC;
		Virtual_DLB[i].ELC = DLB[i].ELC;
	};

	_tcscpy(BianHao, L"");
	_tcscpy(DesignBianHao, L"");
	//	_tcscpy(SJST,L"新建");

	IsDrawPolygon = 0;
	JDTDBZ = 1;
	ExtentLen = 15000.0;//默认起终点延伸15Km
	NDL = 2;
	NH = 3;
	NLC = 2;
	texth = 1;
	draw_zybz = 1;
	Chinese_Engbz_type = 1;
	JD0 = 0;
	DLB[0].DL = 0.0; DLB[i].BLC = 0.0;
	_tcscpy(DLB[0].BGH, L"AK"); _tcscpy(DLB[0].EGH, L"AK");

	DLB[1].DL = 0.0; DLB[1].BLC = 9000000.0;
	_tcscpy(DLB[1].BGH, L"AK"); _tcscpy(DLB[1].EGH, L"AK");

	//	Zx[0]=Zx[1]=-1;
		//detN = 0.0;
		//detE = 0.0;
		//detA = 0.0;

	return 1;
}

void  CBaseLine::setINIT(int nlc, int nh, int Chinese_Engbz, double Texth)
{
	NLC = nlc;
	NH = nh;
	texth = Texth;
	Chinese_Engbz_type = Chinese_Engbz;
}

void CBaseLine::SetGDSX(Track TrackData)
{
	//	assertWriteEnabled();
	_tcscpy(DesignBianHao, TrackData.DesignBianHao);
	_tcscpy(BianHao, TrackData.BianHao);
	_tcscpy(LCGH, TrackData.LCGH);
	startMileage = TrackData.startMileage;
	StartCml = startMileage;
	TJindex = TrackData.TjIndex;
}

double CBaseLine::XLC(double TYLC, ACHAR* GH, int NDL1)//根据彭工建议修改
{
	double LCB = 0;
	double XCLC = 0.0;// LCB 断链的统一里程
	int iDL = 0;
	CString strs1, strs2;
	for (iDL = 0; iDL < NDL1; iDL++)
	{
		//该断链点统一里程
		if (iDL > 0)
			LCB = LCB + DLB[iDL].BLC - (DLB[iDL - 1].BLC + DLB[iDL - 1].DL);
		else
			LCB = DLB[iDL].BLC + DLB[iDL].DL;

		if (iDL == 0)
		{
			if (TYLC < LCB)
			{
				strs1 = DLB[0].BGH;
				//strs1.MakeUpper();
				_tcscpy(GH, strs1);
				XCLC = DLB[0].BLC - LCB + TYLC;
				break;
			}
			else if (fabs(TYLC - LCB) < 0.00005)//TYLC-LCB
			{
				//				if (fabs(DLB[0].BLC-floor(DLB[0].BLC))<0.00005)//断前是整数
				//				{
				//					strs1 = DLB[0].BGH;
				//					XCLC = DLB[0].BLC;
				//				}
				//				else
				//				{
				strs1 = DLB[0].EGH;
				XCLC = DLB[0].BLC + DLB[0].DL;
				//				}
				//strs1.MakeUpper();
				_tcscpy(GH, strs1);
				break;
			}
		}
		else if (iDL < NDL1)
		{
			if (TYLC < LCB)
			{
				XCLC = DLB[iDL].BLC - LCB + TYLC;
				strs1 = DLB[iDL - 1].EGH;
				strs2 = DLB[iDL].BGH;
				strs1.TrimLeft();
				strs1.TrimRight();
				//strs1.MakeUpper();
				strs2.TrimLeft();
				strs2.TrimRight();
				//strs2.MakeUpper();
				if (strs1 == strs2)
					_tcscpy(GH, strs1);
				else
				{
					if (DLB[iDL - 1].DL < 0.00001 && XCLC < DLB[iDL - 1].BLC)//in 前长链
						_tcscpy(GH, strs1);
					else if (DLB[iDL].DL < 0.00001 && XCLC < DLB[iDL].BLC)//in 后长链
						_tcscpy(GH, strs2);
					else//前后都不是长链
					{
						if (2.0 * (LCB - TYLC) > DLB[iDL].BLC - DLB[iDL - 1].BLC - DLB[iDL - 1].DL)
							_tcscpy(GH, strs1);
						else
							_tcscpy(GH, strs2);
					}
				}
				break;
			}
			else if (fabs(TYLC - LCB) < 0.00005)//==
			{
				if (fabs(DLB[iDL].BLC - floor(DLB[iDL].BLC)) < 0.00005)
				{
					strs1 = DLB[iDL].BGH;
					XCLC = DLB[iDL].BLC;
				}
				else
				{
					strs1 = DLB[iDL].EGH;
					XCLC = DLB[iDL].BLC + DLB[iDL].DL;
				}
				//strs1.MakeUpper();
				_tcscpy(GH, strs1);
				break;
			}
		}
	}
	if (iDL == NDL1)
	{
		XCLC = DLB[iDL - 1].BLC + DLB[iDL - 1].DL + TYLC - LCB;
		_tcscpy(GH, DLB[iDL - 1].EGH);
	}
	return XCLC;
}

void CBaseLine::TrsProjPtToNE(double Cml, double Dist, double& N, double& E)
{
	xlpoint PZ;
	PZ.lc = Cml;  // 统一里程
	int k = xlpoint_pz(virtual_array, virtual_tarray, &PZ);
	N = PZ.x + Dist * cos(PZ.a + 0.5 * PI);
	E = PZ.y + Dist * sin(PZ.a + 0.5 * PI);
}

//起终点延长 生成虚拟线元数组
BOOL  CBaseLine::CreateVirtualXYarray()
{
	if (NJD < 2)
	{
		//ads_printf(L"测点数小于两个，请检查！\n");
		return false;
	}
	double JDarrayTmp[MAXJDNUM][7];
	for (int i = 0; i < NJD; i++)
		for (int j = 0; j < 7; j++)
			JDarrayTmp[i][j] = JDarray[i][j];
	
	DVec3d vec = DVec3d::From(0, 0, 0);
	DPoint3d PT[2];
	PT[0].x = JDarray[0][1];
	PT[0].y = JDarray[0][2];
	PT[0].z = 0;
	PT[1].x = JDarray[1][1];
	PT[1].y = JDarray[1][2];
	PT[1].z = 0;
	vec = PT[0] - PT[1];
	vec.Normalize();
	PT[0] += vec * ExtentLen;
	JDarrayTmp[0][1] = PT[0].x;
	JDarrayTmp[0][2] = PT[0].y;

	//JDarrayTmp[0][1] = JDarray[0][1] + ExtentLen*cos(rad(array[0][3])+PI);
	//JDarrayTmp[0][2] = JDarray[0][2] + ExtentLen*sin(rad(array[0][3])+PI);	
	JDarrayTmp[0][3] = JDarray[0][3] - ExtentLen;

	PT[0].x = JDarray[NJD - 1][1];
	PT[0].y = JDarray[NJD - 1][2];
	PT[0].z = 0;
	PT[1].x = JDarray[NJD - 2][1];
	PT[1].y = JDarray[NJD - 2][2];
	PT[1].z = 0;
	vec = PT[0] - PT[1];
	vec.Normalize();
	PT[0] += vec * ExtentLen;
	JDarrayTmp[NJD - 1][1] = PT[0].x;
	JDarrayTmp[NJD - 1][2] = PT[0].y;

	//JDarrayTmp[NJD-1][1] = JDarray[NJD-1][1] + ExtentLen*cos(array[tarray-1][5]);
	//JDarrayTmp[NJD-1][2] = JDarray[NJD-1][2] + ExtentLen*sin(array[tarray-1][5]);

	double Out_QXB[MAXJDNUM][9];
	CalXYArray(JDarrayTmp, NJD, virtual_array, virtual_tarray, Out_QXB, Virtual_XLlen);
	return true;
}

BOOL CBaseLine::getpxy_DATA()
{
	//ADS
	// 导线法由导线数据文件求线位 L.SC.S.L.S.C.S.L...  得到线元文件
	double fb, fe, db, de, aa, c, lcb, lce, T0 = 0.0, interal = 18;
	ACHAR zy[STR_MIN];
	ACHAR  GH[STR_SHORT];
	int i = 0, j = 0, k, color = 2, iq = 2;
	double BPMTL[10], T1, T2, L;
	double LCB, DDlength;
	int iDL = 0;

	for (i = 0; i < 10; i++)
		BPMTL[i] = 0.0;
	//for (i = 0; i < MAXJDNUM; i++)
	//{
	//	for (k = 0; k < 4; k++)
	//	{
	//		for (j = 0; j < 5; j++)
	//			array_pxy[i][k][j] = 0.0;
	//	}
	//}
	//检验曲线半径和缓长是否超过限制
	for (int ii = 1; ii < NJD - 1; ii++)
	{
		if (JDarray[ii][3] > 1000.0)
			JDarray[ii][3] = 1000.0;
		if (JDarray[ii][3] < 0.0)
			JDarray[ii][3] = 0.0;

		if (JDarray[ii][5] > 1000.0)
			JDarray[ii][5] = 1000.0;
		if (JDarray[ii][5] < 0.0)
			JDarray[ii][5] = 0.0;

		if (JDarray[ii][4] > 15000.0)
			JDarray[ii][4] = 15000.0;
	}
	array_pxy.clear();
	array_pxy.resize(20 * NJD);

	for (i = 0; i < (int)array_pxy.size(); i++)
		array_pxy[i] = 0.0;

	//起终点半径应为0    
	JDarray[0][4] = JDarray[NJD - 1][4] = JDarray[0][5] = JDarray[NJD - 1][5] = 0.0;

	db = xyddaa(JDarray[0][1], JDarray[0][2], JDarray[1][1], JDarray[1][2], &fb);
	//   JDarray[0][6]=fb;  
	lcb = JDarray[0][3];
	lce = lcb;
	fb = dms_rad(fb);
	//array_pxy[0][0][0] = 10.0;
	//// N(X)=ACAD_Y
	//array_pxy[0][0][1] = JDarray[0][1];
	////E(Y)=ACAD_X
	//array_pxy[0][0][2] = JDarray[0][2];
	//// FA
	//array_pxy[0][0][3] = fb;
	//// LC
	//array_pxy[0][0][4] = JDarray[0][3];

	array_pxy[0] = 10.0;// 起点属性
	array_pxy[1] = JDarray[0][1]; //坐标
	array_pxy[2] = JDarray[0][2]; //坐标
	array_pxy[3] = fb;  //方位角 FA
	array_pxy[4] = JDarray[0][3]; //里程 LC

	//  DLB[0].BLC=JDarray[0][3];   DLB[0].DL=0.0;  NDL=1;   // 起点 作为一特殊断链点

	QXB[0][0] = JDarray[0][3];  // 起点里程
	QXB[0][1] = QXB[0][0];
	QXB[0][2] = QXB[0][0];
	QXB[0][3] = QXB[0][0];
	QXB[0][4] = QXB[0][5] = QXB[0][6] = QXB[0][7] = QXB[0][8] = 0.0;
	T0 = 0.0;
	if (NJD == 2) // 仅有两个交点,即一条直线时
	{
		//array_pxy[1][0][0] = 1.0;
		//array_pxy[1][0][1] = db;
		array_pxy[1 * 20] = 1.0; //
		array_pxy[1 * 20 + 1] = db;
	};

	LCB = QXB[0][0]; // 起点里程
	DDlength = LCB;
	iDL = 1;
	double L01, L02;
	for (i = 1; i < NJD - 1; i++)
	{
		L01 = JDarray[i][3] < 1.0 ? 0.0 : JDarray[i][3];
		L02 = JDarray[i][5] < 1.0 ? 0.0 : JDarray[i][5];

		de = xyddaa(JDarray[i][1], JDarray[i][2], JDarray[i + 1][1], JDarray[i + 1][2], &fe);
		fe = dms_rad(fe);
		aa = rad(fe) - rad(fb);
		if (aa < -PI)
			aa = aa + 2 * PI;
		if (aa > PI)
			aa = aa - 2 * PI;
		if (aa > 0)
		{
			c = 1;
			_tcscpy(zy, L"y");
		}  //右偏
		else
		{
			c = -1;
			_tcscpy(zy, L"z");
		}//左偏
		aa = fabs(aa);
		aa = dms_rad(aa);
		//if (JDarray[i][4]<=0)
		//	break;
		getBPMTL(aa, JDarray[i][4], L01, L02, &BPMTL[0]);
		//		getBPMTL(aa,JDarray[i][4],JDarray[i][3],JDarray[i][5],&BPMTL[0]);
		T1 = BPMTL[6];
		T2 = BPMTL[7];
		L = BPMTL[8];

		//		if ( L<(JDarray[i][3]+JDarray[i][5]))
		if (L < (L01 + L02))
		{
			//return 0;
			//ads_printf(L"\n%d号曲线 前后缓和曲线重叠：缓长L1+L2=%10.3f>大于曲线长L=%10.3f ",i,JDarray[i][3]+JDarray[i][5],L);
		}
		//		else if((L-JDarray[i][3]-JDarray[i][5])<20.0)
		else if ((L - L01 - L02) < 20.0)
		{
			//return 0;
			//ads_printf(L"\n %d号曲线的(R=%8.1f)圆曲线长L=%10.3f 不足20m ",i,JDarray[i][4],L-JDarray[i][3]-JDarray[i][5]);
		}

		//曲线要数 角度为度.分秒
		QXB[i][4] = aa;
		QXB[i][5] = T1;
		QXB[i][6] = T2;
		QXB[i][7] = L;
		QXB[i][8] = c;
		//  line
		//array_pxy[i][0][0] = 1.0;
		//array_pxy[i][0][1] = db - T1 - T0;
		array_pxy[i * 20] = 1.0;
		array_pxy[i * 20 + 1] = db - T1 - T0;
		//ZH (zy)
		QXB[i][0] = QXB[i - 1][3] + (db - T1 - T0);
		if ((db - T1 - T0) < 20.0)
		{
			//	ads_printf(L"\n %d号曲线起点ZH/ZY距前一曲线HZ/ZY距离=%-10.3f ",i+JD0-1,db-T1-T0);
			if ((db - T1 - T0) < 0)
			{
				//return 0;
				//ads_printf(L"超过前一曲线HZ/YZ不合法\n ");
			}
		}

		//		aa=sqrt(JDarray[i][3]*JDarray[i][4]);
		aa = sqrt(L01 * JDarray[i][4]);
		if (aa > 0.000000001)
		{
			//array_pxy[i][1][0] = 3.0;
			//array_pxy[i][1][1] = aa;
			////array_pxy[i][1][2]=QXLRL[i][1];
			//array_pxy[i][1][2] = JDarray[i][4];
			//array_pxy[i][1][3] = 0.0;
			//array_pxy[i][1][4] = c;

			array_pxy[i * 20 + 1 * 5] = 3.0;
			array_pxy[i * 20 + 1 * 5 + 1] = aa; //A
			array_pxy[i * 20 + 1 * 5 + 2] = JDarray[i][4];  //R
			array_pxy[i * 20 + 1 * 5 + 3] = 0.0;
			array_pxy[i * 20 + 1 * 5 + 4] = c;  //z y pian
		}
		// HY
		//		QXB[i][1]=QXB[i][0]+JDarray[i][3];
		QXB[i][1] = QXB[i][0] + L01;
		//  circle 
		//		aa=L-JDarray[i][3]-JDarray[i][5];
		aa = L - L01 - L02;
		if (aa > 0.000000001)
		{
			//array_pxy[i][2][0] = 2.0;
			//array_pxy[i][2][1] = JDarray[i][4];
			//array_pxy[i][2][2] = aa;
			//array_pxy[i][2][3] = 0.0;
			//array_pxy[i][2][4] = c;

			array_pxy[i * 20 + 2 * 5] = 2.0;  //性质
			array_pxy[i * 20 + 2 * 5 + 1] = JDarray[i][4]; //R
			array_pxy[i * 20 + 2 * 5 + 2] = aa; //yuan  chang 
			array_pxy[i * 20 + 2 * 5 + 3] = 0.0;
			array_pxy[i * 20 + 2 * 5 + 4] = c;
		}
		else//半径为0 转角要传递
		{
			//array_pxy[i][2][0] = 2.0;  //性质
			//array_pxy[i][2][1] = JDarray[i][4]; //R
			//array_pxy[i][2][2] = aa; //yuan  chang 
			//array_pxy[i][2][3] = rad(fe);
			//array_pxy[i][2][4] = c;

			array_pxy[i * 20 + 2 * 5] = 2.0;  //性质
			array_pxy[i * 20 + 2 * 5 + 1] = JDarray[i][4]; //R
			array_pxy[i * 20 + 2 * 5 + 2] = aa; //yuan  chang 
			array_pxy[i * 20 + 2 * 5 + 3] = rad(fe);
			array_pxy[i * 20 + 2 * 5 + 4] = c;
		}
		// YH
		QXB[i][2] = QXB[i][1] + aa;
		//2-spiral
		//		aa=sqrt(JDarray[i][5]*JDarray[i][4]);
		aa = sqrt(L02 * JDarray[i][4]);
		if (aa > 0.000000001)
		{
			//array_pxy[i][3][0] = 4.0;
			//array_pxy[i][3][1] = aa;
			//array_pxy[i][3][2] = JDarray[i][4];
			//array_pxy[i][3][3] = 0.0;
			//array_pxy[i][3][4] = c;

			array_pxy[i * 20 + 3 * 5 + 0] = 4.0; //A
			array_pxy[i * 20 + 3 * 5 + 1] = aa; //A
			array_pxy[i * 20 + 3 * 5 + 2] = JDarray[i][4];  //R
			array_pxy[i * 20 + 3 * 5 + 3] = 0.0;
			array_pxy[i * 20 + 3 * 5 + 4] = c;  //z y pian
		}
		// HZ
		//		QXB[i][3]=QXB[i][2]+JDarray[i][5];
		QXB[i][3] = QXB[i][2] + L02;
		db = de;
		lcb = lce;
		fb = fe;
		T0 = T2;
	}
	//  line
	db = db - T0;
	if (db > 0.0000000001)
	{
		//array_pxy[NJD-1][0][0] = 1.0;
		//array_pxy[NJD-1][0][1] = db;
		array_pxy[(NJD - 1) * 20] = 1.0;
		array_pxy[(NJD - 1) * 20 + 1] = db;
	}

	// 终点里程
	QXB[NJD - 1][0] = QXB[NJD - 2][3] + db;
	xl_length = QXB[NJD - 1][0] - QXB[0][0];
	QXB[NJD - 1][1] = QXB[NJD - 1][0];
	QXB[NJD - 1][2] = QXB[NJD - 1][0];
	QXB[NJD - 1][3] = QXB[NJD - 1][0];
	QXB[NJD - 1][4] = QXB[NJD - 1][5] = QXB[NJD - 1][6] = QXB[NJD - 1][7] = QXB[NJD - 1][8] = 0.0;
	// 修改终点特殊断链点里程 (终点 作为一特殊断链点)
	ACHAR Sgh[STR_MIN];
	ACHAR  Egh[STR_MIN];
	_tcscpy(Sgh, DLB[NDL - 1].BGH);
	_tcscpy(Egh, DLB[NDL - 1].EGH);//修改终点断链冠号  2004.7.16
	lcb = XLC(QXB[NJD - 1][0], DLB[NDL - 1].BGH, NDL - 1);//2005-12-16
	if (_tcscmp(DLB[NDL - 1].BGH, DLB[NDL - 1].EGH) != 0 || fabs(DLB[NDL - 1].DL) > 0.00001)
		lce = DLB[NDL - 1].BLC + DLB[NDL - 1].DL;//2005-12-16
	else
		lce = lcb;
	setDLM(NDL - 1, lcb, lce - lcb, Sgh, Egh);
	//  起点
	double INDATA[5], l, L0;
	struct xlpoint PB0, PEZ;
	for (j = 0; j < 5; j++)
		//array[0][j] = array_pxy[0][0][j];
		array[0][j] = array_pxy[j];
	tarray = 1;
	///////除去起终交点 每个交点四个线元  ( 在只有圆曲线 没有 缓和曲线时  只有两个线元)
	for (i = 1; i < NJD - 1; i++)
	{
		for (k = 0; k < 4; k++)
		{
			for (j = 0; j < 5; j++)
				//INDATA[j] = array_pxy[i][k][j];
				INDATA[j] = array_pxy[i * 20 + k * 5 + j];
			j = (int)(INDATA[0] + 0.01);
			l = 0.0;
			if (j == 1)
				l = INDATA[1];
			else if (j == 2)
				l = INDATA[2];
			else if (j == 3 || j == 4)
			{
				if (fabs(INDATA[2]) > 0.000001)
					l = INDATA[1] * INDATA[1] / INDATA[2];
				else
					l = 0.0;
			}
			else if (j == 5)
			{
				if (fabs(INDATA[2] * INDATA[3]) > 0.000001)
					l = INDATA[1] * INDATA[1] * (INDATA[2] - INDATA[3]) / (INDATA[2] * INDATA[3]);
				else
					l = 0.0;
			}
			else if (j == 6)
			{
				if (fabs(INDATA[2] * INDATA[3]) > 0.000001)
					l = INDATA[1] * INDATA[1] * (INDATA[3] - INDATA[2]) / (INDATA[2] * INDATA[3]);
				else
					l = 0.0;
			}
			// 求L0=？
			L0 = l;
			if (j == 2)
				L0 = INDATA[1];
			//		   ads_printf(L"l=%lf\n",l);
			if (L0 > 0.001)
			{
				for (int kk = 0; kk < 5; kk++)
					//array[tarray][kk] = array_pxy[i][k][kk];
					array[tarray][kk] = array_pxy[i * 20 + k * 5 + kk];
				if (j == 1)//直线
				{
					db = xyddaa(JDarray[i - 1][1], JDarray[i - 1][2], JDarray[i][1], JDarray[i][2], &fb);
					array[tarray][5] = fb;
					array[tarray][6] = QXB[i - 1][3];
					array[tarray][7] = 1e40;
					array[tarray][8] = JDarray[i - 1][1] + QXB[i - 1][6] * cos(fb);
					array[tarray][9] = JDarray[i - 1][2] + QXB[i - 1][6] * sin(fb);
				}
				// ads_printf(L"\n %1.0f %10.3f %10.3f %1.0f",array[trow][0],array[trow][1],array[trow][2],array[trow][4]);
				tarray = tarray + 1;
			};
		};
	};
	//  终点
	for (j = 0; j < 5; j++)
		//array[tarray][j] = array_pxy[NJD-1][0][j];
		array[tarray][j] = array_pxy[(NJD - 1) * 20 + j];
	if (NJD > 2)
	{
		i = NJD - 1;
		db = xyddaa(JDarray[i - 1][1], JDarray[i - 1][2], JDarray[i][1], JDarray[i][2], &fb);
		array[tarray][5] = fb;
		array[tarray][6] = QXB[i - 1][3];
		array[tarray][7] = 1e40;
		array[tarray][8] = JDarray[i - 1][1] + QXB[i - 1][6] * cos(fb);
		array[tarray][9] = JDarray[i - 1][2] + QXB[i - 1][6] * sin(fb);
	}

	tarray = tarray + 1;
	i = 0;
	PB0.lc = array[0][4];
	PB0.x = array[0][1];
	PB0.y = array[0][2];
	PB0.a = rad(array[0][3]);
	PB0.r = 1e40;
	array[1][5] = PB0.a;
	array[1][6] = PB0.lc;
	array[1][7] = PB0.r;
	array[1][8] = PB0.x;
	array[1][9] = PB0.y;
	for (i = 1; i < tarray; i++)
	{
		// 第 i 线元
		for (k = 0; k < 5; k++)
			INDATA[k] = array[i][k];

		j = (int)(array[i + 1][0] + 0.0001);
		if (j == 1)//下一单元是直线 
		{
			PB0.a = array[i + 1][5];
			PB0.lc = array[i + 1][6];
			PB0.r = array[i + 1][7];
			PB0.x = array[i + 1][8];
			PB0.y = array[i + 1][9];
		}
		else
		{
			l = Get_INDATA_L(INDATA, &j);
			xlzbxy(&PB0, &PEZ, l, INDATA, 0);
			array[i + 1][5] = PEZ.a;
			//if( fabs(array_pxy[i][2][0]-2)<0.00001 && l<0.00000001 ) array[i+1][5] = INDATA[3];
			array[i + 1][6] = PEZ.lc;
			array[i + 1][7] = PEZ.r;//线元起点的半径,直圆\圆直为无穷大
			array[i + 1][8] = PEZ.x;
			array[i + 1][9] = PEZ.y;
			PB0 = PEZ;
		}
	}
	xlpoint PZ1;
	PZ1.lc = QXB[NJD - 1][0];
	xlpoint_pz(array, tarray, &PZ1);

	/////////////////////////////线路终点线元属性 为10时  有可能投影函数投影不到
	array[tarray][0] = 10;
	array[tarray][1] = PZ1.x;
	array[tarray][2] = PZ1.y;
	array[tarray][3] = PZ1.a;
	array[tarray][4] = PZ1.lc;

	startMileage = QXB[0][0];
	StartCml = QXB[0][0];
	EndCml = QXB[NJD - 1][0];
	double xlc = XLC(StartCml, GH, NDL);
	_tcscpy(StartCKml, L"\0");
	//_tcscpy(StartCKml,LCchr(GH,xlc,NLC,1));
	LCchr(GH, xlc, NLC, StartCKml, 1);

	xlc = XLC(EndCml, GH, NDL);
	_tcscpy(EndCKml, L"\0");
	//_tcscpy(EndCKml,LCchr(GH,xlc,NLC,1));
	LCchr(GH, xlc, NLC, EndCKml, 1);
	//	acutPrintf("\n命令:");//plh07.19a
	CreateVirtualXYarray();//起终点延伸后的虚拟线元
	CreateVirtualDLB();//起终点延伸后的虚拟断链
	//计算控制多边形屏幕点
	DPoint3d Pt2;

	if (V_JDARRAY.size() > 0)//绘制控制多边形
	{
		if (V_JDARRAY[0].Distance(V_JDARRAY[(int)V_JDARRAY.size() - 1]) < 0.1)
			V_JDARRAY.pop_back();

		PolygonPtArrayScreen.clear();
		for (i = 0; i < (int)V_JDARRAY.size(); i++)
		{
			TrsProjPtToNE(V_JDARRAY[i].x, V_JDARRAY[i].y, Pt2.y, Pt2.x);
			Pt2.z = 0.0;
			PolygonPtArrayScreen.push_back(Pt2);
		}
	}
	/////////////////////////////临时用输出线元数组
	return 1;
}


bool CBaseLine::TrsCkmltoCml(ACHAR Ckml[], double& Cml)
{
	Cml = TYLC1(Ckml, NDL, Virtual_DLB);
	return true;
}

void CBaseLine::TrsCkmlPtToNE(ACHAR Ckml[STR_SHORT], double Dist, double& N, double& E)
{
	xlpoint PZ;
	TrsCkmltoCml(Ckml, PZ.lc);
	int k = xlpoint_pz(virtual_array, virtual_tarray, &PZ);
	N = PZ.x + Dist * cos(PZ.a + 0.5 * PI);
	E = PZ.y + Dist * sin(PZ.a + 0.5 * PI);
}

void  CBaseLine::CreateVirtualDLB()
{
	for (int i = 0; i < NDL; i++)
	{
		_tcscpy(Virtual_DLB[i].BianHao, DLB[i].BianHao);
		Virtual_DLB[i].BLC = DLB[i].BLC;
		Virtual_DLB[i].DL = DLB[i].DL;
		_tcscpy(Virtual_DLB[i].BGH, DLB[i].BGH);
		_tcscpy(Virtual_DLB[i].EGH, DLB[i].EGH);
		Virtual_DLB[i].TYLC = DLB[i].TYLC;
		Virtual_DLB[i].ELC = DLB[i].ELC;
	}
	Virtual_DLB[0].BLC = DLB[0].BLC - ExtentLen;
	Virtual_DLB[NDL - 1].BLC = DLB[NDL - 1].BLC + ExtentLen;
}

//  设置断链数据
BOOL  CBaseLine::setDLM(int iDL, double BLC, double DL, const ACHAR* BGH, const ACHAR* EGH, int mode)
{
	/*if (mode != 1)
		assertWriteEnabled();*/
	if (iDL >= 0 && iDL < NDL)
	{
		_tcscpy(DLB[iDL].BGH, BGH);
		_tcscpy(DLB[iDL].EGH, EGH);
		DLB[iDL].BLC = BLC;
		DLB[iDL].DL = DL;
		//起点
		if (iDL == 0)
			JDarray[0][3] = BLC + DL;
		CreateVirtualDLB();
		return 1;
	}
	else
		return 0;
}