⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 muldoc.cpp

📁 VC++ 写的PLC上位机编程软件。非常有用!
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// MULDoc.cpp : implementation of the CMULDoc class
//

#include "stdafx.h"
#include "MUL.h"
#include "Syntax.h"
#include "MULDoc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern int MaxRows;
extern int MaxCols;
/////////////////////////////////////////////////////////////////////////////
// CMULDoc

IMPLEMENT_DYNCREATE(CMULDoc, CDocument)

BEGIN_MESSAGE_MAP(CMULDoc, CDocument)
	//{{AFX_MSG_MAP(CMULDoc)
	ON_COMMAND(ID_COMPILE_INSTR, OnCompileInstr)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMULDoc construction/destruction

CMULDoc::CMULDoc()
{
	// TODO: add one-time construction code here
	err_dlg=NULL;
	Rows=0;
	Cols=0;
}

CMULDoc::~CMULDoc()
{
	Code_Array.RemoveAll();
	Addr_Array.RemoveAll();
	Name_Array.RemoveAll();
}

BOOL CMULDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CMULDoc serialization

void CMULDoc::Serialize(CArchive& ar)
{
	// CEditView contains an edit control which handles all serialization
	((CEditView*)m_viewList.GetHead())->SerializeRaw(ar);
}

/////////////////////////////////////////////////////////////////////////////
// CMULDoc diagnostics

#ifdef _DEBUG
void CMULDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CMULDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMULDoc commands

void CMULDoc::ArrayInit()
{
	Code_Array.RemoveAll();
	Addr_Array.RemoveAll();
	Name_Array.RemoveAll();
}


CPoint CMULDoc::transfer()
{
	int x,y;
	int maxx,maxy;
	int start;
	int j;
	POINTINFO temp;
	CPoint result;
	CPoint value;

	x=0;
	y=0;
	maxx=0;
	maxy=0;
	
	seat.SetAt(m_nstep,CPoint(y,x));
	m_nstep++;
	while (m_nstep<m_nCodecount)
	{
		temp=Code_Array.GetAt(m_nstep);
		switch(temp.code)
		{
		case 0:
			seat.SetAt(m_nstep,CPoint(-3,-3));
			m_nstep++;
			return (CPoint(y,maxy));
		case 1:
		case 2:
			start=m_nstep;
			result=transfer();
			if (m_nstep>=m_nCodecount)
				return (CPoint(y,maxy));
			if (Code_Array.GetAt(m_nstep).code==7) 
			{
				maxx++;
				for (j=start;j<m_nstep;j++)
				{
					value=seat.GetAt(j);
					if (value.y!=-1 && value.y!=-2)
					{
						value.y+=maxx;
						value.x+=y;
						seat.SetAt(j,value);
					}
				}
				maxx+=result.x;
				if (maxy<result.y)
					maxy=result.y;
			}
			if (Code_Array.GetAt(m_nstep).code==8)
			{
				maxy++;
				for (j=start;j<m_nstep;j++)
				{
					value=seat.GetAt(j);
					if (value.x!=-1 && value.x!=-2)
					{
						value.x+=maxy;
						seat.SetAt(j,value);
					}
				}
				maxy+=result.y;
				if (maxx<result.x)
					maxx=result.x;
			}
			break;
		case 3:
		case 4:
			maxy++;
			seat.SetAt(m_nstep,CPoint(maxy,x));
			break;
		case 5:
		case 6:
			maxx++;
			seat.SetAt(m_nstep,CPoint(y,maxx));
			break;
		case 7:
			seat.SetAt(m_nstep,CPoint(-1,-1));
			return(CPoint(maxx,maxy));
		case 8:
			seat.SetAt(m_nstep,CPoint(-2,-2));
			return(CPoint(maxx,maxy));
		case 9:
		case 10:
		case 11:
		case 12:
		case 13:
		case 14:
		case 15:
		case 16:
		case 17:
		case 18:
		case 19:
		case 20:
		case 21:
			seat.SetAt(m_nstep,CPoint(y,maxx+1));
			y++;
			break;
		default:
			break;
		}
		m_nstep++;
	}
	return (CPoint(y,maxy));
}

int CMULDoc::CodeToSeat()
{
	CString str;
	int errflag;
	int j;
	seat.RemoveAll();
	m_nCodecount=Code_Array.GetSize();
	for (j=0;j<m_nCodecount;j++)
		seat.Add(CPoint(0,0));

	errflag=0;
	try{

		CPoint result;
		CPoint temp;
		m_nstep=0;
		int row=0;
		int startpoint;
		while (m_nstep<m_nCodecount)
		{
			startpoint=m_nstep;
			result=transfer();
			for (int j=startpoint;j<m_nstep;j++)
			{
				temp=seat.GetAt(j);
				if (temp.x!=-1 && temp.x!=-2 && temp.x!=-3)
				temp.x+=row;
				seat.SetAt(j,temp);
			}
			if (result.x>result.y)
				row+=result.x;
			else
				row+=result.y+1;
		}
	}
	catch(...)
	{
		errflag=1;
		POSITION pos=this->GetFirstViewPosition();
		CView * pView=this->GetNextView(pos);
		pView->MessageBox("将指令转换成结点地址时出错!请与开发商联系。",
			"编译出错",MB_ICONERROR|MB_OK);
	}
/*	for (j=0;j<m_nCodecount;j++)
	{
		CPoint point;
		point=seat.GetAt(j);
		str.Format("row=%d\ncol=%d",point.x,point.y);
		AfxMessageBox(str);
	}*/
	return errflag;
}

CPoint CMULDoc::GetRowCol()
{
	int rows;
	int cols;
	CPoint temp;
	rows=10;
	cols=12;
	for (int i=0;i<seat.GetSize();i++)
	{
		temp=seat.GetAt(i);
		if (temp.x+1>rows)
			rows=temp.x+1;
		if (temp.y+1>cols)
			cols=temp.y+1;
	}
	return (CPoint(rows,cols));
}

void CMULDoc::AddERECT(int row, int col)
{
	IOPOINT *point;
	do
	{
		point=new ERECT;
		point->setfiveflag(5);
		point->setcode("ERECT");
		point->setwhere(row,col);
		m_mPlc.insert_point(point);
		row--;
	}while(row>0 && (!m_mPlc.testpoint(row,col) 
		|| (m_mPlc.testpoint(row,col) 
		&& m_mPlc.getpoint(row,col)->getflag()==0
		&& m_mPlc.getpoint(row,col)->getfiveflag()!=5)));
}

void CMULDoc::AddHOR(int row, int col, int count)
{
	IOPOINT *point;
	for (int i=0;i<count;i++)
	{
		point=new HOR;
		point->setflag(6);
		point->setcode("HOR");
		point->setwhere(row,col+i);
		m_mPlc.insert_point(point);
	}
}

int CMULDoc::FindPort(int i,CPoint &point1, CPoint &point2)
{
	int count;
	POINTINFO temp;
	CPoint point_temp;
	count=0;
	point2=CPoint(0,0);
	while (count!=1)
	{
		temp=Code_Array.GetAt(i);
		if (temp.code==7 || temp.code==8)
			count--;
		else
		{
			point_temp=seat.GetAt(i);
			if (point_temp.y>point2.y)
				point2=point_temp;
		}
		if (temp.code==1 || temp.code==2)
			count++;
		i--;
	}
	point1=seat.GetAt(++i);
	return i;
}

int CMULDoc::preCompile(CString &errMessage)
{
	CString CodeSet[20]={
		";***","LD","LDI","OR","ORI","AND","ANI","ANB","ORB","OUT",
		"SET","RST","S","R","MC","MCR","CJP","EJP","PLS","END"};

	char  line[255];
	CString code;
	CString addr;
	CString name;
	FILE  *stream;
	CString fname;
	int result;


	ArrayInit();
	result=0;

	fname=this->GetPathName();
	if( (stream = fopen( fname, "r" )) != NULL )  
	{
		POINTINFO Info_Temp;
		BOOL HaveFlag;
		BOOL errFlag;
		CString errmsg;
		int count=1;
		int lcount=0;
		int bcount=0;
		int dcount=0;
		int i;

		while( fgets( line, 250, stream ) != NULL)         
		{
			HaveFlag=TRUE;
			errFlag=FALSE;

			code.GetBuffer(255);
			addr.GetBuffer(255);
			name.GetBuffer(255);

			sscanf(line,"%s %s %s",code,addr,name);

			code.ReleaseBuffer(4);
			addr.ReleaseBuffer(4);
			name.ReleaseBuffer(9);
 
			if (code!="")
			{
				code.MakeUpper();
				for (i=0;i<20;i++)
				{
					if (code==CodeSet[i]) 
					{
						Info_Temp.code=i;
						if (i==1 || i==2)
							lcount++;
						if (i==7 || i==8)
							bcount++;
						if (i==0)
						{	
							dcount++;

							if (!(lcount==0 && bcount==0) && lcount!=bcount+1)
							{
								result=1;
								errmsg.Format("第 %d 程序段LD,ANB不匹配!",dcount);
								errMessage+=errmsg;
							}
							lcount=0;
							bcount=0;
						}
						break;
					}
				}
				if (i==20)
				{
					if (code.GetAt(0)=='K')
						Info_Temp.code=20;
					else if (code.GetAt(0)=='F')
						Info_Temp.code=21;
					else if (code.GetAt(0)!=';')
					{
						HaveFlag=FALSE;
						errFlag=TRUE;
					}
					else 
						HaveFlag=FALSE;
				}
				if (HaveFlag)
				{
					if (name!="")
					{
						name=name.Right(name.GetLength()-1);
						name.TrimRight();
					}
					if (name!="")
						Info_Temp.name=Name_Array.Add(name);
					else

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -