From aac3b319965967f465881f3a8961904d57b6f337 Mon Sep 17 00:00:00 2001
From: zxd <zxdvslxy@gmail.com>
Date: 星期四, 28 九月 2023 17:01:41 +0800
Subject: [PATCH] 节前提交,包含部分图码的转换,提出单独函数专做转换

---
 MTerm1/MTerm1View.cpp | 2169 ++++++++++++++++++++++++++++++++++++++--------------------
 1 files changed, 1,421 insertions(+), 748 deletions(-)

diff --git a/MTerm1/MTerm1View.cpp b/MTerm1/MTerm1View.cpp
index a34e24c..4abbea4 100644
--- a/MTerm1/MTerm1View.cpp
+++ b/MTerm1/MTerm1View.cpp
@@ -28,6 +28,10 @@
 
 IMPLEMENT_DYNCREATE(CMTerm1View, CScrollView)
 
+/// <summary>
+/// 娑堟伅鏄犲皠
+/// 鏄� MFC 涓敤浜庡皢 Windows 娑堟伅锛堝榧犳爣鐐瑰嚮銆佹寜閿瓑锛夋槧灏勫埌鎴愬憳鍑芥暟鐨勬満鍒躲��
+/// </summary>
 BEGIN_MESSAGE_MAP(CMTerm1View, CScrollView)
 	// 鏍囧噯鎵撳嵃鍛戒护
 	ON_COMMAND(ID_FILE_PRINT, &CScrollView::OnFilePrint)
@@ -102,19 +106,31 @@
 	return CScrollView::PreCreateWindow(cs);
 }
 
+/// <summary>
+///  MFC 涓殑涓�涓嚱鏁帮紝瀹冨湪瑙嗗浘棣栨鏄剧ず涔嬪墠琚皟鐢�
+/// </summary>
 void CMTerm1View::OnInitialUpdate()
 {
 	CScrollView::OnInitialUpdate();
+
+	//鍒涘缓浜嗕笁绉嶄笉鍚岀殑瀛椾綋
 	TextFont.CreateFont(13, 0, 0, 0, FW_NORMAL, FALSE, FALSE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_ROMAN, _T("瀹嬩綋"));
 	MonTextFont.CreateFont(12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_ROMAN, _T("瀹嬩綋"));
 	AnnoFont.CreateFont(12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_ROMAN, _T("瀹嬩綋"));
+	
+	//鑾峰彇浜嗙姸鎬佹爮鐨勫紩鐢ㄥ苟瀛樺偍鍦� m_pStatusBar 鎴愬憳鍙橀噺涓�
 	m_pStatusBar = ((CChildFrame *)GetParentFrame())->GetStatusBar();
+
 	CSize sizeTotal;
 	// TODO: 璁$畻姝よ鍥剧殑鍚堣澶у皬
 	sizeTotal.cx = m_LeftMargin + m_CellWidth* m_CellPerLine + m_CellWidth *2;
 	sizeTotal.cy = 2000;
+	//璁剧疆浜嗘粴鍔ㄥぇ灏�
 	SetScrollSizes(MM_TEXT, sizeTotal);
-	CString s1;
+
+	CString s1;//娌″暐鐢�
+
+	//璁剧疆浜嗕袱涓畾鏃跺櫒锛屼竴涓棿闅斾负50姣锛屽彟涓�涓负10姣銆�
 	SetTimer(1, 50,NULL);
 	SetTimer(2, 10, NULL);
 
@@ -130,25 +146,27 @@
 		return;
 
 	// TODO: 鍦ㄦ澶勪负鏈満鏁版嵁娣诲姞缁樺埗浠g爜
-	CString s1;
+	CString s1;//娌″暐鐢�
+
 	needReDraw = 1;
 	DrawLDSGraph(pDC);
 	needReDraw = 0;
 }
 
-// CMTerm1View 鎵撳嵃
-
+// CMTerm1View 鎵撳嵃锛孧FC 鍑芥暟锛岀敤浜庡噯澶囨墦鍗般��
 BOOL CMTerm1View::OnPreparePrinting(CPrintInfo* pInfo)
 {
 	// 榛樿鍑嗗
 	return DoPreparePrinting(pInfo);
 }
 
+// MFC 鍑芥暟锛岀敤浜庡湪鎵撳嵃寮�濮嬩箣鍓嶈繘琛屽垵濮嬪寲銆�
 void CMTerm1View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
 {
 	// TODO: 娣诲姞棰濆鐨勬墦鍗板墠杩涜鐨勫垵濮嬪寲杩囩▼
 }
 
+//MFC 鍑芥暟锛岀敤浜庡湪鎵撳嵃缁撴潫鍚庤繘琛屾竻鐞嗐��
 void CMTerm1View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
 {
 	// TODO: 娣诲姞鎵撳嵃鍚庤繘琛岀殑娓呯悊杩囩▼
@@ -158,16 +176,19 @@
 // CMTerm1View 璇婃柇
 
 #ifdef _DEBUG
+//鍑芥暟鐢ㄤ簬楠岃瘉瀵硅薄鐨勬湁鏁堟�с��
 void CMTerm1View::AssertValid() const
 {
 	CScrollView::AssertValid();
-}
+ }
 
+// 鍑芥暟鐢ㄤ簬灏嗗璞$殑鍐呭杞偍鍒版寚瀹氱殑涓婁笅鏂囥��
 void CMTerm1View::Dump(CDumpContext& dc) const
 {
 	CScrollView::Dump(dc);
 }
 
+//鍑芥暟杩斿洖涓庢瑙嗗浘鍏宠仈鐨勬枃妗e璞°��
 CMTerm1Doc* CMTerm1View::GetDocument() const // 闈炶皟璇曠増鏈槸鍐呰仈鐨�
 {
 	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMTerm1Doc)));
@@ -175,6 +196,7 @@
 }
 #endif //_DEBUG
 
+//MFC 鍑芥暟锛岀敤浜庡鐞嗚鍥剧殑鍒涘缓浜嬩欢銆�
 int CMTerm1View::OnCreate(LPCREATESTRUCT lpCreateStruct)
 {
 	if (CScrollView::OnCreate(lpCreateStruct) == -1)
@@ -185,9 +207,13 @@
 	return 0;
 }
 
+/// <summary>
+/// 鐢ㄤ簬閲嶆柊缁樺埗瑙嗗浘銆�
+/// </summary>
+/// <returns></returns>
 int CMTerm1View::DoReDraw()
 {
-	// TODO: 鍦ㄦ澶勬坊鍔犲疄鐜颁唬鐮�.
+
 	CPoint scroll1;
 	scroll1 = this->GetScrollPosition();
 
@@ -210,6 +236,13 @@
 	return 0;
 }
 
+/// <summary>
+/// 婊氬姩鍒扮壒瀹氬崟鍏冩牸
+/// 鍑芥暟鍐呴儴璁$畻浜嗗崟鍏冩牸鐨勬�婚珮搴︼紝鑰冭檻浜嗘槸鍚︽樉绀烘敞閲�
+/// </summary>
+/// <param name="nRow"></param>
+/// <param name="nCol"></param>
+/// <returns></returns>
 int CMTerm1View::ScrollToCell(int nRow, int nCol)
 {
 	int CellTotalHeight = m_CellHeight + (m_bShowComments ? m_CommentHeight : 0);
@@ -227,36 +260,57 @@
 	return 0;
 }
 
+/// <summary>
+/// 婊氬姩鍗曞厓鏍艰繘鍏ヨ鍥撅紙鏈畬鎴愮殑鍔熻兘锛�
+/// </summary>
+/// <param name="nRow"></param>
+/// <param name="nCol"></param>
+/// <returns></returns>
 int CMTerm1View::ScrollCellIntoView(int nRow, int nCol)
 {
 	int CellTotalHeight = m_CellHeight + (m_bShowComments ? m_CommentHeight : 0);
 	return 0;
 }
 
+/// <summary>
+/// 妫�鏌ュ崟鍏冩牸鏄惁鍦ㄨ鍥句腑锛堟湭瀹屾垚鐨勫姛鑳斤級
+/// </summary>
+/// <param name="nRow"></param>
+/// <param name="nCol"></param>
+/// <returns></returns>
 int CMTerm1View::isCellInView(int nRow, int nCol)
 {
 	int CellTotalHeight = m_CellHeight + (m_bShowComments ? m_CommentHeight : 0);
 	return 0;
 }
 
+/// <summary>
+/// 鐢ㄤ簬鍦ㄨ鍥句腑缁樺埗鍥惧舰鐨勪富瑕佸嚱鏁�
+/// </summary>
+/// <param name="pDC">鏂囨。</param>
+/// <returns></returns>
 int CMTerm1View::DrawLDSGraph(CDC* pDC)
 {
 	// TODO: 鍦ㄦ澶勬坊鍔犲疄鐜颁唬鐮�.
-	CString s1;
+	CString s1;//娌″暐鐢�
+
+	//楠岃瘉鏂囨。寮曠敤
 	CMTerm1Doc* pDoc = GetDocument();
 	ASSERT_VALID(pDoc);
 	if (!pDoc)	return false;
+
 	if (m_bMonitoring) {
 		pDoc->DoPLCMonitor();
 	}
+
 	int x1, y1, x2, y2;
 	//	RECT rect1;
 	int CellTotalHeight = m_CellHeight + (m_bShowComments ? m_CommentHeight : 0);
 
 	int nDataType, nDataAddr;
 	int nStat;
-	CBrush br0(BkColor);
-	CBrush br01(BkEditColor);
+	CBrush br0(BkColor);//鑳屾櫙棰滆壊
+	CBrush br01(BkEditColor);//缂栬緫锛堥�変腑锛夌姸鎬佷笅鐨勮儗鏅鑹�
 	int nRow, nCol;
 	CRect rect0;
 	this->GetClientRect(&rect0);
@@ -264,19 +318,26 @@
 	CPoint scroll1;
 	scroll1 = this->GetScrollPosition();
 
-	int nFirstVisibleLine;
-	int nLastVisibleLine;
-
+	int nFirstVisibleLine;//鐢ㄤ簬纭畾鍙琛岃寖鍥�-璧峰浣嶇疆
+	int nLastVisibleLine;//鐢ㄤ簬纭畾鍙琛岃寖鍥�-缁堟浣嶇疆
+	//閫氳繃婊氬姩浣嶇疆鍜岃鍥剧殑楂樺害璁$畻涓婅堪涓や釜鍊�
 	nFirstVisibleLine = (scroll1.y - m_TopMargin) / CellTotalHeight;
 	nLastVisibleLine = (rect0.Height() + scroll1.y - m_TopMargin) / CellTotalHeight + 1;
-	if (nFirstVisibleLine < 0) { nFirstVisibleLine = 0; }
-	if (nLastVisibleLine < 0) { nLastVisibleLine = 0; }
+	if (nFirstVisibleLine < 0) 
+	{
+		nFirstVisibleLine = 0; 
+	}
+	if (nLastVisibleLine < 0)
+	{ 
+		nLastVisibleLine = 0; 
+	}
 
+	//閫夋嫨瀛椾綋
 	CFont *pOldFont;
 	pOldFont = (CFont *)pDC->SelectObject(&TextFont);
-	
 
-	for (int i = nFirstVisibleLine; i < nLastVisibleLine && i < 1000; i++) {
+	for (int i = nFirstVisibleLine; i < nLastVisibleLine && i < 1000; i++) 
+	{
 		// 鐢讳竴琛� 鍗曞厓
 		nRow = i;
 		int x0, y0;
@@ -401,7 +462,6 @@
 	return 0;
 }
 
-
 void  CMTerm1View::DrawFocusRect(CDC* pDC)
 {
 //	DrawCell(pDC, m_oldFocusRow, m_oldFocusCol);
@@ -430,7 +490,6 @@
 //	pDC->SelectObject(poldBrush);
 }
 
-
 int CMTerm1View::DrawLeftRightLine(CDC* pDC, int x0, int y0, int size1, int size2)
 {
 	// TODO: 鍦ㄦ澶勬坊鍔犲疄鐜颁唬鐮�.
@@ -452,7 +511,6 @@
 	pDC->LineTo(x2, y2);
 	return 0;
 }
-
 
 void CMTerm1View::DrawOT(CDC* pDC, int x0, int y0)
 {
@@ -535,6 +593,7 @@
 	pDC->LineTo(x2, y2);
 	return 0;
 }
+
 int CMTerm1View::DrawAngleBracket(CDC* pDC, int x0, int y0, int size1, int size2)
 {
 	// TODO: 鍦ㄦ澶勬坊鍔犲疄鐜颁唬鐮�.
@@ -542,7 +601,7 @@
 	DrawLeftRightLine(pDC, x0, y0, size1, size2);
 	// 宸﹀皷鎷彿
 	x1 = x0 + size1 + 6;
-	y1 = y0 + m_LinePosY -6;
+	y1 = y0 + m_LinePosY -6; 
 	x2 = x1 -6;
 	y2 = y1 +6 ;
 	pDC->MoveTo(x1, y1);
@@ -664,7 +723,7 @@
 	int CellTotalHeight = m_CellHeight + (m_bShowComments ? m_CommentHeight : 0);
 
 	rect1.left = m_LeftMargin + 1 + m_CellWidth * nCol;
-	rect1.right = rect1.left + m_CellWidth;
+	rect1.right = rect1.left + m_CellWidth; 
 	rect1.top = m_TopMargin + CellTotalHeight * nRow;
 	rect1.bottom = rect1.top + CellTotalHeight;
 
@@ -678,6 +737,9 @@
 		if (needReDraw) {
 			pDC->MoveTo(x0, y0);
 			pDC->LineTo(x0, y0 + m_LinePosY);
+			//鐢荤洿绔栫嚎
+			//pDC->MoveTo(x0, y0 + (0.32 * CellTotalHeight));
+			//pDC->LineTo(x0, y0 - (0.69 * CellTotalHeight));
 		}
 	}
 	if (Cells[nRow][nCol].bLeftLineDn && nCol != 0)
@@ -702,9 +764,9 @@
 	}
 	else if (nType == typeLine2)
 	{	//Draw Line
-		//鐢荤洿妯嚎
-//		pDC->MoveTo(x0, y0);
-//		pDC->LineTo(x0, y0 + CellTotalHeight);
+		//鐢荤洿绔栫嚎
+		pDC->MoveTo(x0, y0 + (0.32 * CellTotalHeight));
+		pDC->LineTo(x0, y0 - (0.69 * CellTotalHeight));
 	}
 	else if (nType == typeNO)
 	{
@@ -1138,6 +1200,13 @@
 
 	CScrollView::OnKeyDown(nChar, nRepCnt, nFlags);
 }
+
+/// <summary>
+/// 鍗曞厓鏍肩劍鐐瑰彉鍖�*閫変腑鐐瑰彉鍖�
+/// </summary>
+/// <param name="nRow"></param>
+/// <param name="nCol"></param>
+/// <returns></returns>
 int CMTerm1View::CellFocusChg(int nRow, int nCol)
 {
 	CString s1;
@@ -1149,13 +1218,11 @@
 
 	auto p1 = (CMainFrame*)AfxGetMainWnd();
 	auto p2 = p1->GetInputWnd();
-	p2->SetCurCellPos(nRow,nCol,thecell);
-
-
-
+    	p2->SetCurCellPos(nRow,nCol,thecell);
+		p2->focusCell = thecell;
 	return 0;
 }
-
+//榧犳爣宸﹂敭鐐瑰嚮浜嬩欢
 void CMTerm1View::OnLButtonDown(UINT nFlags, CPoint point)
 {
 	// TODO: 鍦ㄦ娣诲姞娑堟伅澶勭悊绋嬪簭浠g爜鍜�/鎴栬皟鐢ㄩ粯璁ゅ��
@@ -1169,10 +1236,21 @@
 	int CellTotalHeight = m_CellHeight + (m_bShowComments ? m_CommentHeight : 0);
 	nRow = (ty - m_TopMargin) / CellTotalHeight;
 	nCol = (tx - m_LeftMargin) / m_CellWidth;
-	if (nRow < 0) { nRow = 0; }
-	if (nCol < 0) { nCol = 0; }
-	if (nCol >= m_CellPerLine) { nCol = m_CellPerLine - 1; }
-	s1.Format(_T("LD %d %d  %02X  Scroll %d %d  Total %d %d Row %d Col %d"), point.x, point.y, nFlags, scroll1.x, scroll1.y, tx, ty, nRow, nCol);
+	if (nRow < 0) 
+	{
+		nRow = 0; 
+	}
+	if (nCol < 0) 
+	{ 
+		nCol = 0; 
+	}
+	if (nCol >= m_CellPerLine) 
+	{
+		nCol = m_CellPerLine - 1; 
+	}
+	s1.Format(_T("OnLButtonDown::zxd:: LD %d %d  %02X  Scroll %d %d  Total %d %d Row %d Col %d"), point.x, point.y, nFlags, scroll1.x, scroll1.y, tx, ty, nRow, nCol);
+	DbgLog(s1);//zxd add to test
+
 	m_pStatusBar->SetPaneText(6, s1);
 	m_FocusRow = nRow;
 	m_FocusCol = nCol;
@@ -1401,16 +1479,27 @@
 	pCmdUI->SetCheck(m_bMonitoring == true);
 }
 
+/// <summary>
+/// 绋嬪簭杞崲鎸夐挳鐐瑰嚮
+/// </summary>
 void CMTerm1View::OnProgConvert()
 {
-	// TODO: 鍦ㄦ娣诲姞鍛戒护澶勭悊绋嬪簭浠g爜
+	//杞崲鍓嶅厛瀵筁DS鐨勮鍒欒繘琛屾楠岋紙妫�楠岃鍒欎笌杩斿洖鍙傛暟骞舵湭瀹屽叏纭畾
+ 	std::pair<int,CString> result = LDSCheckRule();
+	if (result.first == 111)
+	{
+		DbgLog(result.second);
+		return;
+	}
+
 	CString s1;
 	s1.Format(_T("Prog Convert"));
 	SysLog(s1);
 	int r = TransLDSToProg();
 	s1.Format(_T("LDS To Prog result %d"), r);
 	SysLog(s1);
-	if (r==0) {
+	if (r==0) 
+	{
 	//	m_bModified = 0;
 		int j=TransProgToLDS();
 		s1.Format(_T("Porg to LDS retuls %d"), j);
@@ -1496,24 +1585,61 @@
 	return -1;
 }
 
-void CMTerm1View::SetCellToView(stCell cell1)               //**************************************************************************************************//
+/// <summary>
+/// 璁剧疆Cell鍒扮獥鍙h鍥�
+/// </summary>
+/// <param name="cell1"></param>
+void CMTerm1View::SetCellToView(stCell cell1, int flag)               //**************************************************************************************************//
 {
-
 	Cells[m_FocusRow][m_FocusCol] = cell1;
 	m_bModified = 1;
 	needReDraw = 1;
-	m_FocusCol += 1;
+	
 	Cells[m_FocusRow][0].bEditing = 1;
 
-	if (m_nTotalRow < m_FocusRow + 1) {
+	if (m_nTotalRow < m_FocusRow + 1) 
+	{
 		m_nTotalRow = m_FocusRow + 1;
 	}
+	//鍗曞厓鏍煎叧鑱斾慨鏀�
+	switch (flag)
+	{
+	case 1:
+	{
+		if(Cells[m_FocusRow - 1][m_FocusCol].nType == CMTerm1View::typeExt1)
+		{
+			//鍙戦�侀敊璇俊鎭�
+			DbgLog(_T("鎻掑叆绾电嚎澶辫触锛氬厜鏍囦綅缃敊璇紒鍙傛暟浣嶇疆涓嶅厑璁告坊鍔犵旱绾�"));
+			return;
+		}
+		//娣诲姞绾电嚎鏃跺悓姝ユ坊鍔犱笂涓�琛�
+		Cells[m_FocusRow - 1][m_FocusCol].bLeftLineDn = 1;
+	}break;
+	case 2:
+	{
+		if (Cells[m_FocusRow - 1][m_FocusCol].nType == CMTerm1View::typeExt1)
+		{
+			//鍙戦�侀敊璇俊鎭�
+			DbgLog(_T("鍒犻櫎绾电嚎澶辫触锛氬厜鏍囦綅缃敊璇紒鍙傛暟浣嶇疆涓嶅厑璁稿垹闄ょ旱绾�"));
+			return;
+		}
+		//鍒犻櫎绾电嚎鏃跺悓姝ュ垹闄や笂涓�琛�
+		Cells[m_FocusRow - 1][m_FocusCol].bLeftLineDn = 0;
+	}break;
+	default:
+		break;
+	}
 
+	//鍗曞厓鏍间綅缃悗绉�
+	m_FocusCol += 1;
 	if (m_FocusCol >= 16) 
-	{ m_FocusCol = 0;m_FocusRow += 1; }
-	
+	{ 
+		m_FocusCol = 0;
+		m_FocusRow += 1; 
+	}
+
 	this->RedrawWindow();
-	
+	this->CellFocusChg(m_FocusRow, m_FocusCol);//鏀瑰彉鐒︾偣
 }
 void CMTerm1View::OnUpdateInsertBlankLine(CCmdUI *pCmdUI)
 {
@@ -1800,7 +1926,10 @@
 	}
 }
 
-
+/// <summary>
+/// 绋嬪簭鏁版嵁杞负姊舰鍥�
+/// </summary>
+/// <returns></returns>
 int CMTerm1View::TransProgToLDS()
 {
 	CString s1;
@@ -1810,10 +1939,12 @@
 	int stpos[100] = { 0 };
 	int nSts = 0;
 
-	for (int i = 0; i < m_nTotalRow && i < 2000; i++) {
-		for (int j = 0; j < 16; j++) {
+	for (int i = 0; i < m_nTotalRow && i < 2000; i++) 
+	{
+		for (int j = 0; j < 16; j++) 
+		{
 			Cells[i][j].clear();
-			s1.Format(_T("%d:%d"), i, j);
+ 			s1.Format(_T("%d:%d"), i, j);
 			Cells[i][j].sCoilName = s1;
 		}
 	}
@@ -1826,518 +1957,561 @@
 
 	int cx = 0, cy = 0;
 	int maxy = 0;
-	for (int i = 0; i < pDoc->m_nProgSteps; i++)
+	try
 	{
-		int nOp = pDoc->Progs[i].nOpType1;
-		int nParamCount = pDoc->Progs[i].nParamCount;
-		CStringA OpName;
-		CStringA ShowTxt;
-		pDoc->OpToTxt(nOp, OpName);
-		pDoc->OpToShowTxt(nOp, ShowTxt);
-		int nPairTo = pDoc->Progs[i].PairTo;
-		int nPairOp = 0;
-		if (nPairTo) nPairOp = pDoc->Progs[nPairTo].nOpType1;
-		int nCellType;
-		switch (nOp)
+		for (int i = 0; i < pDoc->m_nProgSteps; i++)
 		{
-
-		case OP_NOP:
-			break;
-		case OP_ST:
-		case OP_ST_:
-			if (i == 0) {
-
+			int nOp = pDoc->Progs[i].nOpType1;
+			int nParamCount = pDoc->Progs[i].nParamCount;
+			CStringA OpName;
+			CStringA ShowTxt;
+			pDoc->OpToTxt(nOp, OpName);
+			pDoc->OpToShowTxt(nOp, ShowTxt);
+			int nPairTo = pDoc->Progs[i].PairTo;
+			int nPairOp = 0;
+			if (nPairTo)
+			{
+				nPairOp = pDoc->Progs[nPairTo].nOpType1;
 			}
-			else {
-				//璁板綍褰撳墠浣嶇疆 
-				EndPt[nEndPts] = POINT{ cx,cy };
-				nEndPts++;
-				if (nPairOp == OP_ANS) {
-					//缁х画鍓嶈繘				
-					//cx = 0, cy = nCurLine;
-					//cx = StPts[nSts - 1].x;
-					//nCurLine = cy + 1; //鍙﹁捣涓�琛�
+			int nCellType;
+			switch (nOp)
+			{
+			case OP_NOP:
+				break;
+			case OP_ST:
+			case OP_ST_:
+				if (i == 0) 
+				{
+
+				}
+				else 
+				{
+					//璁板綍褰撳墠浣嶇疆 
+					EndPt[nEndPts] = POINT{ cx,cy };
+					nEndPts++;
+					if (nPairOp == OP_ANS) 
+					{
+						//缁х画鍓嶈繘				
+						//cx = 0, cy = nCurLine;
+						//cx = StPts[nSts - 1].x;
+						//nCurLine = cy + 1; //鍙﹁捣涓�琛�
 ///*				
-					nCurLine=cy;
-					int hasData = 1;
-					while (hasData) {
-						hasData = 0;
-						for (int j = cx +1; j < m_CellPerLine; j++) {
-							if (Cells[nCurLine][j].nType != 0) {
-								nCurLine++; hasData = 1; break;
+						nCurLine = cy;
+						int hasData = 1;
+						while (hasData) {
+							hasData = 0;
+							for (int j = cx + 1; j < m_CellPerLine; j++) 
+							{
+								if (Cells[nCurLine][j].nType != 0) 
+								{
+									nCurLine++; hasData = 1; break;
+								}
 							}
 						}
+						//nCurLine = maxy + 1; //鍙﹁捣涓�琛�
+						if (nCurLine > maxy)
+						{
+							maxy = nCurLine;
+						}
+						for (int j = cy; j < nCurLine; j++) 
+						{
+							Cells[j][cx].bLeftLineDn = 1;
+							Cells[j + 1][cx].bLeftLineUp = 1;
+						}
+						cy = nCurLine;
+						//*/
 					}
-					//					nCurLine = maxy + 1; //鍙﹁捣涓�琛�
-					if (nCurLine > maxy) maxy = nCurLine;
-					for (int j = cy; j < nCurLine; j++) {
-						Cells[j][cx].bLeftLineDn = 1;
-						Cells[j + 1][cx].bLeftLineUp = 1;
-					}
-					cy = nCurLine;
-//*/
-				}
-				else if (nPairOp == OP_ORS) {
-					cx = StPts[nSts - 1].x;
-					nCurLine = cy + 1; //鍙﹁捣涓�琛�
-					int hasData = 1;
-					while (hasData) {
-						hasData = 0;
-						for (int j = cx; j < m_CellPerLine; j++) {
-							if (Cells[nCurLine][j].nType != 0) {
-								nCurLine++; hasData = 1; break;
+					else if (nPairOp == OP_ORS) 
+					{
+						cx = StPts[nSts - 1].x;
+						nCurLine = cy + 1; //鍙﹁捣涓�琛�
+						int hasData = 1;
+						while (hasData) 
+						{
+							hasData = 0;
+							for (int j = cx; j < m_CellPerLine; j++) 
+							{
+								if (Cells[nCurLine][j].nType != 0) 
+								{
+									nCurLine++; hasData = 1; break;
+								}
 							}
 						}
+						//					nCurLine = maxy + 1; //鍙﹁捣涓�琛�
+						if (nCurLine > maxy) maxy = nCurLine;
+						for (int j = cy; j < nCurLine; j++) 
+						{
+							Cells[j][cx].bLeftLineDn = 1;
+							Cells[j + 1][cx].bLeftLineUp = 1;
+						}
+						cy = nCurLine;
 					}
-					//					nCurLine = maxy + 1; //鍙﹁捣涓�琛�
-					if (nCurLine > maxy) maxy = nCurLine;
-					for (int j = cy; j < nCurLine; j++) {
-						Cells[j][cx].bLeftLineDn = 1;
-						Cells[j + 1][cx].bLeftLineUp = 1;
+					else if (nPairOp == 0) 
+					{
+						nCurLine = maxy + 1; //鍙﹁捣涓�琛�
+						maxy = nCurLine;
+						cx = 0; cy = nCurLine;
 					}
-					cy = nCurLine;
 				}
-				else if (nPairOp == 0) {
-					nCurLine = maxy + 1; //鍙﹁捣涓�琛�
-					maxy = nCurLine;
-					cx = 0; cy = nCurLine;
-				}
-			}
-			stpos[nSts] = i;
-			StPts[nSts] = POINT{ cx,cy };
-			nSts++;
-			Cells[cy][cx].nType = pDoc->Progs[i].nOpType1 == OP_ST ? typeNO : typeNC; //typeNC
-			Cells[cy][cx].nProgStep = i;
-			Cells[cy][cx].sCoilName = pDoc->Progs[i].Params[0].sParamStr;
-			Cells[cy][cx].sParam = pDoc->Progs[i].Params[0].sParamStr;
-			Cells[cy][cx].nDataType = pDoc->Progs[i].Params[0].nParamType;
-			Cells[cy][cx].nDataAddr = pDoc->Progs[i].Params[0].nParamAddr;
-			cx++;	//绉诲埌涓嬩竴鏍�
-			break;
-		case OP_ST_EQ:
-		case OP_ST_NE:
-		case OP_ST_LT:
-		case OP_ST_GT:
-		case OP_ST_LE:
-		case OP_ST_GE:
-			if (i == 0) {
+				stpos[nSts] = i;
+				StPts[nSts] = POINT{ cx,cy };
+				nSts++;
+				Cells[cy][cx].nType = pDoc->Progs[i].nOpType1 == OP_ST ? typeNO : typeNC; //typeNC
+				Cells[cy][cx].nProgStep = i;
+				Cells[cy][cx].sCoilName = pDoc->Progs[i].Params[0].sParamStr;
+				Cells[cy][cx].sParam = pDoc->Progs[i].Params[0].sParamStr;
+				Cells[cy][cx].nDataType = pDoc->Progs[i].Params[0].nParamType;
+				Cells[cy][cx].nDataAddr = pDoc->Progs[i].Params[0].nParamAddr;
+				cx++;	//绉诲埌涓嬩竴鏍�
+				break;
+			case OP_ST_EQ:
+			case OP_ST_NE:
+			case OP_ST_LT:
+			case OP_ST_GT:
+			case OP_ST_LE:
+			case OP_ST_GE:
+				if (i == 0) {
 
-			}
-			else {
-				//璁板綍褰撳墠浣嶇疆 
-				EndPt[nEndPts] = POINT{ cx,cy };
-				nEndPts++;
-				if (nPairOp == OP_ANS) {
-					//缁х画鍓嶈繘				
-					//cx = 0, cy = nCurLine;
 				}
-				else if (nPairOp == OP_ORS) {
-					cx = StPts[nSts - 1].x;
-					nCurLine = cy + 1; //鍙﹁捣涓�琛�
-					int hasData = 1;
-					while (hasData) {
-						hasData = 0;
-						for (int j = cx; j < m_CellPerLine; j++) {
-							if (Cells[nCurLine][j].nType != 0) {
-								nCurLine++; hasData = 1; break;
+				else {
+					//璁板綍褰撳墠浣嶇疆 
+					EndPt[nEndPts] = POINT{ cx,cy };
+					nEndPts++;
+					if (nPairOp == OP_ANS) {
+						//缁х画鍓嶈繘				
+						//cx = 0, cy = nCurLine;
+					}
+					else if (nPairOp == OP_ORS) {
+						cx = StPts[nSts - 1].x;
+						nCurLine = cy + 1; //鍙﹁捣涓�琛�
+						int hasData = 1;
+						while (hasData) {
+							hasData = 0;
+							for (int j = cx; j < m_CellPerLine; j++) {
+								if (Cells[nCurLine][j].nType != 0) {
+									nCurLine++; hasData = 1; break;
+								}
 							}
 						}
-					}
-					//					nCurLine = maxy + 1; //鍙﹁捣涓�琛�
-					if (nCurLine > maxy) maxy = nCurLine;
-					for (int j = cy; j < nCurLine; j++) {
-						Cells[j][cx].bLeftLineDn = 1;
-						Cells[j + 1][cx].bLeftLineUp = 1;
-					}
-					cy = nCurLine;
-				}
-				else if (nPairOp == 0) {
-					nCurLine = maxy + 1; //鍙﹁捣涓�琛�
-					maxy = nCurLine;
-					cx = 0; cy = nCurLine;
-				}
-			}
-			stpos[nSts] = i;
-			StPts[nSts] = POINT{ cx,cy };
-			nSts++;
-			Cells[cy][cx].nType = typeCMP;
-			Cells[cy][cx].nProgStep = i;
-			Cells[cy][cx].sParam = OpName;
-			Cells[cy][cx].sCoilName = ShowTxt;
-			Cells[cy][cx + 1].nType = typeExt1;
-			Cells[cy][cx + 1].sParam = pDoc->Progs[i].Params[0].sParamStr;
-			Cells[cy][cx + 1].nDataType = pDoc->Progs[i].Params[0].nParamType;
-			Cells[cy][cx + 1].nDataAddr = pDoc->Progs[i].Params[0].nParamAddr;
-			Cells[cy][cx + 2].nType = typeExt1;
-			Cells[cy][cx + 2].sParam = pDoc->Progs[i].Params[1].sParamStr;
-			Cells[cy][cx + 2].nDataType = pDoc->Progs[i].Params[1].nParamType;
-			Cells[cy][cx + 2].nDataAddr = pDoc->Progs[i].Params[1].nParamAddr;
-			cx += 3;	//绉诲埌涓�3鏍�
-			break;
-
-		case OP_AN:
-		case OP_AN_:
-
-			Cells[cy][cx].nType = nOp == OP_AN ? typeNO : typeNC; //typeNC
-			Cells[cy][cx].nProgStep = i;
-			Cells[cy][cx].sParam = pDoc->Progs[i].Params[0].sParamStr;
-			Cells[cy][cx].sCoilName = pDoc->Progs[i].Params[0].sParamStr;
-			Cells[cy][cx].nDataType = pDoc->Progs[i].Params[0].nParamType;
-			Cells[cy][cx].nDataAddr = pDoc->Progs[i].Params[0].nParamAddr;
-			cx++;
-			break;
-		case OP_AN_EQ:
-		case OP_AN_NE:
-		case OP_AN_LT:
-		case OP_AN_GT:
-		case OP_AN_LE:
-		case OP_AN_GE:
-			Cells[cy][cx].nType = typeCMP;
-			Cells[cy][cx].nProgStep = i;
-			Cells[cy][cx].sCoilName = ShowTxt;
-			Cells[cy][cx + 1].nType = typeExt1;
-			Cells[cy][cx + 1].sParam = pDoc->Progs[i].Params[0].sParamStr;
-			Cells[cy][cx + 1].nDataType = pDoc->Progs[i].Params[0].nParamType;
-			Cells[cy][cx + 1].nDataAddr = pDoc->Progs[i].Params[0].nParamAddr;
-			Cells[cy][cx + 2].nType = typeExt1;
-			Cells[cy][cx + 2].sParam = pDoc->Progs[i].Params[1].sParamStr;
-			Cells[cy][cx + 2].nDataType = pDoc->Progs[i].Params[1].nParamType;
-			Cells[cy][cx + 2].nDataAddr = pDoc->Progs[i].Params[1].nParamAddr;
-			cx += 3;
-			break;
-
-		case OP_OR:
-		case OP_OR_:
-			EndPt[nEndPts] = POINT{ cx,cy };
-			nEndPts++;
-			cx = StPts[nSts - 1].x;
-			nCurLine = cy + 1; //鍙﹁捣涓�琛�
-			{
-				int hasData = 1;
-				while (hasData) {
-					hasData = 0;
-					for (int j = cx; j < m_CellPerLine; j++) {
-						if (Cells[nCurLine][j].nType != 0) {
-							nCurLine++; hasData = 1; break;
+						//					nCurLine = maxy + 1; //鍙﹁捣涓�琛�
+						if (nCurLine > maxy) maxy = nCurLine;
+						for (int j = cy; j < nCurLine; j++) {
+							Cells[j][cx].bLeftLineDn = 1;
+							Cells[j + 1][cx].bLeftLineUp = 1;
 						}
+						cy = nCurLine;
+					}
+					else if (nPairOp == 0) {
+						nCurLine = maxy + 1; //鍙﹁捣涓�琛�
+						maxy = nCurLine;
+						cx = 0; cy = nCurLine;
 					}
 				}
-			}
-			if (nCurLine > maxy)	maxy = nCurLine;
-			for (int j = cy; j < nCurLine; j++) {
-				Cells[j][cx].bLeftLineDn = 1;
-				Cells[j + 1][cx].bLeftLineUp = 1;
-			}
-			cy = nCurLine;
-			Cells[cy][cx].nProgStep = i;
-			Cells[cy][cx].nType = nOp == OP_OR ? typeNO : typeNC; //typeNC
-			Cells[cy][cx].sCoilName = pDoc->Progs[i].Params[0].sParamStr;
-			Cells[cy][cx].sParam = pDoc->Progs[i].Params[0].sParamStr;
-			Cells[cy][cx].nDataType = pDoc->Progs[i].Params[0].nParamType;
-			Cells[cy][cx].nDataAddr = pDoc->Progs[i].Params[0].nParamAddr;
-			cx++;
-			if (cx < EndPt[nEndPts - 1].x)
-			{	//鏈琛ラ綈
-				for (int j = cx; j < EndPt[nEndPts - 1].x; j++)
-				{
-					Cells[cy][j].nType = typeLine1;
-				}
-				cx = EndPt[nEndPts - 1].x;
-			}
-			else {
-				//鍓嶄竴琛岃ˉ榻�
-				for (int j = EndPt[nEndPts - 1].x; j < cx; j++)
-				{
-					Cells[EndPt[nEndPts - 1].y][j].nType = typeLine1;
-				}
+				stpos[nSts] = i;
+				StPts[nSts] = POINT{ cx,cy };
+				nSts++;
+				Cells[cy][cx].nType = typeCMP;
+				Cells[cy][cx].nProgStep = i;
+				Cells[cy][cx].sParam = OpName;
+				Cells[cy][cx].sCoilName = ShowTxt;
+				Cells[cy][cx + 1].nType = typeExt1;
+				Cells[cy][cx + 1].sParam = pDoc->Progs[i].Params[0].sParamStr;
+				Cells[cy][cx + 1].nDataType = pDoc->Progs[i].Params[0].nParamType;
+				Cells[cy][cx + 1].nDataAddr = pDoc->Progs[i].Params[0].nParamAddr;
+				Cells[cy][cx + 2].nType = typeExt1;
+				Cells[cy][cx + 2].sParam = pDoc->Progs[i].Params[1].sParamStr;
+				Cells[cy][cx + 2].nDataType = pDoc->Progs[i].Params[1].nParamType;
+				Cells[cy][cx + 2].nDataAddr = pDoc->Progs[i].Params[1].nParamAddr;
+				cx += 3;	//绉诲埌涓�3鏍�
+				break;
 
-			}
-			//杩炴帴涓婁笅绾�
-			for (int j = EndPt[nEndPts - 1].y; j < cy; j++)
-			{
-				Cells[j][cx].bLeftLineDn = 1;
-				Cells[j + 1][cx].bLeftLineUp = 1;
-			}
-			//鍏夋爣浣嶇疆, 鍓嶄竴缁撴潫鐐逛綅缃�
-			cy = EndPt[nEndPts - 1].y;
-			nEndPts--;
+			case OP_AN:
+			case OP_AN_:
 
-			break;
-		case OP_OR_EQ:
-		case OP_OR_NE:
-		case OP_OR_LT:
-		case OP_OR_GT:
-		case OP_OR_LE:
-		case OP_OR_GE:
-			EndPt[nEndPts] = POINT{ cx,cy };
-			nEndPts++;
-			cx = StPts[nSts - 1].x;
-			nCurLine = cy + 1; //鍙﹁捣涓�琛�
-			{
-				int hasData = 1;
-				while (hasData) {
-					hasData = 0;
-					for (int j = cx; j < m_CellPerLine; j++) {
-						if (Cells[nCurLine][j].nType != 0) {
-							nCurLine++; hasData = 1; break;
-						}
-					}
-				}
-			}
-			if (nCurLine > maxy)	maxy = nCurLine;
-			for (int j = cy; j < nCurLine; j++) {
-				Cells[j][cx].bLeftLineDn = 1;
-				Cells[j + 1][cx].bLeftLineUp = 1;
-			}
-			cy = nCurLine;
-			Cells[cy][cx].nType = typeCMP;
-			Cells[cy][cx].nProgStep = i;
-			Cells[cy][cx].sCoilName = ShowTxt;
-			Cells[cy][cx + 1].nType = typeExt1;
-			Cells[cy][cx + 1].sParam = pDoc->Progs[i].Params[0].sParamStr;
-			Cells[cy][cx + 1].nDataType = pDoc->Progs[i].Params[0].nParamType;
-			Cells[cy][cx + 1].nDataAddr = pDoc->Progs[i].Params[0].nParamAddr;
-			Cells[cy][cx + 2].nType = typeExt1;
-			Cells[cy][cx + 2].sParam = pDoc->Progs[i].Params[1].sParamStr;
-			Cells[cy][cx + 2].nDataType = pDoc->Progs[i].Params[1].nParamType;
-			Cells[cy][cx + 2].nDataAddr = pDoc->Progs[i].Params[1].nParamAddr;
-			cx += 3;
-			if (cx < EndPt[nEndPts - 1].x)
-			{	//鏈琛ラ綈
-				for (int j = cx; j < EndPt[nEndPts - 1].x; j++)
-				{
-					Cells[cy][j].nType = typeLine1;
-				}
-				cx = EndPt[nEndPts - 1].x;
-			}
-			else {
-				//鍓嶄竴琛岃ˉ榻�
-				for (int j = EndPt[nEndPts - 1].x; j < cx; j++)
-				{
-					Cells[EndPt[nEndPts - 1].y][j].nType = typeLine1;
-				}
-
-			}
-			//杩炴帴涓婁笅绾�
-			for (int j = EndPt[nEndPts - 1].y; j < cy; j++)
-			{
-				Cells[j][cx].bLeftLineDn = 1;
-				Cells[j + 1][cx].bLeftLineUp = 1;
-			}
-			//鍏夋爣浣嶇疆, 鍓嶄竴缁撴潫鐐逛綅缃�
-			cy = EndPt[nEndPts - 1].y;
-			nEndPts--;
-
-			break;
-		case OP_NOT:
-			Cells[cy][cx].nType = typeNOT;
-			Cells[cy][cx].sCoilName = _T("NOT");
-
-			Cells[cy][cx].nProgStep = i;
-			cx++;
-			break;
-		case OP_DF:
-		case OP_DF_:
-			Cells[cy][cx].nType = nOp == OP_DF ? typeDF : typeDF_;
-			Cells[cy][cx].sCoilName = nOp == OP_DF ? _T("DF") : _T("DF/");
-
-			Cells[cy][cx].nProgStep = i;
-			cx++;
-			break;
-		case OP_ANS:
-			nSts--;
-			nEndPts--;
-
-			break;
-		case OP_ORS:
-			// 褰撳墠搴忓垪涓庡墠闈㈢殑搴忓垪鍚堝苟銆�
-			//褰撳墠搴忓垪鐨勫紑濮嬬粨鏉熶綅缃�
-			//EndPt[nEndPts] = POINT{ cx,cy };
-			//nEndPts++;
-			//StPts[nSts - 1]; EndPt[nEndPts - 1];
-			//鍓嶄竴搴忓垪鐨勫紑濮嬬粨鏉熶綅缃�
-			StPts[nSts - 1]; EndPt[nEndPts - 1];
-			//鍒ゆ柇浜岃�呴暱搴�
-			if (cx < EndPt[nEndPts - 1].x)
-			{	//鏈琛ラ綈
-				for (int j = cx; j < EndPt[nEndPts - 1].x; j++)
-				{
-					Cells[cy][j].nType = typeLine1;
-				}
-				cx = EndPt[nEndPts - 1].x;
-			}
-			else {
-				//鍓嶄竴琛岃ˉ榻�
-				for (int j = EndPt[nEndPts - 1].x; j < cx; j++)
-				{
-					Cells[EndPt[nEndPts - 1].y][j].nType = typeLine1;
-				}
-
-			}
-			//杩炴帴涓婁笅绾�
-			for (int j = EndPt[nEndPts - 1].y; j < cy; j++)
-			{
-				Cells[j][cx].bLeftLineDn = 1;
-				Cells[j + 1][cx].bLeftLineUp = 1;
-			}
-			//鍏夋爣浣嶇疆, 鍓嶄竴缁撴潫鐐逛綅缃�
-			cy = EndPt[nEndPts - 1].y;
-			nSts--;
-			nEndPts--;
-
-			break;
-		case OP_PSHS:
-			EndPt[nEndPts] = POINT{ cx,cy };
-			nEndPts++;
-			break;
-		case OP_RDS:
-			cx = EndPt[nEndPts - 1].x;
-			for (int j = cx; j < m_CellPerLine; j++) {
-				if (Cells[cy][j].nType != 0)
-				{
-					cy++; j = cx - 1; //break;
-				}
-			}
-			for (int j = EndPt[nEndPts - 1].y; j < cy; j++)
-			{
-				Cells[j][cx].bLeftLineDn = 1;
-				Cells[j + 1][cx].bLeftLineUp = 1;
-			}
-			break;
-		case OP_POPS:
-			cx = EndPt[nEndPts - 1].x;
-			for (int j = cx; j < m_CellPerLine; j++) {
-				if (Cells[cy][j].nType != 0)
-				{
-					cy++; j = cx - 1; break;
-				}
-			}
-			for (int j = EndPt[nEndPts - 1].y; j < cy; j++)
-			{
-				Cells[j][cx].bLeftLineDn = 1;
-				Cells[j + 1][cx].bLeftLineUp = 1;
-			}
-			nEndPts--;
-
-			break;
-		case OP_OUT:
-		case OP_SET:
-		case OP_RESET:
-			nCellType = nOp == OP_OUT ? typeOUT : nOp == OP_SET ? typeSET : typeRESET;
-			nCurLine = cy;
-			if (cx <= m_CellPerLine) {
-
-				int hasData = 1;
-				while (hasData) {
-					hasData = 0;
-					for (int j = cx; j < m_CellPerLine; j++) {
-						if (Cells[nCurLine][j].nType != 0) {
-							nCurLine++; hasData = 1; break;
-						}
-					}
-				}
-				//if (nCurLine > maxy)	maxy = nCurLine;
-				for (int j = cy; j < nCurLine; j++) {
-					Cells[j][cx].bLeftLineDn = 1;
-					Cells[j + 1][cx].bLeftLineUp = 1;
-				}
-				cy = nCurLine;
-				for (int j = cx; j < m_CellPerLine; j++)
-				{
-					Cells[cy][j].nType = typeLine1;
-				}
-				Cells[cy][m_CellPerLine - 1].nType = nCellType;
-				Cells[cy][m_CellPerLine - 1].nProgStep = i;
-				Cells[cy][m_CellPerLine - 1].sParam = pDoc->Progs[i].Params[0].sParamStr;
-				Cells[cy][m_CellPerLine - 1].sCoilName = pDoc->Progs[i].Params[0].sParamStr;
-				Cells[cy][m_CellPerLine - 1].nDataType = pDoc->Progs[i].Params[0].nParamType;
-				Cells[cy][m_CellPerLine - 1].nDataAddr = pDoc->Progs[i].Params[0].nParamAddr;
-				
-			}
-			else {
-				Cells[cy][cx].nType = nCellType;
+				Cells[cy][cx].nType = nOp == OP_AN ? typeNO : typeNC; //typeNC
 				Cells[cy][cx].nProgStep = i;
 				Cells[cy][cx].sParam = pDoc->Progs[i].Params[0].sParamStr;
 				Cells[cy][cx].sCoilName = pDoc->Progs[i].Params[0].sParamStr;
 				Cells[cy][cx].nDataType = pDoc->Progs[i].Params[0].nParamType;
 				Cells[cy][cx].nDataAddr = pDoc->Progs[i].Params[0].nParamAddr;
+				cx++;
+				break;
+			case OP_AN_EQ:
+			case OP_AN_NE:
+			case OP_AN_LT:
+			case OP_AN_GT:
+			case OP_AN_LE:
+			case OP_AN_GE:
+				Cells[cy][cx].nType = typeCMP;
+				Cells[cy][cx].nProgStep = i;
+				Cells[cy][cx].sCoilName = ShowTxt;
+				Cells[cy][cx + 1].nType = typeExt1;
+				Cells[cy][cx + 1].sParam = pDoc->Progs[i].Params[0].sParamStr;
+				Cells[cy][cx + 1].nDataType = pDoc->Progs[i].Params[0].nParamType;
+				Cells[cy][cx + 1].nDataAddr = pDoc->Progs[i].Params[0].nParamAddr;
+				Cells[cy][cx + 2].nType = typeExt1;
+				Cells[cy][cx + 2].sParam = pDoc->Progs[i].Params[1].sParamStr;
+				Cells[cy][cx + 2].nDataType = pDoc->Progs[i].Params[1].nParamType;
+				Cells[cy][cx + 2].nDataAddr = pDoc->Progs[i].Params[1].nParamAddr;
+				cx += 3;
+				break;
+
+			case OP_OR:
+			case OP_OR_:
+				EndPt[nEndPts] = POINT{ cx,cy };
+ 				nEndPts++;
+				cx = StPts[nSts - 1].x;
+				nCurLine = cy + 1; //鍙﹁捣涓�琛�
+				{
+					int hasData = 1;
+					while (hasData) 
+					{
+						hasData = 0;             
+						for (int j = cx; j < m_CellPerLine; j++) 
+						{
+							if (Cells[nCurLine][j].nType != 0) 
+							{
+								nCurLine++; 
+								hasData = 1; 
+								break;
+							}
+						}
+					}
+				}
+				if (nCurLine > maxy)
+				{
+					maxy = nCurLine;
+				}
+				for (int j = cy; j < nCurLine; j++) 
+				{
+					Cells[j][cx].bLeftLineDn = 1;
+					Cells[j + 1][cx].bLeftLineUp = 1;
+				}
+				cy = nCurLine;
+				StPts[nSts] = POINT{ cx,cy };//+++
+				Cells[cy][cx].nProgStep = i;
+				Cells[cy][cx].nType = nOp == OP_OR ? typeNO : typeNC; //typeNC
+				Cells[cy][cx].sCoilName = pDoc->Progs[i].Params[0].sParamStr;
+				Cells[cy][cx].sParam = pDoc->Progs[i].Params[0].sParamStr;
+				Cells[cy][cx].nDataType = pDoc->Progs[i].Params[0].nParamType;
+				Cells[cy][cx].nDataAddr = pDoc->Progs[i].Params[0].nParamAddr;
+				cx++;
+				//if (cx <= EndPt[nEndPts - 1].x)
+				//{	//鏈琛ラ綈
+				//	for (int j = cx; j < EndPt[nEndPts - 1].x; j++)
+				//	{
+				//		Cells[cy][j].nType = typeLine1;
+				//	}
+				//	cx = EndPt[nEndPts - 1].x;
+				//}
+				//else {
+				//	//鍓嶄竴琛岃ˉ榻�
+				//	for (int j = EndPt[nEndPts - 1].x; j < cx; j++)
+				//	{
+				//		Cells[EndPt[nEndPts - 1].y][j].nType = typeLine1;
+				//	}
+
+				//}
+				//杩炴帴涓婁笅绾�
+				for (int j = EndPt[nEndPts - 1].y; j < cy; j++)
+				{
+					Cells[j][cx].bLeftLineDn = 1;
+					Cells[j + 1][cx].bLeftLineUp = 1;
+				}
+				//鍏夋爣浣嶇疆, 鍓嶄竴缁撴潫鐐逛綅缃�
+				cy = EndPt[nEndPts - 1].y;
+
+				nEndPts--;
+
+				break;
+			case OP_OR_EQ:
+			case OP_OR_NE:
+			case OP_OR_LT:
+			case OP_OR_GT:
+			case OP_OR_LE:
+			case OP_OR_GE:
+				EndPt[nEndPts] = POINT{ cx,cy };
+				nEndPts++;
+				cx = StPts[nSts - 1].x;
+				nCurLine = cy + 1; //鍙﹁捣涓�琛�
+				{
+					int hasData = 1;
+					while (hasData) {
+						hasData = 0;
+						for (int j = cx; j < m_CellPerLine; j++) {
+							if (Cells[nCurLine][j].nType != 0) {
+								nCurLine++; hasData = 1; break;
+							}
+						}
+					}
+				}
+				if (nCurLine > maxy)	maxy = nCurLine;
+				for (int j = cy; j < nCurLine; j++) {
+					Cells[j][cx].bLeftLineDn = 1;
+					Cells[j + 1][cx].bLeftLineUp = 1;
+				}
+				cy = nCurLine;
+				Cells[cy][cx].nType = typeCMP;
+				Cells[cy][cx].nProgStep = i;
+				Cells[cy][cx].sCoilName = ShowTxt;
+				Cells[cy][cx + 1].nType = typeExt1;
+				Cells[cy][cx + 1].sParam = pDoc->Progs[i].Params[0].sParamStr;
+				Cells[cy][cx + 1].nDataType = pDoc->Progs[i].Params[0].nParamType;
+				Cells[cy][cx + 1].nDataAddr = pDoc->Progs[i].Params[0].nParamAddr;
+				Cells[cy][cx + 2].nType = typeExt1;
+				Cells[cy][cx + 2].sParam = pDoc->Progs[i].Params[1].sParamStr;
+				Cells[cy][cx + 2].nDataType = pDoc->Progs[i].Params[1].nParamType;
+				Cells[cy][cx + 2].nDataAddr = pDoc->Progs[i].Params[1].nParamAddr;
+				cx += 3;
+				if (cx < EndPt[nEndPts - 1].x)
+				{	//鏈琛ラ綈
+					for (int j = cx; j < EndPt[nEndPts - 1].x; j++)
+					{
+						Cells[cy][j].nType = typeLine1;
+					}
+					cx = EndPt[nEndPts - 1].x;
+				}
+				else {
+					//鍓嶄竴琛岃ˉ榻�
+					for (int j = EndPt[nEndPts - 1].x; j < cx; j++)
+					{
+						Cells[EndPt[nEndPts - 1].y][j].nType = typeLine1;
+					}
+
+				}
+				//杩炴帴涓婁笅绾�
+				for (int j = EndPt[nEndPts - 1].y; j < cy; j++)
+				{
+					Cells[j][cx].bLeftLineDn = 1;
+					Cells[j + 1][cx].bLeftLineUp = 1;
+				}
+				//鍏夋爣浣嶇疆, 鍓嶄竴缁撴潫鐐逛綅缃�
+				cy = EndPt[nEndPts - 1].y;
+				nEndPts--;
+
+				break;
+			case OP_NOT:
+				Cells[cy][cx].nType = typeNOT;
+				Cells[cy][cx].sCoilName = _T("NOT");
+
+				Cells[cy][cx].nProgStep = i;
+				cx++;
+				break;
+			case OP_DF:
+			case OP_DF_:
+				Cells[cy][cx].nType = nOp == OP_DF ? typeDF : typeDF_;
+				Cells[cy][cx].sCoilName = nOp == OP_DF ? _T("DF") : _T("DF/");
+
+				Cells[cy][cx].nProgStep = i;
+				cx++;
+				break;
+			case OP_ANS:
+				nSts--;
+				nEndPts--;
+
+				break;
+			case OP_ORS:
+				//褰撳墠搴忓垪涓庡墠闈㈢殑搴忓垪鍚堝苟銆�
+				//褰撳墠搴忓垪鐨勫紑濮嬬粨鏉熶綅缃�
+				//EndPt[nEndPts] = POINT{ cx,cy };
+				//nEndPts++;
+				//StPts[nSts - 1]; EndPt[nEndPts - 1];
+				//鍓嶄竴搴忓垪鐨勫紑濮嬬粨鏉熶綅缃�
+				StPts[nSts - 1]; 
+				EndPt[nEndPts - 1];
+				if (nEndPts <= 0)
+				{
+					break;
+				}
+
+				////鍒ゆ柇浜岃�呴暱搴�
+				//if (cx < EndPt[nEndPts - 1].x)
+				//{	
+				//	//鏈琛ラ綈
+				//	for (int j = cx; j < EndPt[nEndPts - 1].x; j++)
+				//	{
+				//		Cells[cy][j].nType = typeLine1;
+				//	}
+				//	cx = EndPt[nEndPts - 1].x;
+				//}
+				//else 
+				//{
+				//	//鍓嶄竴琛岃ˉ榻�
+				//	for (int j = EndPt[nEndPts - 1].x; j < cx; j++)
+				//	{
+				//		Cells[EndPt[nEndPts - 1].y][j].nType = typeLine1;
+				//	}
+				//}
+				////杩炴帴涓婁笅绾�
+				//for (int j = EndPt[nEndPts - 1].y; j < cy; j++)
+				//{
+				//	Cells[j][cx].bLeftLineDn = 1;
+				//	Cells[j + 1][cx].bLeftLineUp = 1;
+				//}
+				////鍏夋爣浣嶇疆, 鍓嶄竴缁撴潫鐐逛綅缃�
+				//cy = EndPt[nEndPts - 1].y;
+				nSts--;
+				nEndPts--;
+
+				break;
+			case OP_PSHS:
+				EndPt[nEndPts] = POINT{ cx,cy };
+				nEndPts++;
+				break;
+			case OP_RDS:
+				cx = EndPt[nEndPts - 1].x;
+				for (int j = cx; j < m_CellPerLine; j++) {
+					if (Cells[cy][j].nType != 0)
+					{
+						cy++; j = cx - 1; //break;
+					}
+				}
+				for (int j = EndPt[nEndPts - 1].y; j < cy; j++)
+				{
+					Cells[j][cx].bLeftLineDn = 1;
+					Cells[j + 1][cx].bLeftLineUp = 1;
+				}
+				break;
+			case OP_POPS:
+				cx = EndPt[nEndPts - 1].x;
+				for (int j = cx; j < m_CellPerLine; j++) {
+					if (Cells[cy][j].nType != 0)
+					{
+						cy++; j = cx - 1; break;
+					}
+				}
+				for (int j = EndPt[nEndPts - 1].y; j < cy; j++)
+				{
+					Cells[j][cx].bLeftLineDn = 1;
+					Cells[j + 1][cx].bLeftLineUp = 1;
+				}
+				nEndPts--;
+
+				break;
+			case OP_OUT:
+			case OP_SET:
+			case OP_RESET:
+				nCellType = nOp == OP_OUT ? typeOUT : nOp == OP_SET ? typeSET : typeRESET;
+				nCurLine = cy;
+				if (cx <= m_CellPerLine) {
+
+					int hasData = 1;
+					while (hasData) {
+						hasData = 0;
+						for (int j = cx; j < m_CellPerLine; j++) {
+							if (Cells[nCurLine][j].nType != 0) {
+								nCurLine++; hasData = 1; break;
+							}
+						}
+					}
+					//if (nCurLine > maxy)	maxy = nCurLine;
+					for (int j = cy; j < nCurLine; j++) {
+						Cells[j][cx].bLeftLineDn = 1;
+						Cells[j + 1][cx].bLeftLineUp = 1;
+					}
+					cy = nCurLine;
+					for (int j = cx; j < m_CellPerLine; j++)
+					{
+						Cells[cy][j].nType = typeLine1;
+					}
+					Cells[cy][m_CellPerLine - 1].nType = nCellType;
+					Cells[cy][m_CellPerLine - 1].nProgStep = i;
+					Cells[cy][m_CellPerLine - 1].sParam = pDoc->Progs[i].Params[0].sParamStr;
+					Cells[cy][m_CellPerLine - 1].sCoilName = pDoc->Progs[i].Params[0].sParamStr;
+					Cells[cy][m_CellPerLine - 1].nDataType = pDoc->Progs[i].Params[0].nParamType;
+					Cells[cy][m_CellPerLine - 1].nDataAddr = pDoc->Progs[i].Params[0].nParamAddr;
+
+				}
+				else {
+					Cells[cy][cx].nType = nCellType;
+					Cells[cy][cx].nProgStep = i;
+					Cells[cy][cx].sParam = pDoc->Progs[i].Params[0].sParamStr;
+					Cells[cy][cx].sCoilName = pDoc->Progs[i].Params[0].sParamStr;
+					Cells[cy][cx].nDataType = pDoc->Progs[i].Params[0].nParamType;
+					Cells[cy][cx].nDataAddr = pDoc->Progs[i].Params[0].nParamAddr;
+				}
+				if (cy > maxy) { maxy = cy; }
+				//cy++;
+				break;
+
+			case OP_TML:
+			case OP_TMR:
+			case OP_TMX:
+			case OP_TMY:
+				Cells[cy][cx].nType = typeTM;
+				Cells[cy][cx].nProgStep = i;
+				Cells[cy][cx].sCoilName = ShowTxt;
+				Cells[cy][cx + 1].nType = typeExt1;
+				Cells[cy][cx + 1].sParam = pDoc->Progs[i].Params[0].sParamStr;
+				Cells[cy][cx + 1].nDataType = KLDataTypeEV;
+				Cells[cy][cx + 1].nDataAddr = pDoc->Progs[i].Params[0].nParamAddr;
+				Cells[cy][cx + 2].nType = typeExt1;
+				Cells[cy][cx + 2].sParam = pDoc->Progs[i].Params[1].sParamStr;
+				Cells[cy][cx + 2].nDataType = pDoc->Progs[i].Params[1].nParamType;
+				Cells[cy][cx + 2].nDataAddr = pDoc->Progs[i].Params[1].nParamAddr;
+				cx += 3;
+				break;
+			case OP_INC:
+			case OP_DEC:
+				Cells[cy][cx].nType = typeFN1;
+				Cells[cy][cx].nProgStep = i;
+				Cells[cy][cx].sCoilName = ShowTxt;
+				Cells[cy][cx + 1].nType = typeExt1;
+				Cells[cy][cx + 1].sParam = pDoc->Progs[i].Params[0].sParamStr;
+				Cells[cy][cx + 1].nDataType = pDoc->Progs[i].Params[0].nParamType;
+				Cells[cy][cx + 1].nDataAddr = pDoc->Progs[i].Params[0].nParamAddr;
+				cx += 2;
+				break;
+
+			case OP_MV:
+			case OP_ADD2:
+			case OP_SUB2:
+				Cells[cy][cx].nType = typeFN2;
+				Cells[cy][cx].nProgStep = i;
+				Cells[cy][cx].sCoilName = ShowTxt;
+				Cells[cy][cx + 1].nType = typeExt1;
+				Cells[cy][cx + 1].sParam = pDoc->Progs[i].Params[0].sParamStr;
+				Cells[cy][cx + 1].nDataType = pDoc->Progs[i].Params[0].nParamType;
+				Cells[cy][cx + 1].nDataAddr = pDoc->Progs[i].Params[0].nParamAddr;
+				Cells[cy][cx + 2].nType = typeExt1;
+				Cells[cy][cx + 2].sParam = pDoc->Progs[i].Params[1].sParamStr;
+				Cells[cy][cx + 2].nDataType = pDoc->Progs[i].Params[1].nParamType;
+				Cells[cy][cx + 2].nDataAddr = pDoc->Progs[i].Params[1].nParamAddr;
+				cx += 3;
+				break;
+
+			case OP_ADD3:
+			case OP_SUB3:
+			case OP_MUL:
+			case OP_DIV:
+				Cells[cy][cx].nType = typeFN3;
+				Cells[cy][cx].nProgStep = i;
+				Cells[cy][cx].sCoilName = ShowTxt;
+				Cells[cy][cx + 1].nType = typeExt1;
+				Cells[cy][cx + 1].sParam = pDoc->Progs[i].Params[0].sParamStr;
+				Cells[cy][cx + 1].nDataType = pDoc->Progs[i].Params[0].nParamType;
+				Cells[cy][cx + 1].nDataAddr = pDoc->Progs[i].Params[0].nParamAddr;
+				Cells[cy][cx + 2].nType = typeExt1;
+				Cells[cy][cx + 2].sParam = pDoc->Progs[i].Params[1].sParamStr;
+				Cells[cy][cx + 2].nDataType = pDoc->Progs[i].Params[1].nParamType;
+				Cells[cy][cx + 2].nDataAddr = pDoc->Progs[i].Params[1].nParamAddr;
+				Cells[cy][cx + 3].nType = typeExt1;
+				Cells[cy][cx + 3].sParam = pDoc->Progs[i].Params[2].sParamStr;
+				Cells[cy][cx + 3].nDataType = pDoc->Progs[i].Params[2].nParamType;
+				Cells[cy][cx + 3].nDataAddr = pDoc->Progs[i].Params[2].nParamAddr;
+				cx += 4;
+				break;
+
+			default:
+				break;
 			}
-			if (cy > maxy) { maxy = cy; }
-			//cy++;
-			break;
-
-		case OP_TML:
-		case OP_TMR:
-		case OP_TMX:
-		case OP_TMY:
-			Cells[cy][cx].nType = typeTM;
-			Cells[cy][cx].nProgStep = i;
-			Cells[cy][cx].sCoilName = ShowTxt;
-			Cells[cy][cx + 1].nType = typeExt1;
-			Cells[cy][cx + 1].sParam = pDoc->Progs[i].Params[0].sParamStr;
-			Cells[cy][cx + 1].nDataType = KLDataTypeEV;
-			Cells[cy][cx + 1].nDataAddr = pDoc->Progs[i].Params[0].nParamAddr;
-			Cells[cy][cx + 2].nType = typeExt1;
-			Cells[cy][cx + 2].sParam = pDoc->Progs[i].Params[1].sParamStr;
-			Cells[cy][cx + 2].nDataType = pDoc->Progs[i].Params[1].nParamType;
-			Cells[cy][cx + 2].nDataAddr = pDoc->Progs[i].Params[1].nParamAddr;
-			cx += 3;
-			break;
-		case OP_INC:
-		case OP_DEC:
-			Cells[cy][cx].nType = typeFN1;
-			Cells[cy][cx].nProgStep = i;
-			Cells[cy][cx].sCoilName = ShowTxt;
-			Cells[cy][cx + 1].nType = typeExt1;
-			Cells[cy][cx + 1].sParam = pDoc->Progs[i].Params[0].sParamStr;
-			Cells[cy][cx + 1].nDataType = pDoc->Progs[i].Params[0].nParamType;
-			Cells[cy][cx + 1].nDataAddr = pDoc->Progs[i].Params[0].nParamAddr;
-			cx += 2;
-			break;
-
-		case OP_MV:
-		case OP_ADD2:
-		case OP_SUB2:
-			Cells[cy][cx].nType = typeFN2;
-			Cells[cy][cx].nProgStep = i;
-			Cells[cy][cx].sCoilName = ShowTxt;
-			Cells[cy][cx + 1].nType = typeExt1;
-			Cells[cy][cx + 1].sParam = pDoc->Progs[i].Params[0].sParamStr;
-			Cells[cy][cx + 1].nDataType = pDoc->Progs[i].Params[0].nParamType;
-			Cells[cy][cx + 1].nDataAddr = pDoc->Progs[i].Params[0].nParamAddr;
-			Cells[cy][cx + 2].nType = typeExt1;
-			Cells[cy][cx + 2].sParam = pDoc->Progs[i].Params[1].sParamStr;
-			Cells[cy][cx + 2].nDataType = pDoc->Progs[i].Params[1].nParamType;
-			Cells[cy][cx + 2].nDataAddr = pDoc->Progs[i].Params[1].nParamAddr;
-			cx += 3;
-			break;
-
-		case OP_ADD3:
-		case OP_SUB3:
-		case OP_MUL:
-		case OP_DIV:
-			Cells[cy][cx].nType = typeFN3;
-			Cells[cy][cx].nProgStep = i;
-			Cells[cy][cx].sCoilName = ShowTxt;
-			Cells[cy][cx + 1].nType = typeExt1;
-			Cells[cy][cx + 1].sParam = pDoc->Progs[i].Params[0].sParamStr;
-			Cells[cy][cx + 1].nDataType = pDoc->Progs[i].Params[0].nParamType;
-			Cells[cy][cx + 1].nDataAddr = pDoc->Progs[i].Params[0].nParamAddr;
-			Cells[cy][cx + 2].nType = typeExt1;
-			Cells[cy][cx + 2].sParam = pDoc->Progs[i].Params[1].sParamStr;
-			Cells[cy][cx + 2].nDataType = pDoc->Progs[i].Params[1].nParamType;
-			Cells[cy][cx + 2].nDataAddr = pDoc->Progs[i].Params[1].nParamAddr;
-			Cells[cy][cx + 3].nType = typeExt1;
-			Cells[cy][cx + 3].sParam = pDoc->Progs[i].Params[2].sParamStr;
-			Cells[cy][cx + 3].nDataType = pDoc->Progs[i].Params[2].nParamType;
-			Cells[cy][cx + 3].nDataAddr = pDoc->Progs[i].Params[2].nParamAddr;
-			cx += 4;
-			break;
-
-		default:
-			break;
 		}
 	}
+	catch (const std::exception&)
+	{
+		DbgLog(_T("鏃犳硶缁樺埗鍥惧舰锛堢▼搴忎笉鍚堢悊锛�"));
+	}
+
 	m_nTotalRow = maxy + 1;
 	/*
 		for (int i = 0; i < 25; i++)
@@ -2353,45 +2527,73 @@
 	needReDraw = 1;
 	return 0;
 }
+bool firstCoil = true;//鏈绗竴涓崟鍏冩牸
 
-
+/// <summary>
+/// 姊舰鍥捐浆Prog
+/// </summary>
+/// <returns></returns>
 int CMTerm1View::TransLDSToProg()
 {
-	// TODO: 鍦ㄦ澶勬坊鍔犲疄鐜颁唬鐮�.
 	CMTerm1Doc * pDoc = GetDocument();
 	CString s1;
 	s1.Format(_T("Trans LDS to PRrog"));
 	DbgLog(s1);
+	s1.Format(_T("灏嗘褰㈠浘杞垚prog鏍煎紡"));
+	DbgLog(s1);
 
-	//姊舰鍥� 鏁伴噺
+	//姊舰鍥� ROW鏁伴噺
+	s1.Format(_T("鎬昏琛屾暟 %d"), m_nTotalRow);
+	DbgLog(s1);
 	s1.Format(_T("Total Lines %d"), m_nTotalRow);
 	DbgLog(s1);
+	
 	//鍒嗘
 	int nDivCount = 0;
+	//鍒嗘 鐐归泦鍚�
 	int Divs[100] = { 0 };
-	for (int i = 0; i < m_nTotalRow; i++)	{
+	for (int i = 0; i < m_nTotalRow; i++)	
+	{
 		int nRow = i;
 		int bConnected = 0;
-		for (int j = 0; j < m_CellPerLine; j++)		{
+ 		for (int j = 0; j < m_CellPerLine; j++)		
+		{
 			int nCol = j;
-			if (Cells[nRow][nCol].bLeftLineDn) {
-				bConnected = 1; break;
+			if (Cells[nRow][nCol].bLeftLineDn) 
+			{
+				bConnected = 1; 
+				s1.Format(_T("row==%d col==%d 鏈夊乏涓嬬珫绾匡紒锛侊紒"), nRow,nCol);
+				DbgLog(s1);
+				break;
 			}
-		}
-		if (!bConnected) {
+  		}
+		if (!bConnected) 
+		{
 			//鎵惧埌涓�澶勫垎鐣岀偣
 			Divs[nDivCount] = i;
-			s1.Format(_T("Div at Line %d "), nRow);
+			s1.Format(_T("Div at Line %d 鏄� 锛氾細锛氾細锛氾細锛氾細锛氾細锛氾細鍒嗙晫鐐�"), nRow);
 			DbgLog(s1);
 			nDivCount++;
 		}
 	}
-	stProgSection allprogs;
+
+ 	stProgSection allprogs;
 	//姣忔鍗曠嫭澶勭悊
-	for (int i = 0; i < nDivCount; i++)	{
+	for (int i = 0; i < nDivCount; i++)	
+	{
 		int nStartLine, nEndLine;
-		if (i == 0) { nStartLine = 0; nEndLine = Divs[i]; }
-		else { nStartLine = Divs[i - 1]+1; nEndLine = Divs[i]; }
+		if (i == 0) 
+		{
+			nStartLine = 0; 
+			nEndLine = Divs[i]; 
+		}
+		else 
+		{
+			nStartLine = Divs[i - 1]+1;
+			nEndLine = Divs[i]; 
+		}
+		s1.Format(_T("鏈琛屽彿 璧峰-缁堟锛� %d - %d "), nStartLine, nEndLine);
+		DbgLog(s1);
 		s1.Format(_T("Process Line %d - %d "), nStartLine, nEndLine);
 		DbgLog(s1);
 		int nCounts[20] = { 0 };
@@ -2399,35 +2601,50 @@
 		int nCounts3[20] = { 0 };
 
 		s1.Empty();
-		for (int j = 0; j < m_CellPerLine; j++)	{
+		for (int j = 0; j < m_CellPerLine; j++)	
+		{
 			int nCount = 0;
 			int nCount2 = 0;
 			int nCount3 = 0;
-			for (int k = nStartLine; k <= nEndLine; k++) {
-				if (Cells[k][j].nType) {
+			for (int k = nStartLine; k <= nEndLine; k++) 
+			{
+				if (Cells[k][j].nType) 
+				{
 					nCount += 1;
 					nCount2 += 1;
 					nCount3 += 1;
-					if (Cells[k][j].bLeftLineUp) nCount2 -= 1;
+					if (Cells[k][j].bLeftLineUp)
+					{
+						nCount2 -= 1;
+					}
 //					if (Cells[k][j].bLeftLineDn) nCount2 -= 1;
-					if (j != m_CellPerLine - 1)	{
-						if (Cells[k][j + 1].bLeftLineUp) nCount3 -= 1;
+
+					if (j != m_CellPerLine - 1)	
+					{
+						if (Cells[k][j + 1].bLeftLineUp)
+						{
+							nCount3 -= 1;
+						}
 //						if (Cells[k][j + 1].bLeftLineDn) nCount3 -= 1;
 					}
-
 				}
 			}
 			nCounts[j] = nCount;
 			nCounts2[j] = nCount2;
 			nCounts3[j] = nCount3;
 		}
+
 		s1.Empty();
-		for (int j = 0; j < m_CellPerLine; j++) {
+		for (int j = 0; j < m_CellPerLine; j++) 
+		{
 			s1.AppendFormat(_T(" %d(%d)"), nCounts[j],nCounts3[j]);
 		}
+		DbgLog(_T(""));
+
 		DbgLog(s1);
 		s1.Empty();
-		for (int j = 0; j < m_CellPerLine; j++)	{
+		for (int j = 0; j < m_CellPerLine; j++)	
+		{
 			s1.AppendFormat(_T(" %d "), nCounts2[j]);
 		}
 		DbgLog(s1);
@@ -2438,22 +2655,31 @@
 		CString sProg;
 		int nAllSteps=0;
 		stProgSection Progsec;
-		for (int i = nStartLine; i <= nEndLine; i++){
+		for (int i = nStartLine; i <= nEndLine; i++)
+		{
 			CString sProgSec;
 			int nSteps = 0;;
 			nCurPosY = i;
 			stProgSection thisprogsec;
 			if (Cells[i][0].nType)
-			ScanLDSCells(nStartLine, nEndLine, nCurPosY, nCurPosX, 0, thisprogsec, sProgSec,nSteps);
+			{
+				firstCoil = true;
+				//寰幆閬嶅巻鍗曞厓鏍�(杞崲鐨勯�昏緫鍦ㄨ繖涓嚱鏁�)
+				ScanLDSCells(nStartLine, nEndLine, nCurPosY, nCurPosX, 0, thisprogsec, sProgSec, nSteps);
+			}
 			sProg += sProgSec;
-			nAllSteps += nSteps;
+ 			nAllSteps += nSteps;
 			Progsec += thisprogsec;
 		}
+
 		DbgLog(_T("\r\n")+ sProg);
 		int n = int(Progsec.Progs.size());
+		s1.Format(_T("绋嬪簭娈垫鏁帮細 %d "), n);
+		DbgLog(s1);
 		s1.Format(_T("progSec steps %d "), n);
 		DbgLog(s1);
-		for (int i = 0; i < n; i++) {
+		for (int i = 0; i < n; i++) 
+		{
 			int optype = Progsec.Progs[i].nOpType1;
 			CStringA OpTxtA, OpShowTxtA;
 			CString OpTxt, OpShowTxt;
@@ -2466,11 +2692,15 @@
 		}
 		allprogs += Progsec;
 	}
+
 	//杈撳嚭绋嬪簭
 	int n=(int)allprogs.Progs.size();
+	s1.Format(_T("鎬荤▼搴忔鏁� %d "), n);
+	DbgLog(s1);
 	s1.Format(_T("all prog steps %d "), n);
 	DbgLog(s1);
-	for (int i = 0; i < n; i++)	{
+	for (int i = 0; i < n; i++)	
+	{
 		int optype=allprogs.Progs[i].nOpType1;
 		allprogs.Progs[i].PairTo = 0;
 		allprogs.Progs[i].nBinStep = i;
@@ -2488,17 +2718,31 @@
 	return 0;
 }
 
-int CMTerm1View::ScanLDSCells(int nStartLine, int nEndLine, int nPosY, int nPosX, int nLevel, stProgSection & progsec, CString & sProgSec, int &nSteps)
+
+/// <summary>
+/// 锛氱粯鍒舵瘡涓崟鍏冩牸鐨勫唴瀹�
+/// </summary>
+/// <param name="nStartLine"></param>
+/// <param name="nEndLine"></param>
+/// <param name="nPosY">琛�</param>
+/// <param name="nPosX">鍒�</param>
+/// <param name="nLevel">灞傜骇</param>
+/// <param name="progsec">prog鏍煎紡鐨勬寚浠ら泦</param>
+/// <param name="sProgSec">鎸囦护闆�</param>
+/// <param name="nSteps">绋嬪簭姝ユ暟</param>
+/// <returns></returns>
+int CMTerm1View::ScanLDSCells(int nStartLine, int nEndLine, int nPosY, int nPosX,
+	int nLevel, stProgSection & progsec, CString & sProgSec, int &nSteps)
 {
-	// TODO: 鍦ㄦ澶勬坊鍔犲疄鐜颁唬鐮�.
 	CString s1;
 	int nCurPosX, nCurPosY;
-	int nNextX = 1;
+	int nNextX = 1;//姝ラ暱
 	nCurPosY = nPosY;
 	nCurPosX = nPosX;
 	int nCoilType, CoilAddr;
 	CMTerm1Doc* pDoc = GetDocument();
-	for (int j = nPosX; j < m_CellPerLine; j+=nNextX) {
+	for (int j = nPosX; j < m_CellPerLine; j += nNextX)
+	{
 		nCurPosX = j;
 		//鍏堝鐞嗗綋鍓嶅崟鍏�;
 		int nType = Cells[nCurPosY][nCurPosX].nType;
@@ -2506,19 +2750,235 @@
 		CMTerm1Doc::stProg theprog;
 		CStringA sCellNameA;
 		sCellNameA = sCellName;
-		if (nType == typeNO) {
-			if (j==0) {
-				s1.Format(_T("%d %d ST %s"), nCurPosY, nCurPosX,sCellName);
-				sProgSec.AppendFormat(_T("ST %s\r\n"),sCellName);
-				theprog.nOpType1 = OP_ST;
+
+		if (j + nNextX >= m_CellPerLine)
+		{
+			continue;
+		}
+
+		//1.鍏堣幏鍙栨鍗曞厓鏍煎彸渚т竴鏍肩殑绔栫嚎
+		if (Cells[nCurPosY][j + nNextX].bLeftLineUp
+			|| Cells[nCurPosY][j + nNextX].bLeftLineDn)
+		{
+
+			////鍏堢湅寰�涓婇潰鏈夋病鏈夎繛鎺�
+			//if (Cells[nCurPosY][j + nNextX].bLeftLineUp)
+			//{
+			//	// 寰�涓婇潰鏈夎繛鎺ヤ笖鏈夎Е鐐硅繛鎺ワ紝鎵嶄娇鐢∣RS
+			//	s1.Format(_T("%d %d ORS "), nCurPosY, nCurPosX);
+			//	DbgLog(s1);
+			//	theprog.nOpType1 = OP_ORS;
+			//	//theprog.nOpType1 = OP_ST;
+			//	//theprog.PairTo = OP_ORS;
+			//	progsec.Append(theprog);
+			//	sProgSec.AppendFormat(_T("ORS \r\n"));
+			//	firstCoil = false;
+			//	nSteps += 1;
+			//	nLevel -= 1;
+			//}
+			//宸︿笂鏄惁鏈夐摼鎺�
+			int nLeftUpCon = 0;
+			if (Cells[nCurPosY][j + nNextX].bLeftLineUp)
+			{
+				nLeftUpCon = 1;
+			}
+			//鍚戜笅鏌ユ壘锛岀湅鐪嬬珫绾跨殑宸︿晶涓嬮潰杩樻湁娌℃湁杩炴帴鍏朵粬涓滆タ
+			int nLeftDnCon = 0;//鍗曞厓鏍煎乏渚у悜涓嬭繛鎺ユ暟
+			for (int k = nCurPosY + 1; k <= nEndLine; k++)
+			{
+				if (Cells[k][j + nNextX].bLeftLineDn)
+				{
+					if (Cells[k][j + nNextX - 1].nType)
+					{
+						nLeftDnCon += 1;
+					}
+				}
+				else
+				{
+					break;  // 绔栫嚎鍒版渶涓嬮潰鐨勭鐐逛簡銆�
+				}
+			}
+
+			s1.Format(_T("LeftUp %d   LeftDn  %d"), nLeftUpCon, nLeftDnCon);
+			DbgLog(s1);
+
+			//宸︿晶涓嬮潰鏈夎繛鎺ワ紝閭d箞鎵弿鍒拌繖涓珫绾挎椂杩斿洖锛岀户缁壂鎻忓乏渚т笅闈㈢殑鍐呭銆�
+			if (nLeftDnCon)
+			{
+				return 1;
+			}
+			// 宸︿晶涓嬮潰娌℃湁杩炴帴锛岄偅涔堣繖涓氨鏄乏闈㈡渶鍚庣殑鍗曞厓锛屽紑濮嬪鐞嗗彸闈㈢殑鍗曞厓銆�
+			else
+			{
+				if (nLeftUpCon)
+				{
+					if (nPosX > 0 || nLevel > 0)
+					{
+						s1.Format(_T("%d %d ANS "), nCurPosY, nCurPosX);
+						DbgLog(s1);
+						theprog.nOpType1 = OP_ANS;
+						progsec.Append(theprog);
+						sProgSec.AppendFormat(_T("ANS \r\n"));
+						nSteps += 1;
+						nLevel -= 1;
+					}
+				}
+				//浠庡ご鍒板熬澶勭悊, 鏌ユ壘姝ょ珫绾跨殑鏈�楂樹綅缃�;
+				int nLineTop = nCurPosY;
+				int nLineBottom = nCurPosY;
+				for (int k = nCurPosY; k >= nStartLine; k--)
+				{
+					if (!Cells[k][j + nNextX].bLeftLineUp)
+					{
+						break;
+					}
+					nLineTop = k - 1;
+				}
+
+				// 鏌ユ壘鍙充晶鏈夊嚑涓繛鎺�
+				int nRightCon = 0;//鍗曞厓鏍煎彸渚ц繛鎺ユ暟
+				for (int k = nLineTop; k <= nEndLine; k++)
+				{
+					if (Cells[k][j + nNextX].nType)
+					{
+						nRightCon += 1;
+					}
+					nLineBottom = k;
+					if (!Cells[k][j + nNextX].bLeftLineDn)
+					{
+						break;
+					}
+				}
+				s1.Format(_T("VLine %d - %d : %d , right = %d "), nLineTop, nLineBottom, j + nNextX, nRightCon);
+				DbgLog(s1);
+
+				if (nRightCon == 1)
+				{
+					s1.Format(_T(">>>> Go %d : %d , level %d "), nLineTop, j + nNextX, nLevel);
+					DbgLog(s1);
+					CString ProgSec;
+					int theSteps = 0;
+					stProgSection thisprogsec;
+
+					//
+					int r = ScanLDSCells(nStartLine, nEndLine, nLineTop, j + nNextX, nLevel, thisprogsec, ProgSec, theSteps);
+
+					sProgSec += ProgSec;
+					nSteps += theSteps;
+					progsec += thisprogsec;
+					s1.Format(_T("<<<< Re %d : %d , Result %d "), nLineTop, j + nNextX, 0);
+					DbgLog(s1);
+				}
+				else
+				{
+					int nLastResult = 0;
+					int nRightCount = 0;
+					CString sCons[100];
+					CString ProgSecs[100];
+					int res[100] = { 0 };
+					int nLastSteps = 0;
+					for (int k = nLineTop; k <= nLineBottom; k++)
+					{
+						//s1.Format(_T("VLine %d - %d : %d       %d of %d "), nLineTop, nLineBottom, j + nNextX, k+1,nRightCon);
+						//DbgLog(s1);
+						if (Cells[k][j + nNextX].nType)
+						{
+							s1.Format(_T(" >>> Go %d : %d , level %d "), k, j + nNextX, nLevel + 1);
+							DbgLog(s1);
+							CString ProgSec;
+							int theSteps = 0;
+							nLevel += 1;
+							stProgSection thisprogsec;
+							res[nRightCount] = ScanLDSCells(nStartLine, nEndLine, k, j + nNextX, nLevel, thisprogsec, ProgSecs[nRightCount], theSteps);
+							nLastResult = res[nRightCount];
+							nSteps += theSteps;
+							progsec += thisprogsec;
+							//if (res[nRightCount] > 0) nLevel += 1;
+							sProgSec += ProgSec;
+							s1.Format(_T(" <<< Re %d : %d , Result %d Steps %d Last %d"), k, j + nNextX, res[nRightCount], theSteps, nLastSteps);
+							DbgLog(s1);
+							if (nRightCount == 0)
+							{
+
+							}
+							else if (res[nRightCount - 1] == 0)
+							{
+								s1.Format(_T(" POPS"), k, j + nNextX, nLevel + 1);
+								DbgLog(s1);
+								//sProgSec.AppendFormat(_T("POPS \r\n"));
+								if (nLastSteps > 1)
+								{
+									if (sCons[nRightCount - 1] == _T("POPS"))
+									{
+										sCons[nRightCount - 1] = _T("RDS");
+									}
+									else
+									{
+										sCons[nRightCount - 1] = _T("PSHS");
+									}
+									sCons[nRightCount] = _T("POPS");
+								}
+							}
+							else
+							{
+
+							}
+							nLastSteps = theSteps;
+							nRightCount++;
+						}
+					}
+					nLastResult = 0;
+					for (int k = 0; k < nRightCount; k++)
+					{
+						if (nLastResult || res[k])
+						{
+							s1 = _T("ST") + ProgSecs[k];
+
+							ProgSecs[k] = s1;
+						}
+						s1 = sCons[k] + _T("\r\n") + ProgSecs[k];
+						sProgSec.Append(s1);
+						nLastResult = res[k];
+					}
+					if (nLastResult)
+					{
+						sProgSec.Append(_T("ANS\r\n"));
+					}
+					return 0;
+				}
+			}
+		}
+
+		#pragma region  鏍规嵁鍗曞厓鏍肩被鍨嬭浆鎹负鎸囦护鍜宲rog
+
+		if (nType == typeNO)
+		{
+			if (firstCoil && nType != typeLine1 && nType != typeLine2)
+			{
+				//鍏堢湅寰�涓婇潰鏈夋病鏈夎繛鎺�
+				if (Cells[nCurPosY][j + nNextX].bLeftLineUp)
+				{
+					// 寰�涓婇潰鏈夎繛鎺ヤ笖鏈夎Е鐐硅繛鎺ワ紝鎵嶄娇鐢∣R
+					s1.Format(_T("%d %d OR %s"), nCurPosY, nCurPosX, sCellName);
+					sProgSec.AppendFormat(_T("OR %s\r\n"), sCellName);
+					theprog.nOpType1 = OP_OR;
+				}
+				else
+				{
+					s1.Format(_T("%d %d ST %s"), nCurPosY, nCurPosX, sCellName);
+					sProgSec.AppendFormat(_T("ST %s\r\n"), sCellName);
+					theprog.nOpType1 = OP_ST;
+				}
 				theprog.nParamCount = 1;
 				pDoc->TxtToCoilType(sCellNameA, &nCoilType, &CoilAddr);
 				theprog.Params[0].nParamType = nCoilType;
 				theprog.Params[0].nParamAddr = CoilAddr;
 				theprog.Params[0].sParamStr = sCellNameA;
 				progsec.Append(theprog);
+				firstCoil = false;
 			}
-			else {
+			else
+			{
 				s1.Format(_T("%d %d AN %s"), nCurPosY, nCurPosX, sCellName);
 				sProgSec.AppendFormat(_T("NO %s\r\n"), sCellName);
 				theprog.nOpType1 = OP_AN;
@@ -2531,10 +2991,12 @@
 			}
 			nSteps += 1;
 			DbgLog(s1);
-			nNextX =  1;
-		}else 
-		if (nType == typeNC) {
-			if (j == 0) {
+			nNextX = 1;
+		}
+		else if (nType == typeNC)
+		{
+			if (firstCoil && nType != typeLine1 && nType != typeLine2)
+			{
 				s1.Format(_T("%d %d ST/ %s"), nCurPosY, nCurPosX, sCellName);
 				sProgSec.AppendFormat(_T("ST/ %s\r\n"), sCellName);
 				theprog.nOpType1 = OP_ST_;
@@ -2544,7 +3006,7 @@
 				theprog.Params[0].nParamAddr = CoilAddr;
 				theprog.Params[0].sParamStr = sCellNameA;
 				progsec.Append(theprog);
-
+				firstCoil = false;
 			}
 			else
 			{
@@ -2560,52 +3022,58 @@
 			}
 			DbgLog(s1);
 			nSteps += 1;
-			nNextX =  1;
-		}else 
-		if (nType == typePP) {
+			nNextX = 1;
+		}
+		else if (nType == typePP)
+		{
 			s1.Format(_T("%d %d PP %s"), nCurPosY, nCurPosX, sCellName);
 			DbgLog(s1);
 			//progsec.Append(theprog);
 			sProgSec.AppendFormat(_T("PP %s\r\n"), sCellName);
 			nSteps += 1;
-			nNextX =  1;
-		}else 
-		if (nType == typePN) {
+			nNextX = 1;
+		}
+		else if (nType == typePN)
+		{
 			s1.Format(_T("%d %d PN %s"), nCurPosY, nCurPosX, sCellName);
 			DbgLog(s1);
 			//progsec.Append(theprog);
 			sProgSec.AppendFormat(_T("PN %s\r\n"), sCellName, sCellName);
 			nSteps += 1;
-			nNextX =  1;
-		}else 
-		if (nType == typeNOT) {
+			nNextX = 1;
+		}
+		else if (nType == typeNOT)
+		{
 			s1.Format(_T("%d %d NOT %s"), nCurPosY, nCurPosX, sCellName);
 			DbgLog(s1);
 			sProgSec.AppendFormat(_T("NOT %s\r\n"), sCellName);
 			theprog.nOpType1 = OP_NOT;
 			progsec.Append(theprog);
 			nSteps += 1;
-			nNextX =  1;
-		}else 
-		if (nType == typeDF) {
+			nNextX = 1;
+		}
+		else if (nType == typeDF)
+		{
 			s1.Format(_T("%d %d DF %s"), nCurPosY, nCurPosX, sCellName);
 			DbgLog(s1);
 			sProgSec.AppendFormat(_T("DF %s\r\n"), sCellName);
 			theprog.nOpType1 = OP_DF;
 			progsec.Append(theprog);
 			nSteps += 1;
-			nNextX =  1;
-		}else 
-		if (nType == typeDF_) {
+			nNextX = 1;
+		}
+		else if (nType == typeDF_)
+		{
 			s1.Format(_T("%d %d DF/ %s"), nCurPosY, nCurPosX, sCellName);
 			DbgLog(s1);
 			sProgSec.AppendFormat(_T("DF/ %s\r\n"), sCellName);
 			theprog.nOpType1 = OP_DF_;
 			progsec.Append(theprog);
 			nSteps += 1;
-			nNextX =  1;
-		}else 
-		if (nType == typeOUT) {
+			nNextX = 1;
+		}
+		else if (nType == typeOUT)
+		{
 			s1.Format(_T("%d %d OUT %s"), nCurPosY, nCurPosX, sCellName);
 			DbgLog(s1);
 			sProgSec.AppendFormat(_T("OUT %s\r\n"), sCellName);
@@ -2617,9 +3085,10 @@
 			theprog.Params[0].sParamStr = sCellNameA;
 			progsec.Append(theprog);
 			nSteps += 1;
-			nNextX =  1;
-		}else 
-		if (nType == typeSET) {
+			nNextX = 1;
+		}
+		else if (nType == typeSET)
+		{
 			s1.Format(_T("%d %d SET %s"), nCurPosY, nCurPosX, sCellName);
 			DbgLog(s1);
 			sProgSec.AppendFormat(_T("SET %s\r\n"), sCellName);
@@ -2631,9 +3100,10 @@
 			theprog.Params[0].sParamStr = sCellNameA;
 			progsec.Append(theprog);
 			nSteps += 1;
-			nNextX =  1;
-		}else 
-		if (nType == typeRESET) {
+			nNextX = 1;
+		}
+		else if (nType == typeRESET)
+		{
 			s1.Format(_T("%d %d RESET %s"), nCurPosY, nCurPosX, sCellName);
 			DbgLog(s1);
 			sProgSec.AppendFormat(_T("RESET %s\r\n"), sCellName);
@@ -2645,20 +3115,22 @@
 			theprog.Params[0].sParamStr = sCellNameA;
 			progsec.Append(theprog);
 			nSteps += 1;
-			nNextX =  1;
-		}else 
-		if (nType == typeCMP) {
-			s1.Format(_T("%d %d CMP %s %s %s"), nCurPosY, nCurPosX, sCellName,Cells[nCurPosY][nCurPosX+1].sParam,Cells[nCurPosY][nCurPosX + 2].sParam);
+			nNextX = 1;
+		}
+		else if (nType == typeCMP)
+		{
+			s1.Format(_T("%d %d CMP %s %s %s"), nCurPosY, nCurPosX, sCellName, Cells[nCurPosY][nCurPosX + 1].sParam, Cells[nCurPosY][nCurPosX + 2].sParam);
 			DbgLog(s1);
 			theprog.nOpType1 = OP_ST_GT;
 			theprog.nParamCount = 1;
 			progsec.Append(theprog);
 			sProgSec.AppendFormat(_T("CMP %s %s %s \r\n"), sCellName, Cells[nCurPosY][nCurPosX + 1].sParam, Cells[nCurPosY][nCurPosX + 2].sParam);
 			nSteps += 1;
-			nNextX =  3;
-		}else 
-		if (nType == typeTM) {
-			s1.Format(_T("%d %d TM %s %d %s"), nCurPosY, nCurPosX, sCellName,Cells[nCurPosY][nCurPosX+1].sParam,Cells[nCurPosY][nCurPosX + 2].sParam);
+			nNextX = 3;
+		}
+		else if (nType == typeTM)
+		{
+			s1.Format(_T("%d %d TM %s %d %s"), nCurPosY, nCurPosX, sCellName, Cells[nCurPosY][nCurPosX + 1].sParam, Cells[nCurPosY][nCurPosX + 2].sParam);
 			DbgLog(s1);
 			theprog.nOpType1 = OP_TMX;
 			theprog.nParamCount = 1;
@@ -2667,171 +3139,372 @@
 			theprog.Params[0].nParamAddr = CoilAddr;
 			theprog.Params[0].sParamStr = sCellNameA;
 			progsec.Append(theprog);
-			sProgSec.AppendFormat(_T("TM %s %d %s\r\n"), sCellName, Cells[nCurPosY][nCurPosX+1].sParam, Cells[nCurPosY][nCurPosX + 2].sParam);
+			sProgSec.AppendFormat(_T("TM %s %d %s\r\n"), sCellName, Cells[nCurPosY][nCurPosX + 1].sParam, Cells[nCurPosY][nCurPosX + 2].sParam);
 			nSteps += 1;
-			nNextX =  3;
-		}else 
-		if (nType == typeFN1) {
-			s1.Format(_T("%d %d FN1 %s %s"), nCurPosY, nCurPosX, sCellName,Cells[nCurPosY][nCurPosX + 1].sParam);
+			nNextX = 3;
+		}
+		else if (nType == typeFN1)
+		{
+			s1.Format(_T("%d %d FN1 %s %s"), nCurPosY, nCurPosX, sCellName, Cells[nCurPosY][nCurPosX + 1].sParam);
 			DbgLog(s1);
 			theprog.nOpType1 = OP_INC;
 			progsec.Append(theprog);
 			sProgSec.AppendFormat(_T("FN1 %s %s\r\n"), sCellName, Cells[nCurPosY][nCurPosX + 1].sParam);
 			nSteps += 1;
-			nNextX =  2;
-		}else 
-		if (nType == typeFN2) {
+			nNextX = 2;
+		}
+		else if (nType == typeFN2)
+		{
 			s1.Format(_T("%d %d FN2 %s %s %s "), nCurPosY, nCurPosX, sCellName, Cells[nCurPosY][nCurPosX + 1].sParam, Cells[nCurPosY][nCurPosX + 2].sParam);
 			DbgLog(s1);
 			theprog.nOpType1 = OP_MV;
 			progsec.Append(theprog);
 			sProgSec.AppendFormat(_T("FN2 %s %s %s \r\n"), sCellName, Cells[nCurPosY][nCurPosX + 1].sParam, Cells[nCurPosY][nCurPosX + 2].sParam);
 			nSteps += 1;
-			nNextX =  3;
-		}else 
-		if (nType == typeFN3) {
+			nNextX = 3;
+		}
+		else if (nType == typeFN3)
+		{
 			s1.Format(_T("%d %d FN3 %s %s %s %s"), nCurPosY, nCurPosX, sCellName, Cells[nCurPosY][nCurPosX + 1].sParam, Cells[nCurPosY][nCurPosX + 2].sParam, Cells[nCurPosY][nCurPosX + 3].sParam);
 			DbgLog(s1);
 			theprog.nOpType1 = OP_ADD3;
 			progsec.Append(theprog);
 			sProgSec.AppendFormat(_T("FN3 %s %s %s %s\r\n"), sCellName, Cells[nCurPosY][nCurPosX + 1].sParam, Cells[nCurPosY][nCurPosX + 2].sParam, Cells[nCurPosY][nCurPosX + 3].sParam);
 			nSteps += 1;
-			nNextX =  4;
-		}else {
-			nNextX =  1;
+			nNextX = 4;
+		}
+		else
+		{
+			nNextX = 1;
 			//continue;
 		}
+		
+		#pragma endregion
 
-		if (j + nNextX >= m_CellPerLine) continue;
-		if (Cells[nCurPosY][j + nNextX].bLeftLineUp || Cells[nCurPosY][j + nNextX].bLeftLineDn) {	// 鍙戠幇绔栫嚎
-			//鍏堢湅寰�涓婇潰鏈夋病鏈夎繛鎺�
-			if (Cells[nCurPosY][j + nNextX].bLeftLineUp) { // 寰�涓婇潰鏈夎繛鎺�
-				s1.Format(_T("%d %d ORS "), nCurPosY, nCurPosX);
-				DbgLog(s1);
-				theprog.nOpType1 = OP_ORS;
-				progsec.Append(theprog);
-				sProgSec.AppendFormat(_T("ORS \r\n"));
-				nSteps += 1;
-				nLevel -= 1;
-			}
-			int nLeftUpCon = 0;
-			if (Cells[nCurPosY][j + nNextX].bLeftLineUp ) nLeftUpCon = 1;
-			//鍚戜笅鏌ユ壘锛岀湅鐪嬬珫绾跨殑宸︿晶涓嬮潰杩樻湁娌℃湁杩炴帴鍏朵粬涓滆タ/
-			int nLeftDnCon = 0;
-			for (int k = nCurPosY + 1; k <= nEndLine; k++) {
-				if (!Cells[k][j + nNextX].bLeftLineUp) break;  // 绔栫嚎鍒版渶涓嬮潰鐨勭鐐逛簡銆�
-				if (Cells[k][j + nNextX - 1 ].nType) { nLeftDnCon += 1; break; }
-			}
-			s1.Format(_T("LeftUp %d   LeftDn  %d"), nLeftUpCon, nLeftDnCon);
-			DbgLog(s1);
-			if (nLeftDnCon) {	//宸︿晶鏈夎繛鎺ワ紝閭d箞鎵弿鍒拌繖涓珫绾匡紝缁х画鎵弿宸︿晶涓嬮潰鐨勫唴瀹广��
-
-				return 1;
-			}
-			if (!nLeftDnCon) {// 宸︿晶涓嬮潰娌℃湁杩炴帴锛岄偅涔堣繖涓氨鏄乏闈㈡渶鍚庣殑鍗曞厓锛屽紑濮嬪鐞嗗彸闈㈢殑鍗曞厓銆�
-				if (nLeftUpCon) {
-					if ( nPosX >0 || nLevel > 0) {
-					s1.Format(_T("%d %d ANS "), nCurPosY, nCurPosX);
-					DbgLog(s1);
-					theprog.nOpType1 = OP_ANS;
-					progsec.Append(theprog);
-					sProgSec.AppendFormat(_T("ANS \r\n"));
-					nSteps += 1;
-					nLevel -= 1;
-				}
-
-				}
-				//浠庡ご鍒板熬澶勭悊, 鏌ユ壘姝ょ珫绾跨殑鏈�楂樹綅缃�;
-				int nLineTop = nCurPosY;
-				int nLineBottom = nCurPosY;
-				for (int k = nCurPosY; k >= nStartLine; k--)
-				{
-					if (!Cells[k][j + nNextX].bLeftLineUp) {  break; }
-					nLineTop = k -1;
-				}
-				// 鏌ユ壘鍙充晶鏈夊嚑涓繛鎺�
-				int nRightCon = 0;
-				for (int k = nLineTop; k <= nEndLine; k++) {
-					if (Cells[k][j + nNextX].nType) { nRightCon += 1; }
-					nLineBottom = k;
-					if (!Cells[k][j + nNextX].bLeftLineDn) { break; }
-				}
-				s1.Format(_T("VLine %d - %d : %d , right = %d "), nLineTop,nLineBottom,j+nNextX,nRightCon);
-				DbgLog(s1);
-				if (nRightCon == 1) {
-					s1.Format(_T(">>>> Go %d : %d , level %d "), nLineTop, j + nNextX, nLevel);
-					DbgLog(s1);
-					CString ProgSec;
-					int theSteps = 0;
-					stProgSection thisprogsec;
-					int r = ScanLDSCells(nStartLine, nEndLine, nLineTop, j + nNextX, nLevel,thisprogsec, ProgSec,theSteps);
-					sProgSec += ProgSec;
-					nSteps += theSteps;
-					progsec += thisprogsec;
-					s1.Format(_T("<<<< Re %d : %d , Result %d "), nLineTop, j + nNextX, r);
-					DbgLog(s1);
-				}
-				else
-				{
-					int nLastResult = 0;
-					int nRightCount = 0;
-					CString sCons[100];
-					CString ProgSecs[100];
-					int res[100] = { 0 };
-					int nLastSteps = 0;
-					for (int k = nLineTop; k <= nLineBottom; k++)
-					{
-						//s1.Format(_T("VLine %d - %d : %d       %d of %d "), nLineTop, nLineBottom, j + nNextX, k+1,nRightCon);
-						//DbgLog(s1);
-						if (Cells[k][j + nNextX].nType) {
-							s1.Format(_T(" >>> Go %d : %d , level %d "), k, j + nNextX, nLevel +1);
-							DbgLog(s1);
-							CString ProgSec;
-							int theSteps = 0;
-							nLevel += 1;
-							stProgSection thisprogsec;
-							res[nRightCount] = ScanLDSCells(nStartLine, nEndLine, k, j + nNextX, nLevel ,thisprogsec,ProgSecs[nRightCount],theSteps);
-							nLastResult = res[nRightCount];
-							nSteps += theSteps;
-							progsec += thisprogsec;
-							//if (res[nRightCount] > 0) nLevel += 1;
-							sProgSec += ProgSec;
-							s1.Format(_T(" <<< Re %d : %d , Result %d Steps %d Last %d"), k, j + nNextX, res[nRightCount],theSteps,nLastSteps);
-							DbgLog(s1);
-							if (nRightCount == 0) {
-							}
-							else if (res[nRightCount-1] == 0) {
-								s1.Format(_T(" POPS"), k, j + nNextX, nLevel + 1);
-								DbgLog(s1);
-								//sProgSec.AppendFormat(_T("POPS \r\n"));
-								if (nLastSteps > 1) 
-								{
-									if (sCons[nRightCount - 1] == _T("POPS")) { sCons[nRightCount - 1] = _T("RDS"); }
-									else { sCons[nRightCount - 1] = _T("PSHS"); }
-									sCons[nRightCount] = _T("POPS");
-								}
-							}
-							else {
-
-							}
-							nLastSteps = theSteps;
-							nRightCount++;
-						}
-					}
-					nLastResult = 0;
-					for (int k = 0; k < nRightCount; k++)
-					{
-						if (nLastResult || res[k]) { s1 = _T("ST") + ProgSecs[k];  ProgSecs[k] =s1; }
-						s1= sCons[k] + _T("\r\n") + ProgSecs[k];
-						sProgSec.Append(s1);
-						nLastResult = res[k];
-					}
-					if (nLastResult) sProgSec.Append(_T("ANS\r\n"));
-					return 0;
-				}
-			}
-		}
 	}
 	return 0;
-}
+ }
+ /*
+ void CMTerm1View::chuansileitetoprog(int nStartLine, int nEndLine, int nPosY, int nPosX, int nLevel, stProgSection& progsec, CString& sProgSec, int& nSteps)
+ {
+	 CString s1;
+	 int nCurPosX, nCurPosY;
+	 int nNextX = 1;//姝ラ暱
+	 nCurPosY = nPosY;
+	 nCurPosX = nPosX;
+	 int nCoilType, CoilAddr;
+	 CMTerm1Doc* pDoc = GetDocument();
+#pragma region  鏍规嵁鍗曞厓鏍肩被鍨嬭浆鎹负鎸囦护鍜宲rog
 
+	 if (nType == typeNO)
+	 {
+		 if (firstCoil && nType != typeLine1 && nType != typeLine2)
+		 {
+			 //鍏堢湅寰�涓婇潰鏈夋病鏈夎繛鎺�
+			 if (Cells[nCurPosY][j + nNextX].bLeftLineUp)
+			 {
+				 // 寰�涓婇潰鏈夎繛鎺ヤ笖鏈夎Е鐐硅繛鎺ワ紝鎵嶄娇鐢∣R
+				 s1.Format(_T("%d %d OR %s"), nCurPosY, nCurPosX, sCellName);
+				 sProgSec.AppendFormat(_T("OR %s\r\n"), sCellName);
+				   .nOpType1 = OP_OR;
+			 }
+			 else
+			 {
+				 s1.Format(_T("%d %d ST %s"), nCurPosY, nCurPosX, sCellName);
+				 sProgSec.AppendFormat(_T("ST %s\r\n"), sCellName);
+				 theprog.nOpType1 = OP_ST;
+			 }
+			 theprog.nParamCount = 1;
+			 pDoc->TxtToCoilType(sCellNameA, &nCoilType, &CoilAddr);
+			 theprog.Params[0].nParamType = nCoilType;
+			 theprog.Params[0].nParamAddr = CoilAddr;
+			 theprog.Params[0].sParamStr = sCellNameA;
+			 progsec.Append(theprog);
+			 firstCoil = false;
+		 }
+		 else
+		 {
+			 s1.Format(_T("%d %d AN %s"), nCurPosY, nCurPosX, sCellName);
+			 sProgSec.AppendFormat(_T("NO %s\r\n"), sCellName);
+			 theprog.nOpType1 = OP_AN;
+			 theprog.nParamCount = 1;
+			 pDoc->TxtToCoilType(sCellNameA, &nCoilType, &CoilAddr);
+			 theprog.Params[0].nParamType = nCoilType;
+			 theprog.Params[0].nParamAddr = CoilAddr;
+			 theprog.Params[0].sParamStr = sCellNameA;
+			 progsec.Append(theprog);
+		 }
+		 nSteps += 1;
+		 DbgLog(s1);
+		 nNextX = 1;
+	 }
+	 else if (nType == typeNC)
+	 {
+		 if (firstCoil && nType != typeLine1 && nType != typeLine2)
+		 {
+			 s1.Format(_T("%d %d ST/ %s"), nCurPosY, nCurPosX, sCellName);
+			 sProgSec.AppendFormat(_T("ST/ %s\r\n"), sCellName);
+			 theprog.nOpType1 = OP_ST_;
+			 theprog.nParamCount = 1;
+			 pDoc->TxtToCoilType(sCellNameA, &nCoilType, &CoilAddr);
+			 theprog.Params[0].nParamType = nCoilType;
+			 theprog.Params[0].nParamAddr = CoilAddr;
+			 theprog.Params[0].sParamStr = sCellNameA;
+			 progsec.Append(theprog);
+			 firstCoil = false;
+		 }
+		 else
+		 {
+			 s1.Format(_T("%d %d AN/ %s"), nCurPosY, nCurPosX, sCellName);
+			 sProgSec.AppendFormat(_T("AN/ %s\r\n"), sCellName);
+			 theprog.nOpType1 = OP_AN_;
+			 theprog.nParamCount = 1;
+			 pDoc->TxtToCoilType(sCellNameA, &nCoilType, &CoilAddr);
+			 theprog.Params[0].nParamType = nCoilType;
+			 theprog.Params[0].nParamAddr = CoilAddr;
+			 theprog.Params[0].sParamStr = sCellNameA;
+			 progsec.Append(theprog);
+		 }
+		 DbgLog(s1);
+		 nSteps += 1;
+		 nNextX = 1;
+	 }
+	 else if (nType == typePP)
+	 {
+		 s1.Format(_T("%d %d PP %s"), nCurPosY, nCurPosX, sCellName);
+		 DbgLog(s1);
+		 //progsec.Append(theprog);
+		 sProgSec.AppendFormat(_T("PP %s\r\n"), sCellName);
+		 nSteps += 1;
+		 nNextX = 1;
+	 }
+	 else if (nType == typePN)
+	 {
+		 s1.Format(_T("%d %d PN %s"), nCurPosY, nCurPosX, sCellName);
+		 DbgLog(s1);
+		 //progsec.Append(theprog);
+		 sProgSec.AppendFormat(_T("PN %s\r\n"), sCellName, sCellName);
+		 nSteps += 1;
+		 nNextX = 1;
+	 }
+	 else if (nType == typeNOT)
+	 {
+		 s1.Format(_T("%d %d NOT %s"), nCurPosY, nCurPosX, sCellName);
+		 DbgLog(s1);
+		 sProgSec.AppendFormat(_T("NOT %s\r\n"), sCellName);
+		 theprog.nOpType1 = OP_NOT;
+		 progsec.Append(theprog);
+		 nSteps += 1;
+		 nNextX = 1;
+	 }
+	 else if (nType == typeDF)
+	 {
+		 s1.Format(_T("%d %d DF %s"), nCurPosY, nCurPosX, sCellName);
+		 DbgLog(s1);
+		 sProgSec.AppendFormat(_T("DF %s\r\n"), sCellName);
+		 theprog.nOpType1 = OP_DF;
+		 progsec.Append(theprog);
+		 nSteps += 1;
+		 nNextX = 1;
+	 }
+	 else if (nType == typeDF_)
+	 {
+		 s1.Format(_T("%d %d DF/ %s"), nCurPosY, nCurPosX, sCellName);
+		 DbgLog(s1);
+		 sProgSec.AppendFormat(_T("DF/ %s\r\n"), sCellName);
+		 theprog.nOpType1 = OP_DF_;
+		 progsec.Append(theprog);
+		 nSteps += 1;
+		 nNextX = 1;
+	 }
+	 else if (nType == typeOUT)
+	 {
+		 s1.Format(_T("%d %d OUT %s"), nCurPosY, nCurPosX, sCellName);
+		 DbgLog(s1);
+		 sProgSec.AppendFormat(_T("OUT %s\r\n"), sCellName);
+		 theprog.nOpType1 = OP_OUT;
+		 theprog.nParamCount = 1;
+		 pDoc->TxtToCoilType(sCellNameA, &nCoilType, &CoilAddr);
+		 theprog.Params[0].nParamType = nCoilType;
+		 theprog.Params[0].nParamAddr = CoilAddr;
+		 theprog.Params[0].sParamStr = sCellNameA;
+		 progsec.Append(theprog);
+		 nSteps += 1;
+		 nNextX = 1;
+	 }
+	 else if (nType == typeSET)
+	 {
+		 s1.Format(_T("%d %d SET %s"), nCurPosY, nCurPosX, sCellName);
+		 DbgLog(s1);
+		 sProgSec.AppendFormat(_T("SET %s\r\n"), sCellName);
+		 theprog.nOpType1 = OP_SET;
+		 theprog.nParamCount = 1;
+		 pDoc->TxtToCoilType(sCellNameA, &nCoilType, &CoilAddr);
+		 theprog.Params[0].nParamType = nCoilType;
+		 theprog.Params[0].nParamAddr = CoilAddr;
+		 theprog.Params[0].sParamStr = sCellNameA;
+		 progsec.Append(theprog);
+		 nSteps += 1;
+		 nNextX = 1;
+	 }
+	 else if (nType == typeRESET)
+	 {
+		 s1.Format(_T("%d %d RESET %s"), nCurPosY, nCurPosX, sCellName);
+		 DbgLog(s1);
+		 sProgSec.AppendFormat(_T("RESET %s\r\n"), sCellName);
+		 theprog.nOpType1 = OP_RESET;
+		 theprog.nParamCount = 1;
+		 pDoc->TxtToCoilType(sCellNameA, &nCoilType, &CoilAddr);
+		 theprog.Params[0].nParamType = nCoilType;
+		 theprog.Params[0].nParamAddr = CoilAddr;
+		 theprog.Params[0].sParamStr = sCellNameA;
+		 progsec.Append(theprog);
+		 nSteps += 1;
+		 nNextX = 1;
+	 }
+	 else if (nType == typeCMP)
+	 {
+		 s1.Format(_T("%d %d CMP %s %s %s"), nCurPosY, nCurPosX, sCellName, Cells[nCurPosY][nCurPosX + 1].sParam, Cells[nCurPosY][nCurPosX + 2].sParam);
+		 DbgLog(s1);
+		 theprog.nOpType1 = OP_ST_GT;
+		 theprog.nParamCount = 1;
+		 progsec.Append(theprog);
+		 sProgSec.AppendFormat(_T("CMP %s %s %s \r\n"), sCellName, Cells[nCurPosY][nCurPosX + 1].sParam, Cells[nCurPosY][nCurPosX + 2].sParam);
+		 nSteps += 1;
+		 nNextX = 3;
+	 }
+	 else if (nType == typeTM)
+	 {
+		 s1.Format(_T("%d %d TM %s %d %s"), nCurPosY, nCurPosX, sCellName, Cells[nCurPosY][nCurPosX + 1].sParam, Cells[nCurPosY][nCurPosX + 2].sParam);
+		 DbgLog(s1);
+		 theprog.nOpType1 = OP_TMX;
+		 theprog.nParamCount = 1;
+		 pDoc->TxtToCoilType(sCellNameA, &nCoilType, &CoilAddr);
+		 theprog.Params[0].nParamType = nCoilType;
+		 theprog.Params[0].nParamAddr = CoilAddr;
+		 theprog.Params[0].sParamStr = sCellNameA;
+		 progsec.Append(theprog);
+		 sProgSec.AppendFormat(_T("TM %s %d %s\r\n"), sCellName, Cells[nCurPosY][nCurPosX + 1].sParam, Cells[nCurPosY][nCurPosX + 2].sParam);
+		 nSteps += 1;
+		 nNextX = 3;
+	 }
+	 else if (nType == typeFN1)
+	 {
+		 s1.Format(_T("%d %d FN1 %s %s"), nCurPosY, nCurPosX, sCellName, Cells[nCurPosY][nCurPosX + 1].sParam);
+		 DbgLog(s1);
+		 theprog.nOpType1 = OP_INC;
+		 progsec.Append(theprog);
+		 sProgSec.AppendFormat(_T("FN1 %s %s\r\n"), sCellName, Cells[nCurPosY][nCurPosX + 1].sParam);
+		 nSteps += 1;
+		 nNextX = 2;
+	 }
+	 else if (nType == typeFN2)
+	 {
+		 s1.Format(_T("%d %d FN2 %s %s %s "), nCurPosY, nCurPosX, sCellName, Cells[nCurPosY][nCurPosX + 1].sParam, Cells[nCurPosY][nCurPosX + 2].sParam);
+		 DbgLog(s1);
+		 theprog.nOpType1 = OP_MV;
+		 progsec.Append(theprog);
+		 sProgSec.AppendFormat(_T("FN2 %s %s %s \r\n"), sCellName, Cells[nCurPosY][nCurPosX + 1].sParam, Cells[nCurPosY][nCurPosX + 2].sParam);
+		 nSteps += 1;
+		 nNextX = 3;
+	 }
+	 else if (nType == typeFN3)
+	 {
+		 s1.Format(_T("%d %d FN3 %s %s %s %s"), nCurPosY, nCurPosX, sCellName, Cells[nCurPosY][nCurPosX + 1].sParam, Cells[nCurPosY][nCurPosX + 2].sParam, Cells[nCurPosY][nCurPosX + 3].sParam);
+		 DbgLog(s1);
+		 theprog.nOpType1 = OP_ADD3;
+		 progsec.Append(theprog);
+		 sProgSec.AppendFormat(_T("FN3 %s %s %s %s\r\n"), sCellName, Cells[nCurPosY][nCurPosX + 1].sParam, Cells[nCurPosY][nCurPosX + 2].sParam, Cells[nCurPosY][nCurPosX + 3].sParam);
+		 nSteps += 1;
+		 nNextX = 4;
+	 }
+	 else
+	 {
+		 nNextX = 1;
+		 //continue;
+	 }
+
+#pragma endregion
+}
+*/
+
+/// <summary>
+/// 鍏ㄩ儴鍥惧舰鏍¢獙
+/// </summary>
+/// <returns></returns>
+/// <remark>
+/// 璁″垝鍦ㄧ涓�涓繑鍥炲�间腑浣跨敤涓嶅悓鐨勯敊璇唬鐮佷唬琛ㄤ笉鍚岀殑閿欒绫诲瀷锛�
+/// 骞跺湪绗簩涓繑鍥炲�间腑濉厖閿欒鎻愮ず淇℃伅
+/// </remark>
+std::pair<int, CString> CMTerm1View::LDSCheckRule()
+{
+	CString message;
+	int nDivCount = 0;
+	int Divs[100] = { 0 };
+	//浠ヨ涓哄崟浣嶄粠涓婂埌涓嬮亶鍘嗭紙琛岄亶鍘嗭級
+	for (int i = 0; i < m_nTotalRow; i++)	
+	{
+		int nRow = i;
+		int bConnected = 0;
+		bool checkFlag = false;//0鍒楁楠岀粨鏋�
+
+		//鏈鐨�0鍒楁鏌�
+		if (Cells[nRow][0].nType == 0)
+		{
+			checkFlag = true;
+		}
+
+		//閬嶅巻姝よ鐨�1-15鍒楋紙纭畾鍗曞厓鏍硷級
+		for (int j = 1; j < m_CellPerLine; j++)
+		{
+			int nCol = j;
+			//濡傛灉0鍒椾负绌�
+			if (checkFlag)
+			{
+				//鏈夊乏渚т笂绔栫嚎
+				if (Cells[nRow][nCol].bLeftLineUp)
+				{
+					//濡傛灉涓婂眰涓虹┖鍗曞厓鏍硷紝鏃犳晥绔栫嚎锛岀洿鎺ュ垹闄�-----鍙互鍗曠嫭鍒ゆ柇
+					if (nRow - 1 >= 0 && Cells[nRow-1][nCol].nType == 0)
+					{
+						//娓呯悊鍗曞厓鏍�
+						Cells[nRow][nCol].clear();
+					}
+					//濡傛灉鏄痭one锛岄敊璇細鐭矾鎴栧洖璺�
+					else if (Cells[nRow][nCol].nType == 0)
+					{
+						message.Format(_T("((%d,%d) 闄勮繎浣嶇疆浜х敓瑙︾偣鐭矾鎴栧洖璺紒"),nRow,nCol);
+						return std::make_pair(111, message);
+					}
+					//濡傛灉涓嶄负none銆傚彲浠ヨ浆鎹紝浣嗘槸鎻愮ず锛氭棤娉曠粯鍒跺浘褰紙绋嬪簭涓嶅悎鐞嗭級
+					if (Cells[nRow][nCol].nType != 1)
+					{
+						message.Format(_T("((%d,%d) 鏃犳硶缁樺埗鍥惧舰锛堢▼搴忎笉鍚堢悊锛�"), nRow, nCol);
+						return std::make_pair(111, message);
+					}
+
+				}
+				//娌℃湁宸︿晶涓婄珫绾匡紝涓旀鍗曞厓鏍肩被鍨� 涓嶄负绌烘垨绾�
+				else if (Cells[nRow][nCol].nType > 2)
+				{
+					//姝ゅ崟鍏冩牸涓哄崟鐙殑绾垮湀鎴栨寚浠ゅ崟鍏冿紝闇�瑕佽Е鐐硅緭鍏�
+					message.Format(_T("((%d,%d) 闇�瑕佽Е鐐硅緭鍏�"), nRow, nCol);
+					return std::make_pair(111, message);
+				}
+				//鏈夊乏渚т笅绔栫嚎锛屼笖姝ゅ崟鍏冩牸绫诲瀷涓虹┖
+				if (Cells[nRow][nCol].bLeftLineDn)
+				{
+					//涓嶆敮鎸佸洖璺�
+				}
+			}
+			//濡�0鍒楅潪绌�
+			else
+			{
+
+			}
+		}
+
+
+	}
+	return std::make_pair(0,message);
+}

--
Gitblit v1.9.1