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

📄 mshowdoc.cpp

📁 用VC++开发的一个数控加工的仿真程序。此程序能根据NC代码仿真生成加工轨迹
💻 CPP
字号:
// MshowDoc.cpp : implementation of the CMshowDoc class
//

#include "stdafx.h"
#include "Mshow.h"

#include "MainFrm.h"
#include "MshowDoc.h"
#include "MshowView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMshowDoc

IMPLEMENT_DYNCREATE(CMshowDoc, CDocument)

BEGIN_MESSAGE_MAP(CMshowDoc, CDocument)
	//{{AFX_MSG_MAP(CMshowDoc)
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMshowDoc construction/destruction

CMshowDoc::CMshowDoc()
{
	// TODO: add one-time construction code here
	Init();
	
}

CMshowDoc::~CMshowDoc()
{
}

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

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

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CMshowDoc serialization

void CMshowDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CMshowDoc diagnostics

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

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

/////////////////////////////////////////////////////////////////////////////
// CMshowDoc commands

void CMshowDoc::OnFileOpen() 
{
	
	CFileDialog dlg(TRUE);///TRUE为OPEN对话框,FALSE为SAVE AS对话框
    
	
	if(dlg.DoModal()==IDOK)
	{
		int listindex;
		listindex = ((CListBox *)(((CMainFrame*)AfxGetMainWnd())->m_wndDialogBar.GetDlgItem(IDC_LIST)))->GetCount();
		if (listindex) 
		{
			for(int i=0;i<listindex;i++)
				((CListBox *)(((CMainFrame*)AfxGetMainWnd())->m_wndDialogBar.GetDlgItem(IDC_LIST)))->DeleteString(i);
		}


		(((CMainFrame*)AfxGetMainWnd())->m_wndDialogBar).GetDlgItem(IDC_STATIC_SIM)->SetWindowText("未建立");
       FileName=dlg.GetFileName();
       fp=fopen(FileName,"r");  //只读方式
	   Read();    //读取代码
	   fclose(fp);
	   List();
	}
}
int CMshowDoc::Read()
{  
//	char cha;
	
	
	
	int SectErr = 0,SenErr = 0;

	char GSen[50];

	char * pGSen;

	
	CodeNum=-1;

	Init();

	do 
	{
		pGSen = fgets(GSen,50,fp); // 从fp所指的文件中读出n-1个字符送入字符数组GSen中
		                           // fp 输入的数控代码

		if (pGSen == NULL)
			break;
		SectErr = CheckSect(GSen);  
		
		if ( SectErr != 0)
		{
			AfxMessageBox(bf);
			return 1;
		}
        SenErr = CheckSen(GSen);
		
		if ( SenErr != 0)
		{
			sprintf(bf,"Line %d ",CodeLine+1);
			AfxMessageBox(bf);
			return 1;
		}
		
	} while(pGSen != NULL);

	CodeNum +=1;

	return 0;
}

int CMshowDoc::CheckSect( char* GSen)   //检查
{
	int  fd=0;

	
	for (int i =0;GSen[i];i++)
	{
		
		switch (GSen[i])
		{
			case 'n':
			case 'N':
				if (nflag ==1)
				{
					sprintf(bf,"Line %d N error",CodeLine+1);
					return 1;
				}
				nflag = 1;
				CodeNum+=1;


				fd = atoi(&GSen[i+1]);

				if (fd<0||fd>999)
				{
					sprintf(bf,"Line %d N error",CodeLine+1);
					return 1;
				}
				GCode[CodeNum].n=fd;
				 break;

			case 'g':
			case 'G':
				if (GSen[i+1]<'0' || GSen[i+1] >'9')
				{
					sprintf(bf,"Line %d G error",CodeLine+1);
					return 1;
				}

				fd = atoi(&GSen[i+1]);
				if (fd<0||fd>99)
				{
					sprintf(bf,"Line %d G error",CodeLine+1);
					return 1;
				}
				switch (fd)
				{
					
					case 90:
						G91Sign=0;	//absolute  绝对值输入
						break;
					case 91:
						G91Sign=1;  //relate  增量值输入
						break;
					case 00:   /*line fastmove*/		
					case 01:   /*line interloation*/
					case 02:   /* CW */
					case 03:   /* CCW */
						if (GCode[CodeNum].gflag == 1)
						{
							sprintf(bf,"Line %d G error",CodeLine+1);
//							AfxMessageBox(bf);
							return 1;
						}
						GCode[CodeNum].gflag = 1;
						GCode[CodeNum].g=fd;
						break;				
					
					default:
						break;
				}
				 break;

			case 'x':
		    case 'X': 				
			    if (xflag == 1)
				{
					sprintf(bf,"Line %d X error",CodeLine+1);
//					AfxMessageBox(bf);
					return 1;
				}
				xflag = 1;

				if ( !((GSen[i+1]>='0') && (GSen[i+1] <='9') || (GSen[i+1] =='-')))
				{
					sprintf(bf,"Line %d X error",CodeLine+1);
					return 1;
				}
				fd = atoi(&GSen[i+1]);
				if (fd<X_MIN||fd>X_MAX)
				{
					sprintf(bf,"Line %d X error",CodeLine+1);
//					AfxMessageBox(bf);
					return 1;
				}
		
				if(G91Sign==1)
					GCode[CodeNum].x=fd;				
				else
					GCode[CodeNum].x=fd-Last_x;
				Last_x += GCode[CodeNum].x;
								
			
				break;

			case 'y':
			case 'Y': 				
			   /* Y_do();*/
				if (yflag == 1)
				{
					sprintf(bf,"Line %d Y error",CodeLine+1);
//					AfxMessageBox(bf);
					return 1;
				}
				yflag = 1;
				if (!((GSen[i+1]>='0') && (GSen[i+1] <='9') || (GSen[i+1] =='-')))
				{
					sprintf(bf,"Line %d X error",CodeLine+1);
					return 1;
				}
				fd = atoi(&GSen[i+1]);
				if (fd<Y_MIN||fd>Y_MAX)
				{
					sprintf(bf,"Line %d Y error",CodeLine+1);
//					AfxMessageBox(bf);
					return 1;
				}
				if(G91Sign==1)
					GCode[CodeNum].y=fd;				
				else
					GCode[CodeNum].y=fd-Last_y;
				
				Last_y +=GCode[CodeNum].y;
					
				break;

			case 'z':
			case 'Z': 
				if (zflag == 1)
				{
					sprintf(bf,"Line %d Z error",CodeLine+1);
//					AfxMessageBox(bf);
					return 1;
				}
				zflag =1;
				if (!((GSen[i+1]>='0') && (GSen[i+1] <='9') || (GSen[i+1] =='-')))
				{
					sprintf(bf,"Line %d Z error",CodeLine+1);
					return 1;
				}
				fd = atoi(&GSen[i+1]);
				if (fd<Z_MIN||fd>Z_MAX)
				{
					sprintf(bf,"Line %d Z error",CodeLine+1);
//					AfxMessageBox(bf);
					return 1;
				}
				if(G91Sign==1)
					GCode[CodeNum].z=fd;				
				else
					GCode[CodeNum].z=fd-Last_z;
					
				Last_z +=GCode[CodeNum].z;
				break;

			case 'i':
			case 'I': 				
			   /* I_do();*/
				if (iflag == 1)
				{
					sprintf(bf,"Line %d I error",CodeLine+1);
//					AfxMessageBox(bf);
					return 1;
				}
				iflag = 1;

				if (!((GSen[i+1]>='0') && (GSen[i+1] <='9') || (GSen[i+1] =='-')))
				{
					sprintf(bf,"Line %d I error",CodeLine+1);
					return 1;
				}
				fd = atoi(&GSen[i+1]);
				GCode[CodeNum].i = fd;
				break;
			
			case 'j':
			case 'J': 				
			    /*J_do();*/
				if (jflag == 1)
				{
					sprintf(bf,"Line %d J error",CodeLine+1);
//					AfxMessageBox(bf);
					return 1;
				}
				jflag = 1;
				if (!((GSen[i+1]>='0') && (GSen[i+1] <='9') || (GSen[i+1] =='-')))
				{
					sprintf(bf,"Line %d J error",CodeLine+1);
					return 1;
				}
				fd = atoi(&GSen[i+1]);
				GCode[CodeNum].j = fd;
				break;

			case 'k':
			case 'K': 				
			   /* K_do();*/
				if (kflag == 1)
				{
					sprintf(bf,"Line %d K error",CodeLine+1);
//					AfxMessageBox(bf);
					return 1;
				}
				kflag = 1;
				if (!((GSen[i+1]>='0') && (GSen[i+1] <='9') || (GSen[i+1] =='-')))
				{
					sprintf(bf,"Line %d X error",CodeLine+1);
					return 1;
				}
				fd = atoi(&GSen[i+1]);
				GCode[CodeNum].k = fd;
				break;
			
			case 'r':	
			case 'R': 				
			    /*R_do();*/
				break;

			case 's':
			case 'S': 
				
			/*	S_do();*/
				if (GCode[CodeNum].sflag == 1)
				{
					sprintf(bf,"Line %d S error",CodeLine+1);
//					AfxMessageBox(bf);
					return 1;
				}
				GCode[CodeNum].sflag = 1;
				if ((GSen[i+1]<'0') || (GSen[i+1] >'9') )
				{
					sprintf(bf,"Line %d S error",CodeLine+1);
					return 1;
				}
				fd = atoi(&GSen[i+1]);

				if (fd>S_MAX)
				{
					sprintf(bf,"Line %d S error",CodeLine+1);
//					AfxMessageBox(bf);
					return 1;
				}
				GCode[CodeNum].s = fd;
				 break;

			case 'f':
			case 'F':  
				/*F_do();*/
				if (GCode[CodeNum].fflag == 1)
				{
					sprintf(bf,"Line %d F error",CodeLine+1);
//					AfxMessageBox(bf);
					return 1;
				}

				GCode[CodeNum].fflag = 1;
				if ((GSen[i+1]<'0') || (GSen[i+1] >'9') )
				{
					sprintf(bf,"Line %d F error",CodeLine+1);
					return 1;
				}
				fd = atoi(&GSen[i+1]);
				
				if (fd>F_MAX)
				{
					sprintf(bf,"Line %d F error",CodeLine+1);
//					AfxMessageBox(bf);
					return 1;
				}

				GCode[CodeNum].f = fd;

				break;

			case 'm':
	   	    case 'M': 
			/*	M_do();*/
				if (fd<0||fd>99)
				{
					sprintf(bf,"Line %d M error",CodeLine+1);
//					AfxMessageBox(bf);
					return 1;
				}
				GCode[CodeNum].mflag = 1;
				fd = atoi(&GSen[i+1]);
				GCode[CodeNum].m = fd;
				break;

			case ' ':   /*Space*/
				break;	
				
			
			case '\n':

				
				break;
				
			
			case EOF:
				break;   /*End Of File*/	
			
			default:
			   break;
		}
		
	}


	return 0;


}

int CMshowDoc::CheckSen(char * GSen)
{
	if (CodeNum !=0)			
	{
		if (GCode[CodeNum].n <= GCode[CodeNum-1].n )		// Check N number inc
			return 1;
		if ((xflag || yflag || zflag)&& (GCode[CodeNum-1].gflag == 1)&& (GCode[CodeNum].gflag == 0) )
		{
			GCode[CodeNum].g = GCode[CodeNum-1].g;		// G mode trans
			GCode[CodeNum].gflag = 1;
		}
	}

	CodeLine++;
	nflag = xflag = yflag = zflag = iflag = jflag = kflag = 0;
	
		



	return 0;
}



void CMshowDoc::Init()
{
	for (int i=0;i<50;i++)
		memset(&GCode[i],0,sizeof(GCODE));

	G91Sign=0;
	
	nflag = xflag = yflag = zflag = iflag = jflag = kflag = 0;
	CodeLine = 0;
	Last_x = Last_y = Last_z = 0;
}

void CMshowDoc::List()
{
    CStdioFile myinputfile;
	BOOL isend=TRUE;
	myinputfile.Open(FileName,CFile::modeRead| CFile::typeText );
	CString mystring[100];
	int number=0;
	while (isend)
	{
			isend = myinputfile.ReadString(mystring[number]);
			if (isend) 
			{
				number++;
			}
			else
				number--;
	}
	myinputfile.Close();

    CMshowView *pMshowview = (CMshowView*)((CMainFrame*)AfxGetMainWnd())->GetActiveView();
    
	((CListBox *)(((CMainFrame*)AfxGetMainWnd())->m_wndDialogBar.GetDlgItem(IDC_LIST)))->ResetContent();
	for(int i=0;i<=number;i++)
	{
		((CListBox *)(((CMainFrame*)AfxGetMainWnd())->m_wndDialogBar.GetDlgItem(IDC_LIST)))->InsertString(i,mystring[i]);
	}
}

⌨️ 快捷键说明

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