- #include
- using namespace std;
- class CNC //CNC控制類
- {
- //私有方法部分
- protected:
- int CNC_x;//x軸當前值
- int CNC_y;//y軸當前值
- int CNC_Z;//z軸當前值
- int CNC_YS_X;//原點x坐標值
- int CNC_YS_Y;//原點y坐標值
- int CNC_YS_Z;//原點z坐標值
- //屏幕GDI函數用到的一些坐標
- int CNC_GDI_X; //屏幕顯示偏移坐標
- int CNC_GDI_Y;
- int CNC_GDI_X_x; //x框架軸坐標
- int CNC_GDI_X_y;
- int CNC_GDI_Y_x; //y軸框架坐標
- int CNC_GDI_Y_y;
- int CNC_GDI_Z_x; //z軸框架坐標
- int CNC_GDI_Z_y;
- int CNC_GDI_D_x; /具位置坐標
- int CNC_GDI_D_y;
- int CNC_GDI_D_Z; //下刀深度
- //GDI函數用到的一些句柄
- HDC hdc; //獲取繪圖DC句柄
- HPEN hpen_x; //x軸畫筆
- HBRUSH hbrush_x; //x軸畫刷
- HPEN hpen_y; //y軸畫筆
- HBRUSH hbrush_y; //y軸畫刷
- HPEN hpen_z; //z軸畫筆
- HBRUSH hbrush_z; //z軸畫刷
- HPEN hpen_D; /路畫筆
- HPEN hpen_cls; //清屏畫筆
- HBRUSH hbrush_cls; //清屏畫刷
- HPEN hpen_GJ; //工件畫筆
- HBRUSH hbrush_GJ; //工件畫刷
- //公共方法部分
- public:
- CNC(void);
- ~CNC(void);
- void CNC_GDI_DC(void); //初始化GDI繪圖相關句柄
- void CNC_GDI_CLS(void); //清屏并畫工件
- void CNC_GDI(void); //屏幕輸出CNC狀態
- void CNC_X_進(void); //x軸操作
- void CNC_X_退(void);
- void CNC_Y_進(void); //y軸操作
- void CNC_Y_退(void);
- void CNC_Z_進(void); //z軸操作
- void CNC_Z_退(void);
- void CNC_COM(void); //COM口驅動信號輸出
- void CNC_DuiDao(void); //手動對刀設置原點
- int CNC_G00(char x,int x_z,char y,int y_z,char z,int z_z); //G00指令實現
- int CNC_G01(char x,int x_z,char y,int y_z,char z,int z_z,int f); //G01指令實現
- };
- #include "stdafx.h"
- #include "CNC控制.h"
- int CNC_GDI_DL[440][780];
- CNC::CNC(void)
- {
- //初始化
- //屏幕顯示參考坐標,整個三軸都以此點為偏移
- CNC_GDI_X=30;
- CNC_GDI_Y=20;
- //為x,y,z軸坐標值及刀具位置坐標賦初值
- CNC_GDI_X_x=CNC_GDI_X; //x軸寬度為900像素
- CNC_GDI_X_y=CNC_GDI_Y;
- CNC_GDI_Y_x=CNC_GDI_X_x+10; //y軸寬度為500像素
- CNC_GDI_Y_y=CNC_GDI_X_y-10;
- CNC_GDI_Z_x=CNC_GDI_Y_x-10; //z軸深度屏幕上無法表示出來
- CNC_GDI_Z_y=CNC_GDI_Y_y+20;
- CNC_GDI_D_x=CNC_GDI_Z_x+35; /具位置坐標
- CNC_GDI_D_y=CNC_GDI_Z_y+25;
- CNC_GDI_D_Z=0; //下刀深度初始化
- int x,y;
- for(y=0;y<440;y++)
- for(x=0;x<780;x++)
- {
- CNC_GDI_DL[y][x]=0;
- }
- //////////////////////////////////////
- //初始化原點
- CNC_YS_X=CNC_GDI_D_x;
- CNC_YS_Y=CNC_GDI_D_y;
- CNC_YS_Z=CNC_GDI_D_Z;
- }
- CNC::~CNC(void)
- {
- // 釋放資源
- DeleteObject(hpen_x);
- DeleteObject(hbrush_x);
- DeleteObject(hpen_y);
- DeleteObject(hbrush_y);
- DeleteObject(hpen_z);
- DeleteObject(hbrush_z);
- DeleteObject(hpen_x);
- DeleteObject(hpen_D);
- DeleteObject(hpen_x);
- }
- void CNC::CNC_GDI_CLS(void)
- {
- // 為DC選擇筆和筆刷
- SelectObject(hdc, hpen_cls);
- SelectObject(hdc, hbrush_cls);
- // 繪制清屏矩形
- Rectangle( hdc,0,0,900,550 );
- // 為DC選擇筆和筆刷
- SelectObject(hdc, hpen_GJ);
- SelectObject(hdc, hbrush_GJ);
- // 繪制工件矩形
- Rectangle( hdc,80,60,800,500 );
- //繪制刀路
- int x,y;
- if(CNC_GDI_D_Z>0&&CNC_GDI_D_y>=60&&CNC_GDI_D_x>=70&&CNC_GDI_D_y<=500&&CNC_GDI_D_x<=850)
- {
- //記錄刀路
- CNC_GDI_DL[CNC_GDI_D_y-60][CNC_GDI_D_x-70]=CNC_GDI_D_Z;
- }
- for(y=0;y<440;y++)
- for(x=0;x<780;x++)
- {
- if(CNC_GDI_DL[y][x]!=0)
- {
- SetPixel (hdc, x+70, y+60,RGB(0,0,255));
- }
- }
- }
- void CNC::CNC_GDI_DC(void)
- {
- //初始化繪圖DC
- CWnd * pWnd = AfxGetMainWnd();
- HWND haha = pWnd->m_hWnd;
- hdc = GetDC(haha); //這個DC為全局的
- hpen_x=CreatePen(PS_SOLID,1, RGB(255,255,0));
- hbrush_x=CreateSolidBrush(RGB(255,255,0));
- hpen_y=CreatePen(PS_SOLID,1, RGB(0,255,0));
- hbrush_y=CreateSolidBrush(RGB(0,255,0));
- hpen_z=CreatePen(PS_SOLID,1, RGB(255,0,0));
- hbrush_z=CreateSolidBrush(RGB(255,0,0));
- hpen_D=CreatePen(PS_SOLID,1, RGB(0,0,255));
- hpen_cls=CreatePen(PS_SOLID,1, RGB(0,0,0));
- hbrush_cls=CreateSolidBrush(RGB(0,0,0));
- hpen_GJ=CreatePen(PS_SOLID,1, RGB(128,255,255));
- hbrush_GJ=CreateSolidBrush(RGB(128,255,255));
- }
- void CNC::CNC_GDI(void)
- {
- CNC_GDI_CLS();
- // 為DC選擇筆和筆刷
- SelectObject(hdc, hpen_x);
- SelectObject(hdc, hbrush_x);
- // 繪制x軸框架
- Rectangle( hdc,CNC_GDI_X_x,CNC_GDI_X_y,CNC_GDI_X_x+900,CNC_GDI_X_y+10 );
- Rectangle( hdc,CNC_GDI_X_x,CNC_GDI_X_y+510,CNC_GDI_X_x+900,CNC_GDI_X_y+10+510 );
- ///////////////////////////////////////////////////
- // 為DC選擇筆和筆刷
- SelectObject(hdc, hpen_y);
- SelectObject(hdc, hbrush_y);
- // 繪制y軸框架
- Rectangle( hdc,CNC_GDI_Y_x,CNC_GDI_Y_y,CNC_GDI_Y_x+10,CNC_GDI_Y_y+540 );
- Rectangle( hdc,CNC_GDI_Y_x+40,CNC_GDI_Y_y,CNC_GDI_Y_x+10+40,CNC_GDI_Y_y+540 );
- ///////////////////////////////////////////////////
- // 為DC選擇筆和筆刷
- SelectObject(hdc, hpen_z);
- SelectObject(hdc, hbrush_z);
- // 繪制z軸框架
- Rectangle( hdc,CNC_GDI_Z_x,CNC_GDI_Z_y,CNC_GDI_Z_x+70,CNC_GDI_Z_y+10 );
- Rectangle( hdc,CNC_GDI_Z_x,CNC_GDI_Z_y+40,CNC_GDI_Z_x+70,CNC_GDI_Z_y+10+40 );
- Rectangle( hdc,CNC_GDI_Z_x+30,CNC_GDI_Z_y+20,CNC_GDI_Z_x+40,CNC_GDI_Z_y+30);
- }
- void CNC::CNC_COM(void)
- {
- //從串口輸出三軸步進電機驅動信號
- }
- void CNC::CNC_DuiDao(void)
- {
- //手動對刀設置原點
- CNC_YS_X=CNC_GDI_D_x;
- CNC_YS_Y=CNC_GDI_D_y;
- CNC_YS_Z=CNC_GDI_D_Z;
- }
- void CNC::CNC_X_進(void)
- {
- //x軸進給一個單位
- if(CNC_GDI_Y_x<(CNC_GDI_X+840))
- {
- CNC_GDI_Y_x+=1;
- CNC_GDI_Z_x+=1;
- CNC_GDI_D_x+=1;
- CNC_GDI();
- CNC_COM();
- }
- }
- void CNC::CNC_X_退(void)
- {
- //x軸后退一個單位
- if(CNC_GDI_Y_x>CNC_GDI_X+10)
- {
- CNC_GDI_Y_x-=1;
- CNC_GDI_Z_x-=1;
- CNC_GDI_D_x-=1;
- CNC_GDI();
- CNC_COM();
- }
- }
- void CNC::CNC_Y_進(void)
- {
- //y軸進給一個單位
- if(CNC_GDI_Z_y<(CNC_GDI_Y+460))
- {
- CNC_GDI_Z_y+=1;
- CNC_GDI_D_y+=1;
- CNC_GDI();
- CNC_COM();
- }
- }
- void CNC::CNC_Y_退(void)
- {
- //y軸后退一個單位
- if(CNC_GDI_Z_y>(CNC_GDI_Y+10))
- {
- CNC_GDI_Z_y-=1;
- CNC_GDI_D_y-=1;
- CNC_GDI();
- CNC_COM();
- }
- }
- void CNC::CNC_Z_進(void)
- {
- if(CNC_GDI_D_Z<10)
- {
- CNC_GDI_D_Z+=1;
- }
- CNC_GDI();
- CNC_COM();
- }
- void CNC::CNC_Z_退(void)
- {
- if(CNC_GDI_D_Z>0)
- {
- CNC_GDI_D_Z-=1;
- }
- CNC_GDI();
- CNC_COM();
- }
- int CNC::CNC_G00(char x,int x_z,char y,int y_z,char z,int z_z)
- {
- //G00指令實現
- int a,b,c;
- CNC_GDI_D_Z=0;
- for( a=1,b=1,c=1;a==1||b==1||c==1;)
- {
- if((x_z+CNC_YS_X)==CNC_GDI_D_x)
- {
- a=0;
- } else if((x_z+CNC_YS_X)<CNC_GDI_D_x&&a==1)
- {
- CNC_X_退();
- }else if((x_z+CNC_YS_X)>CNC_GDI_D_x&&a==1)
- {
- CNC_X_進();
- }
- if((y_z+CNC_YS_Y)==CNC_GDI_D_y)
- {
- b=0;
- } else if((y_z+CNC_YS_Y)<CNC_GDI_D_y&&b==1)
- {
- CNC_Y_退();
- }else if((y_z+CNC_YS_Y)>CNC_GDI_D_y&&b==1)
- {
- CNC_Y_進();
- }
- if((z_z+CNC_YS_Z)==CNC_GDI_D_Z)
- {
- c=0;
- } else if((z_z+CNC_YS_Z)<CNC_GDI_D_Z&&c==1)
- {
- CNC_Z_退();
- }else if((z_z+CNC_YS_Z)>CNC_GDI_D_Z&&c==1)
- {
- CNC_Z_進();
- }
- Sleep(10);
- }
- return 1;
- }
- int CNC::CNC_G01(char x,int x_z,char y,int y_z,char z,int z_z,int f)
- {
- //G00指令實現
- int a,b,c;
- for( a=1,b=1,c=1;a==1||b==1||c==1;)
- {
- if((x_z+CNC_YS_X)==CNC_GDI_D_x)
- {
- a=0;
- } else if((x_z+CNC_YS_X)<CNC_GDI_D_x&&a==1)
- {
- CNC_X_退();
- }else if((x_z+CNC_YS_X)>CNC_GDI_D_x&&a==1)
- {
- CNC_X_進();
- }
- if((y_z+CNC_YS_Y)==CNC_GDI_D_y)
- {
- b=0;
- } else if((y_z+CNC_YS_Y)<CNC_GDI_D_y&&b==1)
- {
- CNC_Y_退();
- }else if((y_z+CNC_YS_Y)>CNC_GDI_D_y&&b==1)
- {
- CNC_Y_進();
- }
- if((z_z+CNC_YS_Z)==CNC_GDI_D_Z)
- {
- c=0;
- } else if((z_z+CNC_YS_Z)<CNC_GDI_D_Z&&c==1)
- {
- CNC_Z_退();
- }else if((z_z+CNC_YS_Z)>CNC_GDI_D_Z&&c==1)
- {
- CNC_Z_進();
- }
- Sleep(f);
- }
- return 1;
- }
復制代碼
|